Bug 13267 - python: gdb.Value needs a method to get raw memory as a string
Summary: python: gdb.Value needs a method to get raw memory as a string
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 7.3
: P2 normal
Target Milestone: 15.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-05 20:42 UTC by S Boucher
Modified: 2023-11-14 15:29 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description S Boucher 2011-10-05 20:42:14 UTC
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)
Comment 1 Tom Tromey 2018-09-15 06:18:27 UTC
A method on Value would be fine, but there's also
Inferior.read_memory.
Comment 2 Tom Tromey 2022-06-05 16:29:00 UTC
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.
Comment 3 Sourceware Commits 2023-10-26 19:26:03 UTC
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>
Comment 4 Tom Tromey 2023-11-14 15:29:25 UTC
Nowadays I think the Value.bytes method is enough.
For a pointer you can use deref or indexing, which should be plenty.