User Tools

Site Tools


developer:debugging-tutorial

Debugging

Getting GCC & GDB

Windows

After installation the Windows search path must be extended with the path where the binaries of MinGW are installed, for example: "C:\MinGW\bin"
Needed packages:

  • mingw32-base

Linux

  • sudo apt-get install gcc
  • sudo apt-get install gdb


Example

crash.c

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 int main(int argc, char** argv) {
 5   char* x = NULL;
 6   printf("Let it crash with option (%s)...\n", argc > 1 ? argv[1]:"none");
 7   if( x[0] == '1' )
 8     printf("Should have crashed before...\n");
 9   return 0;
10 }

Compile

C:\crash> gcc -g crash.c -o crash.exe

The -g option is for adding debug information.

Debug

Start the debugger

C:\crash> gdb crash.exe

Start the debugger with application option(s)

C:\crash> gdb --args crash.exe docrash

Debugger welcome message

GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\crash/crash.exe...done.
(gdb)

Run the application

(gdb) run

The application crash

Starting program: C:\crash/crash.exe
[New Thread 2800.0xaf4]
Let it crash with option (none)...

Program received signal SIGSEGV, Segmentation fault.
0x004013e6 in main (argc=1, argv=0x3e3ea8) at crash.c:7
7         if( x[0] == '1' )
(gdb)

Show the back trace after the crash

(gdb) bt
#0  0x004013ff in main (argc=1, argv=0x3e24c8) at crash.c:7
(gdb)


The crash is reported at line 7 of crash.c:
0x004013e6 in main (argc=1, argv=0x3e3ea8) at crash.c:7
7 if( x[0] == '1' )
The application did try to read memory at address zero which does not belong to the process and is protected by the Operating System.

This information is used by developers to fix the crash.

Please post the complete backtrace in case of a reproducible crash to team@rocrail.net.


rocrail.exe

:!: Do not use the "-console" parameter in case the rocrail.exe has to be debugged with gdb, because this will disable backtracing.

Example

C:\Rocrail\meinAbeitsBereich>C:\MinGW\bin\gdb.exe --args "C:\Rocrail\Rocrail.exe" -l C:\Rocrail run
developer/debugging-tutorial.txt · Last modified: 2018/11/12 08:56 (external edit)