What is GDB?

What is GDB?

GDB stands for GNU project DeBugger which helps you to debug your binary object file created in compilation process.

GDB allows to you see what is happening in your program which really helps much when program crashes, especially when segmentation fault occurs,😅😅

GDB can do four main things:

  1. Start your program, specifying anything that might affect its behavior
  2. Make your program stop on specified conditions through breakpoints
  3. Examine what has happened, when your program has stopped through checking the values of variables etc
  4. Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another
Those programs might be executing on the same machine as GDB (native), on another machine (remote), or on a simulator. GDB can run on most popular UNIX and Microsoft Windows variants, as well as on Mac OS X.

Languages supported by GDB

Ada, Assembly, C, C++, D, Fortran, Go, Objective-C, OpenCL, Modula-2, Pascal, Rust

The most recent version of GDB at this moment is 8.2.1, you can download from this link.

How to install GDB on Debian-based linux distro(e.g. Ubuntu)?

Install pre-built gdb binaries from verified distribution resources

  • sudo apt-get update
  • sudo apt-get install gdb
Download source code of GDB(from given above link), compile it and install
Now you can verify version of installed GDB,
        gdb --version

Example

Here is the small example of segmentation fault:










In above example, ptr is not pointing anywhere and we are trying to deference pointer which results in segmentation fault at run time.

We can generate GDB enabled binary object file by providing -g option to gcc.

When we give -g, gcc adds debugging information(symbols etc) to binary object file. This can be verified through size of binary and providing object file which is not compiled with -g option. See below for more understanding,

File size of seg_fault_gdb is more than seg_fault because of seg_fault_gdb has been compiled with -g option and it has some extra debugging information.

What if you provide seg_fault to gdb ?

GDB can't find debugging information and throws a message as shown below:
Now pass seg_fault_gdb binary file to GDB and it shows the line number(6 in our example) at which segmentation fault occurs.



This way you can debug any binary file, here are mostly used commands for GDB:
  • run or r –> executes the program from start to end
  • break or b –> sets breakpoint on a particular line
  • disable -> disable a breakpoint
  • enable –> enable a disabled breakpoint
  • next or n -> executes next line of code, but don’t dive into functions
  • step –> go to next instruction, diving into the function
  • list or l –> displays the code
  • print or p –> used to display the stored value
  • quit or q –> exits out of gdb
  • clear –> to clear all breakpoints
  • continue –> continue normal execution

Comments

  1. Explain with more examples and it is usefull like learning guys for debuging.

    ReplyDelete

Post a Comment

Popular posts from this blog

MBR partitioning with 'parted' utility

Replace default splash screen in Yocto

Disk Partitioning: MBR vs GPT