You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "anchao (via GitHub)" <gi...@apache.org> on 2023/07/06 09:31:24 UTC

[GitHub] [nuttx] anchao commented on a diff in pull request #9706: libs/libc:Add support for gdb rsp protocol

anchao commented on code in PR #9706:
URL: https://github.com/apache/nuttx/pull/9706#discussion_r1254120654


##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,
+                            FAR const unsigned char *data, size_t data_len);
+typedef int (*gdb_dec_func)(FAR unsigned const char *buf, size_t buf_len,
+                            FAR unsigned char *data, size_t data_len);
+
+/****************************************************************************
+ *  GDB Remote Serial Protocol
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* System functions, supported by all stubs */
+
+static int gdb_getchar(FAR struct gdb_state *state);
+static int gdb_putchar(FAR struct gdb_state *state, int ch);
+
+/* Communication functions */
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len);
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len);
+
+/* Packet functions */
+
+static int gdb_send_packet(FAR struct gdb_state *state);
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len);
+static int gdb_checksum(FAR const unsigned char *buf, size_t len);
+static int gdb_recv_ack(FAR struct gdb_state *state);
+
+/* Data encoding/decoding */
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len);
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+
+/* Packet creation helpers */
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state);
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal);
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error);
+
+/* Command functions */
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc);
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * String Processing Helper Functions
+ ****************************************************************************/
+
+static uintptr_t gdb_strtol(FAR char *str, size_t len, int base,
+                            FAR char **endptr)
+{
+  char tmp = str[len];
+  uintptr_t ret;
+
+  /* Ensure string is null terminated
+   * Gdb rsp use \x48 ('0') make sure it's over
+   * */
+
+  str[len] = '\0';
+  ret =  strtoul(str, (FAR char **)endptr, base);
+  str[len] = tmp;
+
+  return ret;
+}
+
+/****************************************************************************
+ * Packet Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_recv_ack
+ *
+ * Description:
+ *   Receive a packet acknowledgment.
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if an ACK (+) was received
+ *   1       if a NACK (-) was received
+ *   -EINVAL if a bad response was received
+ *
+ ****************************************************************************/
+
+static int gdb_recv_ack(FAR struct gdb_state *state)
+{
+  int response;
+
+  /* Wait for packet ack */
+
+  switch (response = gdb_getchar(state))
+    {
+      case '+':
+
+        /* Packet acknowledged */
+
+        return 0;
+      case '-':
+
+        /* Packet negative acknowledged */
+
+        return 1;
+      default:
+
+        /* Bad response! */
+
+        GDB_DEBUG("received bad packet response: 0x%2x\n", response);
+        return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Calculate 8-bit checksum of a buffer.
+ *
+ * Input Parameters:
+ *   buf - The buffer to checksum.
+ *
+ * Returned Value:
+ *   8-bit checksum
+ *
+ ****************************************************************************/
+
+static int gdb_checksum(FAR const unsigned char *buf, size_t len)
+{
+  unsigned char csum = 0;
+
+  while (len--)
+    {
+      csum += *buf++;
+    }
+
+  return csum;
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Transmits a packet of data.
+ *   Packets are of the form: $<packet-data>#<checksum>
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if the packet was transmitted and acknowledged.
+ *   1       if the packet was transmitted but not acknowledged.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_packet(FAR struct gdb_state *state)
+{
+  unsigned char buf[3];
+  unsigned char csum;
+  int ret;
+
+  /* Send packet start */
+
+  ret = gdb_putchar(state, '$');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("-> ");
+      for (p = 0; p < state->pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Send packet data */
+
+  ret = gdb_write(state, state->pkt_buf, state->pkt_len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Send the checksum */
+
+  buf[0] = '#';
+  csum = gdb_checksum(state->pkt_buf, state->pkt_len);
+
+  ret = gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_write(state, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return gdb_recv_ack(state);
+}
+
+/****************************************************************************
+ * Name: gdb_recv_packet
+ *
+ * Description:
+ *   Receives a packet of data, assuming a 7-bit clean connection.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   pkt_len - The length of the packet received.
+ *
+ * Returned Value:
+ *   0          if the packet was received.
+ *   -EIO       if the packet was not received.
+ *   -EOVERFLOW if the packet was too long.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len)
+{
+  unsigned char expected_csum;
+  unsigned char actual_csum;
+  unsigned char buf[2];
+  int ret;
+
+  /* Wait for packet start */
+
+  actual_csum = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else if (ret == '$')
+        {
+          /* Detected start of packet. */
+
+          break;
+        }
+    }
+
+  /* Read until checksum */
+
+  *pkt_len = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          /* Error receiving character */
+
+          return ret;
+        }
+      else if (ret == '#')
+        {
+          /* End of packet */
+
+          break;
+        }
+      else
+        {
+          /* Check for space */
+
+          if (*pkt_len >= state->pkt_buf_len)
+            {
+              GDB_DEBUG("packet buffer overflow\n");
+              return -EOVERFLOW;
+            }
+
+          /* Store character and update checksum */
+
+          state->pkt_buf[(*pkt_len)++] = (char) ret;
+        }
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("<- ");
+      for (p = 0; p < *pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Receive the checksum */
+
+  ret = gdb_read(state, buf, sizeof(buf), 2);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_dec_hex(buf, 2, &expected_csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Verify checksum */
+
+  actual_csum = gdb_checksum(state->pkt_buf, *pkt_len);
+  if (actual_csum != expected_csum)
+    {
+      /* Send packet nack */
+
+      GDB_DEBUG("received packet with bad checksum\n");
+      gdb_putchar(state, '-');
+      return -EIO;
+    }
+
+  /* Send packet ack */
+
+  gdb_putchar(state, '+');
+  return 0;
+}
+
+/****************************************************************************
+ * Data Encoding/Decoding
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_enc_hex
+ *
+ * Description:
+ *   Encode data to its hex-value representation in a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer to store the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The data to encode.
+ *   data_len - The length of the data to encode.
+ *
+ * Returned Value:
+ *    The number of bytes written to buf on success.
+ *    -EINVAL if the buffer is too small.
+ *
+ ****************************************************************************/
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len)
+{
+  size_t pos;
+
+  if (buf_len < data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      itoa((data[pos] >> 4) & 0xf, (FAR char *)buf++, 16);
+      itoa((data[pos]) & 0xf, (FAR char *)buf++, 16);
+    }
+
+  return data_len * 2;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *   -EINVAL if the buffer is too small.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t pos;
+  int tmp;
+  char ch[2] =
+    {
+      0, 0
+    };
+
+  if (buf_len != data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      /* Decode high nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] = tmp << 4;
+
+      /* Decode low nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] |= tmp;
+    }
+
+  return data_len;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex2string
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a string.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex2string(FAR const unsigned char *buf, size_t buf_len,
+                              FAR char *data, size_t data_len)
+{
+  for (int i = 0; i < buf_len; i += 2)
+    {
+      char hex_byte[3] =
+        {
+          buf[i], buf[i + 1], '\0'
+        };
+
+      int byte = strtol(hex_byte, NULL, 16);
+
+      data[i / 2] = (char)byte;
+    }
+
+  data[buf_len / 2] = '\0';
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_bin
+ *
+ * Description:
+ *   Decode data from its bin-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *     The number of bytes written to data on success.
+ *    -EOVERFLOW if the output buffer is too small.
+ *    -EINVAL if the input buffer is invalid.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t buf_pos;
+  size_t data_pos;
+
+  for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++)
+    {
+      if (data_pos >= data_len)
+        {
+          /* Output buffer overflow */
+
+          GDB_ASSERT();
+          return -EOVERFLOW;
+        }
+
+      if (buf[buf_pos] == '}')
+        {
+          /* The next byte is escaped! */
+
+          if (buf_pos + 1 >= buf_len)
+            {
+              /* There's an escape character, but no escaped character
+               * following the escape character.
+               */
+
+              GDB_ASSERT();
+              return -EINVAL;
+            }
+
+          buf_pos += 1;
+          data[data_pos++] = buf[buf_pos] ^ 0x20;
+        }
+      else
+        {
+          data[data_pos++] = buf[buf_pos];
+        }
+    }
+
+  return data_pos;
+}
+
+/****************************************************************************
+ * Command Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_mem_read
+ *
+ * Description:
+ *   Read from memory and encode into buf.
+ *
+ * Input Parameters:
+ *   state   - The state structure.
+ *   buf     - The buffer to store the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to read from.
+ *   len     - The length of the data to read.
+ *   enc     - The encoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to buf on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return enc(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Name: gdb_mem_write
+ *
+ * Description:
+ *   Write to memory from encoded buf
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to write to.
+ *   len     - The length of the data to write.
+ *   dec     - The decoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to addr on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return dec(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Packet Creation Helpers
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_send_ok_packet
+ *
+ * Description:
+ *   Send OK packet.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state)
+{
+  state->pkt_buf[0] = 'O';
+  state->pkt_buf[1] = 'K';
+  state->pkt_len = 2;
+
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_signal_packet
+ *
+ * Description:
+ *   Send a signal packet (S AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   signal  - The signal to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'S';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &signal, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_error_packet
+ *
+ * Description:
+ *   Send a error packet (E AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   error   - The error to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'E';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &error, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Communication Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_write
+ *
+ * Description:
+ *   Write a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the data to write.
+ *   len     - The length of the data to write.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len)
+{
+  return state->send(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: gdb_read
+ *
+ * Description:
+ *   Read a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer to store the read data.
+ *   buf_len - The length of the buffer.
+ *   len     - The length of the data to read.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len)
+{
+  if (buf_len < len)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  return state->recv(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: get_register
+ *
+ * Description:
+ *   Get the registers of the specified task.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ ****************************************************************************/
+
+static void get_register(FAR struct gdb_state *state)
+{
+  FAR unsigned int *regs = state->registers;
+  FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
+  FAR uint8_t *reg;
+  int i;
+
+  reg = (FAR uint8_t *)tcb->xcp.regs;
+  if (state->pid == getpid())
+    {
+      reg = (FAR uint8_t *)CURRENT_REGS;
+    }
+
+  for (i = 0; i < g_tcbinfo.total_num; i++)
+    {
+      if (g_tcbinfo.reg_off.p[i] == UINT16_MAX)
+        {
+          regs[i] = 0;
+        }
+      else
+        {
+          regs[i] = *(FAR unsigned int *)(reg + g_tcbinfo.reg_off.p[i]);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_read_register_handle
+ *
+ * Description:
+ *   Read Registers Command Format: g.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  get_register(state);
+
+  /* Encode registers */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                    (FAR unsigned char *)(&(state->registers[0])),
+                    state->size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_register_handle
+ *
+ * Description:
+ *   Write Registers Command Format: G XX...
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ *   Note: This function is not really change the register values.
+ *
+ ****************************************************************************/
+
+static int gdb_write_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  ret = gdb_dec_hex(state->pkt_buf + 1, state->pkt_len - 1,
+                    (FAR unsigned char *)&(state->registers),
+                    sizeof(state->registers));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_one_register_handle
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_one_register_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  get_register(state);
+  if (addr >= state->size)
+    {
+      return 0;
+    }
+
+  /* Read Register */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                   (FAR unsigned char *)&(state->registers[addr]),
+                   sizeof(state->registers[addr]));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_read_mem_handle
+ *
+ * Description:
+ *   Read Memory Command Format: m addr,length
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes read if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+
+  /* Read Memory */
+
+  ret = gdb_mem_read(state, state->pkt_buf, state->pkt_buf_len,
+                     addr, length, gdb_enc_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_mem_handle
+ *
+ * Description:
+ *   Write Memory Command Format: M addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_bin_mem_handle
+ *
+ * Description:
+ *   Write Memory (Binary) Command Format: X addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_bin_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_bin);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_get_thread
+ *
+ * Description:
+ *   Get the all thread id.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct gdb_state *state = arg;
+  char pid = tcb->pid;
+
+  itoa((pid >> 4) & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  itoa(pid & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  state->pkt_buf[state->pkt_len++] = ',';
+}
+
+/****************************************************************************
+ * Name: gdb_query_handle
+ *
+ * Description:
+ *  The query packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_query_handle(FAR struct gdb_state *state)
+{
+  static const char thread_extra_info[] = "ThreadExtraInfo";
+  static const char fthread_info[] = "fThreadInfo";
+  static const char sthread_info[] = "sThreadInfo";
+  static const char r_cmd[] = "Rcmd";
+
+  if (memcmp(&state->pkt_buf[1], fthread_info,
+              sizeof(fthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'm';
+      state->pkt_len = 1;
+      nxsched_foreach(gdb_get_thread, state);
+      state->pkt_len--;
+      state->pkt_buf[state->pkt_len] = 0;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], sthread_info,
+                   sizeof(sthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'l';
+      state->pkt_len = 1;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], thread_extra_info,
+                   sizeof(thread_extra_info) - 1) == 0)
+    {
+      char thread_info[128];
+      FAR struct tcb_s *tcb;
+      int ret;
+      int pid;
+      FAR static const char *thread_state[] =
+        {
+          "Invalid",
+          "Waiting,Unlock",
+          "Ready",
+#ifdef CONFIG_SMP
+          "Assigned",
+#endif
+          "Running",
+          "Inactive",
+          "Waiting,Semaphore",
+          "Waiting,Signal"
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)
+          , "Waiting,MQ empty"
+          , "Waiting,MQ full"
+#endif
+#ifdef CONFIG_PAGING
+          , "Waiting,Paging fill"
+#endif
+#ifdef CONFIG_SIG_SIGSTOP_ACTION
+          , "Stopped"
+#endif
+        };
+
+      state->ptr_next += sizeof(thread_extra_info) + 1;
+      token_expect_integer_arg(state, pid);
+      tcb = nxsched_get_tcb(pid);
+      if (tcb == NULL)
+        {
+          return -EINVAL;
+        }
+
+#if CONFIG_TASK_NAME_SIZE > 0
+      sprintf(thread_info, "Name: %s, State: %s, Priority: %d, Stack: %d",
+              tcb->name, thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#else
+      sprintf(thread_info, "State: %s, Priority: %d, Stack: %d",
+              thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#endif
+
+      ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                        (FAR unsigned char *)thread_info,
+                        strlen(thread_info));
+
+      state->pkt_len = ret;
+      gdb_send_packet(state);
+    }
+  else if (state->monitor_cmd != NULL && memcmp(&state->pkt_buf[1], r_cmd,
+                                                sizeof(r_cmd) - 1) == 0)
+    {
+      /* skip the 'Rcmd,' */
+
+      int len = state->pkt_len - sizeof(r_cmd) - 1;
+      char cmd[64];
+      int ret;
+
+      gdb_dec_hex2string(&state->pkt_buf[sizeof(r_cmd) + 1], len, cmd,
+                         len / 2);
+      ret = state->monitor_cmd(state, cmd);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else
+        {
+          gdb_send_ok_packet(state);
+        }
+    }
+  else
+    {
+      return -EPROTONOSUPPORT;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_is_thread_active_handle
+ *
+ * Description:
+ *  The is thread active packet is used by GDB to request information from
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_is_thread_active_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next = state->pkt_buf + 1;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_thread_context_handle
+ *
+ * Description:
+ *  The thread context packet is used by GDB to request information from
+ *  the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_thread_context_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next += 2;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_send_stop_handle
+ *
+ * Description:
+ *  The stop packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_stop_handle(FAR struct gdb_state *state)
+{
+  return gdb_send_signal_packet(state, 0x00);
+}
+
+/****************************************************************************
+ * Name: gdb_putchar
+ *
+ * Description:
+ *   Put a character to the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   ch      - The character to be sent.
+ *
+ * Returned Value:
+ *   The character sent to the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_putchar(FAR struct gdb_state *state, int ch)
+{
+  unsigned char tmp = ch & 0xff;
+  ssize_t ret;
+
+  ret = state->send(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return ch;
+}
+
+/****************************************************************************
+ * Name: gdb_getchar
+ *
+ * Description:
+ *  Get a character from the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The character read from the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_getchar(FAR struct gdb_state *state)
+{
+  unsigned char tmp;
+  ssize_t ret;
+
+  ret = state->recv(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return tmp;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd)
+{
+  struct gdb_state *state = kmm_zalloc(sizeof(*state) +
+                                       g_tcbinfo.basic_num *
+                                       sizeof(unsigned int));
+  if (state == NULL)
+    {
+      return NULL;
+    }
+
+  state->size = g_tcbinfo.basic_num * sizeof(unsigned int);
+  state->pkt_buf_len = sizeof(state->pkt_buf);
+  state->send = send;
+  state->recv = recv;
+  state->priv = priv;
+  state->monitor_cmd = monitor_cmd;
+
+  return state;
+}
+
+/****************************************************************************
+ * Name: gdb_state_uninit
+ *
+ * Description:
+ *   Uninitialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+void gdb_state_uninit(struct gdb_state *state)
+{
+  if (state == NULL)
+    {
+      return;
+    }
+
+  kmm_free(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_conmsg_packet
+ *
+ * Description:
+ *   Send a message to the GDB console (via O XX... packet).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_send_conmsg_packet(FAR struct gdb_state *state, FAR const char *msg)
+{
+  int ret;
+  int len;
+
+  if (state == NULL || msg == NULL)
+    {
+      return -EINVAL;
+    }
+
+  len = strlen(msg);
+  if (len > state->pkt_buf_len - 1)
+    {
+      return -EOVERFLOW;
+    }
+
+  state->pkt_buf[0] = 'O';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    (FAR unsigned char *)msg, len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_process
+ *
+ * Description:
+ *   Main debug loop. Handles commands.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_process(FAR struct gdb_state *state)
+{
+  int ret = 0;
+  int i;
+  static struct gdb_handle_cmd handle_cmd[] =
+    {
+      {
+        .handle = gdb_read_register_handle,
+        .cmd = 'g'

Review Comment:
   add some comment for each command entry



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,
+                            FAR const unsigned char *data, size_t data_len);
+typedef int (*gdb_dec_func)(FAR unsigned const char *buf, size_t buf_len,
+                            FAR unsigned char *data, size_t data_len);
+
+/****************************************************************************
+ *  GDB Remote Serial Protocol
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* System functions, supported by all stubs */
+
+static int gdb_getchar(FAR struct gdb_state *state);
+static int gdb_putchar(FAR struct gdb_state *state, int ch);
+
+/* Communication functions */
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len);
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len);
+
+/* Packet functions */
+
+static int gdb_send_packet(FAR struct gdb_state *state);
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len);
+static int gdb_checksum(FAR const unsigned char *buf, size_t len);
+static int gdb_recv_ack(FAR struct gdb_state *state);
+
+/* Data encoding/decoding */
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len);
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+
+/* Packet creation helpers */
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state);
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal);
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error);
+
+/* Command functions */
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc);
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * String Processing Helper Functions
+ ****************************************************************************/
+
+static uintptr_t gdb_strtol(FAR char *str, size_t len, int base,
+                            FAR char **endptr)
+{
+  char tmp = str[len];
+  uintptr_t ret;
+
+  /* Ensure string is null terminated
+   * Gdb rsp use \x48 ('0') make sure it's over
+   * */
+
+  str[len] = '\0';
+  ret =  strtoul(str, (FAR char **)endptr, base);
+  str[len] = tmp;
+
+  return ret;
+}
+
+/****************************************************************************
+ * Packet Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_recv_ack
+ *
+ * Description:
+ *   Receive a packet acknowledgment.
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if an ACK (+) was received
+ *   1       if a NACK (-) was received
+ *   -EINVAL if a bad response was received
+ *
+ ****************************************************************************/
+
+static int gdb_recv_ack(FAR struct gdb_state *state)
+{
+  int response;
+
+  /* Wait for packet ack */
+
+  switch (response = gdb_getchar(state))
+    {
+      case '+':
+
+        /* Packet acknowledged */
+
+        return 0;
+      case '-':
+
+        /* Packet negative acknowledged */
+
+        return 1;
+      default:
+
+        /* Bad response! */
+
+        GDB_DEBUG("received bad packet response: 0x%2x\n", response);
+        return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Calculate 8-bit checksum of a buffer.
+ *
+ * Input Parameters:
+ *   buf - The buffer to checksum.
+ *
+ * Returned Value:
+ *   8-bit checksum
+ *
+ ****************************************************************************/
+
+static int gdb_checksum(FAR const unsigned char *buf, size_t len)
+{
+  unsigned char csum = 0;
+
+  while (len--)
+    {
+      csum += *buf++;
+    }
+
+  return csum;
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Transmits a packet of data.
+ *   Packets are of the form: $<packet-data>#<checksum>
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if the packet was transmitted and acknowledged.
+ *   1       if the packet was transmitted but not acknowledged.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_packet(FAR struct gdb_state *state)
+{
+  unsigned char buf[3];
+  unsigned char csum;
+  int ret;
+
+  /* Send packet start */
+
+  ret = gdb_putchar(state, '$');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("-> ");
+      for (p = 0; p < state->pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Send packet data */
+
+  ret = gdb_write(state, state->pkt_buf, state->pkt_len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Send the checksum */
+
+  buf[0] = '#';
+  csum = gdb_checksum(state->pkt_buf, state->pkt_len);
+
+  ret = gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_write(state, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return gdb_recv_ack(state);
+}
+
+/****************************************************************************
+ * Name: gdb_recv_packet
+ *
+ * Description:
+ *   Receives a packet of data, assuming a 7-bit clean connection.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   pkt_len - The length of the packet received.
+ *
+ * Returned Value:
+ *   0          if the packet was received.
+ *   -EIO       if the packet was not received.
+ *   -EOVERFLOW if the packet was too long.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len)
+{
+  unsigned char expected_csum;
+  unsigned char actual_csum;
+  unsigned char buf[2];
+  int ret;
+
+  /* Wait for packet start */
+
+  actual_csum = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else if (ret == '$')
+        {
+          /* Detected start of packet. */
+
+          break;
+        }
+    }
+
+  /* Read until checksum */
+
+  *pkt_len = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          /* Error receiving character */
+
+          return ret;
+        }
+      else if (ret == '#')
+        {
+          /* End of packet */
+
+          break;
+        }
+      else
+        {
+          /* Check for space */
+
+          if (*pkt_len >= state->pkt_buf_len)
+            {
+              GDB_DEBUG("packet buffer overflow\n");
+              return -EOVERFLOW;
+            }
+
+          /* Store character and update checksum */
+
+          state->pkt_buf[(*pkt_len)++] = (char) ret;
+        }
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("<- ");
+      for (p = 0; p < *pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Receive the checksum */
+
+  ret = gdb_read(state, buf, sizeof(buf), 2);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_dec_hex(buf, 2, &expected_csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Verify checksum */
+
+  actual_csum = gdb_checksum(state->pkt_buf, *pkt_len);
+  if (actual_csum != expected_csum)
+    {
+      /* Send packet nack */
+
+      GDB_DEBUG("received packet with bad checksum\n");
+      gdb_putchar(state, '-');
+      return -EIO;
+    }
+
+  /* Send packet ack */
+
+  gdb_putchar(state, '+');
+  return 0;
+}
+
+/****************************************************************************
+ * Data Encoding/Decoding
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_enc_hex
+ *
+ * Description:
+ *   Encode data to its hex-value representation in a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer to store the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The data to encode.
+ *   data_len - The length of the data to encode.
+ *
+ * Returned Value:
+ *    The number of bytes written to buf on success.
+ *    -EINVAL if the buffer is too small.
+ *
+ ****************************************************************************/
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len)
+{
+  size_t pos;
+
+  if (buf_len < data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      itoa((data[pos] >> 4) & 0xf, (FAR char *)buf++, 16);
+      itoa((data[pos]) & 0xf, (FAR char *)buf++, 16);
+    }
+
+  return data_len * 2;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *   -EINVAL if the buffer is too small.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t pos;
+  int tmp;
+  char ch[2] =
+    {
+      0, 0
+    };
+
+  if (buf_len != data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      /* Decode high nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] = tmp << 4;
+
+      /* Decode low nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] |= tmp;
+    }
+
+  return data_len;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex2string
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a string.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex2string(FAR const unsigned char *buf, size_t buf_len,
+                              FAR char *data, size_t data_len)
+{
+  for (int i = 0; i < buf_len; i += 2)
+    {
+      char hex_byte[3] =
+        {
+          buf[i], buf[i + 1], '\0'
+        };
+
+      int byte = strtol(hex_byte, NULL, 16);
+
+      data[i / 2] = (char)byte;
+    }
+
+  data[buf_len / 2] = '\0';
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_bin
+ *
+ * Description:
+ *   Decode data from its bin-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *     The number of bytes written to data on success.
+ *    -EOVERFLOW if the output buffer is too small.
+ *    -EINVAL if the input buffer is invalid.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t buf_pos;
+  size_t data_pos;
+
+  for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++)
+    {
+      if (data_pos >= data_len)
+        {
+          /* Output buffer overflow */
+
+          GDB_ASSERT();
+          return -EOVERFLOW;
+        }
+
+      if (buf[buf_pos] == '}')
+        {
+          /* The next byte is escaped! */
+
+          if (buf_pos + 1 >= buf_len)
+            {
+              /* There's an escape character, but no escaped character
+               * following the escape character.
+               */
+
+              GDB_ASSERT();
+              return -EINVAL;
+            }
+
+          buf_pos += 1;
+          data[data_pos++] = buf[buf_pos] ^ 0x20;
+        }
+      else
+        {
+          data[data_pos++] = buf[buf_pos];
+        }
+    }
+
+  return data_pos;
+}
+
+/****************************************************************************
+ * Command Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_mem_read
+ *
+ * Description:
+ *   Read from memory and encode into buf.
+ *
+ * Input Parameters:
+ *   state   - The state structure.
+ *   buf     - The buffer to store the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to read from.
+ *   len     - The length of the data to read.
+ *   enc     - The encoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to buf on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return enc(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Name: gdb_mem_write
+ *
+ * Description:
+ *   Write to memory from encoded buf
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to write to.
+ *   len     - The length of the data to write.
+ *   dec     - The decoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to addr on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return dec(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Packet Creation Helpers
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_send_ok_packet
+ *
+ * Description:
+ *   Send OK packet.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state)
+{
+  state->pkt_buf[0] = 'O';
+  state->pkt_buf[1] = 'K';
+  state->pkt_len = 2;
+
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_signal_packet
+ *
+ * Description:
+ *   Send a signal packet (S AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   signal  - The signal to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'S';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &signal, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_error_packet
+ *
+ * Description:
+ *   Send a error packet (E AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   error   - The error to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'E';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &error, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Communication Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_write
+ *
+ * Description:
+ *   Write a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the data to write.
+ *   len     - The length of the data to write.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len)
+{
+  return state->send(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: gdb_read
+ *
+ * Description:
+ *   Read a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer to store the read data.
+ *   buf_len - The length of the buffer.
+ *   len     - The length of the data to read.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len)
+{
+  if (buf_len < len)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  return state->recv(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: get_register
+ *
+ * Description:
+ *   Get the registers of the specified task.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ ****************************************************************************/
+
+static void get_register(FAR struct gdb_state *state)
+{
+  FAR unsigned int *regs = state->registers;
+  FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
+  FAR uint8_t *reg;
+  int i;
+
+  reg = (FAR uint8_t *)tcb->xcp.regs;
+  if (state->pid == getpid())
+    {
+      reg = (FAR uint8_t *)CURRENT_REGS;
+    }
+
+  for (i = 0; i < g_tcbinfo.total_num; i++)
+    {
+      if (g_tcbinfo.reg_off.p[i] == UINT16_MAX)
+        {
+          regs[i] = 0;
+        }
+      else
+        {
+          regs[i] = *(FAR unsigned int *)(reg + g_tcbinfo.reg_off.p[i]);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_read_register_handle
+ *
+ * Description:
+ *   Read Registers Command Format: g.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  get_register(state);
+
+  /* Encode registers */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                    (FAR unsigned char *)(&(state->registers[0])),
+                    state->size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_register_handle
+ *
+ * Description:
+ *   Write Registers Command Format: G XX...
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ *   Note: This function is not really change the register values.
+ *
+ ****************************************************************************/
+
+static int gdb_write_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  ret = gdb_dec_hex(state->pkt_buf + 1, state->pkt_len - 1,
+                    (FAR unsigned char *)&(state->registers),
+                    sizeof(state->registers));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_one_register_handle
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_one_register_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  get_register(state);
+  if (addr >= state->size)
+    {
+      return 0;
+    }
+
+  /* Read Register */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                   (FAR unsigned char *)&(state->registers[addr]),
+                   sizeof(state->registers[addr]));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_read_mem_handle
+ *
+ * Description:
+ *   Read Memory Command Format: m addr,length
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes read if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+
+  /* Read Memory */
+
+  ret = gdb_mem_read(state, state->pkt_buf, state->pkt_buf_len,
+                     addr, length, gdb_enc_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_mem_handle
+ *
+ * Description:
+ *   Write Memory Command Format: M addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_bin_mem_handle
+ *
+ * Description:
+ *   Write Memory (Binary) Command Format: X addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_bin_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_bin);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_get_thread
+ *
+ * Description:
+ *   Get the all thread id.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct gdb_state *state = arg;
+  char pid = tcb->pid;
+
+  itoa((pid >> 4) & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  itoa(pid & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  state->pkt_buf[state->pkt_len++] = ',';
+}
+
+/****************************************************************************
+ * Name: gdb_query_handle
+ *
+ * Description:
+ *  The query packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_query_handle(FAR struct gdb_state *state)
+{
+  static const char thread_extra_info[] = "ThreadExtraInfo";
+  static const char fthread_info[] = "fThreadInfo";
+  static const char sthread_info[] = "sThreadInfo";
+  static const char r_cmd[] = "Rcmd";
+
+  if (memcmp(&state->pkt_buf[1], fthread_info,
+              sizeof(fthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'm';
+      state->pkt_len = 1;
+      nxsched_foreach(gdb_get_thread, state);
+      state->pkt_len--;
+      state->pkt_buf[state->pkt_len] = 0;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], sthread_info,
+                   sizeof(sthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'l';
+      state->pkt_len = 1;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], thread_extra_info,
+                   sizeof(thread_extra_info) - 1) == 0)
+    {
+      char thread_info[128];
+      FAR struct tcb_s *tcb;
+      int ret;
+      int pid;
+      FAR static const char *thread_state[] =
+        {
+          "Invalid",
+          "Waiting,Unlock",
+          "Ready",
+#ifdef CONFIG_SMP
+          "Assigned",
+#endif
+          "Running",
+          "Inactive",
+          "Waiting,Semaphore",
+          "Waiting,Signal"
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)
+          , "Waiting,MQ empty"
+          , "Waiting,MQ full"
+#endif
+#ifdef CONFIG_PAGING
+          , "Waiting,Paging fill"
+#endif
+#ifdef CONFIG_SIG_SIGSTOP_ACTION
+          , "Stopped"
+#endif
+        };
+
+      state->ptr_next += sizeof(thread_extra_info) + 1;
+      token_expect_integer_arg(state, pid);
+      tcb = nxsched_get_tcb(pid);
+      if (tcb == NULL)
+        {
+          return -EINVAL;
+        }
+
+#if CONFIG_TASK_NAME_SIZE > 0
+      sprintf(thread_info, "Name: %s, State: %s, Priority: %d, Stack: %d",
+              tcb->name, thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#else
+      sprintf(thread_info, "State: %s, Priority: %d, Stack: %d",
+              thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#endif
+
+      ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                        (FAR unsigned char *)thread_info,
+                        strlen(thread_info));
+
+      state->pkt_len = ret;
+      gdb_send_packet(state);
+    }
+  else if (state->monitor_cmd != NULL && memcmp(&state->pkt_buf[1], r_cmd,
+                                                sizeof(r_cmd) - 1) == 0)
+    {
+      /* skip the 'Rcmd,' */
+
+      int len = state->pkt_len - sizeof(r_cmd) - 1;
+      char cmd[64];
+      int ret;
+
+      gdb_dec_hex2string(&state->pkt_buf[sizeof(r_cmd) + 1], len, cmd,
+                         len / 2);
+      ret = state->monitor_cmd(state, cmd);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else
+        {
+          gdb_send_ok_packet(state);
+        }
+    }
+  else
+    {
+      return -EPROTONOSUPPORT;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_is_thread_active_handle
+ *
+ * Description:
+ *  The is thread active packet is used by GDB to request information from
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_is_thread_active_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next = state->pkt_buf + 1;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_thread_context_handle
+ *
+ * Description:
+ *  The thread context packet is used by GDB to request information from
+ *  the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_thread_context_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next += 2;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_send_stop_handle
+ *
+ * Description:
+ *  The stop packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_stop_handle(FAR struct gdb_state *state)
+{
+  return gdb_send_signal_packet(state, 0x00);
+}
+
+/****************************************************************************
+ * Name: gdb_putchar
+ *
+ * Description:
+ *   Put a character to the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   ch      - The character to be sent.
+ *
+ * Returned Value:
+ *   The character sent to the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_putchar(FAR struct gdb_state *state, int ch)
+{
+  unsigned char tmp = ch & 0xff;
+  ssize_t ret;
+
+  ret = state->send(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return ch;
+}
+
+/****************************************************************************
+ * Name: gdb_getchar
+ *
+ * Description:
+ *  Get a character from the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The character read from the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_getchar(FAR struct gdb_state *state)
+{
+  unsigned char tmp;
+  ssize_t ret;
+
+  ret = state->recv(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return tmp;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd)
+{
+  struct gdb_state *state = kmm_zalloc(sizeof(*state) +
+                                       g_tcbinfo.basic_num *
+                                       sizeof(unsigned int));
+  if (state == NULL)
+    {
+      return NULL;
+    }
+
+  state->size = g_tcbinfo.basic_num * sizeof(unsigned int);
+  state->pkt_buf_len = sizeof(state->pkt_buf);
+  state->send = send;
+  state->recv = recv;
+  state->priv = priv;
+  state->monitor_cmd = monitor_cmd;
+
+  return state;
+}
+
+/****************************************************************************
+ * Name: gdb_state_uninit
+ *
+ * Description:
+ *   Uninitialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+void gdb_state_uninit(struct gdb_state *state)
+{
+  if (state == NULL)
+    {
+      return;
+    }
+
+  kmm_free(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_conmsg_packet
+ *
+ * Description:
+ *   Send a message to the GDB console (via O XX... packet).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_send_conmsg_packet(FAR struct gdb_state *state, FAR const char *msg)
+{
+  int ret;
+  int len;
+
+  if (state == NULL || msg == NULL)
+    {
+      return -EINVAL;
+    }
+
+  len = strlen(msg);
+  if (len > state->pkt_buf_len - 1)
+    {
+      return -EOVERFLOW;
+    }
+
+  state->pkt_buf[0] = 'O';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    (FAR unsigned char *)msg, len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_process
+ *
+ * Description:
+ *   Main debug loop. Handles commands.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_process(FAR struct gdb_state *state)
+{
+  int ret = 0;
+  int i;
+  static struct gdb_handle_cmd handle_cmd[] =

Review Comment:
   I think use switch case maybe better than static array



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,
+                            FAR const unsigned char *data, size_t data_len);
+typedef int (*gdb_dec_func)(FAR unsigned const char *buf, size_t buf_len,
+                            FAR unsigned char *data, size_t data_len);
+
+/****************************************************************************
+ *  GDB Remote Serial Protocol
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* System functions, supported by all stubs */
+
+static int gdb_getchar(FAR struct gdb_state *state);
+static int gdb_putchar(FAR struct gdb_state *state, int ch);
+
+/* Communication functions */
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len);
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len);
+
+/* Packet functions */
+
+static int gdb_send_packet(FAR struct gdb_state *state);
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len);
+static int gdb_checksum(FAR const unsigned char *buf, size_t len);
+static int gdb_recv_ack(FAR struct gdb_state *state);
+
+/* Data encoding/decoding */
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len);
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+
+/* Packet creation helpers */
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state);
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal);
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error);
+
+/* Command functions */
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc);
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * String Processing Helper Functions
+ ****************************************************************************/
+
+static uintptr_t gdb_strtol(FAR char *str, size_t len, int base,
+                            FAR char **endptr)
+{
+  char tmp = str[len];
+  uintptr_t ret;
+
+  /* Ensure string is null terminated
+   * Gdb rsp use \x48 ('0') make sure it's over
+   * */
+
+  str[len] = '\0';
+  ret =  strtoul(str, (FAR char **)endptr, base);
+  str[len] = tmp;
+
+  return ret;
+}
+
+/****************************************************************************
+ * Packet Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_recv_ack
+ *
+ * Description:
+ *   Receive a packet acknowledgment.
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if an ACK (+) was received
+ *   1       if a NACK (-) was received
+ *   -EINVAL if a bad response was received
+ *
+ ****************************************************************************/
+
+static int gdb_recv_ack(FAR struct gdb_state *state)
+{
+  int response;
+
+  /* Wait for packet ack */
+
+  switch (response = gdb_getchar(state))
+    {
+      case '+':
+
+        /* Packet acknowledged */
+
+        return 0;
+      case '-':
+
+        /* Packet negative acknowledged */
+
+        return 1;
+      default:
+
+        /* Bad response! */
+
+        GDB_DEBUG("received bad packet response: 0x%2x\n", response);
+        return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Calculate 8-bit checksum of a buffer.
+ *
+ * Input Parameters:
+ *   buf - The buffer to checksum.
+ *
+ * Returned Value:
+ *   8-bit checksum
+ *
+ ****************************************************************************/
+
+static int gdb_checksum(FAR const unsigned char *buf, size_t len)
+{
+  unsigned char csum = 0;
+
+  while (len--)
+    {
+      csum += *buf++;
+    }
+
+  return csum;
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Transmits a packet of data.
+ *   Packets are of the form: $<packet-data>#<checksum>
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if the packet was transmitted and acknowledged.
+ *   1       if the packet was transmitted but not acknowledged.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_packet(FAR struct gdb_state *state)
+{
+  unsigned char buf[3];
+  unsigned char csum;
+  int ret;
+
+  /* Send packet start */
+
+  ret = gdb_putchar(state, '$');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("-> ");
+      for (p = 0; p < state->pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Send packet data */
+
+  ret = gdb_write(state, state->pkt_buf, state->pkt_len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Send the checksum */
+
+  buf[0] = '#';
+  csum = gdb_checksum(state->pkt_buf, state->pkt_len);
+
+  ret = gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_write(state, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return gdb_recv_ack(state);
+}
+
+/****************************************************************************
+ * Name: gdb_recv_packet
+ *
+ * Description:
+ *   Receives a packet of data, assuming a 7-bit clean connection.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   pkt_len - The length of the packet received.
+ *
+ * Returned Value:
+ *   0          if the packet was received.
+ *   -EIO       if the packet was not received.
+ *   -EOVERFLOW if the packet was too long.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len)
+{
+  unsigned char expected_csum;
+  unsigned char actual_csum;
+  unsigned char buf[2];
+  int ret;
+
+  /* Wait for packet start */
+
+  actual_csum = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else if (ret == '$')
+        {
+          /* Detected start of packet. */
+
+          break;
+        }
+    }
+
+  /* Read until checksum */
+
+  *pkt_len = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          /* Error receiving character */
+
+          return ret;
+        }
+      else if (ret == '#')
+        {
+          /* End of packet */
+
+          break;
+        }
+      else
+        {
+          /* Check for space */
+
+          if (*pkt_len >= state->pkt_buf_len)
+            {
+              GDB_DEBUG("packet buffer overflow\n");
+              return -EOVERFLOW;
+            }
+
+          /* Store character and update checksum */
+
+          state->pkt_buf[(*pkt_len)++] = (char) ret;
+        }
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("<- ");
+      for (p = 0; p < *pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Receive the checksum */
+
+  ret = gdb_read(state, buf, sizeof(buf), 2);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_dec_hex(buf, 2, &expected_csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Verify checksum */
+
+  actual_csum = gdb_checksum(state->pkt_buf, *pkt_len);
+  if (actual_csum != expected_csum)
+    {
+      /* Send packet nack */
+
+      GDB_DEBUG("received packet with bad checksum\n");
+      gdb_putchar(state, '-');
+      return -EIO;
+    }
+
+  /* Send packet ack */
+
+  gdb_putchar(state, '+');
+  return 0;
+}
+
+/****************************************************************************
+ * Data Encoding/Decoding
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_enc_hex
+ *
+ * Description:
+ *   Encode data to its hex-value representation in a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer to store the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The data to encode.
+ *   data_len - The length of the data to encode.
+ *
+ * Returned Value:
+ *    The number of bytes written to buf on success.
+ *    -EINVAL if the buffer is too small.
+ *
+ ****************************************************************************/
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len)
+{
+  size_t pos;
+
+  if (buf_len < data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      itoa((data[pos] >> 4) & 0xf, (FAR char *)buf++, 16);
+      itoa((data[pos]) & 0xf, (FAR char *)buf++, 16);
+    }
+
+  return data_len * 2;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *   -EINVAL if the buffer is too small.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t pos;
+  int tmp;
+  char ch[2] =
+    {
+      0, 0
+    };
+
+  if (buf_len != data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      /* Decode high nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] = tmp << 4;
+
+      /* Decode low nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] |= tmp;
+    }
+
+  return data_len;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex2string
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a string.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex2string(FAR const unsigned char *buf, size_t buf_len,
+                              FAR char *data, size_t data_len)
+{
+  for (int i = 0; i < buf_len; i += 2)
+    {
+      char hex_byte[3] =
+        {
+          buf[i], buf[i + 1], '\0'
+        };
+
+      int byte = strtol(hex_byte, NULL, 16);
+
+      data[i / 2] = (char)byte;
+    }
+
+  data[buf_len / 2] = '\0';
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_bin
+ *
+ * Description:
+ *   Decode data from its bin-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *     The number of bytes written to data on success.
+ *    -EOVERFLOW if the output buffer is too small.
+ *    -EINVAL if the input buffer is invalid.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t buf_pos;
+  size_t data_pos;
+
+  for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++)
+    {
+      if (data_pos >= data_len)
+        {
+          /* Output buffer overflow */
+
+          GDB_ASSERT();
+          return -EOVERFLOW;
+        }
+
+      if (buf[buf_pos] == '}')
+        {
+          /* The next byte is escaped! */
+
+          if (buf_pos + 1 >= buf_len)
+            {
+              /* There's an escape character, but no escaped character
+               * following the escape character.
+               */
+
+              GDB_ASSERT();
+              return -EINVAL;
+            }
+
+          buf_pos += 1;
+          data[data_pos++] = buf[buf_pos] ^ 0x20;
+        }
+      else
+        {
+          data[data_pos++] = buf[buf_pos];
+        }
+    }
+
+  return data_pos;
+}
+
+/****************************************************************************
+ * Command Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_mem_read
+ *
+ * Description:
+ *   Read from memory and encode into buf.
+ *
+ * Input Parameters:
+ *   state   - The state structure.
+ *   buf     - The buffer to store the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to read from.
+ *   len     - The length of the data to read.
+ *   enc     - The encoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to buf on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return enc(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Name: gdb_mem_write
+ *
+ * Description:
+ *   Write to memory from encoded buf
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to write to.
+ *   len     - The length of the data to write.
+ *   dec     - The decoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to addr on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return dec(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Packet Creation Helpers
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_send_ok_packet
+ *
+ * Description:
+ *   Send OK packet.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state)
+{
+  state->pkt_buf[0] = 'O';
+  state->pkt_buf[1] = 'K';
+  state->pkt_len = 2;
+
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_signal_packet
+ *
+ * Description:
+ *   Send a signal packet (S AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   signal  - The signal to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'S';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &signal, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_error_packet
+ *
+ * Description:
+ *   Send a error packet (E AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   error   - The error to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'E';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &error, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Communication Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_write
+ *
+ * Description:
+ *   Write a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the data to write.
+ *   len     - The length of the data to write.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len)
+{
+  return state->send(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: gdb_read
+ *
+ * Description:
+ *   Read a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer to store the read data.
+ *   buf_len - The length of the buffer.
+ *   len     - The length of the data to read.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len)
+{
+  if (buf_len < len)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  return state->recv(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: get_register
+ *
+ * Description:
+ *   Get the registers of the specified task.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ ****************************************************************************/
+
+static void get_register(FAR struct gdb_state *state)
+{
+  FAR unsigned int *regs = state->registers;
+  FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
+  FAR uint8_t *reg;
+  int i;
+
+  reg = (FAR uint8_t *)tcb->xcp.regs;
+  if (state->pid == getpid())
+    {
+      reg = (FAR uint8_t *)CURRENT_REGS;
+    }
+
+  for (i = 0; i < g_tcbinfo.total_num; i++)
+    {
+      if (g_tcbinfo.reg_off.p[i] == UINT16_MAX)
+        {
+          regs[i] = 0;
+        }
+      else
+        {
+          regs[i] = *(FAR unsigned int *)(reg + g_tcbinfo.reg_off.p[i]);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_read_register_handle
+ *
+ * Description:
+ *   Read Registers Command Format: g.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  get_register(state);
+
+  /* Encode registers */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                    (FAR unsigned char *)(&(state->registers[0])),
+                    state->size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_register_handle
+ *
+ * Description:
+ *   Write Registers Command Format: G XX...
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ *   Note: This function is not really change the register values.
+ *
+ ****************************************************************************/
+
+static int gdb_write_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  ret = gdb_dec_hex(state->pkt_buf + 1, state->pkt_len - 1,
+                    (FAR unsigned char *)&(state->registers),
+                    sizeof(state->registers));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_one_register_handle
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_one_register_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  get_register(state);
+  if (addr >= state->size)
+    {
+      return 0;
+    }
+
+  /* Read Register */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                   (FAR unsigned char *)&(state->registers[addr]),
+                   sizeof(state->registers[addr]));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_read_mem_handle
+ *
+ * Description:
+ *   Read Memory Command Format: m addr,length
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes read if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+
+  /* Read Memory */
+
+  ret = gdb_mem_read(state, state->pkt_buf, state->pkt_buf_len,
+                     addr, length, gdb_enc_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_mem_handle
+ *
+ * Description:
+ *   Write Memory Command Format: M addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_bin_mem_handle
+ *
+ * Description:
+ *   Write Memory (Binary) Command Format: X addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_bin_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_bin);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_get_thread
+ *
+ * Description:
+ *   Get the all thread id.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct gdb_state *state = arg;
+  char pid = tcb->pid;
+
+  itoa((pid >> 4) & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  itoa(pid & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  state->pkt_buf[state->pkt_len++] = ',';
+}
+
+/****************************************************************************
+ * Name: gdb_query_handle
+ *
+ * Description:
+ *  The query packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_query_handle(FAR struct gdb_state *state)
+{
+  static const char thread_extra_info[] = "ThreadExtraInfo";
+  static const char fthread_info[] = "fThreadInfo";
+  static const char sthread_info[] = "sThreadInfo";
+  static const char r_cmd[] = "Rcmd";
+
+  if (memcmp(&state->pkt_buf[1], fthread_info,
+              sizeof(fthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'm';
+      state->pkt_len = 1;
+      nxsched_foreach(gdb_get_thread, state);
+      state->pkt_len--;
+      state->pkt_buf[state->pkt_len] = 0;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], sthread_info,
+                   sizeof(sthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'l';
+      state->pkt_len = 1;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], thread_extra_info,
+                   sizeof(thread_extra_info) - 1) == 0)
+    {
+      char thread_info[128];
+      FAR struct tcb_s *tcb;
+      int ret;
+      int pid;
+      FAR static const char *thread_state[] =
+        {
+          "Invalid",
+          "Waiting,Unlock",
+          "Ready",
+#ifdef CONFIG_SMP
+          "Assigned",
+#endif
+          "Running",
+          "Inactive",
+          "Waiting,Semaphore",
+          "Waiting,Signal"
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)
+          , "Waiting,MQ empty"
+          , "Waiting,MQ full"
+#endif
+#ifdef CONFIG_PAGING
+          , "Waiting,Paging fill"
+#endif
+#ifdef CONFIG_SIG_SIGSTOP_ACTION
+          , "Stopped"
+#endif
+        };
+
+      state->ptr_next += sizeof(thread_extra_info) + 1;
+      token_expect_integer_arg(state, pid);
+      tcb = nxsched_get_tcb(pid);
+      if (tcb == NULL)
+        {
+          return -EINVAL;
+        }
+
+#if CONFIG_TASK_NAME_SIZE > 0
+      sprintf(thread_info, "Name: %s, State: %s, Priority: %d, Stack: %d",
+              tcb->name, thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#else
+      sprintf(thread_info, "State: %s, Priority: %d, Stack: %d",
+              thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#endif
+
+      ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                        (FAR unsigned char *)thread_info,
+                        strlen(thread_info));
+
+      state->pkt_len = ret;
+      gdb_send_packet(state);
+    }
+  else if (state->monitor_cmd != NULL && memcmp(&state->pkt_buf[1], r_cmd,
+                                                sizeof(r_cmd) - 1) == 0)
+    {
+      /* skip the 'Rcmd,' */
+
+      int len = state->pkt_len - sizeof(r_cmd) - 1;
+      char cmd[64];
+      int ret;
+
+      gdb_dec_hex2string(&state->pkt_buf[sizeof(r_cmd) + 1], len, cmd,
+                         len / 2);
+      ret = state->monitor_cmd(state, cmd);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else
+        {
+          gdb_send_ok_packet(state);
+        }
+    }
+  else
+    {
+      return -EPROTONOSUPPORT;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_is_thread_active_handle
+ *
+ * Description:
+ *  The is thread active packet is used by GDB to request information from
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_is_thread_active_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next = state->pkt_buf + 1;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_thread_context_handle
+ *
+ * Description:
+ *  The thread context packet is used by GDB to request information from
+ *  the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_thread_context_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next += 2;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_send_stop_handle
+ *
+ * Description:
+ *  The stop packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_stop_handle(FAR struct gdb_state *state)
+{
+  return gdb_send_signal_packet(state, 0x00);
+}
+
+/****************************************************************************
+ * Name: gdb_putchar
+ *
+ * Description:
+ *   Put a character to the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   ch      - The character to be sent.
+ *
+ * Returned Value:
+ *   The character sent to the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_putchar(FAR struct gdb_state *state, int ch)
+{
+  unsigned char tmp = ch & 0xff;
+  ssize_t ret;
+
+  ret = state->send(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return ch;
+}
+
+/****************************************************************************
+ * Name: gdb_getchar
+ *
+ * Description:
+ *  Get a character from the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The character read from the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_getchar(FAR struct gdb_state *state)
+{
+  unsigned char tmp;
+  ssize_t ret;
+
+  ret = state->recv(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return tmp;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd)
+{
+  struct gdb_state *state = kmm_zalloc(sizeof(*state) +
+                                       g_tcbinfo.basic_num *
+                                       sizeof(unsigned int));
+  if (state == NULL)
+    {
+      return NULL;
+    }
+
+  state->size = g_tcbinfo.basic_num * sizeof(unsigned int);
+  state->pkt_buf_len = sizeof(state->pkt_buf);
+  state->send = send;
+  state->recv = recv;
+  state->priv = priv;
+  state->monitor_cmd = monitor_cmd;
+
+  return state;
+}
+
+/****************************************************************************
+ * Name: gdb_state_uninit
+ *
+ * Description:
+ *   Uninitialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+void gdb_state_uninit(struct gdb_state *state)
+{
+  if (state == NULL)
+    {
+      return;
+    }
+
+  kmm_free(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_conmsg_packet
+ *
+ * Description:
+ *   Send a message to the GDB console (via O XX... packet).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_send_conmsg_packet(FAR struct gdb_state *state, FAR const char *msg)
+{
+  int ret;
+  int len;
+
+  if (state == NULL || msg == NULL)
+    {
+      return -EINVAL;
+    }
+
+  len = strlen(msg);
+  if (len > state->pkt_buf_len - 1)
+    {
+      return -EOVERFLOW;
+    }
+
+  state->pkt_buf[0] = 'O';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    (FAR unsigned char *)msg, len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_process
+ *
+ * Description:
+ *   Main debug loop. Handles commands.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_process(FAR struct gdb_state *state)
+{
+  int ret = 0;
+  int i;
+  static struct gdb_handle_cmd handle_cmd[] =
+    {
+      {
+        .handle = gdb_read_register_handle,
+        .cmd = 'g'
+      },
+      {
+        .handle = gdb_write_register_handle,
+        .cmd = 'G'
+      },
+      {
+        .handle = gdb_read_one_register_handle,
+        .cmd = 'p'
+      },
+      {
+        .handle = gdb_read_mem_handle,
+        .cmd = 'm'
+      },
+      {
+        .handle = gdb_write_mem_handle,
+        .cmd = 'M'
+      },
+      {
+        .handle = gdb_write_bin_mem_handle,
+        .cmd = 'X'
+      },
+      {
+        .handle = gdb_send_stop_handle,
+        .cmd = '?'
+      },
+      {
+        .handle = gdb_query_handle,
+        .cmd = 'q'
+      },
+      {
+        .handle = gdb_is_thread_active_handle,
+        .cmd = 'T'
+      },
+      {
+        .handle = gdb_thread_context_handle,
+        .cmd = 'H'
+      },
+    };
+
+  while (1)
+    {
+      /* Receive the next packet */
+
+      ret = gdb_recv_packet(state, &state->pkt_len);
+      if (ret < 0)
+        {
+          break;
+        }
+
+      if (state->pkt_len == 0)
+        {
+          /* Received empty packet.. */
+
+          continue;
+        }
+
+      state->ptr_next = state->pkt_buf;
+
+      /* Handle one letter commands */
+
+      for (i = 0; i < sizeof(handle_cmd) / sizeof(handle_cmd[0]); i++)
+        {
+          if (state->pkt_buf[0] == handle_cmd[i].cmd)
+            {
+              ret = handle_cmd[i].handle(state);
+              if (ret == -EPROTONOSUPPORT)
+                {
+                  goto nosupport;
+                }
+              else if (ret == -ECONNRESET)
+                {
+                  return ret;
+                }
+              else if (ret < 0)
+                {
+                  gdb_send_error_packet(state, 0x00);
+                }
+
+              break;
+            }
+        }
+
+      if (i == sizeof(handle_cmd) / sizeof(handle_cmd[0]))

Review Comment:
   nitems(array)



##########
include/nuttx/gdbstub/gdbstub.h:
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * include/nuttx/gdbstub/gdbstub.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_GDBSTUB_GDBSTUB_H
+#define __INCLUDE_NUTTX_GDBSTUB_GDBSTUB_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+struct gdb_state;
+
+typedef ssize_t (*gdb_send_func)(FAR void *priv,
+                                 FAR const unsigned char *buf, size_t len);
+typedef ssize_t (*gdb_recv_func)(FAR void *priv, FAR unsigned char *buf,
+                                 size_t len);
+typedef int (*monitor_cmd_func)(FAR struct gdb_state *state, FAR char *cmd);

Review Comment:
   *_func_t



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,

Review Comment:
   gdb_enc_func_t



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state

Review Comment:
   gdb_state_s



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,
+                            FAR const unsigned char *data, size_t data_len);
+typedef int (*gdb_dec_func)(FAR unsigned const char *buf, size_t buf_len,
+                            FAR unsigned char *data, size_t data_len);
+
+/****************************************************************************
+ *  GDB Remote Serial Protocol
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* System functions, supported by all stubs */
+
+static int gdb_getchar(FAR struct gdb_state *state);
+static int gdb_putchar(FAR struct gdb_state *state, int ch);
+
+/* Communication functions */
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len);
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len);
+
+/* Packet functions */
+
+static int gdb_send_packet(FAR struct gdb_state *state);
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len);
+static int gdb_checksum(FAR const unsigned char *buf, size_t len);
+static int gdb_recv_ack(FAR struct gdb_state *state);
+
+/* Data encoding/decoding */
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len);
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+
+/* Packet creation helpers */
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state);
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal);
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error);
+
+/* Command functions */
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc);
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * String Processing Helper Functions
+ ****************************************************************************/
+
+static uintptr_t gdb_strtol(FAR char *str, size_t len, int base,
+                            FAR char **endptr)
+{
+  char tmp = str[len];
+  uintptr_t ret;
+
+  /* Ensure string is null terminated
+   * Gdb rsp use \x48 ('0') make sure it's over
+   * */
+
+  str[len] = '\0';
+  ret =  strtoul(str, (FAR char **)endptr, base);
+  str[len] = tmp;
+
+  return ret;
+}
+
+/****************************************************************************
+ * Packet Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_recv_ack
+ *
+ * Description:
+ *   Receive a packet acknowledgment.
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if an ACK (+) was received
+ *   1       if a NACK (-) was received
+ *   -EINVAL if a bad response was received
+ *
+ ****************************************************************************/
+
+static int gdb_recv_ack(FAR struct gdb_state *state)
+{
+  int response;
+
+  /* Wait for packet ack */
+
+  switch (response = gdb_getchar(state))
+    {
+      case '+':
+
+        /* Packet acknowledged */
+
+        return 0;
+      case '-':
+
+        /* Packet negative acknowledged */
+
+        return 1;
+      default:
+
+        /* Bad response! */
+
+        GDB_DEBUG("received bad packet response: 0x%2x\n", response);
+        return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Calculate 8-bit checksum of a buffer.
+ *
+ * Input Parameters:
+ *   buf - The buffer to checksum.
+ *
+ * Returned Value:
+ *   8-bit checksum
+ *
+ ****************************************************************************/
+
+static int gdb_checksum(FAR const unsigned char *buf, size_t len)
+{
+  unsigned char csum = 0;
+
+  while (len--)
+    {
+      csum += *buf++;
+    }
+
+  return csum;
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Transmits a packet of data.
+ *   Packets are of the form: $<packet-data>#<checksum>
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if the packet was transmitted and acknowledged.
+ *   1       if the packet was transmitted but not acknowledged.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_packet(FAR struct gdb_state *state)
+{
+  unsigned char buf[3];
+  unsigned char csum;
+  int ret;
+
+  /* Send packet start */
+
+  ret = gdb_putchar(state, '$');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("-> ");
+      for (p = 0; p < state->pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Send packet data */
+
+  ret = gdb_write(state, state->pkt_buf, state->pkt_len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Send the checksum */
+
+  buf[0] = '#';
+  csum = gdb_checksum(state->pkt_buf, state->pkt_len);
+
+  ret = gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_write(state, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return gdb_recv_ack(state);
+}
+
+/****************************************************************************
+ * Name: gdb_recv_packet
+ *
+ * Description:
+ *   Receives a packet of data, assuming a 7-bit clean connection.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   pkt_len - The length of the packet received.
+ *
+ * Returned Value:
+ *   0          if the packet was received.
+ *   -EIO       if the packet was not received.
+ *   -EOVERFLOW if the packet was too long.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len)
+{
+  unsigned char expected_csum;
+  unsigned char actual_csum;
+  unsigned char buf[2];
+  int ret;
+
+  /* Wait for packet start */
+
+  actual_csum = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else if (ret == '$')
+        {
+          /* Detected start of packet. */
+
+          break;
+        }
+    }
+
+  /* Read until checksum */
+
+  *pkt_len = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          /* Error receiving character */
+
+          return ret;
+        }
+      else if (ret == '#')
+        {
+          /* End of packet */
+
+          break;
+        }
+      else
+        {
+          /* Check for space */
+
+          if (*pkt_len >= state->pkt_buf_len)
+            {
+              GDB_DEBUG("packet buffer overflow\n");
+              return -EOVERFLOW;
+            }
+
+          /* Store character and update checksum */
+
+          state->pkt_buf[(*pkt_len)++] = (char) ret;
+        }
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("<- ");
+      for (p = 0; p < *pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Receive the checksum */
+
+  ret = gdb_read(state, buf, sizeof(buf), 2);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_dec_hex(buf, 2, &expected_csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Verify checksum */
+
+  actual_csum = gdb_checksum(state->pkt_buf, *pkt_len);
+  if (actual_csum != expected_csum)
+    {
+      /* Send packet nack */
+
+      GDB_DEBUG("received packet with bad checksum\n");
+      gdb_putchar(state, '-');
+      return -EIO;
+    }
+
+  /* Send packet ack */
+
+  gdb_putchar(state, '+');
+  return 0;
+}
+
+/****************************************************************************
+ * Data Encoding/Decoding
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_enc_hex
+ *
+ * Description:
+ *   Encode data to its hex-value representation in a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer to store the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The data to encode.
+ *   data_len - The length of the data to encode.
+ *
+ * Returned Value:
+ *    The number of bytes written to buf on success.
+ *    -EINVAL if the buffer is too small.
+ *
+ ****************************************************************************/
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len)
+{
+  size_t pos;
+
+  if (buf_len < data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      itoa((data[pos] >> 4) & 0xf, (FAR char *)buf++, 16);
+      itoa((data[pos]) & 0xf, (FAR char *)buf++, 16);
+    }
+
+  return data_len * 2;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *   -EINVAL if the buffer is too small.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t pos;
+  int tmp;
+  char ch[2] =
+    {
+      0, 0
+    };
+
+  if (buf_len != data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      /* Decode high nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] = tmp << 4;
+
+      /* Decode low nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] |= tmp;
+    }
+
+  return data_len;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex2string
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a string.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex2string(FAR const unsigned char *buf, size_t buf_len,
+                              FAR char *data, size_t data_len)
+{
+  for (int i = 0; i < buf_len; i += 2)
+    {
+      char hex_byte[3] =
+        {
+          buf[i], buf[i + 1], '\0'
+        };
+
+      int byte = strtol(hex_byte, NULL, 16);
+
+      data[i / 2] = (char)byte;
+    }
+
+  data[buf_len / 2] = '\0';
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_bin
+ *
+ * Description:
+ *   Decode data from its bin-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *     The number of bytes written to data on success.
+ *    -EOVERFLOW if the output buffer is too small.
+ *    -EINVAL if the input buffer is invalid.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t buf_pos;
+  size_t data_pos;
+
+  for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++)
+    {
+      if (data_pos >= data_len)
+        {
+          /* Output buffer overflow */
+
+          GDB_ASSERT();
+          return -EOVERFLOW;
+        }
+
+      if (buf[buf_pos] == '}')
+        {
+          /* The next byte is escaped! */
+
+          if (buf_pos + 1 >= buf_len)
+            {
+              /* There's an escape character, but no escaped character
+               * following the escape character.
+               */
+
+              GDB_ASSERT();
+              return -EINVAL;
+            }
+
+          buf_pos += 1;
+          data[data_pos++] = buf[buf_pos] ^ 0x20;
+        }
+      else
+        {
+          data[data_pos++] = buf[buf_pos];
+        }
+    }
+
+  return data_pos;
+}
+
+/****************************************************************************
+ * Command Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_mem_read
+ *
+ * Description:
+ *   Read from memory and encode into buf.
+ *
+ * Input Parameters:
+ *   state   - The state structure.
+ *   buf     - The buffer to store the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to read from.
+ *   len     - The length of the data to read.
+ *   enc     - The encoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to buf on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return enc(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Name: gdb_mem_write
+ *
+ * Description:
+ *   Write to memory from encoded buf
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to write to.
+ *   len     - The length of the data to write.
+ *   dec     - The decoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to addr on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return dec(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Packet Creation Helpers
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_send_ok_packet
+ *
+ * Description:
+ *   Send OK packet.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state)
+{
+  state->pkt_buf[0] = 'O';
+  state->pkt_buf[1] = 'K';
+  state->pkt_len = 2;
+
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_signal_packet
+ *
+ * Description:
+ *   Send a signal packet (S AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   signal  - The signal to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'S';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &signal, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_error_packet
+ *
+ * Description:
+ *   Send a error packet (E AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   error   - The error to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'E';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &error, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Communication Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_write
+ *
+ * Description:
+ *   Write a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the data to write.
+ *   len     - The length of the data to write.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len)
+{
+  return state->send(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: gdb_read
+ *
+ * Description:
+ *   Read a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer to store the read data.
+ *   buf_len - The length of the buffer.
+ *   len     - The length of the data to read.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len)
+{
+  if (buf_len < len)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  return state->recv(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: get_register
+ *
+ * Description:
+ *   Get the registers of the specified task.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ ****************************************************************************/
+
+static void get_register(FAR struct gdb_state *state)
+{
+  FAR unsigned int *regs = state->registers;
+  FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
+  FAR uint8_t *reg;
+  int i;
+
+  reg = (FAR uint8_t *)tcb->xcp.regs;
+  if (state->pid == getpid())
+    {
+      reg = (FAR uint8_t *)CURRENT_REGS;
+    }
+
+  for (i = 0; i < g_tcbinfo.total_num; i++)
+    {
+      if (g_tcbinfo.reg_off.p[i] == UINT16_MAX)
+        {
+          regs[i] = 0;
+        }
+      else
+        {
+          regs[i] = *(FAR unsigned int *)(reg + g_tcbinfo.reg_off.p[i]);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_read_register_handle
+ *
+ * Description:
+ *   Read Registers Command Format: g.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  get_register(state);
+
+  /* Encode registers */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                    (FAR unsigned char *)(&(state->registers[0])),
+                    state->size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_register_handle
+ *
+ * Description:
+ *   Write Registers Command Format: G XX...
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ *   Note: This function is not really change the register values.
+ *
+ ****************************************************************************/
+
+static int gdb_write_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  ret = gdb_dec_hex(state->pkt_buf + 1, state->pkt_len - 1,
+                    (FAR unsigned char *)&(state->registers),
+                    sizeof(state->registers));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_one_register_handle
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_one_register_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  get_register(state);
+  if (addr >= state->size)
+    {
+      return 0;
+    }
+
+  /* Read Register */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                   (FAR unsigned char *)&(state->registers[addr]),
+                   sizeof(state->registers[addr]));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_read_mem_handle
+ *
+ * Description:
+ *   Read Memory Command Format: m addr,length
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes read if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+
+  /* Read Memory */
+
+  ret = gdb_mem_read(state, state->pkt_buf, state->pkt_buf_len,
+                     addr, length, gdb_enc_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_mem_handle
+ *
+ * Description:
+ *   Write Memory Command Format: M addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_bin_mem_handle
+ *
+ * Description:
+ *   Write Memory (Binary) Command Format: X addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_bin_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_bin);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_get_thread
+ *
+ * Description:
+ *   Get the all thread id.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct gdb_state *state = arg;
+  char pid = tcb->pid;
+
+  itoa((pid >> 4) & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  itoa(pid & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  state->pkt_buf[state->pkt_len++] = ',';
+}
+
+/****************************************************************************
+ * Name: gdb_query_handle
+ *
+ * Description:
+ *  The query packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_query_handle(FAR struct gdb_state *state)
+{
+  static const char thread_extra_info[] = "ThreadExtraInfo";
+  static const char fthread_info[] = "fThreadInfo";
+  static const char sthread_info[] = "sThreadInfo";
+  static const char r_cmd[] = "Rcmd";
+
+  if (memcmp(&state->pkt_buf[1], fthread_info,
+              sizeof(fthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'm';
+      state->pkt_len = 1;
+      nxsched_foreach(gdb_get_thread, state);
+      state->pkt_len--;
+      state->pkt_buf[state->pkt_len] = 0;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], sthread_info,
+                   sizeof(sthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'l';
+      state->pkt_len = 1;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], thread_extra_info,
+                   sizeof(thread_extra_info) - 1) == 0)
+    {
+      char thread_info[128];
+      FAR struct tcb_s *tcb;
+      int ret;
+      int pid;
+      FAR static const char *thread_state[] =
+        {
+          "Invalid",
+          "Waiting,Unlock",
+          "Ready",
+#ifdef CONFIG_SMP
+          "Assigned",
+#endif
+          "Running",
+          "Inactive",
+          "Waiting,Semaphore",
+          "Waiting,Signal"
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)
+          , "Waiting,MQ empty"
+          , "Waiting,MQ full"
+#endif
+#ifdef CONFIG_PAGING
+          , "Waiting,Paging fill"
+#endif
+#ifdef CONFIG_SIG_SIGSTOP_ACTION
+          , "Stopped"
+#endif
+        };
+
+      state->ptr_next += sizeof(thread_extra_info) + 1;
+      token_expect_integer_arg(state, pid);
+      tcb = nxsched_get_tcb(pid);
+      if (tcb == NULL)
+        {
+          return -EINVAL;
+        }
+
+#if CONFIG_TASK_NAME_SIZE > 0
+      sprintf(thread_info, "Name: %s, State: %s, Priority: %d, Stack: %d",
+              tcb->name, thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#else
+      sprintf(thread_info, "State: %s, Priority: %d, Stack: %d",
+              thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#endif
+
+      ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                        (FAR unsigned char *)thread_info,
+                        strlen(thread_info));
+
+      state->pkt_len = ret;
+      gdb_send_packet(state);
+    }
+  else if (state->monitor_cmd != NULL && memcmp(&state->pkt_buf[1], r_cmd,
+                                                sizeof(r_cmd) - 1) == 0)
+    {
+      /* skip the 'Rcmd,' */
+
+      int len = state->pkt_len - sizeof(r_cmd) - 1;
+      char cmd[64];
+      int ret;
+
+      gdb_dec_hex2string(&state->pkt_buf[sizeof(r_cmd) + 1], len, cmd,
+                         len / 2);
+      ret = state->monitor_cmd(state, cmd);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else
+        {
+          gdb_send_ok_packet(state);
+        }
+    }
+  else
+    {
+      return -EPROTONOSUPPORT;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_is_thread_active_handle
+ *
+ * Description:
+ *  The is thread active packet is used by GDB to request information from
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_is_thread_active_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next = state->pkt_buf + 1;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_thread_context_handle
+ *
+ * Description:
+ *  The thread context packet is used by GDB to request information from
+ *  the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_thread_context_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next += 2;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_send_stop_handle
+ *
+ * Description:
+ *  The stop packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_stop_handle(FAR struct gdb_state *state)
+{
+  return gdb_send_signal_packet(state, 0x00);
+}
+
+/****************************************************************************
+ * Name: gdb_putchar
+ *
+ * Description:
+ *   Put a character to the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   ch      - The character to be sent.
+ *
+ * Returned Value:
+ *   The character sent to the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_putchar(FAR struct gdb_state *state, int ch)
+{
+  unsigned char tmp = ch & 0xff;
+  ssize_t ret;
+
+  ret = state->send(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return ch;
+}
+
+/****************************************************************************
+ * Name: gdb_getchar
+ *
+ * Description:
+ *  Get a character from the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The character read from the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_getchar(FAR struct gdb_state *state)
+{
+  unsigned char tmp;
+  ssize_t ret;
+
+  ret = state->recv(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return tmp;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd)
+{
+  struct gdb_state *state = kmm_zalloc(sizeof(*state) +
+                                       g_tcbinfo.basic_num *
+                                       sizeof(unsigned int));

Review Comment:
   uintptr_t



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;

Review Comment:
   char cmd;  
   CODE int (*handle)(FAR struct gdb_state *state);
   



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,
+                            FAR const unsigned char *data, size_t data_len);
+typedef int (*gdb_dec_func)(FAR unsigned const char *buf, size_t buf_len,
+                            FAR unsigned char *data, size_t data_len);
+
+/****************************************************************************
+ *  GDB Remote Serial Protocol
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* System functions, supported by all stubs */
+
+static int gdb_getchar(FAR struct gdb_state *state);
+static int gdb_putchar(FAR struct gdb_state *state, int ch);
+
+/* Communication functions */
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len);
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len);
+
+/* Packet functions */
+
+static int gdb_send_packet(FAR struct gdb_state *state);
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len);
+static int gdb_checksum(FAR const unsigned char *buf, size_t len);
+static int gdb_recv_ack(FAR struct gdb_state *state);
+
+/* Data encoding/decoding */
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len);
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+
+/* Packet creation helpers */
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state);
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal);
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error);
+
+/* Command functions */
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc);
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * String Processing Helper Functions
+ ****************************************************************************/
+
+static uintptr_t gdb_strtol(FAR char *str, size_t len, int base,
+                            FAR char **endptr)
+{
+  char tmp = str[len];
+  uintptr_t ret;
+
+  /* Ensure string is null terminated
+   * Gdb rsp use \x48 ('0') make sure it's over
+   * */
+
+  str[len] = '\0';
+  ret =  strtoul(str, (FAR char **)endptr, base);
+  str[len] = tmp;
+
+  return ret;
+}
+
+/****************************************************************************
+ * Packet Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_recv_ack
+ *
+ * Description:
+ *   Receive a packet acknowledgment.
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if an ACK (+) was received
+ *   1       if a NACK (-) was received
+ *   -EINVAL if a bad response was received
+ *
+ ****************************************************************************/
+
+static int gdb_recv_ack(FAR struct gdb_state *state)
+{
+  int response;
+
+  /* Wait for packet ack */
+
+  switch (response = gdb_getchar(state))
+    {
+      case '+':
+
+        /* Packet acknowledged */
+
+        return 0;
+      case '-':
+
+        /* Packet negative acknowledged */
+
+        return 1;
+      default:
+
+        /* Bad response! */
+
+        GDB_DEBUG("received bad packet response: 0x%2x\n", response);
+        return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Calculate 8-bit checksum of a buffer.
+ *
+ * Input Parameters:
+ *   buf - The buffer to checksum.
+ *
+ * Returned Value:
+ *   8-bit checksum
+ *
+ ****************************************************************************/
+
+static int gdb_checksum(FAR const unsigned char *buf, size_t len)
+{
+  unsigned char csum = 0;
+
+  while (len--)
+    {
+      csum += *buf++;
+    }
+
+  return csum;
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Transmits a packet of data.
+ *   Packets are of the form: $<packet-data>#<checksum>
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if the packet was transmitted and acknowledged.
+ *   1       if the packet was transmitted but not acknowledged.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_packet(FAR struct gdb_state *state)
+{
+  unsigned char buf[3];
+  unsigned char csum;
+  int ret;
+
+  /* Send packet start */
+
+  ret = gdb_putchar(state, '$');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("-> ");
+      for (p = 0; p < state->pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Send packet data */
+
+  ret = gdb_write(state, state->pkt_buf, state->pkt_len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Send the checksum */
+
+  buf[0] = '#';
+  csum = gdb_checksum(state->pkt_buf, state->pkt_len);
+
+  ret = gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_write(state, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return gdb_recv_ack(state);
+}
+
+/****************************************************************************
+ * Name: gdb_recv_packet
+ *
+ * Description:
+ *   Receives a packet of data, assuming a 7-bit clean connection.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   pkt_len - The length of the packet received.
+ *
+ * Returned Value:
+ *   0          if the packet was received.
+ *   -EIO       if the packet was not received.
+ *   -EOVERFLOW if the packet was too long.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len)
+{
+  unsigned char expected_csum;
+  unsigned char actual_csum;
+  unsigned char buf[2];
+  int ret;
+
+  /* Wait for packet start */
+
+  actual_csum = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else if (ret == '$')
+        {
+          /* Detected start of packet. */
+
+          break;
+        }
+    }
+
+  /* Read until checksum */
+
+  *pkt_len = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          /* Error receiving character */
+
+          return ret;
+        }
+      else if (ret == '#')
+        {
+          /* End of packet */
+
+          break;
+        }
+      else
+        {
+          /* Check for space */
+
+          if (*pkt_len >= state->pkt_buf_len)
+            {
+              GDB_DEBUG("packet buffer overflow\n");
+              return -EOVERFLOW;
+            }
+
+          /* Store character and update checksum */
+
+          state->pkt_buf[(*pkt_len)++] = (char) ret;
+        }
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("<- ");
+      for (p = 0; p < *pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Receive the checksum */
+
+  ret = gdb_read(state, buf, sizeof(buf), 2);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_dec_hex(buf, 2, &expected_csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Verify checksum */
+
+  actual_csum = gdb_checksum(state->pkt_buf, *pkt_len);
+  if (actual_csum != expected_csum)
+    {
+      /* Send packet nack */
+
+      GDB_DEBUG("received packet with bad checksum\n");
+      gdb_putchar(state, '-');
+      return -EIO;
+    }
+
+  /* Send packet ack */
+
+  gdb_putchar(state, '+');
+  return 0;
+}
+
+/****************************************************************************
+ * Data Encoding/Decoding
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_enc_hex
+ *
+ * Description:
+ *   Encode data to its hex-value representation in a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer to store the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The data to encode.
+ *   data_len - The length of the data to encode.
+ *
+ * Returned Value:
+ *    The number of bytes written to buf on success.
+ *    -EINVAL if the buffer is too small.
+ *
+ ****************************************************************************/
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len)
+{
+  size_t pos;
+
+  if (buf_len < data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      itoa((data[pos] >> 4) & 0xf, (FAR char *)buf++, 16);
+      itoa((data[pos]) & 0xf, (FAR char *)buf++, 16);
+    }
+
+  return data_len * 2;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *   -EINVAL if the buffer is too small.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t pos;
+  int tmp;
+  char ch[2] =
+    {
+      0, 0
+    };
+
+  if (buf_len != data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      /* Decode high nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] = tmp << 4;
+
+      /* Decode low nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] |= tmp;
+    }
+
+  return data_len;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex2string
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a string.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex2string(FAR const unsigned char *buf, size_t buf_len,
+                              FAR char *data, size_t data_len)
+{
+  for (int i = 0; i < buf_len; i += 2)
+    {
+      char hex_byte[3] =
+        {
+          buf[i], buf[i + 1], '\0'
+        };
+
+      int byte = strtol(hex_byte, NULL, 16);
+
+      data[i / 2] = (char)byte;
+    }
+
+  data[buf_len / 2] = '\0';
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_bin
+ *
+ * Description:
+ *   Decode data from its bin-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *     The number of bytes written to data on success.
+ *    -EOVERFLOW if the output buffer is too small.
+ *    -EINVAL if the input buffer is invalid.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t buf_pos;
+  size_t data_pos;
+
+  for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++)
+    {
+      if (data_pos >= data_len)
+        {
+          /* Output buffer overflow */
+
+          GDB_ASSERT();
+          return -EOVERFLOW;
+        }
+
+      if (buf[buf_pos] == '}')
+        {
+          /* The next byte is escaped! */
+
+          if (buf_pos + 1 >= buf_len)
+            {
+              /* There's an escape character, but no escaped character
+               * following the escape character.
+               */
+
+              GDB_ASSERT();
+              return -EINVAL;
+            }
+
+          buf_pos += 1;
+          data[data_pos++] = buf[buf_pos] ^ 0x20;
+        }
+      else
+        {
+          data[data_pos++] = buf[buf_pos];
+        }
+    }
+
+  return data_pos;
+}
+
+/****************************************************************************
+ * Command Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_mem_read
+ *
+ * Description:
+ *   Read from memory and encode into buf.
+ *
+ * Input Parameters:
+ *   state   - The state structure.
+ *   buf     - The buffer to store the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to read from.
+ *   len     - The length of the data to read.
+ *   enc     - The encoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to buf on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return enc(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Name: gdb_mem_write
+ *
+ * Description:
+ *   Write to memory from encoded buf
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to write to.
+ *   len     - The length of the data to write.
+ *   dec     - The decoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to addr on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return dec(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Packet Creation Helpers
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_send_ok_packet
+ *
+ * Description:
+ *   Send OK packet.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state)
+{
+  state->pkt_buf[0] = 'O';
+  state->pkt_buf[1] = 'K';
+  state->pkt_len = 2;
+
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_signal_packet
+ *
+ * Description:
+ *   Send a signal packet (S AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   signal  - The signal to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'S';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &signal, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_error_packet
+ *
+ * Description:
+ *   Send a error packet (E AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   error   - The error to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'E';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &error, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Communication Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_write
+ *
+ * Description:
+ *   Write a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the data to write.
+ *   len     - The length of the data to write.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len)
+{
+  return state->send(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: gdb_read
+ *
+ * Description:
+ *   Read a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer to store the read data.
+ *   buf_len - The length of the buffer.
+ *   len     - The length of the data to read.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len)
+{
+  if (buf_len < len)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  return state->recv(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: get_register
+ *
+ * Description:
+ *   Get the registers of the specified task.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ ****************************************************************************/
+
+static void get_register(FAR struct gdb_state *state)
+{
+  FAR unsigned int *regs = state->registers;
+  FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
+  FAR uint8_t *reg;
+  int i;
+
+  reg = (FAR uint8_t *)tcb->xcp.regs;
+  if (state->pid == getpid())
+    {
+      reg = (FAR uint8_t *)CURRENT_REGS;
+    }
+
+  for (i = 0; i < g_tcbinfo.total_num; i++)
+    {
+      if (g_tcbinfo.reg_off.p[i] == UINT16_MAX)
+        {
+          regs[i] = 0;
+        }
+      else
+        {
+          regs[i] = *(FAR unsigned int *)(reg + g_tcbinfo.reg_off.p[i]);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_read_register_handle
+ *
+ * Description:
+ *   Read Registers Command Format: g.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  get_register(state);
+
+  /* Encode registers */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                    (FAR unsigned char *)(&(state->registers[0])),
+                    state->size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_register_handle
+ *
+ * Description:
+ *   Write Registers Command Format: G XX...
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ *   Note: This function is not really change the register values.
+ *
+ ****************************************************************************/
+
+static int gdb_write_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  ret = gdb_dec_hex(state->pkt_buf + 1, state->pkt_len - 1,
+                    (FAR unsigned char *)&(state->registers),
+                    sizeof(state->registers));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_one_register_handle
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_one_register_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  get_register(state);
+  if (addr >= state->size)
+    {
+      return 0;
+    }
+
+  /* Read Register */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                   (FAR unsigned char *)&(state->registers[addr]),
+                   sizeof(state->registers[addr]));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_read_mem_handle
+ *
+ * Description:
+ *   Read Memory Command Format: m addr,length
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes read if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+
+  /* Read Memory */
+
+  ret = gdb_mem_read(state, state->pkt_buf, state->pkt_buf_len,
+                     addr, length, gdb_enc_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_mem_handle
+ *
+ * Description:
+ *   Write Memory Command Format: M addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_bin_mem_handle
+ *
+ * Description:
+ *   Write Memory (Binary) Command Format: X addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_bin_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_bin);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_get_thread
+ *
+ * Description:
+ *   Get the all thread id.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct gdb_state *state = arg;
+  char pid = tcb->pid;
+
+  itoa((pid >> 4) & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  itoa(pid & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  state->pkt_buf[state->pkt_len++] = ',';
+}
+
+/****************************************************************************
+ * Name: gdb_query_handle
+ *
+ * Description:
+ *  The query packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_query_handle(FAR struct gdb_state *state)
+{
+  static const char thread_extra_info[] = "ThreadExtraInfo";
+  static const char fthread_info[] = "fThreadInfo";
+  static const char sthread_info[] = "sThreadInfo";
+  static const char r_cmd[] = "Rcmd";
+
+  if (memcmp(&state->pkt_buf[1], fthread_info,
+              sizeof(fthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'm';
+      state->pkt_len = 1;
+      nxsched_foreach(gdb_get_thread, state);
+      state->pkt_len--;
+      state->pkt_buf[state->pkt_len] = 0;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], sthread_info,
+                   sizeof(sthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'l';
+      state->pkt_len = 1;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], thread_extra_info,
+                   sizeof(thread_extra_info) - 1) == 0)
+    {
+      char thread_info[128];
+      FAR struct tcb_s *tcb;
+      int ret;
+      int pid;
+      FAR static const char *thread_state[] =
+        {
+          "Invalid",
+          "Waiting,Unlock",
+          "Ready",
+#ifdef CONFIG_SMP
+          "Assigned",
+#endif
+          "Running",
+          "Inactive",
+          "Waiting,Semaphore",
+          "Waiting,Signal"
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)
+          , "Waiting,MQ empty"
+          , "Waiting,MQ full"
+#endif
+#ifdef CONFIG_PAGING
+          , "Waiting,Paging fill"
+#endif
+#ifdef CONFIG_SIG_SIGSTOP_ACTION
+          , "Stopped"
+#endif
+        };
+
+      state->ptr_next += sizeof(thread_extra_info) + 1;
+      token_expect_integer_arg(state, pid);
+      tcb = nxsched_get_tcb(pid);
+      if (tcb == NULL)
+        {
+          return -EINVAL;
+        }
+
+#if CONFIG_TASK_NAME_SIZE > 0
+      sprintf(thread_info, "Name: %s, State: %s, Priority: %d, Stack: %d",
+              tcb->name, thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#else
+      sprintf(thread_info, "State: %s, Priority: %d, Stack: %d",
+              thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#endif
+
+      ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                        (FAR unsigned char *)thread_info,
+                        strlen(thread_info));
+
+      state->pkt_len = ret;
+      gdb_send_packet(state);
+    }
+  else if (state->monitor_cmd != NULL && memcmp(&state->pkt_buf[1], r_cmd,
+                                                sizeof(r_cmd) - 1) == 0)
+    {
+      /* skip the 'Rcmd,' */
+
+      int len = state->pkt_len - sizeof(r_cmd) - 1;
+      char cmd[64];
+      int ret;
+
+      gdb_dec_hex2string(&state->pkt_buf[sizeof(r_cmd) + 1], len, cmd,
+                         len / 2);
+      ret = state->monitor_cmd(state, cmd);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else
+        {
+          gdb_send_ok_packet(state);
+        }
+    }
+  else
+    {
+      return -EPROTONOSUPPORT;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_is_thread_active_handle
+ *
+ * Description:
+ *  The is thread active packet is used by GDB to request information from
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_is_thread_active_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next = state->pkt_buf + 1;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_thread_context_handle
+ *
+ * Description:
+ *  The thread context packet is used by GDB to request information from
+ *  the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_thread_context_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next += 2;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_send_stop_handle
+ *
+ * Description:
+ *  The stop packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_stop_handle(FAR struct gdb_state *state)
+{
+  return gdb_send_signal_packet(state, 0x00);
+}
+
+/****************************************************************************
+ * Name: gdb_putchar
+ *
+ * Description:
+ *   Put a character to the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   ch      - The character to be sent.
+ *
+ * Returned Value:
+ *   The character sent to the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_putchar(FAR struct gdb_state *state, int ch)
+{
+  unsigned char tmp = ch & 0xff;
+  ssize_t ret;
+
+  ret = state->send(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return ch;
+}
+
+/****************************************************************************
+ * Name: gdb_getchar
+ *
+ * Description:
+ *  Get a character from the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The character read from the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_getchar(FAR struct gdb_state *state)
+{
+  unsigned char tmp;
+  ssize_t ret;
+
+  ret = state->recv(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return tmp;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd)
+{
+  struct gdb_state *state = kmm_zalloc(sizeof(*state) +
+                                       g_tcbinfo.basic_num *
+                                       sizeof(unsigned int));
+  if (state == NULL)
+    {
+      return NULL;
+    }
+
+  state->size = g_tcbinfo.basic_num * sizeof(unsigned int);
+  state->pkt_buf_len = sizeof(state->pkt_buf);
+  state->send = send;
+  state->recv = recv;
+  state->priv = priv;
+  state->monitor_cmd = monitor_cmd;
+
+  return state;
+}
+
+/****************************************************************************
+ * Name: gdb_state_uninit
+ *
+ * Description:
+ *   Uninitialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+void gdb_state_uninit(struct gdb_state *state)
+{
+  if (state == NULL)
+    {
+      return;
+    }
+
+  kmm_free(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_conmsg_packet
+ *
+ * Description:
+ *   Send a message to the GDB console (via O XX... packet).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_send_conmsg_packet(FAR struct gdb_state *state, FAR const char *msg)
+{
+  int ret;
+  int len;
+
+  if (state == NULL || msg == NULL)
+    {
+      return -EINVAL;
+    }
+
+  len = strlen(msg);
+  if (len > state->pkt_buf_len - 1)
+    {
+      return -EOVERFLOW;
+    }
+
+  state->pkt_buf[0] = 'O';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    (FAR unsigned char *)msg, len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_process
+ *
+ * Description:
+ *   Main debug loop. Handles commands.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+int gdb_process(FAR struct gdb_state *state)
+{
+  int ret = 0;
+  int i;
+  static struct gdb_handle_cmd handle_cmd[] =
+    {
+      {
+        .handle = gdb_read_register_handle,
+        .cmd = 'g'
+      },
+      {
+        .handle = gdb_write_register_handle,
+        .cmd = 'G'
+      },
+      {
+        .handle = gdb_read_one_register_handle,
+        .cmd = 'p'
+      },
+      {
+        .handle = gdb_read_mem_handle,
+        .cmd = 'm'
+      },
+      {
+        .handle = gdb_write_mem_handle,
+        .cmd = 'M'
+      },
+      {
+        .handle = gdb_write_bin_mem_handle,
+        .cmd = 'X'
+      },
+      {
+        .handle = gdb_send_stop_handle,
+        .cmd = '?'
+      },
+      {
+        .handle = gdb_query_handle,
+        .cmd = 'q'
+      },
+      {
+        .handle = gdb_is_thread_active_handle,
+        .cmd = 'T'
+      },
+      {
+        .handle = gdb_thread_context_handle,
+        .cmd = 'H'
+      },
+    };
+
+  while (1)
+    {
+      /* Receive the next packet */
+
+      ret = gdb_recv_packet(state, &state->pkt_len);
+      if (ret < 0)
+        {
+          break;
+        }
+
+      if (state->pkt_len == 0)
+        {
+          /* Received empty packet.. */
+
+          continue;
+        }
+
+      state->ptr_next = state->pkt_buf;
+
+      /* Handle one letter commands */
+
+      for (i = 0; i < sizeof(handle_cmd) / sizeof(handle_cmd[0]); i++)

Review Comment:
   nitems(array)



##########
include/nuttx/gdbstub/gdbstub.h:
##########
@@ -0,0 +1,110 @@
+/****************************************************************************
+ * include/nuttx/gdbstub/gdbstub.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_GDBSTUB_GDBSTUB_H
+#define __INCLUDE_NUTTX_GDBSTUB_GDBSTUB_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdlib.h>
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+struct gdb_state;
+
+typedef ssize_t (*gdb_send_func)(FAR void *priv,
+                                 FAR const unsigned char *buf, size_t len);
+typedef ssize_t (*gdb_recv_func)(FAR void *priv, FAR unsigned char *buf,
+                                 size_t len);
+typedef int (*monitor_cmd_func)(FAR struct gdb_state *state, FAR char *cmd);
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd);

Review Comment:
   move parameter priv  to last and rename monitor_cmd to monitor



##########
libs/libc/gdbstub/lib_gdbstub.c:
##########
@@ -0,0 +1,1711 @@
+/****************************************************************************
+ * libs/libc/gdbstub/lib_gdbstub.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <syslog.h>
+#include <sys/types.h>
+
+#include <nuttx/sched.h>
+#include <nuttx/gdbstub/gdbstub.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+#define GDB_DEBUG(...) printf(__VA_ARGS__)
+#define GDB_ASSERT() __assert(__FILE__, __LINE__, 0)
+#else
+#define GDB_DEBUG(...)
+#define GDB_ASSERT() do {} while (0)
+#endif
+
+/* Calculate remaining space in packet from ptr_next position. */
+
+#define token_remaining_buf(state) (state->pkt_len - \
+                                    (state->ptr_next - state->pkt_buf))
+
+/* Expecting a seperator. If not present, go to error */
+
+#define token_expect_seperator(state, c) \
+          { \
+            if (!state->ptr_next || *state->ptr_next != c) \
+              { \
+                return -EINVAL; \
+              } \
+            else \
+              { \
+                  state->ptr_next += 1; \
+              } \
+          }
+
+/* Expecting an integer argument. If not present, go to error */
+
+#define token_expect_integer_arg(state, arg) \
+          { \
+            arg = gdb_strtol((FAR char *)state->ptr_next, \
+                              token_remaining_buf(state), 16, \
+                              (FAR char **)(&state->ptr_next)); \
+            if (!state->ptr_next) \
+              { \
+                return -EINVAL; \
+              } \
+          }
+
+/****************************************************************************
+ * Pre-processor types
+ ****************************************************************************/
+
+extern const struct tcbinfo_s g_tcbinfo;
+
+struct gdb_state
+{
+  /* transport layer */
+
+  CODE ssize_t (*send)(FAR void *priv, FAR const unsigned char *buf,
+                       size_t len);
+  CODE ssize_t (*recv)(FAR void *priv, FAR unsigned char *buf,
+                       size_t len);
+  CODE int (*monitor_cmd)(FAR struct gdb_state *state, FAR char *cmd);
+
+  FAR void *priv;
+
+  /* gdb current thread */
+
+  pid_t pid;
+
+  /* gdb packet */
+
+  FAR unsigned char *ptr_next;
+  unsigned char pkt_buf[256];
+  size_t pkt_buf_len;
+  size_t pkt_len;
+
+  /* registers */
+
+  unsigned int size;
+  FAR unsigned int registers[0];
+};
+
+struct gdb_handle_cmd
+{
+  CODE int (*handle)(FAR struct gdb_state *state);
+  char cmd;
+};
+
+typedef int (*gdb_enc_func)(FAR unsigned char *buf, size_t buf_len,
+                            FAR const unsigned char *data, size_t data_len);
+typedef int (*gdb_dec_func)(FAR unsigned const char *buf, size_t buf_len,
+                            FAR unsigned char *data, size_t data_len);
+
+/****************************************************************************
+ *  GDB Remote Serial Protocol
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* System functions, supported by all stubs */
+
+static int gdb_getchar(FAR struct gdb_state *state);
+static int gdb_putchar(FAR struct gdb_state *state, int ch);
+
+/* Communication functions */
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len);
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len);
+
+/* Packet functions */
+
+static int gdb_send_packet(FAR struct gdb_state *state);
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len);
+static int gdb_checksum(FAR const unsigned char *buf, size_t len);
+static int gdb_recv_ack(FAR struct gdb_state *state);
+
+/* Data encoding/decoding */
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len);
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len);
+
+/* Packet creation helpers */
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state);
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal);
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error);
+
+/* Command functions */
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc);
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * String Processing Helper Functions
+ ****************************************************************************/
+
+static uintptr_t gdb_strtol(FAR char *str, size_t len, int base,
+                            FAR char **endptr)
+{
+  char tmp = str[len];
+  uintptr_t ret;
+
+  /* Ensure string is null terminated
+   * Gdb rsp use \x48 ('0') make sure it's over
+   * */
+
+  str[len] = '\0';
+  ret =  strtoul(str, (FAR char **)endptr, base);
+  str[len] = tmp;
+
+  return ret;
+}
+
+/****************************************************************************
+ * Packet Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_recv_ack
+ *
+ * Description:
+ *   Receive a packet acknowledgment.
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if an ACK (+) was received
+ *   1       if a NACK (-) was received
+ *   -EINVAL if a bad response was received
+ *
+ ****************************************************************************/
+
+static int gdb_recv_ack(FAR struct gdb_state *state)
+{
+  int response;
+
+  /* Wait for packet ack */
+
+  switch (response = gdb_getchar(state))
+    {
+      case '+':
+
+        /* Packet acknowledged */
+
+        return 0;
+      case '-':
+
+        /* Packet negative acknowledged */
+
+        return 1;
+      default:
+
+        /* Bad response! */
+
+        GDB_DEBUG("received bad packet response: 0x%2x\n", response);
+        return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Calculate 8-bit checksum of a buffer.
+ *
+ * Input Parameters:
+ *   buf - The buffer to checksum.
+ *
+ * Returned Value:
+ *   8-bit checksum
+ *
+ ****************************************************************************/
+
+static int gdb_checksum(FAR const unsigned char *buf, size_t len)
+{
+  unsigned char csum = 0;
+
+  while (len--)
+    {
+      csum += *buf++;
+    }
+
+  return csum;
+}
+
+/****************************************************************************
+ * Name: gdb_checksum
+ *
+ * Description:
+ *   Transmits a packet of data.
+ *   Packets are of the form: $<packet-data>#<checksum>
+ *
+ * Input Parameters:
+ *   state - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0       if the packet was transmitted and acknowledged.
+ *   1       if the packet was transmitted but not acknowledged.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_packet(FAR struct gdb_state *state)
+{
+  unsigned char buf[3];
+  unsigned char csum;
+  int ret;
+
+  /* Send packet start */
+
+  ret = gdb_putchar(state, '$');
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("-> ");
+      for (p = 0; p < state->pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Send packet data */
+
+  ret = gdb_write(state, state->pkt_buf, state->pkt_len);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Send the checksum */
+
+  buf[0] = '#';
+  csum = gdb_checksum(state->pkt_buf, state->pkt_len);
+
+  ret = gdb_enc_hex(buf + 1, sizeof(buf) - 1, &csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_write(state, buf, sizeof(buf));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return gdb_recv_ack(state);
+}
+
+/****************************************************************************
+ * Name: gdb_recv_packet
+ *
+ * Description:
+ *   Receives a packet of data, assuming a 7-bit clean connection.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   pkt_len - The length of the packet received.
+ *
+ * Returned Value:
+ *   0          if the packet was received.
+ *   -EIO       if the packet was not received.
+ *   -EOVERFLOW if the packet was too long.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_recv_packet(FAR struct gdb_state *state,
+                           FAR size_t *pkt_len)
+{
+  unsigned char expected_csum;
+  unsigned char actual_csum;
+  unsigned char buf[2];
+  int ret;
+
+  /* Wait for packet start */
+
+  actual_csum = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else if (ret == '$')
+        {
+          /* Detected start of packet. */
+
+          break;
+        }
+    }
+
+  /* Read until checksum */
+
+  *pkt_len = 0;
+  while (1)
+    {
+      ret = gdb_getchar(state);
+      if (ret < 0)
+        {
+          /* Error receiving character */
+
+          return ret;
+        }
+      else if (ret == '#')
+        {
+          /* End of packet */
+
+          break;
+        }
+      else
+        {
+          /* Check for space */
+
+          if (*pkt_len >= state->pkt_buf_len)
+            {
+              GDB_DEBUG("packet buffer overflow\n");
+              return -EOVERFLOW;
+            }
+
+          /* Store character and update checksum */
+
+          state->pkt_buf[(*pkt_len)++] = (char) ret;
+        }
+    }
+
+#ifdef CONFIG_LIB_GDBSTUB_DEBUG
+    {
+      size_t p;
+      GDB_DEBUG("<- ");
+      for (p = 0; p < *pkt_len; p++)
+        {
+          if (isprint(state->pkt_buf[p]))
+            {
+              GDB_DEBUG("%c", state->pkt_buf[p]);
+            }
+          else
+            {
+              GDB_DEBUG("\\x%02x", state->pkt_buf[p] & 0xff);
+            }
+        }
+
+      GDB_DEBUG("\n");
+    }
+#endif
+
+  /* Receive the checksum */
+
+  ret = gdb_read(state, buf, sizeof(buf), 2);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = gdb_dec_hex(buf, 2, &expected_csum, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Verify checksum */
+
+  actual_csum = gdb_checksum(state->pkt_buf, *pkt_len);
+  if (actual_csum != expected_csum)
+    {
+      /* Send packet nack */
+
+      GDB_DEBUG("received packet with bad checksum\n");
+      gdb_putchar(state, '-');
+      return -EIO;
+    }
+
+  /* Send packet ack */
+
+  gdb_putchar(state, '+');
+  return 0;
+}
+
+/****************************************************************************
+ * Data Encoding/Decoding
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_enc_hex
+ *
+ * Description:
+ *   Encode data to its hex-value representation in a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer to store the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The data to encode.
+ *   data_len - The length of the data to encode.
+ *
+ * Returned Value:
+ *    The number of bytes written to buf on success.
+ *    -EINVAL if the buffer is too small.
+ *
+ ****************************************************************************/
+
+static int gdb_enc_hex(FAR unsigned char *buf, size_t buf_len,
+                       FAR const unsigned char *data, size_t data_len)
+{
+  size_t pos;
+
+  if (buf_len < data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      itoa((data[pos] >> 4) & 0xf, (FAR char *)buf++, 16);
+      itoa((data[pos]) & 0xf, (FAR char *)buf++, 16);
+    }
+
+  return data_len * 2;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *   -EINVAL if the buffer is too small.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t pos;
+  int tmp;
+  char ch[2] =
+    {
+      0, 0
+    };
+
+  if (buf_len != data_len * 2)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  for (pos = 0; pos < data_len; pos++)
+    {
+      /* Decode high nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] = tmp << 4;
+
+      /* Decode low nibble */
+
+      ch[0] = *buf++;
+      tmp = strtoul((FAR char *)&ch, NULL, 16);
+      if (tmp == 0 && errno)
+        {
+          /* Buffer contained junk. */
+
+          GDB_ASSERT();
+          return -errno;
+        }
+
+      data[pos] |= tmp;
+    }
+
+  return data_len;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_hex2string
+ *
+ * Description:
+ *   Decode data from its hex-value representation to a string.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *   The number of bytes written to data on success.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_hex2string(FAR const unsigned char *buf, size_t buf_len,
+                              FAR char *data, size_t data_len)
+{
+  for (int i = 0; i < buf_len; i += 2)
+    {
+      char hex_byte[3] =
+        {
+          buf[i], buf[i + 1], '\0'
+        };
+
+      int byte = strtol(hex_byte, NULL, 16);
+
+      data[i / 2] = (char)byte;
+    }
+
+  data[buf_len / 2] = '\0';
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_dec_bin
+ *
+ * Description:
+ *   Decode data from its bin-value representation to a buffer.
+ *
+ * Input Parameters:
+ *   buf      - The buffer containing the encoded data.
+ *   buf_len  - The length of the buffer.
+ *   data     - The buffer to store the decoded data.
+ *   data_len - The length of the data to decode.
+ *
+ * Returned Value:
+ *     The number of bytes written to data on success.
+ *    -EOVERFLOW if the output buffer is too small.
+ *    -EINVAL if the input buffer is invalid.
+ *
+ ****************************************************************************/
+
+static int gdb_dec_bin(FAR const unsigned char *buf, size_t buf_len,
+                       FAR unsigned char *data, size_t data_len)
+{
+  size_t buf_pos;
+  size_t data_pos;
+
+  for (buf_pos = 0, data_pos = 0; buf_pos < buf_len; buf_pos++)
+    {
+      if (data_pos >= data_len)
+        {
+          /* Output buffer overflow */
+
+          GDB_ASSERT();
+          return -EOVERFLOW;
+        }
+
+      if (buf[buf_pos] == '}')
+        {
+          /* The next byte is escaped! */
+
+          if (buf_pos + 1 >= buf_len)
+            {
+              /* There's an escape character, but no escaped character
+               * following the escape character.
+               */
+
+              GDB_ASSERT();
+              return -EINVAL;
+            }
+
+          buf_pos += 1;
+          data[data_pos++] = buf[buf_pos] ^ 0x20;
+        }
+      else
+        {
+          data[data_pos++] = buf[buf_pos];
+        }
+    }
+
+  return data_pos;
+}
+
+/****************************************************************************
+ * Command Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_mem_read
+ *
+ * Description:
+ *   Read from memory and encode into buf.
+ *
+ * Input Parameters:
+ *   state   - The state structure.
+ *   buf     - The buffer to store the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to read from.
+ *   len     - The length of the data to read.
+ *   enc     - The encoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to buf on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                        size_t buf_len, uintptr_t addr,
+                        size_t len, gdb_enc_func enc)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return enc(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Name: gdb_mem_write
+ *
+ * Description:
+ *   Write to memory from encoded buf
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the encoded data.
+ *   buf_len - The length of the buffer.
+ *   addr    - The address to write to.
+ *   len     - The length of the data to write.
+ *   dec     - The decoding function to use.
+ *
+ * Returned Value:
+ *   The number of bytes written to addr on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_mem_write(FAR struct gdb_state *state,
+                         FAR const unsigned char *buf,
+                         size_t buf_len, uintptr_t addr,
+                         size_t len, gdb_dec_func dec)
+{
+  /* Check if the address is valid */
+
+  if (addr >= UINTPTR_MAX - 0xf || addr == 0)
+    {
+      return -EINVAL;
+    }
+
+  return dec(buf, buf_len, (FAR void *)addr, len);
+}
+
+/****************************************************************************
+ * Packet Creation Helpers
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_send_ok_packet
+ *
+ * Description:
+ *   Send OK packet.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *    Zero on success.
+ *    Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_ok_packet(FAR struct gdb_state *state)
+{
+  state->pkt_buf[0] = 'O';
+  state->pkt_buf[1] = 'K';
+  state->pkt_len = 2;
+
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_signal_packet
+ *
+ * Description:
+ *   Send a signal packet (S AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   signal  - The signal to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_signal_packet(FAR struct gdb_state *state,
+                                  unsigned char signal)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'S';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &signal, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Name: gdb_send_error_packet
+ *
+ * Description:
+ *   Send a error packet (E AA).
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   error   - The error to send.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_error_packet(FAR struct gdb_state *state,
+                                 unsigned char error)
+{
+  int ret;
+
+  state->pkt_buf[0] = 'E';
+  ret = gdb_enc_hex(&state->pkt_buf[1], state->pkt_buf_len - 1,
+                    &error, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = 1 + ret;
+  return gdb_send_packet(state);
+}
+
+/****************************************************************************
+ * Communication Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_write
+ *
+ * Description:
+ *   Write a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer containing the data to write.
+ *   len     - The length of the data to write.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write(FAR struct gdb_state *state,
+                     FAR const unsigned char *buf, size_t len)
+{
+  return state->send(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: gdb_read
+ *
+ * Description:
+ *   Read a sequence of bytes.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   buf     - The buffer to store the read data.
+ *   buf_len - The length of the buffer.
+ *   len     - The length of the data to read.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read(FAR struct gdb_state *state, FAR unsigned char *buf,
+                    size_t buf_len, size_t len)
+{
+  if (buf_len < len)
+    {
+      /* Buffer too small */
+
+      return -EINVAL;
+    }
+
+  return state->recv(state->priv, buf, len);
+}
+
+/****************************************************************************
+ * Name: get_register
+ *
+ * Description:
+ *   Get the registers of the specified task.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ ****************************************************************************/
+
+static void get_register(FAR struct gdb_state *state)
+{
+  FAR unsigned int *regs = state->registers;
+  FAR struct tcb_s *tcb = nxsched_get_tcb(state->pid);
+  FAR uint8_t *reg;
+  int i;
+
+  reg = (FAR uint8_t *)tcb->xcp.regs;
+  if (state->pid == getpid())
+    {
+      reg = (FAR uint8_t *)CURRENT_REGS;
+    }
+
+  for (i = 0; i < g_tcbinfo.total_num; i++)
+    {
+      if (g_tcbinfo.reg_off.p[i] == UINT16_MAX)
+        {
+          regs[i] = 0;
+        }
+      else
+        {
+          regs[i] = *(FAR unsigned int *)(reg + g_tcbinfo.reg_off.p[i]);
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: gdb_read_register_handle
+ *
+ * Description:
+ *   Read Registers Command Format: g.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  get_register(state);
+
+  /* Encode registers */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                    (FAR unsigned char *)(&(state->registers[0])),
+                    state->size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_register_handle
+ *
+ * Description:
+ *   Write Registers Command Format: G XX...
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ *   Note: This function is not really change the register values.
+ *
+ ****************************************************************************/
+
+static int gdb_write_register_handle(FAR struct gdb_state *state)
+{
+  int ret;
+
+  ret = gdb_dec_hex(state->pkt_buf + 1, state->pkt_len - 1,
+                    (FAR unsigned char *)&(state->registers),
+                    sizeof(state->registers));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_write_one_register_handle
+ *
+ * Description:
+ *   Write a Register Command Format: P n.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   Zero on success.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_one_register_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  get_register(state);
+  if (addr >= state->size)
+    {
+      return 0;
+    }
+
+  /* Read Register */
+
+  ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                   (FAR unsigned char *)&(state->registers[addr]),
+                   sizeof(state->registers[addr]));
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_read_mem_handle
+ *
+ * Description:
+ *   Read Memory Command Format: m addr,length
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes read if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_read_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+
+  /* Read Memory */
+
+  ret = gdb_mem_read(state, state->pkt_buf, state->pkt_buf_len,
+                     addr, length, gdb_enc_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  state->pkt_len = ret;
+  gdb_send_packet(state);
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_mem_handle
+ *
+ * Description:
+ *   Write Memory Command Format: M addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_hex);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_write_bin_mem_handle
+ *
+ * Description:
+ *   Write Memory (Binary) Command Format: X addr,length:XX..
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The number of bytes written if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_write_bin_mem_handle(FAR struct gdb_state *state)
+{
+  uintptr_t addr;
+  size_t length;
+  int ret;
+
+  state->ptr_next += 1;
+  token_expect_integer_arg(state, addr);
+  token_expect_seperator(state, ',');
+  token_expect_integer_arg(state, length);
+  token_expect_seperator(state, ':');
+
+  /* Write Memory */
+
+  ret = gdb_mem_write(state, state->ptr_next, token_remaining_buf(state),
+                      addr, length, gdb_dec_bin);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  gdb_send_ok_packet(state);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gdb_get_thread
+ *
+ * Description:
+ *   Get the all thread id.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ ****************************************************************************/
+
+static void gdb_get_thread(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct gdb_state *state = arg;
+  char pid = tcb->pid;
+
+  itoa((pid >> 4) & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  itoa(pid & 0xf, (FAR char *)&state->pkt_buf[state->pkt_len++], 16);
+  state->pkt_buf[state->pkt_len++] = ',';
+}
+
+/****************************************************************************
+ * Name: gdb_query_handle
+ *
+ * Description:
+ *  The query packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_query_handle(FAR struct gdb_state *state)
+{
+  static const char thread_extra_info[] = "ThreadExtraInfo";
+  static const char fthread_info[] = "fThreadInfo";
+  static const char sthread_info[] = "sThreadInfo";
+  static const char r_cmd[] = "Rcmd";
+
+  if (memcmp(&state->pkt_buf[1], fthread_info,
+              sizeof(fthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'm';
+      state->pkt_len = 1;
+      nxsched_foreach(gdb_get_thread, state);
+      state->pkt_len--;
+      state->pkt_buf[state->pkt_len] = 0;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], sthread_info,
+                   sizeof(sthread_info) - 1) == 0)
+    {
+      /* Return the current thread */
+
+      state->pkt_buf[0] = 'l';
+      state->pkt_len = 1;
+      gdb_send_packet(state);
+    }
+  else if (memcmp(&state->pkt_buf[1], thread_extra_info,
+                   sizeof(thread_extra_info) - 1) == 0)
+    {
+      char thread_info[128];
+      FAR struct tcb_s *tcb;
+      int ret;
+      int pid;
+      FAR static const char *thread_state[] =
+        {
+          "Invalid",
+          "Waiting,Unlock",
+          "Ready",
+#ifdef CONFIG_SMP
+          "Assigned",
+#endif
+          "Running",
+          "Inactive",
+          "Waiting,Semaphore",
+          "Waiting,Signal"
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)
+          , "Waiting,MQ empty"
+          , "Waiting,MQ full"
+#endif
+#ifdef CONFIG_PAGING
+          , "Waiting,Paging fill"
+#endif
+#ifdef CONFIG_SIG_SIGSTOP_ACTION
+          , "Stopped"
+#endif
+        };
+
+      state->ptr_next += sizeof(thread_extra_info) + 1;
+      token_expect_integer_arg(state, pid);
+      tcb = nxsched_get_tcb(pid);
+      if (tcb == NULL)
+        {
+          return -EINVAL;
+        }
+
+#if CONFIG_TASK_NAME_SIZE > 0
+      sprintf(thread_info, "Name: %s, State: %s, Priority: %d, Stack: %d",
+              tcb->name, thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#else
+      sprintf(thread_info, "State: %s, Priority: %d, Stack: %d",
+              thread_state[tcb->task_state], tcb->sched_priority,
+              tcb->adj_stack_size);
+#endif
+
+      ret = gdb_enc_hex(state->pkt_buf, state->pkt_buf_len,
+                        (FAR unsigned char *)thread_info,
+                        strlen(thread_info));
+
+      state->pkt_len = ret;
+      gdb_send_packet(state);
+    }
+  else if (state->monitor_cmd != NULL && memcmp(&state->pkt_buf[1], r_cmd,
+                                                sizeof(r_cmd) - 1) == 0)
+    {
+      /* skip the 'Rcmd,' */
+
+      int len = state->pkt_len - sizeof(r_cmd) - 1;
+      char cmd[64];
+      int ret;
+
+      gdb_dec_hex2string(&state->pkt_buf[sizeof(r_cmd) + 1], len, cmd,
+                         len / 2);
+      ret = state->monitor_cmd(state, cmd);
+      if (ret < 0)
+        {
+          return ret;
+        }
+      else
+        {
+          gdb_send_ok_packet(state);
+        }
+    }
+  else
+    {
+      return -EPROTONOSUPPORT;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_is_thread_active_handle
+ *
+ * Description:
+ *  The is thread active packet is used by GDB to request information from
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_is_thread_active_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next = state->pkt_buf + 1;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_thread_context_handle
+ *
+ * Description:
+ *  The thread context packet is used by GDB to request information from
+ *  the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_thread_context_handle(FAR struct gdb_state *state)
+{
+  FAR struct tcb_s *tcb;
+  int pid;
+
+  state->ptr_next += 2;
+  token_expect_integer_arg(state, pid);
+
+  tcb = nxsched_get_tcb(pid);
+  if (tcb == NULL)
+    {
+      return -EINVAL;
+    }
+
+  state->pid = pid;
+  gdb_send_ok_packet(state);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: gdb_send_stop_handle
+ *
+ * Description:
+ *  The stop packet is used by GDB to request information from the stub.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   0  if successful.
+ *   Negative value on error.
+ *
+ ****************************************************************************/
+
+static int gdb_send_stop_handle(FAR struct gdb_state *state)
+{
+  return gdb_send_signal_packet(state, 0x00);
+}
+
+/****************************************************************************
+ * Name: gdb_putchar
+ *
+ * Description:
+ *   Put a character to the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *   ch      - The character to be sent.
+ *
+ * Returned Value:
+ *   The character sent to the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_putchar(FAR struct gdb_state *state, int ch)
+{
+  unsigned char tmp = ch & 0xff;
+  ssize_t ret;
+
+  ret = state->send(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return ch;
+}
+
+/****************************************************************************
+ * Name: gdb_getchar
+ *
+ * Description:
+ *  Get a character from the GDB server.
+ *
+ * Input Parameters:
+ *   state   - The pointer to the GDB state structure.
+ *
+ * Returned Value:
+ *   The character read from the GDB server.
+ *
+ ****************************************************************************/
+
+static int gdb_getchar(FAR struct gdb_state *state)
+{
+  unsigned char tmp;
+  ssize_t ret;
+
+  ret = state->recv(state->priv, &tmp, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return tmp;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gdb_state_init
+ *
+ * Description:
+ *   Initialize the GDB state structure.
+ *
+ * Input Parameters:
+ *   send        - The pointer to the send function.
+ *   recv        - The pointer to the receive function.
+ *   priv        - The pointer to the private data.
+ *   monitor_cmd - The pointer to the monitor_cmd function.
+ *
+ * Returned Value:
+ *   The pointer to the GDB state structure on success.
+ *   NULL on failure.
+ *
+ ****************************************************************************/
+
+struct gdb_state *gdb_state_init(gdb_send_func send, gdb_recv_func recv,
+                                 void *priv, monitor_cmd_func monitor_cmd)
+{
+  struct gdb_state *state = kmm_zalloc(sizeof(*state) +
+                                       g_tcbinfo.basic_num *
+                                       sizeof(unsigned int));
+  if (state == NULL)
+    {
+      return NULL;
+    }
+
+  state->size = g_tcbinfo.basic_num * sizeof(unsigned int);

Review Comment:
   sizeof(uintptr_t);



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org