Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can GDB be used on a cross-compiled win32 application on the target platform

That's a mouthful of a question but basically I want to know if I can use GDB on an application compiled to i686-pc-mingw on a linux cross-compiler. I want to debug the resulting program on the target windows box with source code references etc.

Is it possible and if so what to I need to take into account (ie, same version of mingw files, same binutils, same relative path to source code, etc...)?

One thing that throws me a bit is the version numbers of GCC and GDB don't seem to match up. How do you tell if versions will be compatible?

If these questions sound silly it's just because I haven't used GDB much, it's just something I want to start doing so I'm not trying to guess what broke.

like image 514
SpliFF Avatar asked Sep 15 '10 18:09

SpliFF


2 Answers

Yes, you should be able to use GDB on a Linux host to debug an executable running on i686-pc-mingw.

Terminology: the i686-pc-mingw system is your target, the Linux system is your host.

  • You already have a linux-hosted compiler which targets i686-pc-mingw.
  • You need to build a gdbserver for your target (using your cross-compiler).
  • You also need to build a gdb (using native, not cross-compiler). That gdb needs to be configured for --host=x86-linux and --target=i686-pc-mingw. This is effectively cross-gdb -- it runs on Linux, but debugs mingw executables.

Now run gdbserver :0 foo.exe (on target). That should print a port number on which gdbserver is listening for connections.

Run gdb foo.exe (on host), and connect to the remote target with target remote <windows-target-host-name>:<gdbserver-port-number>, and you should be in business.

P.S.
GCC and GDB are completely separate projects, their versions do not have anything to do with each other. You should be able to build GDB using any relatively recent GCC version.

like image 65
Employed Russian Avatar answered Sep 23 '22 02:09

Employed Russian


OK it turns out this is possible using "native" gdb.

Basically, you download native gdb.exe (32 or 64 bit, to match your executable), and cross compile your .exe "with debug symbols" (for instance, gcc -g source.c create a.exe).

Then copy a.exe to your target, run it like gdb a.exe and you can set breakpoints, inspect variables, etc. If you also want access to the source code while you're debugging it, you can copy your source code to your windows box, then run gdb a.exe from within the directory where your source is, so that it lines up. You'd need/want a verbatim copy of the source, of course, but then you have a native debugger with full source.

If you want to run the debugger "from linux" (one benefit being easier access to the actualized source) while your program runs on your windows box, then see the other answer. I suppose there's also the possibility of using a wine debugger to run it "in linux" though I've never tried that way.

like image 27
rogerdpack Avatar answered Sep 23 '22 02:09

rogerdpack