hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
 
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.
 
(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual.  Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Blocks In Python</title>
 
<meta name="description" content="Debugging with GDB: Blocks In Python">
<meta name="keywords" content="Debugging with GDB: Blocks In Python">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Python-API.html#Python-API" rel="up" title="Python API">
<link href="Symbols-In-Python.html#Symbols-In-Python" rel="next" title="Symbols In Python">
<link href="Frames-In-Python.html#Frames-In-Python" rel="previous" title="Frames In Python">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
 
 
</head>
 
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Blocks-In-Python"></a>
<div class="header">
<p>
Next: <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>
</div>
<hr>
<a name="Accessing-blocks-from-Python"></a>
<h4 class="subsubsection">23.2.2.26 Accessing blocks from Python</h4>
 
<a name="index-blocks-in-python"></a>
<a name="index-gdb_002eBlock"></a>
 
<p>In <small>GDB</small>, symbols are stored in blocks.  A block corresponds
roughly to a scope in the source code.  Blocks are organized
hierarchically, and are represented individually in Python as a
<code>gdb.Block</code>.  Blocks rely on debugging information being
available.
</p>
<p>A frame has a block.  Please see <a href="Frames-In-Python.html#Frames-In-Python">Frames In Python</a>, for a more
in-depth discussion of frames.
</p>
<p>The outermost block is known as the <em>global block</em>.  The global
block typically holds public global variables and functions.
</p>
<p>The block nested just inside the global block is the <em>static
block</em>.  The static block typically holds file-scoped variables and
functions.
</p>
<p><small>GDB</small> provides a method to get a block&rsquo;s superblock, but there
is currently no way to examine the sub-blocks of a block, or to
iterate 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>).
</p>
<p>Here is a short example that should help explain blocks:
</p>
<div class="smallexample">
<pre class="smallexample">/* This is in the global block.  */
int global;
 
/* This is in the static block.  */
static int file_scope;
 
/* 'function' is in the global block, and 'argument' is
   in a block nested inside of 'function'.  */
int function (int argument)
{
  /* 'local' is in a block inside 'function'.  It may or may
     not be in the same block as 'argument'.  */
  int local;
 
  {
     /* 'inner' is in a block whose superblock is the one holding
        'local'.  */
     int inner;
 
     /* If this call is expanded by the compiler, you may see
        a nested block here whose function is 'inline_function'
        and whose superblock is the one holding 'inner'.  */
     inline_function ();
  }
}
</pre></div>
 
<p>A <code>gdb.Block</code> is iterable.  The iterator returns the symbols
(see <a href="Symbols-In-Python.html#Symbols-In-Python">Symbols In Python</a>) local to the block.  Python programs
should not assume that a specific block object will always contain a
given symbol, since changes in <small>GDB</small> features and
infrastructure may cause symbols move across blocks in a symbol
table.  You can also use Python&rsquo;s <em>dictionary syntax</em> to access
variables in this block, e.g.:
</p>
<div class="smallexample">
<pre class="smallexample">symbol = some_block['variable']  # symbol is of type gdb.Symbol
</pre></div>
 
<p>The following block-related functions are available in the <code>gdb</code>
module:
</p>
<a name="index-gdb_002eblock_005ffor_005fpc"></a>
<dl>
<dt><a name="index-gdb_002eblock_005ffor_005fpc-1"></a>Function: <strong>gdb.block_for_pc</strong> <em>(pc)</em></dt>
<dd><p>Return the innermost <code>gdb.Block</code> containing the given <var>pc</var>
value.  If the block cannot be found for the <var>pc</var> value specified,
the function will return <code>None</code>.  This is identical to
<code>gdb.current_progspace().block_for_pc(pc)</code> and is included for
historical compatibility.
</p></dd></dl>
 
<p>A <code>gdb.Block</code> object has the following methods:
</p>
<dl>
<dt><a name="index-Block_002eis_005fvalid"></a>Function: <strong>Block.is_valid</strong> <em>()</em></dt>
<dd><p>Returns <code>True</code> if the <code>gdb.Block</code> object is valid,
<code>False</code> if not.  A block object can become invalid if the block it
refers to doesn&rsquo;t exist anymore in the inferior.  All other
<code>gdb.Block</code> methods will throw an exception if it is invalid at
the time the method is called.  The block&rsquo;s validity is also checked
during iteration over symbols of the block.
</p></dd></dl>
 
<p>A <code>gdb.Block</code> object has the following attributes:
</p>
<dl>
<dt><a name="index-Block_002estart"></a>Variable: <strong>Block.start</strong></dt>
<dd><p>The start address of the block.  This attribute is not writable.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002eend"></a>Variable: <strong>Block.end</strong></dt>
<dd><p>One past the last address that appears in the block.  This attribute
is not writable.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002efunction"></a>Variable: <strong>Block.function</strong></dt>
<dd><p>The name of the block represented as a <code>gdb.Symbol</code>.  If the
block is not named, then this attribute holds <code>None</code>.  This
attribute is not writable.
</p>
<p>For ordinary function blocks, the superblock is the static block.
However, you should note that it is possible for a function block to
have a superblock that is not the static block &ndash; for instance this
happens for an inlined function.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002esuperblock"></a>Variable: <strong>Block.superblock</strong></dt>
<dd><p>The block containing this block.  If this parent block does not exist,
this attribute holds <code>None</code>.  This attribute is not writable.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002eglobal_005fblock"></a>Variable: <strong>Block.global_block</strong></dt>
<dd><p>The global block associated with this block.  This attribute is not
writable.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002estatic_005fblock"></a>Variable: <strong>Block.static_block</strong></dt>
<dd><p>The static block associated with this block.  This attribute is not
writable.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002eis_005fglobal"></a>Variable: <strong>Block.is_global</strong></dt>
<dd><p><code>True</code> if the <code>gdb.Block</code> object is a global block,
<code>False</code> if not.  This attribute is not
writable.
</p></dd></dl>
 
<dl>
<dt><a name="index-Block_002eis_005fstatic"></a>Variable: <strong>Block.is_static</strong></dt>
<dd><p><code>True</code> if the <code>gdb.Block</code> object is a static block,
<code>False</code> if not.  This attribute is not writable.
</p></dd></dl>
 
<hr>
<div class="header">
<p>
Next: <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>
</div>
 
 
 
</body>
</html>