[binutils-gdb] Allow styling when using throw_* functions

Tom Tromey via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=54c605be19b266001c0fcd19ea4c916d74759927

commit 54c605be19b266001c0fcd19ea4c916d74759927
Author: Tom Tromey <[email protected]>
Date:   Tue Apr 7 18:18:40 2026 -0600

    Allow styling when using throw_* functions
    
    The throw_* family of functions use the varargs constructor for
    gdb_exception, which then calls string_vprintf.  This means that
    styling cannot be applied here by gdb.
    
    This patch changes this code to use a client-supplied formatting
    function.  For gdbserver this remains string_vprintf, but for gdb it
    now allows styling.
    
    I did look at unifying 'error' and the throw_* functions a bit more,
    but this would have meant unravelling some IPA code.

Diff:
---
 gdb/utils.c                    | 10 ++++++++++
 gdbserver/utils.cc             | 12 ++++++++++++
 gdbsupport/common-exceptions.h |  7 ++++++-
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/gdb/utils.c b/gdb/utils.c
index a7e91ce686b..4f99cdb425b 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -182,6 +182,16 @@ vwarning (const char *string, va_list args)
     }
 }
 
+/* See common-exceptions.h.  */
+
+std::string
+vformat_exception (const char *fmt, va_list args)
+{
+  string_file text (true);
+  text.vprintf (fmt, args);
+  return text.release ();
+}
+
 /* Print an error message and return to command level.
    The first argument STRING is the error message, used as a fprintf string,
    and the remaining args are passed as arguments to it.  */
diff --git a/gdbserver/utils.cc b/gdbserver/utils.cc
index 9bc3fc4477e..468e803e34d 100644
--- a/gdbserver/utils.cc
+++ b/gdbserver/utils.cc
@@ -104,3 +104,15 @@ paddress (CORE_ADDR addr)
 {
   return phex_nz (addr);
 }
+
+#ifndef IN_PROCESS_AGENT
+
+/* See common-exceptions.h.  */
+
+std::string
+vformat_exception (const char *fmt, va_list args)
+{
+  return string_vprintf (fmt, args);
+}
+
+#endif /* IN_PROCESS_AGENT */
diff --git a/gdbsupport/common-exceptions.h b/gdbsupport/common-exceptions.h
index 5a50ff44c31..1f3bc84216e 100644
--- a/gdbsupport/common-exceptions.h
+++ b/gdbsupport/common-exceptions.h
@@ -116,6 +116,11 @@ enum errors {
   NR_ERRORS
 };
 
+/* The client application must provide this.  It is a printf-like that
+   formats a string for an exception.  */
+std::string vformat_exception (const char *fmt, va_list args)
+  ATTRIBUTE_PRINTF (1, 0);
+
 struct gdb_exception
 {
   gdb_exception ()
@@ -142,7 +147,7 @@ struct gdb_exception
     ATTRIBUTE_PRINTF (4, 0)
     : reason (r),
       error (e),
-      message (std::make_shared<std::string> (string_vprintf (fmt, ap)))
+      message (std::make_shared<std::string> (vformat_exception (fmt, ap)))
   {
   }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.