]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Change serial_setbaudrate to throw exception
authorTom Tromey <tromey@adacore.com>
Tue, 29 Aug 2023 13:20:22 +0000 (07:20 -0600)
committerTom Tromey <tromey@adacore.com>
Mon, 27 Nov 2023 19:55:14 +0000 (12:55 -0700)
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
gdb/ser-base.c
gdb/ser-base.h
gdb/ser-mingw.c
gdb/ser-unix.c
gdb/serial.c
gdb/serial.h

index 1bc59928dcf847e811615987a23feed097823dbc..00c84f167d09f2742ea2e4381b5df1e0a13f8b8d 100644 (file)
@@ -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;
        }
     }
 
index 0883305ac2b4eee71dae3c8c21692d9ca4284fcd..072211df1ca69c0a06e6f6087c62abc5a4a9d010 100644 (file)
@@ -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
index 27f9f3439775f8e644092dd414f78db54731c937..aeb7a4da333de4846c4d94e734209e3e82f57a62 100644 (file)
@@ -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);
index 806f3999385bff4089eb86b0193303db3227ec08..65ee1697331bc8a1f1f448e64e91219ce6c72dd9 100644 (file)
@@ -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
index cdc0cf98b7b12b4d13f72e9cd0c79a0c1e61a047..5bd15985d8c056b809c6564fecd565471a8d3bdb 100644 (file)
@@ -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
index 8a8bab46eadf411843aa0c4a24e7484bc91ea2ca..122ab0bd10e6fc8022c10c8999bc91a975c7f39f 100644 (file)
@@ -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
index 3b8612003021da202634f12617d886c379b1fe6e..9a51fdf78166aa4921680353dc69d5401ffed608 100644 (file)
@@ -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.  */