3

For example, consider the following debugging session:

(gdb) break foo
Breakpoint 1 at 0x4004f1: file tst.c, line 5.

(gdb) run
Starting program: /tmp/tst 
Breakpoint 1, foo () at tst.c:5
5         return ary[i++];

(gdb) finish
Run till exit from #0  foo () at tst.c:5
Value returned is $1 = 1

(gdb) cont
Continuing.
Breakpoint 1, foo () at tst.c:5
5         return ary[i++];

(gdb) finish
Run till exit from #0  foo () at tst.c:5
Value returned is $2 = 3

After executing a finish command, I get the return value assigned to a convenience variable (e.g. $1 or $2). Unfortunately, every time the command is executed, the value is assigned to a different variable. That's the problem, I cannot write a script which examines the returned value cause I don't know what variable the value was assigned to.

Why do I need that? I want to set a breakpoint at a certain function but to stop program execution only if the function has returned a specific value. Something like this:

break foo
commands
  finish
  if ($return_value != 42)
    continue;
  end
end

So the question is: Is there any way to examine in a script the value returned from a function?

2
  • You can use $ to access the most recent value in gdb's value history. I don't know whether you can have commands in a breakpoint command list that will actually run after a finish command, though. Commented Nov 19, 2015 at 16:22
  • Wow! I tried a dozen of various combinations like $$ or $? but it turned out to be much simpler. However, it seems that indeed no more commands are executed after a finish command. Anyway, if you convert your comment into an answer I'll mark it as the correct one. Commented Nov 19, 2015 at 18:10

2 Answers 2

1

This isn't easy to do from the gdb CLI. Maybe it is impossible purely using the traditional CLI -- because you can have inferior control commands like finish in a breakpoint's commands. This is a longstanding gdb issue.

However, like most automation problems in gdb, it can be solved using the Python API. Now, unfortunately, this approach requires a bit of work on your part.

Essentially what you want to do is subclass the Python FinishBreakpoint class to have it do what you want. In particular you want to write a new command that will set a regular breakpoint in some function; then when this breakpoint is hit, it will instantiate your new FinishBreakpoint class. Your class will have a stop method that will use the return_value of the finish breakpoint as you like.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I know that it is possible with python. But I was curious if it was possible in interactive mode from command line interface.
0

The first part of your question is straightforward: just use $ to access the most recent value in gdb's value history.

From GDB: Value History

The values printed are given history numbers by which you can refer to them. These are successive integers starting with one. print shows you the history number assigned to a value by printing ‘$num = ’ before the value; here num is the history number.

To refer to any previous value, use ‘$’ followed by the value's history number. The way print labels its output is designed to remind you of this. Just $ refers to the most recent value in the history, and $$ refers to the value before that. $$n refers to the nth value from the end.

But, executing commands following a finish command in a breakpoint command list may not currently be possible; see Tom Tromey's answer for a workaround.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.