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/lldb-python.h"
16 // Other libraries and framework includes
19 #include "lldb/lldb-public.h"
20 #include "lldb/lldb-enumerations.h"
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Core/Timer.h"
25 #include "lldb/DataFormatters/TypeFormat.h"
26 #include "lldb/Interpreter/CommandInterpreter.h"
27 #include "lldb/Symbol/ClangASTType.h"
28 #include "lldb/Symbol/TypeList.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
33 using namespace lldb_private;
35 TypeFormatImpl::TypeFormatImpl (const Flags& flags) :
41 TypeFormatImpl::~TypeFormatImpl ()
45 TypeFormatImpl_Format::TypeFormatImpl_Format (lldb::Format f,
46 const TypeFormatImpl::Flags& flags) :
47 TypeFormatImpl(flags),
52 TypeFormatImpl_Format::~TypeFormatImpl_Format ()
57 TypeFormatImpl_Format::FormatObject (ValueObject *valobj,
58 std::string& dest) const
62 if (valobj->GetClangType().IsAggregateType () == false)
64 const Value& value(valobj->GetValue());
65 const Value::ContextType context_type = value.GetContextType();
66 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
69 if (context_type == Value::eContextTypeRegisterInfo)
71 const RegisterInfo *reg_info = value.GetRegisterInfo();
75 valobj->GetData(data, error);
79 StreamString reg_sstr;
89 exe_ctx.GetBestExecutionContextScope());
90 dest.swap(reg_sstr.GetString());
95 ClangASTType clang_type = valobj->GetClangType ();
98 // put custom bytes to display in the DataExtractor to override the default value logic
99 if (GetFormat() == eFormatCString)
101 lldb_private::Flags type_flags(clang_type.GetTypeInfo(NULL)); // disambiguate w.r.t. TypeFormatImpl::Flags
102 if (type_flags.Test(ClangASTType::eTypeIsPointer) && !type_flags.Test(ClangASTType::eTypeIsObjC))
104 // if we are dumping a pointer as a c-string, get the pointee data as a string
105 TargetSP target_sp(valobj->GetTargetSP());
108 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
110 DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
111 Address address(valobj->GetPointerValue());
112 if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
113 data.SetData(buffer_sp);
120 valobj->GetData(data, error);
126 clang_type.DumpTypeValue (&sstr, // The stream to use for display
127 GetFormat(), // Format to display this type with
128 data, // Data to extract from
129 0, // Byte offset into "m_data"
130 valobj->GetByteSize(), // Byte size of item in "m_data"
131 valobj->GetBitfieldBitSize(), // Bitfield bit size
132 valobj->GetBitfieldBitOffset(), // Bitfield bit offset
133 exe_ctx.GetBestExecutionContextScope());
134 // Given that we do not want to set the ValueObject's m_error
135 // for a formatting error (or else we wouldn't be able to reformat
136 // until a next update), an empty string is treated as a "false"
137 // return from here, but that's about as severe as we get
138 // ClangASTType::DumpTypeValue() should always return
139 // something, even if that something is an error message
140 if (sstr.GetString().empty())
143 dest.swap(sstr.GetString());
146 return !dest.empty();
153 TypeFormatImpl_Format::GetDescription()
156 sstr.Printf ("%s%s%s%s",
157 FormatManager::GetFormatAsCString (GetFormat()),
158 Cascades() ? "" : " (not cascading)",
159 SkipsPointers() ? " (skip pointers)" : "",
160 SkipsReferences() ? " (skip references)" : "");
161 return sstr.GetString();
164 TypeFormatImpl_EnumType::TypeFormatImpl_EnumType (ConstString type_name,
165 const TypeFormatImpl::Flags& flags) :
166 TypeFormatImpl(flags),
167 m_enum_type(type_name),
172 TypeFormatImpl_EnumType::~TypeFormatImpl_EnumType ()
177 TypeFormatImpl_EnumType::FormatObject (ValueObject *valobj,
178 std::string& dest) const
183 if (valobj->GetClangType().IsAggregateType ())
185 ProcessSP process_sp;
187 void* valobj_key = (process_sp = valobj->GetProcessSP()).get();
189 valobj_key = (target_sp = valobj->GetTargetSP()).get();
191 target_sp = process_sp->GetTarget().shared_from_this();
194 auto iter = m_types.find(valobj_key),
196 ClangASTType valobj_enum_type;
199 // probably a redundant check
202 const ModuleList& images(target_sp->GetImages());
205 images.FindTypes(sc, m_enum_type, false, UINT32_MAX, types);
206 if (types.GetSize() == 0)
208 for (lldb::TypeSP type_sp : types.Types())
212 if ( (type_sp->GetClangForwardType().GetTypeInfo() & ClangASTType::eTypeIsEnumeration) == ClangASTType::eTypeIsEnumeration)
214 valobj_enum_type = type_sp->GetClangFullType();
215 m_types.emplace(valobj_key,valobj_enum_type);
221 valobj_enum_type = iter->second;
222 if (valobj_enum_type.IsValid() == false)
226 valobj->GetData(data, error);
229 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
231 valobj_enum_type.DumpTypeValue(&sstr,
238 exe_ctx.GetBestExecutionContextScope());
239 if (!sstr.GetString().empty())
240 dest.swap(sstr.GetString());
241 return !dest.empty();
245 TypeFormatImpl_EnumType::GetDescription()
248 sstr.Printf ("as type %s%s%s%s",
249 m_enum_type.AsCString("<invalid type>"),
250 Cascades() ? "" : " (not cascading)",
251 SkipsPointers() ? " (skip pointers)" : "",
252 SkipsReferences() ? " (skip references)" : "");
253 return sstr.GetString();