5

I am making a nice presentation of ARM assembly code execution and I would need GDB to step the code every 1 second infinitely long (well until I press CTRL+C). Has anyone got solution?

I don't want to keep on standing next to the keyboard and stepping the program when visitors come visit my stall.

8

Gdb's CLI supports a while loop. There's no builtin sleep command, but you can either call out to the shell to run the sleep program, or use gdb's builtin python interpreter, if it has one. It's interruptible with Control-C.

Method 1:

(gdb) while (1)
 >step
 >shell sleep 1
 >end

Method 2:

(gdb) python import time
(gdb) while (1)
 >step
 >python time.sleep(1)
 >end

Method 3 (define a macro):

(gdb) define runslowly
Type commands for definition of "runslowly".
End with a line saying just "end".
>python import time
>while (1)
 >step
 >python time.sleep(1)
 >end
>end
(gdb) document runslowly
Type documentation for "runslowly".
End with a line saying just "end".
>step a line at a time, every 1 second
>end

(gdb) runslowly
  • This kooks like the best answer! – 71GA Feb 8 '18 at 23:55
7

expect can automate this

#!/usr/bin/env expect
spawn -noecho gdb -q ls
expect -ex {(gdb)}
send -- "break main\r"
expect -ex {(gdb)}
send -- "run\r"
while {1} {
    expect -ex {(gdb)}
    send -- "s\r"
    sleep 1
}

or if there's a risk of the program running out of s you can repeatedly gdb it with a little more complication

#!/usr/bin/env expect

while {1} {
    spawn -noecho gdb -q ls
    expect -ex {(gdb)}
    send -- "break main\r"
    expect -ex {(gdb)}
    send -- "run\r"
    expect {
        -ex {The program is not being run} {}
        eof {}
        -ex {(gdb)} {
            send -- "s\r"
            sleep 1
            exp_continue
        }
    }
}
  • So how do I use this? Can you add a description on how to start it. – 71GA Feb 8 '18 at 23:39
  • install expect, save the scripts to a file, chmod +x them, run them like any other script with a shebang line – thrig Feb 8 '18 at 23:50
3

You could have the shell pipe in commands; here's the idea:

while :; do echo step; sleep 1; done | gdb arm-program

gdb reads the commands from the pipe; it sees a "step" command every second ad infinitum.

You may want to set up some break-points and run the program; adjust to taste:

(echo br 1; echo run; while :; do echo step; sleep 1; done ) | gdb arm-program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.