Programming in C

(Barry) #1
Debugging Programs with gdb 401

Single Stepping


Another useful command for controlling program execution is the stepcommand,
which can be abbreviated as s.This command single steps your program, meaning that
one line of C code in your program is executed for each stepcommand you enter. If
you follow the stepcommand with a number, then that many lines are executed. Note
that a line might contain several C statements; however,gdbis line oriented, and exe-
cutes all statements on a line as a single step. If a statement spans several lines, single step-
ping the first line of the statement causes all the lines of the statement to be executed.
You can single step your program at any time that a continueis appropriate (after a sig-
nal or breakpoint).
If the statement contains a function call and you step,gdbtakes you into the func-
tion (provided it’s not a system library function; these are typically not entered). If you
use the nextcommand instead of step,gdbmakes the function call and does not step
you into it.
Tr y some of gdb’s features on Program 18.5, which otherwise serves no useful pur-
pose.


Program 18.5 Working with gdb


#include <stdio.h>
#include <stdlib.h>


struct date {
int month;
int day;
int year;
};


struct date foo (struct date x)
{
++x.day;


return x;
}


int main (void)
{
struct date today = {10, 11, 2004};
int array[5] = {1, 2, 3, 4, 5};
struct date newdate, foo ();
char
string = "test string";
int i = 3;


newdate = (struct date *) malloc (sizeof (struct date));
newdate->month = 11;
Free download pdf