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

Support call stacks on Windows #11461

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/exception/call_stack/stackwalk.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ struct Exception::CallStack
def self.unwind
load_debug_info

machine_type = {% if flag?(:x86_64) %}
LibC::IMAGE_FILE_MACHINE_AMD64
{% elsif flag?(:i386) %}
# TODO: use WOW64_CONTEXT in place of CONTEXT
{% raise "x86 not supported" %}
{% else %}
{% raise "architecture not supported" %}
{% end %}

# TODO: use stack if possible (must be 16-byte aligned)
context = Pointer(LibC::CONTEXT).malloc(1)
context.value.contextFlags = LibC::CONTEXT_FULL
Expand All @@ -42,19 +51,9 @@ struct Exception::CallStack
stack_frame.addrFrame.mode = LibC::ADDRESS_MODE::AddrModeFlat
stack_frame.addrStack.mode = LibC::ADDRESS_MODE::AddrModeFlat

machine_type = LibC::IMAGE_FILE_MACHINE_AMD64
{% if flag?(:x86_64) %}
stack_frame.addrPC.offset = context.value.rip
stack_frame.addrFrame.offset = context.value.rbp
stack_frame.addrStack.offset = context.value.rsp
{% elsif flag?(:i386) %}
machine_type = LibC::IMAGE_FILE_MACHINE_I386
stack_frame.addrPC.offset = context.value.eip
stack_frame.addrFrame.offset = context.value.ebp
stack_frame.addrStack.offset = context.value.esp
{% else %}
{% raise "architecture not supported" %}
{% end %}
stack_frame.addrPC.offset = context.value.rip
stack_frame.addrFrame.offset = context.value.rbp
stack_frame.addrStack.offset = context.value.rsp

stack = [] of Void*

Expand Down Expand Up @@ -97,7 +96,8 @@ struct Exception::CallStack

if LibC.SymGetModuleInfoW64(LibC.GetCurrentProcess, pc, module_info) != 0
mod_displacement = pc - LibC.SymGetModuleBase64(LibC.GetCurrentProcess, pc)
file_name = "#{String.from_utf16(module_info.value.loadedImageName.to_unsafe)[0]}+0x#{mod_displacement.to_s(16)}"
image_name = String.from_utf16(module_info.value.loadedImageName.to_unsafe)[0]
file_name = "#{image_name} +#{mod_displacement}"
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
else
file_name = "??"
end
Expand Down
2 changes: 0 additions & 2 deletions src/lib_c/x86_64-windows-msvc/c/dbghelp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ lib LibC
kdHelp : KDHELP64
end

IMAGE_FILE_MACHINE_I386 = DWORD.new!(0x014C)
IMAGE_FILE_MACHINE_IA64 = DWORD.new!(0x0200)
IMAGE_FILE_MACHINE_AMD64 = DWORD.new!(0x8664)

alias PREAD_PROCESS_MEMORY_ROUTINE64 = HANDLE, DWORD64, Void*, DWORD, DWORD* -> BOOL
Expand Down