blob: 0959cabf86a3f56f5977150776b51c0bcd49e842 [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"
Zachary Turner633a29c2015-03-04 01:58:01 +000019#include "lldb/Core/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Stream.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000021#include "lldb/Host/FileSystem.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Symbol/CompileUnit.h"
23#include "lldb/Symbol/Function.h"
Greg Clayton176761e2011-04-19 04:19:37 +000024#include "lldb/Symbol/SymbolContext.h"
Greg Clayton7e14f912011-04-23 02:04:55 +000025#include "lldb/Target/Target.h"
Todd Fiala9666ba72016-09-21 20:13:14 +000026#include "lldb/Utility/AnsiTerminal.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) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 if (!m_mod_time.IsValid()) {
350 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
406 if (m_mod_time.IsValid())
407 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 Labath1408bf72016-11-01 16:11:14 +0000480 TimeValue curr_mod_time(FileSystem::GetModificationTime(m_file_spec));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481
482 if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time) {
483 m_mod_time = curr_mod_time;
484 m_data_sp = m_file_spec.ReadFileContents();
485 m_offsets.clear();
486 }
487}
488
Todd Fiala9666ba72016-09-21 20:13:14 +0000489size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 uint32_t context_before,
491 uint32_t context_after,
492 Stream *s) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000493 // Nothing to write if there's no stream.
494 if (!s)
495 return 0;
496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 // Sanity check m_data_sp before proceeding.
498 if (!m_data_sp)
499 return 0;
500
501 const uint32_t start_line =
502 line <= context_before ? 1 : line - context_before;
503 const uint32_t start_line_offset = GetLineOffset(start_line);
504 if (start_line_offset != UINT32_MAX) {
505 const uint32_t end_line = line + context_after;
506 uint32_t end_line_offset = GetLineOffset(end_line + 1);
507 if (end_line_offset == UINT32_MAX)
508 end_line_offset = m_data_sp->GetByteSize();
509
510 assert(start_line_offset <= end_line_offset);
511 size_t bytes_written = 0;
512 if (start_line_offset < end_line_offset) {
513 size_t count = end_line_offset - start_line_offset;
514 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
Todd Fiala9666ba72016-09-21 20:13:14 +0000515
516 bool displayed_line = false;
517
518 if (column && (column < count)) {
519 auto debugger_sp = m_debugger_wp.lock();
520 if (should_show_stop_column_with_ansi(debugger_sp) && debugger_sp) {
521 // Check if we have any ANSI codes with which to mark this column.
522 // If not, no need to do this work.
523 auto ansi_prefix_entry = debugger_sp->GetStopShowColumnAnsiPrefix();
524 auto ansi_suffix_entry = debugger_sp->GetStopShowColumnAnsiSuffix();
525
526 // We only bother breaking up the line to format the marked column if
527 // there is any marking specified on both sides of the marked column.
528 // In ANSI-terminal-sequence land, there must be a post if there is a
529 // pre format, and vice versa.
530 if (ansi_prefix_entry && ansi_suffix_entry) {
531 // Mark the current column with the desired escape sequence for
532 // formatting the column (e.g. underline, inverse, etc.)
533
534 // First print the part before the column to mark.
535 bytes_written = s->Write(cstr, column - 1);
536
537 // Write the pre escape sequence.
538 const SymbolContext *sc = nullptr;
539 const ExecutionContext *exe_ctx = nullptr;
540 const Address addr = LLDB_INVALID_ADDRESS;
541 ValueObject *valobj = nullptr;
542 const bool function_changed = false;
543 const bool initial_function = false;
544
545 FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
546 valobj, function_changed, initial_function);
547
548 // Write the marked column.
549 bytes_written += s->Write(cstr + column - 1, 1);
550
551 // Write the post escape sequence.
552 FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
553 valobj, function_changed, initial_function);
554
555 // And finish up with the rest of the line.
556 bytes_written += s->Write(cstr + column, count - column);
557
558 // Keep track of the fact that we just wrote the line.
559 displayed_line = true;
560 }
561 }
562 }
563
564 // If we didn't end up displaying the line with ANSI codes for whatever
565 // reason, display it now sans codes.
566 if (!displayed_line)
567 bytes_written = s->Write(cstr, count);
568
569 // Ensure we get an end of line character one way or another.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 if (!is_newline_char(cstr[count - 1]))
571 bytes_written += s->EOL();
572 }
573 return bytes_written;
574 }
575 return 0;
576}
577
578void SourceManager::File::FindLinesMatchingRegex(
579 RegularExpression &regex, uint32_t start_line, uint32_t end_line,
580 std::vector<uint32_t> &match_lines) {
581 match_lines.clear();
582
583 if (!LineIsValid(start_line) ||
584 (end_line != UINT32_MAX && !LineIsValid(end_line)))
585 return;
586 if (start_line > end_line)
587 return;
588
589 for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
590 std::string buffer;
591 if (!GetLine(line_no, buffer))
592 break;
Zachary Turner95eae422016-09-21 16:01:28 +0000593 if (regex.Execute(buffer)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 match_lines.push_back(line_no);
595 }
596 }
597}
598
599bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) {
600 return FileSpec::Equal(m_file_spec, file_spec, false);
601}
602
603bool lldb_private::operator==(const SourceManager::File &lhs,
604 const SourceManager::File &rhs) {
605 if (lhs.m_file_spec == rhs.m_file_spec) {
606 if (lhs.m_mod_time.IsValid()) {
607 if (rhs.m_mod_time.IsValid())
608 return lhs.m_mod_time == rhs.m_mod_time;
609 else
610 return false;
611 } else if (rhs.m_mod_time.IsValid())
612 return false;
613 else
614 return true;
615 } else
616 return false;
617}
618
619bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
620 line =
621 UINT32_MAX; // TODO: take this line out when we support partial indexing
622 if (line == UINT32_MAX) {
623 // Already done?
624 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
625 return true;
626
627 if (m_offsets.empty()) {
628 if (m_data_sp.get() == NULL)
629 return false;
630
631 const char *start = (char *)m_data_sp->GetBytes();
632 if (start) {
633 const char *end = start + m_data_sp->GetByteSize();
634
635 // Calculate all line offsets from scratch
636
637 // Push a 1 at index zero to indicate the file has been completely
638 // indexed.
639 m_offsets.push_back(UINT32_MAX);
640 const char *s;
641 for (s = start; s < end; ++s) {
642 char curr_ch = *s;
643 if (is_newline_char(curr_ch)) {
644 if (s + 1 < end) {
645 char next_ch = s[1];
646 if (is_newline_char(next_ch)) {
647 if (curr_ch != next_ch)
648 ++s;
649 }
650 }
651 m_offsets.push_back(s + 1 - start);
652 }
653 }
654 if (!m_offsets.empty()) {
Zachary Turner5a8ad4592016-10-05 17:07:34 +0000655 if (m_offsets.back() < size_t(end - start))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 m_offsets.push_back(end - start);
657 }
658 return true;
659 }
660 } else {
661 // Some lines have been populated, start where we last left off
662 assert("Not implemented yet" && false);
663 }
664
665 } else {
666 // Calculate all line offsets up to "line"
667 assert("Not implemented yet" && false);
668 }
669 return false;
670}
671
672bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
673 if (!LineIsValid(line_no))
674 return false;
675
676 size_t start_offset = GetLineOffset(line_no);
677 size_t end_offset = GetLineOffset(line_no + 1);
678 if (end_offset == UINT32_MAX) {
679 end_offset = m_data_sp->GetByteSize();
680 }
681 buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
682 end_offset - start_offset);
683
684 return true;
685}
686
687void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
688 FileSpec file_spec;
689 FileCache::iterator pos = m_file_cache.find(file_spec);
690 if (pos == m_file_cache.end())
691 m_file_cache[file_spec] = file_sp;
692 else {
693 if (file_sp != pos->second)
694 m_file_cache[file_spec] = file_sp;
695 }
696}
697
698SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
699 const FileSpec &file_spec) const {
700 FileSP file_sp;
701 FileCache::const_iterator pos = m_file_cache.find(file_spec);
702 if (pos != m_file_cache.end())
703 file_sp = pos->second;
704 return file_sp;
705}