0

I want to define a new command which basically sets a breakpoint on a line, print a value of a certain variable and then continues execution. Unfortunately I am having issues. Here is the code I am using

(gdb) define print_and_continue
Type commands for definition of "print_and_continue".
End with a line saying just "end".
>break $arg0
>command $bpnum
 >print $arg1
 >continue
 >end
>end

So I want to print the value of variable len which is defined in linked_list.h:109. And I execute the following code:

(gdb) print_and_continue linked_list.h:111 len
Breakpoint 1 at 0x388a: linked_list.h:111. (12 locations)
(gdb) r
...

Breakpoint 1, linked_list<test_struct<1>, 1>::remove_if<run_test<1, 1, 1>(std::vector<int, std::allocator<int> >&)::{lambda(test_struct<1> const&)#1}>(run_test<1, 1, 1>(std::vector<int, std::allocator<int> >&)::{lambda(test_struct<1> const&)#1}&&) (this=0x7fffffffdca0, condition=...) at linked_list.h:112
112         linked_list_node* prev = nullptr;
$1 = void

It seems like $arg1 in print function didn't get replaces by the actual argument. What am I doing wrong?

Bogi
  • 2,274
  • 5
  • 26
  • 34

1 Answers1

1

It seems like $arg1 in print function didn't get replaces by the actual argument.

I don't believe that's what is actually happening. Rather, everything following command $bpnum is attached to the newly-created breakpoint literally (without any expansion at all). You can see that happening with info break, which will show something like:

Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001136 at ...
        print $arg1
        continue

This is generally what you would want (deferring evaluating the argument until the time breakpoint is hit). Otherwise you would print current value of len if you use print len, when what you want is to print the value of len when the breakpoint is hit.

Of course, when the breakpoint is hit, there is no $arg1 (or $arg0) anywhere around, so you get the same output you'd get trying to print any other non-existent GDB variable.

What am I doing wrong?

You are using "quick hack of a language" (which is what the "native" GDB scripting language is), instead of using a proper programming language.

I am 99.99% certain that defining print_and_continue is possible (and probably quite easy) using embedded Python.

That said, I don't believe that print_and_continue is all that useful (in my 20+ years of using GDB, I never needed anything like that).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362