1 //===-- TypeFormat.cpp ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/DataFormatters/TypeFormat.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-public.h"
17 #include "lldb/Core/DumpDataExtractor.h"
18 #include "lldb/DataFormatters/FormatManager.h"
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolFile.h"
22 #include "lldb/Symbol/TypeList.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/DataExtractor.h"
25 #include "lldb/Utility/StreamString.h"
28 using namespace lldb_private;
30 TypeFormatImpl::TypeFormatImpl(const Flags &flags)
31 : m_flags(flags), m_my_revision(0) {}
33 TypeFormatImpl::~TypeFormatImpl() {}
35 TypeFormatImpl_Format::TypeFormatImpl_Format(lldb::Format f,
36 const TypeFormatImpl::Flags &flags)
37 : TypeFormatImpl(flags), m_format(f) {}
39 TypeFormatImpl_Format::~TypeFormatImpl_Format() {}
41 bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj,
42 std::string &dest) const {
45 if (valobj->CanProvideValue()) {
46 Value &value(valobj->GetValue());
47 const Value::ContextType context_type = value.GetContextType();
48 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
51 if (context_type == Value::eContextTypeRegisterInfo) {
52 const RegisterInfo *reg_info = value.GetRegisterInfo();
55 valobj->GetData(data, error);
59 StreamString reg_sstr;
60 DumpDataExtractor(data, ®_sstr, 0, GetFormat(), reg_info->byte_size,
61 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0,
62 exe_ctx.GetBestExecutionContextScope());
63 dest = reg_sstr.GetString();
66 CompilerType compiler_type = value.GetCompilerType();
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
77 TargetSP target_sp(valobj->GetTargetSP());
79 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
81 DataBufferSP buffer_sp(new DataBufferHeap(max_len + 1, 0));
82 Address address(valobj->GetPointerValue());
83 if (target_sp->ReadCStringFromMemory(
84 address, (char *)buffer_sp->GetBytes(), max_len, error) &&
86 data.SetData(buffer_sp);
91 valobj->GetData(data, error);
96 ExecutionContextScope *exe_scope =
97 exe_ctx.GetBestExecutionContextScope();
98 llvm::Optional<uint64_t> size = compiler_type.GetByteSize(exe_scope);
102 compiler_type.DumpTypeValue(
103 &sstr, // The stream to use for display
104 GetFormat(), // Format to display this type with
105 data, // Data to extract from
106 0, // Byte offset into "m_data"
107 *size, // Byte size of item in "m_data"
108 valobj->GetBitfieldBitSize(), // Bitfield bit size
109 valobj->GetBitfieldBitOffset(), // Bitfield bit offset
111 // Given that we do not want to set the ValueObject's m_error for a
112 // formatting error (or else we wouldn't be able to reformat until a
113 // next update), an empty string is treated as a "false" return from
114 // here, but that's about as severe as we get
115 // CompilerType::DumpTypeValue() should always return something, even
116 // if that something is an error message
117 dest = sstr.GetString();
120 return !dest.empty();
125 std::string TypeFormatImpl_Format::GetDescription() {
127 sstr.Printf("%s%s%s%s", FormatManager::GetFormatAsCString(GetFormat()),
128 Cascades() ? "" : " (not cascading)",
129 SkipsPointers() ? " (skip pointers)" : "",
130 SkipsReferences() ? " (skip references)" : "");
131 return sstr.GetString();
134 TypeFormatImpl_EnumType::TypeFormatImpl_EnumType(
135 ConstString type_name, const TypeFormatImpl::Flags &flags)
136 : TypeFormatImpl(flags), m_enum_type(type_name), m_types() {}
138 TypeFormatImpl_EnumType::~TypeFormatImpl_EnumType() {}
140 bool TypeFormatImpl_EnumType::FormatObject(ValueObject *valobj,
141 std::string &dest) const {
145 if (!valobj->CanProvideValue())
147 ProcessSP process_sp;
149 void *valobj_key = (process_sp = valobj->GetProcessSP()).get();
151 valobj_key = (target_sp = valobj->GetTargetSP()).get();
153 target_sp = process_sp->GetTarget().shared_from_this();
156 auto iter = m_types.find(valobj_key), end = m_types.end();
157 CompilerType valobj_enum_type;
159 // probably a redundant check
162 const ModuleList &images(target_sp->GetImages());
164 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
165 images.FindTypes(nullptr, m_enum_type, false, UINT32_MAX,
166 searched_symbol_files, types);
167 if (types.GetSize() == 0)
169 for (lldb::TypeSP type_sp : types.Types()) {
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);
180 valobj_enum_type = iter->second;
181 if (!valobj_enum_type.IsValid())
185 valobj->GetData(data, error);
188 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
190 valobj_enum_type.DumpTypeValue(&sstr, lldb::eFormatEnum, data, 0,
191 data.GetByteSize(), 0, 0,
192 exe_ctx.GetBestExecutionContextScope());
193 if (!sstr.GetString().empty())
194 dest = sstr.GetString();
195 return !dest.empty();
198 std::string TypeFormatImpl_EnumType::GetDescription() {
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 sstr.GetString();