Skip to content

Commit 865996d

Browse files
emrekultursaylabath
authored andcommitted
[lldb] Remove m_last_file_sp from SourceManager
Summary: ...and replace it with m_last_file_spec instead. When Source Cache is enabled, the value stored in m_last_file_sp is already in the Source Cache, and caching it again in SourceManager brings no extra benefit. All we need is to "remember" the last used file, and FileSpec can serve the same purpose. When Source Cache is disabled, the user explicitly requested no caching of source files, and therefore, m_last_file_sp should NOT be used. Bug: llvm.org/PR45310 Depends on D76805. Reviewers: labath, jingham Reviewed By: jingham Subscribers: labath, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D76806
1 parent 1f820fa commit 865996d

File tree

5 files changed

+721
-28
lines changed

5 files changed

+721
-28
lines changed

lldb/include/lldb/Core/SourceManager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class SourceManager {
119119

120120
~SourceManager();
121121

122-
FileSP GetLastFile() { return m_last_file_sp; }
122+
FileSP GetLastFile() { return GetFile(m_last_file_spec); }
123123

124124
size_t
125125
DisplaySourceLinesWithLineNumbers(const FileSpec &file, uint32_t line,
@@ -141,7 +141,9 @@ class SourceManager {
141141

142142
bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
143143

144-
bool DefaultFileAndLineSet() { return (m_last_file_sp.get() != nullptr); }
144+
bool DefaultFileAndLineSet() {
145+
return (GetFile(m_last_file_spec).get() != nullptr);
146+
}
145147

146148
void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression &regex,
147149
uint32_t start_line, uint32_t end_line,
@@ -150,7 +152,7 @@ class SourceManager {
150152
FileSP GetFile(const FileSpec &file_spec);
151153

152154
protected:
153-
FileSP m_last_file_sp;
155+
FileSpec m_last_file_spec;
154156
uint32_t m_last_line;
155157
uint32_t m_last_count;
156158
bool m_default_set;

lldb/source/Core/SourceManager.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,24 @@ static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
5252

5353
// SourceManager constructor
5454
SourceManager::SourceManager(const TargetSP &target_sp)
55-
: m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
55+
: m_last_line(0), m_last_count(0), m_default_set(false),
5656
m_target_wp(target_sp),
5757
m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
5858

5959
SourceManager::SourceManager(const DebuggerSP &debugger_sp)
60-
: m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
61-
m_target_wp(), m_debugger_wp(debugger_sp) {}
60+
: m_last_line(0), m_last_count(0), m_default_set(false), m_target_wp(),
61+
m_debugger_wp(debugger_sp) {}
6262

6363
// Destructor
6464
SourceManager::~SourceManager() {}
6565

6666
SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
67-
bool same_as_previous =
68-
m_last_file_sp &&
69-
FileSpec::Match(file_spec, m_last_file_sp->GetFileSpec());
67+
if (!file_spec)
68+
return nullptr;
7069

7170
DebuggerSP debugger_sp(m_debugger_wp.lock());
7271
FileSP file_sp;
73-
if (same_as_previous)
74-
file_sp = m_last_file_sp;
75-
else if (debugger_sp && debugger_sp->GetUseSourceCache())
72+
if (debugger_sp && debugger_sp->GetUseSourceCache())
7673
file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
7774

7875
TargetSP target_sp(m_target_wp.lock());
@@ -178,10 +175,10 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
178175
m_last_line = start_line;
179176
m_last_count = count;
180177

181-
if (m_last_file_sp.get()) {
178+
if (FileSP last_file_sp = GetLastFile()) {
182179
const uint32_t end_line = start_line + count - 1;
183180
for (uint32_t line = start_line; line <= end_line; ++line) {
184-
if (!m_last_file_sp->LineIsValid(line)) {
181+
if (!last_file_sp->LineIsValid(line)) {
185182
m_last_line = UINT32_MAX;
186183
break;
187184
}
@@ -219,12 +216,12 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
219216
columnToHighlight = column - 1;
220217

221218
size_t this_line_size =
222-
m_last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
219+
last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
223220
if (column != 0 && line == curr_line &&
224221
should_show_stop_column_with_caret(debugger_sp)) {
225222
// Display caret cursor.
226223
std::string src_line;
227-
m_last_file_sp->GetLine(line, src_line);
224+
last_file_sp->GetLine(line, src_line);
228225
s->Printf(" \t");
229226
// Insert a space for every non-tab character in the source line.
230227
for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
@@ -255,10 +252,11 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbers(
255252
else
256253
start_line = 1;
257254

258-
if (m_last_file_sp.get() != file_sp.get()) {
255+
FileSP last_file_sp(GetLastFile());
256+
if (last_file_sp.get() != file_sp.get()) {
259257
if (line == 0)
260258
m_last_line = 0;
261-
m_last_file_sp = file_sp;
259+
m_last_file_spec = file_spec;
262260
}
263261
return DisplaySourceLinesWithLineNumbersUsingLastFile(
264262
start_line, count, line, column, current_line_cstr, s, bp_locs);
@@ -268,14 +266,15 @@ size_t SourceManager::DisplayMoreWithLineNumbers(
268266
Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
269267
// If we get called before anybody has set a default file and line, then try
270268
// to figure it out here.
271-
const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
269+
FileSP last_file_sp(GetLastFile());
270+
const bool have_default_file_line = last_file_sp && m_last_line > 0;
272271
if (!m_default_set) {
273272
FileSpec tmp_spec;
274273
uint32_t tmp_line;
275274
GetDefaultFileAndLine(tmp_spec, tmp_line);
276275
}
277276

278-
if (m_last_file_sp) {
277+
if (last_file_sp) {
279278
if (m_last_line == UINT32_MAX)
280279
return 0;
281280

@@ -310,22 +309,21 @@ size_t SourceManager::DisplayMoreWithLineNumbers(
310309

311310
bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
312311
uint32_t line) {
313-
FileSP old_file_sp = m_last_file_sp;
314-
m_last_file_sp = GetFile(file_spec);
315-
316312
m_default_set = true;
317-
if (m_last_file_sp) {
313+
FileSP file_sp(GetFile(file_spec));
314+
315+
if (file_sp) {
318316
m_last_line = line;
317+
m_last_file_spec = file_spec;
319318
return true;
320319
} else {
321-
m_last_file_sp = old_file_sp;
322320
return false;
323321
}
324322
}
325323

326324
bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
327-
if (m_last_file_sp) {
328-
file_spec = m_last_file_sp->GetFileSpec();
325+
if (FileSP last_file_sp = GetLastFile()) {
326+
file_spec = m_last_file_spec;
329327
line = m_last_line;
330328
return true;
331329
} else if (!m_default_set) {
@@ -355,7 +353,7 @@ bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
355353
.GetBaseAddress()
356354
.CalculateSymbolContextLineEntry(line_entry)) {
357355
SetDefaultFileAndLine(line_entry.file, line_entry.line);
358-
file_spec = m_last_file_sp->GetFileSpec();
356+
file_spec = m_last_file_spec;
359357
line = m_last_line;
360358
return true;
361359
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CXX_SOURCES := main-copy.cpp
2+
3+
include Makefile.rules
4+
5+
6+
# Copy file into the build folder to enable the test to modify it.
7+
main-copy.cpp: main.cpp
8+
cp -f $< $@
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Tests large source files are not locked on Windows when source cache is disabled
3+
"""
4+
5+
import lldb
6+
import os
7+
from lldbsuite.test.decorators import *
8+
from lldbsuite.test.lldbtest import *
9+
from lldbsuite.test import lldbutil
10+
from shutil import copy
11+
12+
class SettingsUseSourceCacheTestCase(TestBase):
13+
14+
mydir = TestBase.compute_mydir(__file__)
15+
NO_DEBUG_INFO_TESTCASE = True
16+
17+
def test_set_use_source_cache_false(self):
18+
"""Test that after 'set use-source-cache false', files are not locked."""
19+
self.set_use_source_cache_and_test(False)
20+
21+
@skipIf(hostoslist=no_match(["windows"]))
22+
def test_set_use_source_cache_true(self):
23+
"""Test that after 'set use-source-cache false', files are locked."""
24+
self.set_use_source_cache_and_test(True)
25+
26+
def set_use_source_cache_and_test(self, is_cache_enabled):
27+
"""Common test for both True/False values of use-source-cache."""
28+
self.build()
29+
30+
# Enable/Disable source cache
31+
self.runCmd(
32+
"settings set use-source-cache " +
33+
("true" if is_cache_enabled else "false"))
34+
35+
# Get paths for the main source file.
36+
src = self.getBuildArtifact("main-copy.cpp")
37+
self.assertTrue(src)
38+
39+
# Make sure source file is bigger than 16K to trigger memory mapping
40+
self.assertGreater(os.stat(src).st_size, 4*4096)
41+
42+
target, process, thread, breakpoint = lldbutil.run_to_name_breakpoint(
43+
self,
44+
"calc")
45+
46+
# Show the source file contents to make sure LLDB loads src file.
47+
self.runCmd("source list")
48+
49+
# Try deleting the source file.
50+
is_file_removed = self.removeFile(src)
51+
52+
if is_cache_enabled:
53+
self.assertFalse(
54+
is_file_removed,
55+
"Source cache is enabled, but delete file succeeded")
56+
57+
if not is_cache_enabled:
58+
self.assertTrue(
59+
is_file_removed,
60+
"Source cache is disabled, but delete file failed")
61+
62+
def removeFile(self, src):
63+
"""Remove file and return true iff file was successfully removed."""
64+
try:
65+
os.remove(src)
66+
return True
67+
except Exception:
68+
return False
69+

0 commit comments

Comments
 (0)