From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta040.useast.a.cloudfilter.net (omta040.useast.a.cloudfilter.net [44.202.169.39]) by sourceware.org (Postfix) with ESMTPS id 11E05385828D for ; Tue, 30 Jan 2024 01:06:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 11E05385828D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 11E05385828D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706576767; cv=none; b=AmhOkV9kv1wggUrB3s8P9fGr7Oc9zhGEnMETvFn/QkXYfSMAoG3JA5xeF6l2uabE9PIascgm/MlN6qH5r5UgOnlHu5Gy1B2EvPzAjd91IJGSj9EVhi0wTCE9OTWX6BxC04KO5QAoqH4v0+tK0P9Kjn3XD1R+K79jBdX1jn53X9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706576767; c=relaxed/simple; bh=VV/B0IDmYnVhNBI5bRVt4D+4Muy7v+xRShIfrjLEUew=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=OeSPi4l+/ySczmjY4Dp3QbJjRreSQYKr7gLVE+xj6gIV00DPe1ieBPFG4Pwbiw/fSvgRHb+HIV36GDFSa32VmNyG+EEPCNL0BfQLWGJ3SemslPZslphO/mKfF5HpyStqVcuKjs4Gub8egHWi97w0f/L3AhIE0DvaZwCRdpcYK0I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-5004a.ext.cloudfilter.net ([10.0.29.221]) by cmsmtp with ESMTPS id UaoSrQvg8THHuUcZgrlRDr; Tue, 30 Jan 2024 01:06:04 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id UcZfrZyihKleDUcZgre8Vz; Tue, 30 Jan 2024 01:06:04 +0000 X-Authority-Analysis: v=2.4 cv=KcbBDCUD c=1 sm=1 tr=0 ts=65b84b7c a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dEuoMetlWLkA:10 a=Qbun_eYptAEA:10 a=zstS-IiYAAAA:8 a=OXhtBIGw6S4pdDCVDrwA:9 a=4G6NA9xxw8l3yy4pmD5M:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Dxg+HTBHDbIWnOdODk9TjpmAWVxXIcIIsm8bC2WpLMk=; b=wDob1uFtYG8vsvufD/e16K3Zrp 9HcYbl1i0zTkwQ4h+vmjwGM8vYxdFOgxQjk3M8Zw/ISduWsKC9cssyiGI+oG3VBSUii9NRbK1sZfJ 7R3g7eZAzp+/05CTLT2hKL5ul; Received: from [161.98.8.3] (port=34570 helo=prentzel.ci.boulder.co.us) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rUcZX-003BfD-2E; Mon, 29 Jan 2024 18:06:03 -0700 From: Tom Tromey To: binutils@sourceware.org Cc: Tom Tromey Subject: [PATCH 3/3] Introduce bfd_print_error function Date: Mon, 29 Jan 2024 18:03:33 -0700 Message-ID: <20240130010540.1754740-4-tom@tromey.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240130010540.1754740-1-tom@tromey.com> References: <20240130010540.1754740-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 161.98.8.3 X-Source-L: No X-Exim-ID: 1rUcZX-003BfD-2E X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (prentzel.ci.boulder.co.us) [161.98.8.3]:34570 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfGMs/98PeYvmpUDFY5Bp3T0i2zR2AZ9O8u8k5t1HfAF0+rGgA51e4yRQYK+5Ba5tER4vCqYghSNh8oIx3Dh+5+XMhUqdFTHkE8MopLPWAmuV8SRURhh6 EKaEhqFQ5XrA0HiJpfywNirIBOJZAzOTUgQkYcDEYFzMUKo1QPAfmLtNe6caobOde4gPzlQ6Noya7vX2IXrtX4ne3FbFtFbhrgU= X-Spam-Status: No, score=-3022.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 * 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