1 /* backtrace_windows - backtrace displaying on windows platform */
2 /* This file is included by ex.c on need (windows x86) */
4 /* Copyright (c) 2008-2014. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
11 * Win32 (x86) implementation backtrace, backtrace_symbols:
12 * support for application self-debugging.
15 #if defined(_XBT_VISUALC_COMPILER)
16 /* native windows build */
19 /* gcc-based cross-compiling */
20 # include "xbt/wine_dbghelp.h"
25 typedef BOOL(WINAPI * fun_initialize_t) (HANDLE, PSTR, BOOL);
26 static fun_initialize_t fun_initialize;
29 typedef BOOL(WINAPI * fun_cleanup_t) (HANDLE hProcess);
30 static fun_cleanup_t fun_cleanup;
32 /* SymFunctionTableAccess() */
33 typedef PVOID(WINAPI * fun_function_table_access_t) (HANDLE, DWORD);
34 static fun_function_table_access_t fun_function_table_access;
36 /* SymGetLineFromAddr() */
37 typedef BOOL(WINAPI * fun_get_line_from_addr_t) (HANDLE, DWORD,
38 PDWORD, PIMAGEHLP_LINE);
39 static fun_get_line_from_addr_t fun_get_line_from_addr;
41 /* SymGetModuleBase() */
42 typedef DWORD(WINAPI * fun_get_module_base_t) (HANDLE, DWORD);
43 static fun_get_module_base_t fun_get_module_base;
46 typedef DWORD(WINAPI * fun_get_options_t) (VOID);
47 static fun_get_options_t fun_get_options;
50 typedef DWORD(WINAPI * fun_set_options_t) (DWORD);
51 static fun_set_options_t fun_set_options;
53 /* Pointer function to SymGetSymFromAddr() */
54 typedef BOOL(WINAPI * fun_get_sym_from_addr_t) (HANDLE, DWORD, PDWORD,
55 OUT PIMAGEHLP_SYMBOL);
56 static fun_get_sym_from_addr_t fun_get_sym_from_addr;
58 /* Pointer function to StackWalk() */
59 typedef BOOL(WINAPI * fun_stack_walk_t) (DWORD, HANDLE, HANDLE,
61 PREAD_PROCESS_MEMORY_ROUTINE,
62 PFUNCTION_TABLE_ACCESS_ROUTINE,
63 PGET_MODULE_BASE_ROUTINE,
64 PTRANSLATE_ADDRESS_ROUTINE);
65 static fun_stack_walk_t fun_stack_walk;
67 static HINSTANCE hlp_dbg_instance = NULL;
68 static HANDLE process_handle = NULL;
71 /* Module creation/destruction: initialize our tables */
72 void xbt_backtrace_preinit(void)
74 process_handle = GetCurrentProcess();
76 if (hlp_dbg_instance) {
77 /* debug help is already loaded */
81 /* load the library */
82 hlp_dbg_instance = LoadLibraryA("Dbghelp.dll");
84 if (!hlp_dbg_instance)
87 /* get the pointers to debug help library exported functions */
89 (fun_initialize_t) GetProcAddress(hlp_dbg_instance, "SymInitialize");
91 (fun_cleanup_t) GetProcAddress(hlp_dbg_instance, "SymCleanup");
92 fun_function_table_access =
93 (fun_function_table_access_t) GetProcAddress(hlp_dbg_instance,
94 "SymFunctionTableAccess");
95 fun_get_line_from_addr =
96 (fun_get_line_from_addr_t) GetProcAddress(hlp_dbg_instance,
97 "SymGetLineFromAddr");
99 (fun_get_module_base_t) GetProcAddress(hlp_dbg_instance,
102 (fun_get_options_t) GetProcAddress(hlp_dbg_instance,
104 fun_get_sym_from_addr =
105 (fun_get_sym_from_addr_t) GetProcAddress(hlp_dbg_instance,
106 "SymGetSymFromAddr");
108 (fun_set_options_t) GetProcAddress(hlp_dbg_instance,
111 (fun_stack_walk_t) GetProcAddress(hlp_dbg_instance, "StackWalk");
113 /* Check that everything worked well */
114 if (!fun_initialize ||
116 !fun_function_table_access ||
117 !fun_get_line_from_addr ||
118 !fun_get_module_base ||
120 !fun_get_sym_from_addr || !fun_set_options || !fun_stack_walk) {
121 FreeLibrary(hlp_dbg_instance);
122 hlp_dbg_instance = NULL;
126 fun_set_options(fun_get_options() |
127 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
129 if (!fun_initialize(process_handle, 0, 1)) {
130 FreeLibrary(hlp_dbg_instance);
131 hlp_dbg_instance = NULL;
135 void xbt_backtrace_postexit(void)
137 if (!hlp_dbg_instance)
140 if (fun_cleanup(process_handle))
141 FreeLibrary(hlp_dbg_instance);
143 hlp_dbg_instance = NULL;
147 * backtrace() function.
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.
160 int backtrace(void **buffer, int size);
163 * backtrace_symbols() function.
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.
169 char **backtrace_symbols(void *const *buffer, int size);
171 void xbt_backtrace_current(xbt_ex_t * e)
173 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
177 void xbt_ex_setup_backtrace(xbt_ex_t * e)
180 char **backtrace_syms;
182 xbt_assert(e, "Backtrace not setup yet, cannot set it up for display");
187 backtrace_syms = backtrace_symbols(e->bt, e->used);
188 e->bt_strings = NULL;
189 /* parse the output and build a new backtrace */
190 e->bt_strings = xbt_new(char *, e->used);
192 for (i = 0; i < e->used; i++)
193 e->bt_strings[i] = backtrace_syms[i];
195 free(backtrace_syms);
198 int backtrace(void **buffer, int size)
201 STACKFRAME *stack_frame;
204 IMAGEHLP_SYMBOL *pSym;
205 unsigned long offset = 0;
206 IMAGEHLP_LINE line_info = { 0 };
207 byte __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
208 sizeof(ULONG64) - 1) / sizeof(ULONG64)];
210 CONTEXT context = { CONTEXT_FULL };
211 GetThreadContext(GetCurrentThread(), &context);
213 /* ebp points on stack base */
214 /* esp points on stack pointer, ie on last stacked element (current element) */
217 _asm mov context.Eip, eax
219 _asm mov context.Esp, eax
220 _asm mov context.Ebp, ebp
221 if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
226 for (pos = 0; pos < size; pos++)
231 pSym = (IMAGEHLP_SYMBOL *) __buffer;
233 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
234 pSym->MaxNameLength = MAX_SYM_NAME;
237 line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
241 stack_frame = (void *) xbt_new0(STACKFRAME, 1);
243 stack_frame->AddrPC.Offset = context.Eip;
244 stack_frame->AddrPC.Mode = AddrModeFlat;
246 stack_frame->AddrFrame.Offset = context.Ebp;
247 stack_frame->AddrFrame.Mode = AddrModeFlat;
249 stack_frame->AddrStack.Offset = context.Esp;
250 stack_frame->AddrStack.Mode = AddrModeFlat;
252 if ((*fun_stack_walk) (IMAGE_FILE_MACHINE_I386,
258 fun_function_table_access,
259 fun_get_module_base, NULL)
261 if (stack_frame->AddrReturn.Offset) {
263 if ((*fun_get_sym_from_addr)
264 (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
265 if ((*fun_get_line_from_addr)
266 (process_handle, stack_frame->AddrPC.Offset, &offset,
268 buffer[pos++] = (void *) stack_frame;
271 free(stack_frame); /* no symbol or no line info */
287 char **backtrace_symbols(void *const *buffer, int size)
292 STACKFRAME *stack_frame;
293 IMAGEHLP_SYMBOL *pSym;
294 unsigned long offset = 0;
295 IMAGEHLP_LINE line_info = { 0 };
296 IMAGEHLP_MODULE module = { 0 };
297 byte __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
298 sizeof(ULONG64) - 1) / sizeof(ULONG64)];
300 if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
305 strings = xbt_new0(char *, size);
307 pSym = (IMAGEHLP_SYMBOL *) __buffer;
309 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
310 pSym->MaxNameLength = MAX_SYM_NAME;
313 line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE);
314 module.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
316 for (pos = 0; pos < size; pos++) {
317 stack_frame = (STACKFRAME *) (buffer[pos]);
319 if (NULL != stack_frame) {
321 if (fun_get_sym_from_addr
322 (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
323 if (fun_get_line_from_addr
324 (process_handle, stack_frame->AddrPC.Offset, &offset,
327 bprintf("** In %s() at %s:%d", pSym->Name,
328 line_info.FileName, (int) line_info.LineNumber);
330 strings[pos] = bprintf("** In %s()", pSym->Name);
334 strings[pos] = xbt_strdup("** <no symbol>");