1 //===-- 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/lldb-python.h"
11 
12 #include "lldb/Core/SourceManager.h"
13 
14 // C Includes
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project 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"
27 
28 using namespace lldb_private;
29 
30 static inline bool is_newline_char(char ch)
31 {
32     return ch == '\n' || ch == '\r';
33 }
34 
35 
36 //----------------------------------------------------------------------
37 // SourceManager constructor
38 //----------------------------------------------------------------------
39 SourceManager::SourceManager(Target &target) :
40     m_last_file_sp (),
41     m_last_file_line (0),
42     m_last_file_context_before (0),
43     m_last_file_context_after (10),
44     m_default_set(false),
45     m_first_reverse(true),
46     m_target (&target),
47     m_debugger(NULL)
48 {
49     m_debugger = &(m_target->GetDebugger());
50 }
51 
52 SourceManager::SourceManager(Debugger &debugger) :
53     m_last_file_sp (),
54     m_last_file_line (0),
55     m_last_file_context_before (0),
56     m_last_file_context_after (10),
57     m_default_set(false),
58     m_first_reverse (true),
59     m_target (NULL),
60     m_debugger (&debugger)
61 {
62 }
63 
64 //----------------------------------------------------------------------
65 // Destructor
66 //----------------------------------------------------------------------
67 SourceManager::~SourceManager()
68 {
69 }
70 
71 SourceManager::FileSP
72 SourceManager::GetFile (const FileSpec &file_spec)
73 {
74     FileSP file_sp;
75     file_sp = m_debugger->GetSourceFileCache().FindSourceFile (file_spec);
76     // If file_sp is no good or it points to a non-existent file, reset it.
77     if (!file_sp || !file_sp->GetFileSpec().Exists())
78     {
79         file_sp.reset (new File (file_spec, m_target));
80 
81         m_debugger->GetSourceFileCache().AddSourceFile(file_sp);
82     }
83     return file_sp;
84 }
85 
86 size_t
87 SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile
88 (
89     uint32_t line,
90     uint32_t context_before,
91     uint32_t context_after,
92     const char* current_line_cstr,
93     Stream *s,
94     const SymbolContextList *bp_locs
95 )
96 {
97     m_first_reverse = true;
98     size_t return_value = 0;
99     if (line == 0)
100     {
101         if (m_last_file_line != 0
102             && m_last_file_line != UINT32_MAX)
103             line = m_last_file_line + context_before;
104         else
105             line = 1;
106     }
107 
108     m_last_file_line = line + context_after + 1;
109     m_last_file_context_before = context_before;
110     m_last_file_context_after = context_after;
111 
112     if (context_before == UINT32_MAX)
113         context_before = 0;
114     if (context_after == UINT32_MAX)
115         context_after = 10;
116 
117     if (m_last_file_sp.get())
118     {
119         const uint32_t start_line = line <= context_before ? 1 : line - context_before;
120         const uint32_t end_line = line + context_after;
121         uint32_t curr_line;
122         for (curr_line = start_line; curr_line <= end_line; ++curr_line)
123         {
124             if (!m_last_file_sp->LineIsValid (curr_line))
125             {
126                 m_last_file_line = UINT32_MAX;
127                 break;
128             }
129 
130             char prefix[32] = "";
131             if (bp_locs)
132             {
133                 uint32_t bp_count = bp_locs->NumLineEntriesWithLine (curr_line);
134 
135                 if (bp_count > 0)
136                     ::snprintf (prefix, sizeof (prefix), "[%u] ", bp_count);
137                 else
138                     ::snprintf (prefix, sizeof (prefix), "    ");
139             }
140 
141             return_value += s->Printf("%s%2.2s %-4u\t",
142                       prefix,
143                       curr_line == line ? current_line_cstr : "",
144                       curr_line);
145             size_t this_line_size = m_last_file_sp->DisplaySourceLines (curr_line, 0, 0, s);
146             if (this_line_size == 0)
147             {
148                 m_last_file_line = UINT32_MAX;
149                 break;
150             }
151             else
152                 return_value += this_line_size;
153         }
154     }
155     return return_value;
156 }
157 
158 size_t
159 SourceManager::DisplaySourceLinesWithLineNumbers
160 (
161     const FileSpec &file_spec,
162     uint32_t line,
163     uint32_t context_before,
164     uint32_t context_after,
165     const char* current_line_cstr,
166     Stream *s,
167     const SymbolContextList *bp_locs
168 )
169 {
170     bool same_as_previous = m_last_file_sp && m_last_file_sp->FileSpecMatches (file_spec);
171 
172     if (!same_as_previous)
173         m_last_file_sp = GetFile (file_spec);
174 
175     if (line == 0)
176     {
177         if (!same_as_previous)
178             m_last_file_line = 0;
179     }
180 
181     return DisplaySourceLinesWithLineNumbersUsingLastFile (line, context_before, context_after, current_line_cstr, s, bp_locs);
182 }
183 
184 size_t
185 SourceManager::DisplayMoreWithLineNumbers (Stream *s, const SymbolContextList *bp_locs, bool reverse)
186 {
187     // If we get called before anybody has set a default file and line, then try to figure it out here.
188     if (!m_default_set)
189     {
190         FileSpec tmp_spec;
191         uint32_t tmp_line;
192         GetDefaultFileAndLine(tmp_spec, tmp_line);
193     }
194 
195     if (m_last_file_sp)
196     {
197         if (m_last_file_line == UINT32_MAX)
198             return 0;
199 
200         if (reverse && m_last_file_line == 1)
201             return 0;
202 
203         uint32_t line;
204         uint32_t new_last_file_line = 0;
205 
206         if (m_last_file_line != 0
207             && m_last_file_line != UINT32_MAX)
208         {
209             if (reverse)
210             {
211                 // If this is the first time we've done a reverse, then back up one more time so we end
212                 // up showing the chunk before the last one we've shown:
213                 if (m_first_reverse)
214                 {
215                     if (m_last_file_line > m_last_file_context_after)
216                         m_last_file_line -= m_last_file_context_after + 1;
217                 }
218 
219                 if (m_last_file_line > m_last_file_context_after)
220                 {
221                     line = m_last_file_line - m_last_file_context_after;
222                     if (line > m_last_file_context_before)
223                         new_last_file_line = line - m_last_file_context_before - 1;
224                     else
225                         new_last_file_line = 1;
226                 }
227                 else
228                 {
229                     line = 1;
230                     new_last_file_line = 1;
231                 }
232             }
233             else
234                 line = m_last_file_line + m_last_file_context_before;
235         }
236         else
237             line = 1;
238 
239         size_t num_chars_shown = DisplaySourceLinesWithLineNumbersUsingLastFile (line, m_last_file_context_before, m_last_file_context_after, "", s, bp_locs);
240         if (new_last_file_line != 0)
241             m_last_file_line = new_last_file_line;
242         if (reverse)
243             m_first_reverse = false;
244 
245         return num_chars_shown;
246     }
247     return 0;
248 }
249 
250 bool
251 SourceManager::SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line)
252 {
253     FileSP old_file_sp = m_last_file_sp;
254     m_last_file_sp = GetFile (file_spec);
255 
256     m_default_set = true;
257     if (m_last_file_sp)
258     {
259         m_last_file_line = line;
260         return true;
261     }
262     else
263     {
264         m_last_file_sp = old_file_sp;
265         return false;
266     }
267 }
268 
269 bool
270 SourceManager::GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line)
271 {
272     if (m_last_file_sp)
273     {
274         file_spec = m_last_file_sp->GetFileSpec();
275         line = m_last_file_line;
276         return true;
277     }
278     else if (!m_default_set)
279     {
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 = m_target->GetExecutableModulePointer();
284         if (executable_ptr)
285         {
286             SymbolContextList sc_list;
287             uint32_t num_matches;
288             ConstString main_name("main");
289             bool symbols_okay = false;  // Force it to be a debug symbol.
290             bool inlines_okay = true;
291             bool append = false;
292             num_matches = executable_ptr->FindFunctions (main_name, NULL, lldb::eFunctionNameTypeBase, inlines_okay, symbols_okay, append, sc_list);
293             for (uint32_t idx = 0; idx < num_matches; idx++)
294             {
295                 SymbolContext sc;
296                 sc_list.GetContextAtIndex(idx, sc);
297                 if (sc.function)
298                 {
299                     lldb_private::LineEntry line_entry;
300                     if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry))
301                     {
302                         SetDefaultFileAndLine (line_entry.file,
303                                                line_entry.line);
304                         file_spec = m_last_file_sp->GetFileSpec();
305                         line = m_last_file_line;
306                         return true;
307                     }
308                 }
309             }
310         }
311     }
312     return false;
313 }
314 
315 void
316 SourceManager::FindLinesMatchingRegex (FileSpec &file_spec,
317                                        RegularExpression& regex,
318                                        uint32_t start_line,
319                                        uint32_t end_line,
320                                        std::vector<uint32_t> &match_lines)
321 {
322     match_lines.clear();
323     FileSP file_sp = GetFile (file_spec);
324     if (!file_sp)
325         return;
326     return file_sp->FindLinesMatchingRegex (regex, start_line, end_line, match_lines);
327 }
328 
329 SourceManager::File::File(const FileSpec &file_spec, Target *target) :
330     m_file_spec_orig (file_spec),
331     m_file_spec(file_spec),
332     m_mod_time (file_spec.GetModificationTime()),
333     m_data_sp(),
334     m_offsets()
335 {
336     if (!m_mod_time.IsValid())
337     {
338         if (target)
339         {
340             if (!file_spec.GetDirectory() && file_spec.GetFilename())
341             {
342                 // If this is just a file name, lets see if we can find it in the target:
343                 bool check_inlines = false;
344                 SymbolContextList sc_list;
345                 size_t num_matches = target->GetImages().ResolveSymbolContextForFilePath (file_spec.GetFilename().AsCString(),
346                                                                                           0,
347                                                                                           check_inlines,
348                                                                                           lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
349                                                                                           sc_list);
350                 bool got_multiple = false;
351                 if (num_matches != 0)
352                 {
353                     if (num_matches > 1)
354                     {
355                         SymbolContext sc;
356                         FileSpec *test_cu_spec = NULL;
357 
358                         for (unsigned i = 0; i < num_matches; i++)
359                         {
360                             sc_list.GetContextAtIndex(i, sc);
361                             if (sc.comp_unit)
362                             {
363                                 if (test_cu_spec)
364                                 {
365                                     if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
366                                         got_multiple = true;
367                                         break;
368                                 }
369                                 else
370                                     test_cu_spec = sc.comp_unit;
371                             }
372                         }
373                     }
374                     if (!got_multiple)
375                     {
376                         SymbolContext sc;
377                         sc_list.GetContextAtIndex (0, sc);
378                         m_file_spec = sc.comp_unit;
379                         m_mod_time = m_file_spec.GetModificationTime();
380                     }
381                 }
382             }
383             // Try remapping if m_file_spec does not correspond to an existing file.
384             if (!m_file_spec.Exists())
385             {
386                 FileSpec new_file_spec;
387                 // Check target specific source remappings first, then fall back to
388                 // modules objects can have individual path remappings that were detected
389                 // when the debug info for a module was found.
390                 // then
391                 if (target->GetSourcePathMap().FindFile (m_file_spec, new_file_spec) ||
392                     target->GetImages().FindSourceFile (m_file_spec, new_file_spec))
393                 {
394                     m_file_spec = new_file_spec;
395                     m_mod_time = m_file_spec.GetModificationTime();
396                 }
397             }
398         }
399     }
400 
401     if (m_mod_time.IsValid())
402         m_data_sp = m_file_spec.ReadFileContents ();
403 }
404 
405 SourceManager::File::~File()
406 {
407 }
408 
409 uint32_t
410 SourceManager::File::GetLineOffset (uint32_t line)
411 {
412     if (line == 0)
413         return UINT32_MAX;
414 
415     if (line == 1)
416         return 0;
417 
418     if (CalculateLineOffsets (line))
419     {
420         if (line < m_offsets.size())
421             return m_offsets[line - 1]; // yes we want "line - 1" in the index
422     }
423     return UINT32_MAX;
424 }
425 
426 bool
427 SourceManager::File::LineIsValid (uint32_t line)
428 {
429     if (line == 0)
430         return false;
431 
432     if (CalculateLineOffsets (line))
433         return line < m_offsets.size();
434     return false;
435 }
436 
437 size_t
438 SourceManager::File::DisplaySourceLines (uint32_t line, uint32_t context_before, uint32_t context_after, Stream *s)
439 {
440     // TODO: use host API to sign up for file modifications to anything in our
441     // source cache and only update when we determine a file has been updated.
442     // For now we check each time we want to display info for the file.
443     TimeValue curr_mod_time (m_file_spec.GetModificationTime());
444 
445     if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time)
446     {
447         m_mod_time = curr_mod_time;
448         m_data_sp = m_file_spec.ReadFileContents ();
449         m_offsets.clear();
450     }
451 
452     // Sanity check m_data_sp before proceeding.
453     if (!m_data_sp)
454         return 0;
455 
456     const uint32_t start_line = line <= context_before ? 1 : line - context_before;
457     const uint32_t start_line_offset = GetLineOffset (start_line);
458     if (start_line_offset != UINT32_MAX)
459     {
460         const uint32_t end_line = line + context_after;
461         uint32_t end_line_offset = GetLineOffset (end_line + 1);
462         if (end_line_offset == UINT32_MAX)
463             end_line_offset = m_data_sp->GetByteSize();
464 
465         assert (start_line_offset <= end_line_offset);
466         size_t bytes_written = 0;
467         if (start_line_offset < end_line_offset)
468         {
469             size_t count = end_line_offset - start_line_offset;
470             const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
471             bytes_written = s->Write(cstr, count);
472             if (!is_newline_char(cstr[count-1]))
473                 bytes_written += s->EOL();
474         }
475         return bytes_written;
476     }
477     return 0;
478 }
479 
480 void
481 SourceManager::File::FindLinesMatchingRegex (RegularExpression& regex, uint32_t start_line, uint32_t end_line, std::vector<uint32_t> &match_lines)
482 {
483     TimeValue curr_mod_time (m_file_spec.GetModificationTime());
484     if (m_mod_time != curr_mod_time)
485     {
486         m_mod_time = curr_mod_time;
487         m_data_sp = m_file_spec.ReadFileContents ();
488         m_offsets.clear();
489     }
490 
491     match_lines.clear();
492 
493     if (!LineIsValid(start_line) || (end_line != UINT32_MAX && !LineIsValid(end_line)))
494         return;
495     if (start_line > end_line)
496         return;
497 
498     for (uint32_t line_no = start_line; line_no < end_line; line_no++)
499     {
500         std::string buffer;
501         if (!GetLine (line_no, buffer))
502             break;
503         if (regex.Execute(buffer.c_str()))
504         {
505             match_lines.push_back(line_no);
506         }
507     }
508 }
509 
510 bool
511 SourceManager::File::FileSpecMatches (const FileSpec &file_spec)
512 {
513     return FileSpec::Equal (m_file_spec, file_spec, false);
514 }
515 
516 bool
517 lldb_private::operator== (const SourceManager::File &lhs, const SourceManager::File &rhs)
518 {
519     if (lhs.m_file_spec == rhs.m_file_spec)
520     {
521         if (lhs.m_mod_time.IsValid())
522         {
523             if (rhs.m_mod_time.IsValid())
524                 return lhs.m_mod_time == rhs.m_mod_time;
525             else
526                 return false;
527         }
528         else if (rhs.m_mod_time.IsValid())
529             return false;
530         else
531             return true;
532     }
533     else
534         return false;
535 }
536 
537 bool
538 SourceManager::File::CalculateLineOffsets (uint32_t line)
539 {
540     line = UINT32_MAX;  // TODO: take this line out when we support partial indexing
541     if (line == UINT32_MAX)
542     {
543         // Already done?
544         if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
545             return true;
546 
547         if (m_offsets.empty())
548         {
549             if (m_data_sp.get() == NULL)
550                 return false;
551 
552             const char *start = (char *)m_data_sp->GetBytes();
553             if (start)
554             {
555                 const char *end = start + m_data_sp->GetByteSize();
556 
557                 // Calculate all line offsets from scratch
558 
559                 // Push a 1 at index zero to indicate the file has been completely indexed.
560                 m_offsets.push_back(UINT32_MAX);
561                 register const char *s;
562                 for (s = start; s < end; ++s)
563                 {
564                     register char curr_ch = *s;
565                     if (is_newline_char (curr_ch))
566                     {
567                         register char next_ch = s[1];
568                         if (is_newline_char (next_ch))
569                         {
570                             if (curr_ch != next_ch)
571                                 ++s;
572                         }
573                         m_offsets.push_back(s + 1 - start);
574                     }
575                 }
576                 if (!m_offsets.empty())
577                 {
578                     if (m_offsets.back() < end - start)
579                         m_offsets.push_back(end - start);
580                 }
581                 return true;
582             }
583         }
584         else
585         {
586             // Some lines have been populated, start where we last left off
587             assert(!"Not implemented yet");
588         }
589 
590     }
591     else
592     {
593         // Calculate all line offsets up to "line"
594         assert(!"Not implemented yet");
595     }
596     return false;
597 }
598 
599 bool
600 SourceManager::File::GetLine (uint32_t line_no, std::string &buffer)
601 {
602     if (!LineIsValid(line_no))
603         return false;
604 
605     uint32_t start_offset = GetLineOffset (line_no);
606     uint32_t end_offset = GetLineOffset (line_no + 1);
607     if (end_offset == UINT32_MAX)
608     {
609         end_offset = m_data_sp->GetByteSize();
610     }
611     buffer.assign((char *) m_data_sp->GetBytes() + start_offset, end_offset - start_offset);
612 
613     return true;
614 }
615 
616 void
617 SourceManager::SourceFileCache::AddSourceFile (const FileSP &file_sp)
618 {
619     FileSpec file_spec;
620     FileCache::iterator pos = m_file_cache.find(file_spec);
621     if (pos == m_file_cache.end())
622         m_file_cache[file_spec] = file_sp;
623     else
624     {
625         if (file_sp != pos->second)
626             m_file_cache[file_spec] = file_sp;
627     }
628 }
629 
630 SourceManager::FileSP
631 SourceManager::SourceFileCache::FindSourceFile (const FileSpec &file_spec) const
632 {
633     FileSP file_sp;
634     FileCache::const_iterator pos = m_file_cache.find(file_spec);
635     if (pos != m_file_cache.end())
636         file_sp = pos->second;
637     return file_sp;
638 }
639 
640