Unwinding-Frames-in-Python.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Unwinding Frames in Python</title>
  16. <meta name="description" content="Debugging with GDB: Unwinding Frames in Python">
  17. <meta name="keywords" content="Debugging with GDB: Unwinding Frames in Python">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Python-API.html#Python-API" rel="up" title="Python API">
  26. <link href="Xmethods-In-Python.html#Xmethods-In-Python" rel="next" title="Xmethods In Python">
  27. <link href="Writing-a-Frame-Filter.html#Writing-a-Frame-Filter" rel="previous" title="Writing a Frame Filter">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Unwinding-Frames-in-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Xmethods-In-Python.html#Xmethods-In-Python" accesskey="n" rel="next">Xmethods In Python</a>, Previous: <a href="Writing-a-Frame-Filter.html#Writing-a-Frame-Filter" accesskey="p" rel="previous">Writing a Frame Filter</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Unwinding-Frames-in-Python-1"></a>
  65. <h4 class="subsubsection">23.2.2.12 Unwinding Frames in Python</h4>
  66. <a name="index-unwinding-frames-in-Python"></a>
  67. <p>In <small>GDB</small> terminology &ldquo;unwinding&rdquo; is the process of finding
  68. the previous frame (that is, caller&rsquo;s) from the current one. An
  69. unwinder has three methods. The first one checks if it can handle
  70. given frame (&ldquo;sniff&rdquo; it). For the frames it can sniff an unwinder
  71. provides two additional methods: it can return frame&rsquo;s ID, and it can
  72. fetch registers from the previous frame. A running <small>GDB</small>
  73. mantains a list of the unwinders and calls each unwinder&rsquo;s sniffer in
  74. turn until it finds the one that recognizes the current frame. There
  75. is an API to register an unwinder.
  76. </p>
  77. <p>The unwinders that come with <small>GDB</small> handle standard frames.
  78. However, mixed language applications (for example, an application
  79. running Java Virtual Machine) sometimes use frame layouts that cannot
  80. be handled by the <small>GDB</small> unwinders. You can write Python code
  81. that can handle such custom frames.
  82. </p>
  83. <p>You implement a frame unwinder in Python as a class with which has two
  84. attributes, <code>name</code> and <code>enabled</code>, with obvious meanings, and
  85. a single method <code>__call__</code>, which examines a given frame and
  86. returns an object (an instance of <code>gdb.UnwindInfo class)</code>
  87. describing it. If an unwinder does not recognize a frame, it should
  88. return <code>None</code>. The code in <small>GDB</small> that enables writing
  89. unwinders in Python uses this object to return frame&rsquo;s ID and previous
  90. frame registers when <small>GDB</small> core asks for them.
  91. </p>
  92. <p>An unwinder should do as little work as possible. Some otherwise
  93. innocuous operations can cause problems (even crashes, as this code is
  94. not not well-hardened yet). For example, making an inferior call from
  95. an unwinder is unadvisable, as an inferior call will reset
  96. <small>GDB</small>&rsquo;s stack unwinding process, potentially causing re-entrant
  97. unwinding.
  98. </p>
  99. <a name="Unwinder-Input"></a>
  100. <h4 class="subheading">Unwinder Input</h4>
  101. <p>An object passed to an unwinder (a <code>gdb.PendingFrame</code> instance)
  102. provides a method to read frame&rsquo;s registers:
  103. </p>
  104. <dl>
  105. <dt><a name="index-PendingFrame_002eread_005fregister"></a>Function: <strong>PendingFrame.read_register</strong> <em>(reg)</em></dt>
  106. <dd><p>This method returns the contents of the register <var>reg</var> in the
  107. frame as a <code>gdb.Value</code> object. For a description of the
  108. acceptable values of <var>reg</var> see
  109. <a href="Frames-In-Python.html#gdbpy_005fframe_005fread_005fregister">Frame.read_register</a>. If <var>reg</var>
  110. does not name a register for the current architecture, this method
  111. will throw an exception.
  112. </p>
  113. <p>Note that this method will always return a <code>gdb.Value</code> for a
  114. valid register name. This does not mean that the value will be valid.
  115. For example, you may request a register that an earlier unwinder could
  116. not unwind&mdash;the value will be unavailable. Instead, the
  117. <code>gdb.Value</code> returned from this method will be lazy; that is, its
  118. underlying bits will not be fetched until it is first used. So,
  119. attempting to use such a value will cause an exception at the point of
  120. use.
  121. </p>
  122. <p>The type of the returned <code>gdb.Value</code> depends on the register and
  123. the architecture. It is common for registers to have a scalar type,
  124. like <code>long long</code>; but many other types are possible, such as
  125. pointer, pointer-to-function, floating point or vector types.
  126. </p></dd></dl>
  127. <p>It also provides a factory method to create a <code>gdb.UnwindInfo</code>
  128. instance to be returned to <small>GDB</small>:
  129. </p>
  130. <dl>
  131. <dt><a name="index-PendingFrame_002ecreate_005funwind_005finfo"></a>Function: <strong>PendingFrame.create_unwind_info</strong> <em>(frame_id)</em></dt>
  132. <dd><p>Returns a new <code>gdb.UnwindInfo</code> instance identified by given
  133. <var>frame_id</var>. The argument is used to build <small>GDB</small>&rsquo;s frame ID
  134. using one of functions provided by <small>GDB</small>. <var>frame_id</var>&rsquo;s attributes
  135. determine which function will be used, as follows:
  136. </p>
  137. <dl compact="compact">
  138. <dt><code>sp, pc</code></dt>
  139. <dd><p>The frame is identified by the given stack address and PC. The stack
  140. address must be chosen so that it is constant throughout the lifetime
  141. of the frame, so a typical choice is the value of the stack pointer at
  142. the start of the function&mdash;in the DWARF standard, this would be the
  143. &ldquo;Call Frame Address&rdquo;.
  144. </p>
  145. <p>This is the most common case by far. The other cases are documented
  146. for completeness but are only useful in specialized situations.
  147. </p>
  148. </dd>
  149. <dt><code>sp, pc, special</code></dt>
  150. <dd><p>The frame is identified by the stack address, the PC, and a
  151. &ldquo;special&rdquo; address. The special address is used on architectures
  152. that can have frames that do not change the stack, but which are still
  153. distinct, for example the IA-64, which has a second stack for
  154. registers. Both <var>sp</var> and <var>special</var> must be constant
  155. throughout the lifetime of the frame.
  156. </p>
  157. </dd>
  158. <dt><code>sp</code></dt>
  159. <dd><p>The frame is identified by the stack address only. Any other stack
  160. frame with a matching <var>sp</var> will be considered to match this frame.
  161. Inside gdb, this is called a &ldquo;wild frame&rdquo;. You will never need
  162. this.
  163. </p></dd>
  164. </dl>
  165. <p>Each attribute value should be an instance of <code>gdb.Value</code>.
  166. </p>
  167. </dd></dl>
  168. <dl>
  169. <dt><a name="index-PendingFrame_002earchitecture"></a>Function: <strong>PendingFrame.architecture</strong> <em>()</em></dt>
  170. <dd><p>Return the <code>gdb.Architecture</code> (see <a href="Architectures-In-Python.html#Architectures-In-Python">Architectures In Python</a>)
  171. for this <code>gdb.PendingFrame</code>. This represents the architecture of
  172. the particular frame being unwound.
  173. </p></dd></dl>
  174. <a name="Unwinder-Output_003a-UnwindInfo"></a>
  175. <h4 class="subheading">Unwinder Output: UnwindInfo</h4>
  176. <p>Use <code>PendingFrame.create_unwind_info</code> method described above to
  177. create a <code>gdb.UnwindInfo</code> instance. Use the following method to
  178. specify caller registers that have been saved in this frame:
  179. </p>
  180. <dl>
  181. <dt><a name="index-gdb_002eUnwindInfo_002eadd_005fsaved_005fregister"></a>Function: <strong>gdb.UnwindInfo.add_saved_register</strong> <em>(reg, value)</em></dt>
  182. <dd><p><var>reg</var> identifies the register, for a description of the acceptable
  183. values see <a href="Frames-In-Python.html#gdbpy_005fframe_005fread_005fregister">Frame.read_register</a>.
  184. <var>value</var> is a register value (a <code>gdb.Value</code> object).
  185. </p></dd></dl>
  186. <a name="Unwinder-Skeleton-Code"></a>
  187. <h4 class="subheading">Unwinder Skeleton Code</h4>
  188. <p><small>GDB</small> comes with the module containing the base <code>Unwinder</code>
  189. class. Derive your unwinder class from it and structure the code as
  190. follows:
  191. </p>
  192. <div class="smallexample">
  193. <pre class="smallexample">from gdb.unwinders import Unwinder
  194. class FrameId(object):
  195. def __init__(self, sp, pc):
  196. self.sp = sp
  197. self.pc = pc
  198. class MyUnwinder(Unwinder):
  199. def __init__(....):
  200. super(MyUnwinder, self).__init___(&lt;expects unwinder name argument&gt;)
  201. def __call__(pending_frame):
  202. if not &lt;we recognize frame&gt;:
  203. return None
  204. # Create UnwindInfo. Usually the frame is identified by the stack
  205. # pointer and the program counter.
  206. sp = pending_frame.read_register(&lt;SP number&gt;)
  207. pc = pending_frame.read_register(&lt;PC number&gt;)
  208. unwind_info = pending_frame.create_unwind_info(FrameId(sp, pc))
  209. # Find the values of the registers in the caller's frame and
  210. # save them in the result:
  211. unwind_info.add_saved_register(&lt;register&gt;, &lt;value&gt;)
  212. ....
  213. # Return the result:
  214. return unwind_info
  215. </pre></div>
  216. <a name="Registering-a-Unwinder"></a>
  217. <h4 class="subheading">Registering a Unwinder</h4>
  218. <p>An object file, a program space, and the <small>GDB</small> proper can have
  219. unwinders registered with it.
  220. </p>
  221. <p>The <code>gdb.unwinders</code> module provides the function to register a
  222. unwinder:
  223. </p>
  224. <dl>
  225. <dt><a name="index-gdb_002eunwinder_002eregister_005funwinder"></a>Function: <strong>gdb.unwinder.register_unwinder</strong> <em>(locus, unwinder, replace=False)</em></dt>
  226. <dd><p><var>locus</var> is specifies an object file or a program space to which
  227. <var>unwinder</var> is added. Passing <code>None</code> or <code>gdb</code> adds
  228. <var>unwinder</var> to the <small>GDB</small>&rsquo;s global unwinder list. The newly
  229. added <var>unwinder</var> will be called before any other unwinder from the
  230. same locus. Two unwinders in the same locus cannot have the same
  231. name. An attempt to add a unwinder with already existing name raises
  232. an exception unless <var>replace</var> is <code>True</code>, in which case the
  233. old unwinder is deleted.
  234. </p></dd></dl>
  235. <a name="Unwinder-Precedence"></a>
  236. <h4 class="subheading">Unwinder Precedence</h4>
  237. <p><small>GDB</small> first calls the unwinders from all the object files in no
  238. particular order, then the unwinders from the current program space,
  239. and finally the unwinders from <small>GDB</small>.
  240. </p>
  241. <hr>
  242. <div class="header">
  243. <p>
  244. Next: <a href="Xmethods-In-Python.html#Xmethods-In-Python" accesskey="n" rel="next">Xmethods In Python</a>, Previous: <a href="Writing-a-Frame-Filter.html#Writing-a-Frame-Filter" accesskey="p" rel="previous">Writing a Frame Filter</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  245. </div>
  246. </body>
  247. </html>