qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Nicolas Eder <nicolas.eder@lauterbach.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Christian Boenig" <christian.boenig@lauterbach.com>,
	"Nicolas Eder" <nicolas.eder@lauterbach.com>
Subject: [PATCH v5 02/18] gdbstub: hex conversion functions moved to cutils.h
Date: Wed, 20 Dec 2023 17:25:39 +0100	[thread overview]
Message-ID: <20231220162555.19545-3-nicolas.eder@lauterbach.com> (raw)
In-Reply-To: <20231220162555.19545-1-nicolas.eder@lauterbach.com>

---
 debug/gdbstub/gdbstub.c   | 19 ++++++++++---------
 debug/gdbstub/internals.h | 26 --------------------------
 include/qemu/cutils.h     | 30 ++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 35 deletions(-)

diff --git a/debug/gdbstub/gdbstub.c b/debug/gdbstub/gdbstub.c
index 46d752bbc2..f43d4355c0 100644
--- a/debug/gdbstub/gdbstub.c
+++ b/debug/gdbstub/gdbstub.c
@@ -80,8 +80,8 @@ void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
     int i, c;
     for(i = 0; i < len; i++) {
         c = mem[i];
-        g_string_append_c(buf, tohex(c >> 4));
-        g_string_append_c(buf, tohex(c & 0xf));
+        g_string_append_c(buf, nibble_to_hexchar(c >> 4));
+        g_string_append_c(buf, nibble_to_hexchar(c & 0xf));
     }
     g_string_append_c(buf, '\0');
 }
@@ -91,7 +91,8 @@ void gdb_hextomem(GByteArray *mem, const char *buf, int len)
     int i;
 
     for(i = 0; i < len; i++) {
-        guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
+        guint8 byte = hexchar_to_nibble(buf[0]) << 4 |
+                      hexchar_to_nibble(buf[1]);
         g_byte_array_append(mem, &byte, 1);
         buf += 2;
     }
@@ -118,8 +119,8 @@ static void hexdump(const char *buf, int len,
         if (i < len) {
             char value = buf[i];
 
-            line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
-            line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
+            line_buffer[hex_col + 0] = nibble_to_hexchar((value >> 4) & 0xF);
+            line_buffer[hex_col + 1] = nibble_to_hexchar((value >> 0) & 0xF);
             line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
                     ? value
                     : '.';
@@ -151,8 +152,8 @@ int gdb_put_packet_binary(const char *buf, int len, bool dump)
             csum += buf[i];
         }
         footer[0] = '#';
-        footer[1] = tohex((csum >> 4) & 0xf);
-        footer[2] = tohex((csum) & 0xf);
+        footer[1] = nibble_to_hexchar((csum >> 4) & 0xf);
+        footer[2] = nibble_to_hexchar((csum) & 0xf);
         g_byte_array_append(gdbserver_state.last_packet, footer, 3);
 
         gdb_put_buffer(gdbserver_state.last_packet->data,
@@ -2267,7 +2268,7 @@ void gdb_read_byte(uint8_t ch)
                 break;
             }
             gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
-            gdbserver_state.line_csum = fromhex(ch) << 4;
+            gdbserver_state.line_csum = hexchar_to_nibble(ch) << 4;
             gdbserver_state.state = RS_CHKSUM2;
             break;
         case RS_CHKSUM2:
@@ -2277,7 +2278,7 @@ void gdb_read_byte(uint8_t ch)
                 gdbserver_state.state = RS_GETLINE;
                 break;
             }
-            gdbserver_state.line_csum |= fromhex(ch);
+            gdbserver_state.line_csum |= hexchar_to_nibble(ch);
 
             if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
                 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
