blob: 9293d67a115694c03195f5197273eed67516089b [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
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/DataBuffer.h"
Jim Inghame37d6052011-09-13 00:29:56 +000017#include "lldb/Core/Debugger.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000019#include "lldb/Host/FileSystem.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Symbol/CompileUnit.h"
21#include "lldb/Symbol/Function.h"
Greg Clayton176761e2011-04-19 04:19:37 +000022#include "lldb/Symbol/SymbolContext.h"
Greg Clayton7e14f912011-04-23 02:04:55 +000023#include "lldb/Target/Target.h"
Todd Fiala9666ba72016-09-21 20:13:14 +000024#include "lldb/Utility/AnsiTerminal.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/RegularExpression.h"
26#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Greg Clayton9585fbf2013-03-19 00:20:55 +000028using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029using namespace lldb_private;
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33//----------------------------------------------------------------------
34// SourceManager constructor
35//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000036SourceManager::SourceManager(const TargetSP &target_sp)
37 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
38 m_target_wp(target_sp),
39 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
Jim Inghame37d6052011-09-13 00:29:56 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041SourceManager::SourceManager(const DebuggerSP &debugger_sp)
42 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
43 m_target_wp(), m_debugger_wp(debugger_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
45//----------------------------------------------------------------------
46// Destructor
47//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000048SourceManager::~SourceManager() {}
49
50SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
51 bool same_as_previous =
52 m_last_file_sp && m_last_file_sp->FileSpecMatches(file_spec);
53
54 DebuggerSP debugger_sp(m_debugger_wp.lock());
55 FileSP file_sp;
56 if (same_as_previous)
57 file_sp = m_last_file_sp;
58 else if (debugger_sp)
59 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
60
61 TargetSP target_sp(m_target_wp.lock());
62
63 // It the target source path map has been updated, get this file again so we
64 // can successfully remap the source file
65 if (target_sp && file_sp &&
66 file_sp->GetSourceMapModificationID() !=
67 target_sp->GetSourcePathMap().GetModificationID())
68 file_sp.reset();
69
70 // Update the file contents if needed if we found a file
71 if (file_sp)
72 file_sp->UpdateIfNeeded();
73
74 // If file_sp is no good or it points to a non-existent file, reset it.
75 if (!file_sp || !file_sp->GetFileSpec().Exists()) {
Todd Fiala9666ba72016-09-21 20:13:14 +000076 if (target_sp)
77 file_sp.reset(new File(file_spec, target_sp.get()));
78 else
79 file_sp.reset(new File(file_spec, debugger_sp));
Kate Stoneb9c1b512016-09-06 20:57:50 +000080
81 if (debugger_sp)
82 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
83 }
84 return file_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085}
86
Todd Fiala9666ba72016-09-21 20:13:14 +000087static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
88 // We don't use ANSI stop column formatting if we can't lookup values from
89 // the debugger.
90 if (!debugger_sp)
91 return false;
92
93 // We don't use ANSI stop column formatting if the debugger doesn't think
94 // it should be using color.
95 if (!debugger_sp->GetUseColor())
96 return false;
97
98 // We only use ANSI stop column formatting if we're either supposed to show
99 // ANSI where available (which we know we have when we get to this point), or
100 // if we're only supposed to use ANSI.
101 const auto value = debugger_sp->GetStopShowColumn();
102 return ((value == eStopShowColumnAnsiOrCaret) ||
103 (value == eStopShowColumnAnsi));
104}
105
106static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
107 // We don't use text-based stop column formatting if we can't lookup values
108 // from the debugger.
109 if (!debugger_sp)
110 return false;
111
112 // If we're asked to show the first available of ANSI or caret, then
113 // we do show the caret when ANSI is not available.
114 const auto value = debugger_sp->GetStopShowColumn();
115 if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
116 return true;
117
118 // The only other time we use caret is if we're explicitly asked to show
119 // caret.
120 return value == eStopShowColumnCaret;
121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000124 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 const char *current_line_cstr, Stream *s,
126 const SymbolContextList *bp_locs) {
127 if (count == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 size_t return_value = 0;
130 if (start_line == 0) {
131 if (m_last_line != 0 && m_last_line != UINT32_MAX)
132 start_line = m_last_line + m_last_count;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000133 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 start_line = 1;
135 }
136
137 if (!m_default_set) {
138 FileSpec tmp_spec;
139 uint32_t tmp_line;
140 GetDefaultFileAndLine(tmp_spec, tmp_line);
141 }
142
143 m_last_line = start_line;
144 m_last_count = count;
145
146 if (m_last_file_sp.get()) {
147 const uint32_t end_line = start_line + count - 1;
148 for (uint32_t line = start_line; line <= end_line; ++line) {
149 if (!m_last_file_sp->LineIsValid(line)) {
150 m_last_line = UINT32_MAX;
151 break;
152 }
153
154 char prefix[32] = "";
155 if (bp_locs) {
156 uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
157
158 if (bp_count > 0)
159 ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
160 else
161 ::snprintf(prefix, sizeof(prefix), " ");
162 }
163
164 return_value +=
165 s->Printf("%s%2.2s %-4u\t", prefix,
166 line == curr_line ? current_line_cstr : "", line);
Todd Fiala9666ba72016-09-21 20:13:14 +0000167 size_t this_line_size = m_last_file_sp->DisplaySourceLines(
168 line, line == curr_line ? column : 0, 0, 0, s);
169 if (column != 0 && line == curr_line &&
170 should_show_stop_column_with_caret(m_debugger_wp.lock())) {
171 // Display caret cursor.
172 std::string src_line;
173 m_last_file_sp->GetLine(line, src_line);
174 return_value += s->Printf(" \t");
175 // Insert a space for every non-tab character in the source line.
Ed Maste05092032016-09-21 22:36:51 +0000176 for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
Todd Fiala9666ba72016-09-21 20:13:14 +0000177 return_value += s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
178 // Now add the caret.
179 return_value += s->Printf("^\n");
180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 if (this_line_size == 0) {
182 m_last_line = UINT32_MAX;
183 break;
184 } else
185 return_value += this_line_size;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000186 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 }
188 return return_value;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000189}
190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191size_t SourceManager::DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +0000192 const FileSpec &file_spec, uint32_t line, uint32_t column,
193 uint32_t context_before, uint32_t context_after,
194 const char *current_line_cstr, Stream *s,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 const SymbolContextList *bp_locs) {
196 FileSP file_sp(GetFile(file_spec));
Greg Clayton9585fbf2013-03-19 00:20:55 +0000197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 uint32_t start_line;
199 uint32_t count = context_before + context_after + 1;
200 if (line > context_before)
201 start_line = line - context_before;
202 else
203 start_line = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 if (m_last_file_sp.get() != file_sp.get()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 if (line == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 m_last_line = 0;
208 m_last_file_sp = file_sp;
209 }
210 return DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000211 start_line, count, line, column, current_line_cstr, s, bp_locs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212}
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214size_t SourceManager::DisplayMoreWithLineNumbers(
215 Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
216 // If we get called before anybody has set a default file and line, then try
217 // to figure it out here.
218 const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
219 if (!m_default_set) {
220 FileSpec tmp_spec;
221 uint32_t tmp_line;
222 GetDefaultFileAndLine(tmp_spec, tmp_line);
223 }
Greg Clayton44d93782014-01-27 23:43:24 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (m_last_file_sp) {
226 if (m_last_line == UINT32_MAX)
227 return 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 if (reverse && m_last_line == 1)
230 return 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000231
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 if (count > 0)
233 m_last_count = count;
234 else if (m_last_count == 0)
235 m_last_count = 10;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 if (m_last_line > 0) {
238 if (reverse) {
239 // If this is the first time we've done a reverse, then back up one more
240 // time so we end
241 // up showing the chunk before the last one we've shown:
242 if (m_last_line > m_last_count)
243 m_last_line -= m_last_count;
Jim Inghame37d6052011-09-13 00:29:56 +0000244 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 m_last_line = 1;
246 } else if (have_default_file_line)
247 m_last_line += m_last_count;
248 } else
249 m_last_line = 1;
250
Todd Fiala9666ba72016-09-21 20:13:14 +0000251 const uint32_t column = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 return DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000253 m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 }
255 return 0;
Jim Inghame37d6052011-09-13 00:29:56 +0000256}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
259 uint32_t line) {
260 FileSP old_file_sp = m_last_file_sp;
261 m_last_file_sp = GetFile(file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 m_default_set = true;
264 if (m_last_file_sp) {
265 m_last_line = line;
Jim Ingham969795f2011-09-21 01:17:13 +0000266 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 } else {
268 m_last_file_sp = old_file_sp;
269 return false;
270 }
Jim Ingham969795f2011-09-21 01:17:13 +0000271}
272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
274 if (m_last_file_sp) {
275 file_spec = m_last_file_sp->GetFileSpec();
276 line = m_last_line;
277 return true;
278 } else if (!m_default_set) {
279 TargetSP target_sp(m_target_wp.lock());
280
281 if (target_sp) {
282 // If nobody has set the default file and line then try here. If there's
283 // no executable, then we
284 // will try again later when there is one. Otherwise, if we can't find it
285 // we won't look again,
286 // somebody will have to set it (for instance when we stop somewhere...)
287 Module *executable_ptr = target_sp->GetExecutableModulePointer();
288 if (executable_ptr) {
289 SymbolContextList sc_list;
290 ConstString main_name("main");
291 bool symbols_okay = false; // Force it to be a debug symbol.
292 bool inlines_okay = true;
293 bool append = false;
294 size_t num_matches = executable_ptr->FindFunctions(
295 main_name, NULL, lldb::eFunctionNameTypeBase, inlines_okay,
296 symbols_okay, append, sc_list);
297 for (size_t idx = 0; idx < num_matches; idx++) {
298 SymbolContext sc;
299 sc_list.GetContextAtIndex(idx, sc);
300 if (sc.function) {
301 lldb_private::LineEntry line_entry;
302 if (sc.function->GetAddressRange()
303 .GetBaseAddress()
304 .CalculateSymbolContextLineEntry(line_entry)) {
305 SetDefaultFileAndLine(line_entry.file, line_entry.line);
306 file_spec = m_last_file_sp->GetFileSpec();
307 line = m_last_line;
308 return true;
309 }
310 }
311 }
312 }
Jim Inghame37d6052011-09-13 00:29:56 +0000313 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 }
315 return false;
Jim Inghame37d6052011-09-13 00:29:56 +0000316}
317
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
319 RegularExpression &regex,
320 uint32_t start_line,
321 uint32_t end_line,
322 std::vector<uint32_t> &match_lines) {
323 match_lines.clear();
324 FileSP file_sp = GetFile(file_spec);
325 if (!file_sp)
326 return;
327 return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
328 match_lines);
Jim Inghame37d6052011-09-13 00:29:56 +0000329}
330
Todd Fiala9666ba72016-09-21 20:13:14 +0000331SourceManager::File::File(const FileSpec &file_spec,
332 lldb::DebuggerSP debugger_sp)
333 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
Pavel Labath1408bf72016-11-01 16:11:14 +0000334 m_mod_time(FileSystem::GetModificationTime(file_spec)),
335 m_debugger_wp(debugger_sp) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000336 CommonInitializer(file_spec, nullptr);
337}
338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339SourceManager::File::File(const FileSpec &file_spec, Target *target)
340 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
Pavel Labath1408bf72016-11-01 16:11:14 +0000341 m_mod_time(FileSystem::GetModificationTime(file_spec)),
Todd Fiala9666ba72016-09-21 20:13:14 +0000342 m_debugger_wp(target ? target->GetDebugger().shared_from_this()
343 : DebuggerSP()) {
344 CommonInitializer(file_spec, target);
345}
346
Todd Fiala9666ba72016-09-21 20:13:14 +0000347void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
348 Target *target) {
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000349 if (m_mod_time == llvm::sys::TimePoint<>()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 if (target) {
351 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
352
353 if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
354 // If this is just a file name, lets see if we can find it in the
355 // target:
356 bool check_inlines = false;
357 SymbolContextList sc_list;
358 size_t num_matches =
359 target->GetImages().ResolveSymbolContextForFilePath(
360 file_spec.GetFilename().AsCString(), 0, check_inlines,
361 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
362 sc_list);
363 bool got_multiple = false;
364 if (num_matches != 0) {
365 if (num_matches > 1) {
366 SymbolContext sc;
367 FileSpec *test_cu_spec = NULL;
368
369 for (unsigned i = 0; i < num_matches; i++) {
370 sc_list.GetContextAtIndex(i, sc);
371 if (sc.comp_unit) {
372 if (test_cu_spec) {
373 if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit))
374 got_multiple = true;
375 break;
376 } else
377 test_cu_spec = sc.comp_unit;
378 }
379 }
380 }
381 if (!got_multiple) {
382 SymbolContext sc;
383 sc_list.GetContextAtIndex(0, sc);
384 m_file_spec = sc.comp_unit;
Pavel Labath1408bf72016-11-01 16:11:14 +0000385 m_mod_time = FileSystem::GetModificationTime(m_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 }
387 }
388 }
389 // Try remapping if m_file_spec does not correspond to an existing file.
390 if (!m_file_spec.Exists()) {
391 FileSpec new_file_spec;
392 // Check target specific source remappings first, then fall back to
393 // modules objects can have individual path remappings that were
394 // detected
395 // when the debug info for a module was found.
396 // then
397 if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
398 target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
399 m_file_spec = new_file_spec;
Pavel Labath1408bf72016-11-01 16:11:14 +0000400 m_mod_time = FileSystem::GetModificationTime(m_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 }
402 }
403 }
404 }
405
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000406 if (m_mod_time != llvm::sys::TimePoint<>())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 m_data_sp = m_file_spec.ReadFileContents();
408}
409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
411 if (line == 0)
412 return UINT32_MAX;
413
414 if (line == 1)
415 return 0;
416
417 if (CalculateLineOffsets(line)) {
418 if (line < m_offsets.size())
419 return m_offsets[line - 1]; // yes we want "line - 1" in the index
420 }
421 return UINT32_MAX;
422}
423
424uint32_t SourceManager::File::GetNumLines() {
425 CalculateLineOffsets();
426 return m_offsets.size();
427}
428
429const char *SourceManager::File::PeekLineData(uint32_t line) {
430 if (!LineIsValid(line))
431 return NULL;
432
433 size_t line_offset = GetLineOffset(line);
434 if (line_offset < m_data_sp->GetByteSize())
435 return (const char *)m_data_sp->GetBytes() + line_offset;
436 return NULL;
437}
438
439uint32_t SourceManager::File::GetLineLength(uint32_t line,
440 bool include_newline_chars) {
441 if (!LineIsValid(line))
442 return false;
443
444 size_t start_offset = GetLineOffset(line);
445 size_t end_offset = GetLineOffset(line + 1);
446 if (end_offset == UINT32_MAX)
447 end_offset = m_data_sp->GetByteSize();
448
449 if (end_offset > start_offset) {
450 uint32_t length = end_offset - start_offset;
451 if (include_newline_chars == false) {
452 const char *line_start =
453 (const char *)m_data_sp->GetBytes() + start_offset;
454 while (length > 0) {
455 const char last_char = line_start[length - 1];
456 if ((last_char == '\r') || (last_char == '\n'))
457 --length;
458 else
459 break;
460 }
461 }
462 return length;
463 }
464 return 0;
465}
466
467bool SourceManager::File::LineIsValid(uint32_t line) {
468 if (line == 0)
469 return false;
470
471 if (CalculateLineOffsets(line))
472 return line < m_offsets.size();
473 return false;
474}
475
476void SourceManager::File::UpdateIfNeeded() {
477 // TODO: use host API to sign up for file modifications to anything in our
478 // source cache and only update when we determine a file has been updated.
479 // For now we check each time we want to display info for the file.
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000480 auto curr_mod_time = FileSystem::GetModificationTime(m_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000482 if (curr_mod_time != llvm::sys::TimePoint<>() &&
483 m_mod_time != curr_mod_time) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 m_mod_time = curr_mod_time;
485 m_data_sp = m_file_spec.ReadFileContents();
486 m_offsets.clear();
487 }
488}
489
Todd Fiala9666ba72016-09-21 20:13:14 +0000490size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 uint32_t context_before,
492 uint32_t context_after,
493 Stream *s) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000494 // Nothing to write if there's no stream.
495 if (!s)
496 return 0;
497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 // Sanity check m_data_sp before proceeding.
499 if (!m_data_sp)
500 return 0;
501
502 const uint32_t start_line =
503 line <= context_before ? 1 : line - context_before;
504 const uint32_t start_line_offset = GetLineOffset(start_line);
505 if (start_line_offset != UINT32_MAX) {
506 const uint32_t end_line = line + context_after;
507 uint32_t end_line_offset = GetLineOffset(end_line + 1);
508 if (end_line_offset == UINT32_MAX)
509 end_line_offset = m_data_sp->GetByteSize();
510
511 assert(start_line_offset <= end_line_offset);
512 size_t bytes_written = 0;
513 if (start_line_offset < end_line_offset) {
514 size_t count = end_line_offset - start_line_offset;
515 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
Todd Fiala9666ba72016-09-21 20:13:14 +0000516
517 bool displayed_line = false;
518
519 if (column && (column < count)) {
520 auto debugger_sp = m_debugger_wp.lock();
521 if (should_show_stop_column_with_ansi(debugger_sp) && debugger_sp) {
522 // Check if we have any ANSI codes with which to mark this column.
523 // If not, no need to do this work.
524 auto ansi_prefix_entry = debugger_sp->GetStopShowColumnAnsiPrefix();
525 auto ansi_suffix_entry = debugger_sp->GetStopShowColumnAnsiSuffix();
526
527 // We only bother breaking up the line to format the marked column if
528 // there is any marking specified on both sides of the marked column.
529 // In ANSI-terminal-sequence land, there must be a post if there is a
530 // pre format, and vice versa.
531 if (ansi_prefix_entry && ansi_suffix_entry) {
532 // Mark the current column with the desired escape sequence for
533 // formatting the column (e.g. underline, inverse, etc.)
534
535 // First print the part before the column to mark.
536 bytes_written = s->Write(cstr, column - 1);
537
538 // Write the pre escape sequence.
539 const SymbolContext *sc = nullptr;
540 const ExecutionContext *exe_ctx = nullptr;
541 const Address addr = LLDB_INVALID_ADDRESS;
542 ValueObject *valobj = nullptr;
543 const bool function_changed = false;
544 const bool initial_function = false;
545
546 FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
547 valobj, function_changed, initial_function);
548
549 // Write the marked column.
550 bytes_written += s->Write(cstr + column - 1, 1);
551
552 // Write the post escape sequence.
553 FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
554 valobj, function_changed, initial_function);
555
556 // And finish up with the rest of the line.
557 bytes_written += s->Write(cstr + column, count - column);
558
559 // Keep track of the fact that we just wrote the line.
560 displayed_line = true;
561 }
562 }
563 }
564
565 // If we didn't end up displaying the line with ANSI codes for whatever
566 // reason, display it now sans codes.
567 if (!displayed_line)
568 bytes_written = s->Write(cstr, count);
569
570 // Ensure we get an end of line character one way or another.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (!is_newline_char(cstr[count - 1]))
572 bytes_written += s->EOL();
573 }
574 return bytes_written;
575 }
576 return 0;
577}
578
579void SourceManager::File::FindLinesMatchingRegex(
580 RegularExpression &regex, uint32_t start_line, uint32_t end_line,
581 std::vector<uint32_t> &match_lines) {
582 match_lines.clear();
583
584 if (!LineIsValid(start_line) ||
585 (end_line != UINT32_MAX && !LineIsValid(end_line)))
586 return;
587 if (start_line > end_line)
588 return;
589
590 for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
591 std::string buffer;
592 if (!GetLine(line_no, buffer))
593 break;
Zachary Turner95eae422016-09-21 16:01:28 +0000594 if (regex.Execute(buffer)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 match_lines.push_back(line_no);
596 }
597 }
598}
599
600bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) {
601 return FileSpec::Equal(m_file_spec, file_spec, false);
602}
603
604bool lldb_private::operator==(const SourceManager::File &lhs,
605 const SourceManager::File &rhs) {
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000606 if (lhs.m_file_spec != rhs.m_file_spec)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 return false;
Pavel Labath3dc342eb2016-11-09 14:04:08 +0000608 return lhs.m_mod_time == rhs.m_mod_time;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609}
610
611bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
612 line =
613 UINT32_MAX; // TODO: take this line out when we support partial indexing
614 if (line == UINT32_MAX) {
615 // Already done?
616 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
617 return true;
618
619 if (m_offsets.empty()) {
620 if (m_data_sp.get() == NULL)
621 return false;
622
623 const char *start = (char *)m_data_sp->GetBytes();
624 if (start) {
625 const char *end = start + m_data_sp->GetByteSize();
626
627 // Calculate all line offsets from scratch
628
629 // Push a 1 at index zero to indicate the file has been completely
630 // indexed.
631 m_offsets.push_back(UINT32_MAX);
632 const char *s;
633 for (s = start; s < end; ++s) {
634 char curr_ch = *s;
635 if (is_newline_char(curr_ch)) {
636 if (s + 1 < end) {
637 char next_ch = s[1];
638 if (is_newline_char(next_ch)) {
639 if (curr_ch != next_ch)
640 ++s;
641 }
642 }
643 m_offsets.push_back(s + 1 - start);
644 }
645 }
646 if (!m_offsets.empty()) {
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000647 if (m_offsets.back() < size_t(end - start))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 m_offsets.push_back(end - start);
649 }
650 return true;
651 }
652 } else {
653 // Some lines have been populated, start where we last left off
654 assert("Not implemented yet" && false);
655 }
656
657 } else {
658 // Calculate all line offsets up to "line"
659 assert("Not implemented yet" && false);
660 }
661 return false;
662}
663
664bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
665 if (!LineIsValid(line_no))
666 return false;
667
668 size_t start_offset = GetLineOffset(line_no);
669 size_t end_offset = GetLineOffset(line_no + 1);
670 if (end_offset == UINT32_MAX) {
671 end_offset = m_data_sp->GetByteSize();
672 }
673 buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
674 end_offset - start_offset);
675
676 return true;
677}
678
679void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
680 FileSpec file_spec;
681 FileCache::iterator pos = m_file_cache.find(file_spec);
682 if (pos == m_file_cache.end())
683 m_file_cache[file_spec] = file_sp;
684 else {
685 if (file_sp != pos->second)
686 m_file_cache[file_spec] = file_sp;
687 }
688}
689
690SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
691 const FileSpec &file_spec) const {
692 FileSP file_sp;
693 FileCache::const_iterator pos = m_file_cache.find(file_spec);
694 if (pos != m_file_cache.end())
695 file_sp = pos->second;
696 return file_sp;
697}