tags:

views:

191

answers:

1

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 /Developer/Applications/Audio/AU\ Lab.app. For some reason this trick does not work there.. Update: seems the real explanation is rather:

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?

+1  A: 

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:

  1. 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!)

  2. 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#Startup

  3. According 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?

  4. 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?

  5. 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!

user8472
@user847: See new update. In short my own problem is solved now, but there's still a weird phenomenon here to be explained. The reason to debug "AU Lab" in xcode is to debug your Audio-Unit plugins which you can run with "AU Lab". Also I tried this on both my Snow-Leopard desktop and my Leopard laptop and both exhibit the same behavior. No `.gdbinit` and `inferior-tty` is `""`. thanks
yairchu
Oh, I see. Thanks for the clarification, I had launched the application on the command line through gdb, not the way you have been doing; so that's why I could not reproduce the problem right away.
user8472
@user847: even though a mystery remains (as I've updated my question to reflect it), my original problem is solved. so I marked your answer as accepted
yairchu