Index: lldb/include/lldb/Core/Disassembler.h =================================================================== --- lldb/include/lldb/Core/Disassembler.h +++ lldb/include/lldb/Core/Disassembler.h @@ -473,6 +473,8 @@ virtual bool FlavorValidForArchSpec(const lldb_private::ArchSpec &arch, const char *flavor) = 0; + virtual void SetUseColor(bool use_color) {} + protected: // SourceLine and SourceLinesToDisplay structures are only used in the mixed // source and assembly display methods internal to this class. Index: lldb/source/Core/Disassembler.cpp =================================================================== --- lldb/source/Core/Disassembler.cpp +++ lldb/source/Core/Disassembler.cpp @@ -291,6 +291,8 @@ SourceManager &source_manager = target_sp ? target_sp->GetSourceManager() : debugger.GetSourceManager(); + SetUseColor(debugger.GetUseColor()); + if (frame) { pc_addr_ptr = &frame->GetFrameCodeAddress(); } Index: lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.h =================================================================== --- lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.h +++ lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.h @@ -42,6 +42,8 @@ lldb::offset_t data_offset, size_t num_instructions, bool append, bool data_from_file) override; + void SetUseColor(bool use_color) override; + // PluginInterface protocol llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } Index: lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp =================================================================== --- lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp +++ lldb/source/Plugins/Disassembler/LLVMC/DisassemblerLLVMC.cpp @@ -9,6 +9,7 @@ #include "DisassemblerLLVMC.h" #include "llvm-c/Disassembler.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/MC/MCAsmInfo.h" @@ -63,6 +64,8 @@ void PrintMCInst(llvm::MCInst &mc_inst, lldb::addr_t pc, std::string &inst_string, std::string &comments_string); void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style); + void SetUseColor(bool use_color); + bool GetUseColor() const; bool CanBranch(llvm::MCInst &mc_inst) const; bool HasDelaySlot(llvm::MCInst &mc_inst) const; bool IsCall(llvm::MCInst &mc_inst) const; @@ -576,6 +579,12 @@ else mc_disasm_ptr = disasm->m_disasm_up.get(); + // Disable coloring for the duration of this function. + const bool saved_use_color = mc_disasm_ptr->GetUseColor(); + mc_disasm_ptr->SetUseColor(false); + auto on_exit = llvm::make_scope_exit( + [=]() { mc_disasm_ptr->SetUseColor(saved_use_color); }); + lldb::addr_t pc = m_address.GetFileAddress(); m_using_file_addr = true; @@ -1344,10 +1353,12 @@ llvm::raw_string_ostream inst_stream(inst_string); llvm::raw_string_ostream comments_stream(comments_string); + inst_stream.enable_colors(m_instr_printer_up->getUseColor()); m_instr_printer_up->setCommentStream(comments_stream); m_instr_printer_up->printInst(&mc_inst, pc, llvm::StringRef(), *m_subtarget_info_up, inst_stream); m_instr_printer_up->setCommentStream(llvm::nulls()); + comments_stream.flush(); static std::string g_newlines("\r\n"); @@ -1374,6 +1385,14 @@ } } +void DisassemblerLLVMC::MCDisasmInstance::SetUseColor(bool use_color) { + m_instr_printer_up->setUseColor(use_color); +} + +bool DisassemblerLLVMC::MCDisasmInstance::GetUseColor() const { + return m_instr_printer_up->getUseColor(); +} + bool DisassemblerLLVMC::MCDisasmInstance::CanBranch( llvm::MCInst &mc_inst) const { if (m_instr_analysis_up) @@ -1597,6 +1616,13 @@ return lldb::DisassemblerSP(); } +void DisassemblerLLVMC::SetUseColor(bool use_color) { + if (m_disasm_up) + m_disasm_up->SetUseColor(use_color); + if (m_alternate_disasm_up) + m_alternate_disasm_up->SetUseColor(use_color); +} + size_t DisassemblerLLVMC::DecodeInstructions(const Address &base_addr, const DataExtractor &data, lldb::offset_t data_offset, Index: lldb/test/API/python_api/disassemble-raw-data/TestDisassembleRawData.py =================================================================== --- lldb/test/API/python_api/disassemble-raw-data/TestDisassembleRawData.py +++ lldb/test/API/python_api/disassemble-raw-data/TestDisassembleRawData.py @@ -59,9 +59,19 @@ elif arch in ("aarch64", "arm64"): self.assertEqual(inst.GetMnemonic(target), "mov") self.assertEqual(inst.GetOperands(target), "w0, #0x63") + self.assertEqual(inst.GetComment(target), "=99 ") self.assertEqual( inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown ) + # Make sure that using colors doesn't affect the output here. + res = lldb.SBCommandReturnObject() + ci = self.dbg.GetCommandInterpreter() + ci.HandleCommand("settings set use-color true", res) + self.assertEqual(inst.GetOperands(target), "w0, #0x63") + self.assertEqual(inst.GetMnemonic(target), "mov") + self.assertEqual(inst.GetComment(target), "=99 ") + ci.HandleCommand("settings set use-color false", res) + elif arch == "arm": self.assertEqual(inst.GetMnemonic(target), "mov") self.assertEqual(inst.GetOperands(target), "r3, #99")