Blocks-In-Python.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Blocks In Python</title>
  16. <meta name="description" content="Debugging with GDB: Blocks In Python">
  17. <meta name="keywords" content="Debugging with GDB: Blocks In Python">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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="Symbols-In-Python.html#Symbols-In-Python" rel="next" title="Symbols In Python">
  27. <link href="Frames-In-Python.html#Frames-In-Python" rel="previous" title="Frames In Python">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Blocks-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. 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>
  62. </div>
  63. <hr>
  64. <a name="Accessing-blocks-from-Python"></a>
  65. <h4 class="subsubsection">23.2.2.26 Accessing blocks from Python</h4>
  66. <a name="index-blocks-in-python"></a>
  67. <a name="index-gdb_002eBlock"></a>
  68. <p>In <small>GDB</small>, symbols are stored in blocks. A block corresponds
  69. roughly to a scope in the source code. Blocks are organized
  70. hierarchically, and are represented individually in Python as a
  71. <code>gdb.Block</code>. Blocks rely on debugging information being
  72. available.
  73. </p>
  74. <p>A frame has a block. Please see <a href="Frames-In-Python.html#Frames-In-Python">Frames In Python</a>, for a more
  75. in-depth discussion of frames.
  76. </p>
  77. <p>The outermost block is known as the <em>global block</em>. The global
  78. block typically holds public global variables and functions.
  79. </p>
  80. <p>The block nested just inside the global block is the <em>static
  81. block</em>. The static block typically holds file-scoped variables and
  82. functions.
  83. </p>
  84. <p><small>GDB</small> provides a method to get a block&rsquo;s superblock, but there
  85. is currently no way to examine the sub-blocks of a block, or to
  86. 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>).
  87. </p>
  88. <p>Here is a short example that should help explain blocks:
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">/* This is in the global block. */
  92. int global;
  93. /* This is in the static block. */
  94. static int file_scope;
  95. /* 'function' is in the global block, and 'argument' is
  96. in a block nested inside of 'function'. */
  97. int function (int argument)
  98. {
  99. /* 'local' is in a block inside 'function'. It may or may
  100. not be in the same block as 'argument'. */
  101. int local;
  102. {
  103. /* 'inner' is in a block whose superblock is the one holding
  104. 'local'. */
  105. int inner;
  106. /* If this call is expanded by the compiler, you may see
  107. a nested block here whose function is 'inline_function'
  108. and whose superblock is the one holding 'inner'. */
  109. inline_function ();
  110. }
  111. }
  112. </pre></div>
  113. <p>A <code>gdb.Block</code> is iterable. The iterator returns the symbols
  114. (see <a href="Symbols-In-Python.html#Symbols-In-Python">Symbols In Python</a>) local to the block. Python programs
  115. should not assume that a specific block object will always contain a
  116. given symbol, since changes in <small>GDB</small> features and
  117. infrastructure may cause symbols move across blocks in a symbol
  118. table. You can also use Python&rsquo;s <em>dictionary syntax</em> to access
  119. variables in this block, e.g.:
  120. </p>
  121. <div class="smallexample">
  122. <pre class="smallexample">symbol = some_block['variable'] # symbol is of type gdb.Symbol
  123. </pre></div>
  124. <p>The following block-related functions are available in the <code>gdb</code>
  125. module:
  126. </p>
  127. <a name="index-gdb_002eblock_005ffor_005fpc"></a>
  128. <dl>
  129. <dt><a name="index-gdb_002eblock_005ffor_005fpc-1"></a>Function: <strong>gdb.block_for_pc</strong> <em>(pc)</em></dt>
  130. <dd><p>Return the innermost <code>gdb.Block</code> containing the given <var>pc</var>
  131. value. If the block cannot be found for the <var>pc</var> value specified,
  132. the function will return <code>None</code>. This is identical to
  133. <code>gdb.current_progspace().block_for_pc(pc)</code> and is included for
  134. historical compatibility.
  135. </p></dd></dl>
  136. <p>A <code>gdb.Block</code> object has the following methods:
  137. </p>
  138. <dl>
  139. <dt><a name="index-Block_002eis_005fvalid"></a>Function: <strong>Block.is_valid</strong> <em>()</em></dt>
  140. <dd><p>Returns <code>True</code> if the <code>gdb.Block</code> object is valid,
  141. <code>False</code> if not. A block object can become invalid if the block it
  142. refers to doesn&rsquo;t exist anymore in the inferior. All other
  143. <code>gdb.Block</code> methods will throw an exception if it is invalid at
  144. the time the method is called. The block&rsquo;s validity is also checked
  145. during iteration over symbols of the block.
  146. </p></dd></dl>
  147. <p>A <code>gdb.Block</code> object has the following attributes:
  148. </p>
  149. <dl>
  150. <dt><a name="index-Block_002estart"></a>Variable: <strong>Block.start</strong></dt>
  151. <dd><p>The start address of the block. This attribute is not writable.
  152. </p></dd></dl>
  153. <dl>
  154. <dt><a name="index-Block_002eend"></a>Variable: <strong>Block.end</strong></dt>
  155. <dd><p>One past the last address that appears in the block. This attribute
  156. is not writable.
  157. </p></dd></dl>
  158. <dl>
  159. <dt><a name="index-Block_002efunction"></a>Variable: <strong>Block.function</strong></dt>
  160. <dd><p>The name of the block represented as a <code>gdb.Symbol</code>. If the
  161. block is not named, then this attribute holds <code>None</code>. This
  162. attribute is not writable.
  163. </p>
  164. <p>For ordinary function blocks, the superblock is the static block.
  165. However, you should note that it is possible for a function block to
  166. have a superblock that is not the static block &ndash; for instance this
  167. happens for an inlined function.
  168. </p></dd></dl>
  169. <dl>
  170. <dt><a name="index-Block_002esuperblock"></a>Variable: <strong>Block.superblock</strong></dt>
  171. <dd><p>The block containing this block. If this parent block does not exist,
  172. this attribute holds <code>None</code>. This attribute is not writable.
  173. </p></dd></dl>
  174. <dl>
  175. <dt><a name="index-Block_002eglobal_005fblock"></a>Variable: <strong>Block.global_block</strong></dt>
  176. <dd><p>The global block associated with this block. This attribute is not
  177. writable.
  178. </p></dd></dl>
  179. <dl>
  180. <dt><a name="index-Block_002estatic_005fblock"></a>Variable: <strong>Block.static_block</strong></dt>
  181. <dd><p>The static block associated with this block. This attribute is not
  182. writable.
  183. </p></dd></dl>
  184. <dl>
  185. <dt><a name="index-Block_002eis_005fglobal"></a>Variable: <strong>Block.is_global</strong></dt>
  186. <dd><p><code>True</code> if the <code>gdb.Block</code> object is a global block,
  187. <code>False</code> if not. This attribute is not
  188. writable.
  189. </p></dd></dl>
  190. <dl>
  191. <dt><a name="index-Block_002eis_005fstatic"></a>Variable: <strong>Block.is_static</strong></dt>
  192. <dd><p><code>True</code> if the <code>gdb.Block</code> object is a static block,
  193. <code>False</code> if not. This attribute is not writable.
  194. </p></dd></dl>
  195. <hr>
  196. <div class="header">
  197. <p>
  198. 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>
  199. </div>
  200. </body>
  201. </html>