簡體   English   中英

將輸出重定向到gdb中的文件

[英]redirect output to a file from gdb

我正在從gdb打印變量的內容,如下所示:

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

緩沖區包含一個大字符串,我想將其重定向到文件而不是屏幕。 在gdb中啟用logging功能在這里沒有幫助。 我也無法使用>命令重定向。 當然,我可以在程序中創建一個文件,並將緩沖區寫入該文件,並通過gdb調用write to file。 但是有更簡單的方法嗎?

你不能夠使用>或你不知道如何在gdb使用它? 您可以從gdb內部重定向輸出。 嘗試:

(gdb) run > out.txt

(gdb) run > /dev/null

這會將目標的標准輸出重定向到您選擇的文件,調用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM