Next: Iterators In Guile, Previous: I/O Ports in Guile, Up: Guile API

##### 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 it is restricted to one of ‘`"r"`’, ‘`"w"`’, or ‘`"r+"`’. For compatibility ‘`"b"`’ (binary) may also be present, but we ignore it: memory ports are binary only. The default is ‘`"r"`’, read-only.

> 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)