4

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.

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

And then run the script by just typing 'foo_test' at the gdb prompt. If you create this in a text editor and then paste it in be sure to use spaces not tabs.
2
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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.