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?

Community
  • 1
  • 1
IanPudney
  • 5,941
  • 1
  • 24
  • 39

1 Answers1

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362