Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1feb4ce7a19ce1061fd9ec19a558fb9e781d1900
[simgrid.git] / src / xbt / backtrace_windows.c
1 /* $Id: ex.c 5173 2008-01-07 22:10:52Z mquinson $ */
2
3 /* backtrace_windows - backtrace displaying on windows platform             */
4 /* This file is included by ex.c on need (windows x86)                      */
5
6 /*  Copyright (c) 2007 The SimGrid team                                     */
7 /*  All rights reserved.                                                    */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 /* 
13  * Win32 (x86) implementation backtrace, backtrace_symbols
14  * : support for application self-debugging.
15  */
16
17 #include <dbghelp.h>
18
19 /* Pointer function to SymInitialize() */
20 typedef BOOL (WINAPI *xbt_pfn_sym_initialize_t)(HANDLE, PSTR , BOOL);
21
22 /* Pointer function to SymCleanup() */
23 typedef BOOL (WINAPI *xbt_pfn_sym_cleanup_t)(HANDLE hProcess);
24
25 /* Pointer function to SymFunctionTableAccess() */
26 typedef PVOID (WINAPI *xbt_pfn_sym_function_table_access_t)(HANDLE, DWORD);
27
28 /* Pointer function to SymGetLineFromAddr() */
29 typedef BOOL (WINAPI *xbt_pfn_sym_get_line_from_addr_t)(HANDLE, DWORD, PDWORD , PIMAGEHLP_LINE);
30
31 /* Pointer function to SymGetModuleBase() */
32 typedef DWORD (WINAPI *xbt_pfn_sym_get_module_base_t)(HANDLE,DWORD);
33
34 /* Pointer function to SymGetOptions() */
35 typedef DWORD (WINAPI *xbt_pfn_sym_get_options_t)(VOID);
36
37 /* Pointer function to SymGetSymFromAddr() */
38 typedef BOOL (WINAPI *xbt_pfn_sym_get_sym_from_addr_t)(HANDLE, DWORD, PDWORD , OUT PIMAGEHLP_SYMBOL);
39
40 /* Pointer function to SymSetOptions() */
41 typedef DWORD (WINAPI *xbt_pfn_sym_set_options_t)(DWORD);
42
43 /* Pointer function to StackWalk() */
44 typedef BOOL (WINAPI *xbt_pfn_stack_walk_t)(DWORD, HANDLE, HANDLE ,LPSTACKFRAME, PVOID,PREAD_PROCESS_MEMORY_ROUTINE, PFUNCTION_TABLE_ACCESS_ROUTINE, PGET_MODULE_BASE_ROUTINE, PTRANSLATE_ADDRESS_ROUTINE);
45
46 /* This structure represents the debug_help library used interface */
47 typedef struct s_xbt_debug_help
48 {
49         HINSTANCE instance;
50         HANDLE process_handle;                                                                                  
51         xbt_pfn_sym_initialize_t sym_initialize;
52         xbt_pfn_sym_cleanup_t sym_cleanup;
53         xbt_pfn_sym_function_table_access_t sym_function_table_access;
54         xbt_pfn_sym_get_line_from_addr_t sym_get_line_from_addr;
55         xbt_pfn_sym_get_module_base_t sym_get_module_base;
56         xbt_pfn_sym_get_options_t sym_get_options;
57         xbt_pfn_sym_get_sym_from_addr_t sym_get_sym_from_addr;
58         xbt_pfn_sym_set_options_t sym_set_options;
59         xbt_pfn_stack_walk_t stack_walk;
60 }s_xbt_debug_hlp_t,* xbt_debug_hlp_t;
61
62
63 /* the address to the unique reference to the debug help library interface */
64 static xbt_debug_hlp_t 
65 dbg_hlp = NULL;
66
67 /* initialize the debug help library */
68 static int
69 dbg_hlp_init(HANDLE process_handle);
70
71 /* finalize the debug help library */
72 static int
73 dbg_hlp_finalize(void);
74
75 /*
76  * backtrace() function.
77  *
78  * Returns a backtrace for the calling program, in  the  array
79  * pointed  to  by  buffer.  A backtrace is the series of currently active
80  * function calls for the program.  Each item in the array pointed  to  by
81  * buffer  is  of  type  void *, and is the return address from the corre-
82  * sponding stack frame.  The size argument specifies the  maximum  number
83  * of  addresses that can be stored in buffer.  If the backtrace is larger
84  * than size, then the addresses corresponding to  the  size  most  recent
85  * function  calls  are  returned;  to obtain the complete backtrace, make
86  * sure that buffer and size are large enough.
87  */
88
89 int 
90 backtrace (void **buffer, int size);
91
92 /*
93  * backtrace_symbols() function.
94  *
95  * Given the set of addresses returned by  backtrace()  in  buffer,  back-
96  * trace_symbols()  translates the addresses into an array of strings containing
97  * the name, the source file and the line number or the las called functions.
98  */
99 char ** 
100 backtrace_symbols (void *const *buffer, int size);
101
102 void xbt_ex_setup_backtrace(xbt_ex_t *e)  {
103 int i;
104 char **backtrace = backtrace_symbols (e->bt, e->used);
105   
106   /* parse the output and build a new backtrace */
107   e->bt_strings = xbt_new(char*,e->used);
108   
109
110   for(i=0; i<e->used; i++) 
111   {
112       e->bt_strings[i] = xbt_strdup(backtrace[i]);
113       free(backtrace[i]);
114   }
115   
116   free(backtrace);
117 }    
118
119 int 
120 backtrace (void **buffer, int size)
121 {
122         int pos = 0;
123         STACKFRAME* stack_frame;
124         int first = 1;
125
126         IMAGEHLP_SYMBOL * pSym;
127         unsigned long offset = 0;
128         IMAGEHLP_LINE line_info = {0};
129         byte __buffer[(sizeof(SYMBOL_INFO) +MAX_SYM_NAME * sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64)];
130
131         CONTEXT context = {CONTEXT_FULL};
132         GetThreadContext(GetCurrentThread(), &context);
133         
134         /* ebp points on stack base */
135         /* esp points on stack pointer, ie on last stacked element (current element) */
136         _asm call $+5
137         _asm pop eax 
138         _asm mov context.Eip, eax 
139         _asm mov eax, esp 
140         _asm mov context.Esp, eax 
141         _asm mov context.Ebp, ebp 
142
143         dbg_hlp_init(GetCurrentProcess());
144         
145         if((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer))
146         {
147                 errno = EINVAL;
148                 return 0;
149         }
150
151         for(pos = 0; pos < size; pos++)
152                 buffer[pos] = NULL;
153         
154         pos = 0;
155
156         pSym = (IMAGEHLP_SYMBOL*)__buffer;
157
158         pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
159         pSym->MaxNameLength = MAX_SYM_NAME;
160
161
162         line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
163         
164
165     while(pos < size)
166     {
167                 stack_frame = (void*)xbt_new0(STACKFRAME,1);
168                 
169                 if(!stack_frame)
170                 {
171                         errno = ENOMEM;
172                         break;
173                 }
174                 
175                 stack_frame->AddrPC.Offset = context.Eip;
176             stack_frame->AddrPC.Mode = AddrModeFlat;
177         
178             stack_frame->AddrFrame.Offset = context.Ebp;
179             stack_frame->AddrFrame.Mode = AddrModeFlat;
180         
181             stack_frame->AddrStack.Offset = context.Esp;
182             stack_frame->AddrStack.Mode = AddrModeFlat;
183                 
184                 if((*(dbg_hlp->stack_walk))(
185                                         IMAGE_FILE_MACHINE_I386,
186                                         dbg_hlp->process_handle,
187                                         GetCurrentThread(),
188                                         stack_frame, 
189                                         &context,
190                                         NULL,
191                                         dbg_hlp->sym_function_table_access,
192                                         dbg_hlp->sym_get_module_base,
193                                         NULL)
194                         && !first) 
195                 {
196                         if(stack_frame->AddrReturn.Offset)
197                         {
198
199                                 if((*(dbg_hlp->sym_get_sym_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,pSym))
200                                 {       
201                                         if((*(dbg_hlp->sym_get_line_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,&line_info))
202                                                 buffer[pos++] = (void*)stack_frame;
203                                 }
204                         }
205                         else
206                         {
207                                 free(stack_frame); /* no symbol or no line info */
208                                 break;
209                         }
210                 }
211                 else
212                 {
213                         free(stack_frame);
214
215                         if(first)
216                                 first = 0;
217                         else
218                                 break;
219                 }               
220     }
221
222     return pos;
223 }
224
225 char ** 
226 backtrace_symbols (void *const *buffer, int size)
227 {
228         int pos;
229         int success = 0;        
230         char** strings;
231         STACKFRAME* stack_frame;
232         IMAGEHLP_SYMBOL * pSym;
233         unsigned long offset = 0;
234         IMAGEHLP_LINE line_info = {0};
235         IMAGEHLP_MODULE module = {0};
236         byte __buffer[(sizeof(SYMBOL_INFO) +MAX_SYM_NAME * sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64)];
237
238         if((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer))
239         {
240                 errno = EINVAL;
241                 return NULL;
242         }
243         
244         strings = xbt_new0(char*,size);
245         
246         if(NULL == strings)
247         {
248                 errno = ENOMEM;
249                 return NULL;
250         }
251         
252         pSym = (IMAGEHLP_SYMBOL*)__buffer;
253
254         pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
255         pSym->MaxNameLength = MAX_SYM_NAME;
256
257
258         line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
259         module.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
260         
261         for(pos = 0; pos < size; pos++)
262         {
263                 stack_frame = (STACKFRAME*)(buffer[pos]);
264
265                 if(NULL != stack_frame)
266                 {
267                 
268                         if((*(dbg_hlp->sym_get_sym_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,pSym))
269                         {       
270                                 if((*(dbg_hlp->sym_get_line_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,&line_info))
271                                 {
272                                         strings[pos] = bprintf("**   In %s() at %s:%d", pSym->Name,line_info.FileName,line_info.LineNumber);
273                                 }
274                                 else
275                                 {
276                                         strings[pos] = bprintf("**   In %s()", pSym->Name);
277                                 }
278                                 success = 1;
279                         }
280                         else
281                         {
282                                 strings[pos] = xbt_strdup("**   <no symbol>");
283                         }
284
285                         free(stack_frame);
286                 }
287                 else
288                         break;
289         }
290         
291         if(!success)
292         {
293                 free(strings);
294                 strings = NULL;
295         }
296
297         dbg_hlp_finalize();
298         
299         return strings;
300 }
301
302 static int
303 dbg_hlp_init(HANDLE process_handle)
304 {
305         if(dbg_hlp)
306         {
307                 /* debug help is already loaded */
308                 return 0;
309         }
310
311         /* allocation */
312         dbg_hlp = xbt_new0(s_xbt_debug_hlp_t,1);
313         
314         if(!dbg_hlp)
315                 return ENOMEM;
316         
317         /* load the library */
318         dbg_hlp->instance = LoadLibraryA("Dbghelp.dll");
319         
320         if(!(dbg_hlp->instance))
321         {
322                 free(dbg_hlp);
323                 dbg_hlp = NULL;
324                 return (int)GetLastError();
325         }
326         
327         /* get the pointers to debug help library exported functions */
328         
329         if(!((dbg_hlp->sym_initialize) = (xbt_pfn_sym_initialize_t)GetProcAddress(dbg_hlp->instance,"SymInitialize")))
330         {
331                 FreeLibrary(dbg_hlp->instance);
332                 free(dbg_hlp);
333                 dbg_hlp = NULL;
334                 return (int)GetLastError();     
335         }
336                 
337         if(!((dbg_hlp->sym_cleanup) = (xbt_pfn_sym_cleanup_t)GetProcAddress(dbg_hlp->instance,"SymCleanup")))
338         {
339                 FreeLibrary(dbg_hlp->instance);
340                 free(dbg_hlp);
341                 dbg_hlp = NULL;
342                 return (int)GetLastError();     
343         }
344         
345         if(!((dbg_hlp->sym_function_table_access) = (xbt_pfn_sym_function_table_access_t)GetProcAddress(dbg_hlp->instance,"SymFunctionTableAccess")))
346         {
347                 FreeLibrary(dbg_hlp->instance);
348                 free(dbg_hlp);
349                 dbg_hlp = NULL;
350                 return (int)GetLastError();     
351         }
352         
353         if(!((dbg_hlp->sym_get_line_from_addr) = (xbt_pfn_sym_get_line_from_addr_t)GetProcAddress(dbg_hlp->instance,"SymGetLineFromAddr")))
354         {
355                 FreeLibrary(dbg_hlp->instance);
356                 free(dbg_hlp);
357                 dbg_hlp = NULL;
358                 return (int)GetLastError();     
359         }
360         
361         if(!((dbg_hlp->sym_get_module_base) = (xbt_pfn_sym_get_module_base_t)GetProcAddress(dbg_hlp->instance,"SymGetModuleBase")))
362         {
363                 FreeLibrary(dbg_hlp->instance);
364                 free(dbg_hlp);
365                 dbg_hlp = NULL;
366                 return (int)GetLastError();     
367         }
368         
369         if(!((dbg_hlp->sym_get_options) = (xbt_pfn_sym_get_options_t)GetProcAddress(dbg_hlp->instance,"SymGetOptions")))
370         {
371                 FreeLibrary(dbg_hlp->instance);
372                 free(dbg_hlp);
373                 dbg_hlp = NULL;
374                 return (int)GetLastError();     
375         }
376         
377         if(!((dbg_hlp->sym_get_sym_from_addr) = (xbt_pfn_sym_get_sym_from_addr_t)GetProcAddress(dbg_hlp->instance,"SymGetSymFromAddr")))
378         {
379                 FreeLibrary(dbg_hlp->instance);
380                 free(dbg_hlp);
381                 dbg_hlp = NULL;
382                 return (int)GetLastError();     
383         }
384         
385         if(!((dbg_hlp->sym_set_options) = (xbt_pfn_sym_set_options_t)GetProcAddress(dbg_hlp->instance,"SymSetOptions")))
386         {
387                 FreeLibrary(dbg_hlp->instance);
388                 free(dbg_hlp);
389                 dbg_hlp = NULL;
390                 return (int)GetLastError();     
391         }
392         
393         if(!((dbg_hlp->stack_walk) = (xbt_pfn_stack_walk_t)GetProcAddress(dbg_hlp->instance,"StackWalk")))
394         {
395                 FreeLibrary(dbg_hlp->instance);
396                 free(dbg_hlp);
397                 dbg_hlp = NULL;
398                 return (int)GetLastError();     
399         }
400         
401         dbg_hlp->process_handle = process_handle;
402
403         (*(dbg_hlp->sym_set_options))((*(dbg_hlp->sym_get_options))() | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
404         
405         if(!(*(dbg_hlp->sym_initialize))(dbg_hlp->process_handle,0,1))
406         {
407                 FreeLibrary(dbg_hlp->instance);
408                 free(dbg_hlp);
409                 dbg_hlp = NULL;
410                 return (int)GetLastError();
411         }
412                 
413         
414         return 0;
415 }
416
417 static int
418 dbg_hlp_finalize(void)
419 {
420         if(!dbg_hlp)
421                 return EINVAL;
422                 
423         if(!(*(dbg_hlp->sym_cleanup))(dbg_hlp->process_handle))
424                 return (int)GetLastError();
425         
426         if(!FreeLibrary(dbg_hlp->instance))
427                 return (int)GetLastError();
428         
429         free(dbg_hlp);
430         dbg_hlp = NULL;
431         
432         return 0;
433 }
434