Oracle® Developer Studio 12.5:使用 dbx 调试程序

退出打印视图

更新时间: 2016 年 6 月
 
 

事件处理程序示例

本节提供了设置事件处理程序的一些示例。

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

本示例显示如何在 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; }

在函数内时启用处理程序

以下示例显示如何在函数内时启用处理程序:

<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               # disable default handler
(dbx) help signals | grep FPE  # can’t remember the subcode name
...
(dbx) stop sig fpe FPE_FLTUND
...

有关启用 ieee 处理程序的更多信息,请参见捕获 FPE 信号(仅限 Oracle Solaris)