Compiling For TotalView

The first step is to compile your source code, adding your compiler's debug options. Normally, this is just -g. If you are creating optimized code, you'll also need to turn off all optimization flags.

To illustrate how to debug a program using TotalView, we will use the following code:

/********************************* 
/*   simple.c
**********************************/
#include <stdio.h> 
#include <math.h> 
#define JMAX 1000 
#define IMAX 100
    
/*** some global vars **/ 
double b[IMAX];
    
main(argc, argv) char **argv; 
{ 
    /************* command line args ***/ 
    { 
        char command_line_string[80];
        if (argc > 1) 
        { 
            strcpy(command_line_string, argv[1]); 
            printf("arg_2=%s\n", command_line_string); 
        } 
    }
    
    /**** some array operations ***/ 
    { 
        int i, j, jmod; 
        double xi, xj, dx, scale = 100.0;
    
        for (j = 0; j < JMAX; j++) 
        { 
            jmod = (100*j) %JMAX; 
            xj = (double)jmod/(double)JMAX; 
            for (i = 0; i < IMAX; i++) 
            { 
                xi = (double)i/(double)IMAX; 
                dx = xi-xj; 
                b[i] = 2.0/(1.0+exp(scale*dx*dx)); 
            } 
            printf("counter %d\n", j); sleep(1); 
        } 
    }
    exit(1); 
}

Copy this code into a file called simple.c and then compile it using the following command:

    cc -g -lm -o simple simple.c

You need to use the -lm flag because the program calls the exp() function that is contained within the math library.

For more complex programs, you may want to use a makefile and ensure that you compile all source modules being debugged using the -g flag. Additionally, if your program uses fork() and then calls an execv(), you will have to include libdbfork.a in the link step. For more information, consult the TotalView Reference Guide, which is also available under the Help menu.