Go to main content
Oracle® Developer Studio 12.5: Debugging a Program with dbx

Exit Print View

Updated: June 2016

Event Handler Examples

This section provides some examples of setting event handlers.

Setting a Breakpoint for Store to an Array Member

This example shows how to set a data change breakpoint 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;

Implementing a Simple Trace

This example shows how to implement a simple trace:

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

Enabling a Handler While Within a Function

The following example shows how to enable a handler while within a function:

<dbx> trace step -in foo

This command is equivalent to the following:

    # 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"; };

Determining the Number of Lines Executed

This example shows how to see how many lines have been executed in a small program, type:

(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, and then the program terminates. The number of lines executed is 133. This process is very slow. It is most useful with breakpoints on functions that are called many times.

Determining the Number of Instructions Executed by a Source Line

This example shows how 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, the lines in the function are counted as well. You can use the next event instead of step to count instructions, excluding called functions.

Enabling a Breakpoint After an Event Occurs

Enable a breakpoint only after another event has occurred. For example, you would use the following breakpoint if your program begins to execute incorrectly in function hash, but only after the 1300th symbol lookup.

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

Note -  $newhandlerid is referring to the just-executed stop incommand.

Resetting Application Files for replay

In this example, if your application processes files that need to be reset during a replay, you can write a handler to do that 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

Checking Program Status

This example shows how to see quickly where the program is while it is running, type:

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

You would then issue ^C to see a stack trace of the program without stopping it.

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

Catch Floating-Point Exceptions

The following example shows how to catch only specific floating-point exceptions, for example, IEEE underflow:

(dbx) ignore FPE               # disable default handler
(dbx) help signals | grep FPE  # can’t remember the subcode name
(dbx) stop sig fpe FPE_FLTUND

For more information about enabling ieee handlers, see Trapping the FPE Signal (Oracle Solaris Only).