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