[Lldb-commits] [lldb] Colorize output when searching for symbols in lldb (PR #69422)
via lldb-commits
lldb-commits at lists.llvm.org
Sun Nov 19 23:07:18 PST 2023
=?utf-8?q?José?= L. Junior <josejunior at 10xengineers.ai>,taalhaataahir0102
<23100293 at lums.edu.pk>,
=?utf-8?q?José?= L. Junior <josejunior at 10xengineers.ai>,
=?utf-8?q?José?= L. Junior <josejunior at 10xengineers.ai>,taalhaataahir0102
<23100293 at lums.edu.pk>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/69422 at github.com>
https://github.com/taalhaataahir0102 updated https://github.com/llvm/llvm-project/pull/69422
>From 2c23aaf231beef11d3e0db6506fe82323a0be6a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20L=2E=20Junior?= <josejunior at 10xengineers.ai>
Date: Tue, 7 Nov 2023 16:57:18 -0300
Subject: [PATCH 1/7] [lldb] colorize symbols in image lookup
---
lldb/include/lldb/Core/Address.h | 7 ++-
lldb/include/lldb/Symbol/Symbol.h | 4 +-
lldb/include/lldb/Symbol/SymbolContext.h | 8 +--
lldb/source/Commands/CommandObjectTarget.cpp | 15 ++++--
lldb/source/Core/Address.cpp | 53 ++++++++++++++++---
lldb/source/Symbol/Symbol.cpp | 18 ++++---
lldb/source/Symbol/SymbolContext.cpp | 16 ++++--
.../Commands/command-image-lookup-color.test | 25 +++++++++
8 files changed, 114 insertions(+), 32 deletions(-)
create mode 100644 lldb/test/Shell/Commands/command-image-lookup-color.test
diff --git a/lldb/include/lldb/Core/Address.h b/lldb/include/lldb/Core/Address.h
index b19e694427546f8..fac0ced910a11d4 100644
--- a/lldb/include/lldb/Core/Address.h
+++ b/lldb/include/lldb/Core/Address.h
@@ -246,8 +246,11 @@ class Address {
/// \see Address::DumpStyle
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
DumpStyle fallback_style = DumpStyleInvalid,
- uint32_t addr_byte_size = UINT32_MAX,
- bool all_ranges = false) const;
+ uint32_t addr_byte_size = UINT32_MAX, bool all_ranges = false,
+ const char *pattern = nullptr) const;
+
+ static void DumpName(Stream *strm, llvm::StringRef text,
+ const char *pattern = nullptr);
AddressClass GetAddressClass() const;
diff --git a/lldb/include/lldb/Symbol/Symbol.h b/lldb/include/lldb/Symbol/Symbol.h
index 44a2d560010fe40..0e41cd95e0ef17d 100644
--- a/lldb/include/lldb/Symbol/Symbol.h
+++ b/lldb/include/lldb/Symbol/Symbol.h
@@ -174,8 +174,8 @@ class Symbol : public SymbolContextScope {
void SetFlags(uint32_t flags) { m_flags = flags; }
- void GetDescription(Stream *s, lldb::DescriptionLevel level,
- Target *target) const;
+ void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target,
+ const char *pattern = nullptr) const;
bool IsSynthetic() const { return m_is_synthetic; }
diff --git a/lldb/include/lldb/Symbol/SymbolContext.h b/lldb/include/lldb/Symbol/SymbolContext.h
index b0f5ffead2a1656..9567c3f4384c175 100644
--- a/lldb/include/lldb/Symbol/SymbolContext.h
+++ b/lldb/include/lldb/Symbol/SymbolContext.h
@@ -150,8 +150,8 @@ class SymbolContext {
bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
const Address &so_addr, bool show_fullpaths,
bool show_module, bool show_inlined_frames,
- bool show_function_arguments,
- bool show_function_name) const;
+ bool show_function_arguments, bool show_function_name,
+ const char *pattern = nullptr) const;
/// Get the address range contained within a symbol context.
///
@@ -217,8 +217,8 @@ class SymbolContext {
/// The symbol that was found, or \b nullptr if none was found.
const Symbol *FindBestGlobalDataSymbol(ConstString name, Status &error);
- void GetDescription(Stream *s, lldb::DescriptionLevel level,
- Target *target) const;
+ void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target,
+ const char *pattern = nullptr) const;
uint32_t GetResolvedMask() const;
diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index 8f052d0a7b837e2..a83575ad82d6909 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -8,6 +8,7 @@
#include "CommandObjectTarget.h"
+#include "lldb/Core/Address.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/IOHandler.h"
#include "lldb/Core/Module.h"
@@ -1534,7 +1535,7 @@ static void DumpOsoFilesTable(Stream &strm,
static void DumpAddress(ExecutionContextScope *exe_scope,
const Address &so_addr, bool verbose, bool all_ranges,
- Stream &strm) {
+ Stream &strm, const char *pattern = nullptr) {
strm.IndentMore();
strm.Indent(" Address: ");
so_addr.Dump(&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
@@ -1544,13 +1545,14 @@ static void DumpAddress(ExecutionContextScope *exe_scope,
strm.Indent(" Summary: ");
const uint32_t save_indent = strm.GetIndentLevel();
strm.SetIndentLevel(save_indent + 13);
- so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription);
+ so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription,
+ Address::DumpStyleInvalid, UINT32_MAX, false, pattern);
strm.SetIndentLevel(save_indent);
// Print out detailed address information when verbose is enabled
if (verbose) {
strm.EOL();
so_addr.Dump(&strm, exe_scope, Address::DumpStyleDetailedSymbolContext,
- Address::DumpStyleInvalid, UINT32_MAX, all_ranges);
+ Address::DumpStyleInvalid, UINT32_MAX, all_ranges, pattern);
}
strm.IndentLess();
}
@@ -1595,6 +1597,7 @@ static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
return 0;
SymbolContext sc;
+ bool use_color = interpreter.GetDebugger().GetUseColor();
std::vector<uint32_t> match_indexes;
ConstString symbol_name(name);
uint32_t num_matches = 0;
@@ -1620,12 +1623,14 @@ static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
if (symbol->ValueIsAddress()) {
DumpAddress(
interpreter.GetExecutionContext().GetBestExecutionContextScope(),
- symbol->GetAddressRef(), verbose, all_ranges, strm);
+ symbol->GetAddressRef(), verbose, all_ranges, strm,
+ use_color ? name : nullptr);
strm.EOL();
} else {
strm.IndentMore();
strm.Indent(" Name: ");
- strm.PutCString(symbol->GetDisplayName().GetStringRef());
+ Address::DumpName(&strm, symbol->GetDisplayName().GetStringRef(),
+ use_color ? name : nullptr);
strm.EOL();
strm.Indent(" Value: ");
strm.Printf("0x%16.16" PRIx64 "\n", symbol->GetRawValue());
diff --git a/lldb/source/Core/Address.cpp b/lldb/source/Core/Address.cpp
index 189d50fe962a651..7e3ab36c238eabb 100644
--- a/lldb/source/Core/Address.cpp
+++ b/lldb/source/Core/Address.cpp
@@ -28,6 +28,7 @@
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
+#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Endian.h"
@@ -403,9 +404,41 @@ bool Address::GetDescription(Stream &s, Target &target,
return false;
}
+void Address::DumpName(Stream *strm, llvm::StringRef text,
+ const char *pattern) {
+ if (!pattern) {
+ strm->PutCString(text.data());
+ return;
+ }
+
+ llvm::Regex reg_pattern(pattern);
+ llvm::SmallVector<llvm::StringRef, 1> matches;
+ llvm::StringRef remaining = text;
+ std::string red_start =
+ lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.fg.red}");
+ std::string reset_color =
+ lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.normal}");
+
+ size_t last_pos = 0;
+ while (reg_pattern.match(remaining, &matches)) {
+ llvm::StringRef match = matches[0];
+ size_t match_start_pos = match.data() - text.data();
+ size_t match_end_pos = match_start_pos + match.size();
+
+ strm->Write(text.data() + last_pos, match_start_pos - last_pos);
+ strm->PutCString(red_start.c_str());
+ strm->Write(text.data() + match_start_pos, match.size());
+ strm->PutCString(reset_color.c_str());
+ last_pos = match_end_pos;
+ remaining = text.substr(last_pos);
+ }
+ if (last_pos < text.size())
+ strm->PutCString(text.data() + last_pos);
+}
+
bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
DumpStyle fallback_style, uint32_t addr_size,
- bool all_ranges) const {
+ bool all_ranges, const char *pattern) const {
// If the section was nullptr, only load address is going to work unless we
// are trying to deref a pointer
SectionSP section_sp(GetSection());
@@ -515,7 +548,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
if (symbol) {
const char *symbol_name = symbol->GetName().AsCString();
if (symbol_name) {
- s->PutCString(symbol_name);
+ DumpName(s, symbol_name, pattern);
addr_t delta =
file_Addr - symbol->GetAddressRef().GetFileAddress();
if (delta)
@@ -643,7 +676,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
pointer_sc.symbol != nullptr) {
s->PutCString(": ");
pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
- false, true, true);
+ false, true, true, pattern);
}
}
}
@@ -682,19 +715,22 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
// address.
sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
show_module, show_inlined_frames,
- show_function_arguments, show_function_name);
+ show_function_arguments, show_function_name,
+ pattern);
} else {
// We found a symbol but it was in a different section so it
// isn't the symbol we should be showing, just show the section
// name + offset
- Dump(s, exe_scope, DumpStyleSectionNameOffset);
+ Dump(s, exe_scope, DumpStyleSectionNameOffset, DumpStyleInvalid,
+ UINT32_MAX, false, pattern);
}
}
}
}
} else {
if (fallback_style != DumpStyleInvalid)
- return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
+ return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size,
+ false, pattern);
return false;
}
break;
@@ -715,7 +751,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
sc.symbol->GetAddressRef().GetSection() != GetSection())
sc.symbol = nullptr;
}
- sc.GetDescription(s, eDescriptionLevelBrief, target);
+ sc.GetDescription(s, eDescriptionLevelBrief, target, pattern);
if (sc.block) {
bool can_create = true;
@@ -763,7 +799,8 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
}
} else {
if (fallback_style != DumpStyleInvalid)
- return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
+ return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size,
+ false, pattern);
return false;
}
break;
diff --git a/lldb/source/Symbol/Symbol.cpp b/lldb/source/Symbol/Symbol.cpp
index 26b4c4d62ad9c24..1e113e59e53d895 100644
--- a/lldb/source/Symbol/Symbol.cpp
+++ b/lldb/source/Symbol/Symbol.cpp
@@ -8,6 +8,7 @@
#include "lldb/Symbol/Symbol.h"
+#include "lldb/Core/Address.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/Section.h"
@@ -225,7 +226,7 @@ bool Symbol::IsTrampoline() const { return m_type == eSymbolTypeTrampoline; }
bool Symbol::IsIndirect() const { return m_type == eSymbolTypeResolver; }
void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
- Target *target) const {
+ Target *target, const char *pattern) const {
s->Printf("id = {0x%8.8x}", m_uid);
if (m_addr_range.GetBaseAddress().GetSection()) {
@@ -252,11 +253,16 @@ void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
s->Printf(", value = 0x%16.16" PRIx64,
m_addr_range.GetBaseAddress().GetOffset());
}
- ConstString demangled = GetMangled().GetDemangledName();
- if (demangled)
- s->Printf(", name=\"%s\"", demangled.AsCString());
- if (m_mangled.GetMangledName())
- s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
+ if (ConstString mangled_name = m_mangled.GetMangledName()) {
+ s->Printf(", mangled=\"");
+ Address::DumpName(s, mangled_name.GetStringRef(), pattern);
+ s->Printf("\"");
+ }
+ if (ConstString demangled = m_mangled.GetDemangledName()) {
+ s->Printf(", name=\"");
+ Address::DumpName(s, demangled.GetStringRef(), pattern);
+ s->Printf("\"");
+ }
}
void Symbol::Dump(Stream *s, Target *target, uint32_t index,
diff --git a/lldb/source/Symbol/SymbolContext.cpp b/lldb/source/Symbol/SymbolContext.cpp
index 63968ec2d150670..d231432dfd7ae34 100644
--- a/lldb/source/Symbol/SymbolContext.cpp
+++ b/lldb/source/Symbol/SymbolContext.cpp
@@ -8,6 +8,7 @@
#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Core/Address.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
@@ -71,7 +72,8 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
const Address &addr, bool show_fullpaths,
bool show_module, bool show_inlined_frames,
bool show_function_arguments,
- bool show_function_name) const {
+ bool show_function_name,
+ const char *pattern) const {
bool dumped_something = false;
if (show_module && module_sp) {
if (show_fullpaths)
@@ -95,7 +97,8 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
if (!name)
name = function->GetName();
if (name)
- name.Dump(s);
+ pattern ? Address::DumpName(s, name.GetStringRef(), pattern)
+ : name.Dump(s);
}
if (addr.IsValid()) {
@@ -163,7 +166,10 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
dumped_something = true;
if (symbol->GetType() == eSymbolTypeTrampoline)
s->PutCString("symbol stub for: ");
- symbol->GetName().Dump(s);
+ if (pattern)
+ Address::DumpName(s, symbol->GetName().GetStringRef(), pattern);
+ else
+ symbol->GetName().Dump(s);
}
if (addr.IsValid() && symbol->ValueIsAddress()) {
@@ -186,7 +192,7 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
}
void SymbolContext::GetDescription(Stream *s, lldb::DescriptionLevel level,
- Target *target) const {
+ Target *target, const char *pattern) const {
if (module_sp) {
s->Indent(" Module: file = \"");
module_sp->GetFileSpec().Dump(s->AsRawOstream());
@@ -246,7 +252,7 @@ void SymbolContext::GetDescription(Stream *s, lldb::DescriptionLevel level,
if (symbol != nullptr) {
s->Indent(" Symbol: ");
- symbol->GetDescription(s, level, target);
+ symbol->GetDescription(s, level, target, pattern);
s->EOL();
}
diff --git a/lldb/test/Shell/Commands/command-image-lookup-color.test b/lldb/test/Shell/Commands/command-image-lookup-color.test
new file mode 100644
index 000000000000000..754596e1c139850
--- /dev/null
+++ b/lldb/test/Shell/Commands/command-image-lookup-color.test
@@ -0,0 +1,25 @@
+# RUN: %clang_host -g %S/Inputs/main.c -o %t
+
+# Checking simple search
+
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s ma' | FileCheck %s --check-prefix CHECK1
+# CHECK1: Name: {{.+}}31mma{{.+}}0min.c
+# CHECK1: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2
+
+# Checking for regex searches
+
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s main.c|foo' | FileCheck %s --check-prefix CHECK2
+# CHECK2: Name: {{.+}}31mmain.c{{.+}}0m
+# CHECK2: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
+
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s m[abc]' | FileCheck %s --check-prefix CHECK3
+# CHECK3: Name: {{.+}}31mma{{.+}}0min.c
+# CHECK3: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2
+
+# Checking tail match
+
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s .*o$' | FileCheck %s --check-prefix CHECK4
+# CHECK4: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
+
+# RUN: %lldb %t -o 'settings set use-color true' -o 'image lookup -r -s IMPPATTERN123456' | FileCheck %s --check-prefix CHECK5
+# CHECK5-NOT: {{[0-9]+}} symbols match the regular expression
>From f9d778680b3d3b2ba761a2bdc5ca957436f7e109 Mon Sep 17 00:00:00 2001
From: taalhaataahir0102 <23100293 at lums.edu.pk>
Date: Tue, 14 Nov 2023 16:59:42 +0500
Subject: [PATCH 2/7] Updated Testcases
---
lldb/test/Shell/Commands/Inputs/main.c | 1 +
.../Commands/command-image-lookup-color.test | 21 +++++++++++++++----
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/lldb/test/Shell/Commands/Inputs/main.c b/lldb/test/Shell/Commands/Inputs/main.c
index c029ddd96cd52ed..39f3b739fa00c47 100644
--- a/lldb/test/Shell/Commands/Inputs/main.c
+++ b/lldb/test/Shell/Commands/Inputs/main.c
@@ -1,2 +1,3 @@
int foo() { return 0; }
+int foo_bar() { return 0; }
int main() { return foo(); }
diff --git a/lldb/test/Shell/Commands/command-image-lookup-color.test b/lldb/test/Shell/Commands/command-image-lookup-color.test
index 754596e1c139850..6908100df56ddf3 100644
--- a/lldb/test/Shell/Commands/command-image-lookup-color.test
+++ b/lldb/test/Shell/Commands/command-image-lookup-color.test
@@ -1,12 +1,12 @@
# RUN: %clang_host -g %S/Inputs/main.c -o %t
-# Checking simple search
+# Checking simple regex search
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s ma' | FileCheck %s --check-prefix CHECK1
# CHECK1: Name: {{.+}}31mma{{.+}}0min.c
-# CHECK1: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2
+# CHECK1: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:3
-# Checking for regex searches
+# Checking complex regex searches
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s main.c|foo' | FileCheck %s --check-prefix CHECK2
# CHECK2: Name: {{.+}}31mmain.c{{.+}}0m
@@ -14,12 +14,25 @@
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s m[abc]' | FileCheck %s --check-prefix CHECK3
# CHECK3: Name: {{.+}}31mma{{.+}}0min.c
-# CHECK3: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2
+# CHECK3: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:3
# Checking tail match
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s .*o$' | FileCheck %s --check-prefix CHECK4
# CHECK4: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
+# Checking to ensure that no attempt is made to color anything when there are no matching symbols found
+
# RUN: %lldb %t -o 'settings set use-color true' -o 'image lookup -r -s IMPPATTERN123456' | FileCheck %s --check-prefix CHECK5
# CHECK5-NOT: {{[0-9]+}} symbols match the regular expression
+
+# Checking search which find more than 1 symbol
+
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s foo' | FileCheck %s --check-prefix CHECK6
+# CHECK6: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
+# CHECK6: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m_bar at main.c:2
+
+# Checking multiple matches on same symbol
+
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s foo|bar' | FileCheck %s --check-prefix CHECK7
+# CHECK7: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m_{{.+}}31mbar{{.+}}0m at main.c:2
\ No newline at end of file
>From b76ed339c2f67e4c44ebfb65cc7849d328579961 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20L=2E=20Junior?= <josejunior at 10xengineers.ai>
Date: Tue, 14 Nov 2023 14:32:42 -0300
Subject: [PATCH 3/7] changed Address::DumpName to
Stream::PutCStringColorHighlighted
---
lldb/include/lldb/Core/Address.h | 3 --
lldb/include/lldb/Utility/Stream.h | 12 +++++++
lldb/source/Commands/CommandObjectTarget.cpp | 5 +--
lldb/source/Core/Address.cpp | 34 +-------------------
lldb/source/Symbol/Symbol.cpp | 12 +++----
lldb/source/Symbol/SymbolContext.cpp | 6 ++--
lldb/source/Utility/Stream.cpp | 28 ++++++++++++++++
7 files changed, 53 insertions(+), 47 deletions(-)
diff --git a/lldb/include/lldb/Core/Address.h b/lldb/include/lldb/Core/Address.h
index fac0ced910a11d4..c3f2832be424efd 100644
--- a/lldb/include/lldb/Core/Address.h
+++ b/lldb/include/lldb/Core/Address.h
@@ -249,9 +249,6 @@ class Address {
uint32_t addr_byte_size = UINT32_MAX, bool all_ranges = false,
const char *pattern = nullptr) const;
- static void DumpName(Stream *strm, llvm::StringRef text,
- const char *pattern = nullptr);
-
AddressClass GetAddressClass() const;
/// Get the file address.
diff --git a/lldb/include/lldb/Utility/Stream.h b/lldb/include/lldb/Utility/Stream.h
index 1a5fd343e4df0dc..a4897c70a83d6b8 100644
--- a/lldb/include/lldb/Utility/Stream.h
+++ b/lldb/include/lldb/Utility/Stream.h
@@ -231,6 +231,18 @@ class Stream {
/// The string to be output to the stream.
size_t PutCString(llvm::StringRef cstr);
+ /// Output a C string to the stream with color highlighting.
+ ///
+ /// Print a C string \a text to the stream, applying color highlighting to
+ /// the specified \a pattern within the string.
+ ///
+ /// \param[in] text
+ /// The string to be output to the stream.
+ ///
+ /// \param[in] pattern
+ /// The portion of the \a text string to be colorized for highlighting.
+ void PutCStringColorHighlighted(llvm::StringRef text, const char *pattern);
+
/// Output and End of Line character to the stream.
size_t EOL();
diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index a83575ad82d6909..ea69a91ad1224a5 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -1629,8 +1629,9 @@ static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
} else {
strm.IndentMore();
strm.Indent(" Name: ");
- Address::DumpName(&strm, symbol->GetDisplayName().GetStringRef(),
- use_color ? name : nullptr);
+ strm.PutCStringColorHighlighted(
+ symbol->GetDisplayName().GetStringRef(),
+ use_color ? name : nullptr);
strm.EOL();
strm.Indent(" Value: ");
strm.Printf("0x%16.16" PRIx64 "\n", symbol->GetRawValue());
diff --git a/lldb/source/Core/Address.cpp b/lldb/source/Core/Address.cpp
index 7e3ab36c238eabb..4a05a744e02300f 100644
--- a/lldb/source/Core/Address.cpp
+++ b/lldb/source/Core/Address.cpp
@@ -404,38 +404,6 @@ bool Address::GetDescription(Stream &s, Target &target,
return false;
}
-void Address::DumpName(Stream *strm, llvm::StringRef text,
- const char *pattern) {
- if (!pattern) {
- strm->PutCString(text.data());
- return;
- }
-
- llvm::Regex reg_pattern(pattern);
- llvm::SmallVector<llvm::StringRef, 1> matches;
- llvm::StringRef remaining = text;
- std::string red_start =
- lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.fg.red}");
- std::string reset_color =
- lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.normal}");
-
- size_t last_pos = 0;
- while (reg_pattern.match(remaining, &matches)) {
- llvm::StringRef match = matches[0];
- size_t match_start_pos = match.data() - text.data();
- size_t match_end_pos = match_start_pos + match.size();
-
- strm->Write(text.data() + last_pos, match_start_pos - last_pos);
- strm->PutCString(red_start.c_str());
- strm->Write(text.data() + match_start_pos, match.size());
- strm->PutCString(reset_color.c_str());
- last_pos = match_end_pos;
- remaining = text.substr(last_pos);
- }
- if (last_pos < text.size())
- strm->PutCString(text.data() + last_pos);
-}
-
bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
DumpStyle fallback_style, uint32_t addr_size,
bool all_ranges, const char *pattern) const {
@@ -548,7 +516,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
if (symbol) {
const char *symbol_name = symbol->GetName().AsCString();
if (symbol_name) {
- DumpName(s, symbol_name, pattern);
+ s->PutCStringColorHighlighted(symbol_name, pattern);
addr_t delta =
file_Addr - symbol->GetAddressRef().GetFileAddress();
if (delta)
diff --git a/lldb/source/Symbol/Symbol.cpp b/lldb/source/Symbol/Symbol.cpp
index 1e113e59e53d895..af47dc51d9ee9cd 100644
--- a/lldb/source/Symbol/Symbol.cpp
+++ b/lldb/source/Symbol/Symbol.cpp
@@ -253,14 +253,14 @@ void Symbol::GetDescription(Stream *s, lldb::DescriptionLevel level,
s->Printf(", value = 0x%16.16" PRIx64,
m_addr_range.GetBaseAddress().GetOffset());
}
- if (ConstString mangled_name = m_mangled.GetMangledName()) {
- s->Printf(", mangled=\"");
- Address::DumpName(s, mangled_name.GetStringRef(), pattern);
- s->Printf("\"");
- }
if (ConstString demangled = m_mangled.GetDemangledName()) {
s->Printf(", name=\"");
- Address::DumpName(s, demangled.GetStringRef(), pattern);
+ s->PutCStringColorHighlighted(demangled.GetStringRef(), pattern);
+ s->Printf("\"");
+ }
+ if (ConstString mangled_name = m_mangled.GetMangledName()) {
+ s->Printf(", mangled=\"");
+ s->PutCStringColorHighlighted(mangled_name.GetStringRef(), pattern);
s->Printf("\"");
}
}
diff --git a/lldb/source/Symbol/SymbolContext.cpp b/lldb/source/Symbol/SymbolContext.cpp
index d231432dfd7ae34..21064b1fd1b3838 100644
--- a/lldb/source/Symbol/SymbolContext.cpp
+++ b/lldb/source/Symbol/SymbolContext.cpp
@@ -97,8 +97,7 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
if (!name)
name = function->GetName();
if (name)
- pattern ? Address::DumpName(s, name.GetStringRef(), pattern)
- : name.Dump(s);
+ s->PutCStringColorHighlighted(name.GetStringRef(), pattern);
}
if (addr.IsValid()) {
@@ -167,7 +166,8 @@ bool SymbolContext::DumpStopContext(Stream *s, ExecutionContextScope *exe_scope,
if (symbol->GetType() == eSymbolTypeTrampoline)
s->PutCString("symbol stub for: ");
if (pattern)
- Address::DumpName(s, symbol->GetName().GetStringRef(), pattern);
+ s->PutCStringColorHighlighted(symbol->GetName().GetStringRef(),
+ pattern);
else
symbol->GetName().Dump(s);
}
diff --git a/lldb/source/Utility/Stream.cpp b/lldb/source/Utility/Stream.cpp
index af28a49a1f0c2b5..9e2d9737601f7f5 100644
--- a/lldb/source/Utility/Stream.cpp
+++ b/lldb/source/Utility/Stream.cpp
@@ -8,11 +8,13 @@
#include "lldb/Utility/Stream.h"
+#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/VASPrintf.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/LEB128.h"
+#include "llvm/Support/Regex.h"
#include <string>
@@ -70,6 +72,32 @@ size_t Stream::PutCString(llvm::StringRef str) {
return bytes_written;
}
+void Stream::PutCStringColorHighlighted(llvm::StringRef text,
+ const char *pattern) {
+ if (!pattern) {
+ PutCString(text.data());
+ return;
+ }
+
+ // If pattern is not nullptr, we should use color
+ llvm::Regex reg_pattern(pattern);
+ llvm::SmallVector<llvm::StringRef, 1> matches;
+ llvm::StringRef remaining = text;
+ std::string format_str = lldb_private::ansi::FormatAnsiTerminalCodes(
+ "${ansi.fg.red}%s${ansi.normal}");
+ size_t last_pos = 0;
+ while (reg_pattern.match(remaining, &matches)) {
+ llvm::StringRef match = matches[0];
+ size_t match_start_pos = match.data() - remaining.data();
+ Write(remaining.data(), match_start_pos);
+ Printf(format_str.c_str(), match.str().c_str());
+ last_pos = match_start_pos + match.size();
+ remaining = remaining.drop_front(last_pos);
+ }
+ if (remaining.size())
+ PutCString(remaining.data());
+}
+
// Print a double quoted NULL terminated C string to the stream using the
// printf format in "format".
void Stream::QuotedCString(const char *cstr, const char *format) {
>From 6c463bf9d8840c4b443d0dc07eaeebe65ed06900 Mon Sep 17 00:00:00 2001
From: taalhaataahir0102 <23100293 at lums.edu.pk>
Date: Wed, 15 Nov 2023 16:37:07 +0500
Subject: [PATCH 4/7] coreected test cases
---
lldb/test/Shell/Commands/Inputs/main.c | 1 -
.../Shell/Commands/command-image-lookup-color.test | 13 +++++++------
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/lldb/test/Shell/Commands/Inputs/main.c b/lldb/test/Shell/Commands/Inputs/main.c
index 39f3b739fa00c47..c029ddd96cd52ed 100644
--- a/lldb/test/Shell/Commands/Inputs/main.c
+++ b/lldb/test/Shell/Commands/Inputs/main.c
@@ -1,3 +1,2 @@
int foo() { return 0; }
-int foo_bar() { return 0; }
int main() { return foo(); }
diff --git a/lldb/test/Shell/Commands/command-image-lookup-color.test b/lldb/test/Shell/Commands/command-image-lookup-color.test
index 6908100df56ddf3..a58f05680762b67 100644
--- a/lldb/test/Shell/Commands/command-image-lookup-color.test
+++ b/lldb/test/Shell/Commands/command-image-lookup-color.test
@@ -4,7 +4,7 @@
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s ma' | FileCheck %s --check-prefix CHECK1
# CHECK1: Name: {{.+}}31mma{{.+}}0min.c
-# CHECK1: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:3
+# CHECK1: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2
# Checking complex regex searches
@@ -14,7 +14,7 @@
# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s m[abc]' | FileCheck %s --check-prefix CHECK3
# CHECK3: Name: {{.+}}31mma{{.+}}0min.c
-# CHECK3: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:3
+# CHECK3: Summary: {{.+}}`{{.+}}31mma{{.+}}0min at main.c:2
# Checking tail match
@@ -28,11 +28,12 @@
# Checking search which find more than 1 symbol
-# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s foo' | FileCheck %s --check-prefix CHECK6
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s foo|main' | FileCheck %s --check-prefix CHECK6
+# CHECK6: Name: {{.+}}31mmain{{.+}}0m.c
# CHECK6: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m at main.c:1
-# CHECK6: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m_bar at main.c:2
+# CHECK6: Summary: {{.+}}`{{.+}}31mmain{{.+}}0m at main.c:2
# Checking multiple matches on same symbol
-# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s foo|bar' | FileCheck %s --check-prefix CHECK7
-# CHECK7: Summary: {{.+}}`{{.+}}31mfoo{{.+}}0m_{{.+}}31mbar{{.+}}0m at main.c:2
\ No newline at end of file
+# RUN: %lldb %t -b -o 'settings set use-color true' -o 'image lookup -r -s (ma|n$)' | FileCheck %s --check-prefix CHECK7
+# CHECK7: Summary: {{.+}}`{{.+}}31mma{{.+}}0mi{{.+}}31mn{{.+}}0m at main.c:2
\ No newline at end of file
>From 0d82955551403a29105a4e3656848f8a5502219f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20L=2E=20Junior?= <josejunior at 10xengineers.ai>
Date: Thu, 16 Nov 2023 17:48:46 -0300
Subject: [PATCH 5/7] update PutCStringColorHighlighted docstrings and minor
changes
---
lldb/include/lldb/Utility/Stream.h | 10 +++++++---
lldb/source/Utility/Stream.cpp | 4 ++--
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/lldb/include/lldb/Utility/Stream.h b/lldb/include/lldb/Utility/Stream.h
index a4897c70a83d6b8..8e3fd48dfe70579 100644
--- a/lldb/include/lldb/Utility/Stream.h
+++ b/lldb/include/lldb/Utility/Stream.h
@@ -233,14 +233,18 @@ class Stream {
/// Output a C string to the stream with color highlighting.
///
- /// Print a C string \a text to the stream, applying color highlighting to
- /// the specified \a pattern within the string.
+ /// Print a C string \a text to the stream, applying red color highlighting to
+ /// the portions of the string that match the regex pattern \a pattern. The
+ /// pattern is matched as many times as possible throughout the string. If \a
+ /// pattern is nullptr, then no highlighting is applied.
///
/// \param[in] text
/// The string to be output to the stream.
///
/// \param[in] pattern
- /// The portion of the \a text string to be colorized for highlighting.
+ /// The regex pattern to match against the \a text string. Portions of \a
+ /// text matching this pattern will be colorized. If this parameter is
+ /// nullptr, highlighting is not performed.
void PutCStringColorHighlighted(llvm::StringRef text, const char *pattern);
/// Output and End of Line character to the stream.
diff --git a/lldb/source/Utility/Stream.cpp b/lldb/source/Utility/Stream.cpp
index 9e2d9737601f7f5..04679d9b017b117 100644
--- a/lldb/source/Utility/Stream.cpp
+++ b/lldb/source/Utility/Stream.cpp
@@ -75,7 +75,7 @@ size_t Stream::PutCString(llvm::StringRef str) {
void Stream::PutCStringColorHighlighted(llvm::StringRef text,
const char *pattern) {
if (!pattern) {
- PutCString(text.data());
+ PutCString(text);
return;
}
@@ -95,7 +95,7 @@ void Stream::PutCStringColorHighlighted(llvm::StringRef text,
remaining = remaining.drop_front(last_pos);
}
if (remaining.size())
- PutCString(remaining.data());
+ PutCString(remaining);
}
// Print a double quoted NULL terminated C string to the stream using the
>From 4a47c6a9185ce157e40f79a7adf9ca0b95af7c22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20L=2E=20Junior?= <josejunior at 10xengineers.ai>
Date: Thu, 16 Nov 2023 17:48:46 -0300
Subject: [PATCH 6/7] update PutCStringColorHighlighted docstrings and minor
changes
---
lldb/source/Utility/Stream.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/lldb/source/Utility/Stream.cpp b/lldb/source/Utility/Stream.cpp
index 04679d9b017b117..ade280207ba938d 100644
--- a/lldb/source/Utility/Stream.cpp
+++ b/lldb/source/Utility/Stream.cpp
@@ -74,6 +74,7 @@ size_t Stream::PutCString(llvm::StringRef str) {
void Stream::PutCStringColorHighlighted(llvm::StringRef text,
const char *pattern) {
+ // Dump function
if (!pattern) {
PutCString(text);
return;
>From 44c9d9796759c741f43652a5b76922a483112285 Mon Sep 17 00:00:00 2001
From: taalhaataahir0102 <23100293 at lums.edu.pk>
Date: Mon, 20 Nov 2023 12:06:37 +0500
Subject: [PATCH 7/7] minor changes
---
lldb/source/Utility/Stream.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/lldb/source/Utility/Stream.cpp b/lldb/source/Utility/Stream.cpp
index ade280207ba938d..04679d9b017b117 100644
--- a/lldb/source/Utility/Stream.cpp
+++ b/lldb/source/Utility/Stream.cpp
@@ -74,7 +74,6 @@ size_t Stream::PutCString(llvm::StringRef str) {
void Stream::PutCStringColorHighlighted(llvm::StringRef text,
const char *pattern) {
- // Dump function
if (!pattern) {
PutCString(text);
return;
More information about the lldb-commits
mailing list