gdb + osx:重定向stdout可能会导致printf有一个2 ** 14字节的缓冲区? [英] gdb+osx: redirecting stdout may cause printf to have a 2**14 bytes buffer?

查看:110
本文介绍了gdb + osx:重定向stdout可能会导致printf有一个2 ** 14字节的缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OSX中使用gdb连接到一个程序,我想在gdb控制台中使用 CFShow 等。但是,什么也没有显示出来。 printf 不会显示任何内容:

 (gdb)call(int) printf(Hello World!\\\

$ 11 = 13
(gdb)callf(Hello \\\

$ 10 = 6

Apple建议以下提示用于使用gdb进行附加时,使输出显示在gdb控制台中:

 (gdb)call(void)close(1)
(gdb)call(void)close(2)
(gdb)shell tty
/ dev / ttyp1
(gdb)call(int)open(/ dev / ttyp1,2,0)
(gdb) ,2,0)
$ 2 = 2

在xcode的gdb控制台 tty 给出不是tty ,所以我在终端中的gdb中尝试了它。有 tty 可以工作,但重定向stdout后仍然没有输出。如果我将stdout指向一个文件,那么也没有输出..:/ $ / $>

任何救赎?

更新/更多详细信息




在某些程序(如TextMate)中,此方法可行。我试图调试的应用程序是 / Developer / Applications / Audio / AU \ Lab.app 。因为某些原因,这个技巧在那里不起作用。
更新:似乎真正的解释是:

如果在附加之后我重定向stdout第一次调用 printf ,它可以工作!如果我首先 printf ,然后只重定向输出,输出只有在执行 printf(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\
然后按回车键(重复上一个命令)327次。



所以我猜重定向标准输出会让libc感到困惑,并使它使用 2 ** 14 字节由于某种原因?

解决方案

这个问题可能与特殊性有关的设置。这里有几个进一步的建议,你可以尝试找出问题的根源:


  1. 首先,检查调试器启动 - 您是否在终端中使用命令行,还是通过一些可能会提交额外命令行参数超出您的控制的GUI? (你提到过XCode,但是我不明白你为什么要通过XCode调试你所提到的程序?实际上我不确定XCode是如何执行gdb的,所以请尝试坚持到终端!)


  2. 您的主目录中有一个init-file(名为 .gdbinit 左右)的配置gdb的文件有些选项你不知道?以下是gdb在启动时执行的操作的说明: http:// sourceware .org / gdb / current / onlinedocs / gdb / Startup.html#Startup


  3. 根据手册页,使用<$ c $在启动时,c> -n 选项将阻止加载任何配置文件: http://sourceware.org/gdb/current/onlinedocs/gdb/Mode-Options.html#Mode-Options 如果您使用此开关,您的问题仍然存在吗?


  4. 一组特定的命令将关注gdb的日志行为: http://sourceware.org/gdb/current/onlinedocs/gdb/Logging-Output.html#Logging-Output 您可以执行 show logging 并检查发生了什么s到输出?


  5. show inferior-tty 的输出是什么?如果它设置为那么它将和你的gdb控制台一样(这很好)。否则,程序输出将会出现在其他地方,但这不能解释输出消失在交互式gdb命令中的问题!



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?

解决方案

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!

这篇关于gdb + osx:重定向stdout可能会导致printf有一个2 ** 14字节的缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