Target Communication Framework Services - Memory

Memory Service

Version History

Version Date Change
0.1 2008-01-10 Initial contribution
1.0 2008-05-06 Approved
1.1 2009-03-16 Added context properties

Overview

The service provides basic operations to read/write memory on a target. Command and event parameters are encoded as zero terminated JSON strings.

The service uses standard format for error reports, see Error Report Format.

A single memory access can succeed for some addresses and fail for others. In such situation result message can contain partially valid data. Array of error addresses, in addition to error report, describes data validity on per byte basis:


<array of error addresses>
    ⇒ null
    ⇒ [ <error address list> ]

<error address list><error address><error address list> , <error address>

<error address>
    ⇒ { "addr" : <int: range starting address> , "size" : <int: range length in bytes> , "stat" : <int: status code> , "msg" : <object: error description> }

If there is no entry in error addresses array for a data byte, then status of such byte is defined by main error report.

Status code is bitwise or of status flags:

BYTE_VALID = 0x00
no error for this byte
BYTE_UNKNOWN = 0x01
status is unknown
BYTE_INVALID = 0x02
byte value in invalid, error message describes the problem
BYTE_CANNOT_READ = 0x04
cannot read the byte
BYTE_CANNOT_WRITE = 0x08
cannot write the byte

Commands

Get Context


C • <token> • Memory • getContext • <string: context ID>

The command retrieves context info for given context ID. A context corresponds to an execution thread, process, address space, etc. Exact meaning of a context depends on the target. Target agent should define contexts hierarchy that is:

For traditional OS, like UNIX, memory access context can be one of:

Reply:


R • <token><error report><context data><context data>
    ⇒ null
    ⇒ <object>

Context data object should, at least, contain member "ID" : <string>. Context data is expected to be cached by clients. Service sends contextChanged event to notify changes in context data.

Predefined memory context properties are:

Get Children


C • <token> • Memory • getChildren • <string: parent context ID>

The command requests a list of contexts available for memory access commands.

Parent context ID can be null – to retrieve top level of the hierarchy, can be one of context IDs retrieved by previous getChildren commands, or it can be obtained from another service. Contexts hierarchy can be simple plain list or it can form a tree. It is up to target agent developers to choose layout that is most descriptive for a given target.

Reply:


R • <token><error report><array of context IDs>

<array of context IDs>
    ⇒ null
    ⇒ [ ]
    ⇒ [ <context ID list> ]

<context ID list><string: context ID><context ID list> , <string: context ID>

Set Memory


C • <token> • Memory • set •
    <string: context ID><int: address><int: word size><int: byte count><int: mode><string: BASE64 encoded byte array>

Writes data bytes at given address in memory, "word size" bytes at a time. Address should be aligned by "word size". Context ID must be one returned by getContexts. Mode is logical OR of any combination of:

Result message:


R • <token><error report><array of error addresses>

Error report provides integer error code and a short, human readable explanation of error. Error addresses, when present, let client know which bytes of data failed to be written into memory.

Get Memory


C • <token> • Memory • get •
    <string: context ID><int: address><int: word size><int: byte count><int: mode>

Reads data bytes at given address in memory, "word size" bytes at a time. Address should be aligned by "word size". Context ID must be one returned by getContexts. Mode is logical OR of any combination of:

Result message:


R • <token><string: BASE64 encoded byte array><error report><array of error addresses>

Error report provides integer error code and a short, human readable explanation of error. Error addresses, when present, let client know which bytes of data failed to be retrieved from memory.

Fill Memory


C • <token> • Memory • fill •
    <string: context ID><int: address><int: word size> •
    <int: byte count> • <int: mode><array: array of pattern bytes>

Writes pattern bytes at given address in memory, "word size" bytes at a time. Address should be aligned by "word size". If "byte count" is bigger then pattern size, then pattern is repeated necessary number of times. Context ID must be one returned by getContexts. Mode is logical OR of any combination of:

Result message:


R • <token><error report><array of error addresses>

Error report provides integer error code and a short, human readable explanation of error. Error addresses, when present, let client know which bytes of data failed to be written into memory.

Events

Memory service broadcasts notification events when memory contexts are added, removed or changed, and when memory content is altered by "set" or "fill" commands.


E • Memory • contextAdded • <array of context data> •
E • Memory • contextChanged • <array of context data> •
E • Memory • contextRemoved • <array of context IDs> •
E • Memory • memoryChanged • <string: context ID><array of address ranges><array of context data> - see Get Contexts command.

