Breakpoints-In-Python.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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: Breakpoints In Python</title>
  16. <meta name="description" content="Debugging with GDB: Breakpoints In Python">
  17. <meta name="keywords" content="Debugging with GDB: Breakpoints 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="Finish-Breakpoints-in-Python.html#Finish-Breakpoints-in-Python" rel="next" title="Finish Breakpoints in Python">
  27. <link href="Line-Tables-In-Python.html#Line-Tables-In-Python" rel="previous" title="Line 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="Breakpoints-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Finish-Breakpoints-in-Python.html#Finish-Breakpoints-in-Python" accesskey="n" rel="next">Finish Breakpoints in Python</a>, Previous: <a href="Line-Tables-In-Python.html#Line-Tables-In-Python" accesskey="p" rel="previous">Line 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-breakpoints-using-Python"></a>
  65. <h4 class="subsubsection">23.2.2.30 Manipulating breakpoints using Python</h4>
  66. <a name="index-breakpoints-in-python"></a>
  67. <a name="index-gdb_002eBreakpoint"></a>
  68. <p>Python code can manipulate breakpoints via the <code>gdb.Breakpoint</code>
  69. class.
  70. </p>
  71. <p>A breakpoint can be created using one of the two forms of the
  72. <code>gdb.Breakpoint</code> constructor. The first one accepts a string
  73. like one would pass to the <code>break</code>
  74. (see <a href="Set-Breaks.html#Set-Breaks">Setting Breakpoints</a>) and <code>watch</code>
  75. (see <a href="Set-Watchpoints.html#Set-Watchpoints">Setting Watchpoints</a>) commands, and can be used to
  76. create both breakpoints and watchpoints. The second accepts separate Python
  77. arguments similar to <a href="Explicit-Locations.html#Explicit-Locations">Explicit Locations</a>, and can only be used to create
  78. breakpoints.
  79. </p>
  80. <dl>
  81. <dt><a name="index-Breakpoint_002e_005f_005finit_005f_005f"></a>Function: <strong>Breakpoint.__init__</strong> <em>(spec <span class="roman">[</span>, type <span class="roman">][</span>, wp_class <span class="roman">][</span>, internal <span class="roman">][</span>, temporary <span class="roman">][</span>, qualified <span class="roman">]</span>)</em></dt>
  82. <dd><p>Create a new breakpoint according to <var>spec</var>, which is a string naming the
  83. location of a breakpoint, or an expression that defines a watchpoint. The
  84. string should describe a location in a format recognized by the <code>break</code>
  85. command (see <a href="Set-Breaks.html#Set-Breaks">Setting Breakpoints</a>) or, in the case of a
  86. watchpoint, by the <code>watch</code> command
  87. (see <a href="Set-Watchpoints.html#Set-Watchpoints">Setting Watchpoints</a>).
  88. </p>
  89. <p>The optional <var>type</var> argument specifies the type of the breakpoint to create,
  90. as defined below.
  91. </p>
  92. <p>The optional <var>wp_class</var> argument defines the class of watchpoint to create,
  93. if <var>type</var> is <code>gdb.BP_WATCHPOINT</code>. If <var>wp_class</var> is omitted, it
  94. defaults to <code>gdb.WP_WRITE</code>.
  95. </p>
  96. <p>The optional <var>internal</var> argument allows the breakpoint to become invisible
  97. to the user. The breakpoint will neither be reported when created, nor will it
  98. be listed in the output from <code>info breakpoints</code> (but will be listed with
  99. the <code>maint info breakpoints</code> command).
  100. </p>
  101. <p>The optional <var>temporary</var> argument makes the breakpoint a temporary
  102. breakpoint. Temporary breakpoints are deleted after they have been hit. Any
  103. further access to the Python breakpoint after it has been hit will result in a
  104. runtime error (as that breakpoint has now been automatically deleted).
  105. </p>
  106. <p>The optional <var>qualified</var> argument is a boolean that allows interpreting
  107. the function passed in <code>spec</code> as a fully-qualified name. It is equivalent
  108. to <code>break</code>&rsquo;s <code>-qualified</code> flag (see <a href="Linespec-Locations.html#Linespec-Locations">Linespec Locations</a> and
  109. <a href="Explicit-Locations.html#Explicit-Locations">Explicit Locations</a>).
  110. </p>
  111. </dd></dl>
  112. <dl>
  113. <dt><a name="index-Breakpoint_002e_005f_005finit_005f_005f-1"></a>Function: <strong>Breakpoint.__init__</strong> <em>(<span class="roman">[</span> source <span class="roman">][</span>, function <span class="roman">][</span>, label <span class="roman">][</span>, line <span class="roman">]</span>, <span class="roman">][</span> internal <span class="roman">][</span>, temporary <span class="roman">][</span>, qualified <span class="roman">]</span>)</em></dt>
  114. <dd><p>This second form of creating a new breakpoint specifies the explicit
  115. location (see <a href="Explicit-Locations.html#Explicit-Locations">Explicit Locations</a>) using keywords. The new breakpoint will
  116. be created in the specified source file <var>source</var>, at the specified
  117. <var>function</var>, <var>label</var> and <var>line</var>.
  118. </p>
  119. <p><var>internal</var>, <var>temporary</var> and <var>qualified</var> have the same usage as
  120. explained previously.
  121. </p></dd></dl>
  122. <p>The available types are represented by constants defined in the <code>gdb</code>
  123. module:
  124. </p>
  125. <dl compact="compact">
  126. <dd><a name="index-BP_005fBREAKPOINT"></a>
  127. </dd>
  128. <dt><code>gdb.BP_BREAKPOINT</code>
  129. <a name="index-gdb_002eBP_005fBREAKPOINT"></a>
  130. </dt>
  131. <dd><p>Normal code breakpoint.
  132. </p>
  133. <a name="index-BP_005fWATCHPOINT"></a>
  134. </dd>
  135. <dt><code>gdb.BP_WATCHPOINT</code>
  136. <a name="index-gdb_002eBP_005fWATCHPOINT"></a>
  137. </dt>
  138. <dd><p>Watchpoint breakpoint.
  139. </p>
  140. <a name="index-BP_005fHARDWARE_005fWATCHPOINT"></a>
  141. </dd>
  142. <dt><code>gdb.BP_HARDWARE_WATCHPOINT</code>
  143. <a name="index-gdb_002eBP_005fHARDWARE_005fWATCHPOINT"></a>
  144. </dt>
  145. <dd><p>Hardware assisted watchpoint.
  146. </p>
  147. <a name="index-BP_005fREAD_005fWATCHPOINT"></a>
  148. </dd>
  149. <dt><code>gdb.BP_READ_WATCHPOINT</code>
  150. <a name="index-gdb_002eBP_005fREAD_005fWATCHPOINT"></a>
  151. </dt>
  152. <dd><p>Hardware assisted read watchpoint.
  153. </p>
  154. <a name="index-BP_005fACCESS_005fWATCHPOINT"></a>
  155. </dd>
  156. <dt><code>gdb.BP_ACCESS_WATCHPOINT</code>
  157. <a name="index-gdb_002eBP_005fACCESS_005fWATCHPOINT"></a>
  158. </dt>
  159. <dd><p>Hardware assisted access watchpoint.
  160. </p></dd>
  161. </dl>
  162. <p>The available watchpoint types represented by constants are defined in the
  163. <code>gdb</code> module:
  164. </p>
  165. <dl compact="compact">
  166. <dd><a name="index-WP_005fREAD"></a>
  167. </dd>
  168. <dt><code>gdb.WP_READ</code>
  169. <a name="index-gdb_002eWP_005fREAD"></a>
  170. </dt>
  171. <dd><p>Read only watchpoint.
  172. </p>
  173. <a name="index-WP_005fWRITE"></a>
  174. </dd>
  175. <dt><code>gdb.WP_WRITE</code>
  176. <a name="index-gdb_002eWP_005fWRITE"></a>
  177. </dt>
  178. <dd><p>Write only watchpoint.
  179. </p>
  180. <a name="index-WP_005fACCESS"></a>
  181. </dd>
  182. <dt><code>gdb.WP_ACCESS</code>
  183. <a name="index-gdb_002eWP_005fACCESS"></a>
  184. </dt>
  185. <dd><p>Read/Write watchpoint.
  186. </p></dd>
  187. </dl>
  188. <dl>
  189. <dt><a name="index-Breakpoint_002estop"></a>Function: <strong>Breakpoint.stop</strong> <em>(self)</em></dt>
  190. <dd><p>The <code>gdb.Breakpoint</code> class can be sub-classed and, in
  191. particular, you may choose to implement the <code>stop</code> method.
  192. If this method is defined in a sub-class of <code>gdb.Breakpoint</code>,
  193. it will be called when the inferior reaches any location of a
  194. breakpoint which instantiates that sub-class. If the method returns
  195. <code>True</code>, the inferior will be stopped at the location of the
  196. breakpoint, otherwise the inferior will continue.
  197. </p>
  198. <p>If there are multiple breakpoints at the same location with a
  199. <code>stop</code> method, each one will be called regardless of the
  200. return status of the previous. This ensures that all <code>stop</code>
  201. methods have a chance to execute at that location. In this scenario
  202. if one of the methods returns <code>True</code> but the others return
  203. <code>False</code>, the inferior will still be stopped.
  204. </p>
  205. <p>You should not alter the execution state of the inferior (i.e., step,
  206. next, etc.), alter the current frame context (i.e., change the current
  207. active frame), or alter, add or delete any breakpoint. As a general
  208. rule, you should not alter any data within <small>GDB</small> or the inferior
  209. at this time.
  210. </p>
  211. <p>Example <code>stop</code> implementation:
  212. </p>
  213. <div class="smallexample">
  214. <pre class="smallexample">class MyBreakpoint (gdb.Breakpoint):
  215. def stop (self):
  216. inf_val = gdb.parse_and_eval(&quot;foo&quot;)
  217. if inf_val == 3:
  218. return True
  219. return False
  220. </pre></div>
  221. </dd></dl>
  222. <dl>
  223. <dt><a name="index-Breakpoint_002eis_005fvalid"></a>Function: <strong>Breakpoint.is_valid</strong> <em>()</em></dt>
  224. <dd><p>Return <code>True</code> if this <code>Breakpoint</code> object is valid,
  225. <code>False</code> otherwise. A <code>Breakpoint</code> object can become invalid
  226. if the user deletes the breakpoint. In this case, the object still
  227. exists, but the underlying breakpoint does not. In the cases of
  228. watchpoint scope, the watchpoint remains valid even if execution of the
  229. inferior leaves the scope of that watchpoint.
  230. </p></dd></dl>
  231. <dl>
  232. <dt><a name="index-Breakpoint_002edelete"></a>Function: <strong>Breakpoint.delete</strong> <em>()</em></dt>
  233. <dd><p>Permanently deletes the <small>GDB</small> breakpoint. This also
  234. invalidates the Python <code>Breakpoint</code> object. Any further access
  235. to this object&rsquo;s attributes or methods will raise an error.
  236. </p></dd></dl>
  237. <dl>
  238. <dt><a name="index-Breakpoint_002eenabled"></a>Variable: <strong>Breakpoint.enabled</strong></dt>
  239. <dd><p>This attribute is <code>True</code> if the breakpoint is enabled, and
  240. <code>False</code> otherwise. This attribute is writable. You can use it to enable
  241. or disable the breakpoint.
  242. </p></dd></dl>
  243. <dl>
  244. <dt><a name="index-Breakpoint_002esilent"></a>Variable: <strong>Breakpoint.silent</strong></dt>
  245. <dd><p>This attribute is <code>True</code> if the breakpoint is silent, and
  246. <code>False</code> otherwise. This attribute is writable.
  247. </p>
  248. <p>Note that a breakpoint can also be silent if it has commands and the
  249. first command is <code>silent</code>. This is not reported by the
  250. <code>silent</code> attribute.
  251. </p></dd></dl>
  252. <dl>
  253. <dt><a name="index-Breakpoint_002epending"></a>Variable: <strong>Breakpoint.pending</strong></dt>
  254. <dd><p>This attribute is <code>True</code> if the breakpoint is pending, and
  255. <code>False</code> otherwise. See <a href="Set-Breaks.html#Set-Breaks">Set Breaks</a>. This attribute is
  256. read-only.
  257. </p></dd></dl>
  258. <a name="python_005fbreakpoint_005fthread"></a><dl>
  259. <dt><a name="index-Breakpoint_002ethread"></a>Variable: <strong>Breakpoint.thread</strong></dt>
  260. <dd><p>If the breakpoint is thread-specific, this attribute holds the
  261. thread&rsquo;s global id. If the breakpoint is not thread-specific, this
  262. attribute is <code>None</code>. This attribute is writable.
  263. </p></dd></dl>
  264. <dl>
  265. <dt><a name="index-Breakpoint_002etask"></a>Variable: <strong>Breakpoint.task</strong></dt>
  266. <dd><p>If the breakpoint is Ada task-specific, this attribute holds the Ada task
  267. id. If the breakpoint is not task-specific (or the underlying
  268. language is not Ada), this attribute is <code>None</code>. This attribute
  269. is writable.
  270. </p></dd></dl>
  271. <dl>
  272. <dt><a name="index-Breakpoint_002eignore_005fcount"></a>Variable: <strong>Breakpoint.ignore_count</strong></dt>
  273. <dd><p>This attribute holds the ignore count for the breakpoint, an integer.
  274. This attribute is writable.
  275. </p></dd></dl>
  276. <dl>
  277. <dt><a name="index-Breakpoint_002enumber"></a>Variable: <strong>Breakpoint.number</strong></dt>
  278. <dd><p>This attribute holds the breakpoint&rsquo;s number &mdash; the identifier used by
  279. the user to manipulate the breakpoint. This attribute is not writable.
  280. </p></dd></dl>
  281. <dl>
  282. <dt><a name="index-Breakpoint_002etype"></a>Variable: <strong>Breakpoint.type</strong></dt>
  283. <dd><p>This attribute holds the breakpoint&rsquo;s type &mdash; the identifier used to
  284. determine the actual breakpoint type or use-case. This attribute is not
  285. writable.
  286. </p></dd></dl>
  287. <dl>
  288. <dt><a name="index-Breakpoint_002evisible"></a>Variable: <strong>Breakpoint.visible</strong></dt>
  289. <dd><p>This attribute tells whether the breakpoint is visible to the user
  290. when set, or when the &lsquo;<samp>info breakpoints</samp>&rsquo; command is run. This
  291. attribute is not writable.
  292. </p></dd></dl>
  293. <dl>
  294. <dt><a name="index-Breakpoint_002etemporary"></a>Variable: <strong>Breakpoint.temporary</strong></dt>
  295. <dd><p>This attribute indicates whether the breakpoint was created as a
  296. temporary breakpoint. Temporary breakpoints are automatically deleted
  297. after that breakpoint has been hit. Access to this attribute, and all
  298. other attributes and functions other than the <code>is_valid</code>
  299. function, will result in an error after the breakpoint has been hit
  300. (as it has been automatically deleted). This attribute is not
  301. writable.
  302. </p></dd></dl>
  303. <dl>
  304. <dt><a name="index-Breakpoint_002ehit_005fcount"></a>Variable: <strong>Breakpoint.hit_count</strong></dt>
  305. <dd><p>This attribute holds the hit count for the breakpoint, an integer.
  306. This attribute is writable, but currently it can only be set to zero.
  307. </p></dd></dl>
  308. <dl>
  309. <dt><a name="index-Breakpoint_002elocation"></a>Variable: <strong>Breakpoint.location</strong></dt>
  310. <dd><p>This attribute holds the location of the breakpoint, as specified by
  311. the user. It is a string. If the breakpoint does not have a location
  312. (that is, it is a watchpoint) the attribute&rsquo;s value is <code>None</code>. This
  313. attribute is not writable.
  314. </p></dd></dl>
  315. <dl>
  316. <dt><a name="index-Breakpoint_002eexpression"></a>Variable: <strong>Breakpoint.expression</strong></dt>
  317. <dd><p>This attribute holds a breakpoint expression, as specified by
  318. the user. It is a string. If the breakpoint does not have an
  319. expression (the breakpoint is not a watchpoint) the attribute&rsquo;s value
  320. is <code>None</code>. This attribute is not writable.
  321. </p></dd></dl>
  322. <dl>
  323. <dt><a name="index-Breakpoint_002econdition"></a>Variable: <strong>Breakpoint.condition</strong></dt>
  324. <dd><p>This attribute holds the condition of the breakpoint, as specified by
  325. the user. It is a string. If there is no condition, this attribute&rsquo;s
  326. value is <code>None</code>. This attribute is writable.
  327. </p></dd></dl>
  328. <dl>
  329. <dt><a name="index-Breakpoint_002ecommands"></a>Variable: <strong>Breakpoint.commands</strong></dt>
  330. <dd><p>This attribute holds the commands attached to the breakpoint. If
  331. there are commands, this attribute&rsquo;s value is a string holding all the
  332. commands, separated by newlines. If there are no commands, this
  333. attribute is <code>None</code>. This attribute is writable.
  334. </p></dd></dl>
  335. <hr>
  336. <div class="header">
  337. <p>
  338. Next: <a href="Finish-Breakpoints-in-Python.html#Finish-Breakpoints-in-Python" accesskey="n" rel="next">Finish Breakpoints in Python</a>, Previous: <a href="Line-Tables-In-Python.html#Line-Tables-In-Python" accesskey="p" rel="previous">Line 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>
  339. </div>
  340. </body>
  341. </html>