Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding this erratic behavior in gdb

Tags:

c

gcc

debugging

gdb

Consider the following code:

#include <stdio.h>
#include <ctype.h>

char* Mstrupr(char* szCad); 

int main()
{
    char szCadena[] = "This string should print well.";
    printf("%s\n", Mstrupr(szCadena));
    printf("%s\n", Mstrupr("This string should fail."));
    return 0;
}

char* Mstrupr(char* szCad) 
{
    int i;
    for (i=0; szCad[i]; i++) 
        szCad[i] = toupper(szCad[i]);
    return szCad;
}

The second call to Mstrupr fails to run correctly on linux as its receiving the string as a literal (and not as a char array). When the complete program is run on gdb it fails as well, but when a breakpoint is added to main and the program is run via gdb's next command, the second string is capitalized and printed. Why? I believe this should not be, but my instructor insists that it's part of gdb's design.

like image 460
andandandand Avatar asked Sep 27 '10 17:09

andandandand


People also ask

Which command is used in GDB to stop execution?

To stop your program while it is running, type "(ctrl) + c" (hold down the ctrl key and press c). gdb will stop your program at whatever line it has just executed. From here you can examine variables and move through your program. To specify other places where gdb should stop, see the section on breakpoints below.

What does breakpoint do in GDB?

A breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops.

How do I see breakpoints in GDB?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.

What is watchpoint in GDB?

You can use a watchpoint to stop execution whenever the value of an expression changes, without having to predict a particular place where this may happen. (This is sometimes called a data breakpoint.)


1 Answers

I dont see that its part of gdb's design. It seems like an accidental side effect; gdb made the code segment writable when it set the breakpoint, so your code that overwrites literals there now works

In fact no debugger designer would deliberately make their debugger change the behavior of a program; that makes debugging really hard

like image 171
pm100 Avatar answered Oct 16 '22 17:10

pm100