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-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Frames In Python</title> 18 19<meta name="description" content="Debugging with GDB: Frames In Python"> 20<meta name="keywords" content="Debugging with GDB: Frames In Python"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Python-API.html#Python-API" rel="up" title="Python API"> 29<link href="Blocks-In-Python.html#Blocks-In-Python" rel="next" title="Blocks In Python"> 30<link href="Objfiles-In-Python.html#Objfiles-In-Python" rel="previous" title="Objfiles In Python"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Frames-In-Python"></a> 65<div class="header"> 66<p> 67Next: <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> [<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> 68</div> 69<hr> 70<a name="Accessing-inferior-stack-frames-from-Python"></a> 71<h4 class="subsubsection">23.2.2.25 Accessing inferior stack frames from Python</h4> 72 73<a name="index-frames-in-python"></a> 74<p>When the debugged program stops, <small>GDB</small> is able to analyze its call 75stack (see <a href="Frames.html#Frames">Stack frames</a>). The <code>gdb.Frame</code> class 76represents a frame in the stack. A <code>gdb.Frame</code> object is only valid 77while its corresponding frame exists in the inferior’s stack. If you try 78to use an invalid frame object, <small>GDB</small> will throw a <code>gdb.error</code> 79exception (see <a href="Exception-Handling.html#Exception-Handling">Exception Handling</a>). 80</p> 81<p>Two <code>gdb.Frame</code> objects can be compared for equality with the <code>==</code> 82operator, like: 83</p> 84<div class="smallexample"> 85<pre class="smallexample">(gdb) python print gdb.newest_frame() == gdb.selected_frame () 86True 87</pre></div> 88 89<p>The following frame-related functions are available in the <code>gdb</code> module: 90</p> 91<a name="index-gdb_002eselected_005fframe"></a> 92<dl> 93<dt><a name="index-gdb_002eselected_005fframe-1"></a>Function: <strong>gdb.selected_frame</strong> <em>()</em></dt> 94<dd><p>Return the selected frame object. (see <a href="Selection.html#Selection">Selecting a Frame</a>). 95</p></dd></dl> 96 97<a name="index-gdb_002enewest_005fframe"></a> 98<dl> 99<dt><a name="index-gdb_002enewest_005fframe-1"></a>Function: <strong>gdb.newest_frame</strong> <em>()</em></dt> 100<dd><p>Return the newest frame object for the selected thread. 101</p></dd></dl> 102 103<dl> 104<dt><a name="index-gdb_002eframe_005fstop_005freason_005fstring"></a>Function: <strong>gdb.frame_stop_reason_string</strong> <em>(reason)</em></dt> 105<dd><p>Return a string explaining the reason why <small>GDB</small> stopped unwinding 106frames, as expressed by the given <var>reason</var> code (an integer, see the 107<code>unwind_stop_reason</code> method further down in this section). 108</p></dd></dl> 109 110<a name="index-gdb_002einvalidate_005fcached_005fframes"></a> 111<dl> 112<dt><a name="index-gdb_002einvalidate_005fcached_005fframes-1"></a>Function: <strong>gdb.invalidate_cached_frames</strong></dt> 113<dd><p><small>GDB</small> internally keeps a cache of the frames that have been 114unwound. This function invalidates this cache. 115</p> 116<p>This function should not generally be called by ordinary Python code. 117It is documented for the sake of completeness. 118</p></dd></dl> 119 120<p>A <code>gdb.Frame</code> object has the following methods: 121</p> 122<dl> 123<dt><a name="index-Frame_002eis_005fvalid"></a>Function: <strong>Frame.is_valid</strong> <em>()</em></dt> 124<dd><p>Returns true if the <code>gdb.Frame</code> object is valid, false if not. 125A frame object can become invalid if the frame it refers to doesn’t 126exist anymore in the inferior. All <code>gdb.Frame</code> methods will throw 127an exception if it is invalid at the time the method is called. 128</p></dd></dl> 129 130<dl> 131<dt><a name="index-Frame_002ename"></a>Function: <strong>Frame.name</strong> <em>()</em></dt> 132<dd><p>Returns the function name of the frame, or <code>None</code> if it can’t be 133obtained. 134</p></dd></dl> 135 136<dl> 137<dt><a name="index-Frame_002earchitecture"></a>Function: <strong>Frame.architecture</strong> <em>()</em></dt> 138<dd><p>Returns the <code>gdb.Architecture</code> object corresponding to the frame’s 139architecture. See <a href="Architectures-In-Python.html#Architectures-In-Python">Architectures In Python</a>. 140</p></dd></dl> 141 142<dl> 143<dt><a name="index-Frame_002etype"></a>Function: <strong>Frame.type</strong> <em>()</em></dt> 144<dd><p>Returns the type of the frame. The value can be one of: 145</p><dl compact="compact"> 146<dt><code>gdb.NORMAL_FRAME</code></dt> 147<dd><p>An ordinary stack frame. 148</p> 149</dd> 150<dt><code>gdb.DUMMY_FRAME</code></dt> 151<dd><p>A fake stack frame that was created by <small>GDB</small> when performing an 152inferior function call. 153</p> 154</dd> 155<dt><code>gdb.INLINE_FRAME</code></dt> 156<dd><p>A frame representing an inlined function. The function was inlined 157into a <code>gdb.NORMAL_FRAME</code> that is older than this one. 158</p> 159</dd> 160<dt><code>gdb.TAILCALL_FRAME</code></dt> 161<dd><p>A frame representing a tail call. See <a href="Tail-Call-Frames.html#Tail-Call-Frames">Tail Call Frames</a>. 162</p> 163</dd> 164<dt><code>gdb.SIGTRAMP_FRAME</code></dt> 165<dd><p>A signal trampoline frame. This is the frame created by the OS when 166it calls into a signal handler. 167</p> 168</dd> 169<dt><code>gdb.ARCH_FRAME</code></dt> 170<dd><p>A fake stack frame representing a cross-architecture call. 171</p> 172</dd> 173<dt><code>gdb.SENTINEL_FRAME</code></dt> 174<dd><p>This is like <code>gdb.NORMAL_FRAME</code>, but it is only used for the 175newest frame. 176</p></dd> 177</dl> 178</dd></dl> 179 180<dl> 181<dt><a name="index-Frame_002eunwind_005fstop_005freason"></a>Function: <strong>Frame.unwind_stop_reason</strong> <em>()</em></dt> 182<dd><p>Return an integer representing the reason why it’s not possible to find 183more frames toward the outermost frame. Use 184<code>gdb.frame_stop_reason_string</code> to convert the value returned by this 185function to a string. The value can be one of: 186</p> 187<dl compact="compact"> 188<dt><code>gdb.FRAME_UNWIND_NO_REASON</code></dt> 189<dd><p>No particular reason (older frames should be available). 190</p> 191</dd> 192<dt><code>gdb.FRAME_UNWIND_NULL_ID</code></dt> 193<dd><p>The previous frame’s analyzer returns an invalid result. This is no 194longer used by <small>GDB</small>, and is kept only for backward 195compatibility. 196</p> 197</dd> 198<dt><code>gdb.FRAME_UNWIND_OUTERMOST</code></dt> 199<dd><p>This frame is the outermost. 200</p> 201</dd> 202<dt><code>gdb.FRAME_UNWIND_UNAVAILABLE</code></dt> 203<dd><p>Cannot unwind further, because that would require knowing the 204values of registers or memory that have not been collected. 205</p> 206</dd> 207<dt><code>gdb.FRAME_UNWIND_INNER_ID</code></dt> 208<dd><p>This frame ID looks like it ought to belong to a NEXT frame, 209but we got it for a PREV frame. Normally, this is a sign of 210unwinder failure. It could also indicate stack corruption. 211</p> 212</dd> 213<dt><code>gdb.FRAME_UNWIND_SAME_ID</code></dt> 214<dd><p>This frame has the same ID as the previous one. That means 215that unwinding further would almost certainly give us another 216frame with exactly the same ID, so break the chain. Normally, 217this is a sign of unwinder failure. It could also indicate 218stack corruption. 219</p> 220</dd> 221<dt><code>gdb.FRAME_UNWIND_NO_SAVED_PC</code></dt> 222<dd><p>The frame unwinder did not find any saved PC, but we needed 223one to unwind further. 224</p> 225</dd> 226<dt><code>gdb.FRAME_UNWIND_MEMORY_ERROR</code></dt> 227<dd><p>The frame unwinder caused an error while trying to access memory. 228</p> 229</dd> 230<dt><code>gdb.FRAME_UNWIND_FIRST_ERROR</code></dt> 231<dd><p>Any stop reason greater or equal to this value indicates some kind 232of error. This special value facilitates writing code that tests 233for errors in unwinding in a way that will work correctly even if 234the list of the other values is modified in future <small>GDB</small> 235versions. Using it, you could write: 236</p><div class="smallexample"> 237<pre class="smallexample">reason = gdb.selected_frame().unwind_stop_reason () 238reason_str = gdb.frame_stop_reason_string (reason) 239if reason >= gdb.FRAME_UNWIND_FIRST_ERROR: 240 print "An error occured: %s" % reason_str 241</pre></div> 242</dd> 243</dl> 244 245</dd></dl> 246 247<dl> 248<dt><a name="index-Frame_002epc"></a>Function: <strong>Frame.pc</strong> <em>()</em></dt> 249<dd><p>Returns the frame’s resume address. 250</p></dd></dl> 251 252<dl> 253<dt><a name="index-Frame_002eblock"></a>Function: <strong>Frame.block</strong> <em>()</em></dt> 254<dd><p>Return the frame’s code block. See <a href="Blocks-In-Python.html#Blocks-In-Python">Blocks In Python</a>. If the frame 255does not have a block – for example, if there is no debugging 256information for the code in question – then this will throw an 257exception. 258</p></dd></dl> 259 260<dl> 261<dt><a name="index-Frame_002efunction"></a>Function: <strong>Frame.function</strong> <em>()</em></dt> 262<dd><p>Return the symbol for the function corresponding to this frame. 263See <a href="Symbols-In-Python.html#Symbols-In-Python">Symbols In Python</a>. 264</p></dd></dl> 265 266<dl> 267<dt><a name="index-Frame_002eolder"></a>Function: <strong>Frame.older</strong> <em>()</em></dt> 268<dd><p>Return the frame that called this frame. 269</p></dd></dl> 270 271<dl> 272<dt><a name="index-Frame_002enewer"></a>Function: <strong>Frame.newer</strong> <em>()</em></dt> 273<dd><p>Return the frame called by this frame. 274</p></dd></dl> 275 276<dl> 277<dt><a name="index-Frame_002efind_005fsal"></a>Function: <strong>Frame.find_sal</strong> <em>()</em></dt> 278<dd><p>Return the frame’s symtab and line object. 279See <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>. 280</p></dd></dl> 281 282<a name="gdbpy_005fframe_005fread_005fregister"></a><dl> 283<dt><a name="index-Frame_002eread_005fregister"></a>Function: <strong>Frame.read_register</strong> <em>(register)</em></dt> 284<dd><p>Return the value of <var>register</var> in this frame. Returns a 285<code>Gdb.Value</code> object. Throws an exception if <var>register</var> does 286not exist. The <var>register</var> argument must be one of the following: 287</p><ol> 288<li> A string that is the name of a valid register (e.g., <code>'sp'</code> or 289<code>'rax'</code>). 290</li><li> A <code>gdb.RegisterDescriptor</code> object (see <a href="Registers-In-Python.html#Registers-In-Python">Registers In Python</a>). 291</li><li> A <small>GDB</small> internal, platform specific number. Using these 292numbers is supported for historic reasons, but is not recommended as 293future changes to <small>GDB</small> could change the mapping between 294numbers and the registers they represent, breaking any Python code 295that uses the platform-specific numbers. The numbers are usually 296found in the corresponding <samp><var>platform</var>-tdep.h</samp> file in the 297<small>GDB</small> source tree. 298</li></ol> 299<p>Using a string to access registers will be slightly slower than the 300other two methods as <small>GDB</small> must look up the mapping between 301name and internal register number. If performance is critical 302consider looking up and caching a <code>gdb.RegisterDescriptor</code> 303object. 304</p></dd></dl> 305 306<dl> 307<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> 308<dd><p>Return the value of <var>variable</var> in this frame. If the optional 309argument <var>block</var> is provided, search for the variable from that 310block; otherwise start at the frame’s current block (which is 311determined by the frame’s current program counter). The <var>variable</var> 312argument must be a string or a <code>gdb.Symbol</code> object; <var>block</var> must be a 313<code>gdb.Block</code> object. 314</p></dd></dl> 315 316<dl> 317<dt><a name="index-Frame_002eselect"></a>Function: <strong>Frame.select</strong> <em>()</em></dt> 318<dd><p>Set this frame to be the selected frame. See <a href="Stack.html#Stack">Examining the 319Stack</a>. 320</p></dd></dl> 321 322<hr> 323<div class="header"> 324<p> 325Next: <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> [<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> 326</div> 327 328 329 330</body> 331</html> 332