1

I have been attempting to follow the suggestions given in option 3 of the first answer to this similar question. I have succeeded in using the provided commands to redirect input, but I can't manage to redirect output. Here's my commands:

# gdb debugee.exe
(gdb) b main
(gdb) run
(gdb) p dup2(open("output.txt", 256), 1) 
(gdb) c

Noticed that I am using 256 to say "create if not exist" and 1 instead of 0 to redirect stdout instead of stdin.

The file is created, and the program output appears to go somewhere, since it doesn't appear in the terminal window; but the file remains empty.

What am I doing wrong? Or are there additional considerations for output?

1 Answer 1

1

You should first try:

p open("output.txt", 256)

and verify that this returns something other than -1.

The O_CREAT version of open actually takes 3 arguments. Assuming O_CREAT is in fact 256 on your system, and O_WRONLY is 1, the correct call should be something like

p open("output.txt", 257, 0744)

What am I doing wrong?

Besides supplying garbage for the mode of newly-created file, you are creating it in read-only mode. The subsequent writes all fail because of that, and go nowhere.

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.