Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Language/ObjC/NSException.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- NSException.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 "lldb/Core/ValueObject.h"
14
#include "lldb/Core/ValueObjectConstResult.h"
15
#include "lldb/DataFormatters/FormattersHelpers.h"
16
#include "lldb/Target/ProcessStructReader.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
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
26
27
using namespace lldb;
28
using namespace lldb_private;
29
using namespace lldb_private::formatters;
30
31
static bool ExtractFields(ValueObject &valobj, ValueObjectSP *name_sp,
32
                          ValueObjectSP *reason_sp, ValueObjectSP *userinfo_sp,
33
156
                          ValueObjectSP *reserved_sp) {
34
156
  ProcessSP process_sp(valobj.GetProcessSP());
35
156
  if (!process_sp)
36
0
    return false;
37
38
156
  lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
39
40
156
  CompilerType valobj_type(valobj.GetCompilerType());
41
156
  Flags type_flags(valobj_type.GetTypeInfo());
42
156
  if (type_flags.AllClear(eTypeHasValue)) {
43
6
    if (valobj.IsBaseClass() && 
valobj.GetParent()0
)
44
0
      ptr = valobj.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
45
150
  } else {
46
150
    ptr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
47
150
  }
48
49
156
  if (ptr == LLDB_INVALID_ADDRESS)
50
6
    return false;
51
150
  size_t ptr_size = process_sp->GetAddressByteSize();
52
53
150
  Status error;
54
150
  auto name = process_sp->ReadPointerFromMemory(ptr + 1 * ptr_size, error);
55
150
  if (error.Fail() || name == LLDB_INVALID_ADDRESS)
56
0
    return false;
57
150
  auto reason = process_sp->ReadPointerFromMemory(ptr + 2 * ptr_size, error);
58
150
  if (error.Fail() || reason == LLDB_INVALID_ADDRESS)
59
0
    return false;
60
150
  auto userinfo = process_sp->ReadPointerFromMemory(ptr + 3 * ptr_size, error);
61
150
  if (error.Fail() || userinfo == LLDB_INVALID_ADDRESS)
62
0
    return false;
63
150
  auto reserved = process_sp->ReadPointerFromMemory(ptr + 4 * ptr_size, error);
64
150
  if (error.Fail() || reserved == LLDB_INVALID_ADDRESS)
65
0
    return false;
66
67
150
  InferiorSizedWord name_isw(name, *process_sp);
68
150
  InferiorSizedWord reason_isw(reason, *process_sp);
69
150
  InferiorSizedWord userinfo_isw(userinfo, *process_sp);
70
150
  InferiorSizedWord reserved_isw(reserved, *process_sp);
71
72
150
  auto *clang_ast_context =
73
150
      ScratchTypeSystemClang::GetForTarget(process_sp->GetTarget());
74
150
  if (!clang_ast_context)
75
0
    return false;
76
77
150
  CompilerType voidstar =
78
150
      clang_ast_context->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
79
80
150
  if (name_sp)
81
84
    *name_sp = ValueObject::CreateValueObjectFromData(
82
84
        "name", name_isw.GetAsData(process_sp->GetByteOrder()),
83
84
        valobj.GetExecutionContextRef(), voidstar);
84
150
  if (reason_sp)
85
150
    *reason_sp = ValueObject::CreateValueObjectFromData(
86
150
        "reason", reason_isw.GetAsData(process_sp->GetByteOrder()),
87
150
        valobj.GetExecutionContextRef(), voidstar);
88
150
  if (userinfo_sp)
89
84
    *userinfo_sp = ValueObject::CreateValueObjectFromData(
90
84
        "userInfo", userinfo_isw.GetAsData(process_sp->GetByteOrder()),
91
84
        valobj.GetExecutionContextRef(), voidstar);
92
150
  if (reserved_sp)
93
84
    *reserved_sp = ValueObject::CreateValueObjectFromData(
94
84
        "reserved", reserved_isw.GetAsData(process_sp->GetByteOrder()),
95
84
        valobj.GetExecutionContextRef(), voidstar);
96
97
150
  return true;
98
150
}
99
100
bool lldb_private::formatters::NSException_SummaryProvider(
101
72
    ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
102
72
  lldb::ValueObjectSP reason_sp;
103
72
  if (!ExtractFields(valobj, nullptr, &reason_sp, nullptr, nullptr))
104
6
    return false;
105
106
66
  if (!reason_sp) {
107
0
    stream.Printf("No reason");
108
0
    return false;
109
0
  }
110
111
66
  StreamString reason_str_summary;
112
66
  if (NSStringSummaryProvider(*reason_sp, reason_str_summary, options) &&
113
66
      !reason_str_summary.Empty()) {
114
66
    stream.Printf("%s", reason_str_summary.GetData());
115
66
    return true;
116
66
  } else
117
0
    return false;
118
66
}
119
120
class NSExceptionSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
121
public:
122
  NSExceptionSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
