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 
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-public.h"
16 
21 #include "lldb/Symbol/SymbolFile.h"
22 #include "lldb/Symbol/TypeList.h"
23 #include "lldb/Target/Target.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 TypeFormatImpl::TypeFormatImpl(const Flags &flags) : m_flags(flags) {}
31 
33 
35  const TypeFormatImpl::Flags &flags)
36  : TypeFormatImpl(flags), m_format(f) {}
37 
39 
41  std::string &dest) const {
42  if (!valobj)
43  return false;
44  if (valobj->CanProvideValue()) {
45  Value &value(valobj->GetValue());
46  const Value::ContextType context_type = value.GetContextType();
47  ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
48  DataExtractor data;
49 
50  if (context_type == Value::ContextType::RegisterInfo) {
51  const RegisterInfo *reg_info = value.GetRegisterInfo();
52  if (reg_info) {
53  Status error;
54  valobj->GetData(data, error);
55  if (error.Fail())
56  return false;
57 
58  StreamString reg_sstr;
59  DumpDataExtractor(data, &reg_sstr, 0, GetFormat(), reg_info->byte_size,
62  dest = std::string(reg_sstr.GetString());
63  }
64  } else {
65  CompilerType compiler_type = value.GetCompilerType();
66  if (compiler_type) {
67  // put custom bytes to display in the DataExtractor to override the
68  // default value logic
69  if (GetFormat() == eFormatCString) {
70  lldb_private::Flags type_flags(compiler_type.GetTypeInfo(
71  nullptr)); // disambiguate w.r.t. TypeFormatImpl::Flags
72  if (type_flags.Test(eTypeIsPointer) &&
73  !type_flags.Test(eTypeIsObjC)) {
74  // if we are dumping a pointer as a c-string, get the pointee data
75  // as a string
76  TargetSP target_sp(valobj->GetTargetSP());
77  if (target_sp) {
78  size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
79  Status error;
80  DataBufferSP buffer_sp(new DataBufferHeap(max_len + 1, 0));
81  Address address(valobj->GetPointerValue());
82  if (target_sp->ReadCStringFromMemory(
83  address, (char *)buffer_sp->GetBytes(), max_len, error) &&
84  error.Success())
85  data.SetData(buffer_sp);
86  }
87  }
88  } else {
89  Status error;
90  valobj->GetData(data, error);
91  if (error.Fail())
92  return false;
93  }
94 
95  ExecutionContextScope *exe_scope =
97  llvm::Optional<uint64_t> size = compiler_type.GetByteSize(exe_scope);
98  if (!size)
99  return false;
100  StreamString sstr;
101  compiler_type.DumpTypeValue(
102  &sstr, // The stream to use for display
103  GetFormat(), // Format to display this type with
104  data, // Data to extract from
105  0, // Byte offset into "m_data"
106  *size, // Byte size of item in "m_data"
107  valobj->GetBitfieldBitSize(), // Bitfield bit size
108  valobj->GetBitfieldBitOffset(), // Bitfield bit offset
109  exe_scope);
110  // Given that we do not want to set the ValueObject's m_error for a
111  // formatting error (or else we wouldn't be able to reformat until a
112  // next update), an empty string is treated as a "false" return from
113  // here, but that's about as severe as we get
114  // CompilerType::DumpTypeValue() should always return something, even
115  // if that something is an error message
116  dest = std::string(sstr.GetString());
117  }
118  }
119  return !dest.empty();
120  } else
121  return false;
122 }
123 
125  StreamString sstr;
127  Cascades() ? "" : " (not cascading)",
128  SkipsPointers() ? " (skip pointers)" : "",
129  SkipsReferences() ? " (skip references)" : "");
130  return std::string(sstr.GetString());
131 }
132 
134  ConstString type_name, const TypeFormatImpl::Flags &flags)
135  : TypeFormatImpl(flags), m_enum_type(type_name), m_types() {}
136 
138 
140  std::string &dest) const {
141  dest.clear();
142  if (!valobj)
143  return false;
144  if (!valobj->CanProvideValue())
145  return false;
146  ProcessSP process_sp;
147  TargetSP target_sp;
148  void *valobj_key = (process_sp = valobj->GetProcessSP()).get();
149  if (!valobj_key)
150  valobj_key = (target_sp = valobj->GetTargetSP()).get();
151  else
152  target_sp = process_sp->GetTarget().shared_from_this();
153  if (!valobj_key)
154  return false;
155  auto iter = m_types.find(valobj_key), end = m_types.end();
156  CompilerType valobj_enum_type;
157  if (iter == end) {
158  // probably a redundant check
159  if (!target_sp)
160  return false;
161  const ModuleList &images(target_sp->GetImages());
162  TypeList types;
163  llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
164  images.FindTypes(nullptr, m_enum_type, false, UINT32_MAX,
165  searched_symbol_files, types);
166  if (types.Empty())
167  return false;
168  for (lldb::TypeSP type_sp : types.Types()) {
169  if (!type_sp)
170  continue;
171  if ((type_sp->GetForwardCompilerType().GetTypeInfo() &
172  eTypeIsEnumeration) == eTypeIsEnumeration) {
173  valobj_enum_type = type_sp->GetFullCompilerType();
174  m_types.emplace(valobj_key, valobj_enum_type);
175  break;
176  }
177  }
178  } else
179  valobj_enum_type = iter->second;
180  if (!valobj_enum_type.IsValid())
181  return false;
182  DataExtractor data;
183  Status error;
184  valobj->GetData(data, error);
185  if (error.Fail())
186  return false;
187  ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
188  StreamString sstr;
189  valobj_enum_type.DumpTypeValue(&sstr, lldb::eFormatEnum, data, 0,
190  data.GetByteSize(), 0, 0,
191  exe_ctx.GetBestExecutionContextScope());
192  if (!sstr.GetString().empty())
193  dest = std::string(sstr.GetString());
194  return !dest.empty();
195 }
196 
198  StreamString sstr;
199  sstr.Printf("as type %s%s%s%s", m_enum_type.AsCString("<invalid type>"),
200  Cascades() ? "" : " (not cascading)",
201  SkipsPointers() ? " (skip pointers)" : "",
202  SkipsReferences() ? " (skip references)" : "");
203  return std::string(sstr.GetString());
204 }
lldb_private::ValueObject::GetData
virtual uint64_t GetData(DataExtractor &data, Status &error)
Definition: ValueObject.cpp:765
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb-public.h
lldb_private::ValueObject::CanProvideValue
virtual bool CanProvideValue()
Definition: ValueObject.cpp:3100
lldb::Format
Format
Display format definitions.
Definition: lldb-enumerations.h:155
lldb_private::TypeFormatImpl_EnumType::~TypeFormatImpl_EnumType
~TypeFormatImpl_EnumType() override
lldb_private::ValueObject::GetValue
const Value & GetValue() const
Definition: ValueObject.h:497
lldb_private::TypeFormatImpl::~TypeFormatImpl
virtual ~TypeFormatImpl()
lldb_private::Value
Definition: Value.h:38
FormatManager.h
lldb_private::ValueObject::GetPointerValue
lldb::addr_t GetPointerValue(AddressType *address_type=nullptr)
Definition: ValueObject.cpp:1433
lldb_private::Flags
Definition: Flags.h:22
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
lldb_private::CompilerType::GetByteSize
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
Definition: CompilerType.cpp:489
lldb_private::Flags::Test
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
lldb_private::ValueObject::GetBitfieldBitSize
virtual uint32_t GetBitfieldBitSize()
Definition: ValueObject.h:424
lldb_private::DataExtractor::SetData
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
Definition: DataExtractor.cpp:225
lldb_private::TypeFormatImpl_EnumType::FormatObject
bool FormatObject(ValueObject *valobj, std::string &dest) const override
Definition: TypeFormat.cpp:139
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
lldb_private::TypeFormatImpl_Format::~TypeFormatImpl_Format
~TypeFormatImpl_Format() override
lldb_private::TypeFormatImpl::SkipsPointers
bool SkipsPointers() const
Definition: TypeFormat.h:118
lldb_private::Value::GetRegisterInfo
RegisterInfo * GetRegisterInfo() const
Definition: Value.cpp:126
lldb_private::TypeFormatImpl
Definition: TypeFormat.h:24
Target.h
lldb_private::ValueObject::GetProcessSP
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:338
lldb_private::TypeFormatImpl::Cascades
bool Cascades() const
Definition: TypeFormat.h:116
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::TypeFormatImpl::SkipsReferences
bool SkipsReferences() const
Definition: TypeFormat.h:120
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::ModuleList
Definition: ModuleList.h:72
lldb_private::TypeList::Empty
bool Empty() const
Definition: TypeList.h:37
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::FormatManager::GetFormatAsCString
static const char * GetFormatAsCString(lldb::Format format)
Definition: FormatManager.cpp:152
StreamString.h
lldb_private::TypeFormatImpl_EnumType::m_types
std::unordered_map< void *, CompilerType > m_types
Definition: TypeFormat.h:212
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::TypeFormatImpl_EnumType::m_enum_type
ConstString m_enum_type
Definition: TypeFormat.h:211
lldb-enumerations.h
lldb_private::TypeFormatImpl_Format::GetFormat
lldb::Format GetFormat() const
Definition: TypeFormat.h:168
lldb_private::ValueObject::GetExecutionContextRef
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
CompilerType.h
DumpDataExtractor.h
lldb_private::CompilerType::IsValid
bool IsValid() const
Definition: CompilerType.h:72
lldb_private::ValueObject::GetBitfieldBitOffset
virtual uint32_t GetBitfieldBitOffset()
Definition: ValueObject.h:426
TypeList.h
lldb_private::Status
Definition: Status.h:44
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb_private::TypeFormatImpl::Flags
Definition: TypeFormat.h:26
lldb_private::ExecutionContext::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: ExecutionContext.cpp:216
lldb_private::TypeFormatImpl_EnumType::GetDescription
std::string GetDescription() override
Definition: TypeFormat.cpp:197
lldb_private::TypeFormatImpl_Format::GetDescription
std::string GetDescription() override
Definition: TypeFormat.cpp:124
lldb_private::Address
Definition: Address.h:59
lldb::eFormatCString
@ eFormatCString
NULL terminated C strings.
Definition: lldb-enumerations.h:166
lldb_private::CompilerType::GetTypeInfo
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
Definition: CompilerType.cpp:290
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Value::GetContextType
ContextType GetContextType() const
Definition: Value.h:87
TypeFormat.h
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::DumpDataExtractor
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)
Dumps item_count objects into the stream s.
Definition: DumpDataExtractor.cpp:256
lldb_private::Value::GetCompilerType
const CompilerType & GetCompilerType()
Definition: Value.cpp:225
lldb_private::Value::ContextType::RegisterInfo
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
lldb_private::TypeFormatImpl_EnumType::TypeFormatImpl_EnumType
TypeFormatImpl_EnumType(ConstString type_name=ConstString(""), const TypeFormatImpl::Flags &flags=Flags())
Definition: TypeFormat.cpp:133
SymbolContext.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::TypeFormatImpl_Format::TypeFormatImpl_Format
TypeFormatImpl_Format(lldb::Format f=lldb::eFormatInvalid, const TypeFormatImpl::Flags &flags=Flags())
Definition: TypeFormat.cpp:34
lldb_private::Value::ContextType
ContextType
Type that describes Value::m_context.
Definition: Value.h:56
lldb_private::TypeList
Definition: TypeList.h:20
lldb::eFormatEnum
@ eFormatEnum
Definition: lldb-enumerations.h:168
lldb_private::TypeFormatImpl_Format::FormatObject
bool FormatObject(ValueObject *valobj, std::string &dest) const override
Definition: TypeFormat.cpp:40
lldb_private::ValueObject::GetTargetSP
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
lldb_private::CompilerType::DumpTypeValue
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)
Definition: CompilerType.cpp:737
lldb_private::TypeList::Types
TypeIterable Types()
Definition: TypeList.h:45
lldb_private::ModuleList::FindTypes
void FindTypes(Module *search_first, ConstString name, bool name_is_fully_qualified, size_t max_matches, llvm::DenseSet< SymbolFile * > &searched_symbol_files, TypeList &types) const
Find types by name.
Definition: ModuleList.cpp:526
lldb_private::DataExtractor::GetByteSize
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
Definition: DataExtractor.h:270
lldb_private::DataBufferHeap
Definition: DataBufferHeap.h:30
lldb
Definition: SBAddress.h:15
SymbolFile.h