You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Recordings-In-Python.html 14KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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>Recordings In Python (Debugging with GDB)</title>
  17. <meta name="description" content="Recordings In Python (Debugging with GDB)">
  18. <meta name="keywords" content="Recordings 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="Commands-In-Python.html#Commands-In-Python" rel="next" title="Commands In Python">
  27. <link href="Threads-In-Python.html#Threads-In-Python" rel="prev" title="Threads 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="Recordings-In-Python"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Commands-In-Python.html#Commands-In-Python" accesskey="n" rel="next">Commands In Python</a>, Previous: <a href="Threads-In-Python.html#Threads-In-Python" accesskey="p" rel="prev">Threads 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="Recordings-In-Python-1"></a>
  64. <h4 class="subsubsection">23.2.2.19 Recordings In Python</h4>
  65. <a name="index-recordings-in-python"></a>
  66. <p>The following recordings-related functions
  67. (see <a href="Process-Record-and-Replay.html#Process-Record-and-Replay">Process Record and Replay</a>) are available in the <code>gdb</code>
  68. module:
  69. </p>
  70. <dl>
  71. <dt><a name="index-gdb_002estart_005frecording"></a>Function: <strong>gdb.start_recording</strong> <em>(<span class="roman">[</span>method<span class="roman">]</span>, <span class="roman">[</span>format<span class="roman">]</span>)</em></dt>
  72. <dd><p>Start a recording using the given <var>method</var> and <var>format</var>. If
  73. no <var>format</var> is given, the default format for the recording method
  74. is used. If no <var>method</var> is given, the default method will be used.
  75. Returns a <code>gdb.Record</code> object on success. Throw an exception on
  76. failure.
  77. </p>
  78. <p>The following strings can be passed as <var>method</var>:
  79. </p>
  80. <ul>
  81. <li> <code>&quot;full&quot;</code>
  82. </li><li> <code>&quot;btrace&quot;</code>: Possible values for <var>format</var>: <code>&quot;pt&quot;</code>,
  83. <code>&quot;bts&quot;</code> or leave out for default format.
  84. </li></ul>
  85. </dd></dl>
  86. <dl>
  87. <dt><a name="index-gdb_002ecurrent_005frecording"></a>Function: <strong>gdb.current_recording</strong> <em>()</em></dt>
  88. <dd><p>Access a currently running recording. Return a <code>gdb.Record</code>
  89. object on success. Return <code>None</code> if no recording is currently
  90. active.
  91. </p></dd></dl>
  92. <dl>
  93. <dt><a name="index-gdb_002estop_005frecording"></a>Function: <strong>gdb.stop_recording</strong> <em>()</em></dt>
  94. <dd><p>Stop the current recording. Throw an exception if no recording is
  95. currently active. All record objects become invalid after this call.
  96. </p></dd></dl>
  97. <p>A <code>gdb.Record</code> object has the following attributes:
  98. </p>
  99. <dl>
  100. <dt><a name="index-Record_002emethod"></a>Variable: <strong>Record.method</strong></dt>
  101. <dd><p>A string with the current recording method, e.g. <code>full</code> or
  102. <code>btrace</code>.
  103. </p></dd></dl>
  104. <dl>
  105. <dt><a name="index-Record_002eformat"></a>Variable: <strong>Record.format</strong></dt>
  106. <dd><p>A string with the current recording format, e.g. <code>bt</code>, <code>pts</code> or
  107. <code>None</code>.
  108. </p></dd></dl>
  109. <dl>
  110. <dt><a name="index-Record_002ebegin"></a>Variable: <strong>Record.begin</strong></dt>
  111. <dd><p>A method specific instruction object representing the first instruction
  112. in this recording.
  113. </p></dd></dl>
  114. <dl>
  115. <dt><a name="index-Record_002eend"></a>Variable: <strong>Record.end</strong></dt>
  116. <dd><p>A method specific instruction object representing the current
  117. instruction, that is not actually part of the recording.
  118. </p></dd></dl>
  119. <dl>
  120. <dt><a name="index-Record_002ereplay_005fposition"></a>Variable: <strong>Record.replay_position</strong></dt>
  121. <dd><p>The instruction representing the current replay position. If there is
  122. no replay active, this will be <code>None</code>.
  123. </p></dd></dl>
  124. <dl>
  125. <dt><a name="index-Record_002einstruction_005fhistory"></a>Variable: <strong>Record.instruction_history</strong></dt>
  126. <dd><p>A list with all recorded instructions.
  127. </p></dd></dl>
  128. <dl>
  129. <dt><a name="index-Record_002efunction_005fcall_005fhistory"></a>Variable: <strong>Record.function_call_history</strong></dt>
  130. <dd><p>A list with all recorded function call segments.
  131. </p></dd></dl>
  132. <p>A <code>gdb.Record</code> object has the following methods:
  133. </p>
  134. <dl>
  135. <dt><a name="index-Record_002egoto"></a>Function: <strong>Record.goto</strong> <em>(instruction)</em></dt>
  136. <dd><p>Move the replay position to the given <var>instruction</var>.
  137. </p></dd></dl>
  138. <p>The common <code>gdb.Instruction</code> class that recording method specific
  139. instruction objects inherit from, has the following attributes:
  140. </p>
  141. <dl>
  142. <dt><a name="index-Instruction_002epc"></a>Variable: <strong>Instruction.pc</strong></dt>
  143. <dd><p>An integer representing this instruction&rsquo;s address.
  144. </p></dd></dl>
  145. <dl>
  146. <dt><a name="index-Instruction_002edata"></a>Variable: <strong>Instruction.data</strong></dt>
  147. <dd><p>A buffer with the raw instruction data. In Python 3, the return value is a
  148. <code>memoryview</code> object.
  149. </p></dd></dl>
  150. <dl>
  151. <dt><a name="index-Instruction_002edecoded"></a>Variable: <strong>Instruction.decoded</strong></dt>
  152. <dd><p>A human readable string with the disassembled instruction.
  153. </p></dd></dl>
  154. <dl>
  155. <dt><a name="index-Instruction_002esize"></a>Variable: <strong>Instruction.size</strong></dt>
  156. <dd><p>The size of the instruction in bytes.
  157. </p></dd></dl>
  158. <p>Additionally <code>gdb.RecordInstruction</code> has the following attributes:
  159. </p>
  160. <dl>
  161. <dt><a name="index-RecordInstruction_002enumber"></a>Variable: <strong>RecordInstruction.number</strong></dt>
  162. <dd><p>An integer identifying this instruction. <code>number</code> corresponds to
  163. the numbers seen in <code>record instruction-history</code>
  164. (see <a href="Process-Record-and-Replay.html#Process-Record-and-Replay">Process Record and Replay</a>).
  165. </p></dd></dl>
  166. <dl>
  167. <dt><a name="index-RecordInstruction_002esal"></a>Variable: <strong>RecordInstruction.sal</strong></dt>
  168. <dd><p>A <code>gdb.Symtab_and_line</code> object representing the associated symtab
  169. and line of this instruction. May be <code>None</code> if no debug information is
  170. available.
  171. </p></dd></dl>
  172. <dl>
  173. <dt><a name="index-RecordInstruction_002eis_005fspeculative"></a>Variable: <strong>RecordInstruction.is_speculative</strong></dt>
  174. <dd><p>A boolean indicating whether the instruction was executed speculatively.
  175. </p></dd></dl>
  176. <p>If an error occured during recording or decoding a recording, this error is
  177. represented by a <code>gdb.RecordGap</code> object in the instruction list. It has
  178. the following attributes:
  179. </p>
  180. <dl>
  181. <dt><a name="index-RecordGap_002enumber"></a>Variable: <strong>RecordGap.number</strong></dt>
  182. <dd><p>An integer identifying this gap. <code>number</code> corresponds to the numbers seen
  183. in <code>record instruction-history</code> (see <a href="Process-Record-and-Replay.html#Process-Record-and-Replay">Process Record and Replay</a>).
  184. </p></dd></dl>
  185. <dl>
  186. <dt><a name="index-RecordGap_002eerror_005fcode"></a>Variable: <strong>RecordGap.error_code</strong></dt>
  187. <dd><p>A numerical representation of the reason for the gap. The value is specific to
  188. the current recording method.
  189. </p></dd></dl>
  190. <dl>
  191. <dt><a name="index-RecordGap_002eerror_005fstring"></a>Variable: <strong>RecordGap.error_string</strong></dt>
  192. <dd><p>A human readable string with the reason for the gap.
  193. </p></dd></dl>
  194. <p>A <code>gdb.RecordFunctionSegment</code> object has the following attributes:
  195. </p>
  196. <dl>
  197. <dt><a name="index-RecordFunctionSegment_002enumber"></a>Variable: <strong>RecordFunctionSegment.number</strong></dt>
  198. <dd><p>An integer identifying this function segment. <code>number</code> corresponds to
  199. the numbers seen in <code>record function-call-history</code>
  200. (see <a href="Process-Record-and-Replay.html#Process-Record-and-Replay">Process Record and Replay</a>).
  201. </p></dd></dl>
  202. <dl>
  203. <dt><a name="index-RecordFunctionSegment_002esymbol"></a>Variable: <strong>RecordFunctionSegment.symbol</strong></dt>
  204. <dd><p>A <code>gdb.Symbol</code> object representing the associated symbol. May be
  205. <code>None</code> if no debug information is available.
  206. </p></dd></dl>
  207. <dl>
  208. <dt><a name="index-RecordFunctionSegment_002elevel"></a>Variable: <strong>RecordFunctionSegment.level</strong></dt>
  209. <dd><p>An integer representing the function call&rsquo;s stack level. May be
  210. <code>None</code> if the function call is a gap.
  211. </p></dd></dl>
  212. <dl>
  213. <dt><a name="index-RecordFunctionSegment_002einstructions"></a>Variable: <strong>RecordFunctionSegment.instructions</strong></dt>
  214. <dd><p>A list of <code>gdb.RecordInstruction</code> or <code>gdb.RecordGap</code> objects
  215. associated with this function call.
  216. </p></dd></dl>
  217. <dl>
  218. <dt><a name="index-RecordFunctionSegment_002eup"></a>Variable: <strong>RecordFunctionSegment.up</strong></dt>
  219. <dd><p>A <code>gdb.RecordFunctionSegment</code> object representing the caller&rsquo;s
  220. function segment. If the call has not been recorded, this will be the
  221. function segment to which control returns. If neither the call nor the
  222. return have been recorded, this will be <code>None</code>.
  223. </p></dd></dl>
  224. <dl>
  225. <dt><a name="index-RecordFunctionSegment_002eprev"></a>Variable: <strong>RecordFunctionSegment.prev</strong></dt>
  226. <dd><p>A <code>gdb.RecordFunctionSegment</code> object representing the previous
  227. segment of this function call. May be <code>None</code>.
  228. </p></dd></dl>
  229. <dl>
  230. <dt><a name="index-RecordFunctionSegment_002enext"></a>Variable: <strong>RecordFunctionSegment.next</strong></dt>
  231. <dd><p>A <code>gdb.RecordFunctionSegment</code> object representing the next segment of
  232. this function call. May be <code>None</code>.
  233. </p></dd></dl>
  234. <p>The following example demonstrates the usage of these objects and
  235. functions to create a function that will rewind a record to the last
  236. time a function in a different file was executed. This would typically
  237. be used to track the execution of user provided callback functions in a
  238. library which typically are not visible in a back trace.
  239. </p>
  240. <div class="smallexample">
  241. <pre class="smallexample">def bringback ():
  242. rec = gdb.current_recording ()
  243. if not rec:
  244. return
  245. insn = rec.instruction_history
  246. if len (insn) == 0:
  247. return
  248. try:
  249. position = insn.index (rec.replay_position)
  250. except:
  251. position = -1
  252. try:
  253. filename = insn[position].sal.symtab.fullname ()
  254. except:
  255. filename = None
  256. for i in reversed (insn[:position]):
  257. try:
  258. current = i.sal.symtab.fullname ()
  259. except:
  260. current = None
  261. if filename == current:
  262. continue
  263. rec.goto (i)
  264. return
  265. </pre></div>
  266. <p>Another possible application is to write a function that counts the
  267. number of code executions in a given line range. This line range can
  268. contain parts of functions or span across several functions and is not
  269. limited to be contiguous.
  270. </p>
  271. <div class="smallexample">
  272. <pre class="smallexample">def countrange (filename, linerange):
  273. count = 0
  274. def filter_only (file_name):
  275. for call in gdb.current_recording ().function_call_history:
  276. try:
  277. if file_name in call.symbol.symtab.fullname ():
  278. yield call
  279. except:
  280. pass
  281. for c in filter_only (filename):
  282. for i in c.instructions:
  283. try:
  284. if i.sal.line in linerange:
  285. count += 1
  286. break;
  287. except:
  288. pass
  289. return count
  290. </pre></div>
  291. <hr>
  292. <div class="header">
  293. <p>
  294. Next: <a href="Commands-In-Python.html#Commands-In-Python" accesskey="n" rel="next">Commands In Python</a>, Previous: <a href="Threads-In-Python.html#Threads-In-Python" accesskey="p" rel="prev">Threads 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>
  295. </div>
  296. </body>
  297. </html>