Oracle® Solaris Studio 12.4: Debugging a Program With dbx

Exit Print View

Updated: January 2015
 
 

Changing Variables After Fixing

Changes made to global variables are not undone by the pop command or the fix command. To reassign correct values to global variables manually, use the 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>
    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’);

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 “testfix.cc” .....
pc moved to “testfix.cc”:6
stopped in copy at line 6 in file “testfix.cc”
    6        while ((*to++ = *from++) != ’\0’)

If the program is continued from here, it will still get 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 “testfix.cc”
      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. The program then prints the correct string:

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