gdb: Memory Ports in Guile
23.3.3.24 Memory Ports in Guile
...............................
GDB provides a 'port' interface to target memory. This allows Guile
code to read/write target memory using Guile's port and bytevector
functionality. The main routine is 'open-memory' which returns a port
object. One can then read/write memory using that object.
-- Scheme Procedure: open-memory [#:mode mode] [#:start address]
[#:size size]
Return a port object that can be used for reading and writing
memory. The port will be open according to MODE, which is the
standard mode argument to Guile port open routines, except that the
'"a"' and '"l"' modes are not supported. (guile)File
Ports. The '"b"' (binary) character may be present, but is
ignored: memory ports are binary only. If '"0"' is appended then
the port is marked as unbuffered. The default is '"r"', read-only
and buffered.
The chunk of memory that can be accessed can be bounded. If both
START and SIZE are unspecified, all of memory can be accessed. If
only START is specified, all of memory from that point on can be
accessed. If only SIZE if specified, all memory in the range
[0,SIZE) can be accessed. If both are specified, all memory in the
rane [START,START+SIZE) can be accessed.
-- Scheme Procedure: memory-port?
Return '#t' if OBJECT is an object of type '<gdb:memory-port>'.
Otherwise return '#f'.
-- Scheme Procedure: memory-port-range memory-port
Return the range of '<gdb:memory-port>' MEMORY-PORT as a list of
two elements: '(start end)'. The range is START to END inclusive.
-- Scheme Procedure: memory-port-read-buffer-size memory-port
Return the size of the read buffer of '<gdb:memory-port>'
MEMORY-PORT.
-- Scheme Procedure: set-memory-port-read-buffer-size! memory-port size
Set the size of the read buffer of '<gdb:memory-port>' MEMORY-PORT
to SIZE. The result is unspecified.
-- Scheme Procedure: memory-port-write-buffer-size memory-port
Return the size of the write buffer of '<gdb:memory-port>'
MEMORY-PORT.
-- Scheme Procedure: set-memory-port-write-buffer-size! memory-port
size
Set the size of the write buffer of '<gdb:memory-port>' MEMORY-PORT
to SIZE. The result is unspecified.
A memory port is closed like any other port, with 'close-port'.
Combined with Guile's 'bytevectors', memory ports provide a lot of
utility. For example, to fill a buffer of 10 integers in memory, one
can do something like the following.
;; In the program: int buffer[10];
(use-modules (rnrs bytevectors))
(use-modules (rnrs io ports))
(define addr (parse-and-eval "buffer"))
(define n 10)
(define byte-size (* n 4))
(define mem-port (open-memory #:mode "r+" #:start
(value->integer addr) #:size byte-size))
(define byte-vec (make-bytevector byte-size))
(do ((i 0 (+ i 1)))
((>= i n))
(bytevector-s32-native-set! byte-vec (* i 4) (* i 42)))
(put-bytevector mem-port byte-vec)
(close-port mem-port)