Coverage Report

Created: 2023-09-21 18:56

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