| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* backtrace.d - C backtraces |
| 6 |
* |
| 7 |
* Copyright (c) 2010 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <ecl/ecl.h> |
| 16 |
|
| 17 |
#if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS) |
| 18 |
# include <execinfo.h> |
| 19 |
# define ECL_UNIX_BACKTRACE |
| 20 |
#endif |
| 21 |
|
| 22 |
#if defined(ECL_WINDOWS_BACKTRACE) |
| 23 |
# include <windows.h> |
| 24 |
# include <DbgHelp.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
/* Max number of frames dumped by _ecl_dump_c_backtrace */ |
| 28 |
#define MAX_BACKTRACE_SIZE 128 |
| 29 |
/* Max length of symbols printed */ |
| 30 |
#define MAX_SYMBOL_LENGTH 256 |
| 31 |
|
| 32 |
void |
| 33 |
_ecl_dump_c_backtrace() |
| 34 |
{ |
| 35 |
#if defined(ECL_UNIX_BACKTRACE) || defined(ECL_WINDOWS_BACKTRACE) |
| 36 |
{ |
| 37 |
void **pointers = malloc(sizeof(void*) * MAX_BACKTRACE_SIZE); |
| 38 |
# if defined(ECL_UNIX_BACKTRACE) |
| 39 |
int nframes = backtrace(pointers, MAX_BACKTRACE_SIZE); |
| 40 |
char **names = backtrace_symbols(pointers, nframes); |
| 41 |
# elif defined(ECL_WINDOWS_BACKTRACE) |
| 42 |
HANDLE process = GetCurrentProcess(); |
| 43 |
if (!SymInitialize(process, NULL, TRUE)) { |
| 44 |
return; |
| 45 |
} |
| 46 |
int nframes = CaptureStackBackTrace(0, MAX_BACKTRACE_SIZE, pointers, NULL); |
| 47 |
char buffer[sizeof(SYMBOL_INFO) + MAX_SYMBOL_LENGTH * sizeof(TCHAR)]; |
| 48 |
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer; |
| 49 |
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 50 |
pSymbol->MaxNameLen = MAX_SYMBOL_LENGTH; |
| 51 |
# endif |
| 52 |
int i; |
| 53 |
fprintf(stderr, "\n;;; ECL C Backtrace\n"); |
| 54 |
for (i = 0; i < nframes; i++) { |
| 55 |
# if defined(ECL_UNIX_BACKTRACE) |
| 56 |
fprintf(stderr, ";;; %s\n", names[i]); |
| 57 |
# elif defined(ECL_WINDOWS_BACKTRACE) |
| 58 |
DWORD64 displacement; |
| 59 |
if (SymFromAddr(process, (DWORD64) pointers[i], &displacement, pSymbol)) { |
| 60 |
fprintf(stderr, ";;; (%s+0x%llx) [0x%p]\n", pSymbol->Name, displacement, pointers[i]); |
| 61 |
} else { |
| 62 |
fprintf(stderr, ";;; (unknown) [0x%p]\n", pointers[i]); |
| 63 |
} |
| 64 |
# endif |
| 65 |
} |
| 66 |
fflush(stderr); |
| 67 |
free(pointers); |
| 68 |
# if defined(ECL_UNIX_BACKTRACE) |
| 69 |
free(names); |
| 70 |
# elif defined(ECL_WINDOWS_BACKTRACE) |
| 71 |
SymCleanup(process); |
| 72 |
# endif |
| 73 |
} |
| 74 |
#endif /* defined(ECL_UNIX_BACKTRACE) || defined(ECL_WINDOWS_BACKTRACE) */ |
| 75 |
} |
| 76 |
|
| 77 |
cl_object |
| 78 |
si_dump_c_backtrace(cl_object size) |
| 79 |
{ |
| 80 |
cl_env_ptr the_env = ecl_process_env(); |
| 81 |
#if defined(ECL_UNIX_BACKTRACE) || defined(ECL_WINDOWS_BACKTRACE) |
| 82 |
{ |
| 83 |
cl_index nsize = ecl_to_unsigned_integer(size); |
| 84 |
void **pointers = malloc(sizeof(void*) * nsize); |
| 85 |
# if defined(ECL_UNIX_BACKTRACE) |
| 86 |
int nframes = backtrace(pointers, nsize); |
| 87 |
char **names = backtrace_symbols(pointers, nframes); |
| 88 |
# elif defined(ECL_WINDOWS_BACKTRACE) |
| 89 |
HANDLE process = GetCurrentProcess(); |
| 90 |
if (!SymInitialize(process, NULL, TRUE)) { |
| 91 |
return; |
| 92 |
} |
| 93 |
int nframes = CaptureStackBackTrace(0, nsize, pointers, NULL); |
| 94 |
char buffer[sizeof(SYMBOL_INFO) + MAX_SYMBOL_LENGTH * sizeof(TCHAR)]; |
| 95 |
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer; |
| 96 |
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 97 |
pSymbol->MaxNameLen = MAX_SYMBOL_LENGTH; |
| 98 |
# endif |
| 99 |
int i; |
| 100 |
cl_format(2, ECL_T, @"~&C Backtrace:~%"); |
| 101 |
for (i = 0; i < nframes; i++) { |
| 102 |
# if defined(ECL_UNIX_BACKTRACE) |
| 103 |
cl_format(3, ECL_T, @" > ~a~%", |
| 104 |
ecl_make_constant_base_string(names[i],-1)); |
| 105 |
# elif defined(ECL_WINDOWS_BACKTRACE) |
| 106 |
DWORD64 displacement; |
| 107 |
if (SymFromAddr(process, (DWORD64) pointers[i], &displacement, pSymbol)) { |
| 108 |
cl_format(5, ECL_T, @" > (~a+0x~x) [0x~x]~%", |
| 109 |
ecl_make_constant_base_string(pSymbol->Name,-1), |
| 110 |
ecl_make_unsigned_integer(displacement), |
| 111 |
ecl_make_unsigned_integer((cl_index)pointers[i])); |
| 112 |
} else { |
| 113 |
cl_format(3, ECL_T, @" > (unknown) [0x~x]~%", |
| 114 |
ecl_make_unsigned_integer((cl_index)pointers[i])); |
| 115 |
} |
| 116 |
# endif |
| 117 |
} |
| 118 |
free(pointers); |
| 119 |
# if defined(ECL_UNIX_BACKTRACE) |
| 120 |
free(names); |
| 121 |
# elif defined(ECL_WINDOWS_BACKTRACE) |
| 122 |
SymCleanup(process); |
| 123 |
# endif |
| 124 |
} |
| 125 |
ecl_return1(the_env, ECL_T); |
| 126 |
#else |
| 127 |
ecl_return1(the_env, ECL_NIL); |
| 128 |
#endif /* defined(ECL_UNIX_BACKTRACE) || defined(ECL_WINDOWS_BACKTRACE) */ |
| 129 |
} |