LLDB mainline
SourceManager.h
Go to the documentation of this file.
1//===-- SourceManager.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_CORE_SOURCEMANAGER_H
10#define LLDB_CORE_SOURCEMANAGER_H
11
14#include "lldb/Utility/Locked.h"
16#include "lldb/lldb-defines.h"
17#include "lldb/lldb-forward.h"
18
19#include "llvm/Support/Chrono.h"
20#include "llvm/Support/RWMutex.h"
21
22#include <cstddef>
23#include <cstdint>
24#include <map>
25#include <memory>
26#include <optional>
27#include <shared_mutex>
28#include <string>
29#include <vector>
30
31namespace lldb_private {
33class Stream;
35class Target;
36
38public:
39 class File {
40 friend bool operator==(const SourceManager::File &lhs,
41 const SourceManager::File &rhs);
42
43 public:
44 File(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp);
45 File(SupportFileNSP support_file_nsp, lldb::DebuggerSP debugger_sp);
46
47 bool ModificationTimeIsStale() const;
48 bool PathRemappingIsStale() const;
49
50 size_t DisplaySourceLines(
51 uint32_t line, std::optional<size_t> column, uint32_t context_before,
52 uint32_t context_after, Stream *s,
54 void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
55 uint32_t end_line,
56 std::vector<uint32_t> &match_lines);
57
58 bool GetLine(uint32_t line_no, std::string &buffer);
59
60 uint32_t GetLineOffset(uint32_t line);
61
62 bool LineIsValid(uint32_t line);
63
65 assert(m_support_file_nsp && "SupportFileNSP must always be valid");
66 return m_support_file_nsp;
67 }
68
70
71 const char *PeekLineData(uint32_t line);
72
73 uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
74
75 uint32_t GetNumLines();
76
77 llvm::sys::TimePoint<> GetTimestamp() const { return m_mod_time; }
78
79 const Checksum &GetChecksum() const { return m_checksum; }
80
81 std::once_flag &GetChecksumWarningOnceFlag() {
83 }
84
85 protected:
86 /// Set file and update modification time.
87 void SetSupportFile(SupportFileNSP support_file_nsp);
88
89 bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
90
91 /// The support file. If the target has source mappings, this might be
92 /// different from the original support file passed to the constructor.
94
95 /// Keep track of the on-disk checksum.
97
98 /// Once flag for emitting a checksum mismatch warning.
100
101 // Keep the modification time that this file data is valid for
102 llvm::sys::TimePoint<> m_mod_time;
103
104 // If the target uses path remappings, be sure to clear our notion of a
105 // source file if the path modification ID changes
108 typedef std::vector<uint32_t> LineOffsets;
109
110 /// The line offsets for this file.
111 /// This member that is computed after this File was created, so write
112 /// access can happen from several threads..
114
117
118 private:
119 void CommonInitializer(SupportFileNSP support_file_nsp,
120 lldb::TargetSP target_sp);
121 void CommonInitializerImpl(SupportFileNSP support_file_nsp,
122 lldb::TargetSP target_sp);
123 };
124
125 typedef std::shared_ptr<File> FileSP;
126
127 /// The SourceFileCache class separates the source manager from the cache of
128 /// source files. There is one source manager per Target but both the Debugger
129 /// and the Process have their own source caches.
130 ///
131 /// The SourceFileCache just handles adding, storing, removing and looking up
132 /// source files. The caching policies are implemented in
133 /// SourceManager::GetFile.
135 public:
136 SourceFileCache() = default;
137 ~SourceFileCache() = default;
138
139 void AddSourceFile(const FileSpec &file_spec, FileSP file_sp);
140 void RemoveSourceFile(const FileSP &file_sp);
141
142 FileSP FindSourceFile(const FileSpec &file_spec) const;
143
144 // Removes all elements from the cache.
145 void Clear() { m_file_cache.clear(); }
146
147 void Dump(Stream &stream) const;
148
149 private:
150 void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp);
151
152 typedef std::map<FileSpec, FileSP> FileCache;
154
155 mutable llvm::sys::RWMutex m_mutex;
156 };
157
158 /// A source manager can be made with a valid Target, in which case it can use
159 /// the path remappings to find source files that are not in their build
160 /// locations. Without a target it won't be able to do this.
161 /// @{
162 SourceManager(const lldb::DebuggerSP &debugger_sp);
163 SourceManager(const lldb::TargetSP &target_sp);
164 /// @}
165
167
169 bool AtLastLine(bool reverse) {
170 return m_last_line == UINT32_MAX || (reverse && m_last_line == 1);
171 }
172
174 SupportFileNSP support_file_nsp, uint32_t line, uint32_t column,
175 uint32_t context_before, uint32_t context_after,
176 const char *current_line_cstr, Stream *s,
177 const SymbolContextList *bp_locs = nullptr,
179
180 // This variant uses the last file we visited.
182 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
183 const char *current_line_cstr, Stream *s,
184 const SymbolContextList *bp_locs = nullptr,
186
188 Stream *s, uint32_t count, bool reverse,
189 const SymbolContextList *bp_locs = nullptr,
191
192 bool SetDefaultFileAndLine(SupportFileNSP support_file_nsp, uint32_t line);
193
200
201 std::optional<SupportFileAndLine> GetDefaultFileAndLine();
202
204 return (GetFile(m_last_support_file_nsp).get() != nullptr);
205 }
206
207 void FindLinesMatchingRegex(SupportFileNSP support_file_nsp,
208 RegularExpression &regex, uint32_t start_line,
209 uint32_t end_line,
210 std::vector<uint32_t> &match_lines);
211
212 FileSP GetFile(SupportFileNSP support_file_nsp);
213
214protected:
216 uint32_t m_last_line;
217 uint32_t m_last_count;
221
222private:
223 SourceManager(const SourceManager &) = delete;
224 const SourceManager &operator=(const SourceManager &) = delete;
225};
226
227bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
228
229} // namespace lldb_private
230
231#endif // LLDB_CORE_SOURCEMANAGER_H
A file utility class.
Definition FileSpec.h:56
Bundles a value of type T with the Mutex that guards it.
Definition Locked.h:178
std::once_flag & GetChecksumWarningOnceFlag()
void CommonInitializerImpl(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp)
File(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp)
File(SupportFileNSP support_file_nsp, lldb::DebuggerSP debugger_sp)
uint32_t GetSourceMapModificationID() const
void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
std::vector< uint32_t > LineOffsets
void CommonInitializer(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp)
const char * PeekLineData(uint32_t line)
size_t DisplaySourceLines(uint32_t line, std::optional< size_t > column, uint32_t context_before, uint32_t context_after, Stream *s, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
SupportFileNSP GetSupportFile() const
Checksum m_checksum
Keep track of the on-disk checksum.
uint32_t GetLineLength(uint32_t line, bool include_newline_chars)
bool CalculateLineOffsets(uint32_t line=UINT32_MAX)
Guarded< LineOffsets, std::shared_mutex > m_offsets
The line offsets for this file.
SupportFileNSP m_support_file_nsp
The support file.
friend bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs)
bool GetLine(uint32_t line_no, std::string &buffer)
uint32_t GetLineOffset(uint32_t line)
const Checksum & GetChecksum() const
void SetSupportFile(SupportFileNSP support_file_nsp)
Set file and update modification time.
llvm::sys::TimePoint GetTimestamp() const
std::once_flag m_checksum_warning_once_flag
Once flag for emitting a checksum mismatch warning.
void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp)
FileSP FindSourceFile(const FileSpec &file_spec) const
void AddSourceFile(const FileSpec &file_spec, FileSP file_sp)
SourceManager(const SourceManager &)=delete
void FindLinesMatchingRegex(SupportFileNSP support_file_nsp, RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
std::shared_ptr< File > FileSP
const SourceManager & operator=(const SourceManager &)=delete
std::optional< SupportFileAndLine > GetDefaultFileAndLine()
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs=nullptr, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
bool AtLastLine(bool reverse)
SourceManager(const lldb::DebuggerSP &debugger_sp)
A source manager can be made with a valid Target, in which case it can use the path remappings to fin...
SourceManager(const lldb::TargetSP &target_sp)
lldb::DebuggerWP m_debugger_wp
FileSP GetFile(SupportFileNSP support_file_nsp)
size_t DisplaySourceLinesWithLineNumbers(SupportFileNSP support_file_nsp, uint32_t line, uint32_t column, uint32_t context_before, uint32_t context_after, const char *current_line_cstr, Stream *s, const SymbolContextList *bp_locs=nullptr, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
bool SetDefaultFileAndLine(SupportFileNSP support_file_nsp, uint32_t line)
SupportFileNSP m_last_support_file_nsp
size_t DisplaySourceLinesWithLineNumbersUsingLastFile(uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column, const char *current_line_cstr, Stream *s, const SymbolContextList *bp_locs=nullptr, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Defines a list of symbol context objects.
#define UINT32_MAX
A class that represents a running process on the host machine.
NonNullSharedPtr< lldb_private::SupportFile > SupportFileNSP
Definition SupportFile.h:80
bool operator==(const Address &lhs, const Address &rhs)
Definition Address.cpp:1005
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::weak_ptr< lldb_private::Debugger > DebuggerWP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::weak_ptr< lldb_private::Target > TargetWP
std::shared_ptr< lldb_private::Target > TargetSP
SupportFileAndLine(SupportFileNSP support_file_nsp, uint32_t line)