1 /* $Id: ex.c 5173 2008-01-07 22:10:52Z mquinson $ */
3 /* backtrace_windows - backtrace displaying on windows platform */
4 /* This file is included by ex.c on need (windows x86) */
6 /* Copyright (c) 2007 The SimGrid team */
7 /* All rights reserved. */
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. */
13 * Win32 (x86) implementation backtrace, backtrace_symbols
14 * : support for application self-debugging.
19 /* Pointer function to SymInitialize() */
20 typedef BOOL (WINAPI *xbt_pfn_sym_initialize_t)(HANDLE, PSTR , BOOL);
22 /* Pointer function to SymCleanup() */
23 typedef BOOL (WINAPI *xbt_pfn_sym_cleanup_t)(HANDLE hProcess);
25 /* Pointer function to SymFunctionTableAccess() */
26 typedef PVOID (WINAPI *xbt_pfn_sym_function_table_access_t)(HANDLE, DWORD);
28 /* Pointer function to SymGetLineFromAddr() */
29 typedef BOOL (WINAPI *xbt_pfn_sym_get_line_from_addr_t)(HANDLE, DWORD, PDWORD , PIMAGEHLP_LINE);
31 /* Pointer function to SymGetModuleBase() */
32 typedef DWORD (WINAPI *xbt_pfn_sym_get_module_base_t)(HANDLE,DWORD);
34 /* Pointer function to SymGetOptions() */
35 typedef DWORD (WINAPI *xbt_pfn_sym_get_options_t)(VOID);
37 /* Pointer function to SymGetSymFromAddr() */
38 typedef BOOL (WINAPI *xbt_pfn_sym_get_sym_from_addr_t)(HANDLE, DWORD, PDWORD , OUT PIMAGEHLP_SYMBOL);
40 /* Pointer function to SymSetOptions() */
41 typedef DWORD (WINAPI *xbt_pfn_sym_set_options_t)(DWORD);
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);
46 /* This structure represents the debug_help library used interface */
47 typedef struct s_xbt_debug_help
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;
63 /* the address to the unique reference to the debug help library interface */
64 static xbt_debug_hlp_t
67 /* initialize the debug help library */
69 dbg_hlp_init(HANDLE process_handle);
71 /* finalize the debug help library */
73 dbg_hlp_finalize(void);
76 * backtrace() function.
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.
90 backtrace (void **buffer, int size);
93 * backtrace_symbols() function.
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.
100 backtrace_symbols (void *const *buffer, int size);
102 void xbt_ex_setup_backtrace(xbt_ex_t *e) {
104 char **backtrace_syms = backtrace_symbols (e->bt, e->used);
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);
112 for(i=0; i<e->used; i++)
114 e->bt_strings[i] = xbt_strdup(backtrace_syms[i]);
115 free(backtrace_syms[i]);
118 free(backtrace_syms);
122 backtrace (void **buffer, int size)
125 STACKFRAME* stack_frame;
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)];
133 CONTEXT context = {CONTEXT_FULL};
134 GetThreadContext(GetCurrentThread(), &context);
136 /* ebp points on stack base */
137 /* esp points on stack pointer, ie on last stacked element (current element) */
140 _asm mov context.Eip, eax
142 _asm mov context.Esp, eax
143 _asm mov context.Ebp, ebp
145 dbg_hlp_init(GetCurrentProcess());
147 if((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer))
153 for(pos = 0; pos < size; pos++)
158 pSym = (IMAGEHLP_SYMBOL*)__buffer;
160 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
161 pSym->MaxNameLength = MAX_SYM_NAME;
164 line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
169 stack_frame = (void*)xbt_new0(STACKFRAME,1);
177 stack_frame->AddrPC.Offset = context.Eip;
178 stack_frame->AddrPC.Mode = AddrModeFlat;
180 stack_frame->AddrFrame.Offset = context.Ebp;
181 stack_frame->AddrFrame.Mode = AddrModeFlat;
183 stack_frame->AddrStack.Offset = context.Esp;
184 stack_frame->AddrStack.Mode = AddrModeFlat;
186 if((*(dbg_hlp->stack_walk))(
187 IMAGE_FILE_MACHINE_I386,
188 dbg_hlp->process_handle,
193 dbg_hlp->sym_function_table_access,
194 dbg_hlp->sym_get_module_base,
198 if(stack_frame->AddrReturn.Offset)
201 if((*(dbg_hlp->sym_get_sym_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,pSym))
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;
209 free(stack_frame); /* no symbol or no line info */
228 backtrace_symbols (void *const *buffer, int size)
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)];
240 if((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer))
246 strings = xbt_new0(char*,size);
254 pSym = (IMAGEHLP_SYMBOL*)__buffer;
256 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
257 pSym->MaxNameLength = MAX_SYM_NAME;
260 line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
261 module.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
263 for(pos = 0; pos < size; pos++)
265 stack_frame = (STACKFRAME*)(buffer[pos]);
267 if(NULL != stack_frame)
270 if((*(dbg_hlp->sym_get_sym_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,pSym))
272 if((*(dbg_hlp->sym_get_line_from_addr))(dbg_hlp->process_handle,stack_frame->AddrPC.Offset, &offset,&line_info))
274 strings[pos] = bprintf("** In %s() at %s:%d", pSym->Name,line_info.FileName,line_info.LineNumber);
278 strings[pos] = bprintf("** In %s()", pSym->Name);
284 strings[pos] = xbt_strdup("** <no symbol>");
305 dbg_hlp_init(HANDLE process_handle)
309 /* debug help is already loaded */
314 dbg_hlp = xbt_new0(s_xbt_debug_hlp_t,1);
319 /* load the library */
320 dbg_hlp->instance = LoadLibraryA("Dbghelp.dll");
322 if(!(dbg_hlp->instance))
326 return (int)GetLastError();
329 /* get the pointers to debug help library exported functions */
331 if(!((dbg_hlp->sym_initialize) = (xbt_pfn_sym_initialize_t)GetProcAddress(dbg_hlp->instance,"SymInitialize")))
333 FreeLibrary(dbg_hlp->instance);
336 return (int)GetLastError();
339 if(!((dbg_hlp->sym_cleanup) = (xbt_pfn_sym_cleanup_t)GetProcAddress(dbg_hlp->instance,"SymCleanup")))
341 FreeLibrary(dbg_hlp->instance);
344 return (int)GetLastError();
347 if(!((dbg_hlp->sym_function_table_access) = (xbt_pfn_sym_function_table_access_t)GetProcAddress(dbg_hlp->instance,"SymFunctionTableAccess")))
349 FreeLibrary(dbg_hlp->instance);
352 return (int)GetLastError();
355 if(!((dbg_hlp->sym_get_line_from_addr) = (xbt_pfn_sym_get_line_from_addr_t)GetProcAddress(dbg_hlp->instance,"SymGetLineFromAddr")))
357 FreeLibrary(dbg_hlp->instance);
360 return (int)GetLastError();
363 if(!((dbg_hlp->sym_get_module_base) = (xbt_pfn_sym_get_module_base_t)GetProcAddress(dbg_hlp->instance,"SymGetModuleBase")))
365 FreeLibrary(dbg_hlp->instance);
368 return (int)GetLastError();
371 if(!((dbg_hlp->sym_get_options) = (xbt_pfn_sym_get_options_t)GetProcAddress(dbg_hlp->instance,"SymGetOptions")))
373 FreeLibrary(dbg_hlp->instance);
376 return (int)GetLastError();
379 if(!((dbg_hlp->sym_get_sym_from_addr) = (xbt_pfn_sym_get_sym_from_addr_t)GetProcAddress(dbg_hlp->instance,"SymGetSymFromAddr")))
381 FreeLibrary(dbg_hlp->instance);
384 return (int)GetLastError();
387 if(!((dbg_hlp->sym_set_options) = (xbt_pfn_sym_set_options_t)GetProcAddress(dbg_hlp->instance,"SymSetOptions")))
389 FreeLibrary(dbg_hlp->instance);
392 return (int)GetLastError();
395 if(!((dbg_hlp->stack_walk) = (xbt_pfn_stack_walk_t)GetProcAddress(dbg_hlp->instance,"StackWalk")))
397 FreeLibrary(dbg_hlp->instance);
400 return (int)GetLastError();
403 dbg_hlp->process_handle = process_handle;
405 (*(dbg_hlp->sym_set_options))((*(dbg_hlp->sym_get_options))() | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
407 if(!(*(dbg_hlp->sym_initialize))(dbg_hlp->process_handle,0,1))
409 FreeLibrary(dbg_hlp->instance);
412 return (int)GetLastError();
420 dbg_hlp_finalize(void)
425 if(!(*(dbg_hlp->sym_cleanup))(dbg_hlp->process_handle))
426 return (int)GetLastError();
428 if(!FreeLibrary(dbg_hlp->instance))
429 return (int)GetLastError();