Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- InstrumentationRuntimeMainThreadChecker.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 "InstrumentationRuntimeMainThreadChecker.h"
10
11
#include "Plugins/Process/Utility/HistoryThread.h"
12
#include "lldb/Breakpoint/StoppointCallbackContext.h"
13
#include "lldb/Core/Module.h"
14
#include "lldb/Core/PluginManager.h"
15
#include "lldb/Symbol/Symbol.h"
16
#include "lldb/Symbol/SymbolContext.h"
17
#include "lldb/Symbol/Variable.h"
18
#include "lldb/Symbol/VariableList.h"
19
#include "lldb/Target/InstrumentationRuntimeStopInfo.h"
20
#include "lldb/Target/RegisterContext.h"
21
#include "lldb/Target/SectionLoadList.h"
22
#include "lldb/Target/StopInfo.h"
23
#include "lldb/Target/Target.h"
24
#include "lldb/Target/Thread.h"
25
#include "lldb/Utility/RegularExpression.h"
26
27
#include <memory>
28
29
using namespace lldb;
30
using namespace lldb_private;
31
32
LLDB_PLUGIN_DEFINE(InstrumentationRuntimeMainThreadChecker)
33
34
InstrumentationRuntimeMainThreadChecker::
35
2.69k
    ~InstrumentationRuntimeMainThreadChecker() {
36
2.69k
  Deactivate();
37
2.69k
}
38
39
lldb::InstrumentationRuntimeSP
40
InstrumentationRuntimeMainThreadChecker::CreateInstance(
41
2.69k
    const lldb::ProcessSP &process_sp) {
42
2.69k
  return InstrumentationRuntimeSP(
43
2.69k
      new InstrumentationRuntimeMainThreadChecker(process_sp));
44
2.69k
}
45
46
3.44k
void InstrumentationRuntimeMainThreadChecker::Initialize() {
47
3.44k
  PluginManager::RegisterPlugin(
48
3.44k
      GetPluginNameStatic(),
49
3.44k
      "MainThreadChecker instrumentation runtime plugin.", CreateInstance,
50
3.44k
      GetTypeStatic);
51
3.44k
}
52
53
3.43k
void InstrumentationRuntimeMainThreadChecker::Terminate() {
54
3.43k
  PluginManager::UnregisterPlugin(CreateInstance);
55
3.43k
}
56
57
lldb::InstrumentationRuntimeType
58
11.4k
InstrumentationRuntimeMainThreadChecker::GetTypeStatic() {
59
11.4k
  return eInstrumentationRuntimeTypeMainThreadChecker;
60
11.4k
}
61
62
const RegularExpression &
63
139k
InstrumentationRuntimeMainThreadChecker::GetPatternForRuntimeLibrary() {
64
139k
  static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib"));
65
139k
  return regex;
66
139k
}
67
68
bool InstrumentationRuntimeMainThreadChecker::CheckIfRuntimeIsValid(
69
2.68k
    const lldb::ModuleSP module_sp) {
70
2.68k
  static ConstString test_sym("__main_thread_checker_on_report");
71
2.68k
  const Symbol *symbol =
72
2.68k
      module_sp->FindFirstSymbolWithNameAndType(test_sym, lldb::eSymbolTypeAny);
73
2.68k
  return symbol != nullptr;
74
2.68k
}
75
76
StructuredData::ObjectSP
77
InstrumentationRuntimeMainThreadChecker::RetrieveReportData(
78
3
    ExecutionContextRef exe_ctx_ref) {
79
3
  ProcessSP process_sp = GetProcessSP();
80
3
  if (!process_sp)
81
0
    return StructuredData::ObjectSP();
82
83
3
  ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
84
3
  StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
85
3
  ModuleSP runtime_module_sp = GetRuntimeModuleSP();
86
3
  Target &target = process_sp->GetTarget();
87
88
3
  if (!frame_sp)
89
0
    return StructuredData::ObjectSP();
90
91
3
  RegisterContextSP regctx_sp = frame_sp->GetRegisterContext();
92
3
  if (!regctx_sp)
93
0
    return StructuredData::ObjectSP();
94
95
3
  const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1");
96
3
  if (!reginfo)
97
0
    return StructuredData::ObjectSP();
98
99
3
  uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0);
100
3
  if (!apiname_ptr)
101
0
    return StructuredData::ObjectSP();
102
103
3
  std::string apiName;
104
3
  Status read_error;
105
3
  target.ReadCStringFromMemory(apiname_ptr, apiName, read_error);
106
3
  if (read_error.Fail())
107
0
    return StructuredData::ObjectSP();
108
109
3
  std::string className;
110
3
  std::string selector;
111
3
  if (apiName.substr(0, 2) == "-[") {
112
3
    size_t spacePos = apiName.find(' ');
113
3
    if (spacePos != std::string::npos) {
114
3
      className = apiName.substr(2, spacePos - 2);
115
3
      selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2);
116
3
    }
117
3
  }
118
119
  // Gather the PCs of the user frames in the backtrace.
120
3
  StructuredData::Array *trace = new StructuredData::Array();
121
3
  auto trace_sp = StructuredData::ObjectSP(trace);
122
3
  StackFrameSP responsible_frame;
123
30
  for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); 
++I27
) {
124
27
    StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I);
125
27
    Address addr = frame->GetFrameCodeAddressForSymbolication();
126
27
    if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
127
15
      continue;
128
129
    // The first non-runtime frame is responsible for the bug.
130
12
    if (!responsible_frame)
131
3
      responsible_frame = frame;
132
133
12
    lldb::addr_t PC = addr.GetLoadAddress(&target);
