How to get GDB to replace variables with its current value when creating a conditional breakpoint

I would like GDB to perform variable substitution when I create a conditional breakpoint. For instance:

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 that break in my_function () when my_param is equal to the current value of $ my_value. Therefore, when starting my program, a breakpoint only starts when my_param is 5. What I really wanted was two different conditional breakpoints for values ​​1 and 5.

Is there a way to make GDB to set conditional breakpoints like this, using the current value of a convenience variable instead of the variable itself?

I ask this question because I am trying to create a GDB script to track memory freeing that automatically sets conditional breakpoints, for example.

# 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 believe that this only works for the last pointer value, because all of my automatically generated breakpoints are identical!

I'm going to research the use of Python scripts to see if this can solve my problem, but since I have no experience with Python, I would like to post this question first! I am sure that it should be possible to do what I am trying to achieve, and any help or suggestions would be highly appreciated.

+3
source share
2 answers

Use the command eval(apparently in gdb 7.2 and later)

+3

eval :

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

1 5 !

+5

Source: https://habr.com/ru/post/1782468/


All Articles