Events-In-Python.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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: Events In Python</title>
  16. <meta name="description" content="Debugging with GDB: Events In Python">
  17. <meta name="keywords" content="Debugging with GDB: Events 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="Threads-In-Python.html#Threads-In-Python" rel="next" title="Threads In Python">
  27. <link href="Inferiors-In-Python.html#Inferiors-In-Python" rel="previous" title="Inferiors 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="Events-In-Python"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Threads-In-Python.html#Threads-In-Python" accesskey="n" rel="next">Threads In Python</a>, Previous: <a href="Inferiors-In-Python.html#Inferiors-In-Python" accesskey="p" rel="previous">Inferiors 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="Events-In-Python-1"></a>
  65. <h4 class="subsubsection">23.2.2.17 Events In Python</h4>
  66. <a name="index-inferior-events-in-Python"></a>
  67. <p><small>GDB</small> provides a general event facility so that Python code can be
  68. notified of various state changes, particularly changes that occur in
  69. the inferior.
  70. </p>
  71. <p>An <em>event</em> is just an object that describes some state change. The
  72. type of the object and its attributes will vary depending on the details
  73. of the change. All the existing events are described below.
  74. </p>
  75. <p>In order to be notified of an event, you must register an event handler
  76. with an <em>event registry</em>. An event registry is an object in the
  77. <code>gdb.events</code> module which dispatches particular events. A registry
  78. provides methods to register and unregister event handlers:
  79. </p>
  80. <dl>
  81. <dt><a name="index-EventRegistry_002econnect"></a>Function: <strong>EventRegistry.connect</strong> <em>(object)</em></dt>
  82. <dd><p>Add the given callable <var>object</var> to the registry. This object will be
  83. called when an event corresponding to this registry occurs.
  84. </p></dd></dl>
  85. <dl>
  86. <dt><a name="index-EventRegistry_002edisconnect"></a>Function: <strong>EventRegistry.disconnect</strong> <em>(object)</em></dt>
  87. <dd><p>Remove the given <var>object</var> from the registry. Once removed, the object
  88. will no longer receive notifications of events.
  89. </p></dd></dl>
  90. <p>Here is an example:
  91. </p>
  92. <div class="smallexample">
  93. <pre class="smallexample">def exit_handler (event):
  94. print &quot;event type: exit&quot;
  95. print &quot;exit code: %d&quot; % (event.exit_code)
  96. gdb.events.exited.connect (exit_handler)
  97. </pre></div>
  98. <p>In the above example we connect our handler <code>exit_handler</code> to the
  99. registry <code>events.exited</code>. Once connected, <code>exit_handler</code> gets
  100. called when the inferior exits. The argument <em>event</em> in this example is
  101. of type <code>gdb.ExitedEvent</code>. As you can see in the example the
  102. <code>ExitedEvent</code> object has an attribute which indicates the exit code of
  103. the inferior.
  104. </p>
  105. <p>The following is a listing of the event registries that are available and
  106. details of the events they emit:
  107. </p>
  108. <dl compact="compact">
  109. <dt><code>events.cont</code></dt>
  110. <dd><p>Emits <code>gdb.ThreadEvent</code>.
  111. </p>
  112. <p>Some events can be thread specific when <small>GDB</small> is running in non-stop
  113. mode. When represented in Python, these events all extend
  114. <code>gdb.ThreadEvent</code>. Note, this event is not emitted directly; instead,
  115. events which are emitted by this or other modules might extend this event.
  116. Examples of these events are <code>gdb.BreakpointEvent</code> and
  117. <code>gdb.ContinueEvent</code>.
  118. </p>
  119. <dl>
  120. <dt><a name="index-ThreadEvent_002einferior_005fthread"></a>Variable: <strong>ThreadEvent.inferior_thread</strong></dt>
  121. <dd><p>In non-stop mode this attribute will be set to the specific thread which was
  122. involved in the emitted event. Otherwise, it will be set to <code>None</code>.
  123. </p></dd></dl>
  124. <p>Emits <code>gdb.ContinueEvent</code> which extends <code>gdb.ThreadEvent</code>.
  125. </p>
  126. <p>This event indicates that the inferior has been continued after a stop. For
  127. inherited attribute refer to <code>gdb.ThreadEvent</code> above.
  128. </p>
  129. </dd>
  130. <dt><code>events.exited</code></dt>
  131. <dd><p>Emits <code>events.ExitedEvent</code> which indicates that the inferior has exited.
  132. <code>events.ExitedEvent</code> has two attributes:
  133. </p><dl>
  134. <dt><a name="index-ExitedEvent_002eexit_005fcode"></a>Variable: <strong>ExitedEvent.exit_code</strong></dt>
  135. <dd><p>An integer representing the exit code, if available, which the inferior
  136. has returned. (The exit code could be unavailable if, for example,
  137. <small>GDB</small> detaches from the inferior.) If the exit code is unavailable,
  138. the attribute does not exist.
  139. </p></dd></dl>
  140. <dl>
  141. <dt><a name="index-ExitedEvent_002einferior"></a>Variable: <strong>ExitedEvent.inferior</strong></dt>
  142. <dd><p>A reference to the inferior which triggered the <code>exited</code> event.
  143. </p></dd></dl>
  144. </dd>
  145. <dt><code>events.stop</code></dt>
  146. <dd><p>Emits <code>gdb.StopEvent</code> which extends <code>gdb.ThreadEvent</code>.
  147. </p>
  148. <p>Indicates that the inferior has stopped. All events emitted by this registry
  149. extend StopEvent. As a child of <code>gdb.ThreadEvent</code>, <code>gdb.StopEvent</code>
  150. will indicate the stopped thread when <small>GDB</small> is running in non-stop
  151. mode. Refer to <code>gdb.ThreadEvent</code> above for more details.
  152. </p>
  153. <p>Emits <code>gdb.SignalEvent</code> which extends <code>gdb.StopEvent</code>.
  154. </p>
  155. <p>This event indicates that the inferior or one of its threads has received as
  156. signal. <code>gdb.SignalEvent</code> has the following attributes:
  157. </p>
  158. <dl>
  159. <dt><a name="index-SignalEvent_002estop_005fsignal"></a>Variable: <strong>SignalEvent.stop_signal</strong></dt>
  160. <dd><p>A string representing the signal received by the inferior. A list of possible
  161. signal values can be obtained by running the command <code>info signals</code> in
  162. the <small>GDB</small> command prompt.
  163. </p></dd></dl>
  164. <p>Also emits <code>gdb.BreakpointEvent</code> which extends <code>gdb.StopEvent</code>.
  165. </p>
  166. <p><code>gdb.BreakpointEvent</code> event indicates that one or more breakpoints have
  167. been hit, and has the following attributes:
  168. </p>
  169. <dl>
  170. <dt><a name="index-BreakpointEvent_002ebreakpoints"></a>Variable: <strong>BreakpointEvent.breakpoints</strong></dt>
  171. <dd><p>A sequence containing references to all the breakpoints (type
  172. <code>gdb.Breakpoint</code>) that were hit.
  173. See <a href="Breakpoints-In-Python.html#Breakpoints-In-Python">Breakpoints In Python</a>, for details of the <code>gdb.Breakpoint</code> object.
  174. </p></dd></dl>
  175. <dl>
  176. <dt><a name="index-BreakpointEvent_002ebreakpoint"></a>Variable: <strong>BreakpointEvent.breakpoint</strong></dt>
  177. <dd><p>A reference to the first breakpoint that was hit.
  178. This function is maintained for backward compatibility and is now deprecated
  179. in favor of the <code>gdb.BreakpointEvent.breakpoints</code> attribute.
  180. </p></dd></dl>
  181. </dd>
  182. <dt><code>events.new_objfile</code></dt>
  183. <dd><p>Emits <code>gdb.NewObjFileEvent</code> which indicates that a new object file has
  184. been loaded by <small>GDB</small>. <code>gdb.NewObjFileEvent</code> has one attribute:
  185. </p>
  186. <dl>
  187. <dt><a name="index-NewObjFileEvent_002enew_005fobjfile"></a>Variable: <strong>NewObjFileEvent.new_objfile</strong></dt>
  188. <dd><p>A reference to the object file (<code>gdb.Objfile</code>) which has been loaded.
  189. See <a href="Objfiles-In-Python.html#Objfiles-In-Python">Objfiles In Python</a>, for details of the <code>gdb.Objfile</code> object.
  190. </p></dd></dl>
  191. </dd>
  192. <dt><code>events.clear_objfiles</code></dt>
  193. <dd><p>Emits <code>gdb.ClearObjFilesEvent</code> which indicates that the list of object
  194. files for a program space has been reset.
  195. <code>gdb.ClearObjFilesEvent</code> has one attribute:
  196. </p>
  197. <dl>
  198. <dt><a name="index-ClearObjFilesEvent_002eprogspace"></a>Variable: <strong>ClearObjFilesEvent.progspace</strong></dt>
  199. <dd><p>A reference to the program space (<code>gdb.Progspace</code>) whose objfile list has
  200. been cleared. See <a href="Progspaces-In-Python.html#Progspaces-In-Python">Progspaces In Python</a>.
  201. </p></dd></dl>
  202. </dd>
  203. <dt><code>events.inferior_call</code></dt>
  204. <dd><p>Emits events just before and after a function in the inferior is
  205. called by <small>GDB</small>. Before an inferior call, this emits an event
  206. of type <code>gdb.InferiorCallPreEvent</code>, and after an inferior call,
  207. this emits an event of type <code>gdb.InferiorCallPostEvent</code>.
  208. </p>
  209. <dl compact="compact">
  210. <dd><a name="index-gdb_002eInferiorCallPreEvent"></a>
  211. </dd>
  212. <dt><code><code>gdb.InferiorCallPreEvent</code></code></dt>
  213. <dd><p>Indicates that a function in the inferior is about to be called.
  214. </p>
  215. <dl>
  216. <dt><a name="index-InferiorCallPreEvent_002eptid"></a>Variable: <strong>InferiorCallPreEvent.ptid</strong></dt>
  217. <dd><p>The thread in which the call will be run.
  218. </p></dd></dl>
  219. <dl>
  220. <dt><a name="index-InferiorCallPreEvent_002eaddress"></a>Variable: <strong>InferiorCallPreEvent.address</strong></dt>
  221. <dd><p>The location of the function to be called.
  222. </p></dd></dl>
  223. <a name="index-gdb_002eInferiorCallPostEvent"></a>
  224. </dd>
  225. <dt><code><code>gdb.InferiorCallPostEvent</code></code></dt>
  226. <dd><p>Indicates that a function in the inferior has just been called.
  227. </p>
  228. <dl>
  229. <dt><a name="index-InferiorCallPostEvent_002eptid"></a>Variable: <strong>InferiorCallPostEvent.ptid</strong></dt>
  230. <dd><p>The thread in which the call was run.
  231. </p></dd></dl>
  232. <dl>
  233. <dt><a name="index-InferiorCallPostEvent_002eaddress"></a>Variable: <strong>InferiorCallPostEvent.address</strong></dt>
  234. <dd><p>The location of the function that was called.
  235. </p></dd></dl>
  236. </dd>
  237. </dl>
  238. </dd>
  239. <dt><code>events.memory_changed</code></dt>
  240. <dd><p>Emits <code>gdb.MemoryChangedEvent</code> which indicates that the memory of the
  241. inferior has been modified by the <small>GDB</small> user, for instance via a
  242. command like <code>set&nbsp;*addr&nbsp;=&nbsp;value</code><!-- /@w -->. The event has the following
  243. attributes:
  244. </p>
  245. <dl>
  246. <dt><a name="index-MemoryChangedEvent_002eaddress"></a>Variable: <strong>MemoryChangedEvent.address</strong></dt>
  247. <dd><p>The start address of the changed region.
  248. </p></dd></dl>
  249. <dl>
  250. <dt><a name="index-MemoryChangedEvent_002elength"></a>Variable: <strong>MemoryChangedEvent.length</strong></dt>
  251. <dd><p>Length in bytes of the changed region.
  252. </p></dd></dl>
  253. </dd>
  254. <dt><code>events.register_changed</code></dt>
  255. <dd><p>Emits <code>gdb.RegisterChangedEvent</code> which indicates that a register in the
  256. inferior has been modified by the <small>GDB</small> user.
  257. </p>
  258. <dl>
  259. <dt><a name="index-RegisterChangedEvent_002eframe"></a>Variable: <strong>RegisterChangedEvent.frame</strong></dt>
  260. <dd><p>A gdb.Frame object representing the frame in which the register was modified.
  261. </p></dd></dl>
  262. <dl>
  263. <dt><a name="index-RegisterChangedEvent_002eregnum"></a>Variable: <strong>RegisterChangedEvent.regnum</strong></dt>
  264. <dd><p>Denotes which register was modified.
  265. </p></dd></dl>
  266. </dd>
  267. <dt><code>events.breakpoint_created</code></dt>
  268. <dd><p>This is emitted when a new breakpoint has been created. The argument
  269. that is passed is the new <code>gdb.Breakpoint</code> object.
  270. </p>
  271. </dd>
  272. <dt><code>events.breakpoint_modified</code></dt>
  273. <dd><p>This is emitted when a breakpoint has been modified in some way. The
  274. argument that is passed is the new <code>gdb.Breakpoint</code> object.
  275. </p>
  276. </dd>
  277. <dt><code>events.breakpoint_deleted</code></dt>
  278. <dd><p>This is emitted when a breakpoint has been deleted. The argument that
  279. is passed is the <code>gdb.Breakpoint</code> object. When this event is
  280. emitted, the <code>gdb.Breakpoint</code> object will already be in its
  281. invalid state; that is, the <code>is_valid</code> method will return
  282. <code>False</code>.
  283. </p>
  284. </dd>
  285. <dt><code>events.before_prompt</code></dt>
  286. <dd><p>This event carries no payload. It is emitted each time <small>GDB</small>
  287. presents a prompt to the user.
  288. </p>
  289. </dd>
  290. <dt><code>events.new_inferior</code></dt>
  291. <dd><p>This is emitted when a new inferior is created. Note that the
  292. inferior is not necessarily running; in fact, it may not even have an
  293. associated executable.
  294. </p>
  295. <p>The event is of type <code>gdb.NewInferiorEvent</code>. This has a single
  296. attribute:
  297. </p>
  298. <dl>
  299. <dt><a name="index-NewInferiorEvent_002einferior"></a>Variable: <strong>NewInferiorEvent.inferior</strong></dt>
  300. <dd><p>The new inferior, a <code>gdb.Inferior</code> object.
  301. </p></dd></dl>
  302. </dd>
  303. <dt><code>events.inferior_deleted</code></dt>
  304. <dd><p>This is emitted when an inferior has been deleted. Note that this is
  305. not the same as process exit; it is notified when the inferior itself
  306. is removed, say via <code>remove-inferiors</code>.
  307. </p>
  308. <p>The event is of type <code>gdb.InferiorDeletedEvent</code>. This has a single
  309. attribute:
  310. </p>
  311. <dl>
  312. <dt><a name="index-NewInferiorEvent_002einferior-1"></a>Variable: <strong>NewInferiorEvent.inferior</strong></dt>
  313. <dd><p>The inferior that is being removed, a <code>gdb.Inferior</code> object.
  314. </p></dd></dl>
  315. </dd>
  316. <dt><code>events.new_thread</code></dt>
  317. <dd><p>This is emitted when <small>GDB</small> notices a new thread. The event is of
  318. type <code>gdb.NewThreadEvent</code>, which extends <code>gdb.ThreadEvent</code>.
  319. This has a single attribute:
  320. </p>
  321. <dl>
  322. <dt><a name="index-NewThreadEvent_002einferior_005fthread"></a>Variable: <strong>NewThreadEvent.inferior_thread</strong></dt>
  323. <dd><p>The new thread.
  324. </p></dd></dl>
  325. </dd>
  326. </dl>
  327. <hr>
  328. <div class="header">
  329. <p>
  330. Next: <a href="Threads-In-Python.html#Threads-In-Python" accesskey="n" rel="next">Threads In Python</a>, Previous: <a href="Inferiors-In-Python.html#Inferiors-In-Python" accesskey="p" rel="previous">Inferiors 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>
  331. </div>
  332. </body>
  333. </html>