LLDB mainline
TypeFormat.cpp
Go to the documentation of this file.
1//===-- TypeFormat.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
12
13
15#include "lldb/lldb-public.h"
16
23#include "lldb/Target/Target.h"
26#include <optional>
27
28using namespace lldb;
29using namespace lldb_private;
30
31TypeFormatImpl::TypeFormatImpl(const Flags &flags) : m_flags(flags) {}
32
34
36 const TypeFormatImpl::Flags &flags)
37 : TypeFormatImpl(flags), m_format(f) {}
38
40
42 std::string &dest) const {
43 if (!valobj)
44 return false;
45 if (valobj->CanProvideValue()) {
46 Value &value(valobj->GetValue());
47 const Value::ContextType context_type = value.GetContextType();
49 DataExtractor data;
50
51 if (context_type == Value::ContextType::RegisterInfo) {
52 const RegisterInfo *reg_info = value.GetRegisterInfo();
53 if (reg_info) {
55 valobj->GetData(data, error);
56 if (error.Fail())
57 return false;
58
59 StreamString reg_sstr;
60 DumpDataExtractor(data, &reg_sstr, 0, GetFormat(), reg_info->byte_size,
63 dest = std::string(reg_sstr.GetString());
64 }
65 } else {
66 CompilerType compiler_type = value.GetCompilerType();
67 if (compiler_type) {
68 // put custom bytes to display in the DataExtractor to override the
69 // default value logic
70 if (GetFormat() == eFormatCString) {
71 lldb_private::Flags type_flags(compiler_type.GetTypeInfo(
72 nullptr)); // disambiguate w.r.t. TypeFormatImpl::Flags
73 if (type_flags.Test(eTypeIsPointer) &&
74 !type_flags.Test(eTypeIsObjC)) {
75 // if we are dumping a pointer as a c-string, get the pointee data
76 // as a string
77 TargetSP target_sp(valobj->GetTargetSP());
78 if (target_sp) {
79 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
81 WritableDataBufferSP buffer_sp(
82 new DataBufferHeap(max_len + 1, 0));
83 Address address(valobj->GetPointerValue());
84 target_sp->ReadCStringFromMemory(
85 address, (char *)buffer_sp->GetBytes(), max_len, error);
86 if (error.Success())
87 data.SetData(buffer_sp);
88 }
89 }
90 } else {
92 valobj->GetData(data, error);
93 if (error.Fail())
94 return false;
95 }
96
97 ExecutionContextScope *exe_scope =
99 std::optional<uint64_t> size = compiler_type.GetByteSize(exe_scope);
100 if (!size)
101 return false;
102 StreamString sstr;
103 compiler_type.DumpTypeValue(
104 &sstr, // The stream to use for display
105 GetFormat(), // Format to display this type with
106 data, // Data to extract from
107 0, // Byte offset into "m_data"
108 *size, // Byte size of item in "m_data"
109 valobj->GetBitfieldBitSize(), // Bitfield bit size
110 valobj->GetBitfieldBitOffset(), // Bitfield bit offset
111 exe_scope);
112 // Given that we do not want to set the ValueObject's m_error for a
113 // formatting error (or else we wouldn't be able to reformat until a
114 // next update), an empty string is treated as a "false" return from
115 // here, but that's about as severe as we get
116 // CompilerType::DumpTypeValue() should always return something, even
117 // if that something is an error message
118 dest = std::string(sstr.GetString());
119 }
120 }
121 return !dest.empty();
122 } else
123 return false;
124}
125
127 StreamString sstr;
129 Cascades() ? "" : " (not cascading)",
130 SkipsPointers() ? " (skip pointers)" : "",
131 SkipsReferences() ? " (skip references)" : "");
132 return std::string(sstr.GetString());
133}
134
136 ConstString type_name, const TypeFormatImpl::Flags &flags)
137 : TypeFormatImpl(flags), m_enum_type(type_name), m_types() {}
138
140
142 std::string &dest) const {
143 dest.clear();
144 if (!valobj)
145 return false;
146 if (!valobj->CanProvideValue())
147 return false;
148 ProcessSP process_sp;
149 TargetSP target_sp;
150 void *valobj_key = (process_sp = valobj->GetProcessSP()).get();
151 if (!valobj_key)
152 valobj_key = (target_sp = valobj->GetTargetSP()).get();
153 else
154 target_sp = process_sp->GetTarget().shared_from_this();
155 if (!valobj_key)
156 return false;
157 auto iter = m_types.find(valobj_key), end = m_types.end();
158 CompilerType valobj_enum_type;
159 if (iter == end) {
160 // probably a redundant check
161 if (!target_sp)
162 return false;
163 const ModuleList &images(target_sp->GetImages());
165 TypeResults results;
166 images.FindTypes(nullptr, query, results);
167 if (results.GetTypeMap().Empty())
168 return false;
169 for (lldb::TypeSP type_sp : results.GetTypeMap().Types()) {
170 if (!type_sp)
171 continue;
172 if ((type_sp->GetForwardCompilerType().GetTypeInfo() &
173 eTypeIsEnumeration) == eTypeIsEnumeration) {
174 valobj_enum_type = type_sp->GetFullCompilerType();
175 m_types.emplace(valobj_key, valobj_enum_type);
176 break;
177 }
178 }
179 } else
180 valobj_enum_type = iter->second;
181 if (!valobj_enum_type.IsValid())
182 return false;
183 DataExtractor data;
185 valobj->GetData(data, error);
186 if (error.Fail())
187 return false;
188 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
189 StreamString sstr;
190 valobj_enum_type.DumpTypeValue(&sstr, lldb::eFormatEnum, data, 0,
191 data.GetByteSize(), 0, 0,
193 if (!sstr.GetString().empty())
194 dest = std::string(sstr.GetString());
195 return !dest.empty();
196}
197
199 StreamString sstr;
200 sstr.Printf("as type %s%s%s%s", m_enum_type.AsCString("<invalid type>"),
201 Cascades() ? "" : " (not cascading)",
202 SkipsPointers() ? " (skip pointers)" : "",
203 SkipsReferences() ? " (skip references)" : "");
204 return std::string(sstr.GetString());
205}
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition: Address.h:62
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:197
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
Definition: DataExtractor.h:48
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
A class to manage flags.
Definition: Flags.h:22
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
static const char * GetFormatAsCString(lldb::Format format)
A collection class for Module objects.
Definition: ModuleList.h:103
void FindTypes(Module *search_first, const TypeQuery &query, lldb_private::TypeResults &results) const
Find types using a type-matching object that contains all search parameters.
Definition: ModuleList.cpp:587
An error handling class.
Definition: Status.h:44
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
bool FormatObject(ValueObject *valobj, std::string &dest) const override
Definition: TypeFormat.cpp:141
std::string GetDescription() override
Definition: TypeFormat.cpp:198
std::unordered_map< void *, CompilerType > m_types
Definition: TypeFormat.h:212
TypeFormatImpl_EnumType(ConstString type_name=ConstString(""), const TypeFormatImpl::Flags &flags=Flags())
Definition: TypeFormat.cpp:135
bool FormatObject(ValueObject *valobj, std::string &dest) const override
Definition: TypeFormat.cpp:41
TypeFormatImpl_Format(lldb::Format f=lldb::eFormatInvalid, const TypeFormatImpl::Flags &flags=Flags())
Definition: TypeFormat.cpp:35
lldb::Format GetFormat() const
Definition: TypeFormat.h:168
std::string GetDescription() override
Definition: TypeFormat.cpp:126
TypeFormatImpl(const Flags &flags=Flags())
Definition: TypeFormat.cpp:31
TypeIterable Types() const
Definition: TypeMap.h:49
bool Empty() const
Definition: TypeMap.cpp:77
A class that contains all state required for type lookups.
Definition: Type.h:98
This class tracks the state and results of a TypeQuery.
Definition: Type.h:310
TypeMap & GetTypeMap()
Definition: Type.h:352
virtual uint32_t GetBitfieldBitSize()
Definition: ValueObject.h:424
virtual uint64_t GetData(DataExtractor &data, Status &error)
lldb::addr_t GetPointerValue(AddressType *address_type=nullptr)
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:338
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
virtual uint32_t GetBitfieldBitOffset()
Definition: ValueObject.h:426
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
virtual bool CanProvideValue()
const Value & GetValue() const
Definition: ValueObject.h:511
RegisterInfo * GetRegisterInfo() const
Definition: Value.cpp:140
ContextType
Type that describes Value::m_context.
Definition: Value.h:56
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
ContextType GetContextType() const
Definition: Value.h:87
const CompilerType & GetCompilerType()
Definition: Value.cpp:239
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
lldb::offset_t DumpDataExtractor(const DataExtractor &DE, Stream *s, lldb::offset_t offset, lldb::Format item_format, size_t item_byte_size, size_t item_count, size_t num_per_line, uint64_t base_addr, uint32_t item_bit_size, uint32_t item_bit_offset, ExecutionContextScope *exe_scope=nullptr, bool show_memory_tags=false)
Dumps item_count objects into the stream s.
Definition: SBAddress.h:15
Format
Display format definitions.
@ eFormatCString
NULL terminated C strings.
std::shared_ptr< lldb_private::Type > TypeSP
Definition: lldb-forward.h:457
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:335
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_size
Size in bytes of the register.