[Go to site: main page, start]

LLDB mainline
FormatterSection.cpp
Go to the documentation of this file.
1//===-- FormatterBytecode.cpp ---------------------------------------------===//
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
10
11#include "lldb/Core/Module.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include <memory>
18
19using namespace lldb;
20
21namespace lldb_private {
22
23static bool skipPadding(llvm::DataExtractor &section,
24 llvm::DataExtractor::Cursor &cursor) {
25 while (!section.eof(cursor)) {
26 if (section.getU8(cursor) == 0)
27 continue;
28
29 cursor.seek(cursor.tell() - 1);
30 return true;
31 }
32
33 return false; // reached EOF
34}
35
37 Module &module, SectionType section_type,
38 std::function<void(llvm::DataExtractor, llvm::StringRef)> fn) {
39 auto *sections = module.GetSectionList();
40 if (!sections)
41 return;
42
43 auto section_sp = sections->FindSectionByType(section_type, true);
44 if (!section_sp)
45 return;
46
47 TypeCategoryImplSP category;
49
50 // The type summary record is serialized as follows.
51 //
52 // Each record contains, in order:
53 // * Version number of the record format
54 // * The remaining size of the record
55 // * The size of the type identifier
56 // * The type identifier, either a type name, or a regex
57 // * The size of the entry
58 // * The entry
59 //
60 // Integers are encoded using ULEB.
61 //
62 // Strings are encoded with first a length (ULEB), then the string contents,
63 // and lastly a null terminator. The length includes the null.
64
65 DataExtractor lldb_extractor;
66 auto section_size = section_sp->GetSectionData(lldb_extractor);
67 llvm::DataExtractor section = lldb_extractor.GetAsLLVM();
68 bool le = section.isLittleEndian();
69 llvm::DataExtractor::Cursor cursor(0);
70 while (cursor && cursor.tell() < section_size) {
71 if (!skipPadding(section, cursor))
72 break;
73
74 uint64_t version = section.getULEB128(cursor);
75 uint64_t record_size = section.getULEB128(cursor);
76 if (cursor && record_size > section_size - cursor.tell()) {
78 "Record size {0} exceeds the remaining size of the embedded "
79 "formatter section in {1}; ignoring the rest of the section.",
80 record_size, module.GetFileSpec());
81 break;
82 }
83 if (version == 1) {
84 llvm::DataExtractor record(
85 section.getData().drop_front(cursor.tell()).take_front(record_size),
86 le);
87 llvm::DataExtractor::Cursor cursor(0);
88 uint64_t type_size = record.getULEB128(cursor);
89 llvm::StringRef type_name = record.getBytes(cursor, type_size);
90 llvm::Error error = cursor.takeError();
91 if (!error)
92 fn(llvm::DataExtractor(record.getData().drop_front(cursor.tell()), le),
93 type_name);
94 else
96 "{0}");
97 } else {
98 // Skip unsupported record.
101 "Skipping unsupported embedded type summary of version {0} in {1}.",
102 version, module.GetFileSpec());
103 }
104 section.skip(cursor, record_size);
105 }
106 if (!cursor)
107 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(), "{0}");
108}
109
113 [&](llvm::DataExtractor extractor, llvm::StringRef type_name) {
114 TypeCategoryImplSP category;
116 category);
117 // The type summary record is serialized as follows.
118 //
119 // * The size of the summary string
120 // * The summary string
121 //
122 // Integers are encoded using ULEB.
123 llvm::DataExtractor::Cursor cursor(0);
124 uint64_t summary_size = extractor.getULEB128(cursor);
125 llvm::StringRef summary_string =
126 extractor.getBytes(cursor, summary_size);
127 if (!cursor) {
128 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(),
129 "{0}");
130 return;
131 }
132 if (type_name.empty() || summary_string.empty()) {
134 "Missing string(s) in embedded type summary in {0}, "
135 "type_name={1}, summary={2}",
136 module_sp->GetFileSpec(), type_name, summary_string);
137 return;
138 }
140 auto summary_sp = std::make_shared<StringSummaryFormat>(
141 flags, summary_string.str().c_str());
143 if (type_name.front() == '^')
144 match_type = eFormatterMatchRegex;
145 category->AddTypeSummary(type_name, match_type, summary_sp);
147 "Loaded embedded type summary for '{0}' from {1}.", type_name,
148 module_sp->GetFileSpec());
149 });
150}
151
152static BytecodeSyntheticChildren::SyntheticBytecodeImplementation
154 llvm::MutableArrayRef<std::unique_ptr<llvm::MemoryBuffer>> methods) {
155 using Signatures = FormatterBytecode::Signatures;
157 impl.init = std::move(methods[Signatures::sig_init]);
158 impl.update = std::move(methods[Signatures::sig_update]);
159 impl.num_children = std::move(methods[Signatures::sig_get_num_children]);
160 impl.get_child_at_index =
161 std::move(methods[Signatures::sig_get_child_at_index]);
162 impl.get_child_index = std::move(methods[Signatures::sig_get_child_index]);
163 return impl;
164}
165
168 *module_sp, eSectionTypeLLDBFormatters,
169 [&](llvm::DataExtractor extractor, llvm::StringRef type_name) {
170 // * Flags (ULEB128)
171 // * Function signature (1 byte)
172 // * Length of the program (ULEB128)
173 // * The program bytecode
174 TypeCategoryImplSP category;
176 category);
177 llvm::DataExtractor::Cursor cursor(0);
178 uint64_t flags = extractor.getULEB128(cursor);
179
180 std::unique_ptr<llvm::MemoryBuffer> summary_func_up;
181 std::array<std::unique_ptr<llvm::MemoryBuffer>, kSignatureCount>
182 synthetic_methods;
183 bool has_synthetic_method = false;
184 using Signatures = FormatterBytecode::Signatures;
185 while (cursor && cursor.tell() < extractor.size()) {
186 auto signature = static_cast<Signatures>(extractor.getU8(cursor));
187 uint64_t size = extractor.getULEB128(cursor);
188 llvm::StringRef bytecode = extractor.getBytes(cursor, size);
189 if (!cursor)
190 break;
191 auto buffer_up = llvm::MemoryBuffer::getMemBufferCopy(bytecode);
192 if (signature == Signatures::sig_summary)
193 summary_func_up = std::move(buffer_up);
194 else if (signature <= Signatures::sig_update) {
195 synthetic_methods[signature] = std::move(buffer_up);
196 has_synthetic_method = true;
197 } else
199 "Unsupported formatter signature {0} for '{1}' in {2}",
200 signature, type_name, module_sp->GetFileSpec());
201 }
202 if (!cursor) {
203 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(),
204 "{0}");
205 return;
206 }
207
209 if (type_name.front() == '^')
210 match_type = eFormatterMatchRegex;
211
212 if (summary_func_up) {
213 auto summary_sp = std::make_shared<BytecodeSummaryFormat>(
214 TypeSummaryImpl::Flags(flags), std::move(summary_func_up));
215 category->AddTypeSummary(type_name, match_type, summary_sp);
217 "Loaded embedded type summary for '{0}' from {1}.",
218 type_name, module_sp->GetFileSpec());
219 } else if (has_synthetic_method) {
221 CreateSyntheticImpl(synthetic_methods);
222 auto synthetic_children_sp =
223 std::make_shared<BytecodeSyntheticChildren>(std::move(impl));
224 category->AddTypeSynthetic(type_name, match_type,
225 synthetic_children_sp);
227 "Loaded embedded type synthetic for '{0}' from {1}.",
228 type_name, module_sp->GetFileSpec());
229 } else {
231 "No summary or synthetic methods found for '{0}' in {1}, "
232 "not registering a formatter.",
233 type_name, module_sp->GetFileSpec());
234 }
235 });
236}
237} // namespace lldb_private
static llvm::raw_ostream & error(Stream &strm)
#define kSignatureCount
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:382
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
llvm::DataExtractor GetAsLLVM() const
static bool GetCategory(ConstString category, lldb::TypeCategoryImplSP &entry, bool allow_create=true)
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition Module.h:447
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
static void ForEachFormatterInModule(Module &module, SectionType section_type, std::function< void(llvm::DataExtractor, llvm::StringRef)> fn)
static bool skipPadding(llvm::DataExtractor &section, llvm::DataExtractor::Cursor &cursor)
static BytecodeSyntheticChildren::SyntheticBytecodeImplementation CreateSyntheticImpl(llvm::MutableArrayRef< std::unique_ptr< llvm::MemoryBuffer > > methods)
void LoadTypeSummariesForModule(lldb::ModuleSP module_sp)
Load type summaries embedded in the binary.
void LoadFormattersForModule(lldb::ModuleSP module_sp)
Load data formatters embedded in the binary.
FormatterMatchType
Type of match to be performed when looking for a formatter for a data type.
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP
@ eSectionTypeLLDBFormatters
@ eSectionTypeLLDBTypeSummaries
std::shared_ptr< lldb_private::Module > ModuleSP