2

I want to track the state of all the variables in a function in C.

I know I can use info commands in gdb to get all the variables state at the current context.

And I can use gdbinit to automate the gdb.

But what I want to do is set a breakpoint at the function and then after executing each line print execute the info command.

Basically, I want to set 2 breakpoints: one at the start of a function and one at the end of the same function(I don't know how to this) and execute line-by-line between these 2 points.

I want something like this in my gdbinit:

b <func_name>
commands
while <inside the function>:
   info locals
   next
end
run

Is there a way to have the while loop shown above?

sonic_maniac
  • 59
  • 10
  • Are you looking for [gdb breakpoint commands](https://sourceware.org/gdb/download/onlinedocs/gdb/Break-Commands.html#Break-Commands)? It allows you to configure one or more commands to run every time a breakpoint is hit. – kaylum Mar 25 '20 at 03:52
  • @kaylum Yeah something like this. I already looked at the documentation. But what I want is something different. I edited my question to reflect on what I am looking for. – sonic_maniac Mar 25 '20 at 04:15
  • Did you try to use `display` and `undisplay`? – the busybee Mar 25 '20 at 07:04
  • This is not quite what I am looking for.`display` would show the state of the variable when I do something right(such as entering `next`). But I want to automate this so that I don't write next multiple times in my commands. – sonic_maniac Mar 25 '20 at 20:42

1 Answers1

3

Basically, I want to set 2 breakpoints: one at the start of a function and one at the end of the same function(I don't know how to this) and execute line-by-line between these 2 points.

You can find how to set the breakpoint at the end of the function here. However, that isn't necessary here.

You can use $_caller_is() GDB convenience function to finish executing current routine, and stop after it has returned.

Example:

int fn()
{
  int sum = 0;
  for (int j = 0; j < 5; j++) {
    sum += j;
  }
  return sum;
}

int main()
{
  return fn() - 10;
}

And now GDB session:

gdb -q ./a.out
Reading symbols from ./a.out...
(gdb) b 4
Breakpoint 1 at 0x1130: file t.c, line 4.
(gdb) run
Starting program: /tmp/a.out

Breakpoint 1, fn () at t.c:4
4     for (int j = 0; j < 5; j++) {
(gdb) while $_caller_is("main")
 >info locals
 >next
 >end
j = 1431654464
sum = 0
5       sum += j;
j = 0
sum = 0
4     for (int j = 0; j < 5; j++) {
j = 0
sum = 0
5       sum += j;
j = 1
sum = 0
4     for (int j = 0; j < 5; j++) {
j = 1
sum = 1
5       sum += j;
j = 2
sum = 1
4     for (int j = 0; j < 5; j++) {
j = 2
sum = 3
5       sum += j;
j = 3
sum = 3
4     for (int j = 0; j < 5; j++) {
j = 3
sum = 6
5       sum += j;
j = 4
sum = 6
4     for (int j = 0; j < 5; j++) {
j = 4
sum = 10
7     return sum;
sum = 10
8   }
sum = 10
main () at t.c:12
12    return fn() - 10;
(gdb) q

P.S. For all but toy problems, this method of debugging will be both grossly inefficient and insufficient (it is rare for the interesting state to be captured entirely by local variables).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Hi, So there are two things: 1) ```while true``` has a syntax error. so can you please change it to ```while 1``` 2) This doesn't stop at the next breakpoint, it just continues till the end of program execution. – sonic_maniac Mar 27 '20 at 20:16
  • Thank you very much. – sonic_maniac Mar 28 '20 at 21:12