vigra - vigra/print_backtrace.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

print_backtrace.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 2013-2014 by Martin Bidlingmaier and Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36 #ifndef VIGRA_PRINT_BACKTRACE_HXX
37 #define VIGRA_PRINT_BACKTRACE_HXX
38
39 /* Quick-and-dirty way to print a backtrace upon a signal in Linux.
40
41 Especially useful if you can't use a debugger (e.g. on TravisCI).
42
43 Usage:
44
45 Make sure to compile in debug mode.
46 Have "addr2line" installed (was already present on TravisCI and our local machines).
47
48 #include <vigra/print_backtrace.hxx>
49
50 int main(int argc, char** argv)
51 {
52 program_name = argv[0];
53 signal(SIGSEGV, &vigra_print_backtrace); // catch the desired signal
54
55 run_buggy_code();
56 }
57 */
58
59 #include <execinfo.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62
63
64 static char * program_name;
65
66 static int vigra_addr2line(void const * const addr)
67 {
68 char addr2line_cmd[512] = {0};
69 sprintf(addr2line_cmd,"addr2line -C -f -p -i -e %.256s %p", program_name, addr);
70 return system(addr2line_cmd);
71 }
72
73 static void vigra_print_backtrace(int sig)
74 {
75 int i, trace_size = 0;
76 char **messages = (char **)NULL;
77 static const int BACKTRACE_SIZE = 100;
78 void *stack_traces[BACKTRACE_SIZE];
79
80 fprintf(stderr, "caught signal %d, printing backtrace\n\n", sig);
81
82 trace_size = backtrace(stack_traces, BACKTRACE_SIZE);
83 messages = backtrace_symbols(stack_traces, trace_size);
84
85 for (i = 0; i < trace_size; ++i)
86 {
87 if (vigra_addr2line(stack_traces[i]) != 0)
88 {
89 fprintf(stderr, " error determining line # for: %sn", messages[i]);
90 }
91 }
92 if (messages) { free(messages); }
93 exit(1);
94 }
95
96 #endif // VIGRA_PRINT_BACKTRACE_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Thu Jan 8 2015)