Chapter:C debug

From Juneday education
Jump to: navigation, search

Background

Introdcution

Different debuggers

printf debugger

Well, this is really not a tool so it can't be a debugger. But this technique is useful for finding bugs so we're listing it here.

Debugger

This is the main tool discussed and taught in this article. We're going to use gdb (The GNU Debugger). gdb is text based, but there are graphical tools if you prefer them. However we're using the text interface.

Memory debugger

Quick example to get us started

Let's look at the following source code:

#include <stdio.h>
#include <string.h>

int main(void)
{
  char *s;
  strcpy(s, "Well, hello there");
  printf("s: '%s'\n", s);

  return 0;
}

Looks kind of ok doesn't it? No, it doesn't. Hopefully you see the problem. We can't copy characters to a char * which does not point to any memory we've allocated. So the program will crash. Ok, given we know what's going to happen we might as well fix it - but since we want to introduce the debugger we will compile and run the program:

gcc -g -c  store.c 
gcc store.o -o store
$ ./store 
Segmentation fault (core dumped)

Bring forth the debugger!

$ gdb ./store
GNU gdb (GDB) Fedora 8.0.1-36.fc27
Copyright (C) 2017 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 "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./store...done.
(gdb)

We've now loaded our program store in to the debugger (gdb). Let's run the program (in the debugger):

(gdb) run
Starting program: /home/hesa/opt/prog-unbook/programming-with-c/debug/seg-fault/store 
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.26-24.fc27.x86_64

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004f7 in main () at store.c:7
7	  strcpy(s, "Well, hello there");
(gdb) run
Starting program: /home/hesa/opt/prog-unbook/programming-with-c/debug/seg-fault/store 
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.26-24.fc27.x86_64

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004f7 in main () at store.c:7
7	  strcpy(s, "Well, hello there");

From this we can see that the program crashes at line 7 in file store.c. Hardly surprising since we've already discussed what was wrong with the code. But wth gdb we can (often) quickly find annoying bugs. So, hopefully you've seen the usefullness of gdb. It's time to move on with a bit more details about the debugger.

The above code and a Makefile can be found here: https://github.com/progund/programming-with-c/tree/master/debug/seg-fault

Links