Initial import
[sliver-openvswitch.git] / lib / fault.c
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include "fault.h"
23 #include <dlfcn.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <signal.h>
29 #include "util.h"
30
31 #include "vlog.h"
32 #define THIS_MODULE VLM_fault
33
34 void
35 fault_handler(int sig_nr)
36 {
37     VLOG_EMER("Caught signal %d.", sig_nr);
38     log_backtrace();
39     fflush(stdout);
40     fflush(stderr);
41
42     signal(sig_nr, SIG_DFL);
43     raise(sig_nr);
44 }
45
46 void
47 log_backtrace(void)
48 {
49     /* During the loop:
50
51        frame[0] points to the next frame.
52        frame[1] points to the return address. */
53     void **frame;
54     for (frame = __builtin_frame_address(0);
55          frame != NULL && frame[0] != NULL;
56          frame = frame[0]) {
57         Dl_info addrinfo;
58         if (!dladdr(frame[1], &addrinfo) || !addrinfo.dli_sname) {
59             fprintf(stderr, "  0x%08"PRIxPTR"\n", (uintptr_t) frame[1]);
60         } else {
61             fprintf(stderr, "  0x%08"PRIxPTR" (%s+0x%x)\n",
62                     (uintptr_t) frame[1], addrinfo.dli_sname,
63                     (char *) frame[1] - (char *) addrinfo.dli_saddr); 
64         }
65     }
66     fflush(stderr);
67 }
68
69 void
70 register_fault_handlers(void)
71 {
72     signal(SIGABRT, fault_handler);
73     signal(SIGBUS, fault_handler);
74     signal(SIGFPE, fault_handler);
75     signal(SIGILL, fault_handler);
76     signal(SIGSEGV, fault_handler);
77 }