Skip to content

Commit 73a5fe9

Browse files
committed
[lldb] Implement a formatter bytecode interpreter in C++
Compared to the python version, this also does type checking and error handling, so it's slightly longer, however, it's still comfortably under 500 lines.
1 parent 1e51be8 commit 73a5fe9

File tree

22 files changed

+1365
-83
lines changed

22 files changed

+1365
-83
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- FormattersSection.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_DATAFORMATTERS_FORMATTERSECTION_H
10+
#define LLDB_DATAFORMATTERS_FORMATTERSECTION_H
11+
12+
#include "lldb/lldb-enumerations.h"
13+
#include "lldb/lldb-forward.h"
14+
15+
namespace lldb_private {
16+
/// Load type summaries embedded in the binary. These are type summaries
17+
/// provided by the authors of the code.
18+
void LoadTypeSummariesForModule(lldb::ModuleSP module_sp);
19+
20+
/// Load data formatters embedded in the binary. These are formatters provided
21+
/// by the authors of the code using LLDB formatter bytecode.
22+
void LoadFormattersForModule(lldb::ModuleSP module_sp);
23+
24+
} // namespace lldb_private
25+
26+
#endif // LLDB_DATAFORMATTERS_FORMATTERSECTION_H

lldb/include/lldb/DataFormatters/FormattersHelpers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//===-- FormattersHelpers.h --------------------------------------*- C++
2-
//-*-===//
1+
//===-- FormattersHelpers.h -------------------------------------*- C++ -*-===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.

lldb/include/lldb/DataFormatters/TypeSummary.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "lldb/Utility/Status.h"
2323
#include "lldb/Utility/StructuredData.h"
2424

25+
namespace llvm {
26+
class MemoryBuffer;
27+
}
28+
2529
namespace lldb_private {
2630
class TypeSummaryOptions {
2731
public:
@@ -44,7 +48,7 @@ class TypeSummaryOptions {
4448

4549
class TypeSummaryImpl {
4650
public:
47-
enum class Kind { eSummaryString, eScript, eCallback, eInternal };
51+
enum class Kind { eSummaryString, eScript, eBytecode, eCallback, eInternal };
4852

4953
virtual ~TypeSummaryImpl() = default;
5054

@@ -409,6 +413,23 @@ struct ScriptSummaryFormat : public TypeSummaryImpl {
409413
ScriptSummaryFormat(const ScriptSummaryFormat &) = delete;
410414
const ScriptSummaryFormat &operator=(const ScriptSummaryFormat &) = delete;
411415
};
416+
417+
/// A summary formatter that is defined in LLDB formmater bytecode.
418+
class BytecodeSummaryFormat : public TypeSummaryImpl {
419+
std::unique_ptr<llvm::MemoryBuffer> m_bytecode;
420+
421+
public:
422+
BytecodeSummaryFormat(const TypeSummaryImpl::Flags &flags,
423+
std::unique_ptr<llvm::MemoryBuffer> bytecode);
424+
bool FormatObject(ValueObject *valobj, std::string &dest,
425+
const TypeSummaryOptions &options) override;
426+
std::string GetDescription() override;
427+
std::string GetName() override;
428+
static bool classof(const TypeSummaryImpl *S) {
429+
return S->GetKind() == Kind::eBytecode;
430+
}
431+
};
432+
412433
} // namespace lldb_private
413434

414435
#endif // LLDB_DATAFORMATTERS_TYPESUMMARY_H

lldb/include/lldb/lldb-enumerations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ enum SectionType {
764764
eSectionTypeDWARFDebugTuIndex,
765765
eSectionTypeCTF,
766766
eSectionTypeLLDBTypeSummaries,
767+
eSectionTypeLLDBFormatters,
767768
eSectionTypeSwiftModules,
768769
};
769770

lldb/source/API/SBTypeSummary.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ bool SBTypeSummary::IsEqualTo(lldb::SBTypeSummary &rhs) {
343343
case TypeSummaryImpl::Kind::eCallback:
344344
return llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get()) ==
345345
llvm::dyn_cast<CXXFunctionSummaryFormat>(rhs.m_opaque_sp.get());
346+
case TypeSummaryImpl::Kind::eBytecode:
346347
case TypeSummaryImpl::Kind::eScript:
347348
if (IsFunctionCode() != rhs.IsFunctionCode())
348349
return false;

lldb/source/Core/Section.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ const char *Section::GetTypeAsCString() const {
149149
return "ctf";
150150
case eSectionTypeLLDBTypeSummaries:
151151
return "lldb-type-summaries";
152-
case eSectionTypeOther:
153-
return "regular";
152+
case eSectionTypeLLDBFormatters:
153+
return "lldb-formatters";
154154
case eSectionTypeSwiftModules:
155155
return "swift-modules";
156+
case eSectionTypeOther:
157+
return "regular";
156158
}
157159
return "unknown";
158160
}
@@ -460,6 +462,7 @@ bool Section::ContainsOnlyDebugInfo() const {
460462
case eSectionTypeDWARFGNUDebugAltLink:
461463
case eSectionTypeCTF:
462464
case eSectionTypeLLDBTypeSummaries:
465+
case eSectionTypeLLDBFormatters:
463466
case eSectionTypeSwiftModules:
464467
return true;
465468
}

lldb/source/DataFormatters/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ add_lldb_library(lldbDataFormatters NO_PLUGIN_DEPENDENCIES
55
FormatCache.cpp
66
FormatClasses.cpp
77
FormatManager.cpp
8+
FormatterBytecode.cpp
89
FormattersHelpers.cpp
10+
FormatterSection.cpp
911
LanguageCategory.cpp
1012
StringPrinter.cpp
1113
TypeCategory.cpp

0 commit comments

Comments
 (0)