| 1 |
Debugging tools |
| 2 |
=============== |
| 3 |
|
| 4 |
pyglet includes a number of debug paths that can be enabled during or before |
| 5 |
application startup. These were primarily developed to aid in debugging |
| 6 |
pyglet itself, however some of them may also prove useful for understanding |
| 7 |
and debugging pyglet applications. |
| 8 |
|
| 9 |
Each debug option is a key in the :py:attr:`pyglet.options` dictionary. |
| 10 |
Options can be set directly on the dictionary before any other modules |
| 11 |
are imported:: |
| 12 |
|
| 13 |
import pyglet |
| 14 |
pyglet.options['debug_gl'] = False |
| 15 |
|
| 16 |
They can also be set with environment variables before pyglet is imported. |
| 17 |
The corresponding environment variable for each option is the string |
| 18 |
``PYGLET_`` prefixed to the uppercase option key. For example, the |
| 19 |
environment variable for ``debug_gl`` is ``PYGLET_DEBUG_GL``. Boolean options |
| 20 |
are set or unset with ``1`` and ``0`` values. |
| 21 |
|
| 22 |
A summary of the debug environment variables appears in the table below. |
| 23 |
|
| 24 |
.. list-table:: |
| 25 |
:header-rows: 1 |
| 26 |
|
| 27 |
* - Option |
| 28 |
- Environment variable |
| 29 |
- Type |
| 30 |
* - ``debug_font`` |
| 31 |
- ``PYGLET_DEBUG_FONT`` |
| 32 |
- bool |
| 33 |
* - ``debug_gl`` |
| 34 |
- ``PYGLET_DEBUG_GL`` |
| 35 |
- bool |
| 36 |
* - ``debug_gl_trace`` |
| 37 |
- ``PYGLET_DEBUG_GL_TRACE`` |
| 38 |
- bool |
| 39 |
* - ``debug_gl_trace_args`` |
| 40 |
- ``PYGLET_DEBUG_GL_TRACE_ARGS`` |
| 41 |
- bool |
| 42 |
* - ``debug_graphics_batch`` |
| 43 |
- ``PYGLET_DEBUG_GRAPHICS_BATCH`` |
| 44 |
- bool |
| 45 |
* - ``debug_lib`` |
| 46 |
- ``PYGLET_DEBUG_LIB`` |
| 47 |
- bool |
| 48 |
* - ``debug_media`` |
| 49 |
- ``PYGLET_DEBUG_MEDIA`` |
| 50 |
- bool |
| 51 |
* - ``debug_trace`` |
| 52 |
- ``PYGLET_DEBUG_TRACE`` |
| 53 |
- bool |
| 54 |
* - ``debug_trace_args`` |
| 55 |
- ``PYGLET_DEBUG_TRACE_ARGS`` |
| 56 |
- bool |
| 57 |
* - ``debug_trace_depth`` |
| 58 |
- ``PYGLET_DEBUG_TRACE_DEPTH`` |
| 59 |
- int |
| 60 |
* - ``debug_win32`` |
| 61 |
- ``PYGLET_DEBUG_WIN32`` |
| 62 |
- bool |
| 63 |
* - ``debug_x11`` |
| 64 |
- ``PYGLET_DEBUG_X11`` |
| 65 |
- bool |
| 66 |
|
| 67 |
|
| 68 |
The ``debug_media`` and ``debug_font`` options are used to debug the |
| 69 |
:py:mod:`pyglet.media` and :py:mod:`pyglet.font` modules, respectively. |
| 70 |
Their behaviour is platform-dependent and useful only for pyglet developers. |
| 71 |
|
| 72 |
The remaining debug options are detailed below. |
| 73 |
|
| 74 |
Debugging OpenGL |
| 75 |
---------------- |
| 76 |
|
| 77 |
The ``debug_graphics_batch`` option causes all |
| 78 |
:py:class:`~pyglet.graphics.Batch` objects to dump their |
| 79 |
rendering tree to standard output before drawing, after any change (so two |
| 80 |
drawings of the same tree will only dump once). This is useful to debug |
| 81 |
applications making use of :py:class:`~pyglet.graphics.Group` and |
| 82 |
:py:class:`~pyglet.graphics.Batch` rendering. |
| 83 |
|
| 84 |
Error checking |
| 85 |
^^^^^^^^^^^^^^ |
| 86 |
|
| 87 |
The ``debug_gl`` option intercepts most OpenGL calls and calls ``glGetError`` |
| 88 |
afterwards (it only does this where such a call would be legal). If an error |
| 89 |
is reported, an exception is raised immediately. |
| 90 |
|
| 91 |
This option is enabled by default unless the ``-O`` flag (optimisation) is |
| 92 |
given to Python, or the script is running from within a py2exe or py2app |
| 93 |
package. |
| 94 |
|
| 95 |
Tracing |
| 96 |
^^^^^^^ |
| 97 |
|
| 98 |
The ``debug_gl_trace`` option causes all OpenGL functions called to be dumped |
| 99 |
to standard out. When combined with ``debug_gl_trace_args``, the arguments |
| 100 |
given to each function are also printed (they are abbreviated if necessary to |
| 101 |
avoid dumping large amounts of buffer data). |
| 102 |
|
| 103 |
Tracing execution |
| 104 |
----------------- |
| 105 |
|
| 106 |
The ``debug_trace`` option enables Python-wide function tracing. This causes |
| 107 |
every function call to be printed to standard out. Due to the large number of |
| 108 |
function calls required just to initialise pyglet, it is recommended to |
| 109 |
redirect standard output to a file when using this option. |
| 110 |
|
| 111 |
The ``debug_trace_args`` option additionally prints the arguments to each |
| 112 |
function call. |
| 113 |
|
| 114 |
When ``debug_trace_depth`` is greater than 1 the caller(s) of each function |
| 115 |
(and their arguments, if ``debug_trace_args`` is set) are also printed. Each |
| 116 |
caller is indented beneath the callee. The default depth is 1, specifying |
| 117 |
that no callers are printed. |
| 118 |
|
| 119 |
Platform-specific debugging |
| 120 |
--------------------------- |
| 121 |
|
| 122 |
The ``debug_lib`` option causes the path of each loaded library to be printed |
| 123 |
to standard out. This is performed by the undocumented ``pyglet.lib`` module, |
| 124 |
which on Linux and Mac OS X must sometimes follow complex procedures to find |
| 125 |
the correct library. On Windows not all libraries are loaded via this module, |
| 126 |
so they will not be printed (however, loading Windows DLLs is sufficiently |
| 127 |
simple that there is little need for this information). |
| 128 |
|
| 129 |
Linux |
| 130 |
^^^^^ |
| 131 |
|
| 132 |
X11 errors are caught by pyglet and suppressed, as there are plenty of X |
| 133 |
servers in the wild that generate errors that can be safely ignored. |
| 134 |
The ``debug_x11`` option causes these errors to be dumped to standard out, |
| 135 |
along with a traceback of the Python stack (this may or may not correspond to |
| 136 |
the error, depending on whether or not it was reported asynchronously). |
| 137 |
|
| 138 |
Windows |
| 139 |
^^^^^^^ |
| 140 |
|
| 141 |
The ``debug_win32`` option causes all library calls into ``user32.dll``, |
| 142 |
``kernel32.dll`` and ``gdi32.dll`` to be intercepted. Before each library |
| 143 |
call ``SetLastError(0)`` is called, and afterwards ``GetLastError()`` is |
| 144 |
called. Any errors discovered are written to a file named |
| 145 |
``debug_win32.log``. Note that an error is only valid if the function called |
| 146 |
returned an error code, but the interception function does not check this. |