2014-05-20 47 views
0

目前,我正在打印变量的内容从GDB这样重定向到文件:输出GDB

(gdb) call printf("%s",buffer) 

缓冲区包含一个大的字符串,我想它重定向到一个文件,而不是屏幕。 在gdb中启用logging功能在这里没有帮助。而且我无法使用>命令重定向。当然,我可以在程序中创建一个文件,并将缓冲区写入此文件并通过gdb调用写入文件。但有没有更简单的方法?

回答

1

你是不是能够使用>或者你不知道如何在gdb使用它?你可以重定向来自gdb的输出。尝试:

(gdb) run > out.txt

(gdb) run > /dev/null

1

这将目标的标准输出重定向到所选的文件,调用printf,然后恢复标准输出到原来的设置。在更改文件描述符之前调用fflush,以便将输出发送到正确的位置。

$ gdb f 
... 
(gdb) list 
1 #include <stdlib.h> 
2 #include <stdio.h> 
3 #include <string.h> 
4 
5 main() 
6 { 
7  char buf[] = "test"; 
8 
9  printf("%p ", (void *)buf); 
10  printf("%d\n", strlen(buf)); 
11 } 
(gdb) break 10 
Breakpoint 1 at 0x80484d3: file f.c, line 10. 
(gdb) run 
Starting program: f 
Breakpoint 1, main() at f.c:10 
10  printf("%d\n", strlen(buf)); 
(gdb) call fflush(stdout) 
0xbffff117 $1 = 0 
(gdb) call dup(1) 
$2 = 3 
(gdb) call creat("/tmp/outputfile",0644) 
$3 = 4 
(gdb) call dup2(4,1) 
$4 = 1 
(gdb) call printf("%s\n", buf) 
$5 = 5 
(gdb) call fflush(stdout) 
$6 = 0 
(gdb) call dup2(3,1) 
$7 = 1 
(gdb) call close(3) 
$8 = 0 
(gdb) call close(4) 
$9 = 0 
(gdb) cont 
Continuing. 
4 
[Inferior 1 (process 3214) exited with code 02] 
(gdb) shell cat /tmp/outputfile 
test