Solaris C++ compiler: CC -g Prog.C |
The -g flag will cause the compiler output additional information to help the debugger gdb or dbx to find the program location in the program SOURCE file
The information inserted into the program is compiler/debugger dependend
You must use the vendor X's debugger when you compile your program with vendor X's compiler...
float A[10] = {1, 2, 3, 4, 5, 6, 7}; // Array (vector) float sum = 0.1234; float f(float x) { float i; i = x*x; return(i); } float g(float x) { return (1/(x*f(x)) ); } int main(int argc, char *argv[]) { int i; for (i = 1; i <= MAX; i = i + 1) { A[i-1] = g(i); } sum = 0.0; for (i = 0; i < MAX; i = i + 1) { sum = sum + A[i]; } cout << "sum = " << sum << "\n\n"; } |
Compile with: CC -g -o debug debug.C
dbx Program-Name |
You will see the debugger prompt (dbx)
Now you can enter debugger commands
(dbx) help |
(dbx) help command |
(dbx) help command summary Use `commands' to see a command summary consisting of one-line descriptions of each dbx command. Execution and Tracing cancel catch clear cont delete fix fixed handler ignore intercept next pop replay rerun restore run runargs save status step stop trace unintercept when whocatches ..... |
(dbx 1) list 30 for (i = 1; i <= MAX; i = i + 1) <----- Stop location 31 { 32 A[i-1] = g(i); 33 } 34 35 sum = 0.0; 36 for (i = 0; i < MAX; i = i + 1) 37 { 38 sum = sum + A[i]; 39 } |
run [program-arguments] |
(dbx 2) run Running: debug (process id 1173) Reading libc_psr.so.1 sum = 1.20201 execution completed, exit code is 0 |
|
|
 
 
|
(dbx) stop in main (dbx) run (dbx 6) print i, i+1 i = 0 i+1 = 1 |
(dbx) stop in main (dbx) run (dbx) dump argv = 0xffbfedc4 argc = 1 i = 0 (argv, argc and i are LOCAL variables in function "main") |
float A[10] = {1, 2, 3, 4, 5, 6, 7}; // Array (vector) float sum = 0.1234; float f(float x) { float i; i = x*x; return(i); } float g(float x) { return (1/(x*f(x)) ); } int main(int argc, char *argv[]) { int i; for (i = 1; i <= MAX; i = i + 1) { A[i-1] = g(i); } sum = 0.0; for (i = 0; i < MAX; i = i + 1) { sum = sum + A[i]; } cout << "sum = " << sum << "\n\n"; } |
(dbx) stop in main (dbx) run (dbx) whereis i variable: `debug`debug.C`f(float)`i variable: `debug`debug.C`main`i |
You can monitor the changes of these variables easily to find the cause of error
(dbx) stop in main (dbx) run (dbx) display i, A[0..5] (dbx) run stopped in main at line 30 in file "debug.C" 30 for (i = 1; i <= MAX; i = i + 1) i = 0 A[0..5] = [0] = 1.0 [1] = 2.0 [2] = 3.0 [3] = 4.0 [4] = 5.0 [5] = 6.0 (dbx) next <-- do this repeatedly and see changes in A[] .... i = 2 A[0..5] = [0] = 1.0 [1] = 2.0 [2] = 3.0 [3] = 4.0 [4] = 5.0 .... i = 3 [0] = 1.0 [1] = 0.125 [2] = 3.0 [3] = 4.0 [4] = 5.0 [5] = 6.0 |
|
(dbx) display (1) i (2) A[0..5] |
 
 
|
These commands allows you to execute one statement or one function at a time to find errors
|
stop in FUNCTIONNAME  
Example: stop in main
|
make program stop when it reaches the start of the function FUNCTIONNAME |
stop returns   | stops the program when the current (stopped) function has returned. |
stop returns FUNCTION  
Example: stop returns f
|
stops the program when the function FUNCTION has returned. |
stop at LINENUMBER  
Example: stop at 20
|
sets a breakpoint at the statement on line LINENUMBER of the current (stopped) file (the current file is the file that contains the statement where the program has stopped) |
stop at FILE:LINE  
Example: stop at debug.C:20 |
sets a breakpoint at the statement on line LINENUMBER of the file FILENAME |
stop change VARIABLE  
Example: stop change A[9] |
stops the program when the value of variable VARIABLE is changed |
stop if CONDITION  
Example: stop if i >= 90
|
stops the program whenever the CONDITION is satisfied |
You may want to delete or disable some breakpoints
status   |
list all breakpoints and
the associated number of each break point.
NOTE: the current stopped break point location is marked with an asterix (*) |
delete N   | delete the breakpoint associated with number N |
delete all   | delete all breakpoints... |
handler -disable N   |
Disable breakpoint N
NOTE: A disabled break point is marked as [N] |
handler -enable N   |
Enable breakpoint N
NOTE: An enabled break point is marked as (N) |
Debugging HINT: do not delete breakpoints, just disable them; you may never know if you need them again...
 
 
run [program-arguments] |
always run the program run from the beginning
cont   |
Continue the execution from the current break point
Program may stop again at the next breakpoint.... |
next   |
execute the next source line and stop
If next statement contains one or more function calls, the entire function call will be executed When the program stops, it will print all variables given in the display commands NOTE: the next command will NOT step into a function |
next NUMBER   | execute the next NUMBER source lines and stop |
step   |
execute the next statement line and stop
If next statement contains one or more function calls, the first function is "stepped into" and stops. When the program stops, it will print all variables given in the display commands) NOTE: The step command will STEP INTO a function call |
step NUMBER   | execute the next NUMBER statement lines and stop |
step up   | finish the current function and stop |
To quote George Bernard Shaw:
The reasonable man adapts himself to the world; the
unreasonable one persists in trying to adapt the world
to himself. Therefore, all progress depends on the unreasonable man.
                       
                       
George Bernard Shaw
|
dalias NAME DEFINITION |
Define
NAME
to be an alias for
DEFINITION.
DEFINITION may contain white space. A semicolon or newline terminates the definition. |
dalias NAME | List the definition for the name NAME |
dalias | List the definitions for all aliases |
If you defined:
then DBX will use KShell style alias syntax
(dbx) help .dbxrc > sample-dbxrc |
Once you know what a command does, it very easy to learn a different debugger.
It allows the user to edit, compile and debug programs through a GUI (graphic user interface).
However, it is extremely heavy weight (i.e., slooooow).
You DO NOT have to exit DBX. When you run the program from DBX, it will reload the new version.
You may need to change some break points (especially the break points that uses line numbers)