Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new tracing mask TRACE_VOLUME to trace the msg tasks communication size and group...
[simgrid.git] / src / xbt / backtrace_windows.c
1 /* $Id$ */
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 #if defined(_XBT_BORLAND_COMPILER) || defined(_XBT_VISUALC_COMPILER)
18 /* native windows build */
19 #  include <dbghelp.h>
20 #else
21 /* gcc-based cross-compiling */
22 #  include "xbt/wine_dbghelp.h"
23 #endif
24
25
26 /* SymInitialize() */
27 typedef BOOL(WINAPI * fun_initialize_t) (HANDLE, PSTR, BOOL);
28 static fun_initialize_t fun_initialize;
29
30 /* SymCleanup() */
31 typedef BOOL(WINAPI * fun_cleanup_t) (HANDLE hProcess);
32 static fun_cleanup_t fun_cleanup;
33
34 /* SymFunctionTableAccess() */
35 typedef PVOID(WINAPI * fun_function_table_access_t) (HANDLE, DWORD);
36 static fun_function_table_access_t fun_function_table_access;
37
38 /* SymGetLineFromAddr() */
39 typedef BOOL(WINAPI * fun_get_line_from_addr_t) (HANDLE, DWORD,
40                                                  PDWORD, PIMAGEHLP_LINE);
41 static fun_get_line_from_addr_t fun_get_line_from_addr;
42
43 /* SymGetModuleBase() */
44 typedef DWORD(WINAPI * fun_get_module_base_t) (HANDLE, DWORD);
45 static fun_get_module_base_t fun_get_module_base;
46
47 /* SymGetOptions() */
48 typedef DWORD(WINAPI * fun_get_options_t) (VOID);
49 static fun_get_options_t fun_get_options;
50
51 /* SymSetOptions() */
52 typedef DWORD(WINAPI * fun_set_options_t) (DWORD);
53 static fun_set_options_t fun_set_options;
54
55 /* Pointer function to SymGetSymFromAddr() */
56 typedef BOOL(WINAPI * fun_get_sym_from_addr_t) (HANDLE, DWORD, PDWORD,
57                                                 OUT PIMAGEHLP_SYMBOL);
58 static fun_get_sym_from_addr_t fun_get_sym_from_addr;
59
60 /* Pointer function to StackWalk() */
61 typedef BOOL(WINAPI * fun_stack_walk_t) (DWORD, HANDLE, HANDLE,
62                                          LPSTACKFRAME, PVOID,
63                                          PREAD_PROCESS_MEMORY_ROUTINE,
64                                          PFUNCTION_TABLE_ACCESS_ROUTINE,
65                                          PGET_MODULE_BASE_ROUTINE,
66                                          PTRANSLATE_ADDRESS_ROUTINE);
67 static fun_stack_walk_t fun_stack_walk;
68
69 static HINSTANCE hlp_dbg_instance = NULL;
70 static HANDLE process_handle = NULL;
71
72
73 /* Module creation/destruction: nothing to do on linux */
74 void xbt_backtrace_init(void)
75 {
76   process_handle = GetCurrentProcess();
77
78   if (hlp_dbg_instance) {
79     /* debug help is already loaded */
80     return;
81   }
82
83   /* load the library */
84   hlp_dbg_instance = LoadLibraryA("Dbghelp.dll");
85
86   if (!hlp_dbg_instance)
87     return;
88
89   /* get the pointers to debug help library exported functions */
90   fun_initialize =
91     (fun_initialize_t) GetProcAddress(hlp_dbg_instance, "SymInitialize");
92   fun_cleanup =
93     (fun_cleanup_t) GetProcAddress(hlp_dbg_instance, "SymCleanup");
94   fun_function_table_access =
95     (fun_function_table_access_t) GetProcAddress(hlp_dbg_instance,
96                                                  "SymFunctionTableAccess");
97   fun_get_line_from_addr =
98     (fun_get_line_from_addr_t) GetProcAddress(hlp_dbg_instance,
99                                               "SymGetLineFromAddr");
100   fun_get_module_base =
101     (fun_get_module_base_t) GetProcAddress(hlp_dbg_instance,
102                                            "SymGetModuleBase");
103   fun_get_options =
104     (fun_get_options_t) GetProcAddress(hlp_dbg_instance, "SymGetOptions");
105   fun_get_sym_from_addr =
106     (fun_get_sym_from_addr_t) GetProcAddress(hlp_dbg_instance,
107                                              "SymGetSymFromAddr");
108   fun_set_options =
109     (fun_set_options_t) GetProcAddress(hlp_dbg_instance, "SymSetOptions");
110   fun_stack_walk =
111     (fun_stack_walk_t) GetProcAddress(hlp_dbg_instance, "StackWalk");
112
113   /* Check that everything worked well */
114   if (!fun_initialize ||
115       !fun_cleanup ||
116       !fun_function_table_access ||
117       !fun_get_line_from_addr ||
118       !fun_get_module_base ||
119       !fun_get_options ||
120       !fun_get_sym_from_addr || !fun_set_options || !fun_stack_walk) {
121     FreeLibrary(hlp_dbg_instance);
122     hlp_dbg_instance = NULL;
123     return;
124   }
125
126   (*fun_set_options) ((*fun_get_options) () |
127                       SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
128
129   if (!(*fun_initialize) (process_handle, 0, 1)) {
130     FreeLibrary(hlp_dbg_instance);
131     hlp_dbg_instance = NULL;
132   }
133 }
134
135 void xbt_backtrace_exit(void)
136 {
137   if (!hlp_dbg_instance)
138     return;
139
140   if ((*fun_cleanup) (process_handle))
141     FreeLibrary(hlp_dbg_instance);
142
143   hlp_dbg_instance = NULL;
144 }
145
146 /*
147  * backtrace() function.
148  *
149  * Returns a backtrace for the calling program, in  the  array
150  * pointed  to  by  buffer.  A backtrace is the series of currently active
151  * function calls for the program.  Each item in the array pointed  to  by
152  * buffer  is  of  type  void *, and is the return address from the corre-
153  * sponding stack frame.  The size argument specifies the  maximum  number
154  * of  addresses that can be stored in buffer.  If the backtrace is larger
155  * than size, then the addresses corresponding to  the  size  most  recent
156  * function  calls  are  returned;  to obtain the complete backtrace, make
157  * sure that buffer and size are large enough.
158  */
159
160 int backtrace(void **buffer, int size);
161
162 /*
163  * backtrace_symbols() function.
164  *
165  * Given the set of addresses returned by  backtrace()  in  buffer,  back-
166  * trace_symbols()  translates the addresses into an array of strings containing
167  * the name, the source file and the line number or the las called functions.
168  */
169 char **backtrace_symbols(void *const *buffer, int size);
170
171 void xbt_backtrace_current(xbt_ex_t * e)
172 {
173   e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
174 }
175
176
177 void xbt_ex_setup_backtrace(xbt_ex_t * e)
178 {
179   int i;
180   char **backtrace_syms;
181
182   xbt_assert0(e
183               && e->used,
184               "Backtrace not setup yet, cannot set it up for display");
185
186   backtrace_syms = backtrace_symbols(e->bt, e->used);
187   e->bt_strings = NULL;
188   /* parse the output and build a new backtrace */
189   e->bt_strings = xbt_new(char *, e->used);
190
191
192   for (i = 0; i < e->used; i++)
193     e->bt_strings[i] = backtrace_syms[i];
194
195   free(backtrace_syms);
196 }
197
198 int backtrace(void **buffer, int size)
199 {
200   int pos = 0;
201   STACKFRAME *stack_frame;
202   int first = 1;
203
204   IMAGEHLP_SYMBOL *pSym;
205   unsigned long offset = 0;
206   IMAGEHLP_LINE line_info = { 0 };
207   byte
208     __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
209               sizeof(ULONG64) - 1) / sizeof(ULONG64)];
210
211   CONTEXT context = { CONTEXT_FULL };
212   GetThreadContext(GetCurrentThread(), &context);
213
214   /* ebp points on stack base */
215   /* esp points on stack pointer, ie on last stacked element (current element) */
216   _asm call $ + 5
217     _asm pop eax
218     _asm mov context.Eip, eax
219     _asm mov eax, esp
220     _asm mov context.Esp, eax
221     _asm mov context.Ebp, ebp
222     if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
223     errno = EINVAL;
224     return 0;
225   }
226
227   for (pos = 0; pos < size; pos++)
228     buffer[pos] = NULL;
229
230   pos = 0;
231
232   pSym = (IMAGEHLP_SYMBOL *) __buffer;
233
234   pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
235   pSym->MaxNameLength = MAX_SYM_NAME;
236
237
238   line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
239
240
241   while (pos < size) {
242     stack_frame = (void *) xbt_new0(STACKFRAME, 1);
243
244     stack_frame->AddrPC.Offset = context.Eip;
245     stack_frame->AddrPC.Mode = AddrModeFlat;
246
247     stack_frame->AddrFrame.Offset = context.Ebp;
248     stack_frame->AddrFrame.Mode = AddrModeFlat;
249
250     stack_frame->AddrStack.Offset = context.Esp;
251     stack_frame->AddrStack.Mode = AddrModeFlat;
252
253     if ((*fun_stack_walk) (IMAGE_FILE_MACHINE_I386,
254                            process_handle,
255                            GetCurrentThread(),
256                            stack_frame,
257                            &context,
258                            NULL,
259                            fun_function_table_access,
260                            fun_get_module_base, NULL)
261         && !first) {
262       if (stack_frame->AddrReturn.Offset) {
263
264         if ((*fun_get_sym_from_addr)
265             (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
266           if ((*fun_get_line_from_addr)
267               (process_handle, stack_frame->AddrPC.Offset, &offset,
268                &line_info))
269             buffer[pos++] = (void *) stack_frame;
270         }
271       } else {
272         free(stack_frame);      /* no symbol or no line info */
273         break;
274       }
275     } else {
276       free(stack_frame);
277
278       if (first)
279         first = 0;
280       else
281         break;
282     }
283   }
284
285   return pos;
286 }
287
288 char **backtrace_symbols(void *const *buffer, int size)
289 {
290   int pos;
291   int success = 0;
292   char **strings;
293   STACKFRAME *stack_frame;
294   IMAGEHLP_SYMBOL *pSym;
295   unsigned long offset = 0;
296   IMAGEHLP_LINE line_info = { 0 };
297   IMAGEHLP_MODULE module = { 0 };
298   byte
299     __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
300               sizeof(ULONG64) - 1) / sizeof(ULONG64)];
301
302   if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
303     errno = EINVAL;
304     return NULL;
305   }
306
307   strings = xbt_new0(char *, size);
308
309   pSym = (IMAGEHLP_SYMBOL *) __buffer;
310
311   pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
312   pSym->MaxNameLength = MAX_SYM_NAME;
313
314
315   line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
316   module.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
317
318   for (pos = 0; pos < size; pos++) {
319     stack_frame = (STACKFRAME *) (buffer[pos]);
320
321     if (NULL != stack_frame) {
322
323       if ((*fun_get_sym_from_addr)
324           (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
325         if ((*fun_get_line_from_addr)
326             (process_handle, stack_frame->AddrPC.Offset, &offset, &line_info)) {
327           strings[pos] =
328             bprintf("**   In %s() at %s:%d", pSym->Name, line_info.FileName,
329                     (int) line_info.LineNumber);
330         } else {
331           strings[pos] = bprintf("**   In %s()", pSym->Name);
332         }
333         success = 1;
334       } else {
335         strings[pos] = xbt_strdup("**   <no symbol>");
336       }
337
338       free(stack_frame);
339     } else
340       break;
341   }
342
343   if (!success) {
344     free(strings);
345     strings = NULL;
346   }
347
348   return strings;
349 }