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.

331 lines
14KB

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