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.
17 #if defined(_XBT_BORLAND_COMPILER) || defined(_XBT_VISUALC_COMPILER)
18 /* native windows build */
21 /* gcc-based cross-compiling */
22 # include "xbt/wine_dbghelp.h"
27 typedef BOOL(WINAPI * fun_initialize_t) (HANDLE, PSTR, BOOL);
28 static fun_initialize_t fun_initialize;
31 typedef BOOL(WINAPI * fun_cleanup_t) (HANDLE hProcess);
32 static fun_cleanup_t fun_cleanup;
34 /* SymFunctionTableAccess() */
35 typedef PVOID(WINAPI * fun_function_table_access_t) (HANDLE, DWORD);
36 static fun_function_table_access_t fun_function_table_access;
38 /* SymGetLineFromAddr() */
39 typedef BOOL(WINAPI * fun_get_line_from_addr_t) (HANDLE, DWORD,
42 static fun_get_line_from_addr_t fun_get_line_from_addr;
44 /* SymGetModuleBase() */
45 typedef DWORD(WINAPI *fun_get_module_base_t) (HANDLE, DWORD);
46 static fun_get_module_base_t fun_get_module_base;
49 typedef DWORD(WINAPI *fun_get_options_t) (VOID);
50 static fun_get_options_t fun_get_options;
53 typedef DWORD(WINAPI *fun_set_options_t) (DWORD);
54 static fun_set_options_t fun_set_options;
56 /* Pointer function to SymGetSymFromAddr() */
57 typedef BOOL(WINAPI *fun_get_sym_from_addr_t) (HANDLE, DWORD, PDWORD,
58 OUT PIMAGEHLP_SYMBOL);
59 static fun_get_sym_from_addr_t fun_get_sym_from_addr;
61 /* Pointer function to StackWalk() */
62 typedef BOOL(WINAPI * fun_stack_walk_t) (DWORD, HANDLE, HANDLE,
64 PREAD_PROCESS_MEMORY_ROUTINE,
65 PFUNCTION_TABLE_ACCESS_ROUTINE,
66 PGET_MODULE_BASE_ROUTINE,
67 PTRANSLATE_ADDRESS_ROUTINE);
68 static fun_stack_walk_t fun_stack_walk;
70 static HINSTANCE hlp_dbg_instance = NULL;
71 static HANDLE process_handle = NULL;
74 /* Module creation/destruction: nothing to do on linux */
75 void xbt_backtrace_init(void) {
76 process_handle = GetCurrentProcess();
78 if (hlp_dbg_instance) {
79 /* debug help is already loaded */
83 /* load the library */
84 hlp_dbg_instance = LoadLibraryA("Dbghelp.dll");
86 if (!hlp_dbg_instance)
89 /* get the pointers to debug help library exported functions */
91 (fun_initialize_t) GetProcAddress(hlp_dbg_instance, "SymInitialize");
93 (fun_cleanup_t) GetProcAddress(hlp_dbg_instance, "SymCleanup");
94 fun_function_table_access =
95 (fun_function_table_access_t) GetProcAddress(hlp_dbg_instance, "SymFunctionTableAccess");
96 fun_get_line_from_addr =
97 (fun_get_line_from_addr_t) GetProcAddress(hlp_dbg_instance, "SymGetLineFromAddr");
99 (fun_get_module_base_t) GetProcAddress(hlp_dbg_instance, "SymGetModuleBase");
101 (fun_get_options_t) GetProcAddress(hlp_dbg_instance, "SymGetOptions");
102 fun_get_sym_from_addr =
103 (fun_get_sym_from_addr_t) GetProcAddress(hlp_dbg_instance, "SymGetSymFromAddr");
105 (fun_set_options_t) GetProcAddress(hlp_dbg_instance, "SymSetOptions");
107 (fun_stack_walk_t) GetProcAddress(hlp_dbg_instance, "StackWalk");
109 /* Check that everything worked well */
110 if (!fun_initialize ||
112 !fun_function_table_access ||
113 !fun_get_line_from_addr ||
114 !fun_get_module_base ||
116 !fun_get_sym_from_addr ||
120 FreeLibrary(hlp_dbg_instance);
121 hlp_dbg_instance = NULL;
125 (*fun_set_options) ((*fun_get_options) () |
126 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
128 if (!(*fun_initialize) (process_handle, 0, 1)) {
129 FreeLibrary(hlp_dbg_instance);
130 hlp_dbg_instance = NULL;
133 void xbt_backtrace_exit(void) {
134 if (!hlp_dbg_instance)
137 if ((*fun_cleanup) (process_handle))
138 FreeLibrary(hlp_dbg_instance);
140 hlp_dbg_instance = NULL;
144 * backtrace() function.
146 * Returns a backtrace for the calling program, in the array
147 * pointed to by buffer. A backtrace is the series of currently active
148 * function calls for the program. Each item in the array pointed to by
149 * buffer is of type void *, and is the return address from the corre-
150 * sponding stack frame. The size argument specifies the maximum number
151 * of addresses that can be stored in buffer. If the backtrace is larger
152 * than size, then the addresses corresponding to the size most recent
153 * function calls are returned; to obtain the complete backtrace, make
154 * sure that buffer and size are large enough.
157 int backtrace(void **buffer, int size);
160 * backtrace_symbols() function.
162 * Given the set of addresses returned by backtrace() in buffer, back-
163 * trace_symbols() translates the addresses into an array of strings containing
164 * the name, the source file and the line number or the las called functions.
166 char **backtrace_symbols(void *const *buffer, int size);
168 void xbt_backtrace_current(xbt_ex_t * e) {
169 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
173 void xbt_ex_setup_backtrace(xbt_ex_t * e)
176 char **backtrace_syms;
178 xbt_assert0(e && e->used,"Backtrace not setup yet, cannot set it up for display");
180 backtrace_syms = backtrace_symbols(e->bt, e->used);
181 e->bt_strings = NULL;
182 /* parse the output and build a new backtrace */
183 e->bt_strings = xbt_new(char *, e->used);
186 for (i = 0; i < e->used; i++)
187 e->bt_strings[i] = backtrace_syms[i];
189 free(backtrace_syms);
192 int backtrace(void **buffer, int size)
195 STACKFRAME *stack_frame;
198 IMAGEHLP_SYMBOL *pSym;
199 unsigned long offset = 0;
200 IMAGEHLP_LINE line_info = { 0 };
202 __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
203 sizeof(ULONG64) - 1) / sizeof(ULONG64)];
205 CONTEXT context = { CONTEXT_FULL };
206 GetThreadContext(GetCurrentThread(), &context);
208 /* ebp points on stack base */
209 /* esp points on stack pointer, ie on last stacked element (current element) */
212 _asm mov context.Eip, eax
214 _asm mov context.Esp, eax
215 _asm mov context.Ebp, ebp
217 if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
222 for (pos = 0; pos < size; pos++)
227 pSym = (IMAGEHLP_SYMBOL *) __buffer;
229 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
230 pSym->MaxNameLength = MAX_SYM_NAME;
233 line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
237 stack_frame = (void *) xbt_new0(STACKFRAME, 1);
239 stack_frame->AddrPC.Offset = context.Eip;
240 stack_frame->AddrPC.Mode = AddrModeFlat;
242 stack_frame->AddrFrame.Offset = context.Ebp;
243 stack_frame->AddrFrame.Mode = AddrModeFlat;
245 stack_frame->AddrStack.Offset = context.Esp;
246 stack_frame->AddrStack.Mode = AddrModeFlat;
248 if ((*fun_stack_walk) (IMAGE_FILE_MACHINE_I386,
254 fun_function_table_access,
255 fun_get_module_base, NULL)
257 if (stack_frame->AddrReturn.Offset) {
259 if ((*fun_get_sym_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
260 if ((*fun_get_line_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, &line_info))
261 buffer[pos++] = (void *) stack_frame;
264 free(stack_frame); /* no symbol or no line info */
280 char **backtrace_symbols(void *const *buffer, int size)
285 STACKFRAME *stack_frame;
286 IMAGEHLP_SYMBOL *pSym;
287 unsigned long offset = 0;
288 IMAGEHLP_LINE line_info = { 0 };
289 IMAGEHLP_MODULE module = { 0 };
291 __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
292 sizeof(ULONG64) - 1) / sizeof(ULONG64)];
294 if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
299 strings = xbt_new0(char *, size);
301 pSym = (IMAGEHLP_SYMBOL *) __buffer;
303 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
304 pSym->MaxNameLength = MAX_SYM_NAME;
307 line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
308 module.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
310 for (pos = 0; pos < size; pos++) {
311 stack_frame = (STACKFRAME *) (buffer[pos]);
313 if (NULL != stack_frame) {
315 if ((*fun_get_sym_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
316 if ((*fun_get_line_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, &line_info)) {
318 bprintf("** In %s() at %s:%d", pSym->Name, line_info.FileName,
319 (int)line_info.LineNumber);
321 strings[pos] = bprintf("** In %s()", pSym->Name);
325 strings[pos] = xbt_strdup("** <no symbol>");