From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id DFEAF3858439 for ; Tue, 12 Sep 2023 16:27:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFEAF3858439 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-34f1bcfe044so19264005ab.0 for ; Tue, 12 Sep 2023 09:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1694536072; x=1695140872; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=J4K9VcZ2kQ3Pb4hKpiCoKW37N45dodjG67HUvUMxMQM=; b=chH7c9o9/rpIAjkIc6jxvArgvBx0hp+xq8cDWNMlcNHztu07R1cNgBWRMjgIONjOfh F/jh5Bt2q+Dsslk9OCS7vjCZtHMZTsGVbsSkMPhEaZeHczJo+X/CSuklLtss84AWbN1+ jj3Z82znJeurHwmbSiMK1LLCqyYD0DS3bLMZrgJSP6emr1DLdGjXJzH1J3SKquBnyiWX fgc5OEmEKR4ycaIjTA1mTLoUT/LIrs2YP//ZqK3q8C1Ijbs978RyikYIJmzr2nBNF1ab PQf7vhDcEYO3898sYgrJP+/9NHw59gzjtLX8fA7XXmENj3T17Ao8MeawoMbX8srruu7G j4Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694536072; x=1695140872; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J4K9VcZ2kQ3Pb4hKpiCoKW37N45dodjG67HUvUMxMQM=; b=vQcMxaPGa5YzdAheG1zHyf+thtTXXqFARhXJE/Ww8f/kjUH0kvgEYXe+yPQe18nu3S S4zCvHHYD9YONPMzi/zIw5MH9M2SOEPsEZn66q6m4NeuocvzLZBxmG3D7h16/Uvu6wuA MUFUDGXJLKK4k+xq5XwDPtVsRYlv1dfAV39J4r8f6CS/x6ZEYGRY/ObOqvdZjDssmKwn kjPwQIcJlOAhJjwDjDruXCq649ZaSu9w3ocd/gvj3J1gZIICrNLQcAB4Izpjz0Nmi0aN rrLJxxW2RytNucQG593fCpIPpZSuQd2EHtzSiQJkqzNSwndwX2P5URysuc+KcPmEVCzD e6vQ== X-Gm-Message-State: AOJu0YwEt8WacWV4WfVbe8ZkjbkBq9lvpgORJPaXzNPTz/MEaAJPMT0l KiVF6HAbCCpV6h48hvbZFow6+ZWAe366OlBhfLHyLg== X-Google-Smtp-Source: AGHT+IEsLDmtg/ktGjVj2blUoA44OusrmkxX/qoUbGgn5ljPmfhCOaMh8V4Pnf+iL9Sib4npiMrUlw== X-Received: by 2002:a05:6e02:1c8a:b0:34f:203c:242b with SMTP id w10-20020a056e021c8a00b0034f203c242bmr60119ill.10.1694536072081; Tue, 12 Sep 2023 09:27:52 -0700 (PDT) Received: from localhost.localdomain (71-211-130-31.hlrn.qwest.net. [71.211.130.31]) by smtp.gmail.com with ESMTPSA id l1-20020a02cd81000000b0042b91ec7e31sm2872324jap.3.2023.09.12.09.27.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 09:27:51 -0700 (PDT) From: Tom Tromey Date: Tue, 12 Sep 2023 10:27:50 -0600 Subject: [PATCH 3/6] Change serial_setbaudrate to throw exception MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230912-serial-exceptions-v1-3-af5097485390@adacore.com> References: <20230912-serial-exceptions-v1-0-af5097485390@adacore.com> In-Reply-To: <20230912-serial-exceptions-v1-0-af5097485390@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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