gdb意外的行为:嵌套if [英] gdb unexpected behavior: in nested if
问题描述
我使用的是
p12user @ ubuntu:〜$ uname -a
Linux ubuntu 2.6.32-40 -generic#87 -Ubuntu SMP Tue Mar 6 00:56:56 UTC 2012 x86_64 GNU / Linux
p12user @ ubuntu:〜$ gdb -v
GNU gdb(GDB)7.1-ubuntu
版权所有(C)2010自由软件基金会,Inc.
许可证GPLv3 +:GNU GPL版本3或更高版本< http://gnu.org/licenses/gpl.html>
这是免费软件:您可以自由更改和重新分配它。
在法律允许的范围内,不存在任何担保。有关详细信息,请键入显示复制
和显示保修。
这个GDB被配置为x86_64-linux-gnu。
有关错误报告的说明,请参阅:
< http://www.gnu.org/software/gdb/bugs/> ;.
p12user @ ubuntu:〜/ programming $ gcc --version
gcc(Ubuntu 4.4.3-4ubuntu5.1)4.4.3
版权所有(C)2009 Free Software Foundation ,Inc.
这是免费软件;请参阅复制条件的来源。没有任何b $ b保修;甚至不适用于适销性或针对特定用途的适用性。
在gdb中编译为 gcc -g program .c
:
#include< stdio.h>
int main()
{
int condition1 = 1;
if(condition1)
{
if(!condition1)
{
printf(The control should not here.\\\
);
}
}
else
{
printf(in else \\\
);
}
返回0;
}
的行为是:
断点1,main()在program.c:4
4 int condition1 = 1;
(gdb)n
5 if(condition1)
(gdb)n
7 if(!condition1)
(gdb)n
9 printf( 控制权不应该到这里来。);
(gdb)n
16 return 0;
(gdb)n
17}
(gdb)
在线9的行为在gdb中是意想不到的。但是,print语句仅在gdb中显示,但未执行。如果我把一个匹配的ELSE(带有一些打印语句)和inner-IF放在一起,那么这种情况不会发生,如果匹配的ELSE for outer-IF被删除,那么这种情况就不会发生。我在这里丢失了一些微不足道的东西吗? 解决方案
确保你没有因为你显示的程序似乎是可以优化的,这可能会导致源代码语句不能生成可执行文件。
在GCC中,您可以使用:
gcc -O0 ...
或
g ++ -O0 ...
为此。
I am using
p12user@ubuntu:~$ uname -a
Linux ubuntu 2.6.32-40-generic #87-Ubuntu SMP Tue Mar 6 00:56:56 UTC 2012 x86_64 GNU/Linux
p12user@ubuntu:~$ gdb -v
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
p12user@ubuntu:~/programming$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
On debugging this program in gdb, which is compiled as gcc -g program.c
:
#include <stdio.h>
int main()
{
int condition1 = 1;
if(condition1)
{
if(!condition1)
{
printf("The control shouldn't come here.\n");
}
}
else
{
printf("in else\n");
}
return 0;
}
the behaviour is:
Breakpoint 1, main () at program.c:4
4 int condition1 = 1;
(gdb) n
5 if(condition1)
(gdb) n
7 if(!condition1)
(gdb) n
9 printf("The control shouldn't come here.\n");
(gdb) n
16 return 0;
(gdb) n
17 }
(gdb)
The behaviour at LINE 9 is unexpected in gdb. However, the print statement is only shown in gdb but not executed. And if I put a matching ELSE (with some print statement) with inner-IF then this doesn't happen, also if matching ELSE for outer-IF is removed then this doesn't happen. Am I missing something trivial here?
Make sure you're building your program without any kinds of optimization, as the program you've shown seems to be trivially optimizable which can lead to source code statements not making into the executable.
In GCC you'd use:
gcc -O0 ...
or
g++ -O0 ...
for this.
这篇关于gdb意外的行为:嵌套if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!