4

How do I assign something to a gdb convenience variable from its built-in python? I thought it should be simple, because it naturally arises while trying to implement a user-defined function. However I couldn't find anything.

UPD: Added a simplified example. My real function is more complex though. Anyway, a way to transfer a variable from python up to gdb would help greatly.

An example of usage (achtung! — it raises an error):

#accept a string, return it's lenght
define RetLenOfArg
  py arg0 = gdb.parse_and_eval("$arg0")
  set $Retrn = py len(arg0)
end
3
  • Please explain your problem in greater detail and with relevant examples/code if possible Commented Apr 18, 2014 at 8:14
  • 1
    I found a quick startup guide about GDB convenience functions and arguments in here: sourceware.org/gdb/onlinedocs/gdb/Functions-In-Python.html Commented Apr 18, 2014 at 8:31
  • @Ivaylo yes, you're right, you may post it as an answer. To sum up a steps: instead of implementing a function in .gdbinit we need to create a whole class in a separate python file, load it in a gdb through a command source <python_file>, and then use. In my real case I will need anyway create one more function that will use a python one. Cumbersome, but that's how it is done. Commented Apr 18, 2014 at 9:18

2 Answers 2

5

There exists gdb's python API to get/set convenience variable: from the documentation

Function: gdb.convenience_variable (name) Return the value of the convenience variable (see Convenience Vars) named name. name must be a string. The name should not include the ‘$’ that is used to mark a convenience variable in an expression. If the convenience variable does not exist, then None is returned.

Function: gdb.set_convenience_variable (name, value) Set the value of the convenience variable (see Convenience Vars) named name. name must be a string. The name should not include the ‘$’ that is used to mark a convenience variable in an expression. If value is None, then the convenience variable is removed. Otherwise, if value is not a gdb.Value (see Values From Inferior), it is is converted using the gdb.Value constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: that's a GDB 8.2 feature, in case someone needs it with an older version use gdb.execute as mentioned in the accepted answer.
4

accept a string, return it's lenght

If you indeed need strlen you can use $_strlen - a built in gdb convenience function that compute string length:

(gdb) set $Retrn=$_strlen("test")
(gdb) p $Retrn
$4 = 4

how to assign something to a GDB convenience variable from a built-in python?

You can use gdb.execute:

>more my_own_len.py
class my_own_len (gdb.Function):
   def __init__ (self):
     super (my_own_len, self).__init__ ("my_own_len")

   def invoke (self, arg):
      res=len(arg.string())
      gdb.execute( "set $Retrn=" + str(res))
      return res

my_own_len()

This is a test:

>gdb -q -x my_own_len.py
(gdb) p $_strlen("test")
$1 = 4
(gdb) p $my_own_len("test")
$2 = 4
(gdb) p $Retrn
$3 = 4
(gdb)

Comments

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.