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 (version == 1) {
77 llvm::DataExtractor record(
78 section.getData().drop_front(cursor.tell()).take_front(record_size),
79 le);
80 llvm::DataExtractor::Cursor cursor(0);
81 uint64_t type_size = record.getULEB128(cursor);
82 llvm::StringRef type_name = record.getBytes(cursor, type_size);
83 llvm::Error error = cursor.takeError();
84 if (!error)
85 fn(llvm::DataExtractor(record.getData().drop_front(cursor.tell()), le),
86 type_name);
87 else
89 "{0}");
90 } else {
91 // Skip unsupported record.
94 "Skipping unsupported embedded type summary of version {0} in {1}.",
95 version, module.GetFileSpec());
96 }
97 section.skip(cursor, record_size);
98 }
99 if (!cursor)
100 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(), "{0}");
101}
102
106 [&](llvm::DataExtractor extractor, llvm::StringRef type_name) {
107 TypeCategoryImplSP category;
109 category);
110 // The type summary record is serialized as follows.
111 //
112 // * The size of the summary string
113 // * The summary string
114 //
115 // Integers are encoded using ULEB.
116 llvm::DataExtractor::Cursor cursor(0);
117 uint64_t summary_size = extractor.getULEB128(cursor);
118 llvm::StringRef summary_string =
119 extractor.getBytes(cursor, summary_size);
120 if (!cursor) {
121 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(),
122 "{0}");
123 return;
124 }
125 if (type_name.empty() || summary_string.empty()) {
127 "Missing string(s) in embedded type summary in {0}, "
128 "type_name={1}, summary={2}",
129 module_sp->GetFileSpec(), type_name, summary_string);
130 return;
131 }
133 auto summary_sp = std::make_shared<StringSummaryFormat>(
134 flags, summary_string.str().c_str());
136 if (type_name.front() == '^')
137 match_type = eFormatterMatchRegex;
138 category->AddTypeSummary(type_name, match_type, summary_sp);
140 "Loaded embedded type summary for '{0}' from {1}.", type_name,
141 module_sp->GetFileSpec());
142 });
143}
144
145static BytecodeSyntheticChildren::SyntheticBytecodeImplementation
147 llvm::MutableArrayRef<std::unique_ptr<llvm::MemoryBuffer>> methods) {
148 using Signatures = FormatterBytecode::Signatures;
150 impl.init = std::move(methods[Signatures::sig_init]);
151 impl.update = std::move(methods[Signatures::sig_update]);
152 impl.num_children = std::move(methods[Signatures::sig_get_num_children]);
153 impl.get_child_at_index =
154 std::move(methods[Signatures::sig_get_child_at_index]);
155 impl.get_child_index = std::move(methods[Signatures::sig_get_child_index]);
156 return impl;
157}
158
161 *module_sp, eSectionTypeLLDBFormatters,
162 [&](llvm::DataExtractor extractor, llvm::StringRef type_name) {
163 // * Flags (ULEB128)
164 // * Function signature (1 byte)
165 // * Length of the program (ULEB128)
166 // * The program bytecode
167 TypeCategoryImplSP category;
169 category);
170 llvm::DataExtractor::Cursor cursor(0);
171 uint64_t flags = extractor.getULEB128(cursor);
172
173 std::unique_ptr<llvm::MemoryBuffer> summary_func_up;
174 std::array<std::unique_ptr<llvm::MemoryBuffer>, kSignatureCount>
175 synthetic_methods;
176 using Signatures = FormatterBytecode::Signatures;
177 while (cursor && cursor.tell() < extractor.size()) {
178 auto signature = static_cast<Signatures>(extractor.getU8(cursor));
179 uint64_t size = extractor.getULEB128(cursor);
180 llvm::StringRef bytecode = extractor.getBytes(cursor, size);
181 if (!cursor) {
182 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), cursor.takeError(),
183 "{0}");
184 break;
185 }
186 auto buffer_up = llvm::MemoryBuffer::getMemBufferCopy(bytecode);
187 if (signature == Signatures::sig_summary)
188 summary_func_up = std::move(buffer_up);
189 else if (signature <= Signatures::sig_update)
190 synthetic_methods[signature] = std::move(buffer_up);
191 else
193 "Unsupported formatter signature {0} for '{1}' in {2}",
194 signature, type_name, module_sp->GetFileSpec());
195 }
196
198 if (type_name.front() == '^')
199 match_type = eFormatterMatchRegex;
200
201 if (summary_func_up) {
202 auto summary_sp = std::make_shared<BytecodeSummaryFormat>(
203 TypeSummaryImpl::Flags(flags), std::move(summary_func_up));
204 category->AddTypeSummary(type_name, match_type, summary_sp);
206 "Loaded embedded type summary for '{0}' from {1}.",
207 type_name, module_sp->GetFileSpec());
208 } else {
210 CreateSyntheticImpl(synthetic_methods);
211 auto synthetic_children_sp =
212 std::make_shared<BytecodeSyntheticChildren>(std::move(impl));
213 category->AddTypeSynthetic(type_name, match_type,
214 synthetic_children_sp);
216 "Loaded embedded type synthetic for '{0}' from {1}.",
217 type_name, module_sp->GetFileSpec());
218 }
219 });
220}
221} // 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:364
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
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:327
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