GDB——从stdin重定向给程序作为输入来调试

GDB——从stdin重定向给程序作为输入来调试

最近在跑LLVM单元测试的时候报了很多错,GDB调试帮很大的忙。

但今天遇到了一个麻烦事,执行下面的单元测试报错了:

FAIL: LLVM :: Bindings/llvm-c/invoke.ll (5115 of 32600)

******************** TEST ‘LLVM :: Bindings/llvm-c/invoke.ll’ FAILED ********************

Script:

‘RUN: at line 1’; /home/cmp/work_dir/source_code/WMarsh_shadow_build/bin/llvm-as < /home/cmp/work_dir/source_code/WMarsh_shadow/llvm/test/Bindings/llvm-c/invoke.ll | /home/cmp/work_dir/source_code/WMarsh_shadow_build/bin/llvm-dis > /home/cmp/work_dir/source_code/WMarsh_shadow_build/test/Bindings/llvm-c/Output/invoke.ll.tmp.orig

‘RUN: at line 2’; /home/cmp/work_dir/source_code/WMarsh_shadow_build/bin/llvm-as < /home/cmp/work_dir/source_code/WMarsh_shadow/llvm/test/Bindings/llvm-c/invoke.ll | /home/cmp/work_dir/source_code/WMarsh_shadow_build/bin/llvm-c-test --echo > /home/cmp/work_dir/source_code/WMarsh_shadow_build/test/Bindings/llvm-c/Output/invoke.ll.tmp.echo

‘RUN: at line 3’; diff -w /home/cmp/work_dir/source_code/WMarsh_shadow_build/test/Bindings/llvm-c/Output/invoke.ll.tmp.orig /home/cmp/work_dir/source_code/WMarsh_shadow_build/test/Bindings/llvm-c/Output/invoke.ll.tmp.echo

Exit Code: 1

由于第3个命令在用diff比较文件时报错了。

对于第2个命令,主要是llvm-c-test程序使用–echo选项,接受stdin重定向作为输入,然后输出invoke.ll.tmp.echo

但是想要调试llvm-c-test程序,在IDE里面就不行了,因为IDE里面只接受输入参数,不支持重定向。

所以想到了GDB来调试。

首先看以下GDB重定向stdin的效果

// 一段小程序
#include <stdio.h>
int main(int argc, char** argv)
{
    char c;
    while (1) {
        c = getc(stdin);
        if (c == EOF) {
            break;
        }
        printf("%c\n", c);
    }
    return 0;
}

使用GDB重定向作为输入

cmp@t3600:/tmp$ gcc 1.c -o test
cmp@t3600:/tmp$ gdb ./test 
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...(no debugging symbols found)...done.
(gdb) r < ./1.txt
Starting program: /tmp/test < ./1.txt
1
2
3
4
5
6


[Inferior 1 (process 15663) exited normally]

在GDB中重定向输入还是和shell中是一样的使用<符号。

值得注意的是:

​ 重定向输入,如何带参数启动程序必须使用r param1 param2 < ./1.txt类似与这样的命令。set args param3所定义的启动参数在重定向命令下是不起作用的。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux操作之输入输出重定向和管道 作者:佚名 出处:中国自学编程网收集整理 发布日期:2008-07-12   Unix下使用标准输入stdin和标准输出stdout,来表示每个命令的输入和输出,还使用一个标准错误输出stderr用于输出错误信息。这三个标准输入输出系统缺省与控制终端设备相联系在一起的。因此,在标准情况下,每个命令通常从它的控制终端中获取输入,将输出打印到控制终端的屏幕上。      但是也可以重新定义程序输入 stdin和输出stdout,将它们重新定向。最基本的用法是将她们重新定义到一个文件上去,从一个文件获取输入,输出到另外的文件中等。      $ ls > ls.out      $ cat < ls.out      这种输入输出重定向带来了极大的灵活性,可以将输出结果记录下来,也可以将程序所需要的输入使用文件提前准备就绪,这样一来多次执行就不需要重新输入。      $ echo “ today is “ > out      $ date >> out      使用 >>标记表示输出结果采用添加的方式,将结果附加在文件out后面,而不是简单的将原有文件重新覆盖的方式。      更为灵活的方式是将输入输出和一个执行命令联系起来,而不是一个固定的文件。      $ ls -l | grep mbox   上面的命令,将 ls -l的输入作为grep的输入,这种方式称为管道。Unix提供了很多功能强大的小命令,但使用管道将这些命令组合起来,就形成了非常强大的工具组合,能完成非常复杂的工作。      Unix系统提供了一些特殊的设备文件,用在一些特殊情况下。例如一个特殊设备文件为/dev/null,永远无法写满,写入的内容被系统立即丢弃。如果不想看到程序的输出,可以使用它作输出。      $ make world > /dev/null   去除了屏幕输出,使整个程序执行过程非常平静。   

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值