diff --git a/debug/gdbstub/internals.h b/debug/gdbstub/internals.h
index 5c0c725e54..4b67adfeda 100644
--- a/debug/gdbstub/internals.h
+++ b/debug/gdbstub/internals.h
@@ -75,32 +75,6 @@ typedef struct GDBState {
 /* lives in main gdbstub.c */
 extern GDBState gdbserver_state;
 
-/*
- * Inline utility function, convert from int to hex and back
- */
-
-static inline int fromhex(int v)
-{
-    if (v >= '0' && v <= '9') {
-        return v - '0';
-    } else if (v >= 'A' && v <= 'F') {
-        return v - 'A' + 10;
-    } else if (v >= 'a' && v <= 'f') {
-        return v - 'a' + 10;
-    } else {
-        return 0;
-    }
-}
-
-static inline int tohex(int v)
-{
-    if (v < 10) {
-        return v + '0';
-    } else {
-        return v - 10 + 'a';
-    }
-}
-
 /*
  * Connection helpers for both system and user backends
  */
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 92c927a6a3..5ab1a4ffb0 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -267,4 +267,34 @@ void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,
 void qemu_hexdump(FILE *fp, const char *prefix,
                   const void *bufptr, size_t size);
 
+
+/**
+ * hexchar_to_nibble() - Converts hex character to nibble.
+ */
+static inline int hexchar_to_nibble(int v)
+{
+    if (v >= '0' && v <= '9') {
+        return v - '0';
+    } else if (v >= 'A' && v <= 'F') {
+        return v - 'A' + 10;
+    } else if (v >= 'a' && v <= 'f') {
+        return v - 'a' + 10;
+    } else {
+        g_assert_not_reached();
+    }
+}
+
+/**
+ * nibble_to_hexchar() - Converts nibble to hex character.
+ */
+static inline int nibble_to_hexchar(int v)
+{
+    g_assert(v <= 0xf);
+    if (v < 10) {
+        return v + '0';
+    } else {
+        return v - 10 + 'a';
+    }
+}
+
 #endif
-- 
2.34.1



  parent reply	other threads:[~2023-12-20 16:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-20 16:25 [PATCH v5 00/18] first version of mcdstub Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 01/18] gdbstub, mcdstub: file and build structure adapted to accomodate for the mcdstub Nicolas Eder
2024-02-29 15:33   ` Alex Bennée
2023-12-20 16:25 ` Nicolas Eder [this message]
2024-02-29 15:33   ` [PATCH v5 02/18] gdbstub: hex conversion functions moved to cutils.h Alex Bennée
2023-12-20 16:25 ` [PATCH v5 03/18] gdbstub: GDBRegisterState moved to gdbstub.h so it can be used outside of the gdbstub Nicolas Eder
2024-02-29 15:23   ` Alex Bennée
2023-12-20 16:25 ` [PATCH v5 04/18] gdbstub: DebugClass added to system mode Nicolas Eder
2024-02-29 16:35   ` Alex Bennée
2024-03-05 11:27     ` nicolas.eder
2023-12-20 16:25 ` [PATCH v5 05/18] mcdstub: memory helper functions added Nicolas Eder
2024-02-29 16:56   ` Alex Bennée
2024-03-05 11:03     ` nicolas.eder
2023-12-20 16:25 ` [PATCH v5 06/18] mcdstub: -mcd start option added, mcd specific defines added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 07/18] mcdstub: mcdserver initialization functions added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 08/18] cutils: qemu_strtou32 function added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 09/18] mcdstub: TCP packet plumbing added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 10/18] mcdstub: open and close server functions added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 11/18] mcdstub: system and core queries added Nicolas Eder
2024-02-29 15:11   ` Alex Bennée
2024-03-05 11:08     ` nicolas.eder
2023-12-20 16:25 ` [PATCH v5 12/18] mcdstub: all core specific " Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 13/18] mcdstub: go, step and break added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 14/18] mcdstub: state query added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 15/18] mcdstub: skeleton for reset handling added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 16/18] mcdstub: register access added Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 17/18] mcdstub: memory " Nicolas Eder
2023-12-20 16:25 ` [PATCH v5 18/18] mcdstub: break/watchpoints added Nicolas Eder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231220162555.19545-3-nicolas.eder@lauterbach.com \
    --to=nicolas.eder@lauterbach.com \
    --cc=alex.bennee@linaro.org \
    --cc=christian.boenig@lauterbach.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).