Latest tweets from eosgarden

FileSystem iOS app 2.0 uploaded to the AppStore… Extended review time… Version 1.0 removed from the AppStore… Thanks Apple… #bastards
eosgarden - 09.05.2012 / 00:15
@JonnyBellman Sure… Just write us an email, with as many details as possible : )
eosgarden - 05.02.2012 / 18:17
Noxeos - Mixing C++ with C/Objective-C: http://t.co/VS5zeASf
eosgarden - 16.01.2012 / 23:01
FileSytem for iPhone is finally compatible with iOS 5 - update or download it now: http://t.co/yXqCFkC9
eosgarden - 11.01.2012 / 22:22
Noxeos - Warning flags for Clang: http://t.co/iZFQWi6f
eosgarden - 10.01.2012 / 20:36
AutoPurge - Optimize your system memory with a single click: http://t.co/9oTKasSS
eosgarden - 10.01.2012 / 20:24
Manual - Unix man pages at your fingertips: http://t.co/81rZTSeC
eosgarden - 10.01.2012 / 20:12
PropEdit 2.1.0: http://t.co/zJsERSpb
eosgarden - 10.01.2012 / 19:48
@dodyrw NodeJS binary is included in WebStart. GUI is ready, and will be available in the next version (should be released in a few days).
eosgarden - 08.01.2012 / 22:03
@dfeyer Don't know the Percona version (yet)… Is it better than MySQL?
eosgarden - 07.01.2012 / 10:59
 
 
 

GDB tutorial

Author: Jean-David Gadina <macmade(at)eosgarden.com>
Copyright (C) Jean-David Gadina.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
 
 
I have to admit I always felt stupid, while building XCode projects, when the GDB window comes out, or when it display a message like: «set a breakpoint in malloc to debug».
So I decided to learn a few things about GDB. This tutorial will explain you some of the basics.

Example program & Compilation

Before using GCC, we need a sample program to work with. We'll also need to add a specific compiler flag when compiling.
Let's begin with a simple C program:
#import <stdlib.h>

void do_stuff( void );

unsigned long x;

int main( void )
{
x = 10;

do_stuff();

return 0;
}

void do_stuff( void )
{
char * s;

s = ( char * )x;

if( s != NULL )
{
s[ 0 ] = 0;
}
}
Name the file 'gdb_test.c', then compile and run the code with the following command:
gcc -Wall -o gdb_test gdb_test.c && ./gdb_test
No surprise, the program will end with a segmentation fault (EXC_BAD_ACCESS - SIGSEGV).
Now compile the same file again, and add the '-g' parameter to the GCC invocation:
gcc -Wall -g -o gdb_test gdb_test.c
That will tell GCC to generate the debug symbols file. It will be called 'gdb_test.dSYM'.
Such a file contains informations about each symbol of the executable (functions, variables, line numbers, etc). Now that we have that file, we are ready to use GDB.

Using GDB

Simply type 'gdb' to enter a new GDB session. We'll the load our executable using the file command:
GNU gdb 6.3.50-20050815 (Apple version gdb-1518) (Thu Jan 27 08:34:47 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".
(gdb) file gdb_test
The executable is now loaded. We can run it with the 'run' command:
(gdb) run
Starting program: /Users/macmade/Desktop/gdb_test
Reading symbols for shared libraries +. done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000000a
0x0000000100000f0f in do_stuff () at gdb_test.c:24
24 s[ 0 ] = 0;
We can see GDB caught the segmentation fault, and stopped the program's execution. It even display the line where the segmentation fault occurs. Very useful!
We can also ask GDB for a backtrace, with the 'bt' command:
(gdb) bt
#0 0x0000000100000f0f in do_stuff () at gdb_test.c:24
#1 0x0000000100000eeb in main () at gdb_test.c:11

Breakpoints

We can also set breakpoints with GDB. A breakpoint can be a function's name, a specific line number, or a condition.
When GDB encounters a breakpoint, it will stop the program execution. The execution can the be continued with the 's' (step) or 'n' (next) commands.
So lets run our program again, and let's set a breakpoint in the do_stuff() function:
(gdb) break do_stuff
Breakpoint 1 at 0x100000ef6: file gdb_test.c, line 20.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /Users/macmade/Desktop/gdb_test

Breakpoint 1, do_stuff () at gdb_test.c:20
20 s = ( char * )x;
(gdb)
GDB will automatically stops the program's execution when we call the do_stuff() function.
Now we can inspect our program.
We can start by asking the value of our 'x' variable:
(gdb) p x
That will print the value of the 'x' variable:
$1 = 10
We can now modify that variable, so it equals '0' (NULL):
(gdb) 'p x=0
Now we've fixed the problem, and we can continue the program's execution, by stepping multiple times:
(gdb) s
Till GDB prints:
Program exited normally.

Comments:

Author: Devarshi
Date: 5 January 2012 / 15:37
Thanks for such a simple and useful tutorial... :-)

I noticed one typo.... in below line 'agg' should be written as 'add' -

Now compile the same file again, and agg the '-g' parameter to the GCC invocation:

Author: Jean-David Gadina
Date: 6 January 2012 / 07:49
You're welcome : )

Thanks for the correction.

Add a comment:

Anti-Spam: