Set-Catchpoints.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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: Set Catchpoints</title>
  16. <meta name="description" content="Debugging with GDB: Set Catchpoints">
  17. <meta name="keywords" content="Debugging with GDB: Set Catchpoints">
  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="Breakpoints.html#Breakpoints" rel="up" title="Breakpoints">
  26. <link href="Delete-Breaks.html#Delete-Breaks" rel="next" title="Delete Breaks">
  27. <link href="Set-Watchpoints.html#Set-Watchpoints" rel="previous" title="Set Watchpoints">
  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="Set-Catchpoints"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Delete-Breaks.html#Delete-Breaks" accesskey="n" rel="next">Delete Breaks</a>, Previous: <a href="Set-Watchpoints.html#Set-Watchpoints" accesskey="p" rel="previous">Set Watchpoints</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</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="Setting-Catchpoints"></a>
  65. <h4 class="subsection">5.1.3 Setting Catchpoints</h4>
  66. <a name="index-catchpoints_002c-setting"></a>
  67. <a name="index-exception-handlers"></a>
  68. <a name="index-event-handling"></a>
  69. <p>You can use <em>catchpoints</em> to cause the debugger to stop for certain
  70. kinds of program events, such as C<tt>++</tt> exceptions or the loading of a
  71. shared library. Use the <code>catch</code> command to set a catchpoint.
  72. </p>
  73. <dl compact="compact">
  74. <dd><a name="index-catch"></a>
  75. </dd>
  76. <dt><code>catch <var>event</var></code></dt>
  77. <dd><p>Stop when <var>event</var> occurs. The <var>event</var> can be any of the following:
  78. </p>
  79. <dl compact="compact">
  80. <dt><code>throw <span class="roman">[</span><var>regexp</var><span class="roman">]</span></code></dt>
  81. <dt><code>rethrow <span class="roman">[</span><var>regexp</var><span class="roman">]</span></code></dt>
  82. <dt><code>catch <span class="roman">[</span><var>regexp</var><span class="roman">]</span></code></dt>
  83. <dd><a name="index-catch-throw"></a>
  84. <a name="index-catch-rethrow"></a>
  85. <a name="index-catch-catch"></a>
  86. <a name="index-stop-on-C_002b_002b-exceptions"></a>
  87. <p>The throwing, re-throwing, or catching of a C<tt>++</tt> exception.
  88. </p>
  89. <p>If <var>regexp</var> is given, then only exceptions whose type matches the
  90. regular expression will be caught.
  91. </p>
  92. <a name="index-_0024_005fexception_002c-convenience-variable"></a>
  93. <p>The convenience variable <code>$_exception</code> is available at an
  94. exception-related catchpoint, on some systems. This holds the
  95. exception being thrown.
  96. </p>
  97. <p>There are currently some limitations to C<tt>++</tt> exception handling in
  98. <small>GDB</small>:
  99. </p>
  100. <ul>
  101. <li> The support for these commands is system-dependent. Currently, only
  102. systems using the &lsquo;<samp>gnu-v3</samp>&rsquo; C<tt>++</tt> ABI (see <a href="ABI.html#ABI">ABI</a>) are
  103. supported.
  104. </li><li> The regular expression feature and the <code>$_exception</code> convenience
  105. variable rely on the presence of some SDT probes in <code>libstdc++</code>.
  106. If these probes are not present, then these features cannot be used.
  107. These probes were first available in the GCC 4.8 release, but whether
  108. or not they are available in your GCC also depends on how it was
  109. built.
  110. </li><li> The <code>$_exception</code> convenience variable is only valid at the
  111. instruction at which an exception-related catchpoint is set.
  112. </li><li> When an exception-related catchpoint is hit, <small>GDB</small> stops at a
  113. location in the system library which implements runtime exception
  114. support for C<tt>++</tt>, usually <code>libstdc++</code>. You can use <code>up</code>
  115. (see <a href="Selection.html#Selection">Selection</a>) to get to your code.
  116. </li><li> If you call a function interactively, <small>GDB</small> normally returns
  117. control to you when the function has finished executing. If the call
  118. raises an exception, however, the call may bypass the mechanism that
  119. returns control to you and cause your program either to abort or to
  120. simply continue running until it hits a breakpoint, catches a signal
  121. that <small>GDB</small> is listening for, or exits. This is the case even if
  122. you set a catchpoint for the exception; catchpoints on exceptions are
  123. disabled within interactive calls. See <a href="Calling.html#Calling">Calling</a>, for information on
  124. controlling this with <code>set unwind-on-terminating-exception</code>.
  125. </li><li> You cannot raise an exception interactively.
  126. </li><li> You cannot install an exception handler interactively.
  127. </li></ul>
  128. </dd>
  129. <dt><code>exception <span class="roman">[</span><var>name</var><span class="roman">]</span></code></dt>
  130. <dd><a name="index-catch-exception"></a>
  131. <a name="index-Ada-exception-catching"></a>
  132. <a name="index-catch-Ada-exceptions"></a>
  133. <p>An Ada exception being raised. If an exception name is specified
  134. at the end of the command (eg <code>catch exception Program_Error</code>),
  135. the debugger will stop only when this specific exception is raised.
  136. Otherwise, the debugger stops execution when any Ada exception is raised.
  137. </p>
  138. <p>When inserting an exception catchpoint on a user-defined exception whose
  139. name is identical to one of the exceptions defined by the language, the
  140. fully qualified name must be used as the exception name. Otherwise,
  141. <small>GDB</small> will assume that it should stop on the pre-defined exception
  142. rather than the user-defined one. For instance, assuming an exception
  143. called <code>Constraint_Error</code> is defined in package <code>Pck</code>, then
  144. the command to use to catch such exceptions is <kbd>catch exception
  145. Pck.Constraint_Error</kbd>.
  146. </p>
  147. <a name="index-_0024_005fada_005fexception_002c-convenience-variable"></a>
  148. <p>The convenience variable <code>$_ada_exception</code> holds the address of
  149. the exception being thrown. This can be useful when setting a
  150. condition for such a catchpoint.
  151. </p>
  152. </dd>
  153. <dt><code>exception unhandled</code></dt>
  154. <dd><a name="index-catch-exception-unhandled"></a>
  155. <p>An exception that was raised but is not handled by the program. The
  156. convenience variable <code>$_ada_exception</code> is set as for <code>catch
  157. exception</code>.
  158. </p>
  159. </dd>
  160. <dt><code>handlers <span class="roman">[</span><var>name</var><span class="roman">]</span></code></dt>
  161. <dd><a name="index-catch-handlers"></a>
  162. <a name="index-Ada-exception-handlers-catching"></a>
  163. <a name="index-catch-Ada-exceptions-when-handled"></a>
  164. <p>An Ada exception being handled. If an exception name is
  165. specified at the end of the command
  166. (eg <kbd>catch handlers Program_Error</kbd>), the debugger will stop
  167. only when this specific exception is handled.
  168. Otherwise, the debugger stops execution when any Ada exception is handled.
  169. </p>
  170. <p>When inserting a handlers catchpoint on a user-defined
  171. exception whose name is identical to one of the exceptions
  172. defined by the language, the fully qualified name must be used
  173. as the exception name. Otherwise, <small>GDB</small> will assume that it
  174. should stop on the pre-defined exception rather than the
  175. user-defined one. For instance, assuming an exception called
  176. <code>Constraint_Error</code> is defined in package <code>Pck</code>, then the
  177. command to use to catch such exceptions handling is
  178. <kbd>catch handlers Pck.Constraint_Error</kbd>.
  179. </p>
  180. <p>The convenience variable <code>$_ada_exception</code> is set as for
  181. <code>catch exception</code>.
  182. </p>
  183. </dd>
  184. <dt><code>assert</code></dt>
  185. <dd><a name="index-catch-assert"></a>
  186. <p>A failed Ada assertion. Note that the convenience variable
  187. <code>$_ada_exception</code> is <em>not</em> set by this catchpoint.
  188. </p>
  189. </dd>
  190. <dt><code>exec</code></dt>
  191. <dd><a name="index-catch-exec"></a>
  192. <a name="index-break-on-fork_002fexec"></a>
  193. <p>A call to <code>exec</code>.
  194. </p>
  195. <a name="catch-syscall"></a></dd>
  196. <dt><code>syscall</code></dt>
  197. <dt><code>syscall <span class="roman">[</span><var>name</var> <span class="roman">|</span> <var>number</var> <span class="roman">|</span> <span class="roman">group:</span><var>groupname</var> <span class="roman">|</span> <span class="roman">g:</span><var>groupname</var><span class="roman">]</span> &hellip;</code></dt>
  198. <dd><a name="index-catch-syscall"></a>
  199. <a name="index-break-on-a-system-call_002e"></a>
  200. <p>A call to or return from a system call, a.k.a. <em>syscall</em>. A
  201. syscall is a mechanism for application programs to request a service
  202. from the operating system (OS) or one of the OS system services.
  203. <small>GDB</small> can catch some or all of the syscalls issued by the
  204. debuggee, and show the related information for each syscall. If no
  205. argument is specified, calls to and returns from all system calls
  206. will be caught.
  207. </p>
  208. <p><var>name</var> can be any system call name that is valid for the
  209. underlying OS. Just what syscalls are valid depends on the OS. On
  210. GNU and Unix systems, you can find the full list of valid syscall
  211. names on <samp>/usr/include/asm/unistd.h</samp>.
  212. </p>
  213. <p>Normally, <small>GDB</small> knows in advance which syscalls are valid for
  214. each OS, so you can use the <small>GDB</small> command-line completion
  215. facilities (see <a href="Completion.html#Completion">command completion</a>) to list the
  216. available choices.
  217. </p>
  218. <p>You may also specify the system call numerically. A syscall&rsquo;s
  219. number is the value passed to the OS&rsquo;s syscall dispatcher to
  220. identify the requested service. When you specify the syscall by its
  221. name, <small>GDB</small> uses its database of syscalls to convert the name
  222. into the corresponding numeric code, but using the number directly
  223. may be useful if <small>GDB</small>&rsquo;s database does not have the complete
  224. list of syscalls on your system (e.g., because <small>GDB</small> lags
  225. behind the OS upgrades).
  226. </p>
  227. <p>You may specify a group of related syscalls to be caught at once using
  228. the <code>group:</code> syntax (<code>g:</code> is a shorter equivalent). For
  229. instance, on some platforms <small>GDB</small> allows you to catch all
  230. network related syscalls, by passing the argument <code>group:network</code>
  231. to <code>catch syscall</code>. Note that not all syscall groups are
  232. available in every system. You can use the command completion
  233. facilities (see <a href="Completion.html#Completion">command completion</a>) to list the
  234. syscall groups available on your environment.
  235. </p>
  236. <p>The example below illustrates how this command works if you don&rsquo;t provide
  237. arguments to it:
  238. </p>
  239. <div class="smallexample">
  240. <pre class="smallexample">(gdb) catch syscall
  241. Catchpoint 1 (syscall)
  242. (gdb) r
  243. Starting program: /tmp/catch-syscall
  244. Catchpoint 1 (call to syscall 'close'), \
  245. 0xffffe424 in __kernel_vsyscall ()
  246. (gdb) c
  247. Continuing.
  248. Catchpoint 1 (returned from syscall 'close'), \
  249. 0xffffe424 in __kernel_vsyscall ()
  250. (gdb)
  251. </pre></div>
  252. <p>Here is an example of catching a system call by name:
  253. </p>
  254. <div class="smallexample">
  255. <pre class="smallexample">(gdb) catch syscall chroot
  256. Catchpoint 1 (syscall 'chroot' [61])
  257. (gdb) r
  258. Starting program: /tmp/catch-syscall
  259. Catchpoint 1 (call to syscall 'chroot'), \
  260. 0xffffe424 in __kernel_vsyscall ()
  261. (gdb) c
  262. Continuing.
  263. Catchpoint 1 (returned from syscall 'chroot'), \
  264. 0xffffe424 in __kernel_vsyscall ()
  265. (gdb)
  266. </pre></div>
  267. <p>An example of specifying a system call numerically. In the case
  268. below, the syscall number has a corresponding entry in the XML
  269. file, so <small>GDB</small> finds its name and prints it:
  270. </p>
  271. <div class="smallexample">
  272. <pre class="smallexample">(gdb) catch syscall 252
  273. Catchpoint 1 (syscall(s) 'exit_group')
  274. (gdb) r
  275. Starting program: /tmp/catch-syscall
  276. Catchpoint 1 (call to syscall 'exit_group'), \
  277. 0xffffe424 in __kernel_vsyscall ()
  278. (gdb) c
  279. Continuing.
  280. Program exited normally.
  281. (gdb)
  282. </pre></div>
  283. <p>Here is an example of catching a syscall group:
  284. </p>
  285. <div class="smallexample">
  286. <pre class="smallexample">(gdb) catch syscall group:process
  287. Catchpoint 1 (syscalls 'exit' [1] 'fork' [2] 'waitpid' [7]
  288. 'execve' [11] 'wait4' [114] 'clone' [120] 'vfork' [190]
  289. 'exit_group' [252] 'waitid' [284] 'unshare' [310])
  290. (gdb) r
  291. Starting program: /tmp/catch-syscall
  292. Catchpoint 1 (call to syscall fork), 0x00007ffff7df4e27 in open64 ()
  293. from /lib64/ld-linux-x86-64.so.2
  294. (gdb) c
  295. Continuing.
  296. </pre></div>
  297. <p>However, there can be situations when there is no corresponding name
  298. in XML file for that syscall number. In this case, <small>GDB</small> prints
  299. a warning message saying that it was not able to find the syscall name,
  300. but the catchpoint will be set anyway. See the example below:
  301. </p>
  302. <div class="smallexample">
  303. <pre class="smallexample">(gdb) catch syscall 764
  304. warning: The number '764' does not represent a known syscall.
  305. Catchpoint 2 (syscall 764)
  306. (gdb)
  307. </pre></div>
  308. <p>If you configure <small>GDB</small> using the &lsquo;<samp>--without-expat</samp>&rsquo; option,
  309. it will not be able to display syscall names. Also, if your
  310. architecture does not have an XML file describing its system calls,
  311. you will not be able to see the syscall names. It is important to
  312. notice that these two features are used for accessing the syscall
  313. name database. In either case, you will see a warning like this:
  314. </p>
  315. <div class="smallexample">
  316. <pre class="smallexample">(gdb) catch syscall
  317. warning: Could not open &quot;syscalls/i386-linux.xml&quot;
  318. warning: Could not load the syscall XML file 'syscalls/i386-linux.xml'.
  319. GDB will not be able to display syscall names.
  320. Catchpoint 1 (syscall)
  321. (gdb)
  322. </pre></div>
  323. <p>Of course, the file name will change depending on your architecture and system.
  324. </p>
  325. <p>Still using the example above, you can also try to catch a syscall by its
  326. number. In this case, you would see something like:
  327. </p>
  328. <div class="smallexample">
  329. <pre class="smallexample">(gdb) catch syscall 252
  330. Catchpoint 1 (syscall(s) 252)
  331. </pre></div>
  332. <p>Again, in this case <small>GDB</small> would not be able to display syscall&rsquo;s names.
  333. </p>
  334. </dd>
  335. <dt><code>fork</code></dt>
  336. <dd><a name="index-catch-fork"></a>
  337. <p>A call to <code>fork</code>.
  338. </p>
  339. </dd>
  340. <dt><code>vfork</code></dt>
  341. <dd><a name="index-catch-vfork"></a>
  342. <p>A call to <code>vfork</code>.
  343. </p>
  344. </dd>
  345. <dt><code>load <span class="roman">[</span><var>regexp</var><span class="roman">]</span></code></dt>
  346. <dt><code>unload <span class="roman">[</span><var>regexp</var><span class="roman">]</span></code></dt>
  347. <dd><a name="index-catch-load"></a>
  348. <a name="index-catch-unload"></a>
  349. <p>The loading or unloading of a shared library. If <var>regexp</var> is
  350. given, then the catchpoint will stop only if the regular expression
  351. matches one of the affected libraries.
  352. </p>
  353. </dd>
  354. <dt><code>signal <span class="roman">[</span><var>signal</var>&hellip; <span class="roman">|</span> &lsquo;<samp>all</samp>&rsquo;<span class="roman">]</span></code></dt>
  355. <dd><a name="index-catch-signal"></a>
  356. <p>The delivery of a signal.
  357. </p>
  358. <p>With no arguments, this catchpoint will catch any signal that is not
  359. used internally by <small>GDB</small>, specifically, all signals except
  360. &lsquo;<samp>SIGTRAP</samp>&rsquo; and &lsquo;<samp>SIGINT</samp>&rsquo;.
  361. </p>
  362. <p>With the argument &lsquo;<samp>all</samp>&rsquo;, all signals, including those used by
  363. <small>GDB</small>, will be caught. This argument cannot be used with other
  364. signal names.
  365. </p>
  366. <p>Otherwise, the arguments are a list of signal names as given to
  367. <code>handle</code> (see <a href="Signals.html#Signals">Signals</a>). Only signals specified in this list
  368. will be caught.
  369. </p>
  370. <p>One reason that <code>catch signal</code> can be more useful than
  371. <code>handle</code> is that you can attach commands and conditions to the
  372. catchpoint.
  373. </p>
  374. <p>When a signal is caught by a catchpoint, the signal&rsquo;s <code>stop</code> and
  375. <code>print</code> settings, as specified by <code>handle</code>, are ignored.
  376. However, whether the signal is still delivered to the inferior depends
  377. on the <code>pass</code> setting; this can be changed in the catchpoint&rsquo;s
  378. commands.
  379. </p>
  380. </dd>
  381. </dl>
  382. </dd>
  383. <dt><code>tcatch <var>event</var></code></dt>
  384. <dd><a name="index-tcatch"></a>
  385. <p>Set a catchpoint that is enabled only for one stop. The catchpoint is
  386. automatically deleted after the first time the event is caught.
  387. </p>
  388. </dd>
  389. </dl>
  390. <p>Use the <code>info break</code> command to list the current catchpoints.
  391. </p>
  392. <hr>
  393. <div class="header">
  394. <p>
  395. Next: <a href="Delete-Breaks.html#Delete-Breaks" accesskey="n" rel="next">Delete Breaks</a>, Previous: <a href="Set-Watchpoints.html#Set-Watchpoints" accesskey="p" rel="previous">Set Watchpoints</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</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>
  396. </div>
  397. </body>
  398. </html>