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?

Adrian
  • 10,246
  • 4
  • 44
  • 110

1 Answers1

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.

Adrian
  • 10,246
  • 4
  • 44
  • 110
  • 2
    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" https://sourceware.org/gdb/onlinedocs/gdb/Types-In-Python.html#Types-In-Python – Alexander Fasching Dec 01 '21 at 09:40
  • @AlexanderFasching, ah yes. Thx. Missed that line. – Adrian Dec 01 '21 at 10:47