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: Blocks In Python</title>
18
19<meta name="description" content="Debugging with GDB: Blocks In Python">
20<meta name="keywords" content="Debugging with GDB: Blocks 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="Symbols-In-Python.html#Symbols-In-Python" rel="next" title="Symbols In Python">
30<link href="Frames-In-Python.html#Frames-In-Python" rel="previous" title="Frames 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="Blocks-In-Python"></a>
65<div class="header">
66<p>
67Next: <a href="Symbols-In-Python.html#Symbols-In-Python" accesskey="n" rel="next">Symbols In Python</a>, Previous: <a href="Frames-In-Python.html#Frames-In-Python" accesskey="p" rel="previous">Frames 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>
68</div>
69<hr>
70<a name="Accessing-blocks-from-Python"></a>
71<h4 class="subsubsection">23.2.2.26 Accessing blocks from Python</h4>
72
73<a name="index-blocks-in-python"></a>
74<a name="index-gdb_002eBlock"></a>
75
76<p>In <small>GDB</small>, symbols are stored in blocks.  A block corresponds
77roughly to a scope in the source code.  Blocks are organized
78hierarchically, and are represented individually in Python as a
79<code>gdb.Block</code>.  Blocks rely on debugging information being
80available.
81</p>
82<p>A frame has a block.  Please see <a href="Frames-In-Python.html#Frames-In-Python">Frames In Python</a>, for a more
83in-depth discussion of frames.
84</p>
85<p>The outermost block is known as the <em>global block</em>.  The global
86block typically holds public global variables and functions.
87</p>
88<p>The block nested just inside the global block is the <em>static
89block</em>.  The static block typically holds file-scoped variables and
90functions.
91</p>
92<p><small>GDB</small> provides a method to get a block&rsquo;s superblock, but there
93is currently no way to examine the sub-blocks of a block, or to
94iterate over all the blocks in a symbol table (see <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>).
95</p>
96<p>Here is a short example that should help explain blocks:
97</p>
98<div class="smallexample">
99<pre class="smallexample">/* This is in the global block.  */
100int global;
101
102/* This is in the static block.  */
103static int file_scope;
104
105/* 'function' is in the global block, and 'argument' is
106   in a block nested inside of 'function'.  */
107int function (int argument)
108{
109  /* 'local' is in a block inside 'function'.  It may or may
110     not be in the same block as 'argument'.  */
111  int local;
112
113  {
114     /* 'inner' is in a block whose superblock is the one holding
115        'local'.  */
116     int inner;
117
118     /* If this call is expanded by the compiler, you may see
119        a nested block here whose function is 'inline_function'
120        and whose superblock is the one holding 'inner'.  */
121     inline_function ();
122  }
123}
124</pre></div>
125
126<p>A <code>gdb.Block</code> is iterable.  The iterator returns the symbols
127(see <a href="Symbols-In-Python.html#Symbols-In-Python">Symbols In Python</a>) local to the block.  Python programs
128should not assume that a specific block object will always contain a
129given symbol, since changes in <small>GDB</small> features and
130infrastructure may cause symbols move across blocks in a symbol
131table.  You can also use Python&rsquo;s <em>dictionary syntax</em> to access
132variables in this block, e.g.:
133</p>
134<div class="smallexample">
135<pre class="smallexample">symbol = some_block['variable']  # symbol is of type gdb.Symbol
136</pre></div>
137
138<p>The following block-related functions are available in the <code>gdb</code>
139module:
140</p>
141<a name="index-gdb_002eblock_005ffor_005fpc"></a>
142<dl>
143<dt><a name="index-gdb_002eblock_005ffor_005fpc-1"></a>Function: <strong>gdb.block_for_pc</strong> <em>(pc)</em></dt>
144<dd><p>Return the innermost <code>gdb.Block</code> containing the given <var>pc</var>
145value.  If the block cannot be found for the <var>pc</var> value specified,
146the function will return <code>None</code>.  This is identical to
147<code>gdb.current_progspace().block_for_pc(pc)</code> and is included for
148historical compatibility.
149</p></dd></dl>
150
151<p>A <code>gdb.Block</code> object has the following methods:
152</p>
153<dl>
154<dt><a name="index-Block_002eis_005fvalid"></a>Function: <strong>Block.is_valid</strong> <em>()</em></dt>
155<dd><p>Returns <code>True</code> if the <code>gdb.Block</code> object is valid,
156<code>False</code> if not.  A block object can become invalid if the block it
157refers to doesn&rsquo;t exist anymore in the inferior.  All other
158<code>gdb.Block</code> methods will throw an exception if it is invalid at
159the time the method is called.  The block&rsquo;s validity is also checked
160during iteration over symbols of the block.
161</p></dd></dl>
162
163<p>A <code>gdb.Block</code> object has the following attributes:
164</p>
165<dl>
166<dt><a name="index-Block_002estart"></a>Variable: <strong>Block.start</strong></dt>
167<dd><p>The start address of the block.  This attribute is not writable.
168</p></dd></dl>
169
170<dl>
171<dt><a name="index-Block_002eend"></a>Variable: <strong>Block.end</strong></dt>
172<dd><p>One past the last address that appears in the block.  This attribute
173is not writable.
174</p></dd></dl>
175
176<dl>
177<dt><a name="index-Block_002efunction"></a>Variable: <strong>Block.function</strong></dt>
178<dd><p>The name of the block represented as a <code>gdb.Symbol</code>.  If the
179block is not named, then this attribute holds <code>None</code>.  This
180attribute is not writable.
181</p>
182<p>For ordinary function blocks, the superblock is the static block.
183However, you should note that it is possible for a function block to
184have a superblock that is not the static block &ndash; for instance this
185happens for an inlined function.
186</p></dd></dl>
187
188<dl>
189<dt><a name="index-Block_002esuperblock"></a>Variable: <strong>Block.superblock</strong></dt>
190<dd><p>The block containing this block.  If this parent block does not exist,
191this attribute holds <code>None</code>.  This attribute is not writable.
192</p></dd></dl>
193
194<dl>
195<dt><a name="index-Block_002eglobal_005fblock"></a>Variable: <strong>Block.global_block</strong></dt>
196<dd><p>The global block associated with this block.  This attribute is not
197writable.
198</p></dd></dl>
199
200<dl>
201<dt><a name="index-Block_002estatic_005fblock"></a>Variable: <strong>Block.static_block</strong></dt>
202<dd><p>The static block associated with this block.  This attribute is not
203writable.
204</p></dd></dl>
205
206<dl>
207<dt><a name="index-Block_002eis_005fglobal"></a>Variable: <strong>Block.is_global</strong></dt>
208<dd><p><code>True</code> if the <code>gdb.Block</code> object is a global block,
209<code>False</code> if not.  This attribute is not
210writable.
211</p></dd></dl>
212
213<dl>
214<dt><a name="index-Block_002eis_005fstatic"></a>Variable: <strong>Block.is_static</strong></dt>
215<dd><p><code>True</code> if the <code>gdb.Block</code> object is a static block,
216<code>False</code> if not.  This attribute is not writable.
217</p></dd></dl>
218
219<hr>
220<div class="header">
221<p>
222Next: <a href="Symbols-In-Python.html#Symbols-In-Python" accesskey="n" rel="next">Symbols In Python</a>, Previous: <a href="Frames-In-Python.html#Frames-In-Python" accesskey="p" rel="previous">Frames 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>
223</div>
224
225
226
227</body>
228</html>
229