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.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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>Signals (Debugging with GDB)</title>
  17. <meta name="description" content="Signals (Debugging with GDB)">
  18. <meta name="keywords" content="Signals (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="Stopping.html#Stopping" rel="up" title="Stopping">
  26. <link href="Thread-Stops.html#Thread-Stops" rel="next" title="Thread Stops">
  27. <link href="Skipping-Over-Functions-and-Files.html#Skipping-Over-Functions-and-Files" rel="prev" title="Skipping Over Functions and Files">
  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="Signals"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Thread-Stops.html#Thread-Stops" accesskey="n" rel="next">Thread Stops</a>, Previous: <a href="Skipping-Over-Functions-and-Files.html#Skipping-Over-Functions-and-Files" accesskey="p" rel="prev">Skipping Over Functions and Files</a>, Up: <a href="Stopping.html#Stopping" accesskey="u" rel="up">Stopping</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="Signals-1"></a>
  64. <h3 class="section">5.4 Signals</h3>
  65. <a name="index-signals"></a>
  66. <p>A signal is an asynchronous event that can happen in a program. The
  67. operating system defines the possible kinds of signals, and gives each
  68. kind a name and a number. For example, in Unix <code>SIGINT</code> is the
  69. signal a program gets when you type an interrupt character (often <kbd>Ctrl-c</kbd>);
  70. <code>SIGSEGV</code> is the signal a program gets from referencing a place in
  71. memory far away from all the areas in use; <code>SIGALRM</code> occurs when
  72. the alarm clock timer goes off (which happens only if your program has
  73. requested an alarm).
  74. </p>
  75. <a name="index-fatal-signals"></a>
  76. <p>Some signals, including <code>SIGALRM</code>, are a normal part of the
  77. functioning of your program. Others, such as <code>SIGSEGV</code>, indicate
  78. errors; these signals are <em>fatal</em> (they kill your program immediately) if the
  79. program has not specified in advance some other way to handle the signal.
  80. <code>SIGINT</code> does not indicate an error in your program, but it is normally
  81. fatal so it can carry out the purpose of the interrupt: to kill the program.
  82. </p>
  83. <p><small>GDB</small> has the ability to detect any occurrence of a signal in your
  84. program. You can tell <small>GDB</small> in advance what to do for each kind of
  85. signal.
  86. </p>
  87. <a name="index-handling-signals"></a>
  88. <p>Normally, <small>GDB</small> is set up to let the non-erroneous signals like
  89. <code>SIGALRM</code> be silently passed to your program
  90. (so as not to interfere with their role in the program&rsquo;s functioning)
  91. but to stop your program immediately whenever an error signal happens.
  92. You can change these settings with the <code>handle</code> command.
  93. </p>
  94. <dl compact="compact">
  95. <dd><a name="index-info-signals"></a>
  96. <a name="index-info-handle"></a>
  97. </dd>
  98. <dt><code>info signals</code></dt>
  99. <dt><code>info handle</code></dt>
  100. <dd><p>Print a table of all the kinds of signals and how <small>GDB</small> has been told to
  101. handle each one. You can use this to see the signal numbers of all
  102. the defined types of signals.
  103. </p>
  104. </dd>
  105. <dt><code>info signals <var>sig</var></code></dt>
  106. <dd><p>Similar, but print information only about the specified signal number.
  107. </p>
  108. <p><code>info handle</code> is an alias for <code>info signals</code>.
  109. </p>
  110. </dd>
  111. <dt><code>catch signal <span class="roman">[</span><var>signal</var>&hellip; <span class="roman">|</span> &lsquo;<samp>all</samp>&rsquo;<span class="roman">]</span></code></dt>
  112. <dd><p>Set a catchpoint for the indicated signals. See <a href="Set-Catchpoints.html#Set-Catchpoints">Set Catchpoints</a>,
  113. for details about this command.
  114. </p>
  115. <a name="index-handle"></a>
  116. </dd>
  117. <dt><code>handle <var>signal</var> <span class="roman">[</span><var>keywords</var>&hellip;<span class="roman">]</span></code></dt>
  118. <dd><p>Change the way <small>GDB</small> handles signal <var>signal</var>. The <var>signal</var>
  119. can be the number of a signal or its name (with or without the
  120. &lsquo;<samp>SIG</samp>&rsquo; at the beginning); a list of signal numbers of the form
  121. &lsquo;<samp><var>low</var>-<var>high</var></samp>&rsquo;; or the word &lsquo;<samp>all</samp>&rsquo;, meaning all the
  122. known signals. Optional arguments <var>keywords</var>, described below,
  123. say what change to make.
  124. </p></dd>
  125. </dl>
  126. <p>The keywords allowed by the <code>handle</code> command can be abbreviated.
  127. Their full names are:
  128. </p>
  129. <dl compact="compact">
  130. <dt><code>nostop</code></dt>
  131. <dd><p><small>GDB</small> should not stop your program when this signal happens. It may
  132. still print a message telling you that the signal has come in.
  133. </p>
  134. </dd>
  135. <dt><code>stop</code></dt>
  136. <dd><p><small>GDB</small> should stop your program when this signal happens. This implies
  137. the <code>print</code> keyword as well.
  138. </p>
  139. </dd>
  140. <dt><code>print</code></dt>
  141. <dd><p><small>GDB</small> should print a message when this signal happens.
  142. </p>
  143. </dd>
  144. <dt><code>noprint</code></dt>
  145. <dd><p><small>GDB</small> should not mention the occurrence of the signal at all. This
  146. implies the <code>nostop</code> keyword as well.
  147. </p>
  148. </dd>
  149. <dt><code>pass</code></dt>
  150. <dt><code>noignore</code></dt>
  151. <dd><p><small>GDB</small> should allow your program to see this signal; your program
  152. can handle the signal, or else it may terminate if the signal is fatal
  153. and not handled. <code>pass</code> and <code>noignore</code> are synonyms.
  154. </p>
  155. </dd>
  156. <dt><code>nopass</code></dt>
  157. <dt><code>ignore</code></dt>
  158. <dd><p><small>GDB</small> should not allow your program to see this signal.
  159. <code>nopass</code> and <code>ignore</code> are synonyms.
  160. </p></dd>
  161. </dl>
  162. <p>When a signal stops your program, the signal is not visible to the
  163. program until you
  164. continue. Your program sees the signal then, if <code>pass</code> is in
  165. effect for the signal in question <em>at that time</em>. In other words,
  166. after <small>GDB</small> reports a signal, you can use the <code>handle</code>
  167. command with <code>pass</code> or <code>nopass</code> to control whether your
  168. program sees that signal when you continue.
  169. </p>
  170. <p>The default is set to <code>nostop</code>, <code>noprint</code>, <code>pass</code> for
  171. non-erroneous signals such as <code>SIGALRM</code>, <code>SIGWINCH</code> and
  172. <code>SIGCHLD</code>, and to <code>stop</code>, <code>print</code>, <code>pass</code> for the
  173. erroneous signals.
  174. </p>
  175. <p>You can also use the <code>signal</code> command to prevent your program from
  176. seeing a signal, or cause it to see a signal it normally would not see,
  177. or to give it any signal at any time. For example, if your program stopped
  178. due to some sort of memory reference error, you might store correct
  179. values into the erroneous variables and continue, hoping to see more
  180. execution; but your program would probably terminate immediately as
  181. a result of the fatal signal once it saw the signal. To prevent this,
  182. you can continue with &lsquo;<samp>signal 0</samp>&rsquo;. See <a href="Signaling.html#Signaling">Giving your
  183. Program a Signal</a>.
  184. </p>
  185. <a name="index-stepping-and-signal-handlers"></a>
  186. <a name="stepping-and-signal-handlers"></a>
  187. <p><small>GDB</small> optimizes for stepping the mainline code. If a signal
  188. that has <code>handle nostop</code> and <code>handle pass</code> set arrives while
  189. a stepping command (e.g., <code>stepi</code>, <code>step</code>, <code>next</code>) is
  190. in progress, <small>GDB</small> lets the signal handler run and then resumes
  191. stepping the mainline code once the signal handler returns. In other
  192. words, <small>GDB</small> steps over the signal handler. This prevents
  193. signals that you&rsquo;ve specified as not interesting (with <code>handle
  194. nostop</code>) from changing the focus of debugging unexpectedly. Note that
  195. the signal handler itself may still hit a breakpoint, stop for another
  196. signal that has <code>handle stop</code> in effect, or for any other event
  197. that normally results in stopping the stepping command sooner. Also
  198. note that <small>GDB</small> still informs you that the program received a
  199. signal if <code>handle print</code> is set.
  200. </p>
  201. <a name="stepping-into-signal-handlers"></a>
  202. <p>If you set <code>handle pass</code> for a signal, and your program sets up a
  203. handler for it, then issuing a stepping command, such as <code>step</code>
  204. or <code>stepi</code>, when your program is stopped due to the signal will
  205. step <em>into</em> the signal handler (if the target supports that).
  206. </p>
  207. <p>Likewise, if you use the <code>queue-signal</code> command to queue a signal
  208. to be delivered to the current thread when execution of the thread
  209. resumes (see <a href="Signaling.html#Signaling">Giving your Program a Signal</a>), then a
  210. stepping command will step into the signal handler.
  211. </p>
  212. <p>Here&rsquo;s an example, using <code>stepi</code> to step to the first instruction
  213. of <code>SIGUSR1</code>&rsquo;s handler:
  214. </p>
  215. <div class="smallexample">
  216. <pre class="smallexample">(gdb) handle SIGUSR1
  217. Signal Stop Print Pass to program Description
  218. SIGUSR1 Yes Yes Yes User defined signal 1
  219. (gdb) c
  220. Continuing.
  221. Program received signal SIGUSR1, User defined signal 1.
  222. main () sigusr1.c:28
  223. 28 p = 0;
  224. (gdb) si
  225. sigusr1_handler () at sigusr1.c:9
  226. 9 {
  227. </pre></div>
  228. <p>The same, but using <code>queue-signal</code> instead of waiting for the
  229. program to receive the signal first:
  230. </p>
  231. <div class="smallexample">
  232. <pre class="smallexample">(gdb) n
  233. 28 p = 0;
  234. (gdb) queue-signal SIGUSR1
  235. (gdb) si
  236. sigusr1_handler () at sigusr1.c:9
  237. 9 {
  238. (gdb)
  239. </pre></div>
  240. <a name="index-extra-signal-information"></a>
  241. <a name="extra-signal-information"></a>
  242. <p>On some targets, <small>GDB</small> can inspect extra signal information
  243. associated with the intercepted signal, before it is actually
  244. delivered to the program being debugged. This information is exported
  245. by the convenience variable <code>$_siginfo</code>, and consists of data
  246. that is passed by the kernel to the signal handler at the time of the
  247. receipt of a signal. The data type of the information itself is
  248. target dependent. You can see the data type using the <code>ptype
  249. $_siginfo</code> command. On Unix systems, it typically corresponds to the
  250. standard <code>siginfo_t</code> type, as defined in the <samp>signal.h</samp>
  251. system header.
  252. </p>
  253. <p>Here&rsquo;s an example, on a <small>GNU</small>/Linux system, printing the stray
  254. referenced address that raised a segmentation fault.
  255. </p>
  256. <div class="smallexample">
  257. <pre class="smallexample">(gdb) continue
  258. Program received signal SIGSEGV, Segmentation fault.
  259. 0x0000000000400766 in main ()
  260. 69 *(int *)p = 0;
  261. (gdb) ptype $_siginfo
  262. type = struct {
  263. int si_signo;
  264. int si_errno;
  265. int si_code;
  266. union {
  267. int _pad[28];
  268. struct {...} _kill;
  269. struct {...} _timer;
  270. struct {...} _rt;
  271. struct {...} _sigchld;
  272. struct {...} _sigfault;
  273. struct {...} _sigpoll;
  274. } _sifields;
  275. }
  276. (gdb) ptype $_siginfo._sifields._sigfault
  277. type = struct {
  278. void *si_addr;
  279. }
  280. (gdb) p $_siginfo._sifields._sigfault.si_addr
  281. $1 = (void *) 0x7ffff7ff7000
  282. </pre></div>
  283. <p>Depending on target support, <code>$_siginfo</code> may also be writable.
  284. </p>
  285. <a name="index-Intel-MPX-boundary-violations"></a>
  286. <a name="index-boundary-violations_002c-Intel-MPX"></a>
  287. <p>On some targets, a <code>SIGSEGV</code> can be caused by a boundary
  288. violation, i.e., accessing an address outside of the allowed range.
  289. In those cases <small>GDB</small> may displays additional information,
  290. depending on how <small>GDB</small> has been told to handle the signal.
  291. With <code>handle stop SIGSEGV</code>, <small>GDB</small> displays the violation
  292. kind: &quot;Upper&quot; or &quot;Lower&quot;, the memory address accessed and the
  293. bounds, while with <code>handle nostop SIGSEGV</code> no additional
  294. information is displayed.
  295. </p>
  296. <p>The usual output of a segfault is:
  297. </p><div class="smallexample">
  298. <pre class="smallexample">Program received signal SIGSEGV, Segmentation fault
  299. 0x0000000000400d7c in upper () at i386-mpx-sigsegv.c:68
  300. 68 value = *(p + len);
  301. </pre></div>
  302. <p>While a bound violation is presented as:
  303. </p><div class="smallexample">
  304. <pre class="smallexample">Program received signal SIGSEGV, Segmentation fault
  305. Upper bound violation while accessing address 0x7fffffffc3b3
  306. Bounds: [lower = 0x7fffffffc390, upper = 0x7fffffffc3a3]
  307. 0x0000000000400d7c in upper () at i386-mpx-sigsegv.c:68
  308. 68 value = *(p + len);
  309. </pre></div>
  310. <hr>
  311. <div class="header">
  312. <p>
  313. Next: <a href="Thread-Stops.html#Thread-Stops" accesskey="n" rel="next">Thread Stops</a>, Previous: <a href="Skipping-Over-Functions-and-Files.html#Skipping-Over-Functions-and-Files" accesskey="p" rel="prev">Skipping Over Functions and Files</a>, Up: <a href="Stopping.html#Stopping" accesskey="u" rel="up">Stopping</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>
  314. </div>
  315. </body>
  316. </html>