Line-Tables-In-Python.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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: Line Tables In Python</title>
  16. <meta name="description" content="Debugging with GDB: Line Tables In Python">
  17. <meta name="keywords" content="Debugging with GDB: Line Tables 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="Breakpoints-In-Python.html#Breakpoints-In-Python" rel="next" title="Breakpoints In Python">
  27. <link href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python" rel="previous" title="Symbol Tables 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="Line-Tables-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Breakpoints-In-Python.html#Breakpoints-In-Python" accesskey="n" rel="next">Breakpoints In Python</a>, Previous: <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python" accesskey="p" rel="previous">Symbol Tables 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="Manipulating-line-tables-using-Python"></a>
  65. <h4 class="subsubsection">23.2.2.29 Manipulating line tables using Python</h4>
  66. <a name="index-line-tables-in-python"></a>
  67. <a name="index-gdb_002eLineTable"></a>
  68. <p>Python code can request and inspect line table information from a
  69. symbol table that is loaded in <small>GDB</small>. A line table is a
  70. mapping of source lines to their executable locations in memory. To
  71. acquire the line table information for a particular symbol table, use
  72. the <code>linetable</code> function (see <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python">Symbol Tables In Python</a>).
  73. </p>
  74. <p>A <code>gdb.LineTable</code> is iterable. The iterator returns
  75. <code>LineTableEntry</code> objects that correspond to the source line and
  76. address for each line table entry. <code>LineTableEntry</code> objects have
  77. the following attributes:
  78. </p>
  79. <dl>
  80. <dt><a name="index-LineTableEntry_002eline"></a>Variable: <strong>LineTableEntry.line</strong></dt>
  81. <dd><p>The source line number for this line table entry. This number
  82. corresponds to the actual line of source. This attribute is not
  83. writable.
  84. </p></dd></dl>
  85. <dl>
  86. <dt><a name="index-LineTableEntry_002epc"></a>Variable: <strong>LineTableEntry.pc</strong></dt>
  87. <dd><p>The address that is associated with the line table entry where the
  88. executable code for that source line resides in memory. This
  89. attribute is not writable.
  90. </p></dd></dl>
  91. <p>As there can be multiple addresses for a single source line, you may
  92. receive multiple <code>LineTableEntry</code> objects with matching
  93. <code>line</code> attributes, but with different <code>pc</code> attributes. The
  94. iterator is sorted in ascending <code>pc</code> order. Here is a small
  95. example illustrating iterating over a line table.
  96. </p>
  97. <div class="smallexample">
  98. <pre class="smallexample">symtab = gdb.selected_frame().find_sal().symtab
  99. linetable = symtab.linetable()
  100. for line in linetable:
  101. print &quot;Line: &quot;+str(line.line)+&quot; Address: &quot;+hex(line.pc)
  102. </pre></div>
  103. <p>This will have the following output:
  104. </p>
  105. <div class="smallexample">
  106. <pre class="smallexample">Line: 33 Address: 0x4005c8L
  107. Line: 37 Address: 0x4005caL
  108. Line: 39 Address: 0x4005d2L
  109. Line: 40 Address: 0x4005f8L
  110. Line: 42 Address: 0x4005ffL
  111. Line: 44 Address: 0x400608L
  112. Line: 42 Address: 0x40060cL
  113. Line: 45 Address: 0x400615L
  114. </pre></div>
  115. <p>In addition to being able to iterate over a <code>LineTable</code>, it also
  116. has the following direct access methods:
  117. </p>
  118. <dl>
  119. <dt><a name="index-LineTable_002eline"></a>Function: <strong>LineTable.line</strong> <em>(line)</em></dt>
  120. <dd><p>Return a Python <code>Tuple</code> of <code>LineTableEntry</code> objects for any
  121. entries in the line table for the given <var>line</var>, which specifies
  122. the source code line. If there are no entries for that source code
  123. <var>line</var>, the Python <code>None</code> is returned.
  124. </p></dd></dl>
  125. <dl>
  126. <dt><a name="index-LineTable_002ehas_005fline"></a>Function: <strong>LineTable.has_line</strong> <em>(line)</em></dt>
  127. <dd><p>Return a Python <code>Boolean</code> indicating whether there is an entry in
  128. the line table for this source line. Return <code>True</code> if an entry
  129. is found, or <code>False</code> if not.
  130. </p></dd></dl>
  131. <dl>
  132. <dt><a name="index-LineTable_002esource_005flines"></a>Function: <strong>LineTable.source_lines</strong> <em>()</em></dt>
  133. <dd><p>Return a Python <code>List</code> of the source line numbers in the symbol
  134. table. Only lines with executable code locations are returned. The
  135. contents of the <code>List</code> will just be the source line entries
  136. represented as Python <code>Long</code> values.
  137. </p></dd></dl>
  138. <hr>
  139. <div class="header">
  140. <p>
  141. Next: <a href="Breakpoints-In-Python.html#Breakpoints-In-Python" accesskey="n" rel="next">Breakpoints In Python</a>, Previous: <a href="Symbol-Tables-In-Python.html#Symbol-Tables-In-Python" accesskey="p" rel="previous">Symbol Tables 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>
  142. </div>
  143. </body>
  144. </html>