<array of context IDs>
    ⇒ [ <context ID list> ]

<context ID list><string: context ID><context ID list> , <string: context ID>

<array of address ranges>
    ⇒ null
    ⇒ [ <address ranges list> ]

<address ranges list><address range><address ranges list> , <address range>

<address range>
    ⇒ { "addr" : <int: range starting address> , "size" : <int: range length in bytes> }

API

/**
 * IMemory service provides basic operations to read/write memory on a target.
 */
public interface Memory extends Service {

    static final String NAME = "Memory";

    /**
     * Context property names.
     */
    static final String
        PROP_ID = "ID",                         /** String, ID of the context, same as getContext command argument */
        PROP_PARENT_ID = "ParentID",            /** String, ID of a parent context */
        PROP_PROCESS_ID = "ProcessID",          /** String, process ID, see Processes service */
        PROP_BIG_ENDIAN = "BigEndian",          /** Boolean, true if memory is big-endian */
        PROP_ADDRESS_SIZE = "AddressSize",      /** Number, size of memory address in bytes */
        PROP_NAME = "Name",                     /** String, name of the context, can be used for UI purposes */
        PROP_START_BOUND = "StartBound",        /** Number, lowest address (inclusive) which is valid for the context */
        PROP_END_BOUND = "EndBound",            /** Number, highest address (inclusive) which is valid for the context */
        PROP_ACCESS_TYPES = "AccessTypes";      /** Array of String, the access types allowed for this context */

    /**
     * Values of "AccessTypes".
     * Target system can support multiple different memory access types, like instruction and data access.
     * Different access types can use different logic for address translation and memory mapping, so they can
     * end up accessing different data bits, even if address is the same.
     * Each distinct access type should be represented by separate memory context.
     * A memory context can represent multiple access types if they are equivalent - all access same memory bits.
     * Same data bits can be exposed through multiple memory contexts.
     */
    static final String
        ACCESS_INSTRUCTION = "instruction",     /** Context represent instructions fetch access */
        ACCESS_DATA = "data",                   /** Context represents data access */
        ACCESS_IO = "io",                       /** Context represents IO peripherals */
        ACCESS_USER = "user",                   /** Context represents a user (e.g. application running in Linux) view to memory */
        ACCESS_SUPERVISOR = "supervisor",       /** Context represents a supervisor (e.g. Linux kernel) view to memory */
        ACCESS_HYPERVISOR = "hypervisor",       /** Context represents a hypervisor view to memory */
        ACCESS_VIRTUAL = "virtual",             /** Context uses virtual addresses */
        ACCESS_PHYSICAL = "physical",           /** Context uses physical addresses */
        ACCESS_CACHE = "cache",                 /** Context is a cache */
        ACCESS_TLB = "tlb";                     /** Context is a TLB memory */

    /**
     * Retrieve context info for given context ID.
     *
     * @param id – context ID.
     * @param done - callback interface called when operation is completed.
     */
    IToken getContext(String id, DoneGetContext done);

    /**
     * Client callback interface for getContext().
     */
    interface DoneGetContext {
        /**
         * Called when contexts data retrieval is done.
         * @param error – error description if operation failed, null if succeeded.
         * @param context – context data.
         */
        void doneGetContext(IToken token, Exception error, MemoryContext context);
    }

    /**
     * Retrieve contexts available for memory commands.
     * A context corresponds to an execution thread, process, address space, etc.
     * A context can belong to a parent context. Contexts hierarchy can be simple
     * plain list or it can form a tree. It is up to target agent developers to choose
     * layout that is most descriptive for a given target. Context IDs are valid across
     * all services. In other words, all services access same hierarchy of contexts,
     * with same IDs, however, each service accesses its own subset of context's
     * attributes and functionality, which is relevant to that service.
     *
     * @param parent_context_id – parent context ID. Can be null –
     * to retrieve top level of the hierarchy, or one of context IDs retrieved
     * by previous getContexts commands.
     * @param done - callback interface called when operation is completed.
     */
    IToken getChildren(String parent_context_id, DoneGetChildren done);

    /**
     * Client callback interface for getChildren().
     */
    interface DoneGetChildren {
        /**
         * Called when contexts data retrieval is done.
         * @param error – error description if operation failed, null if succeeded.
         * @param contexts – array of available context IDs.
         */
        void doneGetChildren(IToken token, Exception error, String[] context_ids);
    }

