Skip to content

Commit 841be98

Browse files
committed
[lldb] Color the line marker
Highlight the color marker similar to what we do for the column marker. The default color matches the color of the current PC marker (->) in the default disassembly format. Differential revision: https://reviews.llvm.org/D75070
1 parent a5fa778 commit 841be98

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
287287

288288
uint32_t GetDisassemblyLineCount() const;
289289

290+
llvm::StringRef GetStopShowLineMarkerAnsiPrefix() const;
291+
292+
llvm::StringRef GetStopShowLineMarkerAnsiSuffix() const;
293+
290294
bool GetAutoOneLineSummaries() const;
291295

292296
bool GetAutoIndent() const;

lldb/source/Core/CoreProperties.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ let Definition = "debugger" in {
7575
Global,
7676
DefaultStringValue<"${ansi.normal}">,
7777
Desc<"When displaying the column marker in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format immediately after the column to be marked.">;
78+
def StopShowLineMarkerAnsiPrefix: Property<"stop-show-line-ansi-prefix", "String">,
79+
Global,
80+
DefaultStringValue<"${ansi.fg.yellow}">,
81+
Desc<"When displaying the line marker in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format at the immediately before the line to be marked.">;
82+
def StopShowLineMarkerAnsiSuffix: Property<"stop-show-line-ansi-suffix", "String">,
83+
Global,
84+
DefaultStringValue<"${ansi.normal}">,
85+
Desc<"When displaying the line marker in a color-enabled (i.e. ANSI) terminal, use the ANSI terminal code specified in this format immediately after the line to be marked.">;
7886
def TerminalWidth: Property<"term-width", "SInt64">,
7987
Global,
8088
DefaultUnsignedValue<80>,

lldb/source/Core/Debugger.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@ llvm::StringRef Debugger::GetStopShowColumnAnsiSuffix() const {
360360
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
361361
}
362362

363+
llvm::StringRef Debugger::GetStopShowLineMarkerAnsiPrefix() const {
364+
const uint32_t idx = ePropertyStopShowLineMarkerAnsiPrefix;
365+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
366+
}
367+
368+
llvm::StringRef Debugger::GetStopShowLineMarkerAnsiSuffix() const {
369+
const uint32_t idx = ePropertyStopShowLineMarkerAnsiSuffix;
370+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
371+
}
372+
363373
uint32_t Debugger::GetStopSourceLineCount(bool before) const {
364374
const uint32_t idx =
365375
before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;

lldb/source/Core/SourceManager.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "lldb/Symbol/SymbolContext.h"
2323
#include "lldb/Target/PathMappingList.h"
2424
#include "lldb/Target/Target.h"
25+
#include "lldb/Utility/AnsiTerminal.h"
2526
#include "lldb/Utility/ConstString.h"
2627
#include "lldb/Utility/DataBuffer.h"
2728
#include "lldb/Utility/DataBufferLLVM.h"
@@ -148,6 +149,10 @@ static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
148149
return value == eStopShowColumnCaret;
149150
}
150151

152+
static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) {
153+
return debugger_sp && debugger_sp->GetUseColor();
154+
}
155+
151156
size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
152157
uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
153158
const char *current_line_cstr, Stream *s,
@@ -191,8 +196,20 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
191196
::snprintf(prefix, sizeof(prefix), " ");
192197
}
193198

194-
s->Printf("%s%2.2s %-4u\t", prefix,
195-
line == curr_line ? current_line_cstr : "", line);
199+
char buffer[3];
200+
sprintf(buffer, "%2.2s", (line == curr_line) ? current_line_cstr : "");
201+
std::string current_line_highlight(buffer);
202+
203+
auto debugger_sp = m_debugger_wp.lock();
204+
if (should_show_stop_line_with_ansi(debugger_sp)) {
205+
current_line_highlight = ansi::FormatAnsiTerminalCodes(
206+
(debugger_sp->GetStopShowLineMarkerAnsiPrefix() +
207+
current_line_highlight +
208+
debugger_sp->GetStopShowLineMarkerAnsiSuffix())
209+
.str());
210+
}
211+
212+
s->Printf("%s%s %-4u\t", prefix, current_line_highlight.c_str(), line);
196213

197214
// So far we treated column 0 as a special 'no column value', but
198215
// DisplaySourceLines starts counting columns from 0 (and no column is
@@ -204,7 +221,7 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
204221
size_t this_line_size =
205222
m_last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
206223
if (column != 0 && line == curr_line &&
207-
should_show_stop_column_with_caret(m_debugger_wp.lock())) {
224+
should_show_stop_column_with_caret(debugger_sp)) {
208225
// Display caret cursor.
209226
std::string src_line;
210227
m_last_file_sp->GetLine(line, src_line);

lldb/test/API/source-manager/TestSourceManager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def do_display_source_python_api(self, use_color, needle_regex, highlight_source
9393
# 6 }
9494
self.expect(stream.GetData(), "Source code displayed correctly:\n" + stream.GetData(),
9595
exe=False,
96-
patterns=['=> %d.*Hello world' % self.line,
96+
patterns=['=>', '%d.*Hello world' % self.line,
9797
needle_regex])
9898

9999
# Boundary condition testings for SBStream(). LLDB should not crash!
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# RUN: %clang_host -g -O0 %S/Inputs/main.c -o %t.out
2+
# RUN: %lldb -x -b -s %s %t.out | FileCheck %s
3+
settings set use-color true
4+
settings set stop-show-line-ansi-prefix "prefix"
5+
settings set stop-show-line-ansi-suffix "suffix"
6+
b foo
7+
run
8+
c
9+
settings set stop-show-line-ansi-prefix ${ansi.fg.green}
10+
settings set stop-show-line-ansi-suffix ${ansi.normal}
11+
run
12+
q
13+
14+
# Check the ASCII escape code
15+
# CHECK-NOT: prefix->suffix
16+
# CHECK: ->
17+
# CHECK: {{.*}}->{{.*}}

0 commit comments

Comments
 (0)