Skip to content

Commit fbf8189

Browse files
committed
Adding SymFromAddr to get symbol from address on Windows
1 parent 628c223 commit fbf8189

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/vm.c

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,20 +2877,40 @@ static void print_argument(SgVM *vm, SgContFrame *cont,
28772877
# include <dlfcn.h>
28782878
static int print_c_pc(SgVM *vm, SgObject pfmt, SgContFrame *cont)
28792879
{
2880-
if (cont->fp == C_CONT_MARK) {
2881-
Dl_info info;
2882-
/* pc == after function */
2883-
if (dladdr((void *)cont->pc, &info) && info.dli_sname) {
2884-
SgObject pc = SG_INTERN("pc");
2885-
SgObject name = Sg_Utf8sToUtf32s(info.dli_sname, strlen(info.dli_sname));
2886-
Sg_Printf(vm->logPort, UC(";; %p "),
2887-
(uintptr_t)cont + offsetof(SgContFrame, pc));
2888-
Sg_Format(vm->logPort, pfmt, SG_LIST2(pc, name), TRUE);
2889-
return TRUE;
2890-
}
2880+
Dl_info info;
2881+
/* pc == after function */
2882+
if (dladdr((void *)cont->pc, &info) && info.dli_sname) {
2883+
SgObject pc = SG_INTERN("pc");
2884+
SgObject name = Sg_Utf8sToUtf32s(info.dli_sname, strlen(info.dli_sname));
2885+
Sg_Printf(vm->logPort, UC(";; %p "),
2886+
(uintptr_t)cont + offsetof(SgContFrame, pc));
2887+
Sg_Format(vm->logPort, pfmt, SG_LIST2(pc, name), TRUE);
2888+
return TRUE;
28912889
}
28922890
return FALSE;
28932891
}
2892+
#elif _WIN32
2893+
# include <dbghelp.h>
2894+
# pragma comment(lib, "dbghelp.lib")
2895+
static int print_c_pc(SgVM *vm, SgObject pfmt, SgContFrame *cont)
2896+
{
2897+
#define SYM_LEN 256
2898+
char buffer[sizeof(SYMBOL_INFO) + sizeof(char)*SYM_LEN];
2899+
SYMBOL_INFO *sym = (SYMBOL_INFO *)buffer;
2900+
DWORD64 displacement = 0;
2901+
sym->SizeOfStruct = sizeof(SYMBOL_INFO);
2902+
sym->MaxNameLen = SYM_LEN;
2903+
if (SymFromAddr(GetCurrentProcess(), (DWORD64)cont->pc, &displacement, sym)) {
2904+
SgObject pc = SG_INTERN("pc");
2905+
SgObject name = Sg_Utf8sToUtf32s(sym->Name, sym->NameLen);
2906+
Sg_Printf(vm->logPort, UC(";; %p "),
2907+
(uintptr_t)cont + offsetof(SgContFrame, pc));
2908+
Sg_Format(vm->logPort, pfmt, SG_LIST2(pc, name), TRUE);
2909+
return TRUE;
2910+
}
2911+
return FALSE;
2912+
#undef SYM_LEN
2913+
}
28942914
#else
28952915
static int print_c_pc(SgVM *vm, SgObject pfmt, SgContFrame *cont)
28962916
{
@@ -2907,7 +2927,7 @@ static void print_pc(SgVM *vm, SgObject pfmt, SgContFrame *cont)
29072927
(uintptr_t)cont + offsetof(SgContFrame, pc));
29082928
Sg_Format(vm->logPort, pfmt,
29092929
SG_LIST2(pc, Sg_Cons(p->tag, p->handler)), TRUE);
2910-
} else if (!print_c_pc(vm, pfmt, cont)) {
2930+
} else if (cont->fp != C_CONT_MARK || !print_c_pc(vm, pfmt, cont)) {
29112931
Sg_Printf(vm->logPort, UC(";; %p + pc=%#38p +\n"),
29122932
(uintptr_t)cont + offsetof(SgContFrame, pc), cont->pc);
29132933
}
@@ -3172,6 +3192,10 @@ void Sg__InitVM()
31723192
Sg_AddCleanupHandler(show_inst_count, NULL);
31733193
#endif
31743194
sym_continuation = Sg_MakeSymbol(SG_MAKE_STRING("continuation"), FALSE);
3195+
3196+
#ifdef _WIN32
3197+
SymInitialize(GetCurrentProcess(), NULL, TRUE);
3198+
#endif
31753199
}
31763200

31773201
void Sg__PostInitVM()

0 commit comments

Comments
 (0)