Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stacktrace on win32 #10612

Closed
straight-shoota opened this issue Apr 8, 2021 · 2 comments · Fixed by #11461
Closed

Stacktrace on win32 #10612

straight-shoota opened this issue Apr 8, 2021 · 2 comments · Fixed by #11461
Labels
kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:runtime

Comments

@straight-shoota
Copy link
Member

Exception handling on win32 works, but we still don't have backtraces.
I've been experimenting with that, how we can implement that.

The typical tool on Windows seems to be StackWalk from dbghlp.dll. That library file should generally be available on Windows systems, but may be an older version, so it's recommended to ship a recent library with an application if you use it. That complicates things a bit for distributing Windows executables, but I don't think there's a really feasible alternative.
(As I understand it, Go may have their own implementation, but it's not totally clear to me what exactly they're doing.)

Anyway, this is the output I get for the backtrace_sample test program:

0x7ff78b83becf backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b83be9d backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b83be69 backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b83be18 backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b83bde8 backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b83bd60 backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b82962d backtrace_sample.exe: __crystal_main in ???:0
0x7ff78b84b138 backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b84af07 backtrace_sample.exe: __crystal_raise_string in ???:0
0x7ff78b82aec8 backtrace_sample.exe: main in ???:0
0x7ff78b8a029f backtrace_sample.exe: __scrt_common_main_seh in d:\A01\_work\12\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
0x7ff815977033 KERNEL32.DLL: BaseThreadInitThunk in ???:0

The frame addresses don't look completely unreasonable, but they're pretty close together and are resolved to the same symbol. Not sure if there's an issue with the addresses or the symbol lookup. I'd suspect the latter.
But I'm really leaving my turf here 😅 Getting the basic implementation was doable, even without knowing anything about win32 debugging. But I'm not sure how I could figure out how to get correct output. So any help is welcome =)

The code is available at https://github.com/straight-shoota/crystal/tree/feature/win32-callstack
In order to get any symbol info, you need to pass --link-flags="/Z7" to the build command which generates a .pdb containing the debug information.

crystal.exe spec/std/data/backtrace_sample --link-flags="/Z7"
@straight-shoota straight-shoota added kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:runtime labels Apr 8, 2021
@straight-shoota
Copy link
Member Author

I've looked at the binary again using x64dbg.

The only symbols from Crystal listed in backtrace_sample.exe are those that are defined via fun. No def makes it into the symbol list. But those have to be there somehow, otherwise the program wouldn't run.

@HertzDevil
Copy link
Contributor

DbgHelp does not support DWARF, so instead of setting the Dwarf Version flag in each LLVM module, CodeView=1 should be set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants