From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 3/6] Change serial_setbaudrate to throw exception
Date: Tue, 12 Sep 2023 10:27:50 -0600 [thread overview]
Message-ID: <20230912-serial-exceptions-v1-3-af5097485390@adacore.com> (raw)
In-Reply-To: <20230912-serial-exceptions-v1-0-af5097485390@adacore.com>
remote.c has this code:
if (serial_setbaudrate (rs->remote_desc, baud_rate))
{
/* The requested speed could not be set. Error out to
top level after closing remote_desc. Take care to
set remote_desc to NULL to avoid closing remote_desc
more than once. */
serial_close (rs->remote_desc);
rs->remote_desc = NULL;
perror_with_name (name);
The perror here cannot be correct, because if serial_setbaudrate did
set errno, it may be obscured by serial_close.
This patch changes serial_setbaudrate to throw an exception instead.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30770
---
gdb/remote.c | 8 ++++++--
gdb/ser-base.c | 4 ++--
gdb/ser-base.h | 2 +-
gdb/ser-mingw.c | 7 ++++---
gdb/ser-unix.c | 33 ++++++++++++---------------------
gdb/serial.c | 4 ++--
gdb/serial.h | 8 ++++----
7 files changed, 31 insertions(+), 35 deletions(-)
diff --git a/gdb/remote.c b/gdb/remote.c
index ba81c5b0b6f..9346ae8b3a1 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5959,7 +5959,11 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
if (baud_rate != -1)
{
- if (serial_setbaudrate (rs->remote_desc, baud_rate))
+ try
+ {
+ serial_setbaudrate (rs->remote_desc, baud_rate);
+ }
+ catch (const gdb_exception_error &)
{
/* The requested speed could not be set. Error out to
top level after closing remote_desc. Take care to
@@ -5967,7 +5971,7 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
more than once. */
serial_close (rs->remote_desc);
rs->remote_desc = NULL;
- perror_with_name (name);
+ throw;
}
}
diff --git a/gdb/ser-base.c b/gdb/ser-base.c
index 0883305ac2b..072211df1ca 100644
--- a/gdb/ser-base.c
+++ b/gdb/ser-base.c
@@ -561,10 +561,10 @@ ser_base_print_tty_state (struct serial *scb,
return;
}
-int
+void
ser_base_setbaudrate (struct serial *scb, int rate)
{
- return 0; /* Never fails! */
+ /* Never fails! */
}
int
diff --git a/gdb/ser-base.h b/gdb/ser-base.h
index 4c6537f7adf..60f84e1f42e 100644
--- a/gdb/ser-base.h
+++ b/gdb/ser-base.h
@@ -40,7 +40,7 @@ extern int ser_base_set_tty_state (struct serial *scb,
extern void ser_base_print_tty_state (struct serial *scb,
serial_ttystate ttystate,
struct ui_file *stream);
-extern int ser_base_setbaudrate (struct serial *scb, int rate);
+extern void ser_base_setbaudrate (struct serial *scb, int rate);
extern int ser_base_setstopbits (struct serial *scb, int num);
extern int ser_base_setparity (struct serial *scb, int parity);
extern int ser_base_drain_output (struct serial *scb);
diff --git a/gdb/ser-mingw.c b/gdb/ser-mingw.c
index 806f3999385..65ee1697331 100644
--- a/gdb/ser-mingw.c
+++ b/gdb/ser-mingw.c
@@ -226,18 +226,19 @@ ser_windows_setparity (struct serial *scb, int parity)
return (SetCommState (h, &state) != 0) ? 0 : -1;
}
-static int
+static void
ser_windows_setbaudrate (struct serial *scb, int rate)
{
HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
DCB state;
if (GetCommState (h, &state) == 0)
- return -1;
+ throw_winerror_with_name ("call to GetCommState failed", GetLastError ());
state.BaudRate = rate;
- return (SetCommState (h, &state) != 0) ? 0 : -1;
+ if (SetCommState (h, &state) == 0)
+ throw_winerror_with_name ("call to SetCommState failed", GetLastError ());
}
static void
diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c
index cdc0cf98b7b..5bd15985d8c 100644
--- a/gdb/ser-unix.c
+++ b/gdb/ser-unix.c
@@ -53,7 +53,7 @@ show_serial_hwflow (struct ui_file *file, int from_tty,
static int hardwire_open (struct serial *scb, const char *name);
static void hardwire_raw (struct serial *scb);
static int rate_to_code (int rate);
-static int hardwire_setbaudrate (struct serial *scb, int rate);
+static void hardwire_setbaudrate (struct serial *scb, int rate);
static int hardwire_setparity (struct serial *scb, int parity);
static void hardwire_close (struct serial *scb);
static int get_tty_state (struct serial *scb,
@@ -417,47 +417,38 @@ rate_to_code (int rate)
{
if (i)
{
- warning (_("Invalid baud rate %d. "
- "Closest values are %d and %d."),
- rate, baudtab[i - 1].rate, baudtab[i].rate);
+ error (_("Invalid baud rate %d. "
+ "Closest values are %d and %d."),
+ rate, baudtab[i - 1].rate, baudtab[i].rate);
}
else
{
- warning (_("Invalid baud rate %d. Minimum value is %d."),
- rate, baudtab[0].rate);
+ error (_("Invalid baud rate %d. Minimum value is %d."),
+ rate, baudtab[0].rate);
}
- return -1;
}
}
}
/* The requested speed was too large. */
- warning (_("Invalid baud rate %d. Maximum value is %d."),
- rate, baudtab[i - 1].rate);
- return -1;
+ error (_("Invalid baud rate %d. Maximum value is %d."),
+ rate, baudtab[i - 1].rate);
}
-static int
+static void
hardwire_setbaudrate (struct serial *scb, int rate)
{
struct hardwire_ttystate state;
int baud_code = rate_to_code (rate);
- if (baud_code < 0)
- {
- /* The baud rate was not valid.
- A warning has already been issued. */
- errno = EINVAL;
- return -1;
- }
-
if (get_tty_state (scb, &state))
- return -1;
+ perror_with_name ("could not get tty state");
cfsetospeed (&state.termios, baud_code);
cfsetispeed (&state.termios, baud_code);
- return set_tty_state (scb, &state);
+ if (set_tty_state (scb, &state))
+ perror_with_name ("could not set tty state");
}
static int
diff --git a/gdb/serial.c b/gdb/serial.c
index 8a8bab46ead..122ab0bd10e 100644
--- a/gdb/serial.c
+++ b/gdb/serial.c
@@ -512,10 +512,10 @@ serial_print_tty_state (struct serial *scb,
scb->ops->print_tty_state (scb, ttystate, stream);
}
-int
+void
serial_setbaudrate (struct serial *scb, int rate)
{
- return scb->ops->setbaudrate (scb, rate);
+ scb->ops->setbaudrate (scb, rate);
}
int
diff --git a/gdb/serial.h b/gdb/serial.h
index 3b861200302..9a51fdf7816 100644
--- a/gdb/serial.h
+++ b/gdb/serial.h
@@ -177,10 +177,10 @@ extern void serial_print_tty_state (struct serial *scb,
serial_ttystate ttystate,
struct ui_file *);
-/* Set the baudrate to the decimal value supplied. Returns 0 for
- success, -1 for failure. */
+/* Set the baudrate to the decimal value supplied. Throws exception
+ on error. */
-extern int serial_setbaudrate (struct serial *scb, int rate);
+extern void serial_setbaudrate (struct serial *scb, int rate);
/* Set the number of stop bits to the value specified. Returns 0 for
success, -1 for failure. */
@@ -275,7 +275,7 @@ struct serial_ops
int (*set_tty_state) (struct serial *, serial_ttystate);
void (*print_tty_state) (struct serial *, serial_ttystate,
struct ui_file *);
- int (*setbaudrate) (struct serial *, int rate);
+ void (*setbaudrate) (struct serial *, int rate);
int (*setstopbits) (struct serial *, int num);
/* Set the value PARITY as parity setting for serial object.
Return 0 in the case of success. */
--
2.40.1
next prev parent reply other threads:[~2023-09-12 16:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-12 16:27 [PATCH 0/6] Fix error messages from serial code Tom Tromey
2023-09-12 16:27 ` [PATCH 1/6] Fix latent bug in ser_windows_send_break Tom Tromey
2023-09-12 16:27 ` [PATCH 2/6] Introduce throw_winerror_with_name Tom Tromey
2023-09-12 16:27 ` Tom Tromey [this message]
2023-09-12 16:27 ` [PATCH 4/6] Change serial "open" functions to throw exception Tom Tromey
2023-09-12 16:27 ` [PATCH 5/6] Change serial_send_break and serial_write to throw Tom Tromey
2023-09-12 16:27 ` [PATCH 6/6] Change serial_readchar " Tom Tromey
2023-11-27 20:13 ` [PATCH 0/6] Fix error messages from serial code Tom Tromey
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=20230912-serial-exceptions-v1-3-af5097485390@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).