From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 399A53857B8A for ; Wed, 10 Aug 2022 14:24:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 399A53857B8A Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-19-r2bAI1vRP9G3FvfcSBZDXw-1; Wed, 10 Aug 2022 10:24:46 -0400 X-MC-Unique: r2bAI1vRP9G3FvfcSBZDXw-1 Received: by mail-wm1-f70.google.com with SMTP id f21-20020a1cc915000000b003a3551598a1so671866wmb.0 for ; Wed, 10 Aug 2022 07:24:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=cqTVvEw/4g4zuVRjabwvc6HnmAi6feavjiuUBUItFtE=; b=hSQZznlSX8voONMfS5vRoOxspGHW0YqDFWuaUg2LpgUxxeIWFHQo+aJLZek6X0utYH 6+qdDUc/u5ki3uic22qegUJP8CzB/tFLID0fvMOvj9gjqfW6Vi0ap111FM+LDs7Rmn3A 0if0W+4URqpTCkNt5hKoiuaL2DnjnwL+DhYHyVS03egwuGTTfI0s/TWhi+sQw0HOedCl kd1q1fV9o0iNQS+gb0PraTOeRiti2KTpRc7CSmDSg8etmXi4HwmEoTEuDb2iiInnqd7v /3BR2s1DkEVJmIcSfiyNrrM1cb21hBKGm7byxmNx0knq897d51OyfUv4JflQWm5LZjv8 PJrQ== X-Gm-Message-State: ACgBeo3YWwYU2O3ZePKdDPyY5cYBtr40mcJ8702ysEHp5VtEz3wrE1F6 4NGUVx4OwD9Gb3/K9eYMM6XvqdlvyLCqDPbeJ2M8jeACd6dnOAZqIjX/KFCD4yKzCs/5S2WGvCY hsEXCQIVh3QXqJgK9jxYD8k2D39cYXaUuqTJ6QqnerI9lkIb4Tw7jYaQ99tcP/gd42gJM9g== X-Received: by 2002:a5d:5a8d:0:b0:21d:6d69:ca03 with SMTP id bp13-20020a5d5a8d000000b0021d6d69ca03mr17259457wrb.135.1660141485350; Wed, 10 Aug 2022 07:24:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR5hvNIHyM+Gd4eqzqT+k77j+eCoL1G/KBjmmZdK0l7k1bRy3dDFngwfkQR1RN+Ff4SpaWTYMg== X-Received: by 2002:a5d:5a8d:0:b0:21d:6d69:ca03 with SMTP id bp13-20020a5d5a8d000000b0021d6d69ca03mr17259441wrb.135.1660141485007; Wed, 10 Aug 2022 07:24:45 -0700 (PDT) Received: from localhost (15.72.115.87.dyn.plus.net. [87.115.72.15]) by smtp.gmail.com with ESMTPSA id e9-20020a05600c4e4900b003a5a4635fadsm2710748wmq.37.2022.08.10.07.24.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 07:24:44 -0700 (PDT) From: Andrew Burgess To: binutils@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/3] objdump: introduce OBJDUMP_COLORS environment variable Date: Wed, 10 Aug 2022 15:24:38 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Aug 2022 14:24:50 -0000 Add OBJDUMP_COLORS environment variable, and allow this to control the default behaviour of objdump when --disassembler-color is not used, and we are disassembling to a terminal. After this commit: commit a88c79b77036e4778e70d62081c3cfd1044bb8e3 Date: Tue Aug 9 14:57:48 2022 +0100 Default to enabling colored disassembly if output is to a terminal. If objdump is disassembling to a terminal, and the --disassembler-color argument is not used, then objdump will apply styling by default as if --disassembler-color=color was used. However, some users might not like this color on by default approach, or might prefer to use --disassembler-color=extended-color by default. With this commit objdump will use the OBJDUMP_COLORS environment variable to control its behaviour. With: OBJDUMP_COLORS=off The default behaviour is --disassembler-color=off, that is no colors are added unless the user explicitly uses --disassembler-color at the command line. With: OBJDUMP_COLORS=color Only the basic colors are used, as if --disassembler-color=color was specified. This can be overridden by the user at the command line by explicitly passing --disassembler-color again. With: OBJDUMP_COLORS=extended Only the extended 256-colors are used, as if --disassembler-color=extended-color was specified. This can be overridden by the user at the command line by explicitly passing --disassembler-color again. * doc/binutils.texi (objdump): Add an @xref to the --disassembler-color description. Add a new section describing how to use OBJDUMP_COLORS environment variable. * objdump.c (objdump_colors_var): New global. (objdump_default_disassembler_color_mode): New function. (main): Use objdump_default_disassembler_color_mode. --- binutils/doc/binutils.texi | 22 ++++++++++++++++++++++ binutils/objdump.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 41f38f479f6..6606d2c51ac 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -2830,6 +2830,8 @@ If this option is not specified then the default is to enable color output if displaying to a terminal, but not otherwise. +@xref{Customizing Objdump's Colors}, for more information on +controlling the colors objdump uses in its disassembler output. @item -W[lLiaprmfFsoORtUuTgAckK] @itemx --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,=trace_info,=trace_abbrev,=trace_aranges,=gdb_index,=addr,=cu_index,=links,=follow-links] @@ -3009,6 +3011,26 @@ any other data. @end table +@anchor{Customizing Objdump's Colors} +@section Customizing Objdump's Colors + +The environment variable @code{OBJDUMP_COLORS} can be used to control +the colors objdump uses for it's disassembler output. + +When objdump disassembles to a terminal, and the +@option{--disassembler-color} argument is not used, objdump will, by +default, behave as if @option{--disassembler-color=color} had been +used. + +If @code{OBJDUMP_COLORS} is set to @code{off} then objdump will behave +as if @option{--disassembler-color=off} was given by default. + +If @code{OBJDUMP_COLORS} is set to @code{color} then objdump will +behave as if @option{--disassembler-color=color} was given by default. + +If @code{OBJDUMP_COLORS} is set to @code{extended} then objdump will +behave as if @option{--disassembler-color=extended-color} was given by +default. @c man end @ignore diff --git a/binutils/objdump.c b/binutils/objdump.c index 9259c76c716..1231cda3657 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -139,6 +139,9 @@ static enum color_selection extended /* --disassembler-color=extended-color. */ } disassembler_color = on_if_terminal_output; +/* The environment variable to read for color information. */ +static const char *objdump_colors_var = "OBJDUMP_COLORS"; + static int dump_any_debugging; static int demangle_flags = DMGL_ANSI | DMGL_PARAMS; @@ -2155,6 +2158,32 @@ objdump_sprintf (SFILE *f, const char *format, ...) return n; } +/* Figure out a default disassembler color mode. */ + +static enum color_selection +objdump_default_disassembler_color_mode (void) +{ + enum color_selection mode; + + if (isatty (1)) + { + const char *tmp = getenv (objdump_colors_var); + + if (tmp == NULL || strncmp (tmp, "color", 5) == 0) + mode = on; + if (strncmp (tmp, "extended", 8) == 0) + mode = extended; + else if (strncmp (tmp, "off", 3) == 0) + mode = off; + else + mode = on; + } + else + mode = off; + + return mode; +} + /* Return an integer greater than, or equal to zero, representing the color for STYLE, or -1 if no color should be used. */ @@ -5931,7 +5960,7 @@ main (int argc, char **argv) } if (disassembler_color == on_if_terminal_output) - disassembler_color = isatty (1) ? on : off; + disassembler_color = objdump_default_disassembler_color_mode (); if (show_version) print_version ("objdump"); -- 2.25.4