| 1 |
<html> |
| 2 |
<head> |
| 3 |
<meta charset="UTF-8"> |
| 4 |
<link rel="stylesheet" href="highlight.min.css"> |
| 5 |
<script src="highlight.min.js"></script><script> |
| 6 |
hljs.configure({languages: ['cpp']}); |
| 7 |
hljs.highlightAll(); |
| 8 |
</script><title>Exceptions in signal handlers</title> |
| 9 |
<link rel="stylesheet" type="text/css" href="style.css"> |
| 10 |
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"> |
| 11 |
<link rel="home" href="index.html" title="Programming with gtkmm 4"> |
| 12 |
<link rel="up" href="chapter-signals.html" title="Appendix B. Signals"> |
| 13 |
<link rel="prev" href="sec-eventsignals.html" title="Event signals"> |
| 14 |
<link rel="next" href="chapter-custom-signals.html" title="Appendix C. Creating your own signals"> |
| 15 |
</head> |
| 16 |
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| 17 |
<div class="navheader"> |
| 18 |
<table width="100%" summary="Navigation header"> |
| 19 |
<tr><th colspan="3" align="center">Exceptions in signal handlers</th></tr> |
| 20 |
<tr> |
| 21 |
<td width="20%" align="left"> |
| 22 |
<a accesskey="p" href="sec-eventsignals.html"><img src="icons/prev.png" alt="Prev"></a> </td> |
| 23 |
<th width="60%" align="center">Appendix B. Signals</th> |
| 24 |
<td width="20%" align="right"> <a accesskey="n" href="chapter-custom-signals.html"><img src="icons/next.png" alt="Next"></a> |
| 25 |
</td> |
| 26 |
</tr> |
| 27 |
</table> |
| 28 |
<hr> |
| 29 |
</div> |
| 30 |
<div class="section"> |
| 31 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 32 |
<a name="sec-exceptions-in-signal-handlers"></a>Exceptions in signal handlers</h2></div></div></div> |
| 33 |
|
| 34 |
|
| 35 |
<p> |
| 36 |
When a program is aborted because of an unhandled <span class="nowrap">C++</span> exception, it's sometimes |
| 37 |
possible to use a debugger to find the location where the exception was thrown. |
| 38 |
This is more difficult than usual if the exception was thrown from a signal handler. |
| 39 |
</p> |
| 40 |
<p> |
| 41 |
This section describes primarily what you can expect on a Linux system, when you |
| 42 |
use <a class="ulink" href="http://www.gnu.org/software/gdb/" target="_top">the gdb debugger</a>. |
| 43 |
</p> |
| 44 |
<p> |
| 45 |
First, let's look at a simple example where an exception is thrown from a normal |
| 46 |
function (no signal handler). |
| 47 |
</p> |
| 48 |
<pre class="programlisting"><code class="code">// without_signal.cc |
| 49 |
#include <gtkmm.h> |
| 50 |
|
| 51 |
bool throwSomething() |
| 52 |
{ |
| 53 |
throw "Something"; |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
int main(int argc, char** argv) |
| 58 |
{ |
| 59 |
throwSomething(); |
| 60 |
auto app = Gtk::Application::create("org.gtkmm.without_signal"); |
| 61 |
return app->run(); |
| 62 |
} |
| 63 |
</code></pre> |
| 64 |
<p> |
| 65 |
Here is an excerpt from a <span class="application">gdb</span> session. Only the most |
| 66 |
interesting parts of the output are shown. |
| 67 |
</p> |
| 68 |
<pre class="programlisting"><code class="code">> gdb without_signal |
| 69 |
(gdb) run |
| 70 |
terminate called after throwing an instance of 'char const*' |
| 71 |
|
| 72 |
Program received signal SIGABRT, Aborted. |
| 73 |
(gdb) backtrace |
| 74 |
#7 0x08048864 in throwSomething () at without_signal.cc:6 |
| 75 |
#8 0x0804887d in main (argc=1, argv=0xbfffecd4) at without_signal.cc:12 |
| 76 |
</code></pre> |
| 77 |
<p>You can see that the exception was thrown from <code class="filename">without_signal.cc</code>, |
| 78 |
line 6 (<code class="code">throw "Something";</code>). |
| 79 |
</p> |
| 80 |
<p> |
| 81 |
Now let's see what happens when an exception is thrown from a signal handler. |
| 82 |
Here's the source code. |
| 83 |
</p> |
| 84 |
<pre class="programlisting"><code class="code">// with_signal.cc |
| 85 |
#include <gtkmm.h> |
| 86 |
|
| 87 |
bool throwSomething() |
| 88 |
{ |
| 89 |
throw "Something"; |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
int main(int argc, char** argv) |
| 94 |
{ |
| 95 |
Glib::signal_timeout().connect(sigc::ptr_fun(throwSomething), 500); |
| 96 |
auto app = Gtk::Application::create("org.gtkmm.with_signal"); |
| 97 |
app->hold(); |
| 98 |
return app->run(); |
| 99 |
} |
| 100 |
</code></pre> |
| 101 |
<p> |
| 102 |
And here's an excerpt from a <span class="application">gdb</span> session. |
| 103 |
</p> |
| 104 |
<pre class="programlisting"><code class="code">> gdb with_signal |
| 105 |
(gdb) run |
| 106 |
(with_signal:2703): glibmm-ERROR **: |
| 107 |
unhandled exception (type unknown) in signal handler |
| 108 |
|
| 109 |
Program received signal SIGTRAP, Trace/breakpoint trap. |
| 110 |
(gdb) backtrace |
| 111 |
#2 0x0063c6ab in glibmm_unexpected_exception () at exceptionhandler.cc:77 |
| 112 |
#3 Glib::exception_handlers_invoke () at exceptionhandler.cc:150 |
| 113 |
#4 0x0063d370 in glibmm_source_callback (data=0x804d620) at main.cc:212 |
| 114 |
#13 0x002e1b31 in Gtk::Application::run (this=0x804f300) at application.cc:178 |
| 115 |
#14 0x08048ccc in main (argc=1, argv=0xbfffecd4) at with_signal.cc:16 |
| 116 |
</code></pre> |
| 117 |
<p>The exception is caught in <span class="application">glibmm</span>, and the program |
| 118 |
ends with a call to <code class="function">g_error()</code>. Other exceptions may result |
| 119 |
in different behavior, but in any case the exception from a signal handler is |
| 120 |
caught in <span class="application">glibmm</span> or <span class="application">gtkmm</span>, and |
| 121 |
<span class="application">gdb</span> can't see where it was thrown. |
| 122 |
</p> |
| 123 |
<p> |
| 124 |
To see where the exception is thrown, you can use the <span class="application">gdb</span> |
| 125 |
command <strong class="userinput"><code>catch throw</code></strong>. |
| 126 |
</p> |
| 127 |
<pre class="programlisting"><code class="code">> gdb with_signal |
| 128 |
(gdb) catch throw |
| 129 |
Catchpoint 1 (throw) |
| 130 |
(gdb) run |
| 131 |
Catchpoint 1 (exception thrown), 0x00714ff0 in __cxa_throw () |
| 132 |
(gdb) backtrace |
| 133 |
#0 0x00714ff0 in __cxa_throw () from /usr/lib/i386-linux-gnu/libstdc++.so.6 |
| 134 |
#1 0x08048bd4 in throwSomething () at with_signal.cc:6 |
| 135 |
(gdb) continue |
| 136 |
Continuing. |
| 137 |
(with_signal:2375): glibmm-ERROR ** |
| 138 |
unhandled exception (type unknown) in signal handler |
| 139 |
|
| 140 |
Program received signal SIGTRAP, Trace/breakpoint trap. |
| 141 |
</code></pre> |
| 142 |
<p> |
| 143 |
If there are many caught exceptions before the interesting uncaught one, this |
| 144 |
method can be tedious. It can be automated with the following |
| 145 |
<span class="application">gdb</span> commands. |
| 146 |
</p> |
| 147 |
<pre class="programlisting"><code class="code">(gdb) catch throw |
| 148 |
(gdb) commands |
| 149 |
(gdb) backtrace |
| 150 |
(gdb) continue |
| 151 |
(gdb) end |
| 152 |
(gdb) set pagination off |
| 153 |
(gdb) run |
| 154 |
</code></pre> |
| 155 |
<p>These commands will print a backtrace from each <code class="code">throw</code> and continue. |
| 156 |
The backtrace from the last (or possibly the last but one) <code class="code">throw</code> |
| 157 |
before the program stops, is the interesting one. |
| 158 |
</p> |
| 159 |
</div> |
| 160 |
<div class="navfooter"> |
| 161 |
<hr> |
| 162 |
<table width="100%" summary="Navigation footer"> |
| 163 |
<tr> |
| 164 |
<td width="40%" align="left"> |
| 165 |
<a accesskey="p" href="sec-eventsignals.html"><img src="icons/prev.png" alt="Prev"></a> </td> |
| 166 |
<td width="20%" align="center"><a accesskey="u" href="chapter-signals.html"><img src="icons/up.png" alt="Up"></a></td> |
| 167 |
<td width="40%" align="right"> <a accesskey="n" href="chapter-custom-signals.html"><img src="icons/next.png" alt="Next"></a> |
| 168 |
</td> |
| 169 |
</tr> |
| 170 |
<tr> |
| 171 |
<td width="40%" align="left" valign="top">Event signals </td> |
| 172 |
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> |
| 173 |
<td width="40%" align="right" valign="top"> Appendix C. Creating your own signals</td> |
| 174 |
</tr> |
| 175 |
</table> |
| 176 |
</div> |
| 177 |
</body> |
| 178 |
</html> |