From ad3cf8c64e6e4794fc48d28c90f20cbbfdc51ca4 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 29 Aug 2023 07:20:22 -0600 Subject: Change serial_setbaudrate to throw exception 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(-) (limited to 'gdb') diff --git a/gdb/remote.c b/gdb/remote.c index 1bc5992..00c84f1 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -6082,7 +6082,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 @@ -6090,7 +6094,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 0883305..072211d 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 27f9f34..aeb7a4d 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 806f399..65ee169 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 cdc0cf9..5bd1598 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 8a8bab4..122ab0b 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 3b86120..9a51fdf 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. */ -- cgit v1.1