Oracle Solaris Studio 12.2: dbx コマンドによるデバッグ

修正後の変数の変更

大域変数への変更は、pop コマンドでも fix コマンドでも取り消されません。大域変数に正しい値を手作業で再び割り当てるには、assign コマンドを使用してください (assign コマンド」を参照)。

次の例は、修正継続機能を使用して簡単なバグを修正する方法を示しています。6 行目で NULL ポインタを逆参照しようとしたときに、セグメンテーションエラーが発生します。


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 行目を 0 ではなく buf をコピー (copy) するように変更し、fix を実行します。


    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’)

ここでプログラムを続行しても、NULL ポインタがスタックをプッシュしているためセグメント例外が返されます。pop コマンドを使用して、スタックフレームを 1 つ上がってください。


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

ここでプログラムを続行すると、プログラムは実行されますが、大域変数 from がすでに増分されているため正しい値が出力されません。assign コマンドを使用しないと、プログラムは ships と表示すべきところを hips と表示します。assign コマンド を使用して大域変数を復元し、次に cont コマンドを使用してください。 プログラムは次のように正しい値を表示します。


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