This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit ef8da11d1904830a2e4653ddc1c43e9c46b502f8
Author: chenxiaoyi <[email protected]>
AuthorDate: Sun Mar 30 14:07:46 2025 +0800

    libc/gdbstub: read memory in 4-byte alignment
    
    Some memory, such as the IROM of some chips, cannot be read one byte at
    a time, so GDB could not read it through gdb_bin2hex(), which read the
    data byte by byte.  Read the data as aligned 32-bit words instead.
    
    Signed-off-by: chenxiaoyi <[email protected]>
---
 libs/libc/gdbstub/lib_gdbstub.c | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c
index c10984011c6..964eea08a54 100644
--- a/libs/libc/gdbstub/lib_gdbstub.c
+++ b/libs/libc/gdbstub/lib_gdbstub.c
@@ -686,22 +686,51 @@ static int gdb_recv_ack(FAR struct gdb_state_s *state)
 static ssize_t gdb_bin2hex(FAR void *buf, size_t buf_len,
                            FAR const void *data, size_t data_len)
 {
-  FAR const char *in = data;
+  size_t skip = (uintptr_t)data % 4;
+  size_t ret = data_len * 2;
   FAR char *out = buf;
-  size_t pos;
+  size_t total;
+  uint32_t in;
 
   if (buf_len < data_len * 2)
     {
       return -EOVERFLOW; /* Buffer too small */
     }
 
-  for (pos = 0; pos < data_len; pos++)
+  if (skip != 0)
+    {
+      data = (FAR const void *)((uintptr_t)data & ~3);
+      data_len += skip;
+    }
+
+  total = data_len;
+
+  for (size_t i = 0; i < (data_len + 3) / 4; i++)
     {
-      itoa(in[pos] >> 4 & 0xf, out++, 16);
-      itoa(in[pos] & 0xf, out++, 16);
+      in = *((FAR uint32_t *)data + i);
+
+      for (size_t j = 0; j < 4; j++)
+        {
+          if (skip == 0)
+            {
+              itoa(in >> 4 & 0xf, out++, 16);
+              itoa(in & 0xf, out++, 16);
+            }
+          else
+            {
+              skip--;
+            }
+
+          in >>= 8;
+
+          if (--total == 0)
+            {
+              break;
+            }
+        }
     }
 
-  return data_len * 2;
+  return ret;
 }
 
 /****************************************************************************

Reply via email to