您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Progspaces-In-Python.html 11KB

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