Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1537eba542ca54f9b518431c3c81134c596e8ef6
[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_syms = backtrace_symbols (e->bt, e->used);
105   
106   e->used     = backtrace((void**)e->bt,XBT_BACKTRACE_SIZE);
107   e->bt_strings = NULL;
108   /* parse the output and build a new backtrace */
109   e->bt_strings = xbt_new(char*,e->used);
110   
111
112   for(i=0; i<e->used; i++) 
113   {
114       e->bt_strings[i] = xbt_strdup(backtrace_syms[i]);
115       free(backtrace_syms[i]);
116   }
117   
118   free(backtrace_syms);
119 }    
120
121 int 
122 backtrace (void **buffer, int size)
123 {
124         int pos = 0;
125         STACKFRAME* stack_frame;
126         int first = 1;
127
128         IMAGEHLP_SYMBOL * pSym;
129         unsigned long offset = 0;
130         IMAGEHLP_LINE line_info = {0};
131         byte __buffer[(sizeof(SYMBOL_INFO) +MAX_SYM_NAME * sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64)];
132
133         CONTEXT context = {CONTEXT_FULL};
134         GetThreadContext(GetCurrentThread(), &context);
135         
136         /* ebp points on stack base */
137         /* esp points on stack pointer, ie on last stacked element (current element) */
138         _asm call $+5
139         _asm pop eax 
140         _asm mov context.Eip, eax 
141         _asm mov eax, esp 
142         _asm mov context.Esp, eax 
143         _asm mov context.Ebp, ebp 
144
145         dbg_hlp_init(GetCurrentProcess());
146         
147         if((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer))
148         {
149                 errno = EINVAL;
150                 return 0;
151         }
152
153         for(pos = 0; pos < size; pos++)
154                 buffer[pos] = NULL;
155         
156         pos = 0;
157
158         pSym = (IMAGEHLP_SYMBOL*)__buffer;
159
160         pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
161         pSym->MaxNameLength = MAX_SYM_NAME;
162
163
164         line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
165         
166
167     while(pos < size)
168     {
169                 stack_frame = (void*)xbt_new0(STACKFRAME,1);
170                 
171                 if(!stack_frame)
172                 {
173                         errno = ENOMEM;
174                         break;
175                 }
176                 
177                 stack_frame->AddrPC.Offset = context.Eip;
178             stack_frame->AddrPC.Mode = AddrModeFlat;
179         
180             stack_frame->AddrFrame.Offset = context.Ebp;
181             stack_frame->AddrFrame.Mode = AddrModeFlat;
182         
183             stack_frame->AddrStack.Offset = context.Esp;
184             stack_frame->AddrStack.Mode = AddrModeFlat;
185                 
186                 if((*(dbg_hlp->stack_walk))(
187                                         IMAGE_FILE_MACHINE_I386,
188                                         dbg_hlp->process_handle,
189                                         GetCurrentThread(),
190                                         stack_frame, 
191                                         &context,
192                                         NULL,
193                                         dbg_hlp->sym_function_table_access,
194                                         dbg_hlp->sym_get_module_base,
195                                         NULL)
196                         && !first) 
197                 {
198                         if(stack_frame->AddrReturn.Offset)
199                         {
200
201                                 if((*(dbg_hlp->sym_get_sym_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,pSym))
202                                 {       
203                                         if((*(dbg_hlp->sym_get_line_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,&line_info))
204                                                 buffer[pos++] = (void*)stack_frame;
205                                 }
206                         }
207                         else
208                         {
209                                 free(stack_frame); /* no symbol or no line info */
210                                 break;
211                         }
212                 }
213                 else
214                 {
215                         free(stack_frame);
216
217                         if(first)
218                                 first = 0;
219                         else
220                                 break;
221                 }               
222     }
223
224     return pos;
225 }
226
227 char ** 
228 backtrace_symbols (void *const *buffer, int size)
229 {
230         int pos;
231         int success = 0;        
232         char** strings;
233         STACKFRAME* stack_frame;
234         IMAGEHLP_SYMBOL * pSym;
235         unsigned long offset = 0;
236         IMAGEHLP_LINE line_info = {0};
237         IMAGEHLP_MODULE module = {0};
238         byte __buffer[(sizeof(SYMBOL_INFO) +MAX_SYM_NAME * sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64)];
239
240         if((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer))
241         {
242                 errno = EINVAL;
243                 return NULL;
244         }
245         
246         strings = xbt_new0(char*,size);
247         
248         if(NULL == strings)
249         {
250                 errno = ENOMEM;
251                 return NULL;
252         }
253         
254         pSym = (IMAGEHLP_SYMBOL*)__buffer;
255
256         pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
257         pSym->MaxNameLength = MAX_SYM_NAME;
258
259
260         line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
261         module.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
262         
263         for(pos = 0; pos < size; pos++)
264         {
265                 stack_frame = (STACKFRAME*)(buffer[pos]);
266
267                 if(NULL != stack_frame)
268                 {
269                 
270                         if((*(dbg_hlp->sym_get_sym_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,pSym))
271                         {       
272                                 if((*(dbg_hlp->sym_get_line_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,&line_info))
273                                 {
274                                         strings[pos] = bprintf("**   In %s() at %s:%d", pSym->Name,line_info.FileName,line_info.LineNumber);
275                                 }
276                                 else
277                                 {
278                                         strings[pos] = bprintf("**   In %s()", pSym->Name);
279                                 }
280                                 success = 1;
281                         }
282                         else
283                         {
284                                 strings[pos] = xbt_strdup("**   <no symbol>");
285                         }
286
287                         free(stack_frame);
288                 }
289                 else
290                         break;
291         }
292         
293         if(!success)
294         {
295                 free(strings);
296                 strings = NULL;
297         }
298
299         dbg_hlp_finalize();
300         
301         return strings;
302 }
303
304 static int
305 dbg_hlp_init(HANDLE process_handle)
306 {
307         if(dbg_hlp)
308         {
309                 /* debug help is already loaded */
310                 return 0;
311         }
312
313         /* allocation */
314         dbg_hlp = xbt_new0(s_xbt_debug_hlp_t,1);
315         
316         if(!dbg_hlp)
317                 return ENOMEM;
318         
319         /* load the library */
320         dbg_hlp->instance = LoadLibraryA("Dbghelp.dll");
321         
322         if(!(dbg_hlp->instance))
323         {
324                 free(dbg_hlp);
325                 dbg_hlp = NULL;
326                 return (int)GetLastError();
327         }
328         
329         /* get the pointers to debug help library exported functions */
330         
331         if(!((dbg_hlp->sym_initialize) = (xbt_pfn_sym_initialize_t)GetProcAddress(dbg_hlp->instance,"SymInitialize")))
332         {
333                 FreeLibrary(dbg_hlp->instance);
334                 free(dbg_hlp);
335                 dbg_hlp = NULL;
336                 return (int)GetLastError();     
337         }
338                 
339         if(!((dbg_hlp->sym_cleanup) = (xbt_pfn_sym_cleanup_t)GetProcAddress(dbg_hlp->instance,"SymCleanup")))
340         {
341                 FreeLibrary(dbg_hlp->instance);
342                 free(dbg_hlp);
343                 dbg_hlp = NULL;
344                 return (int)GetLastError();     
345         }
346         
347         if(!((dbg_hlp->sym_function_table_access) = (xbt_pfn_sym_function_table_access_t)GetProcAddress(dbg_hlp->instance,"SymFunctionTableAccess")))
348         {
349                 FreeLibrary(dbg_hlp->instance);
350                 free(dbg_hlp);
351                 dbg_hlp = NULL;
352                 return (int)GetLastError();     
353         }
354         
355         if(!((dbg_hlp->sym_get_line_from_addr) = (xbt_pfn_sym_get_line_from_addr_t)GetProcAddress(dbg_hlp->instance,"SymGetLineFromAddr")))
356         {
357                 FreeLibrary(dbg_hlp->instance);
358                 free(dbg_hlp);
359                 dbg_hlp = NULL;
360                 return (int)GetLastError();     
361         }
362         
363         if(!((dbg_hlp->sym_get_module_base) = (xbt_pfn_sym_get_module_base_t)GetProcAddress(dbg_hlp->instance,"SymGetModuleBase")))
364         {
365                 FreeLibrary(dbg_hlp->instance);
366                 free(dbg_hlp);
367                 dbg_hlp = NULL;
368                 return (int)GetLastError();     
369         }
370         
371         if(!((dbg_hlp->sym_get_options) = (xbt_pfn_sym_get_options_t)GetProcAddress(dbg_hlp->instance,"SymGetOptions")))
372         {
373                 FreeLibrary(dbg_hlp->instance);
374                 free(dbg_hlp);
375                 dbg_hlp = NULL;
376                 return (int)GetLastError();     
377         }
378         
379         if(!((dbg_hlp->sym_get_sym_from_addr) = (xbt_pfn_sym_get_sym_from_addr_t)GetProcAddress(dbg_hlp->instance,"SymGetSymFromAddr")))
380         {
381                 FreeLibrary(dbg_hlp->instance);
382                 free(dbg_hlp);
383                 dbg_hlp = NULL;
384                 return (int)GetLastError();     
385         }
386         
387         if(!((dbg_hlp->sym_set_options) = (xbt_pfn_sym_set_options_t)GetProcAddress(dbg_hlp->instance,"SymSetOptions")))
388         {
389                 FreeLibrary(dbg_hlp->instance);
390                 free(dbg_hlp);
391                 dbg_hlp = NULL;
392                 return (int)GetLastError();     
393         }
394         
395         if(!((dbg_hlp->stack_walk) = (xbt_pfn_stack_walk_t)GetProcAddress(dbg_hlp->instance,"StackWalk")))
396         {
397                 FreeLibrary(dbg_hlp->instance);
398                 free(dbg_hlp);
399                 dbg_hlp = NULL;
400                 return (int)GetLastError();     
401         }
402         
403         dbg_hlp->process_handle = process_handle;
404
405         (*(dbg_hlp->sym_set_options))((*(dbg_hlp->sym_get_options))() | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
406         
407         if(!(*(dbg_hlp->sym_initialize))(dbg_hlp->process_handle,0,1))
408         {
409                 FreeLibrary(dbg_hlp->instance);
410                 free(dbg_hlp);
411                 dbg_hlp = NULL;
412                 return (int)GetLastError();
413         }
414                 
415         
416         return 0;
417 }
418
419 static int
420 dbg_hlp_finalize(void)
421 {
422         if(!dbg_hlp)
423                 return EINVAL;
424                 
425         if(!(*(dbg_hlp->sym_cleanup))(dbg_hlp->process_handle))
426                 return (int)GetLastError();
427         
428         if(!FreeLibrary(dbg_hlp->instance))
429                 return (int)GetLastError();
430         
431         free(dbg_hlp);
432         dbg_hlp = NULL;
433         
434         return 0;
435 }
436