/Misc/gdbinit

http://unladen-swallow.googlecode.com/ · #! · 153 lines · 138 code · 15 blank · 0 comment · 0 complexity · ee74489a462726fba71b201709a38afd MD5 · raw file

  1. # -*- ksh -*-
  2. #
  3. # If you use the GNU debugger gdb to debug the Python C runtime, you
  4. # might find some of the following commands useful. Copy this to your
  5. # ~/.gdbinit file and it'll get loaded into gdb automatically when you
  6. # start it up. Then, at the gdb prompt you can do things like:
  7. #
  8. # (gdb) pyo apyobjectptr
  9. # <module 'foobar' (built-in)>
  10. # refcounts: 1
  11. # address : 84a7a2c
  12. # $1 = void
  13. # (gdb)
  14. # Prints a representation of the object to stderr, along with the
  15. # number of reference counts it current has and the hex address the
  16. # object is allocated at. The argument must be a PyObject*
  17. define pyo
  18. print _PyObject_Dump($arg0)
  19. end
  20. # Prints a representation of the object to stderr, along with the
  21. # number of reference counts it current has and the hex address the
  22. # object is allocated at. The argument must be a PyGC_Head*
  23. define pyg
  24. print _PyGC_Dump($arg0)
  25. end
  26. # print the local variables of the current frame
  27. define pylocals
  28. set $_i = 0
  29. while $_i < f->f_nlocals
  30. if f->f_localsplus + $_i != 0
  31. set $_names = co->co_varnames
  32. set $_name = PyString_AsString(PyTuple_GetItem($_names, $_i))
  33. printf "%s:\n", $_name
  34. # side effect of calling _PyObject_Dump is to dump the object's
  35. # info - assigning just prevents gdb from printing the
  36. # NULL return value
  37. set $_val = _PyObject_Dump(f->f_localsplus[$_i])
  38. end
  39. set $_i = $_i + 1
  40. end
  41. end
  42. # A rewrite of the Python interpreter's line number calculator in GDB's
  43. # command language
  44. define lineno
  45. set $__continue = 1
  46. set $__co = f->f_code
  47. set $__lasti = f->f_lasti
  48. set $__sz = ((PyStringObject *)$__co->co_lnotab)->ob_size/2
  49. set $__p = (unsigned char *)((PyStringObject *)$__co->co_lnotab)->ob_sval
  50. set $__li = $__co->co_firstlineno
  51. set $__ad = 0
  52. while ($__sz-1 >= 0 && $__continue)
  53. set $__sz = $__sz - 1
  54. set $__ad = $__ad + *$__p
  55. set $__p = $__p + 1
  56. if ($__ad > $__lasti)
  57. set $__continue = 0
  58. end
  59. set $__li = $__li + *$__p
  60. set $__p = $__p + 1
  61. end
  62. printf "%d", $__li
  63. end
  64. # print the current frame - verbose
  65. define pyframev
  66. pyframe
  67. pylocals
  68. end
  69. define pyframe
  70. set $__fn = (char *)((PyStringObject *)co->co_filename)->ob_sval
  71. set $__n = (char *)((PyStringObject *)co->co_name)->ob_sval
  72. printf "%s (", $__fn
  73. lineno
  74. printf "): %s\n", $__n
  75. ### Uncomment these lines when using from within Emacs/XEmacs so it will
  76. ### automatically track/display the current Python source line
  77. # printf "%c%c%s:", 032, 032, $__fn
  78. # lineno
  79. # printf ":1\n"
  80. end
  81. ### Use these at your own risk. It appears that a bug in gdb causes it
  82. ### to crash in certain circumstances.
  83. #define up
  84. # up-silently 1
  85. # printframe
  86. #end
  87. #define down
  88. # down-silently 1
  89. # printframe
  90. #end
  91. define printframe
  92. if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
  93. pyframe
  94. else
  95. frame
  96. end
  97. end
  98. # Here's a somewhat fragile way to print the entire Python stack from gdb.
  99. # It's fragile because the tests for the value of $pc depend on the layout
  100. # of specific functions in the C source code.
  101. # Explanation of while and if tests: We want to pop up the stack until we
  102. # land in Py_Main (this is probably an incorrect assumption in an embedded
  103. # interpreter, but the test can be extended by an interested party). If
  104. # Py_Main <= $pc <= Py_GetArgcArv is true, $pc is in Py_Main(), so the while
  105. # tests succeeds as long as it's not true. In a similar fashion the if
  106. # statement tests to see if we are in PyEval_EvalFrame().
  107. # print the entire Python call stack
  108. define pystack
  109. while $pc < Py_Main || $pc > Py_GetArgcArgv
  110. if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
  111. pyframe
  112. end
  113. up-silently 1
  114. end
  115. select-frame 0
  116. end
  117. # print the entire Python call stack - verbose mode
  118. define pystackv
  119. while $pc < Py_Main || $pc > Py_GetArgcArgv
  120. if $pc > PyEval_EvalFrame && $pc < PyEval_EvalCodeEx
  121. pyframev
  122. end
  123. up-silently 1
  124. end
  125. select-frame 0
  126. end
  127. # generally useful macro to print a Unicode string
  128. def pu
  129. set $uni = $arg0
  130. set $i = 0
  131. while (*$uni && $i++<100)
  132. if (*$uni < 0x80)
  133. print *(char*)$uni++
  134. else
  135. print /x *(short*)$uni++
  136. end
  137. end
  138. end