qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Yodel Eldar via <qemu-devel@nongnu.org>
To: qemu-devel@nongnu.org
Cc: "Yodel Eldar" <yodel.eldar@yodel.dev>,
	"Dominik 'Disconnect3d' Czarnota" <dominik.b.czarnota@gmail.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [PATCH] gdbstub/user-target: Map errno values from the host OS to GDB
Date: Wed, 15 Oct 2025 11:25:20 -0500	[thread overview]
Message-ID: <20251015162520.15736-1-yodel.eldar@yodel.dev> (raw)

This patch introduces the function "gdb_host_errno_to_gdb" that maps
host-dependent errno values to their GDB protocol-specific
representations as listed in the GDB manual [1].

The stub now uses the correct GDB errno values in F reply packets.

[1] https://sourceware.org/gdb/current/onlinedocs/gdb.html/Errno-Values.html

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2751
Reported-by: Dominik 'Disconnect3d' Czarnota <dominik.b.czarnota@gmail.com>
Signed-off-by: Yodel Eldar <yodel.eldar@yodel.dev>
---
 gdbstub/user-target.c | 93 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 89 insertions(+), 4 deletions(-)

diff --git a/gdbstub/user-target.c b/gdbstub/user-target.c
index 43231e695e..29feb0509c 100644
--- a/gdbstub/user-target.c
+++ b/gdbstub/user-target.c
@@ -302,6 +302,87 @@ static void hostio_reply_with_data(const void *buf, size_t n)
                           gdbserver_state.str_buf->len, true);
 }
 
+/*
+ * Map host error numbers to their GDB protocol counterparts.
+ * For the list of GDB File-I/O supported error numbers, please consult:
+ * https://sourceware.org/gdb/current/onlinedocs/gdb.html/Errno-Values.html
+ */
+
+static int gdb_host_errno_to_gdb(int errnum)
+{
+    enum {
+        GDB_EPERM        =    1,
+        GDB_ENOENT       =    2,
+        GDB_EINTR        =    4,
+        GDB_EIO          =    5,
+        GDB_EBADF        =    9,
+        GDB_EACCES       =   13,
+        GDB_EFAULT       =   14,
+        GDB_EBUSY        =   16,
+        GDB_EEXIST       =   17,
+        GDB_ENODEV       =   19,
+        GDB_ENOTDIR      =   20,
+        GDB_EISDIR       =   21,
+        GDB_EINVAL       =   22,
+        GDB_ENFILE       =   23,
+        GDB_EMFILE       =   24,
+        GDB_EFBIG        =   27,
+        GDB_ENOSPC       =   28,
+        GDB_ESPIPE       =   29,
+        GDB_EROFS        =   30,
+        GDB_ENOSYS       =   88,
+        GDB_ENAMETOOLONG =   91,
+        GDB_EUNKNOWN     = 9999,
+    };
+
+    switch (errnum) {
+    case EPERM:
+        return GDB_EPERM;
+    case ENOENT:
+        return GDB_ENOENT;
+    case EINTR:
+        return GDB_EINTR;
+    case EIO:
+        return GDB_EIO;
+    case EBADF:
+        return GDB_EBADF;
+    case EACCES:
+        return GDB_EACCES;
+    case EFAULT:
+        return GDB_EFAULT;
+    case EBUSY:
+        return GDB_EBUSY;
+    case EEXIST:
+        return GDB_EEXIST;
+    case ENODEV:
+        return GDB_ENODEV;
+    case ENOTDIR:
+        return GDB_ENOTDIR;
+    case EISDIR:
+        return GDB_EISDIR;
+    case EINVAL:
+        return GDB_EINVAL;
+    case ENFILE:
+        return GDB_ENFILE;
+    case EMFILE:
+        return GDB_EMFILE;
+    case EFBIG:
+        return GDB_EFBIG;
+    case ENOSPC:
+        return GDB_ENOSPC;
+    case ESPIPE:
+        return GDB_ESPIPE;
+    case EROFS:
+        return GDB_EROFS;
+    case ENOSYS:
+        return GDB_ENOSYS;
+    case ENAMETOOLONG:
+        return GDB_ENAMETOOLONG;
+    default:
+        return GDB_EUNKNOWN;
+    }
+}
+
 void gdb_handle_v_file_open(GArray *params, void *user_ctx)
 {
     const char *filename = get_filename_param(params, 0);
@@ -315,7 +396,8 @@ void gdb_handle_v_file_open(GArray *params, void *user_ctx)
     int fd = open(filename, flags, mode);
 #endif
     if (fd < 0) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = gdb_host_errno_to_gdb(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
     } else {
         g_string_printf(gdbserver_state.str_buf, "F%x", fd);
     }
@@ -327,7 +409,8 @@ void gdb_handle_v_file_close(GArray *params, void *user_ctx)
     int fd = gdb_get_cmd_param(params, 0)->val_ul;
 
     if (close(fd) == -1) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = gdb_host_errno_to_gdb(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
         gdb_put_strbuf();
         return;
     }
@@ -350,7 +433,8 @@ void gdb_handle_v_file_pread(GArray *params, void *user_ctx)
 
     ssize_t n = pread(fd, buf, bufsiz, offset);
     if (n < 0) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = gdb_host_errno_to_gdb(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
         gdb_put_strbuf();
         return;
     }
@@ -373,7 +457,8 @@ void gdb_handle_v_file_readlink(GArray *params, void *user_ctx)
     ssize_t n = readlink(filename, buf, BUFSIZ);
 #endif
     if (n < 0) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = gdb_host_errno_to_gdb(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
         gdb_put_strbuf();
         return;
     }
-- 
2.51.0



             reply	other threads:[~2025-10-15 16:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-15 16:25 Yodel Eldar via [this message]
2025-10-16 14:56 ` [PATCH] gdbstub/user-target: Map errno values from the host OS to GDB Alex Bennée
2025-10-16 20:51   ` Yodel Eldar via
2025-10-16 21:51     ` Richard Henderson
2025-10-16 22:41       ` Yodel Eldar via

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=20251015162520.15736-1-yodel.eldar@yodel.dev \
    --to=qemu-devel@nongnu.org \
    --cc=alex.bennee@linaro.org \
    --cc=dominik.b.czarnota@gmail.com \
    --cc=philmd@linaro.org \
    --cc=yodel.eldar@yodel.dev \
    /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).