Sun Studio 12:使用 dbx 调试程序

设置事件处理程序示例

以下是设置事件处理程序的一些示例。

为存储到数组成员设置断点

要在 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;

执行简单跟踪

要实现简单跟踪,请键入:


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

在函数内时启用处理程序 (in function)

要在函数内时启用处理程序,请键入:


<dbx> trace step -in foo

它等效于:


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

确定已执行的行数

要了解在小程序中已执行了多少行,请键入:


(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

程序永远不会停止-程序只会终止。执行的行数为 133。此进程速度非常慢。它对多次调用的函数中的断点用处最大。

确定源代码行执行的指令数

要计算一行代码执行了多少个指令,请键入:


(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

如果步过的行进行函数调用,则该函数中的行也计入在内。可以使用 next 事件而非 step 计算指令数(不包括被调用函数)。

事件发生后启用断点

只在另一事件发生后启用断点。例如,如果程序的执行在函数 hash 中开始不正常,但只是发生在第 1300 次符号查找后,可键入:


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

注 –

$newhandlerid 是指刚执行的 stop in 命令。


重放重置应用程序文件

如果应用程序处理需要在 replay 期间重置的文件,可以编写一个处理程序,在每次运行程序时执行该操作:


(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

检查程序状态

要快速查看程序运行时的位置,请键入:


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

然后键入 ^C 在不停止程序的情况下查看程序的栈跟踪。

这基本上是收集器人工示例模式的任务(还有更多)。可使用 SIGQUIT (^\)^C 已用尽的情况下中断程序。

捕获浮点异常

要只捕获特定的浮点异常(例如 IEEE 下溢),请键入:


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