-
-
Notifications
You must be signed in to change notification settings - Fork 5
Ship debug symbols alongside the binaries #947
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| macro(sourcemeta_debug_symbols_enable) | ||
| message(STATUS "Enabling debug symbols globally") | ||
| add_compile_options(-g) | ||
|
jviotti marked this conversation as resolved.
|
||
| # GCC's `-Wmaybe-uninitialized` produces false positives in | ||
| # template code (`std::tuple`, `std::optional`) when `-g` | ||
| # changes inlining decisions under `-O3 -flto` | ||
| if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
| add_compile_options(-Wno-error=maybe-uninitialized) | ||
| endif() | ||
| endmacro() | ||
|
|
||
| function(sourcemeta_debug_symbols_extract TARGET_NAME) | ||
| cmake_parse_arguments(EXTRACT_DEBUG_SYMBOLS "" "COMPONENT" "" ${ARGN}) | ||
| if(NOT EXTRACT_DEBUG_SYMBOLS_COMPONENT) | ||
| message(FATAL_ERROR "The COMPONENT argument is required") | ||
| endif() | ||
|
|
||
| set(BINARY_INPUT "$<TARGET_FILE:${TARGET_NAME}>") | ||
| get_target_property(BINARY_OUTPUT_NAME "${TARGET_NAME}" OUTPUT_NAME) | ||
| if(NOT BINARY_OUTPUT_NAME) | ||
| set(BINARY_OUTPUT_NAME "${TARGET_NAME}") | ||
| endif() | ||
| get_target_property(BINARY_OUTPUT_DIR "${TARGET_NAME}" RUNTIME_OUTPUT_DIRECTORY) | ||
| if(NOT BINARY_OUTPUT_DIR) | ||
| get_target_property(BINARY_OUTPUT_DIR "${TARGET_NAME}" BINARY_DIR) | ||
| endif() | ||
| get_target_property(BINARY_OUTPUT_SUFFIX "${TARGET_NAME}" SUFFIX) | ||
| if(NOT BINARY_OUTPUT_SUFFIX) | ||
| set(BINARY_OUTPUT_SUFFIX "${CMAKE_EXECUTABLE_SUFFIX}") | ||
| endif() | ||
| set(BINARY_PATH "${BINARY_OUTPUT_DIR}/${BINARY_OUTPUT_NAME}${BINARY_OUTPUT_SUFFIX}") | ||
|
|
||
| if(APPLE) | ||
| message(STATUS "Extracting debug symbols (.dSYM) for: ${TARGET_NAME}") | ||
| # With `-flto=full`, the post-LTO object normally lives in | ||
| # `/tmp/lto.o` and is removed immediately after the link, which | ||
| # makes `dsymutil` silently produce a near-empty bundle. Pin | ||
| # the LTO temp under the binary's build dir so it persists long | ||
| # enough for `dsymutil` to consume it | ||
| target_link_options("${TARGET_NAME}" PRIVATE | ||
| "LINKER:-object_path_lto,${BINARY_INPUT}.lto.o") | ||
|
|
||
| find_program(DSYMUTIL_EXECUTABLE dsymutil REQUIRED) | ||
| add_custom_command(OUTPUT "${BINARY_PATH}.dSYM" | ||
| COMMAND "${DSYMUTIL_EXECUTABLE}" "${BINARY_INPUT}" | ||
| -o "${BINARY_PATH}.dSYM" | ||
| DEPENDS "${TARGET_NAME}" | ||
| VERBATIM) | ||
| add_custom_target("${TARGET_NAME}_debug_symbols" ALL | ||
| DEPENDS "${BINARY_PATH}.dSYM") | ||
|
|
||
| install(DIRECTORY "${BINARY_PATH}.dSYM" | ||
| DESTINATION "${CMAKE_INSTALL_BINDIR}" | ||
| COMPONENT "${EXTRACT_DEBUG_SYMBOLS_COMPONENT}") | ||
| elseif(UNIX) | ||
| message(STATUS "Extracting debug symbols (.debug) for: ${TARGET_NAME}") | ||
| add_custom_command(OUTPUT "${BINARY_PATH}.debug" | ||
| COMMAND "${CMAKE_OBJCOPY}" --only-keep-debug | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents |
||
| "${BINARY_INPUT}" "${BINARY_PATH}.debug" | ||
| COMMAND "${CMAKE_OBJCOPY}" --strip-debug "${BINARY_INPUT}" | ||
| COMMAND "${CMAKE_OBJCOPY}" | ||
| "--add-gnu-debuglink=${BINARY_PATH}.debug" "${BINARY_INPUT}" | ||
| DEPENDS "${TARGET_NAME}" | ||
| VERBATIM) | ||
| add_custom_target("${TARGET_NAME}_debug_symbols" ALL | ||
| DEPENDS "${BINARY_PATH}.debug") | ||
|
|
||
| install(FILES "${BINARY_PATH}.debug" | ||
| DESTINATION "${CMAKE_INSTALL_BINDIR}" | ||
| COMPONENT "${EXTRACT_DEBUG_SYMBOLS_COMPONENT}") | ||
| else() | ||
| message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}") | ||
| endif() | ||
| endfunction() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
| BINARY="$1" | ||
|
|
||
| TMP="$(mktemp -d)" | ||
|
jviotti marked this conversation as resolved.
|
||
| clean() { rm -rf "$TMP"; } | ||
| trap clean EXIT | ||
|
|
||
| if [ "$(uname -s)" = "Darwin" ]; then | ||
| # `_main` is the Mach-O mangled name of `main`. We resolve its | ||
| # address, then ask `atos` to symbolicate. `atos` follows the | ||
| # binary's debug map (which can point at a `.dSYM` bundle via | ||
| # UUID through Spotlight, at the original `.o` files, or both) | ||
| # so the test does not depend on where the symbols ended up. | ||
| # When DWARF is reachable the output ends with `(file:line)`; | ||
| # without DWARF it ends with `+ <offset>` | ||
| ADDRESS="$(nm "$BINARY" | awk '$3 == "_main" {print "0x"$1; exit}')" | ||
| test -n "$ADDRESS" || { echo "no main symbol in $BINARY" >&2; exit 1; } | ||
| atos -o "$BINARY" "$ADDRESS" > "$TMP/output.txt" | ||
| grep -qE '\(.+:[0-9]+\)' "$TMP/output.txt" || { | ||
| echo "no source location resolved:" >&2 | ||
| cat "$TMP/output.txt" >&2 | ||
| exit 1 | ||
| } | ||
| else | ||
| # `nm --line-numbers` annotates each symbol with `<file>:<line>` | ||
| # when source mapping is available. It uses BFD, which follows | ||
| # the binary's `.gnu_debuglink` section to locate a `.debug` | ||
| # sidecar in any standard location, so the test does not depend | ||
| # on where the sidecar ended up. We do not pin to a single symbol | ||
| # because LTO routinely splits or stubs out specific symbols | ||
| # (notably `main`), losing their line mapping while leaving the | ||
| # rest of the binary fully annotated. As long as some symbol | ||
| # resolves, the sidecar is reachable and DWARF is intact | ||
| nm --line-numbers "$BINARY" > "$TMP/output.txt" | ||
| grep -qE ':[1-9][0-9]*$' "$TMP/output.txt" || { | ||
| echo "no symbol resolved to a source location" >&2 | ||
| head -5 "$TMP/output.txt" >&2 | ||
| exit 1 | ||
| } | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
|
|
||
| BINARY="$1" | ||
|
|
||
| TMP="$(mktemp -d)" | ||
| clean() { rm -rf "$TMP"; } | ||
| trap clean EXIT | ||
|
|
||
| if [ "$(uname -s)" = "Darwin" ]; then | ||
| # `_main` is the Mach-O mangled name of `main`. We resolve its | ||
| # address, then ask `atos` to symbolicate. `atos` follows the | ||
| # binary's debug map (which can point at a `.dSYM` bundle via | ||
| # UUID through Spotlight, at the original `.o` files, or both) | ||
| # so the test does not depend on where the symbols ended up. | ||
| # When DWARF is reachable the output ends with `(file:line)`; | ||
| # without DWARF it ends with `+ <offset>` | ||
| ADDRESS="$(nm "$BINARY" | awk '$3 == "_main" {print "0x"$1; exit}')" | ||
| test -n "$ADDRESS" || { echo "no main symbol in $BINARY" >&2; exit 1; } | ||
| atos -o "$BINARY" "$ADDRESS" > "$TMP/output.txt" | ||
| grep -qE '\(.+:[0-9]+\)' "$TMP/output.txt" || { | ||
| echo "no source location resolved:" >&2 | ||
| cat "$TMP/output.txt" >&2 | ||
| exit 1 | ||
| } | ||
| else | ||
| # `nm --line-numbers` annotates each symbol with `<file>:<line>` | ||
| # when source mapping is available. It uses BFD, which follows | ||
| # the binary's `.gnu_debuglink` section to locate a `.debug` | ||
| # sidecar in any standard location, so the test does not depend | ||
| # on where the sidecar ended up. We do not pin to a single symbol | ||
| # because LTO routinely splits or stubs out specific symbols | ||
| # (notably `main`), losing their line mapping while leaving the | ||
| # rest of the binary fully annotated. As long as some symbol | ||
| # resolves, the sidecar is reachable and DWARF is intact | ||
| nm --line-numbers "$BINARY" > "$TMP/output.txt" | ||
| grep -qE ':[1-9][0-9]*$' "$TMP/output.txt" || { | ||
| echo "no symbol resolved to a source location" >&2 | ||
| head -5 "$TMP/output.txt" >&2 | ||
| exit 1 | ||
| } | ||
| fi |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.