1 //===-- SourceManager.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Core/SourceManager.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/DataBuffer.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Symbol/ClangNamespaceDecl.h"
23 #include "lldb/Symbol/CompileUnit.h"
24 #include "lldb/Symbol/Function.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Target/Target.h"
29 using namespace lldb_private;
32 static inline bool is_newline_char(char ch)
34 return ch == '\n' || ch == '\r';
38 //----------------------------------------------------------------------
39 // SourceManager constructor
40 //----------------------------------------------------------------------
41 SourceManager::SourceManager(const TargetSP &target_sp) :
46 m_target_wp (target_sp),
47 m_debugger_wp(target_sp->GetDebugger().shared_from_this())
51 SourceManager::SourceManager(const DebuggerSP &debugger_sp) :
57 m_debugger_wp (debugger_sp)
61 //----------------------------------------------------------------------
63 //----------------------------------------------------------------------
64 SourceManager::~SourceManager()
69 SourceManager::GetFile (const FileSpec &file_spec)
71 bool same_as_previous = m_last_file_sp && m_last_file_sp->FileSpecMatches (file_spec);
73 DebuggerSP debugger_sp (m_debugger_wp.lock());
76 file_sp = m_last_file_sp;
78 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile (file_spec);
80 TargetSP target_sp (m_target_wp.lock());
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 && file_sp->GetSourceMapModificationID() != target_sp->GetSourcePathMap().GetModificationID())
87 // If file_sp is no good or it points to a non-existent file, reset it.
88 if (!file_sp || !file_sp->GetFileSpec().Exists())
90 file_sp.reset (new File (file_spec, target_sp.get()));
93 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
99 SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile (uint32_t start_line,
102 const char* current_line_cstr,
104 const SymbolContextList *bp_locs)
108 size_t return_value = 0;
111 if (m_last_line != 0 && m_last_line != UINT32_MAX)
112 start_line = m_last_line + m_last_count;
121 GetDefaultFileAndLine(tmp_spec, tmp_line);
124 m_last_line = start_line;
125 m_last_count = count;
127 if (m_last_file_sp.get())
129 const uint32_t end_line = start_line + count - 1;
130 for (uint32_t line = start_line; line <= end_line; ++line)
132 if (!m_last_file_sp->LineIsValid (line))
134 m_last_line = UINT32_MAX;
138 char prefix[32] = "";
141 uint32_t bp_count = bp_locs->NumLineEntriesWithLine (line);
144 ::snprintf (prefix, sizeof (prefix), "[%u] ", bp_count);
146 ::snprintf (prefix, sizeof (prefix), " ");
149 return_value += s->Printf("%s%2.2s %-4u\t",
151 line == curr_line ? current_line_cstr : "",
153 size_t this_line_size = m_last_file_sp->DisplaySourceLines (line, 0, 0, s);
154 if (this_line_size == 0)
156 m_last_line = UINT32_MAX;
160 return_value += this_line_size;
167 SourceManager::DisplaySourceLinesWithLineNumbers
169 const FileSpec &file_spec,
171 uint32_t context_before,
172 uint32_t context_after,
173 const char* current_line_cstr,
175 const SymbolContextList *bp_locs
178 FileSP file_sp (GetFile (file_spec));
181 uint32_t count = context_before + context_after + 1;
182 if (line > context_before)
183 start_line = line - context_before;
187 if (m_last_file_sp.get() != file_sp.get())
191 m_last_file_sp = file_sp;
193 return DisplaySourceLinesWithLineNumbersUsingLastFile (start_line, count, line, current_line_cstr, s, bp_locs);
197 SourceManager::DisplayMoreWithLineNumbers (Stream *s,
200 const SymbolContextList *bp_locs)
202 // If we get called before anybody has set a default file and line, then try to figure it out here.
203 const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
208 GetDefaultFileAndLine(tmp_spec, tmp_line);
213 if (m_last_line == UINT32_MAX)
216 if (reverse && m_last_line == 1)
220 m_last_count = count;
221 else if (m_last_count == 0)
228 // If this is the first time we've done a reverse, then back up one more time so we end
229 // up showing the chunk before the last one we've shown:
230 if (m_last_line > m_last_count)
231 m_last_line -= m_last_count;
235 else if (have_default_file_line)
236 m_last_line += m_last_count;
241 return DisplaySourceLinesWithLineNumbersUsingLastFile (m_last_line, m_last_count, UINT32_MAX, "", s, bp_locs);
247 SourceManager::SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line)
249 FileSP old_file_sp = m_last_file_sp;
250 m_last_file_sp = GetFile (file_spec);
252 m_default_set = true;
260 m_last_file_sp = old_file_sp;
266 SourceManager::GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line)
270 file_spec = m_last_file_sp->GetFileSpec();
274 else if (!m_default_set)
276 TargetSP target_sp (m_target_wp.lock());
280 // If nobody has set the default file and line then try here. If there's no executable, then we
281 // will try again later when there is one. Otherwise, if we can't find it we won't look again,
282 // somebody will have to set it (for instance when we stop somewhere...)
283 Module *executable_ptr = target_sp->GetExecutableModulePointer();
286 SymbolContextList sc_list;
287 ConstString main_name("main");
288 bool symbols_okay = false; // Force it to be a debug symbol.
289 bool inlines_okay = true;
291 size_t num_matches = executable_ptr->FindFunctions (main_name,
293 lldb::eFunctionNameTypeBase,
298 for (size_t idx = 0; idx < num_matches; idx++)
301 sc_list.GetContextAtIndex(idx, sc);
304 lldb_private::LineEntry line_entry;
305 if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry))
307 SetDefaultFileAndLine (line_entry.file,
309 file_spec = m_last_file_sp->GetFileSpec();
322 SourceManager::FindLinesMatchingRegex (FileSpec &file_spec,
323 RegularExpression& regex,
326 std::vector<uint32_t> &match_lines)
329 FileSP file_sp = GetFile (file_spec);
332 return file_sp->FindLinesMatchingRegex (regex, start_line, end_line, match_lines);
335 SourceManager::File::File(const FileSpec &file_spec, Target *target) :
336 m_file_spec_orig (file_spec),
337 m_file_spec(file_spec),
338 m_mod_time (file_spec.GetModificationTime()),
339 m_source_map_mod_id (0),
343 if (!m_mod_time.IsValid())
347 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
349 if (!file_spec.GetDirectory() && file_spec.GetFilename())
351 // If this is just a file name, lets see if we can find it in the target:
352 bool check_inlines = false;
353 SymbolContextList sc_list;
354 size_t num_matches = target->GetImages().ResolveSymbolContextForFilePath (file_spec.GetFilename().AsCString(),
357 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
359 bool got_multiple = false;
360 if (num_matches != 0)
365 FileSpec *test_cu_spec = NULL;
367 for (unsigned i = 0; i < num_matches; i++)
369 sc_list.GetContextAtIndex(i, sc);
374 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
379 test_cu_spec = sc.comp_unit;
386 sc_list.GetContextAtIndex (0, sc);
387 m_file_spec = sc.comp_unit;
388 m_mod_time = m_file_spec.GetModificationTime();
392 // Try remapping if m_file_spec does not correspond to an existing file.
393 if (!m_file_spec.Exists())
395 FileSpec new_file_spec;
396 // Check target specific source remappings first, then fall back to
397 // modules objects can have individual path remappings that were detected
398 // when the debug info for a module was found.
400 if (target->GetSourcePathMap().FindFile (m_file_spec, new_file_spec) ||
401 target->GetImages().FindSourceFile (m_file_spec, new_file_spec))
403 m_file_spec = new_file_spec;
404 m_mod_time = m_file_spec.GetModificationTime();
410 if (m_mod_time.IsValid())
411 m_data_sp = m_file_spec.ReadFileContents ();
414 SourceManager::File::~File()
419 SourceManager::File::GetLineOffset (uint32_t line)
427 if (CalculateLineOffsets (line))
429 if (line < m_offsets.size())
430 return m_offsets[line - 1]; // yes we want "line - 1" in the index
436 SourceManager::File::GetNumLines ()
438 CalculateLineOffsets();
439 return m_offsets.size();
443 SourceManager::File::PeekLineData (uint32_t line)
445 if (!LineIsValid(line))
448 size_t line_offset = GetLineOffset (line);
449 if (line_offset < m_data_sp->GetByteSize())
450 return (const char *)m_data_sp->GetBytes() + line_offset;
455 SourceManager::File::GetLineLength (uint32_t line, bool include_newline_chars)
457 if (!LineIsValid(line))
460 size_t start_offset = GetLineOffset (line);
461 size_t end_offset = GetLineOffset (line + 1);
462 if (end_offset == UINT32_MAX)
463 end_offset = m_data_sp->GetByteSize();
465 if (end_offset > start_offset)
467 uint32_t length = end_offset - start_offset;
468 if (include_newline_chars == false)
470 const char *line_start = (const char *)m_data_sp->GetBytes() + start_offset;
473 const char last_char = line_start[length-1];
474 if ((last_char == '\r') || (last_char == '\n'))
486 SourceManager::File::LineIsValid (uint32_t line)
491 if (CalculateLineOffsets (line))
492 return line < m_offsets.size();
497 SourceManager::File::DisplaySourceLines (uint32_t line, uint32_t context_before, uint32_t context_after, Stream *s)
499 // TODO: use host API to sign up for file modifications to anything in our
500 // source cache and only update when we determine a file has been updated.
501 // For now we check each time we want to display info for the file.
502 TimeValue curr_mod_time (m_file_spec.GetModificationTime());
504 if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time)
506 m_mod_time = curr_mod_time;
507 m_data_sp = m_file_spec.ReadFileContents ();
511 // Sanity check m_data_sp before proceeding.
515 const uint32_t start_line = line <= context_before ? 1 : line - context_before;
516 const uint32_t start_line_offset = GetLineOffset (start_line);
517 if (start_line_offset != UINT32_MAX)
519 const uint32_t end_line = line + context_after;
520 uint32_t end_line_offset = GetLineOffset (end_line + 1);
521 if (end_line_offset == UINT32_MAX)
522 end_line_offset = m_data_sp->GetByteSize();
524 assert (start_line_offset <= end_line_offset);
525 size_t bytes_written = 0;
526 if (start_line_offset < end_line_offset)
528 size_t count = end_line_offset - start_line_offset;
529 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
530 bytes_written = s->Write(cstr, count);
531 if (!is_newline_char(cstr[count-1]))
532 bytes_written += s->EOL();
534 return bytes_written;
540 SourceManager::File::FindLinesMatchingRegex (RegularExpression& regex, uint32_t start_line, uint32_t end_line, std::vector<uint32_t> &match_lines)
542 TimeValue curr_mod_time (m_file_spec.GetModificationTime());
543 if (m_mod_time != curr_mod_time)
545 m_mod_time = curr_mod_time;
546 m_data_sp = m_file_spec.ReadFileContents ();
552 if (!LineIsValid(start_line) || (end_line != UINT32_MAX && !LineIsValid(end_line)))
554 if (start_line > end_line)
557 for (uint32_t line_no = start_line; line_no < end_line; line_no++)
560 if (!GetLine (line_no, buffer))
562 if (regex.Execute(buffer.c_str()))
564 match_lines.push_back(line_no);
570 SourceManager::File::FileSpecMatches (const FileSpec &file_spec)
572 return FileSpec::Equal (m_file_spec, file_spec, false);
576 lldb_private::operator== (const SourceManager::File &lhs, const SourceManager::File &rhs)
578 if (lhs.m_file_spec == rhs.m_file_spec)
580 if (lhs.m_mod_time.IsValid())
582 if (rhs.m_mod_time.IsValid())
583 return lhs.m_mod_time == rhs.m_mod_time;
587 else if (rhs.m_mod_time.IsValid())
597 SourceManager::File::CalculateLineOffsets (uint32_t line)
599 line = UINT32_MAX; // TODO: take this line out when we support partial indexing
600 if (line == UINT32_MAX)
603 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
606 if (m_offsets.empty())
608 if (m_data_sp.get() == NULL)
611 const char *start = (char *)m_data_sp->GetBytes();
614 const char *end = start + m_data_sp->GetByteSize();
616 // Calculate all line offsets from scratch
618 // Push a 1 at index zero to indicate the file has been completely indexed.
619 m_offsets.push_back(UINT32_MAX);
621 for (s = start; s < end; ++s)
624 if (is_newline_char (curr_ch))
629 if (is_newline_char (next_ch))
631 if (curr_ch != next_ch)
635 m_offsets.push_back(s + 1 - start);
638 if (!m_offsets.empty())
640 if (m_offsets.back() < end - start)
641 m_offsets.push_back(end - start);
648 // Some lines have been populated, start where we last left off
649 assert("Not implemented yet" == NULL);
655 // Calculate all line offsets up to "line"
656 assert("Not implemented yet" == NULL);
662 SourceManager::File::GetLine (uint32_t line_no, std::string &buffer)
664 if (!LineIsValid(line_no))
667 size_t start_offset = GetLineOffset (line_no);
668 size_t end_offset = GetLineOffset (line_no + 1);
669 if (end_offset == UINT32_MAX)
671 end_offset = m_data_sp->GetByteSize();
673 buffer.assign((char *) m_data_sp->GetBytes() + start_offset, end_offset - start_offset);
679 SourceManager::SourceFileCache::AddSourceFile (const FileSP &file_sp)
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;
687 if (file_sp != pos->second)
688 m_file_cache[file_spec] = file_sp;
692 SourceManager::FileSP
693 SourceManager::SourceFileCache::FindSourceFile (const FileSpec &file_spec) const
696 FileCache::const_iterator pos = m_file_cache.find(file_spec);
697 if (pos != m_file_cache.end())
698 file_sp = pos->second;