From: sunilkumar.dora@windriver.com
To: gdb-patches@sourceware.org
Cc: simon.marchi@efficios.com, tromey@sourceware.org,
Sundeep.Kokkonda@windriver.com, SunilKumar.Dora@windriver.com
Subject: [PATCH] gdb/ser-unix: avoid musl build failure when setting custom baud rates
Date: Fri, 13 Feb 2026 07:21:51 -0800 [thread overview]
Message-ID: <20260213152151.3224544-1-sunilkumar.dora@windriver.com> (raw)
From: Sunil Dora <sunilkumar.dora@windriver.com>
GDB's Linux custom baud rate implementation accessed the non-standard
struct termios members c_ispeed and c_ospeed directly. These members
are provided by glibc but are not exposed by musl, causing the build
to fail with errors such as:
error: no member named 'c_ospeed' in 'termios'
error: no member named 'c_ispeed' in 'termios'
Musl follows strict POSIX semantics and does not expose these
implementation-specific fields. In addition, cfsetospeed/cfsetispeed
may reject non-standard baud rates, and the Linux-specific termios2
interface is not always available through musl libc headers.
Refactor set_custom_baudrate_linux to use a layered approach:
1. Attempt to use cfsetospeed/cfsetispeed
2. Attempt to use the Linux termios2 interface (TCGETS2/TCSETS2)
when available.
3. Fall back to direct struct termios field access only when the
required fields are exposed by the libc.
If none of these mechanisms succeed, report an error.
This preserves existing behavior on glibc systems while avoiding
build failures on musl-based systems.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33747
* gdb/ser-unix.c (set_custom_baudrate_linux): Refactor custom
baud rate handling to avoid unconditional use of non-standard
termios members.
Signed-off-by: Sunil Dora <sunilkumar.dora@windriver.com>
---
gdb/ser-unix.c | 68 ++++++++++++++++++++++++++++++++++++--------------
1 file changed, 49 insertions(+), 19 deletions(-)
diff --git a/gdb/ser-unix.c b/gdb/ser-unix.c
index c295a9c5ba1..571a59ab280 100644
--- a/gdb/ser-unix.c
+++ b/gdb/ser-unix.c
@@ -515,31 +515,61 @@ set_baudcode_baudrate (struct serial *scb, int baud_code)
static void
set_custom_baudrate_linux (int fd, int rate)
{
-#ifdef TCGETS2
- struct termios2 tio;
- const unsigned long req_get = TCGETS2;
- const unsigned long req_set = TCSETS2;
-#else
+ /* Standard POSIX API */
+#if defined (_HAVE_STRUCT_TERMIOS_C_OSPEED) \
+ || defined (_HAVE_STRUCT_TERMIOS_C_ISPEED)
struct termios tio;
- const unsigned long req_get = TCGETS;
- const unsigned long req_set = TCSETS;
+ if (ioctl (fd, TCGETS, &tio) == 0)
+ {
+ if (cfsetospeed (&tio, rate) == 0 && cfsetispeed (&tio, rate) == 0)
+ {
+ if (ioctl (fd, TCSETS, &tio) == 0)
+ return;
+ }
+ }
#endif
- if (ioctl (fd, req_get, &tio) < 0)
- perror_with_name (_("Can not get current baud rate"));
+ /* Linux Kernel 'termios2' API (Linux/Musl).
+ This allows arbitrary baud rates (e.g., 25000). */
+#ifdef TCGETS2
+ struct termios2 tio2;
+
+ if (ioctl (fd, TCGETS2, &tio2) == 0)
+ {
+ tio2.c_cflag &= ~CBAUD;
+ tio2.c_cflag |= BOTHER;
+ tio2.c_ospeed = rate;
- /* Clear the current output baud rate and fill a new value. */
- tio.c_cflag &= ~CBAUD;
- tio.c_cflag |= BOTHER;
- tio.c_ospeed = rate;
+ tio2.c_cflag &= ~(CBAUD << IBSHIFT);
+ tio2.c_cflag |= BOTHER << IBSHIFT;
+ tio2.c_ispeed = rate;
- /* Clear the current input baud rate and fill a new value. */
- tio.c_cflag &= ~(CBAUD << IBSHIFT);
- tio.c_cflag |= BOTHER << IBSHIFT;
- tio.c_ispeed = rate;
+ if (ioctl (fd, TCSETS2, &tio2) == 0)
+ return;
+ }
+#endif
- if (ioctl (fd, req_set, &tio) < 0)
- perror_with_name (_("Can not set custom baud rate"));
+ /* Legacy Fallback.
+ We only try this if the libc actually supports the fields we need.
+ On Musl, this block is correctly skipped. */
+#if defined (_HAVE_STRUCT_TERMIOS_C_OSPEED)
+ struct termios tio3;
+ if (ioctl (fd, TCGETS, &tio3) == 0)
+ {
+ tio3.c_cflag &= ~CBAUD;
+ tio3.c_cflag |= BOTHER;
+ tio3.c_ospeed = rate;
+#ifdef _HAVE_STRUCT_TERMIOS_C_ISPEED
+ tio3.c_cflag &= ~(CBAUD << IBSHIFT);
+ tio3.c_cflag |= BOTHER << IBSHIFT;
+ tio3.c_ispeed = rate;
+#endif
+ if (ioctl (fd, TCSETS, &tio3) == 0)
+ return;
+ }
+#endif
+
+ perror_with_name (_ ("Can not set custom baud rate"));
}
#elif HAVE_CUSTOM_BAUDRATE_SUPPORT && defined(IOSSIOSPEED)
--
2.49.0
next reply other threads:[~2026-02-13 15:23 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-13 15:21 sunilkumar.dora [this message]
2026-02-13 18:08 ` Kevin Buettner
2026-02-13 18:58 ` Maciej W. Rozycki
2026-02-13 21:21 ` Dora, Sunil Kumar
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=20260213152151.3224544-1-sunilkumar.dora@windriver.com \
--to=sunilkumar.dora@windriver.com \
--cc=Sundeep.Kokkonda@windriver.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@efficios.com \
--cc=tromey@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