0

In gdb's Values From Inferior documentation, there's a second constructor for creating objects within python. It states:

Function: Value.__init__ (val, type)

    This second form of the gdb.Value constructor returns a gdb.Value of type type where the value contents are taken from the Python buffer object specified by val. The number of bytes in the Python buffer object must be greater than or equal to the size of type.

My question is, how do I create a buffer object that I can pass into the constructor? For instance, if I wanted to create a string (yes, I know that the first Value constructor can do this, but this is an example) I wrote the following function:

  def make_str(self, str):
    str += '\0'
    s = bytearray(str.encode())
    return gdb.Value(s, gdb.lookup_type('char').array(len(str)))

However, when I tried to use it, I got the message:

Python Exception <class 'ValueError'> Size of type is larger than that of buffer object.: 

How would I make a buffer object that I could pass into the Value constructor? What would I need to do to generate a Value object?

1 Answer 1

0

Hmmmm. Seems to be an off by one error since this worked:

  def make_str(self, str):
    str += '\0'
    s = bytearray(str.encode())
    return gdb.Value(s, gdb.lookup_type('char').array(len(s)-1))

Which is strange, I would have expected the array length to be the length of the string, not one less.

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

2 Comments

It looks weird, but this is on purpose: "If one argument is given, it is the inclusive upper bound of the array; in this case the lower bound is zero" sourceware.org/gdb/onlinedocs/gdb/…
@AlexanderFasching, ah yes. Thx. Missed that line.

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.