gdb.Value provides a way to get a textual string (gdb.Value.string(...)), but there's no way to get it as a raw sequence of bytes. Maybe this could be done with an encoding argument of None? e.g. v = gdb.parse_and_eval("var") data = v.string(None, length=64)
A method on Value would be fine, but there's also Inferior.read_memory.
It seems to me that there are two different things we might want to expose. One thing is access to the underlying bytes of a Value. This is maybe a little tricky because a Value might not have all the bytes available. The other thing is using a pointer value to fetch memory. This can be done via read_memory, though it might be nice to also have a convenience method on Value.
The master branch has been updated by Andrew Burgess <aburgess@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ef8cf9093dcf2f2320336f2d9bb9cca33f098189 commit ef8cf9093dcf2f2320336f2d9bb9cca33f098189 Author: Andrew Burgess <aburgess@redhat.com> Date: Wed Oct 18 15:46:23 2023 +0100 gdb/python: Add new gdb.Value.bytes attribute Add a gdb.Value.bytes attribute. This attribute contains the bytes of the value (assuming the complete bytes of the value are available). If the bytes of the gdb.Value are not available then accessing this attribute raises an exception. The bytes object returned from gdb.Value.bytes is cached within GDB so that the same bytes object is returned each time. The bytes object is created on-demand though to reduce unnecessary work. For some values we can of course obtain the same information by reading inferior memory based on gdb.Value.address and gdb.Value.type.sizeof, however, not every value is in memory, so we don't always have an address. The gdb.Value.bytes attribute will convert any value to a bytes object, so long as the contents are available. The value can be one created purely in Python code, the value could be in a register, or (of course) the value could be in memory. The Value.bytes attribute can also be assigned too. Assigning to this attribute is similar to calling Value.assign, the value of the underlying value is updated within the inferior. The value assigned to Value.bytes must be a buffer which contains exactly the correct number of bytes (i.e. unlike value creation, we don't allow oversized buffers). To support this assignment like behaviour I've factored out the core of valpy_assign. I've also updated convert_buffer_and_type_to_value so that it can (for my use case) check the exact buffer length. The restrictions for when the Value.bytes can or cannot be written too are exactly the same as for Value.assign. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=13267 Reviewed-By: Eli Zaretskii <eliz@gnu.org> Approved-By: Tom Tromey <tom@tromey.com>
Nowadays I think the Value.bytes method is enough. For a pointer you can use deref or indexing, which should be plenty.