Skip to content

Commit 95062d7

Browse files
committed
[llvm-objdump] Enable disassembly color highlighting
Enable color highlighting of disassembly in llvm-objdump. This patch introduces a new flag --disassembler-color=<mode> that enables or disables highlighting disassembly with ANSI escape codes. The default mode is to enable color highlighting if outputting to a color-enabled terminal. Differential revision: https://reviews.llvm.org/D159224
1 parent 14c58cf commit 95062d7

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

llvm/docs/CommandGuide/llvm-objdump.rst

+9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ OPTIONS
179179
* ``att``: x86 only (default). Print in the AT&T syntax.
180180
* ``intel``: x86 only. Print in the intel syntax.
181181

182+
183+
.. option:: --disassembler-color=<mode>
184+
185+
Enable or disable disassembler color output.
186+
187+
* ``off``: Disable disassembler color output.
188+
* ``on``: Enable disassembler color output.
189+
* ``terminal``: Enable disassembler color output if the terminal supports it (default).
190+
182191
.. option:: --mcpu=<cpu-name>
183192

184193
Target a specific CPU type for disassembly. Specify ``--mcpu=help`` to display
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: llvm-mc -triple arm64-apple-macosx %s -filetype=obj -o %t
2+
// RUN: llvm-objdump --disassembler-color=on --disassemble %t | FileCheck %s --check-prefix=COLOR
3+
// RUN: llvm-objdump --disassembler-color=off --disassemble %t | FileCheck %s --check-prefix=NOCOLOR
4+
// RUN: llvm-objdump --disassembler-color=terminal --disassemble %t | FileCheck %s --check-prefix=NOCOLOR
5+
6+
sub sp, sp, #16
7+
str w0, [sp, #12]
8+
ldr w8, [sp, #12]
9+
ldr w9, [sp, #12]
10+
mul w0, w8, w9
11+
add sp, sp, #16
12+
13+
// NOCOLOR: sub sp, sp, #0x10
14+
// NOCOLOR: str w0, [sp, #0xc]
15+
// NOCOLOR: ldr w8, [sp, #0xc]
16+
// NOCOLOR: ldr w9, [sp, #0xc]
17+
// NOCOLOR: mul w0, w8, w9
18+
// NOCOLOR: add sp, sp, #0x10
19+
20+
// COLOR: sub sp, sp, #0x10
21+
// COLOR: str w0, [sp, #0xc]
22+
// COLOR: ldr w8, [sp, #0xc]
23+
// COLOR: ldr w9, [sp, #0xc]
24+
// COLOR: mul w0, w8, w9
25+
// COLOR: add sp, sp, #0x10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: llvm-mc -triple arm64-apple-macosx %s -filetype=obj -o %t
2+
// RUN: llvm-objdump --disassembler-color=on --disassemble %t | FileCheck %s --check-prefix=COLOR
3+
// RUN: llvm-objdump --disassembler-color=off --disassemble %t | FileCheck %s --check-prefix=NOCOLOR
4+
// RUN: llvm-objdump --disassembler-color=terminal --disassemble %t | FileCheck %s --check-prefix=NOCOLOR
5+
6+
sub sp, sp, #16
7+
str w0, [sp, #12]
8+
ldr w8, [sp, #12]
9+
ldr w9, [sp, #12]
10+
mul w0, w8, w9
11+
add sp, sp, #16
12+
13+
// NOCOLOR: sub sp, sp, #0x10
14+
// NOCOLOR: str w0, [sp, #0xc]
15+
// NOCOLOR: ldr w8, [sp, #0xc]
16+
// NOCOLOR: ldr w9, [sp, #0xc]
17+
// NOCOLOR: mul w0, w8, w9
18+
// NOCOLOR: add sp, sp, #0x10
19+
20+
// COLOR: sub sp, sp, #0x10
21+
// COLOR: str w0, [sp, #0xc]
22+
// COLOR: ldr w8, [sp, #0xc]
23+
// COLOR: ldr w9, [sp, #0xc]
24+
// COLOR: mul w0, w8, w9
25+
// COLOR: add sp, sp, #0x10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: llvm-mc -triple arm64-apple-macosx %s -filetype=obj -o %t
2+
// RUN: llvm-objdump --disassembler-color=on --disassemble %t | FileCheck %s --check-prefix=COLOR
3+
// RUN: llvm-objdump --disassembler-color=off --disassemble %t | FileCheck %s --check-prefix=NOCOLOR
4+
// RUN: llvm-objdump --disassembler-color=terminal --disassemble %t | FileCheck %s --check-prefix=NOCOLOR
5+
6+
sub sp, sp, #16
7+
str w0, [sp, #12]
8+
ldr w8, [sp, #12]
9+
ldr w9, [sp, #12]
10+
mul w0, w8, w9
11+
add sp, sp, #16
12+
13+
// NOCOLOR: sub sp, sp, #0x10
14+
// NOCOLOR: str w0, [sp, #0xc]
15+
// NOCOLOR: ldr w8, [sp, #0xc]
16+
// NOCOLOR: ldr w9, [sp, #0xc]
17+
// NOCOLOR: mul w0, w8, w9
18+
// NOCOLOR: add sp, sp, #0x10
19+
20+
// COLOR: sub sp, sp, #0x10
21+
// COLOR: str w0, [sp, #0xc]
22+
// COLOR: ldr w8, [sp, #0xc]
23+
// COLOR: ldr w9, [sp, #0xc]
24+
// COLOR: mul w0, w8, w9
25+
// COLOR: add sp, sp, #0x10

llvm/tools/llvm-objdump/ObjdumpOpts.td

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ def disassembler_options_EQ : Joined<["--"], "disassembler-options=">,
8787
def : JoinedOrSeparate<["-"], "M">, Alias<disassembler_options_EQ>,
8888
HelpText<"Alias for --disassembler-options=">;
8989

90+
def disassembler_color_EQ : Joined<["--"], "disassembler-color=">,
91+
MetaVarName<"mode">,
92+
HelpText<"Enable or disable disassembler color output. "
93+
"Valid options are \"on\", \"off\" and \"terminal\" (default)">;
94+
9095
def dynamic_reloc : Flag<["--"], "dynamic-reloc">,
9196
HelpText<"Display the dynamic relocation entries in the file">;
9297
def : Flag<["-"], "R">, Alias<dynamic_reloc>,

llvm/tools/llvm-objdump/llvm-objdump.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ class OtoolOptTable : public CommonOptTable {
177177

178178
#define DEBUG_TYPE "objdump"
179179

180+
enum class ColorOutput {
181+
Auto,
182+
Enable,
183+
Disable,
184+
Invalid,
185+
};
186+
180187
static uint64_t AdjustVMA;
181188
static bool AllHeaders;
182189
static std::string ArchName;
@@ -189,6 +196,7 @@ bool objdump::TracebackTable;
189196
static std::vector<std::string> DisassembleSymbols;
190197
static bool DisassembleZeroes;
191198
static std::vector<std::string> DisassemblerOptions;
199+
static ColorOutput DisassemblyColor;
192200
DIDumpType objdump::DwarfDumpType;
193201
static bool DynamicRelocations;
194202
static bool FaultMapSection;
@@ -900,6 +908,19 @@ DisassemblerTarget::DisassemblerTarget(const Target *TheTarget, ObjectFile &Obj,
900908
InstPrinter->setPrintBranchImmAsAddress(true);
901909
InstPrinter->setSymbolizeOperands(SymbolizeOperands);
902910
InstPrinter->setMCInstrAnalysis(InstrAnalysis.get());
911+
912+
switch (DisassemblyColor) {
913+
case ColorOutput::Enable:
914+
InstPrinter->setUseColor(true);
915+
break;
916+
case ColorOutput::Auto:
917+
InstPrinter->setUseColor(outs().has_colors());
918+
break;
919+
case ColorOutput::Disable:
920+
case ColorOutput::Invalid:
921+
InstPrinter->setUseColor(false);
922+
break;
923+
};
903924
}
904925

905926
DisassemblerTarget::DisassemblerTarget(DisassemblerTarget &Other,
@@ -1900,6 +1921,13 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
19001921

19011922
formatted_raw_ostream FOS(outs());
19021923

1924+
// FIXME: Workaround for bug in formatted_raw_ostream. Color escape codes
1925+
// are (incorrectly) written directly to the unbuffered raw_ostream
1926+
// wrapped by the formatted_raw_ostream.
1927+
if (DisassemblyColor == ColorOutput::Enable ||
1928+
DisassemblyColor == ColorOutput::Auto)
1929+
FOS.SetUnbuffered();
1930+
19031931
std::unordered_map<uint64_t, std::string> AllLabels;
19041932
std::unordered_map<uint64_t, std::vector<std::string>> BBAddrMapLabels;
19051933
if (SymbolizeOperands) {
@@ -3193,6 +3221,16 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
31933221
if (DbgVariables == DVInvalid)
31943222
invalidArgValue(A);
31953223
}
3224+
if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_disassembler_color_EQ)) {
3225+
DisassemblyColor = StringSwitch<ColorOutput>(A->getValue())
3226+
.Case("on", ColorOutput::Enable)
3227+
.Case("off", ColorOutput::Disable)
3228+
.Case("terminal", ColorOutput::Auto)
3229+
.Default(ColorOutput::Invalid);
3230+
if (DisassemblyColor == ColorOutput::Invalid)
3231+
invalidArgValue(A);
3232+
}
3233+
31963234
parseIntArg(InputArgs, OBJDUMP_debug_vars_indent_EQ, DbgIndent);
31973235

31983236
parseMachOOptions(InputArgs);

0 commit comments

Comments
 (0)