Make gcc put relative filenames in debug information
Asked Answered
E

4

23

The project I'm compiling uses CMake, which loves absolute pathnames.

When I compile with debugging information enabled, gcc puts those long names into .debug_str sections, which is bad for debugging. I'd like to have short relative-to-project-root pathnames there instead.

Is there some option to tell gcc to strip some part of pathname before emitting debug data? Or, maybe, there is some tool that could do that on compiled binaries?

I've tried using SET(CMAKE_USE_RELATIVE_PATHS ON) (which seems to be frowned upon by devs) option, but as I'm using out-of-source builds, pathnames are still not in the form I'd want them to be. I.e. they're ./../src/mod_foo/foo.c instead of mod_foo/foo.c.

Ernaldus answered 7/3, 2012 at 18:35 Comment(4)
./../src/mod_foo/foo.c is a relative path...Ferrigno
Yes, but not relative to the project root, though (which is at ./../src)Ernaldus
Your have a problem with CMake, not with gcc. GCC puts into .debug_str exactly that it gets as a command line argument.Obduliaobdurate
I don't really care about the method. I see three theoretically possible ways to do this, either of one is perfectly fine with me: 1) Making CMake change directory to project root and pass gcc relative-to-project-root source filename; 2) Changing this GCC behavior, so it'll mangle filenames before writing; 3) Patching the compiled binary with some tool, to fix filenames. The problem is, I don't know how to do any of the above.Ernaldus
V
6

You can set the RULE_LAUNCH_COMPILE property of a CMake target to have CMake invoke a shell script which transforms the source file path to a project relative path before invoking gcc. Use the CMake function configure_file to generate a shell script which knows about the PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR of your project.

In your outermost CMakeLists.txt add the following code:

configure_file(
    "${PROJECT_SOURCE_DIR}/gcc_debug_fix.sh.in"
    "${PROJECT_BINARY_DIR}/gcc_debug_fix.sh"
    @ONLY)

add_executable (MyExecutable ...)

set_target_properties(MyExecutable PROPERTIES 
    RULE_LAUNCH_COMPILE "${PROJECT_BINARY_DIR}/gcc_debug_fix.sh")

The following template shell script gcc_debug_fix.sh.in needs to go to the root directory of the CMake project:

#!/bin/sh

PROJECT_BINARY_DIR="@PROJECT_BINARY_DIR@"
PROJECT_SOURCE_DIR="@PROJECT_SOURCE_DIR@"

# shell script invoked with the following arguments
# $(CXX) $(CXX_DEFINES) $(CXX_FLAGS) -o OBJECT_FILE -c SOURCE_FILE

