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
32
34
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().address);
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 auto size_or_err = compiler_type.GetByteSize(exe_scope);
100 if (!size_or_err) {
102 GetLog(LLDBLog::Types), size_or_err.takeError(),
103 "Cannot get size of type while formatting object: {0}");
104 return false;
105 }
106 StreamString sstr;
107 compiler_type.DumpTypeValue(
108 &sstr, // The stream to use for display
109 GetFormat(), // Format to display this type with
110 data, // Data to extract from
111 0, // Byte offset into "m_data"
112 *size_or_err, // Byte size of item in "m_data"
113 valobj->GetBitfieldBitSize(), // Bitfield bit size
114 valobj->GetBitfieldBitOffset(), // Bitfield bit offset
115 exe_scope);
116 // Given that we do not want to set the ValueObject's m_error for a
117 // formatting error (or else we wouldn't be able to reformat until a
118 // next update), an empty string is treated as a "false" return from
119 // here, but that's about as severe as we get
120 // CompilerType::DumpTypeValue() should always return something, even
121 // if that something is an error message
122 dest = std::string(sstr.GetString());
123 }
124 }
125 return !dest.empty();
126 } else
127 return false;
128}
129
131 StreamString sstr;
133 Cascades() ? "" : " (not cascading)",
134 SkipsPointers() ? " (skip pointers)" : "",
135 SkipsReferences() ? " (skip references)" : "");
136 return std::string(sstr.GetString());
137}
138
142
144
146 std::string &dest) const {
147 dest.clear();
148 if (!valobj)
149 return false;
150 if (!valobj->CanProvideValue())
151 return false;
152 ProcessSP process_sp;
153 TargetSP target_sp;
154 void *valobj_key = (process_sp = valobj->GetProcessSP()).get();
155 if (!valobj_key)
156 valobj_key = (target_sp = valobj->GetTargetSP()).get();
157 else
158 target_sp = process_sp->GetTarget().shared_from_this();
159 if (!valobj_key)
160 return false;
161 auto iter = m_types.find(valobj_key), end = m_types.end();
162 CompilerType valobj_enum_type;
163 if (iter == end) {
164 // probably a redundant check
165 if (!target_sp)
166 return false;
167 const ModuleList &images(target_sp->GetImages());
168 TypeQuery query(m_enum_type.GetStringRef());
169 TypeResults results;
170 images.FindTypes(nullptr, query, results);
171 if (results.GetTypeMap().Empty())
172 return false;
173 for (lldb::TypeSP type_sp : results.GetTypeMap().Types()) {
174 if (!type_sp)
175 continue;
176 if ((type_sp->GetForwardCompilerType().GetTypeInfo() &
177 eTypeIsEnumeration) == eTypeIsEnumeration) {
178 valobj_enum_type = type_sp->GetFullCompilerType();
179 m_types.emplace(valobj_key, valobj_enum_type);
180 break;
181 }
182 }
183 } else
184 valobj_enum_type = iter->second;
185 if (!valobj_enum_type.IsValid())
186 return false;
187 DataExtractor data;
189 valobj->GetData(data, error);
190 if (error.Fail())
191 return false;
192 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
193 StreamString sstr;
194 valobj_enum_type.DumpTypeValue(&sstr, lldb::eFormatEnum, data, 0,
195 data.GetByteSize(), 0, 0,
197 if (!sstr.GetString().empty())
198 dest = std::string(sstr.GetString());
199 return !dest.empty();
200}
201
203 StreamString sstr;
204 sstr.Printf("as type %s%s%s%s", m_enum_type.AsCString("<invalid type>"),
205 Cascades() ? "" : " (not cascading)",
206 SkipsPointers() ? " (skip pointers)" : "",
207 SkipsReferences() ? " (skip references)" : "");
208 return std::string(sstr.GetString());
209}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:408
A section + offset based address class.
Definition Address.h:62
Generic representation of a type in a programming language.
llvm::Expected< 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
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
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:104
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.
An error handling class.
Definition Status.h:118
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
std::string GetDescription() override
std::unordered_map< void *, CompilerType > m_types
Definition TypeFormat.h:217
TypeFormatImpl_EnumType(ConstString type_name=ConstString(""), const TypeFormatImpl::Flags &flags=Flags())
bool FormatObject(ValueObject *valobj, std::string &dest) const override
TypeFormatImpl_Format(lldb::Format f=lldb::eFormatInvalid, const TypeFormatImpl::Flags &flags=Flags())
std::string GetDescription() override
TypeFormatImpl(const Flags &flags=Flags())
TypeIterable Types() const
Definition TypeMap.h:50
bool Empty() const
Definition TypeMap.cpp:77
A class that contains all state required for type lookups.
Definition Type.h:104
This class tracks the state and results of a TypeQuery.
Definition Type.h:344
TypeMap & GetTypeMap()
Definition Type.h:386
virtual uint32_t GetBitfieldBitSize()
virtual uint64_t GetData(DataExtractor &data, Status &error)
lldb::ProcessSP GetProcessSP() const
lldb::TargetSP GetTargetSP() const
virtual uint32_t GetBitfieldBitOffset()
const ExecutionContextRef & GetExecutionContextRef() const
const Value & GetValue() const
RegisterInfo * GetRegisterInfo() const
Definition Value.cpp:142
ContextType
Type that describes Value::m_context.
Definition Value.h:56
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
Definition Value.h:61
ContextType GetContextType() const
Definition Value.h:87
const CompilerType & GetCompilerType()
Definition Value.cpp:247
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
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
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.
Format
Display format definitions.
@ eFormatCString
NULL terminated C strings.
std::shared_ptr< lldb_private::Type > TypeSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
std::shared_ptr< lldb_private::Target > TargetSP
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_size
Size in bytes of the register.