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: Progspaces In Python</title>
18
19<meta name="description" content="Debugging with GDB: Progspaces In Python">
20<meta name="keywords" content="Debugging with GDB: Progspaces 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="Objfiles-In-Python.html#Objfiles-In-Python" rel="next" title="Objfiles In Python">
30<link href="Functions-In-Python.html#Functions-In-Python" rel="previous" title="Functions 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="Progspaces-In-Python"></a>
65<div class="header">
66<p>
67Next: <a href="Objfiles-In-Python.html#Objfiles-In-Python" accesskey="n" rel="next">Objfiles In Python</a>, Previous: <a href="Functions-In-Python.html#Functions-In-Python" accesskey="p" rel="previous">Functions 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="Program-Spaces-In-Python"></a>
71<h4 class="subsubsection">23.2.2.23 Program Spaces In Python</h4>
72
73<a name="index-progspaces-in-python"></a>
74<a name="index-gdb_002eProgspace"></a>
75<a name="index-Progspace"></a>
76<p>A program space, or <em>progspace</em>, represents a symbolic view
77of an address space.
78It consists of all of the objfiles of the program.
79See <a href="Objfiles-In-Python.html#Objfiles-In-Python">Objfiles In Python</a>.
80See <a href="Inferiors-Connections-and-Programs.html#Inferiors-Connections-and-Programs">program spaces</a>, for more details
81about program spaces.
82</p>
83<p>The following progspace-related functions are available in the
84<code>gdb</code> module:
85</p>
86<a name="index-gdb_002ecurrent_005fprogspace"></a>
87<dl>
88<dt><a name="index-gdb_002ecurrent_005fprogspace-1"></a>Function: <strong>gdb.current_progspace</strong> <em>()</em></dt>
89<dd><p>This function returns the program space of the currently selected inferior.
90See <a href="Inferiors-Connections-and-Programs.html#Inferiors-Connections-and-Programs">Inferiors Connections and Programs</a>.  This is identical to
91<code>gdb.selected_inferior().progspace</code> (see <a href="Inferiors-In-Python.html#Inferiors-In-Python">Inferiors In Python</a>) and is
92included for historical compatibility.
93</p></dd></dl>
94
95<a name="index-gdb_002eprogspaces"></a>
96<dl>
97<dt><a name="index-gdb_002eprogspaces-1"></a>Function: <strong>gdb.progspaces</strong> <em>()</em></dt>
98<dd><p>Return a sequence of all the progspaces currently known to <small>GDB</small>.
99</p></dd></dl>
100
101<p>Each progspace is represented by an instance of the <code>gdb.Progspace</code>
102class.
103</p>
104<dl>
105<dt><a name="index-Progspace_002efilename"></a>Variable: <strong>Progspace.filename</strong></dt>
106<dd><p>The file name of the progspace as a string.
107</p></dd></dl>
108
109<dl>
110<dt><a name="index-Progspace_002epretty_005fprinters"></a>Variable: <strong>Progspace.pretty_printers</strong></dt>
111<dd><p>The <code>pretty_printers</code> attribute is a list of functions.  It is
112used to look up pretty-printers.  A <code>Value</code> is passed to each
113function in order; if the function returns <code>None</code>, then the
114search continues.  Otherwise, the return value should be an object
115which is used to format the value.  See <a href="Pretty-Printing-API.html#Pretty-Printing-API">Pretty Printing API</a>, for more
116information.
117</p></dd></dl>
118
119<dl>
120<dt><a name="index-Progspace_002etype_005fprinters"></a>Variable: <strong>Progspace.type_printers</strong></dt>
121<dd><p>The <code>type_printers</code> attribute is a list of type printer objects.
122See <a href="Type-Printing-API.html#Type-Printing-API">Type Printing API</a>, for more information.
123</p></dd></dl>
124
125<dl>
126<dt><a name="index-Progspace_002eframe_005ffilters"></a>Variable: <strong>Progspace.frame_filters</strong></dt>
127<dd><p>The <code>frame_filters</code> attribute is a dictionary of frame filter
128objects.  See <a href="Frame-Filter-API.html#Frame-Filter-API">Frame Filter API</a>, for more information.
129</p></dd></dl>
130
131<p>A program space has the following methods:
132</p>
133<a name="index-Progspace_002eblock_005ffor_005fpc"></a>
134<dl>
135<dt><a name="index-Progspace_002eblock_005ffor_005fpc-1"></a>Function: <strong>Progspace.block_for_pc</strong> <em>(pc)</em></dt>
136<dd><p>Return the innermost <code>gdb.Block</code> containing the given <var>pc</var>
137value.  If the block cannot be found for the <var>pc</var> value specified,
138the function will return <code>None</code>.
139</p></dd></dl>
140
141<a name="index-Progspace_002efind_005fpc_005fline"></a>
142<dl>
143<dt><a name="index-Progspace_002efind_005fpc_005fline-1"></a>Function: <strong>Progspace.find_pc_line</strong> <em>(pc)</em></dt>
144<dd><p>Return the <code>gdb.Symtab_and_line</code> object corresponding to the
145<var>pc</var> value.  See <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>.  If an invalid value
146of <var>pc</var> is passed as an argument, then the <code>symtab</code> and
147<code>line</code> attributes of the returned <code>gdb.Symtab_and_line</code>
148object will be <code>None</code> and 0 respectively.
149</p></dd></dl>
150
151<a name="index-Progspace_002eis_005fvalid"></a>
152<dl>
153<dt><a name="index-Progspace_002eis_005fvalid-1"></a>Function: <strong>Progspace.is_valid</strong> <em>()</em></dt>
154<dd><p>Returns <code>True</code> if the <code>gdb.Progspace</code> object is valid,
155<code>False</code> if not.  A <code>gdb.Progspace</code> object can become invalid
156if the program space file it refers to is not referenced by any
157inferior.  All other <code>gdb.Progspace</code> methods will throw an
158exception if it is invalid at the time the method is called.
159</p></dd></dl>
160
161<a name="index-Progspace_002eobjfiles"></a>
162<dl>
163<dt><a name="index-Progspace_002eobjfiles-1"></a>Function: <strong>Progspace.objfiles</strong> <em>()</em></dt>
164<dd><p>Return a sequence of all the objfiles referenced by this program
165space.  See <a href="Objfiles-In-Python.html#Objfiles-In-Python">Objfiles In Python</a>.
166</p></dd></dl>
167
168<a name="index-Progspace_002esolib_005fname"></a>
169<dl>
170<dt><a name="index-Progspace_002esolib_005fname-1"></a>Function: <strong>Progspace.solib_name</strong> <em>(address)</em></dt>
171<dd><p>Return the name of the shared library holding the given <var>address</var>
172as a string, or <code>None</code>.
173</p></dd></dl>
174
175<p>One may add arbitrary attributes to <code>gdb.Progspace</code> objects
176in the usual Python way.
177This is useful if, for example, one needs to do some extra record keeping
178associated with the program space.
179</p>
180<p>In this contrived example, we want to perform some processing when
181an objfile with a certain symbol is loaded, but we only want to do
182this once because it is expensive.  To achieve this we record the results
183with the program space because we can&rsquo;t predict when the desired objfile
184will be loaded.
185</p>
186<div class="smallexample">
187<pre class="smallexample">(gdb) python
188def clear_objfiles_handler(event):
189    event.progspace.expensive_computation = None
190def expensive(symbol):
191    &quot;&quot;&quot;A mock routine to perform an &quot;expensive&quot; computation on symbol.&quot;&quot;&quot;
192    print &quot;Computing the answer to the ultimate question ...&quot;
193    return 42
194def new_objfile_handler(event):
195    objfile = event.new_objfile
196    progspace = objfile.progspace
197    if not hasattr(progspace, 'expensive_computation') or \
198            progspace.expensive_computation is None:
199        # We use 'main' for the symbol to keep the example simple.
200        # Note: There's no current way to constrain the lookup
201        # to one objfile.
202        symbol = gdb.lookup_global_symbol('main')
203        if symbol is not None:
204            progspace.expensive_computation = expensive(symbol)
205gdb.events.clear_objfiles.connect(clear_objfiles_handler)
206gdb.events.new_objfile.connect(new_objfile_handler)
207end
208(gdb) file /tmp/hello
209Reading symbols from /tmp/hello...
210Computing the answer to the ultimate question ...
211(gdb) python print gdb.current_progspace().expensive_computation
21242
213(gdb) run
214Starting program: /tmp/hello
215Hello.
216[Inferior 1 (process 4242) exited normally]
217</pre></div>
218
219<hr>
220<div class="header">
221<p>
222Next: <a href="Objfiles-In-Python.html#Objfiles-In-Python" accesskey="n" rel="next">Objfiles In Python</a>, Previous: <a href="Functions-In-Python.html#Functions-In-Python" accesskey="p" rel="previous">Functions 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