From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] testsuite/lib/set_unbuffered_mode.c: Use constructor priority
Date: Tue, 11 Aug 2026 00:08:26 +0100 [thread overview]
Message-ID: <20260810230826.3466696-3-pedro@palves.net> (raw)
In-Reply-To: <20260810230826.3466696-1-pedro@palves.net>
On native Windows, we link in testsuite/lib/set_unbuffered_mode.c to
all executables. That file has a constructor function that disables
output buffering. We want that constructor to run before the
constructors of global C++ objects, in case those objects print to
output.
Currently, that is done by relying on the link order of object files
on the link line affecting the global ctor order. On Windows, with
gcc and clang targeting the GNU ABI, constructors are run in reverse
link order. I.e., the constructors from the last objfile on the link
line run first.
That is not true if we compile with clang targeting the MSVC ABI,
though. There, global ctors run on link line order. Like on Linux.
This can instead be handled by setting a constructor priority.
Constructors with a priority run before the global C++ objects'
constructors. The GCC feature to allow specifying a constructor with
a priority only appeared in GCC 4.3, released in 2008, and I added
this set_unbuffered_mode feature to GDB also in 2008, so back then it
was too early to rely on the priority feature.
It's 2025 now, and I think we can safely assume nobody is testing on
Windows with such an old compiler.
So this patch gives an explicit priority to the
testsuite/lib/set_unbuffered_mode.c constructor, and tweaks the
comments in testsuite/lib/gdb.exp to no longer talk about controlling
ctor order with link order.
Tested with both GCC and Clang.
Change-Id: Ia5f8972f2ef45f070e63cb0a1b0aa9918957ea8e
---
gdb/testsuite/lib/gdb.exp | 9 ++++-----
gdb/testsuite/lib/set_unbuffered_mode.c | 7 +++++--
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 6fb04869605..fecb9af4883 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -7046,13 +7046,12 @@ proc gdb_compile {source dest type options} {
verbose "gdb_saved_set_unbuffered_obj already compiled"
}
- # Rely on the internal knowledge that the global ctors are ran in
- # reverse link order. In that case, we can use ldflags to
- # avoid copying the object file to the host multiple
- # times.
# This object can only be added if standard libraries are
- # used. Thus, we need to disable it if -nostdlib option is used
+ # used. Thus, we need to disable it if -nostdlib option
+ # is used.
if {[lsearch -regexp $options "-nostdlib"] < 0 } {
+ # Use ldflags to avoid copying the object file to the
+ # host multiple times.
lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj"
}
}
diff --git a/gdb/testsuite/lib/set_unbuffered_mode.c b/gdb/testsuite/lib/set_unbuffered_mode.c
index 6d69ccabaeb..3b5badcf780 100644
--- a/gdb/testsuite/lib/set_unbuffered_mode.c
+++ b/gdb/testsuite/lib/set_unbuffered_mode.c
@@ -19,8 +19,11 @@
#include <stdio.h>
-static void __gdb_set_unbuffered_output (void) __attribute__ ((constructor));
-static void
+/* Use an explicit priority so that this runs before constructors of
+ namespace-scope C++ objects (which may output to stdout/stderr).
+ Lower priorities run first. Constructor priorities from 0 to 100
+ are reserved for the implementation. */
+static void __attribute__ ((constructor (101)))
__gdb_set_unbuffered_output (void)
{
setvbuf (stdout, NULL, _IONBF, BUFSIZ);
--
2.54.0
next prev parent reply other threads:[~2026-08-10 23:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-10 23:08 [PATCH 0/2] gdb/testsuite: Prepare set_unbuffered_mode.c for windows-msvc Pedro Alves
2026-08-10 23:08 ` [PATCH 1/2] testsuite/lib/set_unbuffered_mode.c: Fix return type Pedro Alves
2026-08-10 23:08 ` Pedro Alves [this message]
2026-08-11 9:01 ` [PATCH 0/2] gdb/testsuite: Prepare set_unbuffered_mode.c for windows-msvc Andrew Burgess
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=20260810230826.3466696-3-pedro@palves.net \
--to=pedro@palves.net \
--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