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/lldb-defines.h"
15#include "lldb/lldb-forward.h"
16
17#include "llvm/Support/Chrono.h"
18#include "llvm/Support/RWMutex.h"
19
20#include <cstddef>
21#include <cstdint>
22#include <map>
23#include <memory>
24#include <optional>
25#include <string>
26#include <vector>
27
28namespace lldb_private {
30class Stream;
32class Target;
33
35public:
36 class File {
37 friend bool operator==(const SourceManager::File &lhs,
38 const SourceManager::File &rhs);
39
40 public:
41 File(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp);
42 File(lldb::SupportFileSP support_file_sp, lldb::DebuggerSP debugger_sp);
43
44 bool ModificationTimeIsStale() const;
45 bool PathRemappingIsStale() const;
46
47 size_t DisplaySourceLines(uint32_t line, std::optional<size_t> column,
48 uint32_t context_before, uint32_t context_after,
49 Stream *s);
50 void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
51 uint32_t end_line,
52 std::vector<uint32_t> &match_lines);
53
54 bool GetLine(uint32_t line_no, std::string &buffer);
55
56 uint32_t GetLineOffset(uint32_t line);
57
58 bool LineIsValid(uint32_t line);
59
61 assert(m_support_file_sp && "SupportFileSP must always be valid");
62 return m_support_file_sp;
63 }
64
66
67 const char *PeekLineData(uint32_t line);
68
69 uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
70
71 uint32_t GetNumLines();
72
73 llvm::sys::TimePoint<> GetTimestamp() const { return m_mod_time; }
74
75 const Checksum &GetChecksum() const { return m_checksum; }
76
77 std::once_flag &GetChecksumWarningOnceFlag() {
79 }
80
81 protected:
82 /// Set file and update modification time.
83 void SetSupportFile(lldb::SupportFileSP support_file_sp);
84
85 bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
86
87 /// The support file. If the target has source mappings, this might be
88 /// different from the original support file passed to the constructor.
90
91 /// Keep track of the on-disk checksum.
93
94 /// Once flag for emitting a checksum mismatch warning.
96
97 // Keep the modification time that this file data is valid for
98 llvm::sys::TimePoint<> m_mod_time;
99
100 // If the target uses path remappings, be sure to clear our notion of a
101 // source file if the path modification ID changes
104 typedef std::vector<uint32_t> LineOffsets;
108
109 private:
110 void CommonInitializer(lldb::SupportFileSP support_file_sp,
111 lldb::TargetSP target_sp);
112 void CommonInitializerImpl(lldb::SupportFileSP support_file_sp,
113 lldb::TargetSP target_sp);
114 };
115
116 typedef std::shared_ptr<File> FileSP;
117
118 /// The SourceFileCache class separates the source manager from the cache of
119 /// source files. There is one source manager per Target but both the Debugger
120 /// and the Process have their own source caches.
121 ///
122 /// The SourceFileCache just handles adding, storing, removing and looking up
123 /// source files. The caching policies are implemented in
124 /// SourceManager::GetFile.
126 public:
127 SourceFileCache() = default;
128 ~SourceFileCache() = default;
129
130 void AddSourceFile(const FileSpec &file_spec, FileSP file_sp);
131 void RemoveSourceFile(const FileSP &file_sp);
132
133 FileSP FindSourceFile(const FileSpec &file_spec) const;
134
135 // Removes all elements from the cache.
136 void Clear() { m_file_cache.clear(); }
137
138 void Dump(Stream &stream) const;
139
140 private:
141 void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp);
142
143 typedef std::map<FileSpec, FileSP> FileCache;
145
146 mutable llvm::sys::RWMutex m_mutex;
147 };
148
149 /// A source manager can be made with a valid Target, in which case it can use
150 /// the path remappings to find source files that are not in their build
151 /// locations. Without a target it won't be able to do this.
152 /// @{
153 SourceManager(const lldb::DebuggerSP &debugger_sp);
154 SourceManager(const lldb::TargetSP &target_sp);
155 /// @}
156
158
160 bool AtLastLine(bool reverse) {
161 return m_last_line == UINT32_MAX || (reverse && m_last_line == 1);
162 }
163
165 lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
166 uint32_t context_before, uint32_t context_after,
167 const char *current_line_cstr, Stream *s,
168 const SymbolContextList *bp_locs = nullptr);
169
170 // This variant uses the last file we visited.
172 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
173 const char *current_line_cstr, Stream *s,
174 const SymbolContextList *bp_locs = nullptr);
175
176 size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
177 const SymbolContextList *bp_locs = nullptr);
178
179 bool SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp,
180 uint32_t line);
181
188
189 std::optional<SupportFileAndLine> GetDefaultFileAndLine();
190
192 return (GetFile(m_last_support_file_sp).get() != nullptr);
193 }
194
195 void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp,
196 RegularExpression &regex, uint32_t start_line,
197 uint32_t end_line,
198 std::vector<uint32_t> &match_lines);
199
200 FileSP GetFile(lldb::SupportFileSP support_file_sp);
201
202protected:
204 uint32_t m_last_line;
205 uint32_t m_last_count;
209
210private:
211 SourceManager(const SourceManager &) = delete;
212 const SourceManager &operator=(const SourceManager &) = delete;
213};
214
215bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
216
217} // namespace lldb_private
218
219#endif // LLDB_CORE_SOURCEMANAGER_H
A file utility class.
Definition FileSpec.h:57
std::once_flag & GetChecksumWarningOnceFlag()
llvm::sys::TimePoint m_mod_time
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
const char * PeekLineData(uint32_t line)
void CommonInitializerImpl(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp)
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)
File(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp)
lldb::SupportFileSP GetSupportFile() const
void CommonInitializer(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp)
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)
void SetSupportFile(lldb::SupportFileSP support_file_sp)
Set file and update modification time.
const Checksum & GetChecksum() const
size_t DisplaySourceLines(uint32_t line, std::optional< size_t > column, uint32_t context_before, uint32_t context_after, Stream *s)
lldb::SupportFileSP m_support_file_sp
The support file.
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)
void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp, RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
SourceManager(const SourceManager &)=delete
bool SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp, uint32_t line)
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)
std::shared_ptr< File > FileSP
const SourceManager & operator=(const SourceManager &)=delete
std::optional< SupportFileAndLine > GetDefaultFileAndLine()
bool AtLastLine(bool reverse)
lldb::SupportFileSP m_last_support_file_sp
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...
lldb::DebuggerWP m_debugger_wp
FileSP GetFile(lldb::SupportFileSP support_file_sp)
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs=nullptr)
size_t DisplaySourceLinesWithLineNumbers(lldb::SupportFileSP support_file_sp, 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)
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.
bool operator==(const Address &lhs, const Address &rhs)
Definition Address.cpp:1011
std::shared_ptr< lldb_private::SupportFile > SupportFileSP
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(lldb::SupportFileSP support_file_sp, uint32_t line)