The Vee
The Vee

Reputation: 11580

How to properly debug a cross-compiled Windows code on linux?

I have a small piece of Windows code, basically one copied from the MSDN tutorial but adapted for C++. I can compile it now using one of the methods:

All I want now is to start the file in a debugger (gdb or an interface to it if possible) and stop at the entry point WinMain. I'm failing badly at this. What I have tried (NB: in the following, hello without extension is, quite unconventially, the Windows executable):

I can't even remember the other combinations I tried. Surely it can't be that hard?

Upvotes: 17

Views: 9711

Answers (3)

Maria Gheorghe
Maria Gheorghe

Reputation: 629

You can run winedbg --gdb --no-start progra.exe. And after this you can use Hopper disassembler and attach to the port you get.

Upvotes: 4

VZ.
VZ.

Reputation: 22753

The simplest way to debug programs running under Wine is to run full-featured gdbserver under it. First, the required packages need to be installed, e.g. under Debian:

    # apt install gdb-mingw-w64 gdb-mingw-w64-target

Then run the program as

    $ wine Z:/usr/share/win64/gdbserver.exe localhost:12345 myprogram.exe

and, finally, from another terminal/screen window:

    $ x86_64-w64-mingw32-gdb myprogram.exe
    (gdb) set solib-search-path ...directories with the DLLs used by the program...
    (gdb) target extended-remote localhost:12345

and then debug normally as usual, i.e. with full access to debug information and working breakpoints and so on.

In particular, running gdbserver works much better than using winedbg --gdb which seems to be quite broken since many years.

Upvotes: 12

The Vee
The Vee

Reputation: 11580

It seems I figured it out. With the compile command line

i686-w64-mingw32-g++ -gstabs hello.cpp -o hello.exe

I can run

winedbg hello.exe

and in its command line,

break WinMain@16
cont

The important option was -gstabs instead of -g and no --gdb for winedbg. I figured out both after reading this mailing list thread, more relevant pieces of information are discussed there.

Note that the bare winedbg is seriously impaired when it comes to name mangling and such, but gdb won't work (at least not out of the box) for the reasons outlined in the link above.

Upvotes: 10

Related Questions