GDBN Remote Serial Protocol
***************************

Overview
========

There may be occasions when you need to know something about the protocol---for example, if there is only one serial port to your target machine, you might want your program to do something special if it recognizes a packet meant for GDBN.

In the examples below, <- and -> are used to indicate transmitted and received data respectfully.

All GDBN commands and responses (other than acknowledgments) are sent as a PACKET.  A PACKET is introduced with the character $, the actual PACKET-DATA, and the terminating character # followed by a two-digit CHECKSUM:

     $PACKET-DATA#CHECKSUM

The two-digit CHECKSUM is computed as the modulo 256 sum of all characters between the leading $ and the trailing # (an eight bit unsigned checksum).

Implementors should note that prior to GDBN 5.0 the protocol specification also included an optional two-digit SEQUENCE-ID:

     $SEQUENCE-ID:PACKET-DATA#CHECKSUM

That SEQUENCE-ID was appended to the acknowledgment.  GDBN has never output SEQUENCE-IDs.  Stubs that handle packets added since GDBN 5.0 must not accept SEQUENCE-ID.

When either the host or the target machine receives a packet, the first response expected is an acknowledgment: either + (to indicate the package was received correctly) or - (to request retransmission):

     <- $PACKET-DATA#CHECKSUM
     -> +

The host (GDBN) sends COMMANDs, and the target (the debugging stub incorporated in your program) sends a RESPONSE.  In the case of step and continue COMMANDs, the response is only sent when the operation has completed (the target has again stopped).

PACKET-DATA consists of a sequence of characters with the exception of # and $ (see X packet for additional exceptions).

Fields within the packet should be separated using , ; or :.  Except where otherwise noted all numbers are represented in HEX with leading zeros suppressed.

Implementors should note that prior to GDBN 5.0, the character : could not appear as the third character in a packet (as it would potentially conflict with the SEQUENCE-ID).

Response DATA can be run-length encoded to save space.  A * means that the next character is an ASCII encoding giving a repeat count which stands for that many repetitions of the character preceding the *.  The encoding is n+29, yielding a printable character where n >=3 (which is where rle starts to win).  The printable characters $, #, + and - or with a numeric value greater than 126 should not be used.

Some remote systems have used a different run-length encoding mechanism loosely refered to as the cisco encoding.  Following the * character are two hex digits that indicate the size of the packet.

So:

     "0* "

means the same as "0000".

The error response returned for some packets includes a two character error number.  That number is not well defined.

