blob: b284ff1dbaaa346310a49d78fc8fc33af075dee8 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SourceManager.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/SourceManager.h"
11
Zachary Turner2f3df612017-04-06 21:28:29 +000012#include "lldb/Core/Address.h" // for Address
13#include "lldb/Core/AddressRange.h" // for AddressRange
Jim Inghame37d6052011-09-13 00:29:56 +000014#include "lldb/Core/Debugger.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000015#include "lldb/Core/FormatEntity.h" // for FormatEntity
Greg Clayton1f746072012-08-29 21:13:06 +000016#include "lldb/Core/Module.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000017#include "lldb/Core/ModuleList.h" // for ModuleList
Pavel Labath1408bf72016-11-01 16:11:14 +000018#include "lldb/Host/FileSystem.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Symbol/CompileUnit.h"
20#include "lldb/Symbol/Function.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000021#include "lldb/Symbol/LineEntry.h" // for LineEntry
Greg Clayton176761e2011-04-19 04:19:37 +000022#include "lldb/Symbol/SymbolContext.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000023#include "lldb/Target/PathMappingList.h" // for PathMappingList
Greg Clayton7e14f912011-04-23 02:04:55 +000024#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000025#include "lldb/Utility/ConstString.h" // for ConstString
Zachary Turner666cc0b2017-03-04 01:30:05 +000026#include "lldb/Utility/DataBuffer.h"
Zachary Turner7f6a7a32017-03-06 23:42:14 +000027#include "lldb/Utility/DataBufferLLVM.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000028#include "lldb/Utility/RegularExpression.h"
29#include "lldb/Utility/Stream.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000030#include "lldb/lldb-enumerations.h" // for StopShowColumn::eStopSho...
31
32#include "llvm/ADT/Twine.h" // for Twine
33
34#include <memory>
35#include <utility> // for pair
36
37#include <assert.h> // for assert
38#include <stdio.h> // for size_t, NULL, snprintf
39
40namespace lldb_private {
41class ExecutionContext;
42}
43namespace lldb_private {
44class ValueObject;
45}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Greg Clayton9585fbf2013-03-19 00:20:55 +000047using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048using namespace lldb_private;
49
Kate Stoneb9c1b512016-09-06 20:57:50 +000050static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
52//----------------------------------------------------------------------
53// SourceManager constructor
54//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000055SourceManager::SourceManager(const TargetSP &target_sp)
56 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
57 m_target_wp(target_sp),
58 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
Jim Inghame37d6052011-09-13 00:29:56 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060SourceManager::SourceManager(const DebuggerSP &debugger_sp)
61 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
62 m_target_wp(), m_debugger_wp(debugger_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
64//----------------------------------------------------------------------
65// Destructor
66//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000067SourceManager::~SourceManager() {}
68
69SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
70 bool same_as_previous =
71 m_last_file_sp && m_last_file_sp->FileSpecMatches(file_spec);
72
73 DebuggerSP debugger_sp(m_debugger_wp.lock());
74 FileSP file_sp;
75 if (same_as_previous)
76 file_sp = m_last_file_sp;
77 else if (debugger_sp)
78 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
79
80 TargetSP target_sp(m_target_wp.lock());
81
82 // It the target source path map has been updated, get this file again so we
83 // can successfully remap the source file
84 if (target_sp && file_sp &&
85 file_sp->GetSourceMapModificationID() !=
86 target_sp->GetSourcePathMap().GetModificationID())
87 file_sp.reset();
88
89 // Update the file contents if needed if we found a file
90 if (file_sp)
91 file_sp->UpdateIfNeeded();
92
93 // If file_sp is no good or it points to a non-existent file, reset it.
94 if (!file_sp || !file_sp->GetFileSpec().Exists()) {
Todd Fiala9666ba72016-09-21 20:13:14 +000095 if (target_sp)
Zachary Turner2f3df612017-04-06 21:28:29 +000096 file_sp = std::make_shared<File>(file_spec, target_sp.get());
Todd Fiala9666ba72016-09-21 20:13:14 +000097 else
Zachary Turner2f3df612017-04-06 21:28:29 +000098 file_sp = std::make_shared<File>(file_spec, debugger_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +000099
100 if (debugger_sp)
101 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
102 }
103 return file_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104}
105
Todd Fiala9666ba72016-09-21 20:13:14 +0000106static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
107 // We don't use ANSI stop column formatting if we can't lookup values from
108 // the debugger.
109 if (!debugger_sp)
110 return false;
111
112 // We don't use ANSI stop column formatting if the debugger doesn't think
113 // it should be using color.
114 if (!debugger_sp->GetUseColor())
115 return false;
116
117 // We only use ANSI stop column formatting if we're either supposed to show
118 // ANSI where available (which we know we have when we get to this point), or
119 // if we're only supposed to use ANSI.
120 const auto value = debugger_sp->GetStopShowColumn();
121 return ((value == eStopShowColumnAnsiOrCaret) ||
122 (value == eStopShowColumnAnsi));
123}
124
125static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
126 // We don't use text-based stop column formatting if we can't lookup values
127 // from the debugger.
128 if (!debugger_sp)
129 return false;
130
131 // If we're asked to show the first available of ANSI or caret, then
132 // we do show the caret when ANSI is not available.
133 const auto value = debugger_sp->GetStopShowColumn();
134 if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
135 return true;
136
137 // The only other time we use caret is if we're explicitly asked to show
138 // caret.
139 return value == eStopShowColumnCaret;
140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000143 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 const char *current_line_cstr, Stream *s,
145 const SymbolContextList *bp_locs) {
146 if (count == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 size_t return_value = 0;
149 if (start_line == 0) {
150 if (m_last_line != 0 && m_last_line != UINT32_MAX)
151 start_line = m_last_line + m_last_count;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000152 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 start_line = 1;
154 }
155
156 if (!m_default_set) {
157 FileSpec tmp_spec;
158 uint32_t tmp_line;
159 GetDefaultFileAndLine(tmp_spec, tmp_line);
160 }
161
162 m_last_line = start_line;
163 m_last_count = count;
164
165 if (m_last_file_sp.get()) {
166 const uint32_t end_line = start_line + count - 1;
167 for (uint32_t line = start_line; line <= end_line; ++line) {
168 if (!m_last_file_sp->LineIsValid(line)) {
169 m_last_line = UINT32_MAX;
170 break;
171 }
172
173 char prefix[32] = "";
174 if (bp_locs) {
175 uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
176
177 if (bp_count > 0)
178 ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
179 else
180 ::snprintf(prefix, sizeof(prefix), " ");
181 }
182
183 return_value +=
184 s->Printf("%s%2.2s %-4u\t", prefix,
185 line == curr_line ? current_line_cstr : "", line);
Todd Fiala9666ba72016-09-21 20:13:14 +0000186 size_t this_line_size = m_last_file_sp->DisplaySourceLines(
187 line, line == curr_line ? column : 0, 0, 0, s);
188 if (column != 0 && line == curr_line &&
189 should_show_stop_column_with_caret(m_debugger_wp.lock())) {
190 // Display caret cursor.
191 std::string src_line;
192 m_last_file_sp->GetLine(line, src_line);
193 return_value += s->Printf(" \t");
194 // Insert a space for every non-tab character in the source line.
Ed Maste05092032016-09-21 22:36:51 +0000195 for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
Todd Fiala9666ba72016-09-21 20:13:14 +0000196 return_value += s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
197 // Now add the caret.
198 return_value += s->Printf("^\n");
199 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 if (this_line_size == 0) {
201 m_last_line = UINT32_MAX;
202 break;
203 } else
204 return_value += this_line_size;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 }
207 return return_value;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000208}
209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210size_t SourceManager::DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +0000211 const FileSpec &file_spec, uint32_t line, uint32_t column,
212 uint32_t context_before, uint32_t context_after,
213 const char *current_line_cstr, Stream *s,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 const SymbolContextList *bp_locs) {
215 FileSP file_sp(GetFile(file_spec));
Greg Clayton9585fbf2013-03-19 00:20:55 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 uint32_t start_line;
218 uint32_t count = context_before + context_after + 1;
219 if (line > context_before)
220 start_line = line - context_before;
221 else
222 start_line = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 if (m_last_file_sp.get() != file_sp.get()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 if (line == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 m_last_line = 0;
227 m_last_file_sp = file_sp;
228 }
229 return DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000230 start_line, count, line, column, current_line_cstr, s, bp_locs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233size_t SourceManager::DisplayMoreWithLineNumbers(
234 Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
235 // If we get called before anybody has set a default file and line, then try
236 // to figure it out here.
237 const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
238 if (!m_default_set) {
239 FileSpec tmp_spec;
240 uint32_t tmp_line;
241 GetDefaultFileAndLine(tmp_spec, tmp_line);
242 }
Greg Clayton44d93782014-01-27 23:43:24 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (m_last_file_sp) {
245 if (m_last_line == UINT32_MAX)
246 return 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 if (reverse && m_last_line == 1)
249 return 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 if (count > 0)
252 m_last_count = count;
253 else if (m_last_count == 0)
254 m_last_count = 10;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 if (m_last_line > 0) {
257 if (reverse) {
258 // If this is the first time we've done a reverse, then back up one more
259 // time so we end
260 // up showing the chunk before the last one we've shown:
261 if (m_last_line > m_last_count)
262 m_last_line -= m_last_count;
Jim Inghame37d6052011-09-13 00:29:56 +0000263 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 m_last_line = 1;
265 } else if (have_default_file_line)
266 m_last_line += m_last_count;
267 } else
268 m_last_line = 1;
269
Todd Fiala9666ba72016-09-21 20:13:14 +0000270 const uint32_t column = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 return DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000272 m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 }
274 return 0;
Jim Inghame37d6052011-09-13 00:29:56 +0000275}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
278 uint32_t line) {
279 FileSP old_file_sp = m_last_file_sp;
280 m_last_file_sp = GetFile(file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 m_default_set = true;
283 if (m_last_file_sp) {
284 m_last_line = line;
Jim Ingham969795f2011-09-21 01:17:13 +0000285 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 } else {
287 m_last_file_sp = old_file_sp;
288 return false;
289 }
Jim Ingham969795f2011-09-21 01:17:13 +0000290}
291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
293 if (m_last_file_sp) {
294 file_spec = m_last_file_sp->GetFileSpec();
295 line = m_last_line;
296 return true;
297 } else if (!m_default_set) {
298 TargetSP target_sp(m_target_wp.lock());
299
300 if (target_sp) {
301 // If nobody has set the default file and line then try here. If there's
302 // no executable, then we
303 // will try again later when there is one. Otherwise, if we can't find it
304 // we won't look again,
305 // somebody will have to set it (for instance when we stop somewhere...)
306 Module *executable_ptr = target_sp->GetExecutableModulePointer();
307 if (executable_ptr) {
308 SymbolContextList sc_list;
309 ConstString main_name("main");
310 bool symbols_okay = false; // Force it to be a debug symbol.
311 bool inlines_okay = true;
312 bool append = false;
313 size_t num_matches = executable_ptr->FindFunctions(
314 main_name, NULL, lldb::eFunctionNameTypeBase, inlines_okay,
315 symbols_okay, append, sc_list);
316 for (size_t idx = 0; idx < num_matches; idx++) {
317 SymbolContext sc;
318 sc_list.GetContextAtIndex(idx, sc);
319 if (sc.function) {
320 lldb_private::LineEntry line_entry;
321 if (sc.function->GetAddressRange()
322 .GetBaseAddress()
323 .CalculateSymbolContextLineEntry(line_entry)) {
324 SetDefaultFileAndLine(line_entry.file, line_entry.line);
325 file_spec = m_last_file_sp->GetFileSpec();
326 line = m_last_line;
327 return true;
328 }
329 }
330 }
331 }
Jim Inghame37d6052011-09-13 00:29:56 +0000332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 }
334 return false;
Jim Inghame37d6052011-09-13 00:29:56 +0000335}
336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
338 RegularExpression &regex,
339 uint32_t start_line,
340 uint32_t end_line,
341 std::vector<uint32_t> &match_lines) {
342 match_lines.clear();
343 FileSP file_sp = GetFile(file_spec);
344 if (!file_sp)
345 return;
346 return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
347 match_lines);
Jim Inghame37d6052011-09-13 00:29:56 +0000348}
349
Todd Fiala9666ba72016-09-21 20:13:14 +0000350SourceManager::File::File(const FileSpec &file_spec,
351 lldb::DebuggerSP debugger_sp)
352 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
Pavel Labath1408bf72016-11-01 16:11:14 +0000353 m_mod_time(FileSystem::GetModificationTime(file_spec)),
354 m_debugger_wp(debugger_sp) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000355 CommonInitializer(file_spec, nullptr);
356}
357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358SourceManager::File::File(const FileSpec &file_spec, Target *target)
359 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
Pavel Labath1408bf72016-11-01 16:11:14 +0000360 m_mod_time(FileSystem::GetModificationTime(file_spec)),
Todd Fiala9666ba72016-09-21 20:13:14 +0000361 m_debugger_wp(target ? target->GetDebugger().shared_from_this()
362 : DebuggerSP()) {
363 CommonInitializer(file_spec, target);
364}
365
Todd Fiala9666ba72016-09-21 20:13:14 +0000366void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
367 Target *target) {
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000368 if (m_mod_time == llvm::sys::TimePoint<>()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 if (target) {
370 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
371
372 if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
373 // If this is just a file name, lets see if we can find it in the
374 // target:
375 bool check_inlines = false;
376 SymbolContextList sc_list;
377 size_t num_matches =
378 target->GetImages().ResolveSymbolContextForFilePath(
379 file_spec.GetFilename().AsCString(), 0, check_inlines,
380 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
381 sc_list);
382 bool got_multiple = false;
383 if (num_matches != 0) {
384 if (num_matches > 1) {
385 SymbolContext sc;
386 FileSpec *test_cu_spec = NULL;
387
388 for (unsigned i = 0; i < num_matches; i++) {
389 sc_list.GetContextAtIndex(i, sc);
390 if (sc.comp_unit) {
391 if (test_cu_spec) {
392 if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit))
393 got_multiple = true;
394 break;
395 } else
396 test_cu_spec = sc.comp_unit;
397 }
398 }
399 }
400 if (!got_multiple) {
401 SymbolContext sc;
402 sc_list.GetContextAtIndex(0, sc);
403 m_file_spec = sc.comp_unit;
Pavel Labath1408bf72016-11-01 16:11:14 +0000404 m_mod_time = FileSystem::GetModificationTime(m_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 }
406 }
407 }
408 // Try remapping if m_file_spec does not correspond to an existing file.
409 if (!m_file_spec.Exists()) {
410 FileSpec new_file_spec;
411 // Check target specific source remappings first, then fall back to
412 // modules objects can have individual path remappings that were
413 // detected
414 // when the debug info for a module was found.
415 // then
416 if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
417 target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
418 m_file_spec = new_file_spec;
Pavel Labath1408bf72016-11-01 16:11:14 +0000419 m_mod_time = FileSystem::GetModificationTime(m_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 }
421 }
422 }
423 }
424
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000425 if (m_mod_time != llvm::sys::TimePoint<>())
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000426 m_data_sp = DataBufferLLVM::CreateFromPath(m_file_spec.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427}
428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
430 if (line == 0)
431 return UINT32_MAX;
432
433 if (line == 1)
434 return 0;
435
436 if (CalculateLineOffsets(line)) {
437 if (line < m_offsets.size())
438 return m_offsets[line - 1]; // yes we want "line - 1" in the index
439 }
440 return UINT32_MAX;
441}
442
443uint32_t SourceManager::File::GetNumLines() {
444 CalculateLineOffsets();
445 return m_offsets.size();
446}
447
448const char *SourceManager::File::PeekLineData(uint32_t line) {
449 if (!LineIsValid(line))
450 return NULL;
451
452 size_t line_offset = GetLineOffset(line);
453 if (line_offset < m_data_sp->GetByteSize())
454 return (const char *)m_data_sp->GetBytes() + line_offset;
455 return NULL;
456}
457
458uint32_t SourceManager::File::GetLineLength(uint32_t line,
459 bool include_newline_chars) {
460 if (!LineIsValid(line))
461 return false;
462
463 size_t start_offset = GetLineOffset(line);
464 size_t end_offset = GetLineOffset(line + 1);
465 if (end_offset == UINT32_MAX)
466 end_offset = m_data_sp->GetByteSize();
467
468 if (end_offset > start_offset) {
469 uint32_t length = end_offset - start_offset;
470 if (include_newline_chars == false) {
471 const char *line_start =
472 (const char *)m_data_sp->GetBytes() + start_offset;
473 while (length > 0) {
474 const char last_char = line_start[length - 1];
475 if ((last_char == '\r') || (last_char == '\n'))
476 --length;
477 else
478 break;
479 }
480 }
481 return length;
482 }
483 return 0;
484}
485
486bool SourceManager::File::LineIsValid(uint32_t line) {
487 if (line == 0)
488 return false;
489
490 if (CalculateLineOffsets(line))
491 return line < m_offsets.size();
492 return false;
493}
494
495void SourceManager::File::UpdateIfNeeded() {
496 // TODO: use host API to sign up for file modifications to anything in our
497 // source cache and only update when we determine a file has been updated.
498 // For now we check each time we want to display info for the file.
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000499 auto curr_mod_time = FileSystem::GetModificationTime(m_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000501 if (curr_mod_time != llvm::sys::TimePoint<>() &&
502 m_mod_time != curr_mod_time) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 m_mod_time = curr_mod_time;
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000504 m_data_sp = DataBufferLLVM::CreateFromPath(m_file_spec.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 m_offsets.clear();
506 }
507}
508
Todd Fiala9666ba72016-09-21 20:13:14 +0000509size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 uint32_t context_before,
511 uint32_t context_after,
512 Stream *s) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000513 // Nothing to write if there's no stream.
514 if (!s)
515 return 0;
516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 // Sanity check m_data_sp before proceeding.
518 if (!m_data_sp)
519 return 0;
520
521 const uint32_t start_line =
522 line <= context_before ? 1 : line - context_before;
523 const uint32_t start_line_offset = GetLineOffset(start_line);
524 if (start_line_offset != UINT32_MAX) {
525 const uint32_t end_line = line + context_after;
526 uint32_t end_line_offset = GetLineOffset(end_line + 1);
527 if (end_line_offset == UINT32_MAX)
528 end_line_offset = m_data_sp->GetByteSize();
529
530 assert(start_line_offset <= end_line_offset);
531 size_t bytes_written = 0;
532 if (start_line_offset < end_line_offset) {
533 size_t count = end_line_offset - start_line_offset;
534 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
Todd Fiala9666ba72016-09-21 20:13:14 +0000535
536 bool displayed_line = false;
537
538 if (column && (column < count)) {
539 auto debugger_sp = m_debugger_wp.lock();
540 if (should_show_stop_column_with_ansi(debugger_sp) && debugger_sp) {
541 // Check if we have any ANSI codes with which to mark this column.
542 // If not, no need to do this work.
543 auto ansi_prefix_entry = debugger_sp->GetStopShowColumnAnsiPrefix();
544 auto ansi_suffix_entry = debugger_sp->GetStopShowColumnAnsiSuffix();
545
546 // We only bother breaking up the line to format the marked column if
547 // there is any marking specified on both sides of the marked column.
548 // In ANSI-terminal-sequence land, there must be a post if there is a
549 // pre format, and vice versa.
550 if (ansi_prefix_entry && ansi_suffix_entry) {
551 // Mark the current column with the desired escape sequence for
552 // formatting the column (e.g. underline, inverse, etc.)
553
554 // First print the part before the column to mark.
555 bytes_written = s->Write(cstr, column - 1);
556
557 // Write the pre escape sequence.
558 const SymbolContext *sc = nullptr;
559 const ExecutionContext *exe_ctx = nullptr;
560 const Address addr = LLDB_INVALID_ADDRESS;
561 ValueObject *valobj = nullptr;
562 const bool function_changed = false;
563 const bool initial_function = false;
564
565 FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
566 valobj, function_changed, initial_function);
567
568 // Write the marked column.
569 bytes_written += s->Write(cstr + column - 1, 1);
570
571 // Write the post escape sequence.
572 FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
573 valobj, function_changed, initial_function);
574
575 // And finish up with the rest of the line.
576 bytes_written += s->Write(cstr + column, count - column);
577
578 // Keep track of the fact that we just wrote the line.
579 displayed_line = true;
580 }
581 }
582 }
583
584 // If we didn't end up displaying the line with ANSI codes for whatever
585 // reason, display it now sans codes.
586 if (!displayed_line)
587 bytes_written = s->Write(cstr, count);
588
589 // Ensure we get an end of line character one way or another.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 if (!is_newline_char(cstr[count - 1]))
591 bytes_written += s->EOL();
592 }
593 return bytes_written;
594 }
595 return 0;
596}
597
598void SourceManager::File::FindLinesMatchingRegex(
599 RegularExpression &regex, uint32_t start_line, uint32_t end_line,
600 std::vector<uint32_t> &match_lines) {
601 match_lines.clear();
602
603 if (!LineIsValid(start_line) ||
604 (end_line != UINT32_MAX && !LineIsValid(end_line)))
605 return;
606 if (start_line > end_line)
607 return;
608
609 for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
610 std::string buffer;
611 if (!GetLine(line_no, buffer))
612 break;
Zachary Turner95eae422016-09-21 16:01:28 +0000613 if (regex.Execute(buffer)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 match_lines.push_back(line_no);
615 }
616 }
617}
618
619bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) {
620 return FileSpec::Equal(m_file_spec, file_spec, false);
621}
622
623bool lldb_private::operator==(const SourceManager::File &lhs,
624 const SourceManager::File &rhs) {
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000625 if (lhs.m_file_spec != rhs.m_file_spec)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 return false;
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000627 return lhs.m_mod_time == rhs.m_mod_time;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628}
629
630bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
631 line =
632 UINT32_MAX; // TODO: take this line out when we support partial indexing
633 if (line == UINT32_MAX) {
634 // Already done?
635 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
636 return true;
637
638 if (m_offsets.empty()) {
639 if (m_data_sp.get() == NULL)
640 return false;
641
642 const char *start = (char *)m_data_sp->GetBytes();
643 if (start) {
644 const char *end = start + m_data_sp->GetByteSize();
645
646 // Calculate all line offsets from scratch
647
648 // Push a 1 at index zero to indicate the file has been completely
649 // indexed.
650 m_offsets.push_back(UINT32_MAX);
651 const char *s;
652 for (s = start; s < end; ++s) {
653 char curr_ch = *s;
654 if (is_newline_char(curr_ch)) {
655 if (s + 1 < end) {
656 char next_ch = s[1];
657 if (is_newline_char(next_ch)) {
658 if (curr_ch != next_ch)
659 ++s;
660 }
661 }
662 m_offsets.push_back(s + 1 - start);
663 }
664 }
665 if (!m_offsets.empty()) {
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000666 if (m_offsets.back() < size_t(end - start))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 m_offsets.push_back(end - start);
668 }
669 return true;
670 }
671 } else {
672 // Some lines have been populated, start where we last left off
673 assert("Not implemented yet" && false);
674 }
675
676 } else {
677 // Calculate all line offsets up to "line"
678 assert("Not implemented yet" && false);
679 }
680 return false;
681}
682
683bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
684 if (!LineIsValid(line_no))
685 return false;
686
687 size_t start_offset = GetLineOffset(line_no);
688 size_t end_offset = GetLineOffset(line_no + 1);
689 if (end_offset == UINT32_MAX) {
690 end_offset = m_data_sp->GetByteSize();
691 }
692 buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
693 end_offset - start_offset);
694
695 return true;
696}
697
698void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
699 FileSpec file_spec;
700 FileCache::iterator pos = m_file_cache.find(file_spec);
701 if (pos == m_file_cache.end())
702 m_file_cache[file_spec] = file_sp;
703 else {
704 if (file_sp != pos->second)
705 m_file_cache[file_spec] = file_sp;
706 }
707}
708
709SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
710 const FileSpec &file_spec) const {
711 FileSP file_sp;
712 FileCache::const_iterator pos = m_file_cache.find(file_spec);
713 if (pos != m_file_cache.end())
714 file_sp = pos->second;
715 return file_sp;
716}