GDB

I am going through The C Programming Language, 2nd Ed and while stuck on logic for problem 1.20 (detab), fired up gdb - if its like perl -d, will be immense help.

Pages that helped: sourceware.org/gdb, FreeBSD Forum

Startup

If your program (lets call it detab) takes input from a pipe, forget it. gdb takes commands from the pipe. Unless STDERR could be redirected to detab, can get another way..

% gdb ./detab

Breakpoints

Per the Sample session, can add them at function calls...

(gdb) break getline
Breakpoint 1 at 0x100000c9c: file 0120-detab.c, line 36.
(gdb) break detab
Breakpoint 2 at 0x100000d60: file 0120-detab.c, line 50.

.. later on here is the second breakpoint being reached

(gdb) s 

Breakpoint 2, detab (line=0x7fff5fbfeb20 "hi there - 3 tabs ahead\t\t\tggg        ggg\n", detabbed=0x7fff5fbfdb80 "8") at 0120-detab.c:50
50      j=0;
(gdb) s
51      i=0;
(gdb) s
52      while((detabbed[j] = line[i]) != EOF) {
(gdb) s
53          if (line[i] == '\t') {
(gdb) p i     
$37 = 0
(gdb) p line[i]
$38 = 104 'h'
(gdb) p  detabbed[i]
$39 = 104 'h'

Print values

(gdb) p line[14]
$27 = 97 'a'

STDIN

Thanks - Free BSD forum

(gdb) run < test.txt
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /Users/gnerd/c/knrc/ch1/detab < test.txt

Breakpoint 1, getline (line=0x7fff5fbfeb20 "") at 0120-detab.c:36
36      for(i=0; i< MAXLINE-1 && (c= getchar()) != EOF && c != '\n'; ++i)

Step through the code

Below the first getline call is completing, the value of i is returned and we can see NR being incremented (NR is very awkward to use here.).

(gdb) s
37          line[i] = c;
(gdb) s
36      for(i=0; i< MAXLINE-1 && (c= getchar()) != EOF && c != '\n'; ++i)
(gdb) s
37          line[i] = c;
(gdb) s
36      for(i=0; i< MAXLINE-1 && (c= getchar()) != EOF && c != '\n'; ++i)
(gdb) s
37          line[i] = c;
(gdb) s
36      for(i=0; i< MAXLINE-1 && (c= getchar()) != EOF && c != '\n'; ++i)
(gdb) s
39      if (c == '\n') {
(gdb) s
40          line[i] = c;
(gdb) s
41          ++i;
(gdb) s
43      line[i] = '\0';
(gdb) p i
$28 = 41
(gdb) s
44      return i;
(gdb) p i
$29 = 41
(gdb) s
main () at 0120-detab.c:24
24          NR++;

NR being incremented

(gdb) p NR
$30 = 0
(gdb) s
25          printf("%d  \n", NR);
(gdb) p NR
$31 = 1
(gdb) p len  
$32 = 41

Restart execution of your program

(gdb) R
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /Users/gnerd/c/knrc/ch1/detab 

Breakpoint 1, getline (line=0x7fff5fbfeb20 "") at 0120-detab.c:36
36      for(i=0; i< MAXLINE-1 && (c= getchar()) != EOF && c != '\n'; ++i)

Perl debugging

Since I have it here, might as well have in one please.. Breakpoint in your code (put in your file before starting debugger)

$DB::single=1
[..]
$DB::single=2
[..]
$DB::single=4

Advance to code breakpoint

c

single step through code

s

next

n

print out a variable

p

Dumper a variable - x

x

Published on in