Why doesn't gdb see when "stdio" changes?

According to the GNU C library, it allowed stdio to be assigned exactly as if they were regular variables (I know this is an extension). I tried the following program:

 #include <stdio.h> int main() { stdout = NULL; printf("Crash and %s\n", "burn"); return 0; } 

When the program starts, it will be segfault as expected, but when I run it in gdb , the stdout value is still not NULL :

 _IO_vfprintf_internal (s=0x0, format=0x400631 "Crash and %s\n", ap=0x7fffffffe210) at vfprintf.c:1297 1297 vfprintf.c: No such file or directory. (gdb) print stdout $1 = (struct _IO_FILE *) 0x7ffff7dd77a0 (gdb) 

Why doesn't gdb tell the correct stdout value?

Examining this, I see that it stores stdout at 0x600940 , looking for struct _IO_FILE* where I find a pointer that will be the same as gdb , like stdio :

 (gdb) print stdout $1 = (struct _IO_FILE *) 0x7ffff7dd77a0 (gdb) print (void*)0x600940 $2 = (void *) 0x600940 (gdb) print (struct _IO_FILE*)0x600940 $3 = (struct _IO_FILE *) 0x600940 (gdb) print *(struct _IO_FILE**)0x600940 $4 = (struct _IO_FILE *) 0x7ffff7dd77a0 (gdb) n 7 puts("Crash and burn"); (gdb) print *(struct _IO_FILE**)0x600940 $5 = (struct _IO_FILE *) 0x0 (gdb) print &stdio No symbol "stdio" in current context. (gdb) print &stdout $6 = (struct _IO_FILE **) 0x7ffff7dd7d90 

It looks like gdb thinks that stdout is at 0x7ffff7dd7d90 , but actually it is at 0x600940 .

I am using GNU gdb (GDB) 7.4.1-debian and gcc version 4.7.2 (Debian 4.7.2-5) (x86-64).

+6
source share
1 answer

I think your gdb is working correctly. Take a look at these lines that you indicated:

 _IO_vfprintf_internal (s=0x0, format=0x400631 "Crash and %s\n", ap=0x7fffffffe210) at vfprintf.c:1297 1297 vfprintf.c: No such file or directory. (gdb) print stdout $1 = (struct _IO_FILE *) 0x7ffff7dd77a0 (gdb) 

The signature _IO_vfprintf_internal() has the target stream s as the first parameter. Since you are at a different level of your stack, and stdout not a global variable, it gets reassigned. But you can see that your appointment was held back because s=0x0 .

0
source

All Articles