CMPSC 311, Introduction to Systems Programming

Program-Checking Tools

More Advanced Reading

Language Features
Language Extensions implemented by GCC
Compiler options
Compiler options, language version, Sun
Compiler options, language version, GCC
Compiler options, warnings, GCC
Compiler-like tools
Runtime information - test coverage per line of code - how often?
With -fprofile-arcs, for each function of your program GCC creates a program flow graph, then finds a spanning tree for the graph.  Only arcs that are not on the spanning tree have to be instrumented: the compiler adds code to count the number of times that these arcs are executed.  When an arc is the only exit or only entrance to a block, the instrumentation code can be added to the block; otherwise, a new basic block must be created to hold the instrumentation code.
Runtime information - test coverage per function - how often? how long?
Post-mortem debugger
Runtime debugger

Using the assert() macro

Example, from <assert.h> on Solaris, indentation added
#ifdef  NDEBUG
#define assert(EX) ((void)0)
#if defined(__STDC__)
#if __STDC_VERSION__ - 0 >= 199901L
#define assert(EX) (void)((EX) || \
(__assert_c99(#EX, __FILE__, __LINE__, __func__), 0))
#define assert(EX) (void)((EX) || \
(__assert(#EX, __FILE__, __LINE__), 0))
#endif /* __STDC_VERSION__ - 0 >= 199901L */
#define assert(EX) (void)((EX) || \
(_assert("EX", __FILE__, __LINE__), 0))
#endif  /* __STDC__ */
#endif  /* NDEBUG */
In the source file file.c,
#include <assert.h>
In a function body, in file.c,
assert(some expression you expect to be true or nonzero);
When you compile the program, and you want to leave the assertion-checking on,
gcc file.c
gcc -UNDEBUG file.c
When you compile the program, and you want to turn the assertion-checking off,
gcc -DNDEBUG file.c

For fine-grain control, you can do this, in file.c,
#undef NDEBUG
#define NDEBUG
#include <assert.h>
// any use of assert() here is disabled
#undef NDEBUG
#include <assert.h>
// any use of assert() here is enabled

Using static assertions, C11 only

Anywhere a declaration is valid,
The constant-expression must evaluate to an integer.  If it is not equal to 0, the declaration has no effect.  Otherwise, the compiler issues a diagnostic message using the string-literal.

Follow-up Exercises
(Solaris) (GNU)

Last revised, 12 Feb. 2013