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.