public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: binutils@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 3/3] Introduce bfd_print_error function
Date: Mon, 29 Jan 2024 18:03:33 -0700	[thread overview]
Message-ID: <20240130010540.1754740-4-tom@tromey.com> (raw)
In-Reply-To: <20240130010540.1754740-1-tom@tromey.com>

gdb likes to control its own output; for example, this is important
for gdb's pager, and for logging.  While BFD provides a way to
intercept error output, via bfd_set_error_handler, it turns out to be
difficult for this function to truly generate the desired output in a
gdb-friendly way -- the error handler is expected to implement some
BFD printf format extensions.

This patch introduces a new function that an error handler can use to
format the text.  This way, gdb can set the error handler and arrange
for the output to be displayed as it likes.

bfd/ChangeLog
2024-01-29  Tom Tromey  <tom@tromey.com>

	* bfd.c (bfd_print_callback): Rename from print_func.  Move into
	comment.
	(_bfd_doprnt): Update.
	(bfd_print_error): New function.
	(error_handler_fprintf, error_handler_sprintf): Use
	bfd_print_error.
	* bfd-in2.h: Rebuild.
---
 bfd/ChangeLog | 10 ++++++++++
 bfd/bfd-in2.h |  4 ++++
 bfd/bfd.c     | 48 ++++++++++++++++++++++++++++++++++++------------
 3 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 581d8fe0b3e..e8b0e9f8699 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -2557,6 +2557,10 @@ void bfd_perror (const char *message);
 
 typedef void (*bfd_error_handler_type) (const char *, va_list);
 
+typedef int (*bfd_print_callback) (void *, const char *, ...);
+void bfd_print_error (bfd_print_callback print_func,
+    void *stream, const char *fmt, va_list ap);
+
 void _bfd_error_handler (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
 
 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
diff --git a/bfd/bfd.c b/bfd/bfd.c
index 0f1eaa1629f..886a97ce078 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -1000,10 +1000,13 @@ union _bfd_doprnt_args
       result = print (stream, specifier, value);		\
     } while (0)
 
-typedef int (*print_func) (void *, const char *, ...);
+/*
+CODE_FRAGMENT
+.typedef int (*bfd_print_callback) (void *, const char *, ...);
+*/
 
 static int
-_bfd_doprnt (print_func print, void *stream, const char *format,
+_bfd_doprnt (bfd_print_callback print, void *stream, const char *format,
 	     union _bfd_doprnt_args *args)
 {
   const char *ptr = format;
@@ -1446,21 +1449,44 @@ _bfd_doprnt_scan (const char *format, va_list ap, union _bfd_doprnt_args *args)
   return arg_count;
 }
 
-/* The standard error handler that prints to stderr.  */
+/*
+FUNCTION
+	bfd_print_error
 
-static void
-error_handler_fprintf (const char *fmt, va_list ap)
+SYNOPSIS
+	void bfd_print_error (bfd_print_callback print_func,
+	  void *stream, const char *fmt, va_list ap);
+
+DESCRIPTION
+
+	This formats FMT and AP according to BFD "printf" rules,
+	sending the output to STREAM by repeated calls to PRINT_FUNC.
+	PRINT_FUNC is a printf-like function; it does not need to
+	implement the BFD printf format extensions.  This can be used
+	in a callback that is set via bfd_set_error_handler to turn
+	the error into ordinary output.
+*/
+
+void
+bfd_print_error (bfd_print_callback print_func, void *stream,
+		 const char *fmt, va_list ap)
 {
   union _bfd_doprnt_args args[MAX_ARGS];
 
+  print_func (stream, "%s: ", _bfd_get_error_program_name ());
   _bfd_doprnt_scan (fmt, ap, args);
+  _bfd_doprnt (print_func, stream, fmt, args);
+}
 
+/* The standard error handler that prints to stderr.  */
+
+static void
+error_handler_fprintf (const char *fmt, va_list ap)
+{
   /* PR 4992: Don't interrupt output being sent to stdout.  */
   fflush (stdout);
 
-  fprintf (stderr, "%s: ", _bfd_get_error_program_name ());
-
-  _bfd_doprnt ((print_func) fprintf, stderr, fmt, args);
+  bfd_print_error ((bfd_print_callback) fprintf, stderr, fmt, ap);
 
   /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
      warning, so use the fputc function to avoid it.  */
@@ -1512,15 +1538,13 @@ static TLS bfd *error_handler_bfd;
 static void
 error_handler_sprintf (const char *fmt, va_list ap)
 {
-  union _bfd_doprnt_args args[MAX_ARGS];
   char error_buf[1024];
   struct buf_stream error_stream;
 
-  _bfd_doprnt_scan (fmt, ap, args);
-
   error_stream.ptr = error_buf;
   error_stream.left = sizeof (error_buf);
-  _bfd_doprnt (err_sprintf, &error_stream, fmt, args);
+
+  bfd_print_error (err_sprintf, &error_stream, fmt, ap);
 
   size_t len = error_stream.ptr - error_buf;
   struct per_xvec_message **warn
-- 
2.43.0


  parent reply	other threads:[~2024-01-30  1:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-30  1:03 [PATCH 0/3] Fix some error-printing issues Tom Tromey
2024-01-30  1:03 ` [PATCH 1/3] Make several more BFD globals thread-local Tom Tromey
2024-02-12 15:28   ` Nick Clifton
2024-02-12 23:52     ` Tom Tromey
2024-02-13  0:35     ` Tom Tromey
2024-02-13  1:20       ` Tom Tromey
2024-03-09  1:12         ` Tom Tromey
2024-03-09  4:11           ` Alan Modra
2024-03-09 10:31             ` Alan Modra
2024-03-15  0:12               ` Tom Tromey
2024-03-19 20:17                 ` Tom Tromey
2024-03-15  1:15             ` Tom Tromey
2024-01-30  1:03 ` [PATCH 2/3] Do not call fputc from _bfd_doprnt Tom Tromey
2024-02-12 15:04   ` Nick Clifton
2024-01-30  1:03 ` Tom Tromey [this message]
2024-02-12 15:04   ` [PATCH 3/3] Introduce bfd_print_error function Nick Clifton

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=20240130010540.1754740-4-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=binutils@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).