Make GDB print context around every time - gdb

LLDB print context around current line every time like this:
int a = 12;
int b = a * 13;
-> printf("%d\n", b);
return 0;
}
In the same time, GDB just print one current line:
-> printf("%d\n", b);
Can I make GDB print context every step like LLDB? Googling give all around list command.

a way to accomplish this might be by defining a macro that redefines a keyword, such as 's' or 'n'.
for example, if you wanted to print out the value of the stack pointer at each step you could redefine 's' by entering these lines into the (gbd) console:
def s
step
info registers sp
end
now every time you use the command 's' you actually do a step and print of the sp register

There is no built-in way to do this.
You could maybe make it work, sort of, using hookpost-stop to invoke an explicit list command.
I think most people just use one of the many gdb GUIs instead, though.

Related

gdb - disable printing of return value when calling `finish`

simply put - any way to completely disable printing after calling finish?
I tried modifying the accepted answer of this question but the result is not satisfying because:
if the returned value is too big you still get the annoying "---Type <return> to continue, or q <return> to quit---"
it still takes a while to run as if the output if being printed - very impactful when the returned object is big
think about something like wanting to step into simple_computation() and this is the actual line int res = get_huge_singleton_manager().simple_computation();
yes, we could write it as 2 lines of code:
auto&& m = get_huge_singleton_manager();
int res = m.simple_computation();
and there wouldn't be an issue but we don't always have control of the code we're debugging...
Starting in GDB 9, there is a command to disable this printing:
(gdb) set print finish off
This is documented in the manual.

GDB define command: print $arg1 doesn't print the correct value when in define

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?
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).

How to check current breakpoint function in TRACE32?

I'm trying to check if the program stopped in a function in a TRACE32.
I know I can see the the functions in FRAME window but no idea how to copy them to a variable inside my script.
Any idea how to do that ?
You get the name of the function, where the program counter points to with:
PRINT sYmbol.FUNCTION(PP())
(Instead of printing the result you can also assign it to a macro.)
So one approach to check if you've stopped in function myFunc() would be:
PRINT STRing.ComPare(sYmbol.FUNCTION(PP()),"*\myFunc")
Another way is to check if the program counter is inside the first and last address of your function myFunc():
PRINT (ADDRESS.OFFSET(sYmbol.BEGIN(`myFunc`))<=Register(PP))&&(Register(PP)<=ADDRESS.OFFSET(sYmbol.END(`myFunc`)))

Gdb conditional step based on memory address?

I 'm wondering if it 's possible to create a script that will continue the program 's execution (after a break) step by step based on the memory address value.
So, if I 'm tracing a function and it goes into a high memory value, I 'd call the gdb script until the memory value is below a set value - then it would break again.
I 'm very new to gdb and still reading the manual/tutorials, but I 'd like to know if my goal is possible :) - and if you could bump me to the proper direction, even better ;)
Thanks!
Edit, updated with pseudocode:
while (1) {
cma = getMemoryAddressForCurrentInstruction();
if (cma > 0xdeadbeef) {
stepi;
} else {
break;
}
}
You're talking about the Program Counter (sometimes called the instruction pointer). It's available in gdb as $pc. Your pseudocode can be translated into this actual gdb command:
while $pc <= 0xdeadbeef
stepi
It'll be slow, since it's starting and stopping the program for every instruction, but as far as I know there's no fast way to do it if you don't know exactly what address you're looking for. If you do, then you can just set a breakpoint there:
break *0xf0abcdef
cont
will run until the program counter hits 0xf0abcdef

How to make GDB substitute variables with their current value when a conditional breakpoint is created

I would like GDB to perform variable substitution when I create a conditional breakpoint. For example:
set variable $my_value = 1
b my_function if my_param == $my_value
set variable $my_value = 5
b my_function if my_param == $my_value
This actually creates 2 identical breakpoints which break in my_function() when my_param equals the current value of $my_value. Hence when running my program a breakpoint is only triggered when my_param is equal to 5. What I actually wanted was two different conditional breakpoints, for the values 1 and 5.
Is there any way to make GDB set conditional breakpoints like this using the current value of a convenience variable instead of the variable itself?
I ask this question because I'm trying to create a GDB script to track memory deallocation which will automatically set conditional breakpoints, e.g.
# set breakpoint after malloc() statement of interest
b some_file.c:2238
# define commands to execute when the above breakpoint is hit
commands
# $last is set to the allocated memory address
set variable $last = new_pointer
# set conditional breakpoint in free() to check when allocated pointer is released
b free if ptr == $last
continue
end
But of course I find that this only works for the last pointer value because all my auto generated breakpoints are identical!
I am going to investigate the use of Python scripting to see if this could solve my problem, but as I have no experience of Python I wanted to post this question first! I feel sure that it should be possible to do what I am trying to achive and any help or suggestions would be much appreciated.
For completness here is how to use the eval command with my original example:
set variable $my_value = 1
eval "b my_function if my_param == %d", $my_value
set variable $my_value = 5
eval "b my_function if my_param == %d", $my_value
This generates two breakpoints for the values 1 and 5 as desired!
Use the eval command (apparently in gdb 7.2 and later)