Frames-In-Python.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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: Frames In Python</title>
  16. <meta name="description" content="Debugging with GDB: Frames In Python">
  17. <meta name="keywords" content="Debugging with GDB: 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="Blocks-In-Python.html#Blocks-In-Python" rel="next" title="Blocks In Python">
  27. <link href="Objfiles-In-Python.html#Objfiles-In-Python" rel="previous" title="Objfiles In Python">
  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="Frames-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Blocks-In-Python.html#Blocks-In-Python" accesskey="n" rel="next">Blocks In Python</a>, Previous: <a href="Objfiles-In-Python.html#Objfiles-In-Python" accesskey="p" rel="previous">Objfiles In Python</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="Accessing-inferior-stack-frames-from-Python"></a>
  65. <h4 class="subsubsection">23.2.2.25 Accessing inferior stack frames from Python</h4>
  66. <a name="index-frames-in-python"></a>
  67. <p>When the debugged program stops, <small>GDB</small> is able to analyze its call
  68. stack (see <a href="Frames.html#Frames">Stack frames</a>). The <code>gdb.Frame</code> class
  69. represents a frame in the stack. A <code>gdb.Frame</code> object is only valid
  70. while its corresponding frame exists in the inferior&rsquo;s stack. If you try
  71. to use an invalid frame object, <small>GDB</small> will throw a <code>gdb.error</code>
  72. exception (see <a href="Exception-Handling.html#Exception-Handling">Exception Handling</a>).
  73. </p>
  74. <p>Two <code>gdb.Frame</code> objects can be compared for equality with the <code>==</code>
  75. operator, like:
  76. </p>
  77. <div class="smallexample">
  78. <pre class="smallexample">(gdb) python print gdb.newest_frame() == gdb.selected_frame ()
  79. True
  80. </pre></div>
  81. <p>The following frame-related functions are available in the <code>gdb</code> module:
  82. </p>
  83. <a name="index-gdb_002eselected_005fframe"></a>
  84. <dl>
  85. <dt><a name="index-gdb_002eselected_005fframe-1"></a>Function: <strong>gdb.selected_frame</strong> <em>()</em></dt>
  86. <dd><p>Return the selected frame object. (see <a href="Selection.html#Selection">Selecting a Frame</a>).
  87. </p></dd></dl>
  88. <a name="index-gdb_002enewest_005fframe"></a>
  89. <dl>
  90. <dt><a name="index-gdb_002enewest_005fframe-1"></a>Function: <strong>gdb.newest_frame</strong> <em>()</em></dt>
  91. <dd><p>Return the newest frame object for the selected thread.
  92. </p></dd></dl>
  93. <dl>
  94. <dt><a name="index-gdb_002eframe_005fstop_005freason_005fstring"></a>Function: <strong>gdb.frame_stop_reason_string</strong> <em>(reason)</em></dt>
  95. <dd><p>Return a string explaining the reason why <small>GDB</small> stopped unwinding
  96. frames, as expressed by the given <var>reason</var> code (an integer, see the
  97. <code>unwind_stop_reason</code> method further down in this section).
  98. </p></dd></dl>
  99. <a name="index-gdb_002einvalidate_005fcached_005fframes"></a>
  100. <dl>
  101. <dt><a name="index-gdb_002einvalidate_005fcached_005fframes-1"></a>Function: <strong>gdb.invalidate_cached_frames</strong></dt>
  102. <dd><p><small>GDB</small> internally keeps a cache of the frames that have been
  103. unwound. This function invalidates this cache.
  104. </p>
  105. <p>This function should not generally be called by ordinary Python code.
  106. It is documented for the sake of completeness.
  107. </p></dd></dl>
  108. <p>A <code>gdb.Frame</code> object has the following methods:
  109. </p>
  110. <dl>
  111. <dt><a name="index-Frame_002eis_005fvalid"></a>Function: <strong>Frame.is_valid</strong> <em>()</em></dt>
  112. <dd><p>Returns true if the <code>gdb.Frame</code> object is valid, false if not.
  113. A frame object can become invalid if the frame it refers to doesn&rsquo;t
  114. exist anymore in the inferior. All <code>gdb.Frame</code> methods will throw
  115. an exception if it is invalid at the time the method is called.
  116. </p></dd></dl>
  117. <dl>
  118. <dt><a name="index-Frame_002ename"></a>Function: <strong>Frame.name</strong> <em>()</em></dt>
  119. <dd><p>Returns the function name of the frame, or <code>None</code> if it can&rsquo;t be
  120. obtained.
  121. </p></dd></dl>
  122. <dl>
  123. <dt><a name="index-Frame_002earchitecture"></a>Function: <strong>Frame.architecture</strong> <em>()</em></dt>
  124. <dd><p>Returns the <code>gdb.Architecture</code> object corresponding to the frame&rsquo;s
  125. architecture. See <a href="Architectures-In-Python.html#Architectures-In-Python">Architectures In Python</a>.
  126. </p></dd></dl>
  127. <dl>
  128. <dt><a name="index-Frame_002etype"></a>Function: <strong>Frame.type</strong> <em>()</em></dt>
  129. <dd><p>Returns the type of the frame. The value can be one of:
  130. </p><dl compact="compact">
  131. <dt><code>gdb.NORMAL_FRAME</code></dt>
  132. <dd><p>An ordinary stack frame.
  133. </p>
  134. </dd>
  135. <dt><code>gdb.DUMMY_FRAME</code></dt>
  136. <dd><p>A fake stack frame that was created by <small>GDB</small> when performing an
  137. inferior function call.
  138. </p>
  139. </dd>
  140. <dt><code>gdb.INLINE_FRAME</code></dt>
  141. <dd><p>A frame representing an inlined function. The function was inlined
  142. into a <code>gdb.NORMAL_FRAME</code> that is older than this one.
  143. </p>
  144. </dd>
  145. <dt><code>gdb.TAILCALL_FRAME</code></dt>
  146. <dd><p>A frame representing a tail call. See <a href="Tail-Call-Frames.html#Tail-Call-Frames">Tail Call Frames</a>.
  147. </p>
  148. </dd>
  149. <dt><code>gdb.SIGTRAMP_FRAME</code></dt>
  150. <dd><p>A signal trampoline frame. This is the frame created by the OS when
  151. it calls into a signal handler.
  152. </p>
  153. </dd>
  154. <dt><code>gdb.ARCH_FRAME</code></dt>
  155. <dd><p>A fake stack frame representing a cross-architecture call.
  156. </p>
  157. </dd>
  158. <dt><code>gdb.SENTINEL_FRAME</code></dt>
  159. <dd><p>This is like <code>gdb.NORMAL_FRAME</code>, but it is only used for the
  160. newest frame.
  161. </p></dd>
  162. </dl>
  163. </dd></dl>
  164. <dl>
  165. <dt><a name="index-Frame_002eunwind_005fstop_005freason"></a>Function: <strong>Frame.unwind_stop_reason</strong> <em>()</em></dt>
  166. <dd><p>Return an integer representing the reason why it&rsquo;s not possible to find
  167. more frames toward the outermost frame. Use
  168. <code>gdb.frame_stop_reason_string</code> to convert the value returned by this
  169. function to a string. The value can be one of:
  170. </p>
  171. <dl compact="compact">
  172. <dt><code>gdb.FRAME_UNWIND_NO_REASON</code></dt>
  173. <dd><p>No particular reason (older frames should be available).
  174. </p>
  175. </dd>
  176. <dt><code>gdb.FRAME_UNWIND_NULL_ID</code></dt>
  177. <dd><p>The previous frame&rsquo;s analyzer returns an invalid result. This is no
  178. longer used by <small>GDB</small>, and is kept only for backward
  179. compatibility.
  180. </p>
  181. </dd>
  182. <dt><code>gdb.FRAME_UNWIND_OUTERMOST</code></dt>
  183. <dd><p>This frame is the outermost.
  184. </p>
  185. </dd>
  186. <dt><code>gdb.FRAME_UNWIND_UNAVAILABLE</code></dt>
  187. <dd><p>Cannot unwind further, because that would require knowing the
  188. values of registers or memory that have not been collected.
  189. </p>
  190. </dd>
  191. <dt><code>gdb.FRAME_UNWIND_INNER_ID</code></dt>
  192. <dd><p>This frame ID looks like it ought to belong to a NEXT frame,
  193. but we got it for a PREV frame. Normally, this is a sign of
  194. unwinder failure. It could also indicate stack corruption.
  195. </p>
  196. </dd>
  197. <dt><code>gdb.FRAME_UNWIND_SAME_ID</code></dt>
  198. <dd><p>This frame has the same ID as the previous one. That means
  199. that unwinding further would almost certainly give us another
  200. frame with exactly the same ID, so break the chain. Normally,
  201. this is a sign of unwinder failure. It could also indicate
  202. stack corruption.
  203. </p>
  204. </dd>
  205. <dt><code>gdb.FRAME_UNWIND_NO_SAVED_PC</code></dt>
  206. <dd><p>The frame unwinder did not find any saved PC, but we needed
  207. one to unwind further.
  208. </p>
  209. </dd>
  210. <dt><code>gdb.FRAME_UNWIND_MEMORY_ERROR</code></dt>
  211. <dd><p>The frame unwinder caused an error while trying to access memory.
  212. </p>
  213. </dd>
  214. <dt><code>gdb.FRAME_UNWIND_FIRST_ERROR</code></dt>
  215. <dd><p>Any stop reason greater or equal to this value indicates some kind
  216. of error. This special value facilitates writing code that tests
  217. for errors in unwinding in a way that will work correctly even if
  218. the list of the other values is modified in future <small>GDB</small>
  219. versions. Using it, you could write:
  220. </p><div class="smallexample">
  221. <pre class="smallexample">reason = gdb.selected_frame().unwind_stop_reason ()
  222. reason_str = gdb.frame_stop_reason_string (reason)
  223. if reason &gt;= gdb.FRAME_UNWIND_FIRST_ERROR:
  224. print &quot;An error occured: %s&quot; % reason_str
  225. </pre></div>
  226. </dd>
  227. </dl>
  228. </dd></dl>
  229. <dl>
  230. <dt><a name="index-Frame_002epc"></a>Function: <strong>Frame.pc</strong> <em>()</em></dt>
  231. <dd><p>Returns the frame&rsquo;s resume address.
  232. </p></dd></dl>
  233. <dl>
  234. <dt><a name="index-Frame_002eblock"></a>Function: <strong>Frame.block</strong> <em>()</em></dt>
  235. <dd><p>Return the frame&rsquo;s code block. See <a href="Blocks-In-Python.html#Blocks-In-Python">Blocks In Python</a>. If the frame
  236. does not have a block &ndash; for example, if there is no debugging
  237. information for the code in question &ndash; then this will throw an
  238. exception.
  239. </p></dd></dl>
  240. <dl>
  241. <dt><a name="index-Frame_002efunction"></a>Function: <strong>Frame.function</strong> <em>()</em></dt>
  242. <dd><p>Return the symbol for the function corresponding to this frame.
  243. See <a href="Symbols-In-Python.html#Symbols-In-Python">Symbols In Python</a>.
  244. </p></dd></dl>
  245. <dl>
  246. <dt><a name="index-Frame_002eolder"></a>Function: <strong>Frame.older</strong> <em>()</em></dt>
  247. <dd><p>Return the frame that called this frame.
  248. </p></dd></dl>
  249. <dl>
  250. <dt><a name="index-Frame_002enewer"></a>Function: <strong>Frame.newer</strong> <em>()</em></dt>
  251. <dd><p>Return the frame called by this frame.
  252. </p></dd></dl>
  253. <dl>
  254. <dt><a name="index-Frame_002efind_005fsal"></a>Function: <strong>Frame.find_sal</strong> <em>()</em></dt>
  255. <dd><p>Return the frame&rsquo;s symtab and line object.
  256. See <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>.
  257. </p></dd></dl>
  258. <a name="gdbpy_005fframe_005fread_005fregister"></a><dl>
  259. <dt><a name="index-Frame_002eread_005fregister"></a>Function: <strong>Frame.read_register</strong> <em>(register)</em></dt>
  260. <dd><p>Return the value of <var>register</var> in this frame. Returns a
  261. <code>Gdb.Value</code> object. Throws an exception if <var>register</var> does
  262. not exist. The <var>register</var> argument must be one of the following:
  263. </p><ol>
  264. <li> A string that is the name of a valid register (e.g., <code>'sp'</code> or
  265. <code>'rax'</code>).
  266. </li><li> A <code>gdb.RegisterDescriptor</code> object (see <a href="Registers-In-Python.html#Registers-In-Python">Registers In Python</a>).
  267. </li><li> A <small>GDB</small> internal, platform specific number. Using these
  268. numbers is supported for historic reasons, but is not recommended as
  269. future changes to <small>GDB</small> could change the mapping between
  270. numbers and the registers they represent, breaking any Python code
  271. that uses the platform-specific numbers. The numbers are usually
  272. found in the corresponding <samp><var>platform</var>-tdep.h</samp> file in the
  273. <small>GDB</small> source tree.
  274. </li></ol>
  275. <p>Using a string to access registers will be slightly slower than the
  276. other two methods as <small>GDB</small> must look up the mapping between
  277. name and internal register number. If performance is critical
  278. consider looking up and caching a <code>gdb.RegisterDescriptor</code>
  279. object.
  280. </p></dd></dl>
  281. <dl>
  282. <dt><a name="index-Frame_002eread_005fvar"></a>Function: <strong>Frame.read_var</strong> <em>(variable <span class="roman">[</span>, block<span class="roman">]</span>)</em></dt>
  283. <dd><p>Return the value of <var>variable</var> in this frame. If the optional
  284. argument <var>block</var> is provided, search for the variable from that
  285. block; otherwise start at the frame&rsquo;s current block (which is
  286. determined by the frame&rsquo;s current program counter). The <var>variable</var>
  287. argument must be a string or a <code>gdb.Symbol</code> object; <var>block</var> must be a
  288. <code>gdb.Block</code> object.
  289. </p></dd></dl>
  290. <dl>
  291. <dt><a name="index-Frame_002eselect"></a>Function: <strong>Frame.select</strong> <em>()</em></dt>
  292. <dd><p>Set this frame to be the selected frame. See <a href="Stack.html#Stack">Examining the
  293. Stack</a>.
  294. </p></dd></dl>
  295. <hr>
  296. <div class="header">
  297. <p>
  298. Next: <a href="Blocks-In-Python.html#Blocks-In-Python" accesskey="n" rel="next">Blocks In Python</a>, Previous: <a href="Objfiles-In-Python.html#Objfiles-In-Python" accesskey="p" rel="previous">Objfiles In Python</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>
  299. </div>
  300. </body>
  301. </html>