    /**
     * Memory access mode:
     * Carry on when some of the memory cannot be accessed and
     * return MemoryError at the end if any of the bytes
     * were not processed correctly.
     */
    final static int MODE_CONTINUEONERROR = 0x1;

    /**
     * Memory access mode:
     * Verify result of memory operations (by reading and comparing).
     */
    final static int MODE_VERIFY = 0x2;

    interface MemoryContext {

        /**
         * Get context ID.
         * @return context ID.
         */
        String getID();

        /**
         * Get parent context ID.
         * @return parent ID.
         */
        String getParentID();

        /**
         * Get process ID, if applicable.
         * @return process ID.
         */
        String getProcessID();

        /**
         * Get memory endianess.
         * @return true if memory id big-endian.
         */
        boolean isBigEndian();

        /**
         * Get memory address size.
         * @return number of bytes used to store memory address value.
         */
        int getAddressSize();

        /**
         * Get memory context name.
         * The name can be used for UI purposes.
         * @return context name.
         */
        String getName();

        /**
         * Get lowest address (inclusive) which is valid for the context.
         * @return lowest address.
         */
        Number getStartBound();

        /**
         * Get highest address (inclusive) which is valid for the context.
         * @return highest address.
         */
        Number getEndBound();

        /**
         * Get the access types allowed for this context.
         * @return collection of access type names.
         */
        Collection<String> getAccessTypes();

        /**
         * Get context properties.
         * @return all available context properties.
         */
        Map<String,Object> getProperties();

        /**
         * Set target memory.
         * If 'word_size' is 0 it means client does not care about word size.
         */
        void set(long addr, int word_size, byte[] buf,
                         int offs, int size, int mode, DoneMemory done);

        /**
         * Read target memory.
         */
        void get(long addr, int word_size, byte[] buf,
                         int offs, int size, int mode, DoneMemory done);

        /**
         * Fill target memory with given pattern.
         * 'size' is number of bytes to fill.
         */
        void fill(long addr, int word_size, byte[] value,
                          int size, int mode, DoneMemory done);
    }

    /**
     * Client callback interface for set(), get() and fill().
     */
    interface DoneMemory {
        void doneMemory(MemoryError error);
    }

    class MemoryError extends Exception {
    }

    /**
     * ErrorOffset interface can be implemented by MemoryError object,
     * which is returned by get, set and fill commands.
     *
     * get/set/fill () returns this exception when reading failed
     * for some but not all bytes, and MODE_CONTINUEONERROR
     * has been set in mode. (For example, when only part of the request
     * translates to valid memory addresses.)
     * Exception.getMessage can be used for generalized message of the
     * possible reasons of partial memory operation.
     */
    interface ErrorOffset {

        // Error may have per byte information
        final static int
            BYTE_VALID        = 0x00,
            BYTE_UNKNOWN      = 0x01, // e.g. out of range
            BYTE_INVALID      = 0x02,
            BYTE_CANNOT_READ  = 0x04,
            BYTE_CANNOT_WRITE = 0x08;

        int getStatus(int offset);

        /**
         * Returns the detail message string about the
         * byte associated with specified location.
         * @return  the detail error message string.
         */
        String getMessage(int offset);

    }

    /**
     * Add memory service event listener.
     * @param listener - event listener implementation.
     */
    void addListener(MemoryListener listener);

    /**
     * Remove memory service event listener.
     * @param listener - event listener implementation.
     */
    void removeListener(MemoryListener listener);

    /**
     * Memory event listener is notified when memory context hierarchy
     * changes, and when memory is modified by memory service commands.
     */
    interface MemoryListener {

        /**
         * Called when a new memory access context(s) is created.
         */
        void contextAdded(Context[] contexts);

        /**
         * Called when a new memory access context(s) properties changed.
         */
        void contextChanged(Context[] contexts);

        /**
         * Called when memory access context(s) is removed.
         */
        void contextRemoved(String[] context_ids);

        /**
         * Called when target memory content was changed and clients
         * need to update themselves. Clients, at least, should invalidate
         * corresponding cached memory data.
         * Not every change is notified - it is not possible,
         * only those, which are not caused by normal execution of the debuggee.
         * ‘addr’ and ‘size’ can be null if unknown.
         */
        void memoryChanged(String context_id,
               long[] addr, long[] size);
    }
}