理解gdb中的这种不稳定行为

and*_*and 5 c debugging gcc gdb

请考虑以下代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

第二次调用Mstrupr无法在linux上正确运行,因为它接收字符串作为文字(而不是字符数组).当在gdb上运行完整的程序时它也会失败,但是当断点被添加到main并且程序通过gdb的下一个命令运行时,第二个字符串被大写并打印.为什么?我相信这不应该,但我的导师坚持认为这是gdb设计的一部分.

pm1*_*100 9

我没有看到它是gdb设计的一部分.这似乎是偶然的副作用; gdb在设置断点时使代码段可写,因此现在覆盖文字的代码可以正常工作

事实上,没有调试器设计者会故意让他们的调试器改变程序的行为; 这让调试变得非常困难