Debugging a Program With dbx

Examples

Use these examples for setting event handlers.

Set Watchpoint for Store to Array Member

To set a watchpoint on array[99]:


(dbx) stop access w &array[99]
(2) stop access w &array[99], 4
(dbx) run
Running: watch.x2
watchpoint array[99] (0x2ca88[4]) at line 22 in file "watch.c"	
   22	array[i] = i;

Simple Trace

To implement a simple trace:


(dbx) when step { echo at line $lineno; }

Enable Handler While Within the Given Function (in func)

For example:


trace
step -in foo

is equivalent to:


    # create handler in disabled state
    when step -disable { echo Stepped to $line; } 
    t=$newhandlerid    # remember handler id
    when in foo {
     # when entered foo enable the trace
     handler -enable "$t"
     # arrange so that upon returning from foo,
     # the trace is disabled.
     when returns { handler -disable "$t"; };
    }

Determine the Number of Lines Executed in a Program

To see how many lines were executed in a small program:


(dbx) stop step -count infinity	
 # step and stop when count=inf
(2) stop step -count 0/infinity
(dbx) run
...
(dbx) status
(2) stop step -count 133/infinity

The program never stops--the program terminates. 133 is the number of lines executed. This process is very slow though. This technique is more useful with breakpoints on functions that are called many times.

Determine the Number of Instructions Executed by a Source Line

To count how many instructions a line of code executes:


(dbx) ...                        # get
to the line in question
(dbx) stop step -instr -count infinity
(dbx) step ...
(dbx)status
(3) stop step -count 48/infinity # 48
instructions were executed

If the line you are stepping over makes a function call, you end up counting those as well. You can use the next event instead of step to count instructions, excluding called functions.

Enable Breakpoint after Event Occurs

Enable a breakpoint only after another event has fired. Suppose things go bad in function hash, but only after the 1300'th symbol lookup:


(dbx) when in lookup -count 1300 {
	stop in hash
	hash_bpt=$newhandlerid
	when proc_gone -temp { delete $hash_bpt; }
}


Note -

$newhandlerid is referring to the just executed stop in command.


Reset Application Files for replay

If your application processes files that need to be reset during a replay, you can write a handler to do that for you each time you run the program:


(dbx) when sync { sh regen ./database; }
(dbx) run < ./database...	# during which database gets clobbered
(dbx) save
...              # implies a RUN, which implies the SYNC event which
(dbx) restore	   # causes regen to run

Check Program Status

To see quickly where the program is while it's running:


(dbx) ignore sigint
(dbx) when sig sigint { where; cancel; }

Then type ^C to see a stack trace of the program without stopping it.

This is basically what the collector hand sample mode does (and more of course). Use SIGQUIT (^\) to interrupt the program because ^C is now used up.

Catch Floating Point Exceptions

To catch only specific floating-point exceptions, for example, IEEE underflow:


(dbx) ignore FPE 
             # turn off default handler 
(dbx) help signals | grep FPE
  # can't remember the subcode name
...
(dbx) stop sig fpe FPE_FLTUND
...