You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Unwinding-Frames-in-Python.html 13KB

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