0

I have an real-time OO program (nevertheless written in C) that I am trying to debug. I have an issue that some objects out of thousands get corrupted during a specific window of time, every now and then. I want to have one breakpoint at the start of the window, which automatically sets a watchpoint on a member variable, and then have that watchpoint removed at the end of the window by another breakpoint. The trouble is I need some way of tying a watchpoint number to a given object. If I could construct a convenience variable by some mechanism so that, for example, if $var=28, then set $x${var}=watch -l foo would be the equivalent of set $x28=watch -l foo. (which doesn't actually work) This would allow me to do this:

breakpoint obj_init+23
command
  $var = *obj
  $x${var} = watch -l foo
  continue
done

breakpoint obj_final
command
  $var = *obj
  delete $x${var}
  continue
done

So I don't (hopefully) overrun the number of available hardware watchpoints.

Does anyone know how I might achieve this without trying to write a python extension? (My python is very rusty.)

4
  • 1
    did you try valgrind? seems easier to setup and usually help in data corruption problems Commented Apr 20, 2021 at 16:58
  • I don't think it's possible without python. Also, I agree that you should try it with valgrind or sanitizers first. Commented Apr 21, 2021 at 10:51
  • valgrind was useless. Mostly because I'm running a gstreamer pipeline consisting of glib objects under python, and they ALL do strange things to memory handling. I was not able to get a useful run after a full day of trying, and yes, I've read the valgrind docs for each of the frameworks in question. Commented Apr 21, 2021 at 17:08
  • As for sanitizers I don't think they are (yet) enabled on my platform with gcc. I need to run these tests on CentOS and its years behind on compilers. Commented Apr 21, 2021 at 17:10

1 Answer 1

1

You can use GDB's eval command to set and use variables whose names are composed of the results of numeric or string expressions.

You can think of eval as doing a printf of its arguments and then executing the resulting string. eval "set var $x%d = 1", 5 will run the command set var $x5 = 1.

The other piece of info you need is that the watch command, as with all breakpoint commands, will set the convenience variable $bpnum to the breakpoint number.

break obj_init+23
commands
  set var $var = *obj
  watch -l foo
  eval "set var $x%d = $bpnum", $var
  continue
done

break obj_final
commands
  set var $var = *obj
  eval "delete $x%d", $var
  continue
done
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was unaware of the 'eval' command!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.