1

If a C program is something like:

void main ()
{
    int a, b, c;
    a = 1;
    b = a + 1;
    c = b + 1;
}

while running gdb, and single stepping:

How do I display the variable that got updated by that single step? Of course this is a highly simplified example, the idea being trace the execution. Something like:

(gdb) step
         a=1
(gdb) step
         b=2
(gdb) step
         c=3

Thanks

Deanie
  • 2,316
  • 2
  • 19
  • 35
Mikey
  • 21
  • 3
  • Compiled code doesn't update variables, it updates registers. Your ability to inspect variables by name will depend on debugging symbols. The `print` command is probably what you are looking for. – Alexander O'Mara Jan 29 '16 at 00:27
  • I agree about registers. I know about display and print (at least the basic syntax). I was looking for a shorthand to hit enters (steps) and see the execution trace. – Mikey Jan 29 '16 at 00:32
  • Not sure what your problem is. Note that, however, `main` has an invalid signature. And good programming style includes to `return 0;` (although not required by the standard) from `main` (you will see the reason if you use the correct signature). – too honest for this site Jan 29 '16 at 00:35

3 Answers3

1

gdb has an option that causes it to display 6 panels.

The upper right panel displays locals or registers.

(gdb) step
      a=1

With this display, the line a=1 is the next line to be executed.

How do I display the variable that got updated by that single step?

In the 6 panel display, the variable a is displayed, (and since you did not initialize it, any value might show prior to the step) and when you step, the 'a' value is updated.

In emacs, the command is (setq gdb-many-windows t), and I am confident the gdb manual can identify the command line equivalent.

--- might be related to "layout regs"

2785528
  • 5,438
  • 2
  • 18
  • 20
0

This cannot be done automaticly, imagine a line where you step that loks like this:

int a = someFunction(b++, c--);

a, b, and c change (along with any number of global or static variables affected by someFunction). How would GDB know what to print? You can use the print command to show a variables value.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • I understand there can be a lot of complicated cases where multiple values are being updated. How about simply trace the LHS of the "="? Surely gdb knows the LHS is a single variable. Thanks. – Mikey Jan 29 '16 at 00:59
  • @Mikey And what if the variable is a vector or self made class? How will it know what to display then? Best bet is to print or add a watch. – Fantastic Mr Fox Jan 29 '16 at 01:00
0

The closest thing that gdb has to this is setting a watch for a variable. GDB will break every time a watched variable is changed. GDB: Watch a variable in a given scope for example

Community
  • 1
  • 1
bruceg
  • 2,433
  • 1
  • 22
  • 29