For any COMMAND not supported by the stub, an empty response ($#00) should be returned.  That way it is possible to extend the protocol.  A newer GDBN can tell if a packet is supported based on that response.

A stub is required to support the g, G, m, M, c, and s COMMANDs.  All other COMMANDs are optional.

Packets
=======

The following table provides a complete list of all currently defined COMMANDs and their corresponding response DATA.

! --- extended mode

     Enable extended mode.  In extended mode, the remote server is made persistent.  The R packet is used to restart the program being debugged.

     Reply:

     OK
          The remote target both supports and has enabled extended mode.

? --- last signal

     Indicate the reason the target halted.  The reply is the same as for step and continue.

     Reply: see Stop Reply Packets, for the reply specifications.

a --- reserved

     Reserved for future use.

AARGLEN,ARGNUM,ARG,... ---  set program arguments (reserved)

     Initialized argv[] array passed into program. ARGLEN specifies the number of bytes in the hex encoded byte stream ARG. See gdbserver for more details.

     Reply:

     OK

     ENN

bBAUD --- set baud (deprecated)

     Change the serial line speed to BAUD.

     JTC: When does the transport layer state change?  When it's received, or after the ACK is transmitted.  In either case, there are problems if the command or the acknowledgment packet is dropped.

     Stan: If people really wanted to add something like this, and get it working for the first time, they ought to modify ser-unix.c to send some kind of out-of-band message to a specially-setup stub and have the switch happen "in between" packets, so that from remote protocol's point of view, nothing actually happened.

BADDR,MODE --- set breakpoint (deprecated)

     Set (MODE is S) or clear (MODE is C) a breakpoint at ADDR.  This has been replaced by the Z and z packets.

cADDR --- continue

     ADDR is address to resume.  If ADDR is omitted, resume at current address.

     Reply: see Stop Reply Packets, for the reply specifications.

CSIG;ADDR --- continue with signal

     Continue with signal SIG (hex signal number).  If ;ADDR is omitted, resume at same address.

     Reply: see Stop Reply Packets, for the reply specifications.

d --- toggle debug (deprecated)

     Toggle debug flag.

D --- detach

     Detach GDBN from the remote system.  Sent to the remote target before GDBN disconnects.

     Reply:

     no response
          GDBN does not check for any response after sending this packet.

e --- reserved

     Reserved for future use.

E --- reserved

     Reserved for future use.

f --- reserved

     Reserved for future use.

F --- reserved

     Reserved for future use.

g --- read registers

     Read general registers.

     Reply:

     XX...
          Each byte of register data is described by two hex digits.  The bytes with the register are transmitted in target byte order.  The size of each register and their position within the g PACKET are determined by the GDBN internal macros REGISTER_RAW_SIZE and REGISTER_NAME macros.  The specification of several standard g packets is specified below.

     ENN
          for an error.

GXX... --- write regs

     See read registers packet, for a description of the XX... data.

     Reply:

     OK
          for success

     ENN
          for an error

h --- reserved

     Reserved for future use.

HCT... --- set thread

     Set thread for subsequent operations (m, M, g, G, et.al.).  C depends on the operation to be performed: it should be c for step and continue operations, g for other operations.  The thread designator T... may be -1, meaning all the threads, a thread number, or zero which means pick any thread.

     Reply:

     OK
          for success

     ENN
          for an error

iADDR,NNN --- cycle step (draft)

     Step the remote target by a single clock cycle.  If ,NNN is present, cycle step NNN cycles.  If ADDR is present, cycle step starting at that address.

I --- signal then cycle step (reserved)

     See step with signal packet.  See cycle step packet.

j --- reserved

     Reserved for future use.

J --- reserved

     Reserved for future use.

k --- kill request

     FIXME: There is no description of how to operate when a specific thread context has been selected (i.e. does 'k' kill only that thread?).

K --- reserved

     Reserved for future use.

l --- reserved

     Reserved for future use.

L --- reserved

     Reserved for future use.

mADDR,LENGTH --- read memory

     Read LENGTH bytes of memory starting at address ADDR. Neither GDBN nor the stub assume that sized memory transfers are assumed using word alligned accesses. FIXME: A word aligned memory transfer mechanism is needed.

     Reply:

     XX...
          XX... is mem contents. Can be fewer bytes than requested if able to read only part of the data.  Neither GDBN nor the stub assume that sized memory transfers are assumed using word alligned accesses. FIXME: A word aligned memory transfer mechanism is needed.

     ENN
          NN is errno

MADDR,LENGTH:XX... --- write mem

     Write LENGTH bytes of memory starting at address ADDR. XX... is the data.

     Reply:

     OK
          for success

     ENN
          for an error (this includes the case where only part of the data was written).

n --- reserved

     Reserved for future use.

N --- reserved

     Reserved for future use.

o --- reserved

     Reserved for future use.

O --- reserved

     Reserved for future use.

pN... --- read reg (reserved)

     See write register packet.

     Reply:

     R....
          The hex encoded value of the register in target byte order.

PN...=R... --- write register

     Write register N... with value R..., which contains two hex digits for each byte in the register (target byte order).

     Reply:

     OK
          for success

     ENN
          for an error

qQUERY --- general query

     Request info about QUERY.  In general GDBN queries have a leading upper case letter.  Custom vendor queries should use a company prefix (in lower case) ex: qfsf.var.  QUERY may optionally be followed by a , or ; separated list.  Stubs must ensure that they match the full QUERY name.

     Reply:

     XX...
          Hex encoded data from query.  The reply can not be empty.

     ENN
          error reply

          Indicating an unrecognized QUERY.

QVAR=VAL --- general set

     Set value of VAR to VAL.

     See general query packet, for a discussion of naming conventions.

r --- reset (deprecated)

     Reset the entire system.

RXX --- remote restart

     Restart the program being debugged.  XX, while needed, is ignored. This packet is only available in extended mode.

     Reply:

     no reply
          The R packet has no reply.

sADDR --- step

     ADDR is address to resume.  If ADDR is omitted, resume at same address.

     Reply: see Stop Reply Packets, for the reply specifications.

SSIG;ADDR --- step with signal

     Like C but step not continue.

     Reply: see Stop Reply Packets, for the reply specifications.

tADDR:PP,MM --- search

     Search backwards starting at address ADDR for a match with pattern PP and mask MM.  PP and MM are 4 bytes. ADDR must be at least 3 digits.

TXX --- thread alive

     Find out if the thread XX is alive.

     Reply:

     OK
          thread is still alive

     ENN
          thread is dead

u --- reserved

     Reserved for future use.

U --- reserved

     Reserved for future use.

v --- reserved

     Reserved for future use.

V --- reserved

     Reserved for future use.

w --- reserved

     Reserved for future use.

W --- reserved

     Reserved for future use.

x --- reserved

     Reserved for future use.

XADDR,LENGTH:XX... --- write mem (binary)

     ADDR is address, LENGTH is number of bytes, XX... is binary data.  The characters $, #, and 0x7d are escaped using 0x7d.

     Reply:

     OK
          for success

     ENN
          for an error

y --- reserved

     Reserved for future use.

Y reserved

     Reserved for future use.

zT,ADDR,LENGTH --- remove break or watchpoint (draft)

     See insert breakpoint or watchpoint packet.

ZT,ADDR,LENGTH --- insert break or watchpoint (draft)

     T is type: 0 - software breakpoint, 1 - hardware breakpoint, 2 --- write watchpoint, 3 - read watchpoint, 4 - access watchpoint; ADDR is address; LENGTH is in bytes.  For a software breakpoint, LENGTH specifies the size of the instruction to be patched.  For hardware breakpoints and watchpoints LENGTH specifies the memory region to be monitored.  To avoid potential problems with duplicate packets, the operations should be implemented in an idempotent way.

     Reply:

     ENN
          for an error

     OK
          for success

          If not supported.

Stop Reply Packets
==================

The C, c, S, s and ? packets can receive any of the below as a reply.  In the case of the C, c, S and s packets, that reply is only returned when the target halts.  In the below the exact meaning of signal number is poorly defined.  In general one of the UNIX signal numbering conventions is used.

SAA
     AA is the signal number

TAAN...:R...;N...:R...;N...:R...;

     AA = two hex digit signal number; N... = register number (hex), R...  = target byte ordered register contents, size defined by REGISTER_RAW_SIZE; N... = thread, R... = thread process ID, this is a hex integer; N... = (watch | rwatch | awatch, R... = data address, this is a hex integer; N... = other string not starting with valid hex digit. GDBN should ignore this N..., R... pair and go on to the next.  This way we can extend the protocol.

WAA

     The process exited, and AA is the exit status.  This is only applicable to certain targets.

XAA

     The process terminated with signal AA.

NAA;T...;D...;B... (obsolete)

     AA = signal number; T... = address of symbol _start; D... = base of data section; B... = base of bss section.  Note: only used by Cisco Systems targets. The difference between this reply and the qOffsets query is that the N packet may arrive spontaneously whereas the qOffsets is a query initiated by the host debugger.

OXX...

     XX... is hex encoding of ASCII data.  This can happen at any time while the program is running and the debugger should continue to wait for W, T, etc.

General Query Packets
=====================

The following set and query packets have already been defined.

qC --- current thread

     Return the current thread id.

     Reply:

     QCPID
          Where PID is a HEX encoded 16 bit process id.

     *
          Any other reply implies the old pid.

qfThreadInfo -- all thread ids

     qsThreadInfo

     Obtain a list of active thread ids from the target (OS).  Since there may be too many active threads to fit into one reply packet, this query works iteratively: it may require more than one query/reply sequence to obtain the entire list of threads.  The first query of the sequence will be the qfThreadInfo query; subsequent queries in the sequence will be the qsThreadInfo query.

     NOTE: replaces the qL query (see below).

     Reply:

     mID
          A single thread id

     mID,ID...
          a comma-separated list of thread ids

     l
          (lower case 'el') denotes end of list.

     In response to each query, the target will reply with a list of one or more thread ids, in big-endian hex, separated by commas.  GDBN will respond to each reply with a request for more thread ids (using the qs form of the query), until the target responds with l (lower-case el, for 'last').

qThreadExtraInfo,ID --- extra thread info

     Where ID is a thread-id in big-endian hex.  Obtain a printable string description of a thread's attributes from the target OS.  This string may contain anything that the target OS thinks is interesting for GDBN to tell the user about the thread.  The string is displayed in GDBN's info threads display.  Some examples of possible thread extra info strings are ``Runnable'', or ``Blocked on Mutex''.

     Reply:

     XX...
          Where XX... is a hex encoding of ASCII data, comprising the printable string containing the extra information about the thread's attributes.

qLSTARTFLAGTHREADCOUNTNEXTTHREAD --- query LIST or THREADLIST (deprecated)

     Obtain thread information from RTOS.  Where: STARTFLAG (one hex digit) is one to indicate the first query and zero to indicate a subsequent query; THREADCOUNT (two hex digits) is the maximum number of threads the response packet can contain; and NEXTTHREAD (eight hex digits), for subsequent queries (STARTFLAG is zero), is returned in the response as ARGTHREAD.

     NOTE: this query is replaced by the qfThreadInfo query (see above).

     Reply:

     qMCOUNTDONEARGTHREADTHREAD...
          Where: COUNT (two hex digits) is the number of threads being returned; DONE (one hex digit) is zero to indicate more threads and one indicates no further threads; ARGTHREADID (eight hex digits) is NEXTTHREAD from the request packet; THREAD... is a sequence of thread IDs from the target.  THREADID (eight hex digits).  See remote.c:parse_threadlist_response().

qCRC:ADDR,LENGTH --- compute CRC of memory block

     Reply:

     ENN
          An error (such as memory fault)

     CCRC32
          A 32 bit cyclic redundancy check of the specified memory region.

qOffsets --- query sect offs

     Get section offsets that the target used when re-locating the downloaded image.  Note: while a Bss offset is included in the
     response, GDBN ignores this and instead applies the Data
     offset to the Bss section.

     Reply:

     Text=XXX;Data=YYY;Bss=ZZZ

qPMODETHREADID --- thread info request

     Returns information on THREADID.  Where: MODE is a hex encoded 32 bit mode; THREADID is a hex encoded 64 bit thread ID.

     Reply:

     *

     See remote.c:remote_unpack_thread_info_response().

qRcmd,COMMAND --- remote command

     COMMAND (hex encoded) is passed to the local interpreter for execution.  Invalid commands should be reported using the output string. Before the final result packet, the target may also respond with a number of intermediate OOUTPUT console output packets. Implementors should note that providing access to a stubs's interpreter may have security implications.

     Reply:

     OK
          A command response with no output.

     OUTPUT
          A command response with the hex encoded output string OUTPUT.

     ENN
          Indicate a badly formed request.

          When qRcmd is not recognized.

qSymbol:: --- symbol lookup

     Notify the target that GDBN is prepared to serve symbol lookup requests.  Accept requests from the target for the values of symbols.

     Reply:

     OK
          The target does not need to look up any (more) symbols.

     qSymbol:SYM_NAME
          The target requests the value of symbol SYM_NAME (hex encoded). GDBN may provide the value by using the qSymbol:SYM_VALUE:SYM_NAME message, described below.

qSymbol:SYM_VALUE:SYM_NAME --- symbol value

     Set the value of SYM_NAME to SYM_VALUE.

     SYM_NAME (hex encoded) is the name of a symbol whose value the target has previously requested.

     SYM_VALUE (hex) is the value for symbol SYM_NAME.  If GDBN cannot supply a value for SYM_NAME, then this field will be empty.

     Reply:

     OK
          The target does not need to look up any (more) symbols.

     qSymbol:SYM_NAME
          The target requests the value of a new symbol SYM_NAME (hex encoded).  GDBN will continue to supply the values of symbols (if available), until the target ceases to request them.

Register Packet Format
======================

The following g/G packets have previously been defined. In the below, some thirty-two bit registers are transferred as sixty-four bits.  Those registers should be zero/sign extended (which?) to fill the space allocated.  Register bytes are transfered in target byte order.  The two nibbles within a register byte are transfered most-significant - least-significant.

MIPS32

     All registers are transfered as thirty-two bit quantities in the order: 32 general-purpose; sr; lo; hi; bad; cause; pc; 32 floating-point registers; fsr; fir; fp.

MIPS64

     All registers are transfered as sixty-four bit quantities (including thirty-two bit registers such as sr).  The ordering is the same as MIPS32.

Examples
========

Example sequence of a target being re-started.  Notice how the restart does not get any direct output:

     <- R00
     -> +
     target restarts
     <- ?
     -> +
     -> T001:1234123412341234
     <- +

Example sequence of a target being stepped by a single instruction:

     <- G1445...
     -> +
     <- s
     -> +
     time passes
     -> T001:1234123412341234
     <- +
     <- g
     -> +
     -> 1455...
     <- +