1

I am using macOS Big Sur, v11.5.2, and GDB v10.2

I have seen similar issues before, but with no clear answer. I am trying to run a program in GDB with input redirection

sudo gdb ./executable
r < input.txt

But when I do this the shell hangs, waiting for manual input instead of reading input from the file. I have gone through the process of creating code signing certificate for GDB, running as sudo, but the same issue occurs.

I also know about MacOS's SIP and how that interferes. This link, coming from this StackOverflow post doesn't seem to offer any solutions. At least any solution that maintains the ability to use input/output redirection in GDB.

If anyone has an answer and can clearly articulate it here, I would greatly appreciate it.

konar
  • 23
  • 3
  • Do you have `set startup-with-shell off` in your GDB init file? That's sometimes recommended on MacOS systems. If that's set, then no shell will be used, and `"<"` and `"input.txt"` will simply be passed as arguments to the program. – Mark Plotnick Sep 15 '21 at 14:21
  • I do not have that in my init file. Although that is not what I am looking for. I don't want "<" and "input.txt" to be passed as arguments. I want the contents of input.txt to be passed to stdin via input redirection. This works on windows and linux machines. – konar Sep 15 '21 at 17:58
  • OK, I'm just trying to eliminate that as a possibility. In GDB, can you type `start` and then `print argv` , just to see what arguments your program is getting? – Mark Plotnick Sep 15 '21 at 18:13
  • I have just opted to use `lldb` instead of `gdb`, works like a charm. – konar Sep 15 '21 at 20:20

1 Answers1

0

Caveat: This isn't so much a complete solution as some workarounds and suggestions [that are too big to fit into a comment].

There's basically two issues:

  1. Does gdb's r command [under macOS] allow input redirection?
  2. Can gdb be run under sudo?

So, let's start by eliminating sudo. Does the following work:

gdb ./executable
r < input.txt

If not, either the run command's input redirection does not work or there is a bug in the executable.

Side note: For many years, I did not know that run could take I/O redirection [silly me :-)].

What I usually do/did is modify the program to allow input either from stdin or from an argument [just like cat]. Then, I'd do:

gdb ./executable
set args input.txt
run

The other issue is whether gdb can be run under sudo.

In the past, I've tried to debug a setuid executable as an ordinary user [if possible]. This eliminates the complexity of trying to run gdb and the executable as root. Sometimes this involves creating a test directory that mirrors/mimics the necessary file hierarchy [but the files are owned by the non-root user] and using (e.g.) chroot or equivalent

Much of the debug can be done this way without root privileges. For the parts that do require root, I often resort to printf debug messages--YMMV.

I'm not familiar with macOS, so I don't know how [the aformentioned] SIP will respond to the following ...

Another trick I've used is to make the executable [and a copy of gdb] into setuid programs:

cp /usr/bin/gdb /home/me/mygdb
sudo chown root /home/me/mygdb
sudo chmod 6741 /home/me/mygdb
sudo chown root /home/me/myprogram
sudo chmod 6741 /home/me/myprogram

Then, I run the modified programs. This can bypass some of the issues with using sudo directly on the target programs.

Of course, I try to make sure that there is no access to the containing directory by others. And, I remove the files when done testing.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • I appreciate the detailed response. 1. There is no issue with the syntax, and is an accepted command. 2. Yes it can be run under sudo, when I don't run it under sudo I get a code signing error, even though I've made the code signing certificate. This error goes away when running as sudo. Unfortunately with your trick of making a copy of gdb did not work :/ I don't understand why apple has to make this so difficult. gdb works fine on my windows and linux machines (with input redirection). – konar Sep 15 '21 at 17:31