Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile C program in GCC to enable debug in WinDbg?

I compile such code by GCC (v7.1.0) (command line below).

int func()
{
    return 0x1234;
}

int main()
{
    func();
    return 0;
}

gcc .\001_simpleMain.c -O0 -m64 -g

After compilation I run WinDbg (10.0), open executable (Ctrl+E), program is loading. Breakpoint is hit on start process everthing is ok. After it I want to open source code (Ctrl+O) and try to put breakpoint inside func method.

WinDlg tells me:

*** ERROR: Module load completed but symbols could not be loaded for G:\Examples\Gcc\a.exe

Why it is not working? Should I change compilation param? MY CPU is AMD64

like image 568
Jacek Avatar asked Jun 09 '17 10:06

Jacek


2 Answers

Since you're using Windows with WinDbg you need the proprietary PDB files, which contain the debugging information for debugger tools that come from Microsoft.

GCC will generate debugging information that can be used by the gdb debugger (well known in Linux) for example.

gcc -g:

Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.


If you are using MinGW or Cygwin already you can use gdb from there because it's available in the MinGW/Cygwin environment. If not there are several gdb implemenations for Windows available.

like image 158
Andre Kampling Avatar answered Nov 08 '22 10:11

Andre Kampling


Once you have built debugging files according to @Andre Kampling's instructions, you'll first need to convert them into PDB format. But even then, WinDbg will likely still not find them.

The executable has some data which points to the PDB file. Since you built in on Linux, that will be a Linux path which is not available on your Windows system.

Therefore, you need to set up your symbol path correctly so that WinDbg knows where you put them.

Basically you'll need

0:000> .symfix c:\symbols

for the Microsoft symbols and

0:000> .sympath+ c:\path\to\your\symbols

and then instruct WinDbg to load them again

0:000> .reload /f
0:000> ld*
like image 32
Thomas Weller Avatar answered Nov 08 '22 09:11

Thomas Weller