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所定义的启动参数在重定向命令下是不起作用的。

猜你喜欢

转载自blog.csdn.net/weixin_46222091/article/details/105060305