如何调试使用 gdb 中辅助 txt 文件输入的程序?
How to debug a program that uses input from an auxiliary txt file in gdb?
我想调试一个文件,比如 file.c,这个文件从一个 txt 文件中读取信息,比如 input.txt。所以通常情况下,要编译和 运行 程序,我会执行以下操作:
gcc -std=c99 -g file.c -o file.exe
为了调试,我会尝试以下操作:
gdb ./file.exe input.txt
然而,当程序 file.c 试图打开 argv[1] 中指定的文件时,input.txt 是,这不起作用并且失败并显示 No such file exits. '(null)': Bad address
。
我试过以下方法:
1. gdb ./file.exe
(gdb) run < input.txt
2. gdb ./file.exe
b main
(gdb) r
(gdb) call (int)dup2(open("input.txt",0),0)
= 0
所有结果都与上述相同...No such file exits. '(null)': Bad address
代码只是:
FILE *input = fopen(argv[1],"r");
做
gdb --args ./file.exe file.txt
b main
run
或做
gdb ./file.exe
b main
run file.txt
我想调试一个文件,比如 file.c,这个文件从一个 txt 文件中读取信息,比如 input.txt。所以通常情况下,要编译和 运行 程序,我会执行以下操作:
gcc -std=c99 -g file.c -o file.exe
为了调试,我会尝试以下操作:
gdb ./file.exe input.txt
然而,当程序 file.c 试图打开 argv[1] 中指定的文件时,input.txt 是,这不起作用并且失败并显示 No such file exits. '(null)': Bad address
。
我试过以下方法:
1. gdb ./file.exe
(gdb) run < input.txt
2. gdb ./file.exe
b main
(gdb) r
(gdb) call (int)dup2(open("input.txt",0),0)
= 0
所有结果都与上述相同...No such file exits. '(null)': Bad address
代码只是:
FILE *input = fopen(argv[1],"r");
做
gdb --args ./file.exe file.txt
b main
run
或做
gdb ./file.exe
b main
run file.txt