LLDB  mainline
NSError.cpp
Go to the documentation of this file.
1 //===-- NSError.cpp ---------------------------------------------*- C++ -*-===//
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 
9 #include "clang/AST/DeclCXX.h"
10 
11 #include "Cocoa.h"
12 
13 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/Endian.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/Stream.h"
24 
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace lldb_private::formatters;
30 
32  CompilerType valobj_type(valobj.GetCompilerType());
33  Flags type_flags(valobj_type.GetTypeInfo());
34  if (type_flags.AllClear(eTypeHasValue)) {
35  if (valobj.IsBaseClass() && valobj.GetParent())
37  } else {
39  if (type_flags.AllSet(eTypeIsPointer)) {
40  CompilerType pointee_type(valobj_type.GetPointeeType());
41  Flags pointee_flags(pointee_type.GetTypeInfo());
42  if (pointee_flags.AllSet(eTypeIsPointer)) {
43  if (ProcessSP process_sp = valobj.GetProcessSP()) {
44  Status error;
45  ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error);
46  }
47  }
48  }
49  return ptr_value;
50  }
51 
52  return LLDB_INVALID_ADDRESS;
53 }
54 
56  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
57  ProcessSP process_sp(valobj.GetProcessSP());
58  if (!process_sp)
59  return false;
60 
61  lldb::addr_t ptr_value = DerefToNSErrorPointer(valobj);
62  if (ptr_value == LLDB_INVALID_ADDRESS)
63  return false;
64 
65  size_t ptr_size = process_sp->GetAddressByteSize();
66  lldb::addr_t code_location = ptr_value + 2 * ptr_size;
67  lldb::addr_t domain_location = ptr_value + 3 * ptr_size;
68 
69  Status error;
70  uint64_t code = process_sp->ReadUnsignedIntegerFromMemory(code_location,
71  ptr_size, 0, error);
72  if (error.Fail())
73  return false;
74 
75  lldb::addr_t domain_str_value =
76  process_sp->ReadPointerFromMemory(domain_location, error);
77  if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS)
78  return false;
79 
80  if (!domain_str_value) {
81  stream.Printf("domain: nil - code: %" PRIu64, code);
82  return true;
83  }
84 
85  InferiorSizedWord isw(domain_str_value, *process_sp);
86 
87  ValueObjectSP domain_str_sp = ValueObject::CreateValueObjectFromData(
88  "domain_str", isw.GetAsData(process_sp->GetByteOrder()),
89  valobj.GetExecutionContextRef(), process_sp->GetTarget()
90  .GetScratchClangASTContext()
91  ->GetBasicType(lldb::eBasicTypeVoid)
92  .GetPointerType());
93 
94  if (!domain_str_sp)
95  return false;
96 
97  StreamString domain_str_summary;
98  if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) &&
99  !domain_str_summary.Empty()) {
100  stream.Printf("domain: %s - code: %" PRIu64, domain_str_summary.GetData(),
101  code);
102  return true;
103  } else {
104  stream.Printf("domain: nil - code: %" PRIu64, code);
105  return true;
106  }
107 }
108 
110 public:
111  NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
112  : SyntheticChildrenFrontEnd(*valobj_sp) {}
113 
114  ~NSErrorSyntheticFrontEnd() override = default;
115  // no need to delete m_child_ptr - it's kept alive by the cluster manager on
116  // our behalf
117 
118  size_t CalculateNumChildren() override {
119  if (m_child_ptr)
120  return 1;
121  if (m_child_sp)
122  return 1;
123  return 0;
124  }
125 
126  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
127  if (idx != 0)
128  return lldb::ValueObjectSP();
129 
130  if (m_child_ptr)
131  return m_child_ptr->GetSP();
132  return m_child_sp;
133  }
134 
135  bool Update() override {
136  m_child_ptr = nullptr;
137  m_child_sp.reset();
138 
139  ProcessSP process_sp(m_backend.GetProcessSP());
140  if (!process_sp)
141  return false;
142 
143  lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend);
144  if (userinfo_location == LLDB_INVALID_ADDRESS)
145  return false;
146 
147  size_t ptr_size = process_sp->GetAddressByteSize();
148 
149  userinfo_location += 4 * ptr_size;
150  Status error;
151  lldb::addr_t userinfo =
152  process_sp->ReadPointerFromMemory(userinfo_location, error);
153  if (userinfo == LLDB_INVALID_ADDRESS || error.Fail())
154  return false;
155  InferiorSizedWord isw(userinfo, *process_sp);
156  m_child_sp = CreateValueObjectFromData(
157  "_userInfo", isw.GetAsData(process_sp->GetByteOrder()),
158  m_backend.GetExecutionContextRef(),
159  process_sp->GetTarget().GetScratchClangASTContext()->GetBasicType(
161  return false;
162  }
163 
164  bool MightHaveChildren() override { return true; }
165 
166  size_t GetIndexOfChildWithName(ConstString name) override {
167  static ConstString g___userInfo("_userInfo");
168  if (name == g___userInfo)
169  return 0;
170  return UINT32_MAX;
171  }
172 
173 private:
174  // the child here can be "real" (i.e. an actual child of the root) or
175  // synthetized from raw memory if the former, I need to store a plain pointer
176  // to it - or else a loop of references will cause this entire hierarchy of
177  // values to leak if the latter, then I need to store a SharedPointer to it -
178  // so that it only goes away when everyone else in the cluster goes away oh
179  // joy!
180  ValueObject *m_child_ptr;
181  ValueObjectSP m_child_sp;
182 };
183 
186  CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
187  lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
188  if (!process_sp)
189  return nullptr;
190  ObjCLanguageRuntime *runtime =
191  (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
193  if (!runtime)
194  return nullptr;
195 
197  runtime->GetClassDescriptor(*valobj_sp.get()));
198 
199  if (!descriptor.get() || !descriptor->IsValid())
200  return nullptr;
201 
202  const char *class_name = descriptor->GetClassName().GetCString();
203 
204  if (!class_name || !*class_name)
205  return nullptr;
206 
207  if (!strcmp(class_name, "NSError"))
208  return (new NSErrorSyntheticFrontEnd(valobj_sp));
209  else if (!strcmp(class_name, "__NSCFError"))
210  return (new NSErrorSyntheticFrontEnd(valobj_sp));
211 
212  return nullptr;
213 }
virtual bool IsBaseClass()
Definition: ValueObject.h:413
bool MightHaveChildren() override
Definition: NSError.cpp:164
CompilerType GetCompilerType()
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t GetIndexOfChildWithName(ConstString name) override
Definition: NSError.cpp:166
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
const char * GetData() const
Definition: StreamString.h:43
bool NSError_SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: NSError.cpp:55
bool Update() override
Definition: NSError.cpp:135
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
Definition: NSError.cpp:126
#define UINT32_MAX
Definition: lldb-defines.h:31
virtual ValueObject * GetParent()
Definition: ValueObject.h:771
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
size_t CalculateNumChildren() override
Definition: NSError.cpp:118
bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: NSString.cpp:53
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:353
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
DataExtractor GetAsData(lldb::ByteOrder byte_order=lldb::eByteOrderInvalid) const
A class to manage flags.
Definition: Flags.h:22
uint64_t addr_t
Definition: lldb-types.h:83
A uniqued constant string class.
Definition: ConstString.h:38
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
SyntheticChildrenFrontEnd * NSErrorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
Definition: NSError.cpp:185
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:361
NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: NSError.cpp:111
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
Definition: SBAddress.h:15
virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value)
static lldb::addr_t DerefToNSErrorPointer(ValueObject &valobj)
Definition: NSError.cpp:31
An error handling class.
Definition: Status.h:44