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 uint8_t addr_size = section.getAddressSize();
70 llvm::DataExtractor::Cursor cursor(0);
71 while (cursor && cursor.tell() < section_size) {
72 if (!skipPadding(section, cursor))
73 break;
74
75 uint64_t version = section.getULEB128(cursor);
76 uint64_t record_size = section.getULEB128(cursor);
77 if (version == 1) {
78 llvm::DataExtractor record(
79 section.getData().drop_front(cursor.tell()).take_front(record_size),
80 le, addr_size);
81 llvm::DataExtractor::Cursor cursor(0);
82 uint64_t type_size = record.getULEB128(cursor);
83 llvm::StringRef type_name = record.getBytes(cursor, type_size);
84 llvm::Error error = cursor.takeError();
85 if (!error)
86 fn(llvm::DataExtractor(record.getData().drop_front(cursor.tell()), le,
87 addr_size),
88 type_name);
89 else
91 "{0}");
92 } else {
93 // Skip unsupported record.
96 "Skipping unsupported embedded type summary of version {0} in {1}.",
97 version, module.GetFileSpec());
98 }
99 section.skip(cursor, record_size);
100 }
101 if (!cursor)
102 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(), "{0}");
103}
104
108 [&](llvm::DataExtractor extractor, llvm::StringRef type_name) {
109 TypeCategoryImplSP category;
111 category);
112 // The type summary record is serialized as follows.
113 //
114 // * The size of the summary string
115 // * The summary string
116 //
117 // Integers are encoded using ULEB.
118 llvm::DataExtractor::Cursor cursor(0);
119 uint64_t summary_size = extractor.getULEB128(cursor);
120 llvm::StringRef summary_string =
121 extractor.getBytes(cursor, summary_size);
122 if (!cursor) {
123 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(),
124 "{0}");
125 return;
126 }
127 if (type_name.empty() || summary_string.empty()) {
129 "Missing string(s) in embedded type summary in {0}, "
130 "type_name={1}, summary={2}",
131 module_sp->GetFileSpec(), type_name, summary_string);
132 return;
133 }
135 auto summary_sp = std::make_shared<StringSummaryFormat>(
136 flags, summary_string.str().c_str());
138 if (type_name.front() == '^')
139 match_type = eFormatterMatchRegex;
140 category->AddTypeSummary(type_name, match_type, summary_sp);
142 "Loaded embedded type summary for '{0}' from {1}.", type_name,
143 module_sp->GetFileSpec());
144 });
145}
146
147static BytecodeSyntheticChildren::SyntheticBytecodeImplementation
149 llvm::MutableArrayRef<std::unique_ptr<llvm::MemoryBuffer>> methods) {
150 using Signatures = FormatterBytecode::Signatures;
152 impl.init = std::move(methods[Signatures::sig_init]);
153 impl.update = std::move(methods[Signatures::sig_update]);
154 impl.num_children = std::move(methods[Signatures::sig_get_num_children]);
155 impl.get_child_at_index =
156 std::move(methods[Signatures::sig_get_child_at_index]);
157 impl.get_child_index = std::move(methods[Signatures::sig_get_child_index]);
158 return impl;
159}
160
163 *module_sp, eSectionTypeLLDBFormatters,
164 [&](llvm::DataExtractor extractor, llvm::StringRef type_name) {
165 // * Flags (ULEB128)
166 // * Function signature (1 byte)
167 // * Length of the program (ULEB128)
168 // * The program bytecode
169 TypeCategoryImplSP category;
171 category);
172 llvm::DataExtractor::Cursor cursor(0);
173 uint64_t flags = extractor.getULEB128(cursor);
174
175 std::unique_ptr<llvm::MemoryBuffer> summary_func_up;
176 std::array<std::unique_ptr<llvm::MemoryBuffer>, kSignatureCount>
177 synthetic_methods;
178 using Signatures = FormatterBytecode::Signatures;
179 while (cursor && cursor.tell() < extractor.size()) {
180 auto signature = static_cast<Signatures>(extractor.getU8(cursor));
181 uint64_t size = extractor.getULEB128(cursor);
182 llvm::StringRef bytecode = extractor.getBytes(cursor, size);
183 if (!cursor) {
184 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(),
185 "{0}");
186 break;
187 }
188 auto buffer_up = llvm::MemoryBuffer::getMemBufferCopy(bytecode);
189 if (signature == Signatures::sig_summary)
190 summary_func_up = std::move(buffer_up);
191 else if (signature <= Signatures::sig_update)
192 synthetic_methods[signature] = std::move(buffer_up);
193 else
195 "Unsupported formatter signature {0} for '{1}' in {2}",
196 signature, type_name, module_sp->GetFileSpec());
197 }
198
200 if (type_name.front() == '^')
201 match_type = eFormatterMatchRegex;
202
203 if (summary_func_up) {
204 auto summary_sp = std::make_shared<BytecodeSummaryFormat>(
205 TypeSummaryImpl::Flags(flags), std::move(summary_func_up));
206 category->AddTypeSummary(type_name, match_type, summary_sp);
208 "Loaded embedded type summary for '{0}' from {1}.",
209 type_name, module_sp->GetFileSpec());
210 } else {
212 CreateSyntheticImpl(synthetic_methods);
213 auto synthetic_children_sp =
214 std::make_shared<BytecodeSyntheticChildren>(std::move(impl));
215 category->AddTypeSynthetic(type_name, match_type,
216 synthetic_children_sp);
218 "Loaded embedded type synthetic for '{0}' from {1}.",
219 type_name, module_sp->GetFileSpec());
220 }
221 });
222}
223} // 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:369
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
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:90
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition Module.h:446
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:332
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.
@ eFormatterMatchExact
@ eFormatterMatchRegex
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP
@ eSectionTypeLLDBFormatters
@ eSectionTypeLLDBTypeSummaries
std::shared_ptr< lldb_private::Module > ModuleSP