forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixie-backtrace.c
216 lines (177 loc) · 6.09 KB
/
pixie-backtrace.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
When program crashes, print backtrace with line numbers
*/
#include "pixie-backtrace.h"
#include "unusedparm.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
char global_self[512] = "";
#if defined(__GLIBC__) && !defined(WIN32)
#include <unistd.h>
#include <execinfo.h>
#include <dlfcn.h>
#define BACKTRACE_SIZE 256
static void
handle_segfault(int sig)
{
void *func[BACKTRACE_SIZE];
char **symb = NULL;
int size;
printf("======================================================================\n");
printf(" Segmentation fault: please post this backtrace to:\n");
printf(" https://github.com/robertdavidgraham/masscan/issues\n");
printf("======================================================================\n");
size = backtrace(func, BACKTRACE_SIZE);
symb = backtrace_symbols(func, size);
while (size > 0) {
const char *symbol = symb[size - 1];
char foo[1024];
printf("%d: [%s]\n", size, symbol);
if (strstr(symbol, "[0x")) {
char *p = strstr(symbol, "[0x") + 1;
char *pp = strchr(p, ']');
snprintf(foo, sizeof(foo), "addr2line -p -i -f -e %s %.*s",
global_self,
(unsigned)(pp-p),
p);
if (system(foo) == -1)
printf("(addr2line missing)\n");
}
size --;
}
exit(1);
return;
}
/***************************************************************************
***************************************************************************/
void
pixie_backtrace_finish(void)
{
}
/***************************************************************************
***************************************************************************/
void
pixie_backtrace_init(const char *self)
{
ssize_t x;
/* Need to get a handle to the currently executing program. On Linux,
* we'll get this with /proc/self/exe, but on other platforms, we may
* need to do other things */
/* TODO: should we use readlink() to get the actual filename? */
#if defined(__linux__)
x = readlink("/proc/self/exe", global_self, sizeof(global_self));
#elif defined(__FreeBSD__)
x = readlink("/proc/curproc/file", global_self, sizeof(global_self));
#elif defined(__Solaris__)
x = readlink("/proc/self/path/a.out", global_self, sizeof(global_self));
#else
x = -1;
#endif
if (x == -1)
snprintf(global_self, sizeof(global_self), "%s", self);
signal(SIGSEGV, handle_segfault);
}
#elif defined(__MINGW32__)
void
pixie_backtrace_init(const char *self)
{
}
#elif defined(WIN32)
#include <Windows.h>
typedef struct _SYMBOL_INFO {
ULONG SizeOfStruct;
ULONG TypeIndex; // Type Index of symbol
ULONG64 Reserved[2];
ULONG Index;
ULONG Size;
ULONG64 ModBase; // Base Address of module containing this symbol
ULONG Flags;
ULONG64 Value; // Value of symbol, ValuePresent should be 1
ULONG64 Address; // Address of symbol including base address of module
ULONG Register; // register holding value or pointer to value
ULONG Scope; // scope of the symbol
ULONG Tag; // pdb classification
ULONG NameLen; // Actual length of name
ULONG MaxNameLen;
CHAR Name[1]; // Name of symbol
} SYMBOL_INFO, *PSYMBOL_INFO;
typedef BOOL (__stdcall *FUNC_SymInitialize)(
HANDLE hProcess,
PCSTR UserSearchPath,
BOOL fInvadeProcess
);
typedef BOOL (__stdcall * FUNC_SymFromAddr)(
HANDLE hProcess,
DWORD64 Address,
PDWORD64 Displacement,
PSYMBOL_INFO Symbol
);
struct _Dbg {
FUNC_SymInitialize SymInitialize;
FUNC_SymFromAddr SymFromAddr;
} Dbg;
void printStack( void );
void printStack( void )
{
unsigned int i;
void * stack[ 100 ];
unsigned short frames;
SYMBOL_INFO * symbol;
HANDLE process;
process = GetCurrentProcess();
if (Dbg.SymInitialize == NULL)
return;
if (Dbg.SymFromAddr == NULL)
return;
if (RtlCaptureStackBackTrace == NULL)
return;
Dbg.SymInitialize( process, NULL, TRUE );
frames = CaptureStackBackTrace( 0, 100, stack, NULL );
symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
for( i = 0; i < frames; i++ ) {
Dbg.SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
printf( "%u: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address );
}
free( symbol );
}
static void
handle_segfault(int sig)
{
UNUSEDPARM(sig);
printf("======================================================================");
printf(" Segmentation fault: please post this backtrace to:\n");
printf(" https://github.com/robertdavidgraham/masscan/issues\n");
printf("======================================================================");
exit(1);
}
void
pixie_backtrace_init(const char *self)
{
self;
GetModuleFileNameA(NULL, global_self, sizeof(global_self));
{
HMODULE h;
h = LoadLibraryA("DbgHelp.dll");
if (h != NULL) {
//printf("found DbgHelp.dll\n");
Dbg.SymFromAddr = (FUNC_SymFromAddr)GetProcAddress(h, "SymFromAddr");
//if (Dbg.SymFromAddr) printf("found Dbg.SymFromAddr\n");
Dbg.SymInitialize = (FUNC_SymInitialize)GetProcAddress(h, "SymInitialize");
//if (Dbg.SymInitialize) printf("found Dbg.SymInitialize\n");
h = LoadLibraryA("Kernel32.dll");
if (GetProcAddress(NULL, "RtlCaptureStackBackTrace") != NULL)
; //printf("found Dbg.SymInitialize\n");
}
}
//signal(SIGSEGV, handle_segfault);
}
#else
void
pixie_backtrace_init(const char *self)
{
}
#endif