4

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?

Alexander
  • 1,068
  • 6
  • 22
  • You seem to be looking for this: [Using ORIGIN for a dynamic runtime library search path](http://itee.uq.edu.au/~daniel/using_origin/) – devnull May 24 '13 at 11:21
  • @devnull I read a bit and don't think this is related to my problem or I don't get it. I want to change the debug symbols, not finding/loading shared libraries. – Alexander May 24 '13 at 11:55

1 Answers1

4

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.

Alexander
  • 1,068
  • 6
  • 22
  • Very helpful, however this doesn't work quite right for me. It looks like gcc is interpreting any relative path as the destination for `fdebug-prefix-map` relative to the current working dir (effectively the dir containing pom.xml). So when running your example I get `/home/foo/project/blah/theNewPathInDebug` (as opposed to just `theNewPathInDebug`). Does `fdebug-prefix-map` work with relative paths? Googling turns up nothing. I am using gcc 4.4.5. – Eric Jun 20 '13 at 14:03
  • The *newPath* argument is not a relative path in my case. It is just a (non existing) marker string. While debugging (possibly on a different machine) it can be substituted with the actual source path (gdb set substitute-path). Maybe it's treated differently when the marker string is also a valid relativ path and then substituted by gcc with the full path, but that doesn't make sense to me. – Alexander Jun 23 '13 at 11:44
  • It appears to me that gcc is not treating the arguments to fdebug-prefix-map as paths at all. It just seems to be a kind of pattern substitution as I know it from Makefile rules. Maybe that helps. – Alexander Jun 23 '13 at 12:13