[Lldb-commits] [lldb] [lldb] colorize symbols in image lookup with a regex pattern (PR #69422)

José Lira Junior via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 4 15:53:30 PST 2023


================
@@ -70,6 +72,31 @@ size_t Stream::PutCString(llvm::StringRef str) {
   return bytes_written;
 }
 
+void Stream::PutCStringColorHighlighted(llvm::StringRef text,
+                                        const char *pattern) {
+  if (!pattern) {
+    PutCString(text);
+    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}");
----------------
junior-jl wrote:

I'm not sure if I got this suggestion right. What I did was:

1. Defined these properties in `lldb/source/Core/CoreProperties.td`:

```
def ShowRegexMatchAnsiPrefix: Property<"show-regex-match-ansi-prefix", "String">,
    Global,
    DefaultStringValue<"${ansi.fg.red}">,
    Desc<"When displaying a regex match in a color-enabled terminal, use the ANSI terminal code specified in this format immediately before the match.">;
def ShowRegexMatchAnsiSuffix: Property<"show-regex-match-ansi-suffix", "String">,
    Global,
    DefaultStringValue<"${ansi.normal}">,
    Desc<"When displaying a regex match in a color-enabled terminal, use the ANSI terminal code specified in this format immediately after the match.">;
```

2. Declared the getter functions in `Debugger.h` and defined them in `Debugger.cpp` following the pattern:

```cpp
llvm::StringRef Debugger::GetRegexMatchAnsiPrefix() const {
  const uint32_t idx = ePropertyShowRegexMatchAnsiPrefix;
  return GetPropertyAtIndexAs<llvm::StringRef>(
      idx, g_debugger_properties[idx].default_cstr_value);
}

llvm::StringRef Debugger::GetRegexMatchAnsiSuffix() const {
  const uint32_t idx = ePropertyShowRegexMatchAnsiSuffix;
  return GetPropertyAtIndexAs<llvm::StringRef>(
      idx, g_debugger_properties[idx].default_cstr_value);
}

```

3. Added two parameters to `Stream::PutCStringColorHighlighted`: `prefix` and `suffix`.
4. In `Address.cpp` and `Symbol.cpp`, there was a `Target` object that I used to get the debugger and its properties. In `CommandObjectTarget.cpp`, there was an `CommandInterpreter` object that was used for the same. But in `SymbolContext.cpp`, there was a `TargetSP` object and I tried using it to get the debugger properties, I got a segmentation fault, that's why I put the following two lines hard coded (while we don't solve this):

```
llvm::StringRef ansi_prefix = "${ansi.fg.red}";
llvm::StringRef ansi_suffix = "${ansi.normal}";
```

Please let me know if there's a better way to do this or if I completely misunderstood the suggestion.

https://github.com/llvm/llvm-project/pull/69422


More information about the lldb-commits mailing list