Oracle® Solaris Studio 12.4:使用 dbx 调试程序

退出打印视图

更新时间: 2015 年 1 月
 
 

修复后更改变量

pop 命令或 fix 命令不会撤消对全局变量所做更改。要手动为全局变量赋适当值,请使用 assign 命令。

下例显示如何修复简单的错误。在尝试解除 NULL 指针引用时,应用程序在第 6 行发生段故障。

dbx[1] list 1,$
    1    #include <stdio.h>
    2    
    3    char *from = “ships”;
    4    void copy(char *to)
    5    {
    6        while ((*to++ = *from++) != ’\0’);
    7        *to = ’\0’;
    8    }
    9    
    10    main()
    11    {
    12        char buf[100];
    13    
    14        copy(0);
    15        printf("%s\n", buf);
    16        return 0;
    17    }
(dbx) run
Running: testfix
(process id 4842)
signal SEGV (no mapping at the fault address) in copy at line 6 in file “testfix.cc”
    6        while ((*to++ = *from++) != ’\0’);

将第 14 行更改为 copybuf(而不是 0)并保存文件,然后进行修复。

    14        copy(buf);      <=== modified line
(dbx) fix
fixing “testfix.cc” .....
pc moved to “testfix.cc”:6
stopped in copy at line 6 in file “testfix.cc”
    6        while ((*to++ = *from++) != ’\0’)

如果程序从这里继续,仍会发生段故障,因为仍然会将零指针推入堆栈中。使用 pop 命令可从调用堆栈中弹出一帧。

(dbx) pop
stopped in main at line 14 in file “testfix.cc”
      14 copy(buf);

如果程序从这里继续,则程序会运行,但不会输出正确的值,因为全局变量 from 已经递增 1。程序将输出 hips 而不是 ships。可使用 assign 命令恢复全局变量,然后使用 cont command 然后,程序会输出正确的字符串:

(dbx) assign from = from-1
(dbx) cont
ships