2014-05-20 44 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