2

Say I have a C program that, when run, calls scanf and then branches depending on whether the input meets some criteria. I can run the program in regular gdb and type it in manually, but I have to test a lot of strings and I'd rather do it with a script.

So I basically want to define a command that will:

  1. Load the executable and run it
  2. Input some text in the console for me
  3. If a breakpoint is hit, restart from the beginning. Otherwise, let the program continue.

This is what I have now.

# MyCommand.py

import gdb

class MyCommand(gdb.Command):
    def __init__(self):
        super(MyCommand, self).__init__("my-command", gdb.COMMAND_DATA)

    def invoke(self, arg, from_tty):
        gdb.execute("file prog")
        gdb.execute("b wrong_input")
        gdb.execute("run")
        gdb.write("abcd") #stops working here

MyCommand()

I run it in gdb with:

(gdb) source MyCommand.py
(gdb) my-command

I don't know how to print something to the program's stream instead of gdb. When I run that command, it waits for me to type something and only prints abcd when it hits a breakpoint and the gdb shell comes back. Can I do this with python? And if not, is there any way to do it?

6
  • As a workaround, put your string into a temporary file and use a redirection for the run command. Commented Nov 15, 2019 at 1:04
  • I have about 1k strings to test, though Commented Nov 15, 2019 at 1:05
  • How is that relevant? Commented Nov 15, 2019 at 1:07
  • I only want to do one string at a time. Do you mean I should put all the strings into one file? Or keep one string in the file and change it every time I want to test a new one? Commented Nov 15, 2019 at 1:11
  • Yes, put one at a time. That should be no problem with python. You'd have to pass your strings one at a time even if your solution worked too. Commented Nov 15, 2019 at 1:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.