Sun Studio 12: Debugging a Program With dbx

Changing Variables After Fixing

Changes made to global variables are not undone by the pop command or thefix command. To reassign correct values to global variables manually, use the assign command. (See assign Command.)

The following example shows how a simple bug can be fixed. The application gets a segmentation violation in line 6 when trying to dereference a NULL pointer.

dbx[1] list 1,$
    1    #include <stdio.h>
    3    char *from = “ships”;
    4    void copy(char *to)
    5    {
    6        while ((*to++ = *from++) != ’\0’);
    7        *to = ’\0’;
    8    }
    10    main()
    11    {
    12        char buf[100];
    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 “”
    6        while ((*to++ = *from++) != ’\0’);

Change line 14 to copy to buf instead of 0 and save the file, then do a fix:

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

If the program is continued from here, it still gets a segmentation fault because the zero-pointer is still pushed on the stack. Use the pop command to pop one frame of the stack:

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

If the program is continued from here, it runs, but does not print the correct value because the global variable from has already been incremented by one. The program would print hips and not ships. Use the assign command to restore the global variable and then use the cont command. Now the program prints the correct string:

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