[PATCH 1/2] [gdbsupport] Rewrite handle_eintr
Tom de Vries
tdevries@suse.de
Fri Oct 25 16:52:44 GMT 2024
We have gdb::handle_eintr, which allows us to rewrite:
...
ssize_t ret;
do
{
errno = 0;
ret = ::write (pipe[1], "+", 1);
}
while (ret == -1 && errno == EINTR);
...
into:
...
ssize_t ret = gdb::handle_eintr (-1, ::write, pipe[1], "+", 1);
...
However, the call to write got a bit mangled, requiring effort to decode it
back to its original form.
Instead, rewrite gdb::handle_eintr to take a lambda function as parameter,
getting us instead:
...
ssize_t ret = gdb::handle_eintr<-1> ([&]
{
return ::write (pipe[1], "+", 1);
});
...
where the original call is present.
Tested on aarch64-linux.
---
gdb/cli/cli-cmds.c | 5 ++++-
gdb/nat/linux-waitpid.c | 5 ++++-
gdbserver/netbsd-low.cc | 25 ++++++++++++++++++++-----
gdbsupport/eintr.h | 26 ++++++++++++++------------
4 files changed, 42 insertions(+), 19 deletions(-)
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 65ac7d6e7fb..3cfc85adf14 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -923,7 +923,10 @@ run_under_shell (const char *arg, int from_tty)
if (pid != -1)
{
- int ret = gdb::handle_eintr (-1, ::waitpid, pid, &status, 0);
+ int ret = gdb::handle_eintr<-1> ([&]
+ {
+ return ::waitpid (pid, &status, 0);
+ });
if (ret == -1)
perror_with_name ("Cannot get status of shell command");
}
diff --git a/gdb/nat/linux-waitpid.c b/gdb/nat/linux-waitpid.c
index 0ac2f9fb2b9..6271397b036 100644
--- a/gdb/nat/linux-waitpid.c
+++ b/gdb/nat/linux-waitpid.c
@@ -51,5 +51,8 @@ status_to_str (int status)
int
my_waitpid (int pid, int *status, int flags)
{
- return gdb::handle_eintr (-1, ::waitpid, pid, status, flags);
+ return gdb::handle_eintr<-1> ([&]
+ {
+ return ::waitpid (pid, status, flags);
+ });
}
diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc
index 4b58826e091..9c6aa9c56ab 100644
--- a/gdbserver/netbsd-low.cc
+++ b/gdbserver/netbsd-low.cc
@@ -222,7 +222,10 @@ netbsd_waitpid (ptid_t ptid, struct target_waitstatus *ourstatus,
int options = (target_options & TARGET_WNOHANG) ? WNOHANG : 0;
pid_t pid
- = gdb::handle_eintr (-1, ::waitpid, ptid.pid (), &status, options);
+ = gdb::handle_eintr<-1> ([&]
+ {
+ return ::waitpid (ptid.pid (), &status, options);
+ });
if (pid == -1)
perror_with_name (_("Child process unexpectedly missing"));
@@ -432,7 +435,10 @@ netbsd_process_target::kill (process_info *process)
return -1;
int status;
- if (gdb::handle_eintr (-1, ::waitpid, pid, &status, 0) == -1)
+ if (gdb::handle_eintr<-1> ([&]
+ {
+ return ::waitpid (pid, &status, 0);
+ }) == -1)
return -1;
mourn (process);
return 0;
@@ -1123,15 +1129,24 @@ netbsd_qxfer_libraries_svr4 (const pid_t pid, const char *annex,
static bool
elf_64_file_p (const char *file)
{
- int fd = gdb::handle_eintr (-1, ::open, file, O_RDONLY);
+ int fd = gdb::handle_eintr<-1> ([&]
+ {
+ return ::open (file, O_RDONLY);
+ });
if (fd < 0)
perror_with_name (("open"));
Elf64_Ehdr header;
- ssize_t ret = gdb::handle_eintr (-1, ::read, fd, &header, sizeof (header));
+ ssize_t ret = gdb::handle_eintr<-1> ([&]
+ {
+ return ::read (fd, &header, sizeof (header));
+ });
if (ret == -1)
perror_with_name (("read"));
- gdb::handle_eintr (-1, ::close, fd);
+ gdb::handle_eintr<-1> ([&]
+ {
+ return ::close (fd);
+ });
if (ret != sizeof (header))
error ("Cannot read ELF file header: %s", file);
diff --git a/gdbsupport/eintr.h b/gdbsupport/eintr.h
index e440f935fcf..e08afe1fd9a 100644
--- a/gdbsupport/eintr.h
+++ b/gdbsupport/eintr.h
@@ -43,27 +43,29 @@ namespace gdb
You could wrap it by writing the wrapped form:
- ssize_t ret = gdb::handle_eintr (-1, ::write, pipe[1], "+", 1);
+ ssize_t ret = gdb::handle_eintr<-1> ([&]
+ {
+ return ::write (pipe[1], "+", 1);
+ });
ERRVAL specifies the failure value indicating that the call to the
F function with ARGS... arguments was possibly interrupted with a
signal. */
-template<typename ErrorValType, typename Fun, typename... Args>
-inline auto
-handle_eintr (ErrorValType errval, const Fun &f, const Args &... args)
- -> decltype (f (args...))
+template <auto val, typename F>
+static auto
+handle_eintr (F &&f)
{
- decltype (f (args...)) ret;
-
- do
+ while (true)
{
errno = 0;
- ret = f (args...);
- }
- while (ret == errval && errno == EINTR);
- return ret;
+ auto ret = f ();
+ if (ret == val && errno == EINTR)
+ continue;
+
+ return ret;
+ }
}
} /* namespace gdb */
base-commit: 3d17c8817216343179f64d1a682311a70774ccb4
--
2.35.3
More information about the Gdb-patches
mailing list