123
69
      : SyntheticChildrenFrontEnd(*valobj_sp) {}
124
125
69
  ~NSExceptionSyntheticFrontEnd() override = default;
126
127
0
  size_t CalculateNumChildren() override {
128
0
    return 4;
129
0
  }
130
131
24
  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
132
24
    switch (idx) {
133
6
      case 0: return m_name_sp;
134
6
      case 1: return m_reason_sp;
135
6
      case 2: return m_userinfo_sp;
136
6
      case 3: return m_reserved_sp;
137
24
    }
138
0
    return lldb::ValueObjectSP();
139
24
  }
140
141
84
  bool Update() override {
142
84
    m_name_sp.reset();
143
84
    m_reason_sp.reset();
144
84
    m_userinfo_sp.reset();
145
84
    m_reserved_sp.reset();
146
147
84
    return ExtractFields(m_backend, &m_name_sp, &m_reason_sp, &m_userinfo_sp,
148
84
                         &m_reserved_sp);
149
84
  }
150
151
3
  bool MightHaveChildren() override { return true; }
152
153
24
  size_t GetIndexOfChildWithName(ConstString name) override {
154
    // NSException has 4 members:
155
    //   NSString *name;
156
    //   NSString *reason;
157
    //   NSDictionary *userInfo;
158
    //   id reserved;
159
24
    static ConstString g___name("name");
160
24
    static ConstString g___reason("reason");
161
24
    static ConstString g___userInfo("userInfo");
162
24
    static ConstString g___reserved("reserved");
163
24
    if (name == g___name) 
return 06
;
164
18
    if (name == g___reason) 
return 16
;
165
12
    if (name == g___userInfo) 
return 26
;
166
6
    if (name == g___reserved) return 3;
167
0
    return UINT32_MAX;
168
6
  }
169
170
private:
171
  ValueObjectSP m_name_sp;
172
  ValueObjectSP m_reason_sp;
173
  ValueObjectSP m_userinfo_sp;
174
  ValueObjectSP m_reserved_sp;
175
};
176
177
SyntheticChildrenFrontEnd *
178
lldb_private::formatters::NSExceptionSyntheticFrontEndCreator(
179
75
    CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
180
75
  lldb::ProcessSP process_sp(valobj_sp->GetProcessSP());
181
75
  if (!process_sp)
182
0
    return nullptr;
183
75
  ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
184
75
  if (!runtime)
185
0
    return nullptr;
186
187
75
  ObjCLanguageRuntime::ClassDescriptorSP descriptor(
188
75
      runtime->GetClassDescriptor(*valobj_sp.get()));
189
190
75
  if (!descriptor.get() || 
!descriptor->IsValid()69
)
191
6
    return nullptr;
192
193
69
  const char *class_name = descriptor->GetClassName().GetCString();
194
195
69
  if (!class_name || !*class_name)
196
0
    return nullptr;
197
198
69
  if (!strcmp(class_name, "NSException"))
199
69
    return (new NSExceptionSyntheticFrontEnd(valobj_sp));
200
0
  else if (!strcmp(class_name, "NSCFException"))
201
0
    return (new NSExceptionSyntheticFrontEnd(valobj_sp));
202
0
  else if (!strcmp(class_name, "__NSCFException"))
203
0
    return (new NSExceptionSyntheticFrontEnd(valobj_sp));
204
205
0
  return nullptr;
206
69
}