]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Allow styling when using throw_* functions
authorTom Tromey <tom@tromey.com>
Wed, 8 Apr 2026 00:18:40 +0000 (18:18 -0600)
committerTom Tromey <tom@tromey.com>
Fri, 8 May 2026 19:02:32 +0000 (13:02 -0600)
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.

gdb/utils.c
gdbserver/utils.cc
gdbsupport/common-exceptions.h

index a7e91ce686b9614c6d3b4ebf06dba1730839c9b7..4f99cdb425b1abaf87a9071cecf2a811345c0fbd 100644 (file)
@@ -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.  */
index 9bc3fc4477ea3fbc86f435ee7cc2949562f4f735..468e803e34d2cb2324252d1d7821b85048fd5d8f 100644 (file)
@@ -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 */
index 5a50ff44c31bf3ebb4037f96657ba274f5e2ecf1..1f3bc84216eab5aa67730ecb0362df006c2accf7 100644 (file)
@@ -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)))
   {
   }