How to get gdb on Linux to find source file for binary cross compiled on windows

I am trying to debug an application that cross-compiles on a Windows host for a Linux target.

Problem: Since the source compilation is in windows, the saved binary file source paths are of the form C:\Users\foo\project\... On target Linux, I put the source files in \home\foo\project\.... By default, gdb cannot find the source file because of a different path.

What I have tried so far:

  • Use the "directory" command in gdb to give the exact path for the .c source file on the target Linux system where the application is being debugged. This works, but unfortunately there are literally hundreds of files, so this solution is unrealistic.

  • Use the set substitute-path C:\\Users\\foo\\project /home/foo/project command set substitute-path C:\\Users\\foo\\project /home/foo/project so that gdb replaces all the prefixes. Note that \\ appears to show substitute-path registering the correct string. This, unfortunately, does not work. I assume the substitute-path command does not handle ms-dos style paths.

  • I tried to separate the debug information into a separate .debug file (see How to generate the gcc debug symbol outside the build target? ), And then use debugedit to change the paths using the debugedit --base-dir=C:\Users\foo --dest-dir=/home/foo project.debug . Unfortunately, this does not work either. debugedit seems to work fine if the existing path is all UNIX / Linux, but doesn't seem to work with ms-dos style tracks.

I looked through stackoverflow and as long as there are related topics, I cannot find anything that helps me. I would really appreciate any suggestions / help. I understand that cross-compiling from Windows is very cool, but at the moment this cannot be avoided.

thanks

+8
linux windows cross-compiling gdb
source share
2 answers

Although this is a pretty old question, I ran into the same problem. I managed to resolve it, but using sed in a binary executable ... (yes, a “bit” hack-ish, but could not find another way). With sed I managed to replace the character paths directly inside the executable, the trick is that the new path length should be the same as the previous one.

 sed -i "s#C:/srcpath#/srcpath/.#g" ./executable 

Make sure that the new path has the same length, otherwise the executable will be inhibited.

+1
source share

I have the same problem too. Your option 1 is not as bad as you think, because you can script to create all the "directory" commands with something like this python code:

 def get_directory_paths(): return_array = list() unix_path = os.path.join('my','unix','path') for root, dirs, files in os.walk(unix_path): for dir in dirs: full_unix_path = os.path.join(root,dir) escaped_unix_path = re.sub("\s", "\\\\ ", full_unix_path) return_array.insert(0, "directory " + escaped_unix_path) return '\n'.join(return_array) 

The downside is that if you have two source files with the same name in different directories, I don't think gcc can choose the right one. This bothers me, but in my specific situation, I think I'm safe.

For option 2 (which, I suspect, will fix the C # 1 smoothing condition), I think the problem is that replacements do not end with a “file separator” according to Linux, so they do not apply:

To avoid unexpected substitution results, the rule only applies if part of the directory name ends in a directory delimiter. For example, the rule / substitution / usr / source in / mnt / cross will be applied to / usr / source / foo -1.0, but not to / usr / sourceware / foo -2.0. And since the substitution applies only at the beginning of the directory name, this rule will not apply to /root/usr/source/baz.c either. "(From https://sourceware.org/gdb/current/onlinedocs/gdb/Source-Path.html#index-set-substitute_002dpath )

I have not tried anything like your # 3, and I also looked at something like the @dragn suggestion, but in my situation the paths are not close to the same length, so there will be a problem.

I think I'm stuck in # 1 and the script, but if anyone has any other suggestions, I need options :-)

0
source share

All Articles