@@ -177,6 +177,13 @@ class OtoolOptTable : public CommonOptTable {
177
177
178
178
#define DEBUG_TYPE " objdump"
179
179
180
+ enum class ColorOutput {
181
+ Auto,
182
+ Enable,
183
+ Disable,
184
+ Invalid,
185
+ };
186
+
180
187
static uint64_t AdjustVMA;
181
188
static bool AllHeaders;
182
189
static std::string ArchName;
@@ -189,6 +196,7 @@ bool objdump::TracebackTable;
189
196
static std::vector<std::string> DisassembleSymbols;
190
197
static bool DisassembleZeroes;
191
198
static std::vector<std::string> DisassemblerOptions;
199
+ static ColorOutput DisassemblyColor;
192
200
DIDumpType objdump::DwarfDumpType;
193
201
static bool DynamicRelocations;
194
202
static bool FaultMapSection;
@@ -900,6 +908,19 @@ DisassemblerTarget::DisassemblerTarget(const Target *TheTarget, ObjectFile &Obj,
900
908
InstPrinter->setPrintBranchImmAsAddress (true );
901
909
InstPrinter->setSymbolizeOperands (SymbolizeOperands);
902
910
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
+ };
903
924
}
904
925
905
926
DisassemblerTarget::DisassemblerTarget (DisassemblerTarget &Other,
@@ -1900,6 +1921,13 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
1900
1921
1901
1922
formatted_raw_ostream FOS (outs ());
1902
1923
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
+
1903
1931
std::unordered_map<uint64_t , std::string> AllLabels;
1904
1932
std::unordered_map<uint64_t , std::vector<std::string>> BBAddrMapLabels;
1905
1933
if (SymbolizeOperands) {
@@ -3193,6 +3221,16 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
3193
3221
if (DbgVariables == DVInvalid)
3194
3222
invalidArgValue (A);
3195
3223
}
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
+
3196
3234
parseIntArg (InputArgs, OBJDUMP_debug_vars_indent_EQ, DbgIndent);
3197
3235
3198
3236
parseMachOOptions (InputArgs);
0 commit comments