gby / hacks

Various hacks with no other place

This URL has Read+Write access

hacks / backtrace.c
100644 69 lines (48 sloc) 0.924 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
 
/**********************************
* Compile with:
* gcc bt.c -rdynamic -o bt
*
* Note that you must use the -rdynamic
* to see function names
*/
 
 
/* Obtain a backtrace and print it to stdout. */
void
print_trace (void)
{
  void *array[10];
  size_t size;
  char **strings;
  size_t i;
 
  size = backtrace (array, 10);
  strings = backtrace_symbols (array, size);
 
  printf ("Obtained %zd stack frames.\n", size);
 
  for (i = 0; i < size; i++)
     printf ("%s\n", strings[i]);
 
  free (strings);
}
 
/* The fault handler function */
void
fault_handler (int dummy)
{
  print_trace ();
  exit(-1);
}
 
 
void * dummy_func1(int dummy) {
 
 int * p = NULL;
 
 *p = 1;
  return NULL;
}
 
 
void * dummy_func2(int dummy) {
 
 return dummy_func1(dummy);
}
 
int
main (void)
{
 
  signal(SIGSEGV, fault_handler);
 
  dummy_func2(12);
 
 
  return 0;
}