LLDB  mainline
MainThreadCheckerRuntime.cpp
Go to the documentation of this file.
1 //===-- MainThreadCheckerRuntime.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 
10 
12 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Symbol.h"
16 #include "lldb/Symbol/Variable.h"
21 #include "lldb/Target/StopInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Thread.h"
26 
27 #include <memory>
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 MainThreadCheckerRuntime::~MainThreadCheckerRuntime() {
33  Deactivate();
34 }
35 
36 lldb::InstrumentationRuntimeSP
37 MainThreadCheckerRuntime::CreateInstance(const lldb::ProcessSP &process_sp) {
38  return InstrumentationRuntimeSP(new MainThreadCheckerRuntime(process_sp));
39 }
40 
41 void MainThreadCheckerRuntime::Initialize() {
42  PluginManager::RegisterPlugin(
43  GetPluginNameStatic(), "MainThreadChecker instrumentation runtime plugin.",
44  CreateInstance, GetTypeStatic);
45 }
46 
47 void MainThreadCheckerRuntime::Terminate() {
48  PluginManager::UnregisterPlugin(CreateInstance);
49 }
50 
51 lldb_private::ConstString MainThreadCheckerRuntime::GetPluginNameStatic() {
52  return ConstString("MainThreadChecker");
53 }
54 
55 lldb::InstrumentationRuntimeType MainThreadCheckerRuntime::GetTypeStatic() {
57 }
58 
59 const RegularExpression &
60 MainThreadCheckerRuntime::GetPatternForRuntimeLibrary() {
61  static RegularExpression regex(llvm::StringRef("libMainThreadChecker.dylib"));
62  return regex;
63 }
64 
65 bool MainThreadCheckerRuntime::CheckIfRuntimeIsValid(
66  const lldb::ModuleSP module_sp) {
67  static ConstString test_sym("__main_thread_checker_on_report");
68  const Symbol *symbol =
69  module_sp->FindFirstSymbolWithNameAndType(test_sym, lldb::eSymbolTypeAny);
70  return symbol != nullptr;
71 }
72 
74 MainThreadCheckerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
75  ProcessSP process_sp = GetProcessSP();
76  if (!process_sp)
77  return StructuredData::ObjectSP();
78 
79  ThreadSP thread_sp = exe_ctx_ref.GetThreadSP();
80  StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
81  ModuleSP runtime_module_sp = GetRuntimeModuleSP();
82  Target &target = process_sp->GetTarget();
83 
84  if (!frame_sp)
85  return StructuredData::ObjectSP();
86 
87  RegisterContextSP regctx_sp = frame_sp->GetRegisterContext();
88  if (!regctx_sp)
89  return StructuredData::ObjectSP();
90 
91  const RegisterInfo *reginfo = regctx_sp->GetRegisterInfoByName("arg1");
92  if (!reginfo)
93  return StructuredData::ObjectSP();
94 
95  uint64_t apiname_ptr = regctx_sp->ReadRegisterAsUnsigned(reginfo, 0);
96  if (!apiname_ptr)
97  return StructuredData::ObjectSP();
98 
99  std::string apiName = "";
100  Status read_error;
101  target.ReadCStringFromMemory(apiname_ptr, apiName, read_error);
102  if (read_error.Fail())
103  return StructuredData::ObjectSP();
104 
105  std::string className = "";
106  std::string selector = "";
107  if (apiName.substr(0, 2) == "-[") {
108  size_t spacePos = apiName.find(" ");
109  if (spacePos != std::string::npos) {
110  className = apiName.substr(2, spacePos - 2);
111  selector = apiName.substr(spacePos + 1, apiName.length() - spacePos - 2);
112  }
113  }
114 
115  // Gather the PCs of the user frames in the backtrace.
117  auto trace_sp = StructuredData::ObjectSP(trace);
118  StackFrameSP responsible_frame;
119  for (unsigned I = 0; I < thread_sp->GetStackFrameCount(); ++I) {
120  StackFrameSP frame = thread_sp->GetStackFrameAtIndex(I);
121  Address addr = frame->GetFrameCodeAddress();
122  if (addr.GetModule() == runtime_module_sp) // Skip PCs from the runtime.
123  continue;
124 
125  // The first non-runtime frame is responsible for the bug.
126  if (!responsible_frame)
127  responsible_frame = frame;
128 
129  // First frame in stacktrace should point to a real PC, not return address.
130  if (I != 0 && trace->GetSize() == 0) {
131  addr.Slide(-1);
132  }
133 
134  lldb::addr_t PC = addr.GetLoadAddress(&target);
136  }
137 
138  auto *d = new StructuredData::Dictionary();
139  auto dict_sp = StructuredData::ObjectSP(d);
140  d->AddStringItem("instrumentation_class", "MainThreadChecker");
141  d->AddStringItem("api_name", apiName);
142  d->AddStringItem("class_name", className);
143  d->AddStringItem("selector", selector);
144  d->AddStringItem("description",
145  apiName + " must be used from main thread only");
146  d->AddIntegerItem("tid", thread_sp->GetIndexID());
147  d->AddItem("trace", trace_sp);
148  return dict_sp;
149 }
150 
151 bool MainThreadCheckerRuntime::NotifyBreakpointHit(
152  void *baton, StoppointCallbackContext *context, user_id_t break_id,
153  user_id_t break_loc_id) {
154  assert(baton && "null baton");
155  if (!baton)
156  return false; //< false => resume execution.
157 
158  MainThreadCheckerRuntime *const instance =
159  static_cast<MainThreadCheckerRuntime *>(baton);
160 
161  ProcessSP process_sp = instance->GetProcessSP();
162  ThreadSP thread_sp = context->exe_ctx_ref.GetThreadSP();
163  if (!process_sp || !thread_sp ||
164  process_sp != context->exe_ctx_ref.GetProcessSP())
165  return false;
166 
167  if (process_sp->GetModIDRef().IsLastResumeForUserExpression())
168  return false;
169 
170  StructuredData::ObjectSP report =
171  instance->RetrieveReportData(context->exe_ctx_ref);
172 
173  if (report) {
174  std::string description = report->GetAsDictionary()
175  ->GetValueForKey("description")
176  ->GetAsString()
177  ->GetValue();
178  thread_sp->SetStopInfo(
179  InstrumentationRuntimeStopInfo::CreateStopReasonWithInstrumentationData(
180  *thread_sp, description, report));
181  return true;
182  }
183 
184  return false;
185 }
186 
187 void MainThreadCheckerRuntime::Activate() {
188  if (IsActive())
189  return;
190 
191  ProcessSP process_sp = GetProcessSP();
192  if (!process_sp)
193  return;
194 
195  ModuleSP runtime_module_sp = GetRuntimeModuleSP();
196 
197  ConstString symbol_name("__main_thread_checker_on_report");
198  const Symbol *symbol = runtime_module_sp->FindFirstSymbolWithNameAndType(
199  symbol_name, eSymbolTypeCode);
200 
201  if (symbol == nullptr)
202  return;
203 
204  if (!symbol->ValueIsAddress() || !symbol->GetAddressRef().IsValid())
205  return;
206 
207  Target &target = process_sp->GetTarget();
208  addr_t symbol_address = symbol->GetAddressRef().GetOpcodeLoadAddress(&target);
209 
210  if (symbol_address == LLDB_INVALID_ADDRESS)
211  return;
212 
213  Breakpoint *breakpoint =
214  process_sp->GetTarget()
215  .CreateBreakpoint(symbol_address, /*internal=*/true,
216  /*hardware=*/false)
217  .get();
218  breakpoint->SetCallback(MainThreadCheckerRuntime::NotifyBreakpointHit, this,
219  true);
220  breakpoint->SetBreakpointKind("main-thread-checker-report");
221  SetBreakpointID(breakpoint->GetID());
222 
223  SetActive(true);
224 }
225 
226 void MainThreadCheckerRuntime::Deactivate() {
227  SetActive(false);
228 
229  auto BID = GetBreakpointID();
230  if (BID == LLDB_INVALID_BREAK_ID)
231  return;
232 
233  if (ProcessSP process_sp = GetProcessSP()) {
234  process_sp->GetTarget().RemoveBreakpointByID(BID);
235  SetBreakpointID(LLDB_INVALID_BREAK_ID);
236  }
237 }
238 
239 lldb::ThreadCollectionSP
240 MainThreadCheckerRuntime::GetBacktracesFromExtendedStopInfo(
242  ThreadCollectionSP threads;
243  threads = std::make_shared<ThreadCollection>();
244 
245  ProcessSP process_sp = GetProcessSP();
246 
247  if (info->GetObjectForDotSeparatedPath("instrumentation_class")
248  ->GetStringValue() != "MainThreadChecker")
249  return threads;
250 
251  std::vector<lldb::addr_t> PCs;
252  auto trace = info->GetObjectForDotSeparatedPath("trace")->GetAsArray();
253  trace->ForEach([&PCs](StructuredData::Object *PC) -> bool {
254  PCs.push_back(PC->GetAsInteger()->GetValue());
255  return true;
256  });
257 
258  if (PCs.empty())
259  return threads;
260 
261  StructuredData::ObjectSP thread_id_obj =
262  info->GetObjectForDotSeparatedPath("tid");
263  tid_t tid = thread_id_obj ? thread_id_obj->GetIntegerValue() : 0;
264 
265  uint32_t stop_id = 0;
266  bool stop_id_is_valid = false;
267  HistoryThread *history_thread =
268  new HistoryThread(*process_sp, tid, PCs, stop_id, stop_id_is_valid);
269  ThreadSP new_thread_sp(history_thread);
270 
271  // Save this in the Process' ExtendedThreadList so a strong pointer retains
272  // the object
273  process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
274  threads->AddThread(new_thread_sp);
275 
276  return threads;
277 }
Address & GetAddressRef()
Definition: Symbol.h:56
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:292
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:78
lldb::ProcessSP GetProcessSP() const
Get accessor that creates a strong reference from the weak process reference contained in this object...
"lldb/Utility/RegularExpression.h" A C++ wrapper class for regex.
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition: Breakpoint.h:456
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:467
void SetCallback(BreakpointHitCallback callback, void *baton, bool is_synchronous=false)
Set the callback action invoked when the breakpoint is hit.
Definition: Breakpoint.cpp:421
lldb::ThreadSP GetThreadSP() const
Get accessor that creates a strong reference from the weak thread reference contained in this object...
bool Slide(int64_t offset)
Definition: Address.h:430
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
uint64_t user_id_t
Definition: lldb-types.h:84
InstrumentationRuntimeType
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
uint64_t tid_t
Definition: lldb-types.h:86
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
Definition: Address.cpp:349
bool ValueIsAddress() const
Definition: Symbol.cpp:114
Execution context objects refer to objects in the execution of the program that is being debugged...
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:343
A section + offset based address class.
Definition: Address.h:80
A thread object representing a backtrace from a previous point in the process execution.
Definition: HistoryThread.h:34
uint64_t addr_t
Definition: lldb-types.h:83
A uniqued constant string class.
Definition: ConstString.h:38
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:264
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
size_t ReadCStringFromMemory(const Address &addr, std::string &out_str, Status &error)
Definition: Target.cpp:1853
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:325
An error handling class.
Definition: Status.h:44