Skip to content

Commit

Permalink
Implementing stack trace capture on iree_status_t for Win/Mac. (iree-…
Browse files Browse the repository at this point in the history
…org#15151)

Enabled only when `IREE_STATUS_MODE`>=3, which by default means off in
release builds and on in debug builds. Capturing stacks isn't free so
performance sensitive code should keep them off, though we do split the
symbol resolution/string formatting such that it's not the worst thing
if they are always-on. It's important that we consistently use
`iree_status_from_code` when using status returns for control flow.

Example from Windows:
```
D:\Dev\iree\runtime\src\iree\base\internal\file_io.c:295: NOT_FOUND; failed to open file 'oops.vmfb'; stack:
  0x00007ff6346eeff3 iree-run-module <iree_file_map_contents_readonly_platform+0xa3> (D:\Dev\iree\runtime\src\iree\base\internal\file_io.c:294)
  0x00007ff6346eece7 iree-run-module <iree_file_map_contents_readonly+0x117> (D:\Dev\iree\runtime\src\iree\base\internal\file_io.c:229)
  0x00007ff6346eea13 iree-run-module <iree_file_read_contents+0xa3> (D:\Dev\iree\runtime\src\iree\base\internal\file_io.c:132)
  0x00007ff634622c9d iree-run-module <iree_tooling_load_bytecode_module+0x27d> (D:\Dev\iree\runtime\src\iree\tooling\context_util.c:73)
  0x00007ff634621f97 iree-run-module <iree_tooling_load_modules_from_flags+0x347> (D:\Dev\iree\runtime\src\iree\tooling\context_util.c:155)
  0x00007ff6346259d4 iree-run-module <iree_tooling_create_run_context+0x74> (D:\Dev\iree\runtime\src\iree\tooling\run_module.c:96)
  0x00007ff634624e3e iree-run-module <iree_tooling_run_module_with_data+0x11e> (D:\Dev\iree\runtime\src\iree\tooling\run_module.c:341)
  0x00007ff634624d10 iree-run-module <iree_tooling_run_module_from_flags+0x90> (D:\Dev\iree\runtime\src\iree\tooling\run_module.c:327)
  0x00007ff63461a32e iree-run-module <main+0x10e> (D:\Dev\iree\tools\iree-run-module-main.c:43)
  0x00007ff6347d92e9 iree-run-module <invoke_main+0x39> (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:79)
  0x00007ff6347d918e iree-run-module <__scrt_common_main_seh+0x12e> (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
  0x00007ff6347d904e iree-run-module <__scrt_common_main+0xe> (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331)
  0x00007ff6347d937e iree-run-module <mainCRTStartup+0xe> (D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:17)
  0x00007ffc27b97344 ??? <BaseThreadInitThunk+0x14>
  0x00007ffc29a026b1 ??? <RtlUserThreadStart+0x21>
; loading bytecode module at 'oops.vmfb'; loading modules and dependencies; creating run context
```

Example from MacOS:
```
iree/runtime/src/iree/base/internal/file_io.c:253: NOT_FOUND; failed to open file '/Users/ben/Dev/iree/../iree-tmp/foo.vmfb'; stack:
  0x0000000100093644 iree-run-module <iree_file_map_contents_readonly_platform+0x64>
  0x0000000100093318 iree-run-module <iree_file_map_contents_readonly+0x12c>
  0x0000000100092ff0 iree-run-module <iree_file_read_contents+0x80>
  0x0000000100028a04 iree-run-module <iree_tooling_load_bytecode_module+0x208>
  0x0000000100028518 iree-run-module <iree_tooling_load_modules_from_flags+0x28c>
  0x000000010002ab90 iree-run-module <iree_tooling_create_run_context+0x68>
  0x000000010002a9d8 iree-run-module <iree_tooling_run_module_with_data+0x98>
  0x000000010002a934 iree-run-module <iree_tooling_run_module_from_flags+0x64>
  0x0000000100006228 iree-run-module <main+0xcc>
  0x000000018dd61058 dyld <start+0x8b0>
; loading bytecode module at '/Users/ben/Dev/iree/../iree-tmp/foo.vmfb'; loading modules and dependencies; creating run context
```

Example from Linux:
```
iree/runtime/src/iree/base/internal/file_io.c:199: NOT_FOUND; failed to open file 'oops.vmfb'; stack:
  0x00000000004cdafc iree-run-module <???>
  0x0000000000725319 iree-run-module <???>
  0x0000000000724e7b iree-run-module <???>
  0x00000000004dddc2 iree-run-module <???>
  0x00000000004dc898 iree-run-module <???>
  0x00000000004e7123 iree-run-module <???>
  0x00000000004e6586 iree-run-module <???>
  0x00000000004e5f8a iree-run-module <???>
  0x00000000004c8623 iree-run-module <???>
  0x00007f84b58e1083 libc.so.6 <__libc_start_main+0xf3>
  0x000000000041d9ce iree-run-module <???>
; loading bytecode module at 'oops.vmfb'; loading modules and dependencies; creating run context
```
^ https://www.youtube.com/watch?v=lRAZk0Xb0xM

If we want stack traces with symbols on linux we'll need to have
libdwarf-dev installed and use it to resolve things ala
https://gist.github.com/tuxology/6144170 - I'm happy with having symbols
on Windows and basic ones in debug mode on Mac for now. We've got
everything in place for making linux resolution better if someone cares.
I couldn't figure out how to make the builds happy with all the
GNU_SOURCE shenannigans and just disabled the whole thing for now.

Fixes #55.
  • Loading branch information
benvanik authored Oct 18, 2023
1 parent e918678 commit f84545e
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 34 deletions.
5 changes: 5 additions & 0 deletions runtime/src/iree/base/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ typedef IREE_DEVICE_SIZE_T iree_device_size_t;
#endif // NDEBUG
#endif // !IREE_STATUS_MODE

#if !defined(IREE_STATUS_MAX_STACK_TRACE_FRAMES)
// Maximum number of stack frames captured when enabled.
#define IREE_STATUS_MAX_STACK_TRACE_FRAMES 16
#endif // IREE_STATUS_MAX_STACK_TRACE_FRAMES

//===----------------------------------------------------------------------===//
// Synchronization and threading
//===----------------------------------------------------------------------===//
Expand Down
1 change: 0 additions & 1 deletion runtime/src/iree/base/internal/dynamic_library_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#define IREE_HAVE_DYNAMIC_LIBRARY_PDB_SUPPORT 1
#pragma warning(disable : 4091)
#include <dbghelp.h>

void IREEDbgHelpLock(void);
void IREEDbgHelpUnlock(void);
#endif // TRACY_ENABLE
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/iree/base/internal/file_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ static iree_status_t iree_file_map_contents_readonly_platform(
HANDLE file =
CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY | FILE_FLAG_RANDOM_ACCESS, NULL);
if (!file) {
if (file == INVALID_HANDLE_VALUE) {
return iree_make_status(iree_status_code_from_win32_error(GetLastError()),
"failed to open file '%s'", path);
}
Expand Down
Loading

0 comments on commit f84545e

Please sign in to comment.