Search code examples
gccgdbdebug-symbolsnative-maven-plugin

Force relative debug symbol path (maven-native-plugin)


I am using the native-maven-plugin to compile a shared library on linux. I pass the compiler option -g to enable the debug symbol generation. Here is an excerpt from the POM:

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>native-maven-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <workingDirectory></workingDirectory>
                <compilerStartOptions>
                    <compilerStartOption>-g</compilerStartOption>
                </compilerStartOptions>
                <linkerStartOptions>
                    <linkerStartOption>-shared</linkerStartOption>
                    <linkerStartOption>-g</linkerStartOption>
                </linkerStartOptions>
                <sources>
                    ...
                </sources>
            </configuration>
        </plugin> 

The native-maven-plugin alsways uses absolute paths to the source files when invoking gcc. This results in absolute paths in the debug symbols, too. The output of nm -l libfoo.so listing the debug symbols looks like:

0000797a T GetTickCount /home/myusername/projects/blabla/foo.c:3005

As you can see the source file path is absolute and includes my username and project structure. I don't want that. How can I change the debug symbols to relative path names?


Solution

  • Okay I found out there is a -fdebug-prefix-map=oldPath=newPath option in gcc which does exactly what I want. To compile the file /home/myusername/projects/blabla/foo.c from my question:

    gcc -fdebug-prefix-map=/home/myusername/projects/blabla=theNewPathInDebug -o foo.o foo.c
    gcc -shared -o libfoo.so foo.o
    

    Then the debug symbol paths will look like (nm -l libfoo.so):

    0000797a T GetTickCount theNewPathInDebug/foo.c:3005
    

    You can then use gdb path substitution to set the actual source file location for gdb.

    To get everything working in maven, my pom looks like:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>native-maven-plugin</artifactId>
        <extensions>true</extensions>
            <configuration>
                <workingDirectory></workingDirectory>
                <compilerStartOptions>
                    <compilerStartOption>-g</compilerStartOption>
                    <compilerStartOption>-fdebug-prefix-map=${project.build.directory}/extracted-c=theNewPathInDebug</compilerStartOption>
                </compilerStartOptions>
                <linkerStartOptions>
                    <linkerStartOption>-shared</linkerStartOption>
                    <linkerStartOption>-g</linkerStartOption>
                </linkerStartOptions>
                <sources>
                    <source>
                        <directory>${project.build.directory}/extracted-c</directory>
                        <fileNames>
                            <fileName>foo.c</fileName>
                        </fileNames>
                    </source>
                </sources>
            </configuration>
        </plugin> 
    

    Where extracted-c is the location where the maven-dependency-plugin extracts the C source/header files.