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