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?

0 Answers0