/README

https://bitbucket.org/antocuni/pygdb2/ · #! · 114 lines · 84 code · 30 blank · 0 comment · 0 complexity · 87ac3d4c35105141d900f191ae4af0e0 MD5 · raw file

  1. .. -*- restructuredtext -*-
  2. pygdb2: control GDB from within the Python program being debugged
  3. ==================================================================
  4. ``pygdb2`` is a python module which allows you to send commands to the
  5. underlying ``gdb`` process. For example, it can be used to automatically and
  6. programmatically add breakpoints and watchpoints.
  7. How to install
  8. --------------
  9. Make sure that ``pygdb2`` is installed::
  10. $ pip install pygdb2
  11. Then, you need to activate the GDB integration by putting this line inside
  12. your ``~/.gdbinit``::
  13. python import pygdb2
  14. How to use it
  15. --------------
  16. You need to launch your python program inside ``gdb`` using the special
  17. ``pyrun`` command::
  18. $ gdb --args python myscript.py
  19. ...
  20. (gdb) pyrun
  21. ...
  22. From the python program, you can use ``pygdb2.set_trace()`` to enter the ``gdb``
  23. prompt, or ``pygdb2.execute()`` to send commands to ``gdb``.
  24. At the ``(gdb)`` prompt, it is possible to invoke the ``pdb`` command to enter
  25. in the corresponding debugger at the Python level.
  26. Example
  27. -------
  28. For example, the following code adds a watchpoint for a particular
  29. region of memory created with ``ctypes``::
  30. import ctypes
  31. import pygdb2
  32. def main():
  33. buf = ctypes.c_int()
  34. buf.value = 42
  35. adr = ctypes.cast(ctypes.pointer(buf), ctypes.c_void_p)
  36. # enter gdb when we write to this memory
  37. pygdb2.execute("watch *(int*)%d" % adr.value)
  38. i = 0
  39. while i < 5:
  40. print i
  41. i += 1
  42. if i == 2:
  43. buf.value = 43 # GDB stops here
  44. Here is an example of a gdb/pdb session::
  45. $ gdb --args python set_watchpoint.py
  46. ...
  47. (gdb) # lines prefixed "pygdb2:" contain the command coming from pygdb2.execute()
  48. (gdb) pyrun
  49. ...
  50. pygdb2: watch *(int*)14079984
  51. Hardware watchpoint 1: *(int*)14079984
  52. 0
  53. 1
  54. Hardware watchpoint 1: *(int*)14079984
  55. Old value = 42
  56. New value = 43
  57. i_set (ptr=0xd6d7f0, value=<value optimized out>, size=4) at /build/buildd/python2.7-2.7.1/Modules/_ctypes/cfield.c:663
  58. 663 /build/buildd/python2.7-2.7.1/Modules/_ctypes/cfield.c: No such file or directory.
  59. in /build/buildd/python2.7-2.7.1/Modules/_ctypes/cfield.c
  60. (gdb) # now we are debugging at the C level
  61. (gdb) # in particular, we are inside function of _ctypes
  62. (gdb) # which sets the value of the buffer
  63. (gdb) # Let's jump to the Python level
  64. (gdb) pdb
  65. Signal received, entering pdb
  66. Traceback:
  67. File "set_watchpoint.py", line 20, in <module>
  68. main()
  69. File "set_watchpoint.py", line 16, in main
  70. buf.value = 43 # we should enter gdb here
  71. > /home/antocuni/env/src/pygdb2/pygdb2/test/set_watchpoint.py(12)main()
  72. -> while i < 5:
  73. (Pdb++) print i
  74. 2
  75. (Pdb++) c
  76. 2
  77. 3
  78. 4
  79. Program exited normally.
  80. (gdb) q
  81. Using signals
  82. -----------------
  83. ``pygdb2`` uses both ``SIGUSR1`` and ``SIGUSR2`` to communicate with ``gdb``,
  84. so if your program also needs those, you might have conflicts.