Sun Studio 12:使用 dbx 调试程序

设置断点

断点是程序中要暂时停止程序的执行并让 dbx 进行控制的位置。在程序内怀疑存在错误之处设置断点。如果程序崩溃,请确定崩溃的发生位置,然后在这部分代码前设置断点。

程序在断点处停止时,便可以检查程序的状态和变量值。使用 dbx 可以设置多种类型的断点(请参见使用 Ctrl+C 停止进程)。

最简单的断点类型就是停止断点。可以设置用于在函数或过程中停止的停止断点。例如,要在调用 main 函数时停止:


(dbx) stop in main
(2) stop in main

有关 stop in 命令的更多信息,请参见在函数中设置 stop 断点stop 命令

也可以设置用于在源代码的特定行处停止的停止断点。例如,要在源文件 t.c 中的第 13 行处停止:


(dbx) stop at t.c:13
(3) stop at “t.c”:13

有关 stop at 命令的更多信息,请参见在源代码行设置 stop 断点stop 命令

可以使用 file 命令设置当前文件并使用 list 命令列出要在其中停止的函数来确定要停止在那里的行。然后使用 stop at 命令在源代码行设置断点:


(dbx) file t.c
(dbx) list main
10    main(int argc, char *argv[])
11    {
12        char *msg = "hello world\n";
13        printit(msg);
14    }
(dbx) stop at 13
(4) stop at “t.c”:13

要使程序在断点处停止后继续执行,请使用 cont 命令(请参见继续执行程序cont 命令)。

要获取所有当前断点的列表,请使用 status 命令:


(dbx) status
(2) stop in main
(3) stop at "t.c":13

现在如果运行程序,程序将在第一个断点处停止:


(dbx) run
...
stopped in main at line 12 in file "t.c"
12        char *msg = "hello world\n";