JimR
JimR

Reputation: 2225

Scripting in gdb

Say, for example, I have a C source file with a method like foo(a) where a is a character.

I want to print the output of foo for every character is there an easier way than going through systematically and entering p foo('a') then p foo('b')?

Ideally I'd really like to script it so it's a bit quicker.

Upvotes: 4

Views: 5555

Answers (3)

JimR
JimR

Reputation: 2225

I managed to figure it out, my code was basically:

define foo_test
    set $a = 97
    set $b = 123

    while $a < $b
        p (char)foo($a)
        set $a = $a + 1
    end
end

Upvotes: 7

Johan
Johan

Reputation: 20763

It sounds like the first thing you should add is some "Breakpoint command lists", those will let you run some gdb commands after a breakpoint has hit.

So if you add so your print runs when someone calls the functions foo, you should be are getting kind of close.

Upvotes: 0

Employed Russian
Employed Russian

Reputation: 213526

perl -e 'foreach $i ("a" .. "z") { print "print foo('\''$i'\'')\n"; }' > /tmp/t.$$ &&
gdb --batch -x /tmp/t.$$ ./a.out ; rm -f /tmp/t.$$

You should also look into GDB Python scripting.

Upvotes: 2

Related Questions