# extract parameters
SOURCE_FILE="${@: -1:1}"
OBJECT_FILE="${@: -3:1}"
COMPILER_AND_FLAGS=${@:1:$#-4}

# make source file path relative to project source dir
SOURCE_FILE_RELATIVE="${SOURCE_FILE:${#PROJECT_SOURCE_DIR} + 1}"

# make object file path absolute
OBJECT_FILE_ABSOLUTE="$PROJECT_BINARY_DIR/$OBJECT_FILE"

cd "$PROJECT_SOURCE_DIR"

# invoke compiler
exec $COMPILER_AND_FLAGS -c "${SOURCE_FILE_RELATIVE}" -o "${OBJECT_FILE_ABSOLUTE}"

The shell script uses the information from the variables PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR to transform the path of the source file to a path relative to the project root and the object file's path to an absolute path. Because gcc gets passed a project relative path now, .debug_str should use that path, too.

The following caveats apply:

  • Be sure to set the executable bit of gcc_debug_fix.sh.in.
  • For the script to work CMAKE_USE_RELATIVE_PATHS has to set to OFF again.
  • The script makes assumptions about the location of the file paths on the command line. This may not work if CMake uses a different rule to invoke the compiler. A more robust solution would be to scan the script arguments for the -o and -c flags.
Vella answered 9/3, 2012 at 18:33 Comment(0)
H
34

You can use the -fdebug-prefix-map flag to remap the debugging information paths. For example, to make the paths relative to the build location use: -fdebug-prefix-map=/full/build/path=.

Hyponitrite answered 10/10, 2013 at 8:23 Comment(2)
This should be the accepted answer. It is much less complicated and brittle than sakra's answer. Also of note is the -ffile-prefix-map flag, since -fdebug-prefix-map doesn't affect things such as the __FILE__ macro.Seaweed
-ffile-prefix-map is available since gcc 8 and clang 10. just add a bit of infoCarricarriage
V
6

You can set the RULE_LAUNCH_COMPILE property of a CMake target to have CMake invoke a shell script which transforms the source file path to a project relative path before invoking gcc. Use the CMake function configure_file to generate a shell script which knows about the PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR of your project.

In your outermost CMakeLists.txt add the following code:

configure_file(
    "${PROJECT_SOURCE_DIR}/gcc_debug_fix.sh.in"
    "${PROJECT_BINARY_DIR}/gcc_debug_fix.sh"
    @ONLY)

add_executable (MyExecutable ...)

set_target_properties(MyExecutable PROPERTIES 
    RULE_LAUNCH_COMPILE "${PROJECT_BINARY_DIR}/gcc_debug_fix.sh")

The following template shell script gcc_debug_fix.sh.in needs to go to the root directory of the CMake project:

#!/bin/sh

PROJECT_BINARY_DIR="@PROJECT_BINARY_DIR@"
PROJECT_SOURCE_DIR="@PROJECT_SOURCE_DIR@"

# shell script invoked with the following arguments
# $(CXX) $(CXX_DEFINES) $(CXX_FLAGS) -o OBJECT_FILE -c SOURCE_FILE

# extract parameters
SOURCE_FILE="${@: -1:1}"
OBJECT_FILE="${@: -3:1}"
COMPILER_AND_FLAGS=${@:1:$#-4}

# make source file path relative to project source dir
SOURCE_FILE_RELATIVE="${SOURCE_FILE:${#PROJECT_SOURCE_DIR} + 1}"

# make object file path absolute
OBJECT_FILE_ABSOLUTE="$PROJECT_BINARY_DIR/$OBJECT_FILE"

cd "$PROJECT_SOURCE_DIR"

# invoke compiler
exec $COMPILER_AND_FLAGS -c "${SOURCE_FILE_RELATIVE}" -o "${OBJECT_FILE_ABSOLUTE}"

The shell script uses the information from the variables PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR to transform the path of the source file to a path relative to the project root and the object file's path to an absolute path. Because gcc gets passed a project relative path now, .debug_str should use that path, too.

The following caveats apply:

  • Be sure to set the executable bit of gcc_debug_fix.sh.in.
  • For the script to work CMAKE_USE_RELATIVE_PATHS has to set to OFF again.
  • The script makes assumptions about the location of the file paths on the command line. This may not work if CMake uses a different rule to invoke the compiler. A more robust solution would be to scan the script arguments for the -o and -c flags.
Vella answered 9/3, 2012 at 18:33 Comment(0)
A
1

If I really couldn't fix the make file/tool to do this properly, I would write a wrapper script for gcc that recognises absolute pathnames and converts then to relative ones.

It might look something like this in bash:

#!/bin/bash

out=()
for arg; do
  out=("${out[@]}" $(echo "$arg" | sed 's:/my/absolute/directory/:../:'))
done

exec gcc "${out[@]}"

If your source directory has subdirectories then you'll need to handle those carefully, but the above should work for a flat source directory. I've not tested it though, and I wouldn't be surprised if I've got the quoting wrong, but that'll only be a problem if you have pathnames with spaces in. It also doesn't handle parameters like -I/whatever/include, but you can fix that.

Accord answered 9/3, 2012 at 14:32 Comment(0)
S
0

If your purpose is just for gdb to find the source file, and don't really care about the actual paths, gdb has path substitution.

Create the file .gdbinit in your project's root, with content:

set substitute-path /src/ ./

For context: my project builds in a docker container, and maps the project root on the host, to the container's /src. The above make gdb substitute /src (paths in the debug symbols) to the project root, before trying to find the source files.

Serial answered 13/6 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.