1 //===-- TypeFormat.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/DataFormatters/TypeFormat.h"
15 #include "lldb/lldb-enumerations.h"
16 #include "lldb/lldb-public.h"
18 #include "lldb/Core/DumpDataExtractor.h"
19 #include "lldb/DataFormatters/FormatManager.h"
20 #include "lldb/Symbol/CompilerType.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Symbol/SymbolFile.h"
23 #include "lldb/Symbol/TypeList.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/DataExtractor.h"
26 #include "lldb/Utility/StreamString.h"
29 using namespace lldb_private;
31 TypeFormatImpl::TypeFormatImpl(const Flags &flags)
32 : m_flags(flags), m_my_revision(0) {}
34 TypeFormatImpl::~TypeFormatImpl() {}
36 TypeFormatImpl_Format::TypeFormatImpl_Format(lldb::Format f,
37 const TypeFormatImpl::Flags &flags)
38 : TypeFormatImpl(flags), m_format(f) {}
40 TypeFormatImpl_Format::~TypeFormatImpl_Format() {}
42 bool TypeFormatImpl_Format::FormatObject(ValueObject *valobj,
43 std::string &dest) const {
46 if (valobj->CanProvideValue()) {
47 Value &value(valobj->GetValue());
48 const Value::ContextType context_type = value.GetContextType();
49 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
52 if (context_type == Value::eContextTypeRegisterInfo) {
53 const RegisterInfo *reg_info = value.GetRegisterInfo();
56 valobj->GetData(data, error);
60 StreamString reg_sstr;
61 DumpDataExtractor(data, ®_sstr, 0, GetFormat(), reg_info->byte_size,
62 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0,
63 exe_ctx.GetBestExecutionContextScope());
64 dest = reg_sstr.GetString();
67 CompilerType compiler_type = value.GetCompilerType();
69 // put custom bytes to display in the DataExtractor to override the
70 // default value logic
71 if (GetFormat() == eFormatCString) {
72 lldb_private::Flags type_flags(compiler_type.GetTypeInfo(
73 NULL)); // disambiguate w.r.t. TypeFormatImpl::Flags
74 if (type_flags.Test(eTypeIsPointer) &&
75 !type_flags.Test(eTypeIsObjC)) {
76 // if we are dumping a pointer as a c-string, get the pointee data
78 TargetSP target_sp(valobj->GetTargetSP());
80 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
82 DataBufferSP buffer_sp(new DataBufferHeap(max_len + 1, 0));
83 Address address(valobj->GetPointerValue());
84 if (target_sp->ReadCStringFromMemory(
85 address, (char *)buffer_sp->GetBytes(), max_len, error) &&
87 data.SetData(buffer_sp);
92 valobj->GetData(data, error);
97 ExecutionContextScope *exe_scope =
98 exe_ctx.GetBestExecutionContextScope();
99 llvm::Optional<uint64_t> size = compiler_type.GetByteSize(exe_scope);
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
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 = sstr.GetString();
121 return !dest.empty();
126 std::string TypeFormatImpl_Format::GetDescription() {
128 sstr.Printf("%s%s%s%s", FormatManager::GetFormatAsCString(GetFormat()),
129 Cascades() ? "" : " (not cascading)",
130 SkipsPointers() ? " (skip pointers)" : "",
131 SkipsReferences() ? " (skip references)" : "");
132 return sstr.GetString();
135 TypeFormatImpl_EnumType::TypeFormatImpl_EnumType(
136 ConstString type_name, const TypeFormatImpl::Flags &flags)
137 : TypeFormatImpl(flags), m_enum_type(type_name), m_types() {}
139 TypeFormatImpl_EnumType::~TypeFormatImpl_EnumType() {}
141 bool TypeFormatImpl_EnumType::FormatObject(ValueObject *valobj,
142 std::string &dest) const {
146 if (!valobj->CanProvideValue())
148 ProcessSP process_sp;
150 void *valobj_key = (process_sp = valobj->GetProcessSP()).get();
152 valobj_key = (target_sp = valobj->GetTargetSP()).get();
154 target_sp = process_sp->GetTarget().shared_from_this();
157 auto iter = m_types.find(valobj_key), end = m_types.end();
158 CompilerType valobj_enum_type;
160 // probably a redundant check
163 const ModuleList &images(target_sp->GetImages());
165 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
166 images.FindTypes(nullptr, m_enum_type, false, UINT32_MAX,
167 searched_symbol_files, types);
168 if (types.GetSize() == 0)
170 for (lldb::TypeSP type_sp : types.Types()) {
173 if ((type_sp->GetForwardCompilerType().GetTypeInfo() &
174 eTypeIsEnumeration) == eTypeIsEnumeration) {
175 valobj_enum_type = type_sp->GetFullCompilerType();
176 m_types.emplace(valobj_key, valobj_enum_type);
181 valobj_enum_type = iter->second;
182 if (!valobj_enum_type.IsValid())
186 valobj->GetData(data, error);
189 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
191 valobj_enum_type.DumpTypeValue(&sstr, lldb::eFormatEnum, data, 0,
192 data.GetByteSize(), 0, 0,
193 exe_ctx.GetBestExecutionContextScope());
194 if (!sstr.GetString().empty())
195 dest = sstr.GetString();
196 return !dest.empty();
199 std::string TypeFormatImpl_EnumType::GetDescription() {
201 sstr.Printf("as type %s%s%s%s", m_enum_type.AsCString("<invalid type>"),
202 Cascades() ? "" : " (not cascading)",
203 SkipsPointers() ? " (skip pointers)" : "",
204 SkipsReferences() ? " (skip references)" : "");
205 return sstr.GetString();