Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gdb

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.

like image 525
user556144 Avatar asked Dec 29 '22 03:12

user556144


2 Answers

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!

like image 121
user556144 Avatar answered Jan 31 '23 02:01

user556144


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

like image 25
Jester Avatar answered Jan 31 '23 01:01

Jester