gdb+osx: redirecting stdout may cause printf to have a 2**14 bytes buffer?
I attached to a program with gdb in OSX and I want to use CFShow
in the gdb console etc. However, nothing shows up. printf
shows nothing as well:
(gdb) call (int) printf("Hello\n")
$10 = 6
(gdb) call (int) printf("Hello World!\n")
$11 = 13开发者_开发问答
Apple suggests the following tip for when attaching with gdb, to make the output appear in the gdb console:
(gdb) call (void) close(1)
(gdb) call (void) close(2)
(gdb) shell tty
/dev/ttyp1
(gdb) call (int) open("/dev/ttyp1", 2, 0)
$1 = 1
(gdb) call (int) open("/dev/ttyp1", 2, 0)
$2 = 2
In xcode's gdb console tty
gives "not a tty
", so I tried it in gdb in a terminal. There tty
does work but after redirecting stdout there's still no output. Also no output if I direct stdout to a file.. :/
Any salvation?
Update/More-details:
On some programs (like TextMate) this method does work. The application I was trying to debug is Update: seems the real explanation is rather:/Developer/Applications/Audio/AU\ Lab.app
. For some reason this trick does not work there..
If after attaching I redirect stdout before calling printf
for the first time, it works! If I first printf
and only then redirect output, output shows up only after doing printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n")
and then pressing enter (to repeat last command) 327 times.
So I guess redirecting stdout somehow makes libc confused and makes it use a buffer of 2**14
bytes for some reason?
The problem could be related to a peculiarity of your setup. Here are a couple of further suggestions you could try to identify the source of the problem:
First, check the way the debugger is launched - are you using the command line in the Terminal or is it through some GUI which may submit additional command line arguments beyond your control? (You mentioned XCode, but I don't see why you would debug the program you mentioned through XCode? And actually I am not sure how XCode executes gdb, so please try to stick to the terminal for now!)
Do you have an init-file in your home directory (a file called
.gdbinit
or so) that configures gdb with some options you are unaware of? Here is a description of what gdb does on startup: http://sourceware.org/gdb/current/onlinedocs/gdb/Startup.html#StartupAccording to the manual page, using the
-n
option on startup will prevent loading any configuration files: http://sourceware.org/gdb/current/onlinedocs/gdb/Mode-Options.html#Mode-Options Does your problem still persist if you use this switch?One particular set of commands regards gdb's logging behavior: http://sourceware.org/gdb/current/onlinedocs/gdb/Logging-Output.html#Logging-Output Can you execute
show logging
at the console and check what happens to the output?What is the output of
show inferior-tty
? If it is set to""
then it will be the same as your gdb console (which is fine). Otherwise, program output will go somewhere else, but this cannot explain the issue you had with output disappearing form interactive gdb commands!
精彩评论