20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Debugg<strong>in</strong>g Programs with gdb<br />

395<br />

#ifdef DEBON<br />

# def<strong>in</strong>e DEBUG(level, fmt, ...) \<br />

if (Debug >= level) \<br />

fpr<strong>in</strong>tf (stderr, fmt, __VA_ARGS__)<br />

#else<br />

# def<strong>in</strong>e DEBUG(level, fmt, ...)<br />

#endif<br />

When compil<strong>in</strong>g a program conta<strong>in</strong><strong>in</strong>g the previous def<strong>in</strong>ition (which you can conveniently<br />

place <strong>in</strong>side a header file and <strong>in</strong>clude <strong>in</strong> your program), you either def<strong>in</strong>e DEBON<br />

or not. If you compile prog.c as follows:<br />

$ gcc prog.c<br />

it compiles <strong>in</strong> the null def<strong>in</strong>ition for DEBUG based on the #else clause shown <strong>in</strong> the previous<br />

preprocessor statements. On the other hand, if you compile your program like this:<br />

$ gcc –D DEBON prog.c<br />

the DEBUG macro that calls fpr<strong>in</strong>tf based on the debug level is compiled <strong>in</strong> with the rest<br />

of your code.<br />

At runtime, if you have compiled <strong>in</strong> the debugg<strong>in</strong>g code, you can select the debug<br />

level. As noted, this can be done with a command-l<strong>in</strong>e option as follows:<br />

$ a.out –d3<br />

Here, the debug level is set to 3. Presumably, you would process this command-l<strong>in</strong>e argument<br />

<strong>in</strong> your program and store the debug level <strong>in</strong> a variable (probably global) called<br />

Debug.And <strong>in</strong> this case, only DEBUG macros that specify a level of 3 or greater cause the<br />

fpr<strong>in</strong>tf calls to be made.<br />

Note that a.out -d0 sets the debugg<strong>in</strong>g level to zero and no debugg<strong>in</strong>g output is<br />

generated even though the debugg<strong>in</strong>g code is still <strong>in</strong> there.<br />

To summarize, you have seen here a two-tiered debugg<strong>in</strong>g scheme: Debugg<strong>in</strong>g code<br />

can be compiled <strong>in</strong> or out of the code, and when compiled <strong>in</strong>, different debugg<strong>in</strong>g levels<br />

can be set to produce vary<strong>in</strong>g amounts of debugg<strong>in</strong>g output.<br />

Debugg<strong>in</strong>g Programs with gdb<br />

gdb is a powerful <strong>in</strong>teractive debugger that is frequently used to debug programs compiled<br />

with GNU’s gcc compiler. It allows you to run your program, stop at a predeterm<strong>in</strong>ed<br />

location, display and/or set variables, and cont<strong>in</strong>ue execution. It allows you to<br />

trace your program’s execution and even execute it one l<strong>in</strong>e at a time. gdb also has a<br />

facility for determ<strong>in</strong><strong>in</strong>g where core dumps occur. A core dump occurs due to some abnormal<br />

event, possibly division by zero or attempts to access past the end of an array.This<br />

results <strong>in</strong> the creation of a file named core that conta<strong>in</strong>s a snapshot of the contents of the<br />

process’s memory at the time it term<strong>in</strong>ated. 1<br />

1.Your system might be configured to disable the automatic creation of this core file, often due to<br />

the large size of these files. Sometimes, this has to do with the maximum file creation size, which<br />

can be changed with the ulimit command.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!