1

I want to debug a file, say file.c, and this file reads information from a txt file, say input.txt. So normally, to compile and run the program I would do the following:

gcc -std=c99 -g file.c -o file.exe

and to debug I would try the following:

gdb ./file.exe input.txt

However this doesn't work and fails with No such file exits. '(null)': Bad address when the program, file.c, attempts to open the file specified in argv[1] which input.txt is.

I have tried the following methods:

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)
   $1 = 0

All with the same outcome as described above...No such file exits. '(null)': Bad address

The code is merely:

FILE *input = fopen(argv[1],"r");
4
  • 5
    (gdb) run input.txt Commented Apr 29, 2019 at 21:03
  • 2
    Your file is expected as a parameter, not redirected stream. Commented Apr 29, 2019 at 21:04
  • Try gdb --args ... Commented Apr 29, 2019 at 21:21
  • regarding: FILE *input = fopen(argv[1],"r"); This will not compile. It needs: 1) #include <stdio.h> 2) int main( void ){ FILE *input = fopen(argv[1],"r"); .... etc Commented May 1, 2019 at 5:43

1 Answer 1

1

Do

gdb --args ./file.exe file.txt
b main
run

or do

gdb ./file.exe
b main
run file.txt
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.