简体   繁体   English

将输出重定向到gdb中的文件

[英]redirect output to a file from gdb

I'm currently printing the contents of a variable from gdb like this: 我正在从gdb打印变量的内容,如下所示:

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

The buffer contains a large string and I want to redirect it to a file rather than screen. 缓冲区包含一个大字符串,我想将其重定向到文件而不是屏幕。 Enabling logging feature in gdb will not help here. 在gdb中启用logging功能在这里没有帮助。 And I'm not able to use > command to redirect either. 我也无法使用>命令重定向。 Ofcourse I can create a file in the program and write the buffer to this file and invoke the write to file through gdb. 当然,我可以在程序中创建一个文件,并将缓冲区写入该文件,并通过gdb调用write to file。 But is there a easier way out? 但是有更简单的方法吗?

You aren't able to use > or you did not know how to use it in gdb? 你不能够使用>或你不知道如何在gdb使用它? You can redirect output from inside of gdb. 您可以从gdb内部重定向输出。 Try: 尝试:

(gdb) run > out.txt

(gdb) run > /dev/null

This will redirect the target's stdout to a file of your choice, call printf , then restore stdout to its previous setting. 这会将目标的标准输出重定向到您选择的文件,调用printf ,然后将标准输出恢复到之前的设置。 fflush is called right before changing the file descriptor, so that output gets sent to the correct place. 在更改文件描述符之前调用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