134
12
    trace->AddItem(StructuredData::ObjectSP(new StructuredData::Integer(PC)));
135
12
  }
136
137
3
  auto *d = new StructuredData::Dictionary();
138
3
  auto dict_sp = StructuredData::ObjectSP(d);
139
3
  d->AddStringItem("instrumentation_class", "MainThreadChecker");
140
3
  d->AddStringItem("api_name", apiName);
141
3
  d->AddStringItem("class_name", className);
142
3
  d->AddStringItem("selector", selector);
143
3
  d->AddStringItem("description",
144
3
                   apiName + " must be used from main thread only");
145
3
  d->AddIntegerItem("tid", thread_sp->GetIndexID());
146
3
  d->AddItem("trace", trace_sp);
147
3
  return dict_sp;
148
3
}
149
150
bool InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit(
151
    void *baton, StoppointCallbackContext *context, user_id_t break_id,
152
3
    user_id_t break_loc_id) {
153
3
  assert(baton && "null baton");
154
3
  if (!baton)
155
0
    return false; ///< false => resume execution.
156
157
3
  InstrumentationRuntimeMainThreadChecker *const instance =
158
3
      static_cast<InstrumentationRuntimeMainThreadChecker *>(baton);
159
160
3
  ProcessSP process_sp = instance->GetProcessSP();
161
3
  ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
162
3
  if (!process_sp || !thread_sp ||
163
3
      process_sp != context->exe_ctx_ref.GetProcessSP())
164
0
    return false;
165
166
3
  if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
167
0
    return false;
168
169
3
  StructuredData::ObjectSP report =
170
3
      instance->RetrieveReportData(context->exe_ctx_ref);
171
172
3
  if (report) {
173
3
    std::string description = std::string(report->GetAsDictionary()
174
3
                                              ->GetValueForKey("description")
175
3
                                              ->GetAsString()
176
3
                                              ->GetValue());
177
3
    thread_sp->SetStopInfo(
178
3
        InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
179
3
            *thread_sp, description, report));
180
3
    return true;
181
3
  }
182
183
0
  return false;
184
3
}
185
186
3
void InstrumentationRuntimeMainThreadChecker::Activate() {
187
3
  if (IsActive())
188
0
    return;
189
190
3
  ProcessSP process_sp = GetProcessSP();
191
3
  if (!process_sp)
192
0
    return;
193
194
3
  ModuleSP runtime_module_sp = GetRuntimeModuleSP();
195
196
3
  ConstString symbol_name("__main_thread_checker_on_report");
197
3
  const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
198
3
      symbol_name, eSymbolTypeCode);
199
200
3
  if (symbol == nullptr)
201
0
    return;
202
203
3
  if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
204
0
    return;
205
206
3
  Target &target = process_sp->GetTarget();
207
3
  addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
208
209
3
  if (symbol_address == LLDB_INVALID_ADDRESS)
210
0
    return;
211
212
3
  Breakpoint *breakpoint =
213
3
      process_sp->GetTarget()
214
3
          .CreateBreakpoint(symbol_address, /*internal=*/true,
215
3
                            /*hardware=*/false)
216
3
          .get();
217
3
  breakpoint->SetCallback(
218
3
      InstrumentationRuntimeMainThreadChecker::NotifyBreakpointHit, this, true);
219
3
  breakpoint->SetBreakpointKind("main-thread-checker-report");
220
3
  SetBreakpointID(breakpoint->GetID());
221
222
3
  SetActive(true);
223
3
}
224
225
2.69k
void InstrumentationRuntimeMainThreadChecker::Deactivate() {
226
2.69k
  SetActive(false);
227
228
2.69k
  auto BID = GetBreakpointID();
229
2.69k
  if (BID == LLDB_INVALID_BREAK_ID)
230
2.69k
    return;
231
232
3
  if (ProcessSP process_sp = GetProcessSP()) {
233
3
    process_sp->GetTarget().RemoveBreakpointByID(BID);
234
3
    SetBreakpointID(LLDB_INVALID_BREAK_ID);
235
3
  }
236
3
}
237
238
lldb::ThreadCollectionSP
239
InstrumentationRuntimeMainThreadChecker::GetBacktracesFromExtendedStopInfo(
240
0
    StructuredData::ObjectSP info) {
241
0
  ThreadCollectionSP threads;
242
0
  threads = std::make_shared<ThreadCollection>();
243
244
0
  ProcessSP process_sp = GetProcessSP();
245
246
0
  if (info->GetObjectForDotSeparatedPath("instrumentation_class")
247
0
          ->GetStringValue() != "MainThreadChecker")
248
0
    return threads;
249
250
0
  std::vector<lldb::addr_t> PCs;
251
0
  auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
252
0
  trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
253
0
    PCs.push_back(PC->GetAsInteger()->GetValue());
254
0
    return true;
255
0
  });
256
257
0
  if (PCs.empty())
258
0
    return threads;
259
260
0
  StructuredData::ObjectSP thread_id_obj =
261
0
      info->GetObjectForDotSeparatedPath("tid");
262
0
  tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
263
264
  // We gather symbolication addresses above, so no need for HistoryThread to
265
  // try to infer the call addresses.
266
0
  bool pcs_are_call_addresses = true;
267
0
  ThreadSP new_thread_sp = std::make_shared<HistoryThread>(
268
0
      *process_sp, tid, PCs, pcs_are_call_addresses);
269
270
  // Save this in the Process' ExtendedThreadList so a strong pointer retains
271
  // the object
272
0
  process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
273
0
  threads->AddThread(new_thread_sp);
274
275
0
  return threads;
276
0
}