1 //===-- SBTarget.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 "lldb/API/SBTarget.h"
10 #include "SBReproducerPrivate.h"
11 
12 #include "lldb/lldb-public.h"
13 
14 #include "lldb/API/SBBreakpoint.h"
15 #include "lldb/API/SBDebugger.h"
16 #include "lldb/API/SBEvent.h"
17 #include "lldb/API/SBExpressionOptions.h"
18 #include "lldb/API/SBFileSpec.h"
19 #include "lldb/API/SBListener.h"
20 #include "lldb/API/SBModule.h"
21 #include "lldb/API/SBModuleSpec.h"
22 #include "lldb/API/SBProcess.h"
23 #include "lldb/API/SBSourceManager.h"
24 #include "lldb/API/SBStream.h"
25 #include "lldb/API/SBStringList.h"
26 #include "lldb/API/SBStructuredData.h"
27 #include "lldb/API/SBSymbolContextList.h"
28 #include "lldb/Breakpoint/BreakpointID.h"
29 #include "lldb/Breakpoint/BreakpointIDList.h"
30 #include "lldb/Breakpoint/BreakpointList.h"
31 #include "lldb/Breakpoint/BreakpointLocation.h"
32 #include "lldb/Core/Address.h"
33 #include "lldb/Core/AddressResolver.h"
34 #include "lldb/Core/AddressResolverName.h"
35 #include "lldb/Core/Debugger.h"
36 #include "lldb/Core/Disassembler.h"
37 #include "lldb/Core/Module.h"
38 #include "lldb/Core/ModuleSpec.h"
39 #include "lldb/Core/STLUtils.h"
40 #include "lldb/Core/SearchFilter.h"
41 #include "lldb/Core/Section.h"
42 #include "lldb/Core/StructuredDataImpl.h"
43 #include "lldb/Core/ValueObjectConstResult.h"
44 #include "lldb/Core/ValueObjectList.h"
45 #include "lldb/Core/ValueObjectVariable.h"
46 #include "lldb/Host/Host.h"
47 #include "lldb/Symbol/ClangASTContext.h"
48 #include "lldb/Symbol/DeclVendor.h"
49 #include "lldb/Symbol/ObjectFile.h"
50 #include "lldb/Symbol/SymbolFile.h"
51 #include "lldb/Symbol/SymbolVendor.h"
52 #include "lldb/Symbol/VariableList.h"
53 #include "lldb/Target/ABI.h"
54 #include "lldb/Target/Language.h"
55 #include "lldb/Target/LanguageRuntime.h"
56 #include "lldb/Target/ObjCLanguageRuntime.h"
57 #include "lldb/Target/Process.h"
58 #include "lldb/Target/StackFrame.h"
59 #include "lldb/Target/Target.h"
60 #include "lldb/Target/TargetList.h"
61 #include "lldb/Utility/ArchSpec.h"
62 #include "lldb/Utility/Args.h"
63 #include "lldb/Utility/FileSpec.h"
64 #include "lldb/Utility/Log.h"
65 #include "lldb/Utility/ProcessInfo.h"
66 #include "lldb/Utility/RegularExpression.h"
67 
68 #include "Commands/CommandObjectBreakpoint.h"
69 #include "lldb/Interpreter/CommandReturnObject.h"
70 #include "llvm/Support/PrettyStackTrace.h"
71 #include "llvm/Support/Regex.h"
72 
73 using namespace lldb;
74 using namespace lldb_private;
75 
76 #define DEFAULT_DISASM_BYTE_SIZE 32
77 
78 namespace {
79 
80 Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
81   std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
82 
83   auto process_sp = target.GetProcessSP();
84   if (process_sp) {
85     const auto state = process_sp->GetState();
86     if (process_sp->IsAlive() && state == eStateConnected) {
87       // If we are already connected, then we have already specified the
88       // listener, so if a valid listener is supplied, we need to error out to
89       // let the client know.
90       if (attach_info.GetListener())
91         return Status("process is connected and already has a listener, pass "
92                       "empty listener");
93     }
94   }
95 
96   return target.Attach(attach_info, nullptr);
97 }
98 
99 } // namespace
100 
101 //----------------------------------------------------------------------
102 // SBTarget constructor
103 //----------------------------------------------------------------------
104 SBTarget::SBTarget() : m_opaque_sp() {
105   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTarget);
106 }
107 
108 SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
109   LLDB_RECORD_CONSTRUCTOR(SBTarget, (const lldb::SBTarget &), rhs);
110 }
111 
112 SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {
113   LLDB_RECORD_CONSTRUCTOR(SBTarget, (const lldb::TargetSP &), target_sp);
114 }
115 
116 const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
117   LLDB_RECORD_METHOD(const lldb::SBTarget &,
118                      SBTarget, operator=,(const lldb::SBTarget &), rhs);
119 
120   if (this != &rhs)
121     m_opaque_sp = rhs.m_opaque_sp;
122   return *this;
123 }
124 
125 //----------------------------------------------------------------------
126 // Destructor
127 //----------------------------------------------------------------------
128 SBTarget::~SBTarget() {}
129 
130 bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
131   LLDB_RECORD_STATIC_METHOD(bool, SBTarget, EventIsTargetEvent,
132                             (const lldb::SBEvent &), event);
133 
134   return Target::TargetEventData::GetEventDataFromEvent(event.get()) != NULL;
135 }
136 
137 SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
138   LLDB_RECORD_STATIC_METHOD(lldb::SBTarget, SBTarget, GetTargetFromEvent,
139                             (const lldb::SBEvent &), event);
140 
141   return LLDB_RECORD_RESULT(
142       Target::TargetEventData::GetTargetFromEvent(event.get()));
143 }
144 
145 uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
146   LLDB_RECORD_STATIC_METHOD(uint32_t, SBTarget, GetNumModulesFromEvent,
147                             (const lldb::SBEvent &), event);
148 
149   const ModuleList module_list =
150       Target::TargetEventData::GetModuleListFromEvent(event.get());
151   return module_list.GetSize();
152 }
153 
154 SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
155                                              const SBEvent &event) {
156   LLDB_RECORD_STATIC_METHOD(lldb::SBModule, SBTarget, GetModuleAtIndexFromEvent,
157                             (const uint32_t, const lldb::SBEvent &), idx,
158                             event);
159 
160   const ModuleList module_list =
161       Target::TargetEventData::GetModuleListFromEvent(event.get());
162   return LLDB_RECORD_RESULT(SBModule(module_list.GetModuleAtIndex(idx)));
163 }
164 
165 const char *SBTarget::GetBroadcasterClassName() {
166   LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBTarget,
167                                     GetBroadcasterClassName);
168 
169   return Target::GetStaticBroadcasterClass().AsCString();
170 }
171 
172 bool SBTarget::IsValid() const {
173   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTarget, IsValid);
174 
175   return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
176 }
177 
178 SBProcess SBTarget::GetProcess() {
179   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBProcess, SBTarget, GetProcess);
180 
181   SBProcess sb_process;
182   ProcessSP process_sp;
183   TargetSP target_sp(GetSP());
184   if (target_sp) {
185     process_sp = target_sp->GetProcessSP();
186     sb_process.SetSP(process_sp);
187   }
188 
189   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
190   if (log)
191     log->Printf("SBTarget(%p)::GetProcess () => SBProcess(%p)",
192                 static_cast<void *>(target_sp.get()),
193                 static_cast<void *>(process_sp.get()));
194 
195   return LLDB_RECORD_RESULT(sb_process);
196 }
197 
198 SBPlatform SBTarget::GetPlatform() {
199   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBPlatform, SBTarget, GetPlatform);
200 
201   TargetSP target_sp(GetSP());
202   if (!target_sp)
203     return LLDB_RECORD_RESULT(SBPlatform());
204 
205   SBPlatform platform;
206   platform.m_opaque_sp = target_sp->GetPlatform();
207 
208   return LLDB_RECORD_RESULT(platform);
209 }
210 
211 SBDebugger SBTarget::GetDebugger() const {
212   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBDebugger, SBTarget, GetDebugger);
213 
214   SBDebugger debugger;
215   TargetSP target_sp(GetSP());
216   if (target_sp)
217     debugger.reset(target_sp->GetDebugger().shared_from_this());
218   return LLDB_RECORD_RESULT(debugger);
219 }
220 
221 SBStructuredData SBTarget::GetStatistics() {
222   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStructuredData, SBTarget, GetStatistics);
223 
224   SBStructuredData data;
225   TargetSP target_sp(GetSP());
226   if (!target_sp)
227     return LLDB_RECORD_RESULT(data);
228 
229   auto stats_up = llvm::make_unique<StructuredData::Dictionary>();
230   int i = 0;
231   for (auto &Entry : target_sp->GetStatistics()) {
232     std::string Desc = lldb_private::GetStatDescription(
233         static_cast<lldb_private::StatisticKind>(i));
234     stats_up->AddIntegerItem(Desc, Entry);
235     i += 1;
236   }
237 
238   data.m_impl_up->SetObjectSP(std::move(stats_up));
239   return LLDB_RECORD_RESULT(data);
240 }
241 
242 void SBTarget::SetCollectingStats(bool v) {
243   LLDB_RECORD_METHOD(void, SBTarget, SetCollectingStats, (bool), v);
244 
245   TargetSP target_sp(GetSP());
246   if (!target_sp)
247     return;
248   return target_sp->SetCollectingStats(v);
249 }
250 
251 bool SBTarget::GetCollectingStats() {
252   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, GetCollectingStats);
253 
254   TargetSP target_sp(GetSP());
255   if (!target_sp)
256     return false;
257   return target_sp->GetCollectingStats();
258 }
259 
260 SBProcess SBTarget::LoadCore(const char *core_file) {
261   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, LoadCore, (const char *),
262                      core_file);
263 
264   lldb::SBError error; // Ignored
265   return LLDB_RECORD_RESULT(LoadCore(core_file, error));
266 }
267 
268 SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
269   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, LoadCore,
270                      (const char *, lldb::SBError &), core_file, error);
271 
272   SBProcess sb_process;
273   TargetSP target_sp(GetSP());
274   if (target_sp) {
275     FileSpec filespec(core_file);
276     FileSystem::Instance().Resolve(filespec);
277     ProcessSP process_sp(target_sp->CreateProcess(
278         target_sp->GetDebugger().GetListener(), "", &filespec));
279     if (process_sp) {
280       error.SetError(process_sp->LoadCore());
281       if (error.Success())
282         sb_process.SetSP(process_sp);
283     } else {
284       error.SetErrorString("Failed to create the process");
285     }
286   } else {
287     error.SetErrorString("SBTarget is invalid");
288   }
289   return LLDB_RECORD_RESULT(sb_process);
290 }
291 
292 SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
293                                  const char *working_directory) {
294   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, LaunchSimple,
295                      (const char **, const char **, const char *), argv, envp,
296                      working_directory);
297 
298   char *stdin_path = NULL;
299   char *stdout_path = NULL;
300   char *stderr_path = NULL;
301   uint32_t launch_flags = 0;
302   bool stop_at_entry = false;
303   SBError error;
304   SBListener listener = GetDebugger().GetListener();
305   return LLDB_RECORD_RESULT(Launch(listener, argv, envp, stdin_path,
306                                    stdout_path, stderr_path, working_directory,
307                                    launch_flags, stop_at_entry, error));
308 }
309 
310 SBError SBTarget::Install() {
311   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBError, SBTarget, Install);
312 
313   SBError sb_error;
314   TargetSP target_sp(GetSP());
315   if (target_sp) {
316     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
317     sb_error.ref() = target_sp->Install(NULL);
318   }
319   return LLDB_RECORD_RESULT(sb_error);
320 }
321 
322 SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
323                            char const **envp, const char *stdin_path,
324                            const char *stdout_path, const char *stderr_path,
325                            const char *working_directory,
326                            uint32_t launch_flags, // See LaunchFlags
327                            bool stop_at_entry, lldb::SBError &error) {
328   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, Launch,
329                      (lldb::SBListener &, const char **, const char **,
330                       const char *, const char *, const char *, const char *,
331                       uint32_t, bool, lldb::SBError &),
332                      listener, argv, envp, stdin_path, stdout_path, stderr_path,
333                      working_directory, launch_flags, stop_at_entry, error);
334 
335   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
336 
337   SBProcess sb_process;
338   ProcessSP process_sp;
339   TargetSP target_sp(GetSP());
340 
341   if (log)
342     log->Printf("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, "
343                 "stderr=%s, working-dir=%s, launch_flags=0x%x, "
344                 "stop_at_entry=%i, &error (%p))...",
345                 static_cast<void *>(target_sp.get()), static_cast<void *>(argv),
346                 static_cast<void *>(envp), stdin_path ? stdin_path : "NULL",
347                 stdout_path ? stdout_path : "NULL",
348                 stderr_path ? stderr_path : "NULL",
349                 working_directory ? working_directory : "NULL", launch_flags,
350                 stop_at_entry, static_cast<void *>(error.get()));
351 
352   if (target_sp) {
353     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
354 
355     if (stop_at_entry)
356       launch_flags |= eLaunchFlagStopAtEntry;
357 
358     if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
359       launch_flags |= eLaunchFlagDisableASLR;
360 
361     StateType state = eStateInvalid;
362     process_sp = target_sp->GetProcessSP();
363     if (process_sp) {
364       state = process_sp->GetState();
365 
366       if (process_sp->IsAlive() && state != eStateConnected) {
367         if (state == eStateAttaching)
368           error.SetErrorString("process attach is in progress");
369         else
370           error.SetErrorString("a process is already being debugged");
371         return LLDB_RECORD_RESULT(sb_process);
372       }
373     }
374 
375     if (state == eStateConnected) {
376       // If we are already connected, then we have already specified the
377       // listener, so if a valid listener is supplied, we need to error out to
378       // let the client know.
379       if (listener.IsValid()) {
380         error.SetErrorString("process is connected and already has a listener, "
381                              "pass empty listener");
382         return LLDB_RECORD_RESULT(sb_process);
383       }
384     }
385 
386     if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
387       launch_flags |= eLaunchFlagDisableSTDIO;
388 
389     ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),
390                                   FileSpec(stderr_path),
391                                   FileSpec(working_directory), launch_flags);
392 
393     Module *exe_module = target_sp->GetExecutableModulePointer();
394     if (exe_module)
395       launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
396     if (argv)
397       launch_info.GetArguments().AppendArguments(argv);
398     if (envp)
399       launch_info.GetEnvironment() = Environment(envp);
400 
401     if (listener.IsValid())
402       launch_info.SetListener(listener.GetSP());
403 
404     error.SetError(target_sp->Launch(launch_info, NULL));
405 
406     sb_process.SetSP(target_sp->GetProcessSP());
407   } else {
408     error.SetErrorString("SBTarget is invalid");
409   }
410 
411   log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
412   if (log)
413     log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p), SBError(%s)",
414                 static_cast<void *>(target_sp.get()),
415                 static_cast<void *>(sb_process.GetSP().get()),
416                 error.GetCString());
417 
418   return LLDB_RECORD_RESULT(sb_process);
419 }
420 
421 SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
422   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, Launch,
423                      (lldb::SBLaunchInfo &, lldb::SBError &), sb_launch_info,
424                      error);
425 
426   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
427 
428   SBProcess sb_process;
429   TargetSP target_sp(GetSP());
430 
431   if (log)
432     log->Printf("SBTarget(%p)::Launch (launch_info, error)...",
433                 static_cast<void *>(target_sp.get()));
434 
435   if (target_sp) {
436     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
437     StateType state = eStateInvalid;
438     {
439       ProcessSP process_sp = target_sp->GetProcessSP();
440       if (process_sp) {
441         state = process_sp->GetState();
442 
443         if (process_sp->IsAlive() && state != eStateConnected) {
444           if (state == eStateAttaching)
445             error.SetErrorString("process attach is in progress");
446           else
447             error.SetErrorString("a process is already being debugged");
448           return LLDB_RECORD_RESULT(sb_process);
449         }
450       }
451     }
452 
453     lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
454 
455     if (!launch_info.GetExecutableFile()) {
456       Module *exe_module = target_sp->GetExecutableModulePointer();
457       if (exe_module)
458         launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
459     }
460 
461     const ArchSpec &arch_spec = target_sp->GetArchitecture();
462     if (arch_spec.IsValid())
463       launch_info.GetArchitecture() = arch_spec;
464 
465     error.SetError(target_sp->Launch(launch_info, NULL));
466     sb_launch_info.set_ref(launch_info);
467     sb_process.SetSP(target_sp->GetProcessSP());
468   } else {
469     error.SetErrorString("SBTarget is invalid");
470   }
471 
472   log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
473   if (log)
474     log->Printf("SBTarget(%p)::Launch (...) => SBProcess(%p)",
475                 static_cast<void *>(target_sp.get()),
476                 static_cast<void *>(sb_process.GetSP().get()));
477 
478   return LLDB_RECORD_RESULT(sb_process);
479 }
480 
481 lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
482   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, Attach,
483                      (lldb::SBAttachInfo &, lldb::SBError &), sb_attach_info,
484                      error);
485 
486   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
487 
488   SBProcess sb_process;
489   TargetSP target_sp(GetSP());
490 
491   if (log)
492     log->Printf("SBTarget(%p)::Attach (sb_attach_info, error)...",
493                 static_cast<void *>(target_sp.get()));
494 
495   if (target_sp) {
496     ProcessAttachInfo &attach_info = sb_attach_info.ref();
497     if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid()) {
498       PlatformSP platform_sp = target_sp->GetPlatform();
499       // See if we can pre-verify if a process exists or not
500       if (platform_sp && platform_sp->IsConnected()) {
501         lldb::pid_t attach_pid = attach_info.GetProcessID();
502         ProcessInstanceInfo instance_info;
503         if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
504           attach_info.SetUserID(instance_info.GetEffectiveUserID());
505         } else {
506           error.ref().SetErrorStringWithFormat(
507               "no process found with process ID %" PRIu64, attach_pid);
508           if (log) {
509             log->Printf("SBTarget(%p)::Attach (...) => error %s",
510                         static_cast<void *>(target_sp.get()),
511                         error.GetCString());
512           }
513           return LLDB_RECORD_RESULT(sb_process);
514         }
515       }
516     }
517     error.SetError(AttachToProcess(attach_info, *target_sp));
518     if (error.Success())
519       sb_process.SetSP(target_sp->GetProcessSP());
520   } else {
521     error.SetErrorString("SBTarget is invalid");
522   }
523 
524   if (log)
525     log->Printf("SBTarget(%p)::Attach (...) => SBProcess(%p)",
526                 static_cast<void *>(target_sp.get()),
527                 static_cast<void *>(sb_process.GetSP().get()));
528 
529   return LLDB_RECORD_RESULT(sb_process);
530 }
531 
532 lldb::SBProcess SBTarget::AttachToProcessWithID(
533     SBListener &listener,
534     lldb::pid_t pid, // The process ID to attach to
535     SBError &error   // An error explaining what went wrong if attach fails
536 ) {
537   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, AttachToProcessWithID,
538                      (lldb::SBListener &, lldb::pid_t, lldb::SBError &),
539                      listener, pid, error);
540 
541   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
542 
543   SBProcess sb_process;
544   TargetSP target_sp(GetSP());
545 
546   if (log)
547     log->Printf("SBTarget(%p)::%s (listener, pid=%" PRId64 ", error)...",
548                 static_cast<void *>(target_sp.get()), __FUNCTION__, pid);
549 
550   if (target_sp) {
551     ProcessAttachInfo attach_info;
552     attach_info.SetProcessID(pid);
553     if (listener.IsValid())
554       attach_info.SetListener(listener.GetSP());
555 
556     ProcessInstanceInfo instance_info;
557     if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
558       attach_info.SetUserID(instance_info.GetEffectiveUserID());
559 
560     error.SetError(AttachToProcess(attach_info, *target_sp));
561     if (error.Success())
562       sb_process.SetSP(target_sp->GetProcessSP());
563   } else
564     error.SetErrorString("SBTarget is invalid");
565 
566   if (log)
567     log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
568                 static_cast<void *>(target_sp.get()), __FUNCTION__,
569                 static_cast<void *>(sb_process.GetSP().get()));
570   return LLDB_RECORD_RESULT(sb_process);
571 }
572 
573 lldb::SBProcess SBTarget::AttachToProcessWithName(
574     SBListener &listener,
575     const char *name, // basename of process to attach to
576     bool wait_for, // if true wait for a new instance of "name" to be launched
577     SBError &error // An error explaining what went wrong if attach fails
578 ) {
579   LLDB_RECORD_METHOD(lldb::SBProcess, SBTarget, AttachToProcessWithName,
580                      (lldb::SBListener &, const char *, bool, lldb::SBError &),
581                      listener, name, wait_for, error);
582 
583   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
584 
585   SBProcess sb_process;
586   TargetSP target_sp(GetSP());
587 
588   if (log)
589     log->Printf("SBTarget(%p)::%s (listener, name=%s, wait_for=%s, error)...",
590                 static_cast<void *>(target_sp.get()), __FUNCTION__, name,
591                 wait_for ? "true" : "false");
592 
593   if (name && target_sp) {
594     ProcessAttachInfo attach_info;
595     attach_info.GetExecutableFile().SetFile(name, FileSpec::Style::native);
596     attach_info.SetWaitForLaunch(wait_for);
597     if (listener.IsValid())
598       attach_info.SetListener(listener.GetSP());
599 
600     error.SetError(AttachToProcess(attach_info, *target_sp));
601     if (error.Success())
602       sb_process.SetSP(target_sp->GetProcessSP());
603   } else
604     error.SetErrorString("SBTarget is invalid");
605 
606   if (log)
607     log->Printf("SBTarget(%p)::%s (...) => SBProcess(%p)",
608                 static_cast<void *>(target_sp.get()), __FUNCTION__,
609                 static_cast<void *>(sb_process.GetSP().get()));
610   return LLDB_RECORD_RESULT(sb_process);
611 }
612 
613 lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
614                                         const char *plugin_name,
615                                         SBError &error) {
616   LLDB_RECORD_METHOD(
617       lldb::SBProcess, SBTarget, ConnectRemote,
618       (lldb::SBListener &, const char *, const char *, lldb::SBError &),
619       listener, url, plugin_name, error);
620 
621   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
622 
623   SBProcess sb_process;
624   ProcessSP process_sp;
625   TargetSP target_sp(GetSP());
626 
627   if (log)
628     log->Printf("SBTarget(%p)::ConnectRemote (listener, url=%s, "
629                 "plugin_name=%s, error)...",
630                 static_cast<void *>(target_sp.get()), url, plugin_name);
631 
632   if (target_sp) {
633     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
634     if (listener.IsValid())
635       process_sp =
636           target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, NULL);
637     else
638       process_sp = target_sp->CreateProcess(
639           target_sp->GetDebugger().GetListener(), plugin_name, NULL);
640 
641     if (process_sp) {
642       sb_process.SetSP(process_sp);
643       error.SetError(process_sp->ConnectRemote(NULL, url));
644     } else {
645       error.SetErrorString("unable to create lldb_private::Process");
646     }
647   } else {
648     error.SetErrorString("SBTarget is invalid");
649   }
650 
651   if (log)
652     log->Printf("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
653                 static_cast<void *>(target_sp.get()),
654                 static_cast<void *>(process_sp.get()));
655   return LLDB_RECORD_RESULT(sb_process);
656 }
657 
658 SBFileSpec SBTarget::GetExecutable() {
659   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBTarget, GetExecutable);
660 
661   SBFileSpec exe_file_spec;
662   TargetSP target_sp(GetSP());
663   if (target_sp) {
664     Module *exe_module = target_sp->GetExecutableModulePointer();
665     if (exe_module)
666       exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
667   }
668 
669   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
670   if (log) {
671     log->Printf("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
672                 static_cast<void *>(target_sp.get()),
673                 static_cast<const void *>(exe_file_spec.get()));
674   }
675 
676   return LLDB_RECORD_RESULT(exe_file_spec);
677 }
678 
679 bool SBTarget::operator==(const SBTarget &rhs) const {
680   LLDB_RECORD_METHOD_CONST(bool, SBTarget, operator==,(const lldb::SBTarget &),
681                            rhs);
682 
683   return m_opaque_sp.get() == rhs.m_opaque_sp.get();
684 }
685 
686 bool SBTarget::operator!=(const SBTarget &rhs) const {
687   LLDB_RECORD_METHOD_CONST(bool, SBTarget, operator!=,(const lldb::SBTarget &),
688                            rhs);
689 
690   return m_opaque_sp.get() != rhs.m_opaque_sp.get();
691 }
692 
693 lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
694 
695 void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
696   m_opaque_sp = target_sp;
697 }
698 
699 lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
700   LLDB_RECORD_METHOD(lldb::SBAddress, SBTarget, ResolveLoadAddress,
701                      (lldb::addr_t), vm_addr);
702 
703   lldb::SBAddress sb_addr;
704   Address &addr = sb_addr.ref();
705   TargetSP target_sp(GetSP());
706   if (target_sp) {
707     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
708     if (target_sp->ResolveLoadAddress(vm_addr, addr))
709       return LLDB_RECORD_RESULT(sb_addr);
710   }
711 
712   // We have a load address that isn't in a section, just return an address
713   // with the offset filled in (the address) and the section set to NULL
714   addr.SetRawAddress(vm_addr);
715   return LLDB_RECORD_RESULT(sb_addr);
716 }
717 
718 lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
719   LLDB_RECORD_METHOD(lldb::SBAddress, SBTarget, ResolveFileAddress,
720                      (lldb::addr_t), file_addr);
721 
722   lldb::SBAddress sb_addr;
723   Address &addr = sb_addr.ref();
724   TargetSP target_sp(GetSP());
725   if (target_sp) {
726     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
727     if (target_sp->ResolveFileAddress(file_addr, addr))
728       return LLDB_RECORD_RESULT(sb_addr);
729   }
730 
731   addr.SetRawAddress(file_addr);
732   return LLDB_RECORD_RESULT(sb_addr);
733 }
734 
735 lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
736                                                  lldb::addr_t vm_addr) {
737   LLDB_RECORD_METHOD(lldb::SBAddress, SBTarget, ResolvePastLoadAddress,
738                      (uint32_t, lldb::addr_t), stop_id, vm_addr);
739 
740   lldb::SBAddress sb_addr;
741   Address &addr = sb_addr.ref();
742   TargetSP target_sp(GetSP());
743   if (target_sp) {
744     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
745     if (target_sp->ResolveLoadAddress(vm_addr, addr))
746       return LLDB_RECORD_RESULT(sb_addr);
747   }
748 
749   // We have a load address that isn't in a section, just return an address
750   // with the offset filled in (the address) and the section set to NULL
751   addr.SetRawAddress(vm_addr);
752   return LLDB_RECORD_RESULT(sb_addr);
753 }
754 
755 SBSymbolContext
756 SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
757                                          uint32_t resolve_scope) {
758   LLDB_RECORD_METHOD(lldb::SBSymbolContext, SBTarget,
759                      ResolveSymbolContextForAddress,
760                      (const lldb::SBAddress &, uint32_t), addr, resolve_scope);
761 
762   SBSymbolContext sc;
763   SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
764   if (addr.IsValid()) {
765     TargetSP target_sp(GetSP());
766     if (target_sp)
767       target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
768                                                             sc.ref());
769   }
770   return LLDB_RECORD_RESULT(sc);
771 }
772 
773 size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
774                             lldb::SBError &error) {
775   SBError sb_error;
776   size_t bytes_read = 0;
777   TargetSP target_sp(GetSP());
778   if (target_sp) {
779     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
780     bytes_read =
781         target_sp->ReadMemory(addr.ref(), false, buf, size, sb_error.ref());
782   } else {
783     sb_error.SetErrorString("invalid target");
784   }
785 
786   return bytes_read;
787 }
788 
789 SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
790                                                   uint32_t line) {
791   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
792                      (const char *, uint32_t), file, line);
793 
794   return LLDB_RECORD_RESULT(
795       SBBreakpoint(BreakpointCreateByLocation(SBFileSpec(file, false), line)));
796 }
797 
798 SBBreakpoint
799 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
800                                      uint32_t line) {
801   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
802                      (const lldb::SBFileSpec &, uint32_t), sb_file_spec, line);
803 
804   return LLDB_RECORD_RESULT(BreakpointCreateByLocation(sb_file_spec, line, 0));
805 }
806 
807 SBBreakpoint
808 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
809                                      uint32_t line, lldb::addr_t offset) {
810   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
811                      (const lldb::SBFileSpec &, uint32_t, lldb::addr_t),
812                      sb_file_spec, line, offset);
813 
814   SBFileSpecList empty_list;
815   return LLDB_RECORD_RESULT(
816       BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list));
817 }
818 
819 SBBreakpoint
820 SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
821                                      uint32_t line, lldb::addr_t offset,
822                                      SBFileSpecList &sb_module_list) {
823   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
824                      (const lldb::SBFileSpec &, uint32_t, lldb::addr_t,
825                       lldb::SBFileSpecList &),
826                      sb_file_spec, line, offset, sb_module_list);
827 
828   return LLDB_RECORD_RESULT(BreakpointCreateByLocation(sb_file_spec, line, 0,
829                                                        offset, sb_module_list));
830 }
831 
832 SBBreakpoint SBTarget::BreakpointCreateByLocation(
833     const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
834     lldb::addr_t offset, SBFileSpecList &sb_module_list) {
835   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByLocation,
836                      (const lldb::SBFileSpec &, uint32_t, uint32_t,
837                       lldb::addr_t, lldb::SBFileSpecList &),
838                      sb_file_spec, line, column, offset, sb_module_list);
839 
840   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
841 
842   SBBreakpoint sb_bp;
843   TargetSP target_sp(GetSP());
844   if (target_sp && line != 0) {
845     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
846 
847     const LazyBool check_inlines = eLazyBoolCalculate;
848     const LazyBool skip_prologue = eLazyBoolCalculate;
849     const bool internal = false;
850     const bool hardware = false;
851     const LazyBool move_to_nearest_code = eLazyBoolCalculate;
852     const FileSpecList *module_list = nullptr;
853     if (sb_module_list.GetSize() > 0) {
854       module_list = sb_module_list.get();
855     }
856     sb_bp = target_sp->CreateBreakpoint(
857         module_list, *sb_file_spec, line, column, offset, check_inlines,
858         skip_prologue, internal, hardware, move_to_nearest_code);
859   }
860 
861   if (log) {
862     SBStream sstr;
863     sb_bp.GetDescription(sstr);
864     char path[PATH_MAX];
865     sb_file_spec->GetPath(path, sizeof(path));
866     log->Printf("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => "
867                 "SBBreakpoint(%p): %s",
868                 static_cast<void *>(target_sp.get()), path, line,
869                 static_cast<void *>(sb_bp.GetSP().get()), sstr.GetData());
870   }
871 
872   return LLDB_RECORD_RESULT(sb_bp);
873 }
874 
875 SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
876                                               const char *module_name) {
877   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
878                      (const char *, const char *), symbol_name, module_name);
879 
880   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
881 
882   SBBreakpoint sb_bp;
883   TargetSP target_sp(GetSP());
884   if (target_sp.get()) {
885     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
886 
887     const bool internal = false;
888     const bool hardware = false;
889     const LazyBool skip_prologue = eLazyBoolCalculate;
890     const lldb::addr_t offset = 0;
891     if (module_name && module_name[0]) {
892       FileSpecList module_spec_list;
893       module_spec_list.Append(FileSpec(module_name));
894       sb_bp = target_sp->CreateBreakpoint(
895           &module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto,
896           eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
897     } else {
898       sb_bp = target_sp->CreateBreakpoint(
899           NULL, NULL, symbol_name, eFunctionNameTypeAuto, eLanguageTypeUnknown,
900           offset, skip_prologue, internal, hardware);
901     }
902   }
903 
904   if (log)
905     log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
906                 "module=\"%s\") => SBBreakpoint(%p)",
907                 static_cast<void *>(target_sp.get()), symbol_name, module_name,
908                 static_cast<void *>(sb_bp.GetSP().get()));
909 
910   return LLDB_RECORD_RESULT(sb_bp);
911 }
912 
913 lldb::SBBreakpoint
914 SBTarget::BreakpointCreateByName(const char *symbol_name,
915                                  const SBFileSpecList &module_list,
916                                  const SBFileSpecList &comp_unit_list) {
917   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
918                      (const char *, const lldb::SBFileSpecList &,
919                       const lldb::SBFileSpecList &),
920                      symbol_name, module_list, comp_unit_list);
921 
922   lldb::FunctionNameType name_type_mask = eFunctionNameTypeAuto;
923   return LLDB_RECORD_RESULT(
924       BreakpointCreateByName(symbol_name, name_type_mask, eLanguageTypeUnknown,
925                              module_list, comp_unit_list));
926 }
927 
928 lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
929     const char *symbol_name, uint32_t name_type_mask,
930     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
931   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
932                      (const char *, uint32_t, const lldb::SBFileSpecList &,
933                       const lldb::SBFileSpecList &),
934                      symbol_name, name_type_mask, module_list, comp_unit_list);
935 
936   return LLDB_RECORD_RESULT(
937       BreakpointCreateByName(symbol_name, name_type_mask, eLanguageTypeUnknown,
938                              module_list, comp_unit_list));
939 }
940 
941 lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
942     const char *symbol_name, uint32_t name_type_mask,
943     LanguageType symbol_language, const SBFileSpecList &module_list,
944     const SBFileSpecList &comp_unit_list) {
945   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByName,
946                      (const char *, uint32_t, lldb::LanguageType,
947                       const lldb::SBFileSpecList &,
948                       const lldb::SBFileSpecList &),
949                      symbol_name, name_type_mask, symbol_language, module_list,
950                      comp_unit_list);
951 
952   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
953 
954   SBBreakpoint sb_bp;
955   TargetSP target_sp(GetSP());
956   if (target_sp && symbol_name && symbol_name[0]) {
957     const bool internal = false;
958     const bool hardware = false;
959     const LazyBool skip_prologue = eLazyBoolCalculate;
960     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
961     FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
962     sb_bp = target_sp->CreateBreakpoint(module_list.get(), comp_unit_list.get(),
963                                         symbol_name, mask, symbol_language, 0,
964                                         skip_prologue, internal, hardware);
965   }
966 
967   if (log)
968     log->Printf("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", "
969                 "name_type: %d) => SBBreakpoint(%p)",
970                 static_cast<void *>(target_sp.get()), symbol_name,
971                 name_type_mask, static_cast<void *>(sb_bp.GetSP().get()));
972 
973   return LLDB_RECORD_RESULT(sb_bp);
974 }
975 
976 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
977     const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
978     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
979   LLDB_RECORD_METHOD(
980       lldb::SBBreakpoint, SBTarget, BreakpointCreateByNames,
981       (const char **, uint32_t, uint32_t, const lldb::SBFileSpecList &,
982        const lldb::SBFileSpecList &),
983       symbol_names, num_names, name_type_mask, module_list, comp_unit_list);
984 
985   return LLDB_RECORD_RESULT(BreakpointCreateByNames(
986       symbol_names, num_names, name_type_mask, eLanguageTypeUnknown,
987       module_list, comp_unit_list));
988 }
989 
990 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
991     const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
992     LanguageType symbol_language, const SBFileSpecList &module_list,
993     const SBFileSpecList &comp_unit_list) {
994   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByNames,
995                      (const char **, uint32_t, uint32_t, lldb::LanguageType,
996                       const lldb::SBFileSpecList &,
997                       const lldb::SBFileSpecList &),
998                      symbol_names, num_names, name_type_mask, symbol_language,
999                      module_list, comp_unit_list);
1000 
1001   return LLDB_RECORD_RESULT(BreakpointCreateByNames(
1002       symbol_names, num_names, name_type_mask, eLanguageTypeUnknown, 0,
1003       module_list, comp_unit_list));
1004 }
1005 
1006 lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
1007     const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
1008     LanguageType symbol_language, lldb::addr_t offset,
1009     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
1010   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByNames,
1011                      (const char **, uint32_t, uint32_t, lldb::LanguageType,
1012                       lldb::addr_t, const lldb::SBFileSpecList &,
1013                       const lldb::SBFileSpecList &),
1014                      symbol_names, num_names, name_type_mask, symbol_language,
1015                      offset, module_list, comp_unit_list);
1016 
1017   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1018 
1019   SBBreakpoint sb_bp;
1020   TargetSP target_sp(GetSP());
1021   if (target_sp && num_names > 0) {
1022     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1023     const bool internal = false;
1024     const bool hardware = false;
1025     FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
1026     const LazyBool skip_prologue = eLazyBoolCalculate;
1027     sb_bp = target_sp->CreateBreakpoint(
1028         module_list.get(), comp_unit_list.get(), symbol_names, num_names, mask,
1029         symbol_language, offset, skip_prologue, internal, hardware);
1030   }
1031 
1032   if (log) {
1033     log->Printf("SBTarget(%p)::BreakpointCreateByName (symbols={",
1034                 static_cast<void *>(target_sp.get()));
1035     for (uint32_t i = 0; i < num_names; i++) {
1036       char sep;
1037       if (i < num_names - 1)
1038         sep = ',';
1039       else
1040         sep = '}';
1041       if (symbol_names[i] != NULL)
1042         log->Printf("\"%s\"%c ", symbol_names[i], sep);
1043       else
1044         log->Printf("\"<NULL>\"%c ", sep);
1045     }
1046     log->Printf("name_type: %d) => SBBreakpoint(%p)", name_type_mask,
1047                 static_cast<void *>(sb_bp.GetSP().get()));
1048   }
1049 
1050   return LLDB_RECORD_RESULT(sb_bp);
1051 }
1052 
1053 SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
1054                                                const char *module_name) {
1055   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByRegex,
1056                      (const char *, const char *), symbol_name_regex,
1057                      module_name);
1058 
1059   SBFileSpecList module_spec_list;
1060   SBFileSpecList comp_unit_list;
1061   if (module_name && module_name[0]) {
1062     module_spec_list.Append(FileSpec(module_name));
1063   }
1064   return LLDB_RECORD_RESULT(
1065       BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
1066                               module_spec_list, comp_unit_list));
1067 }
1068 
1069 lldb::SBBreakpoint
1070 SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
1071                                   const SBFileSpecList &module_list,
1072                                   const SBFileSpecList &comp_unit_list) {
1073   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByRegex,
1074                      (const char *, const lldb::SBFileSpecList &,
1075                       const lldb::SBFileSpecList &),
1076                      symbol_name_regex, module_list, comp_unit_list);
1077 
1078   return LLDB_RECORD_RESULT(BreakpointCreateByRegex(
1079       symbol_name_regex, eLanguageTypeUnknown, module_list, comp_unit_list));
1080 }
1081 
1082 lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
1083     const char *symbol_name_regex, LanguageType symbol_language,
1084     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
1085   LLDB_RECORD_METHOD(
1086       lldb::SBBreakpoint, SBTarget, BreakpointCreateByRegex,
1087       (const char *, lldb::LanguageType, const lldb::SBFileSpecList &,
1088        const lldb::SBFileSpecList &),
1089       symbol_name_regex, symbol_language, module_list, comp_unit_list);
1090 
1091   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1092 
1093   SBBreakpoint sb_bp;
1094   TargetSP target_sp(GetSP());
1095   if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
1096     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1097     RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
1098     const bool internal = false;
1099     const bool hardware = false;
1100     const LazyBool skip_prologue = eLazyBoolCalculate;
1101 
1102     sb_bp = target_sp->CreateFuncRegexBreakpoint(
1103         module_list.get(), comp_unit_list.get(), regexp, symbol_language,
1104         skip_prologue, internal, hardware);
1105   }
1106 
1107   if (log)
1108     log->Printf("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") "
1109                 "=> SBBreakpoint(%p)",
1110                 static_cast<void *>(target_sp.get()), symbol_name_regex,
1111                 static_cast<void *>(sb_bp.GetSP().get()));
1112 
1113   return LLDB_RECORD_RESULT(sb_bp);
1114 }
1115 
1116 SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
1117   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateByAddress,
1118                      (lldb::addr_t), address);
1119 
1120   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1121 
1122   SBBreakpoint sb_bp;
1123   TargetSP target_sp(GetSP());
1124   if (target_sp) {
1125     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1126     const bool hardware = false;
1127     sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
1128   }
1129 
1130   if (log)
1131     log->Printf("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64
1132                 ") => SBBreakpoint(%p)",
1133                 static_cast<void *>(target_sp.get()),
1134                 static_cast<uint64_t>(address),
1135                 static_cast<void *>(sb_bp.GetSP().get()));
1136 
1137   return LLDB_RECORD_RESULT(sb_bp);
1138 }
1139 
1140 SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
1141   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateBySBAddress,
1142                      (lldb::SBAddress &), sb_address);
1143 
1144   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1145 
1146   SBBreakpoint sb_bp;
1147   TargetSP target_sp(GetSP());
1148   if (!sb_address.IsValid()) {
1149     if (log)
1150       log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress called with "
1151                   "invalid address",
1152                   static_cast<void *>(target_sp.get()));
1153     return LLDB_RECORD_RESULT(sb_bp);
1154   }
1155 
1156   if (target_sp) {
1157     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1158     const bool hardware = false;
1159     sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
1160   }
1161 
1162   if (log) {
1163     SBStream s;
1164     sb_address.GetDescription(s);
1165     log->Printf("SBTarget(%p)::BreakpointCreateBySBAddress (address=%s) => "
1166                 "SBBreakpoint(%p)",
1167                 static_cast<void *>(target_sp.get()), s.GetData(),
1168                 static_cast<void *>(sb_bp.GetSP().get()));
1169   }
1170 
1171   return LLDB_RECORD_RESULT(sb_bp);
1172 }
1173 
1174 lldb::SBBreakpoint
1175 SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
1176                                         const lldb::SBFileSpec &source_file,
1177                                         const char *module_name) {
1178   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget,
1179                      BreakpointCreateBySourceRegex,
1180                      (const char *, const lldb::SBFileSpec &, const char *),
1181                      source_regex, source_file, module_name);
1182 
1183   SBFileSpecList module_spec_list;
1184 
1185   if (module_name && module_name[0]) {
1186     module_spec_list.Append(FileSpec(module_name));
1187   }
1188 
1189   SBFileSpecList source_file_list;
1190   if (source_file.IsValid()) {
1191     source_file_list.Append(source_file);
1192   }
1193 
1194   return LLDB_RECORD_RESULT(BreakpointCreateBySourceRegex(
1195       source_regex, module_spec_list, source_file_list));
1196 }
1197 
1198 lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1199     const char *source_regex, const SBFileSpecList &module_list,
1200     const lldb::SBFileSpecList &source_file_list) {
1201   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget,
1202                      BreakpointCreateBySourceRegex,
1203                      (const char *, const lldb::SBFileSpecList &,
1204                       const lldb::SBFileSpecList &),
1205                      source_regex, module_list, source_file_list);
1206 
1207   return LLDB_RECORD_RESULT(BreakpointCreateBySourceRegex(
1208       source_regex, module_list, source_file_list, SBStringList()));
1209 }
1210 
1211 lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
1212     const char *source_regex, const SBFileSpecList &module_list,
1213     const lldb::SBFileSpecList &source_file_list,
1214     const SBStringList &func_names) {
1215   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget,
1216                      BreakpointCreateBySourceRegex,
1217                      (const char *, const lldb::SBFileSpecList &,
1218                       const lldb::SBFileSpecList &, const lldb::SBStringList &),
1219                      source_regex, module_list, source_file_list, func_names);
1220 
1221   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1222 
1223   SBBreakpoint sb_bp;
1224   TargetSP target_sp(GetSP());
1225   if (target_sp && source_regex && source_regex[0]) {
1226     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1227     const bool hardware = false;
1228     const LazyBool move_to_nearest_code = eLazyBoolCalculate;
1229     RegularExpression regexp((llvm::StringRef(source_regex)));
1230     std::unordered_set<std::string> func_names_set;
1231     for (size_t i = 0; i < func_names.GetSize(); i++) {
1232       func_names_set.insert(func_names.GetStringAtIndex(i));
1233     }
1234 
1235     sb_bp = target_sp->CreateSourceRegexBreakpoint(
1236         module_list.get(), source_file_list.get(), func_names_set, regexp,
1237         false, hardware, move_to_nearest_code);
1238   }
1239 
1240   if (log)
1241     log->Printf("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") "
1242                 "=> SBBreakpoint(%p)",
1243                 static_cast<void *>(target_sp.get()), source_regex,
1244                 static_cast<void *>(sb_bp.GetSP().get()));
1245 
1246   return LLDB_RECORD_RESULT(sb_bp);
1247 }
1248 
1249 lldb::SBBreakpoint
1250 SBTarget::BreakpointCreateForException(lldb::LanguageType language,
1251                                        bool catch_bp, bool throw_bp) {
1252   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, BreakpointCreateForException,
1253                      (lldb::LanguageType, bool, bool), language, catch_bp,
1254                      throw_bp);
1255 
1256   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1257 
1258   SBBreakpoint sb_bp;
1259   TargetSP target_sp(GetSP());
1260   if (target_sp) {
1261     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1262     const bool hardware = false;
1263     sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
1264                                                   hardware);
1265   }
1266 
1267   if (log)
1268     log->Printf("SBTarget(%p)::BreakpointCreateForException (Language: %s, catch: "
1269                 "%s throw: %s) => SBBreakpoint(%p)",
1270                 static_cast<void *>(target_sp.get()),
1271                 Language::GetNameForLanguageType(language),
1272                 catch_bp ? "on" : "off", throw_bp ? "on" : "off",
1273                 static_cast<void *>(sb_bp.GetSP().get()));
1274 
1275   return LLDB_RECORD_RESULT(sb_bp);
1276 }
1277 
1278 lldb::SBBreakpoint SBTarget::BreakpointCreateFromScript(
1279     const char *class_name, SBStructuredData &extra_args,
1280     const SBFileSpecList &module_list, const SBFileSpecList &file_list,
1281     bool request_hardware) {
1282   LLDB_RECORD_METHOD(
1283       lldb::SBBreakpoint, SBTarget, BreakpointCreateFromScript,
1284       (const char *, lldb::SBStructuredData &, const lldb::SBFileSpecList &,
1285        const lldb::SBFileSpecList &, bool),
1286       class_name, extra_args, module_list, file_list, request_hardware);
1287 
1288   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1289 
1290   SBBreakpoint sb_bp;
1291   TargetSP target_sp(GetSP());
1292   if (target_sp) {
1293     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1294     Status error;
1295 
1296     StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP();
1297     sb_bp =
1298         target_sp->CreateScriptedBreakpoint(class_name,
1299                                             module_list.get(),
1300                                             file_list.get(),
1301                                             false, /* internal */
1302                                             request_hardware,
1303                                             obj_sp,
1304                                             &error);
1305   }
1306   if (log)
1307     log->Printf("SBTarget(%p)::BreakpointCreateFromScript (class name: %s) "
1308                 " => SBBreakpoint(%p)",
1309                 static_cast<void *>(target_sp.get()),
1310                 class_name,
1311                 static_cast<void *>(sb_bp.GetSP().get()));
1312 
1313   return LLDB_RECORD_RESULT(sb_bp);
1314 }
1315 
1316 uint32_t SBTarget::GetNumBreakpoints() const {
1317   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetNumBreakpoints);
1318 
1319   TargetSP target_sp(GetSP());
1320   if (target_sp) {
1321     // The breakpoint list is thread safe, no need to lock
1322     return target_sp->GetBreakpointList().GetSize();
1323   }
1324   return 0;
1325 }
1326 
1327 SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
1328   LLDB_RECORD_METHOD_CONST(lldb::SBBreakpoint, SBTarget, GetBreakpointAtIndex,
1329                            (uint32_t), idx);
1330 
1331   SBBreakpoint sb_breakpoint;
1332   TargetSP target_sp(GetSP());
1333   if (target_sp) {
1334     // The breakpoint list is thread safe, no need to lock
1335     sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1336   }
1337   return LLDB_RECORD_RESULT(sb_breakpoint);
1338 }
1339 
1340 bool SBTarget::BreakpointDelete(break_id_t bp_id) {
1341   LLDB_RECORD_METHOD(bool, SBTarget, BreakpointDelete, (lldb::break_id_t),
1342                      bp_id);
1343 
1344   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1345 
1346   bool result = false;
1347   TargetSP target_sp(GetSP());
1348   if (target_sp) {
1349     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1350     result = target_sp->RemoveBreakpointByID(bp_id);
1351   }
1352 
1353   if (log)
1354     log->Printf("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i",
1355                 static_cast<void *>(target_sp.get()),
1356                 static_cast<uint32_t>(bp_id), result);
1357 
1358   return result;
1359 }
1360 
1361 SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
1362   LLDB_RECORD_METHOD(lldb::SBBreakpoint, SBTarget, FindBreakpointByID,
1363                      (lldb::break_id_t), bp_id);
1364 
1365   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1366 
1367   SBBreakpoint sb_breakpoint;
1368   TargetSP target_sp(GetSP());
1369   if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
1370     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1371     sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
1372   }
1373 
1374   if (log)
1375     log->Printf(
1376         "SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1377         static_cast<void *>(target_sp.get()), static_cast<uint32_t>(bp_id),
1378         static_cast<void *>(sb_breakpoint.GetSP().get()));
1379 
1380   return LLDB_RECORD_RESULT(sb_breakpoint);
1381 }
1382 
1383 bool SBTarget::FindBreakpointsByName(const char *name,
1384                                      SBBreakpointList &bkpts) {
1385   LLDB_RECORD_METHOD(bool, SBTarget, FindBreakpointsByName,
1386                      (const char *, lldb::SBBreakpointList &), name, bkpts);
1387 
1388   TargetSP target_sp(GetSP());
1389   if (target_sp) {
1390     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1391     BreakpointList bkpt_list(false);
1392     bool is_valid =
1393         target_sp->GetBreakpointList().FindBreakpointsByName(name, bkpt_list);
1394     if (!is_valid)
1395       return false;
1396     for (BreakpointSP bkpt_sp : bkpt_list.Breakpoints()) {
1397       bkpts.AppendByID(bkpt_sp->GetID());
1398     }
1399   }
1400   return true;
1401 }
1402 
1403 void SBTarget::GetBreakpointNames(SBStringList &names) {
1404   LLDB_RECORD_METHOD(void, SBTarget, GetBreakpointNames, (lldb::SBStringList &),
1405                      names);
1406 
1407   names.Clear();
1408 
1409   TargetSP target_sp(GetSP());
1410   if (target_sp) {
1411     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1412 
1413     std::vector<std::string> name_vec;
1414     target_sp->GetBreakpointNames(name_vec);
1415     for (auto name : name_vec)
1416       names.AppendString(name.c_str());
1417   }
1418 }
1419 
1420 void SBTarget::DeleteBreakpointName(const char *name) {
1421   LLDB_RECORD_METHOD(void, SBTarget, DeleteBreakpointName, (const char *),
1422                      name);
1423 
1424   TargetSP target_sp(GetSP());
1425   if (target_sp) {
1426     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1427     target_sp->DeleteBreakpointName(ConstString(name));
1428   }
1429 }
1430 
1431 bool SBTarget::EnableAllBreakpoints() {
1432   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, EnableAllBreakpoints);
1433 
1434   TargetSP target_sp(GetSP());
1435   if (target_sp) {
1436     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1437     target_sp->EnableAllowedBreakpoints();
1438     return true;
1439   }
1440   return false;
1441 }
1442 
1443 bool SBTarget::DisableAllBreakpoints() {
1444   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DisableAllBreakpoints);
1445 
1446   TargetSP target_sp(GetSP());
1447   if (target_sp) {
1448     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1449     target_sp->DisableAllowedBreakpoints();
1450     return true;
1451   }
1452   return false;
1453 }
1454 
1455 bool SBTarget::DeleteAllBreakpoints() {
1456   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DeleteAllBreakpoints);
1457 
1458   TargetSP target_sp(GetSP());
1459   if (target_sp) {
1460     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1461     target_sp->RemoveAllowedBreakpoints();
1462     return true;
1463   }
1464   return false;
1465 }
1466 
1467 lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1468                                                   SBBreakpointList &new_bps) {
1469   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, BreakpointsCreateFromFile,
1470                      (lldb::SBFileSpec &, lldb::SBBreakpointList &),
1471                      source_file, new_bps);
1472 
1473   SBStringList empty_name_list;
1474   return LLDB_RECORD_RESULT(
1475       BreakpointsCreateFromFile(source_file, empty_name_list, new_bps));
1476 }
1477 
1478 lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
1479                                                   SBStringList &matching_names,
1480                                                   SBBreakpointList &new_bps) {
1481   LLDB_RECORD_METHOD(
1482       lldb::SBError, SBTarget, BreakpointsCreateFromFile,
1483       (lldb::SBFileSpec &, lldb::SBStringList &, lldb::SBBreakpointList &),
1484       source_file, matching_names, new_bps);
1485 
1486   SBError sberr;
1487   TargetSP target_sp(GetSP());
1488   if (!target_sp) {
1489     sberr.SetErrorString(
1490         "BreakpointCreateFromFile called with invalid target.");
1491     return LLDB_RECORD_RESULT(sberr);
1492   }
1493   std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1494 
1495   BreakpointIDList bp_ids;
1496 
1497   std::vector<std::string> name_vector;
1498   size_t num_names = matching_names.GetSize();
1499   for (size_t i = 0; i < num_names; i++)
1500     name_vector.push_back(matching_names.GetStringAtIndex(i));
1501 
1502   sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
1503                                                      name_vector, bp_ids);
1504   if (sberr.Fail())
1505     return LLDB_RECORD_RESULT(sberr);
1506 
1507   size_t num_bkpts = bp_ids.GetSize();
1508   for (size_t i = 0; i < num_bkpts; i++) {
1509     BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1510     new_bps.AppendByID(bp_id.GetBreakpointID());
1511   }
1512   return LLDB_RECORD_RESULT(sberr);
1513 }
1514 
1515 lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
1516   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, BreakpointsWriteToFile,
1517                      (lldb::SBFileSpec &), dest_file);
1518 
1519   SBError sberr;
1520   TargetSP target_sp(GetSP());
1521   if (!target_sp) {
1522     sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1523     return LLDB_RECORD_RESULT(sberr);
1524   }
1525   SBBreakpointList bkpt_list(*this);
1526   return LLDB_RECORD_RESULT(BreakpointsWriteToFile(dest_file, bkpt_list));
1527 }
1528 
1529 lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
1530                                                SBBreakpointList &bkpt_list,
1531                                                bool append) {
1532   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, BreakpointsWriteToFile,
1533                      (lldb::SBFileSpec &, lldb::SBBreakpointList &, bool),
1534                      dest_file, bkpt_list, append);
1535 
1536   SBError sberr;
1537   TargetSP target_sp(GetSP());
1538   if (!target_sp) {
1539     sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
1540     return LLDB_RECORD_RESULT(sberr);
1541   }
1542 
1543   std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1544   BreakpointIDList bp_id_list;
1545   bkpt_list.CopyToBreakpointIDList(bp_id_list);
1546   sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
1547                                                       bp_id_list, append);
1548   return LLDB_RECORD_RESULT(sberr);
1549 }
1550 
1551 uint32_t SBTarget::GetNumWatchpoints() const {
1552   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetNumWatchpoints);
1553 
1554   TargetSP target_sp(GetSP());
1555   if (target_sp) {
1556     // The watchpoint list is thread safe, no need to lock
1557     return target_sp->GetWatchpointList().GetSize();
1558   }
1559   return 0;
1560 }
1561 
1562 SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
1563   LLDB_RECORD_METHOD_CONST(lldb::SBWatchpoint, SBTarget, GetWatchpointAtIndex,
1564                            (uint32_t), idx);
1565 
1566   SBWatchpoint sb_watchpoint;
1567   TargetSP target_sp(GetSP());
1568   if (target_sp) {
1569     // The watchpoint list is thread safe, no need to lock
1570     sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
1571   }
1572   return LLDB_RECORD_RESULT(sb_watchpoint);
1573 }
1574 
1575 bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
1576   LLDB_RECORD_METHOD(bool, SBTarget, DeleteWatchpoint, (lldb::watch_id_t),
1577                      wp_id);
1578 
1579   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1580 
1581   bool result = false;
1582   TargetSP target_sp(GetSP());
1583   if (target_sp) {
1584     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1585     std::unique_lock<std::recursive_mutex> lock;
1586     target_sp->GetWatchpointList().GetListMutex(lock);
1587     result = target_sp->RemoveWatchpointByID(wp_id);
1588   }
1589 
1590   if (log)
1591     log->Printf("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i",
1592                 static_cast<void *>(target_sp.get()),
1593                 static_cast<uint32_t>(wp_id), result);
1594 
1595   return result;
1596 }
1597 
1598 SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
1599   LLDB_RECORD_METHOD(lldb::SBWatchpoint, SBTarget, FindWatchpointByID,
1600                      (lldb::watch_id_t), wp_id);
1601 
1602   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1603 
1604   SBWatchpoint sb_watchpoint;
1605   lldb::WatchpointSP watchpoint_sp;
1606   TargetSP target_sp(GetSP());
1607   if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
1608     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1609     std::unique_lock<std::recursive_mutex> lock;
1610     target_sp->GetWatchpointList().GetListMutex(lock);
1611     watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1612     sb_watchpoint.SetSP(watchpoint_sp);
1613   }
1614 
1615   if (log)
1616     log->Printf(
1617         "SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1618         static_cast<void *>(target_sp.get()), static_cast<uint32_t>(wp_id),
1619         static_cast<void *>(watchpoint_sp.get()));
1620 
1621   return LLDB_RECORD_RESULT(sb_watchpoint);
1622 }
1623 
1624 lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1625                                           bool read, bool write,
1626                                           SBError &error) {
1627   LLDB_RECORD_METHOD(lldb::SBWatchpoint, SBTarget, WatchAddress,
1628                      (lldb::addr_t, size_t, bool, bool, lldb::SBError &), addr,
1629                      size, read, write, error);
1630 
1631   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1632 
1633   SBWatchpoint sb_watchpoint;
1634   lldb::WatchpointSP watchpoint_sp;
1635   TargetSP target_sp(GetSP());
1636   if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
1637       size > 0) {
1638     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1639     uint32_t watch_type = 0;
1640     if (read)
1641       watch_type |= LLDB_WATCH_TYPE_READ;
1642     if (write)
1643       watch_type |= LLDB_WATCH_TYPE_WRITE;
1644     if (watch_type == 0) {
1645       error.SetErrorString(
1646           "Can't create a watchpoint that is neither read nor write.");
1647       return LLDB_RECORD_RESULT(sb_watchpoint);
1648     }
1649 
1650     // Target::CreateWatchpoint() is thread safe.
1651     Status cw_error;
1652     // This API doesn't take in a type, so we can't figure out what it is.
1653     CompilerType *type = NULL;
1654     watchpoint_sp =
1655         target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1656     error.SetError(cw_error);
1657     sb_watchpoint.SetSP(watchpoint_sp);
1658   }
1659 
1660   if (log)
1661     log->Printf("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64
1662                 ", 0x%u) => SBWatchpoint(%p)",
1663                 static_cast<void *>(target_sp.get()), addr,
1664                 static_cast<uint32_t>(size),
1665                 static_cast<void *>(watchpoint_sp.get()));
1666 
1667   return LLDB_RECORD_RESULT(sb_watchpoint);
1668 }
1669 
1670 bool SBTarget::EnableAllWatchpoints() {
1671   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, EnableAllWatchpoints);
1672 
1673   TargetSP target_sp(GetSP());
1674   if (target_sp) {
1675     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1676     std::unique_lock<std::recursive_mutex> lock;
1677     target_sp->GetWatchpointList().GetListMutex(lock);
1678     target_sp->EnableAllWatchpoints();
1679     return true;
1680   }
1681   return false;
1682 }
1683 
1684 bool SBTarget::DisableAllWatchpoints() {
1685   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DisableAllWatchpoints);
1686 
1687   TargetSP target_sp(GetSP());
1688   if (target_sp) {
1689     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1690     std::unique_lock<std::recursive_mutex> lock;
1691     target_sp->GetWatchpointList().GetListMutex(lock);
1692     target_sp->DisableAllWatchpoints();
1693     return true;
1694   }
1695   return false;
1696 }
1697 
1698 SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
1699                                          SBType type) {
1700   LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, CreateValueFromAddress,
1701                      (const char *, lldb::SBAddress, lldb::SBType), name, addr,
1702                      type);
1703 
1704   SBValue sb_value;
1705   lldb::ValueObjectSP new_value_sp;
1706   if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
1707     lldb::addr_t load_addr(addr.GetLoadAddress(*this));
1708     ExecutionContext exe_ctx(
1709         ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1710     CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1711     new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
1712                                                              exe_ctx, ast_type);
1713   }
1714   sb_value.SetSP(new_value_sp);
1715   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1716   if (log) {
1717     if (new_value_sp)
1718       log->Printf("SBTarget(%p)::CreateValueFromAddress => \"%s\"",
1719                   static_cast<void *>(m_opaque_sp.get()),
1720                   new_value_sp->GetName().AsCString());
1721     else
1722       log->Printf("SBTarget(%p)::CreateValueFromAddress => NULL",
1723                   static_cast<void *>(m_opaque_sp.get()));
1724   }
1725   return LLDB_RECORD_RESULT(sb_value);
1726 }
1727 
1728 lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
1729                                             lldb::SBType type) {
1730   LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, CreateValueFromData,
1731                      (const char *, lldb::SBData, lldb::SBType), name, data,
1732                      type);
1733 
1734   SBValue sb_value;
1735   lldb::ValueObjectSP new_value_sp;
1736   if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
1737     DataExtractorSP extractor(*data);
1738     ExecutionContext exe_ctx(
1739         ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1740     CompilerType ast_type(type.GetSP()->GetCompilerType(true));
1741     new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
1742                                                           exe_ctx, ast_type);
1743   }
1744   sb_value.SetSP(new_value_sp);
1745   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1746   if (log) {
1747     if (new_value_sp)
1748       log->Printf("SBTarget(%p)::CreateValueFromData => \"%s\"",
1749                   static_cast<void *>(m_opaque_sp.get()),
1750                   new_value_sp->GetName().AsCString());
1751     else
1752       log->Printf("SBTarget(%p)::CreateValueFromData => NULL",
1753                   static_cast<void *>(m_opaque_sp.get()));
1754   }
1755   return LLDB_RECORD_RESULT(sb_value);
1756 }
1757 
1758 lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
1759                                                   const char *expr) {
1760   LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, CreateValueFromExpression,
1761                      (const char *, const char *), name, expr);
1762 
1763   SBValue sb_value;
1764   lldb::ValueObjectSP new_value_sp;
1765   if (IsValid() && name && *name && expr && *expr) {
1766     ExecutionContext exe_ctx(
1767         ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
1768     new_value_sp =
1769         ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
1770   }
1771   sb_value.SetSP(new_value_sp);
1772   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1773   if (log) {
1774     if (new_value_sp)
1775       log->Printf("SBTarget(%p)::CreateValueFromExpression => \"%s\"",
1776                   static_cast<void *>(m_opaque_sp.get()),
1777                   new_value_sp->GetName().AsCString());
1778     else
1779       log->Printf("SBTarget(%p)::CreateValueFromExpression => NULL",
1780                   static_cast<void *>(m_opaque_sp.get()));
1781   }
1782   return LLDB_RECORD_RESULT(sb_value);
1783 }
1784 
1785 bool SBTarget::DeleteAllWatchpoints() {
1786   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTarget, DeleteAllWatchpoints);
1787 
1788   TargetSP target_sp(GetSP());
1789   if (target_sp) {
1790     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1791     std::unique_lock<std::recursive_mutex> lock;
1792     target_sp->GetWatchpointList().GetListMutex(lock);
1793     target_sp->RemoveAllWatchpoints();
1794     return true;
1795   }
1796   return false;
1797 }
1798 
1799 void SBTarget::AppendImageSearchPath(const char *from, const char *to,
1800                                      lldb::SBError &error) {
1801   LLDB_RECORD_METHOD(void, SBTarget, AppendImageSearchPath,
1802                      (const char *, const char *, lldb::SBError &), from, to,
1803                      error);
1804 
1805   TargetSP target_sp(GetSP());
1806   if (!target_sp)
1807     return error.SetErrorString("invalid target");
1808 
1809   const ConstString csFrom(from), csTo(to);
1810   if (!csFrom)
1811     return error.SetErrorString("<from> path can't be empty");
1812   if (!csTo)
1813     return error.SetErrorString("<to> path can't be empty");
1814 
1815   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1816   if (log)
1817     log->Printf("SBTarget(%p)::%s: '%s' -> '%s'",
1818                 static_cast<void *>(target_sp.get()),  __FUNCTION__,
1819                 from, to);
1820   target_sp->GetImageSearchPathList().Append(csFrom, csTo, true);
1821 }
1822 
1823 lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1824                                    const char *uuid_cstr) {
1825   LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, AddModule,
1826                      (const char *, const char *, const char *), path, triple,
1827                      uuid_cstr);
1828 
1829   return LLDB_RECORD_RESULT(AddModule(path, triple, uuid_cstr, NULL));
1830 }
1831 
1832 lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
1833                                    const char *uuid_cstr, const char *symfile) {
1834   LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, AddModule,
1835                      (const char *, const char *, const char *, const char *),
1836                      path, triple, uuid_cstr, symfile);
1837 
1838   lldb::SBModule sb_module;
1839   TargetSP target_sp(GetSP());
1840   if (target_sp) {
1841     ModuleSpec module_spec;
1842     if (path)
1843       module_spec.GetFileSpec().SetFile(path, FileSpec::Style::native);
1844 
1845     if (uuid_cstr)
1846       module_spec.GetUUID().SetFromStringRef(uuid_cstr);
1847 
1848     if (triple)
1849       module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec(
1850           target_sp->GetPlatform().get(), triple);
1851     else
1852       module_spec.GetArchitecture() = target_sp->GetArchitecture();
1853 
1854     if (symfile)
1855       module_spec.GetSymbolFileSpec().SetFile(symfile, FileSpec::Style::native);
1856 
1857     sb_module.SetSP(target_sp->GetSharedModule(module_spec));
1858   }
1859   return LLDB_RECORD_RESULT(sb_module);
1860 }
1861 
1862 lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
1863   LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, AddModule,
1864                      (const lldb::SBModuleSpec &), module_spec);
1865 
1866   lldb::SBModule sb_module;
1867   TargetSP target_sp(GetSP());
1868   if (target_sp)
1869     sb_module.SetSP(target_sp->GetSharedModule(*module_spec.m_opaque_up));
1870   return LLDB_RECORD_RESULT(sb_module);
1871 }
1872 
1873 bool SBTarget::AddModule(lldb::SBModule &module) {
1874   LLDB_RECORD_METHOD(bool, SBTarget, AddModule, (lldb::SBModule &), module);
1875 
1876   TargetSP target_sp(GetSP());
1877   if (target_sp) {
1878     target_sp->GetImages().AppendIfNeeded(module.GetSP());
1879     return true;
1880   }
1881   return false;
1882 }
1883 
1884 uint32_t SBTarget::GetNumModules() const {
1885   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBTarget, GetNumModules);
1886 
1887   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1888 
1889   uint32_t num = 0;
1890   TargetSP target_sp(GetSP());
1891   if (target_sp) {
1892     // The module list is thread safe, no need to lock
1893     num = target_sp->GetImages().GetSize();
1894   }
1895 
1896   if (log)
1897     log->Printf("SBTarget(%p)::GetNumModules () => %d",
1898                 static_cast<void *>(target_sp.get()), num);
1899 
1900   return num;
1901 }
1902 
1903 void SBTarget::Clear() {
1904   LLDB_RECORD_METHOD_NO_ARGS(void, SBTarget, Clear);
1905 
1906   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
1907 
1908   if (log)
1909     log->Printf("SBTarget(%p)::Clear ()",
1910                 static_cast<void *>(m_opaque_sp.get()));
1911 
1912   m_opaque_sp.reset();
1913 }
1914 
1915 SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
1916   LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, FindModule,
1917                      (const lldb::SBFileSpec &), sb_file_spec);
1918 
1919   SBModule sb_module;
1920   TargetSP target_sp(GetSP());
1921   if (target_sp && sb_file_spec.IsValid()) {
1922     ModuleSpec module_spec(*sb_file_spec);
1923     // The module list is thread safe, no need to lock
1924     sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
1925   }
1926   return LLDB_RECORD_RESULT(sb_module);
1927 }
1928 
1929 SBSymbolContextList SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
1930   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindCompileUnits,
1931                      (const lldb::SBFileSpec &), sb_file_spec);
1932 
1933   SBSymbolContextList sb_sc_list;
1934   const TargetSP target_sp(GetSP());
1935   if (target_sp && sb_file_spec.IsValid()) {
1936     const bool append = true;
1937     target_sp->GetImages().FindCompileUnits(*sb_file_spec,
1938                                             append, *sb_sc_list);
1939   }
1940   return LLDB_RECORD_RESULT(sb_sc_list);
1941 }
1942 
1943 lldb::ByteOrder SBTarget::GetByteOrder() {
1944   LLDB_RECORD_METHOD_NO_ARGS(lldb::ByteOrder, SBTarget, GetByteOrder);
1945 
1946   TargetSP target_sp(GetSP());
1947   if (target_sp)
1948     return target_sp->GetArchitecture().GetByteOrder();
1949   return eByteOrderInvalid;
1950 }
1951 
1952 const char *SBTarget::GetTriple() {
1953   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTarget, GetTriple);
1954 
1955   TargetSP target_sp(GetSP());
1956   if (target_sp) {
1957     std::string triple(target_sp->GetArchitecture().GetTriple().str());
1958     // Unique the string so we don't run into ownership issues since the const
1959     // strings put the string into the string pool once and the strings never
1960     // comes out
1961     ConstString const_triple(triple.c_str());
1962     return const_triple.GetCString();
1963   }
1964   return NULL;
1965 }
1966 
1967 uint32_t SBTarget::GetDataByteSize() {
1968   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTarget, GetDataByteSize);
1969 
1970   TargetSP target_sp(GetSP());
1971   if (target_sp) {
1972     return target_sp->GetArchitecture().GetDataByteSize();
1973   }
1974   return 0;
1975 }
1976 
1977 uint32_t SBTarget::GetCodeByteSize() {
1978   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTarget, GetCodeByteSize);
1979 
1980   TargetSP target_sp(GetSP());
1981   if (target_sp) {
1982     return target_sp->GetArchitecture().GetCodeByteSize();
1983   }
1984   return 0;
1985 }
1986 
1987 uint32_t SBTarget::GetAddressByteSize() {
1988   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTarget, GetAddressByteSize);
1989 
1990   TargetSP target_sp(GetSP());
1991   if (target_sp)
1992     return target_sp->GetArchitecture().GetAddressByteSize();
1993   return sizeof(void *);
1994 }
1995 
1996 SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
1997   LLDB_RECORD_METHOD(lldb::SBModule, SBTarget, GetModuleAtIndex, (uint32_t),
1998                      idx);
1999 
2000   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
2001 
2002   SBModule sb_module;
2003   ModuleSP module_sp;
2004   TargetSP target_sp(GetSP());
2005   if (target_sp) {
2006     // The module list is thread safe, no need to lock
2007     module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2008     sb_module.SetSP(module_sp);
2009   }
2010 
2011   if (log)
2012     log->Printf("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2013                 static_cast<void *>(target_sp.get()), idx,
2014                 static_cast<void *>(module_sp.get()));
2015 
2016   return LLDB_RECORD_RESULT(sb_module);
2017 }
2018 
2019 bool SBTarget::RemoveModule(lldb::SBModule module) {
2020   LLDB_RECORD_METHOD(bool, SBTarget, RemoveModule, (lldb::SBModule), module);
2021 
2022   TargetSP target_sp(GetSP());
2023   if (target_sp)
2024     return target_sp->GetImages().Remove(module.GetSP());
2025   return false;
2026 }
2027 
2028 SBBroadcaster SBTarget::GetBroadcaster() const {
2029   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBBroadcaster, SBTarget,
2030                                    GetBroadcaster);
2031 
2032   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
2033 
2034   TargetSP target_sp(GetSP());
2035   SBBroadcaster broadcaster(target_sp.get(), false);
2036 
2037   if (log)
2038     log->Printf("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2039                 static_cast<void *>(target_sp.get()),
2040                 static_cast<void *>(broadcaster.get()));
2041 
2042   return LLDB_RECORD_RESULT(broadcaster);
2043 }
2044 
2045 bool SBTarget::GetDescription(SBStream &description,
2046                               lldb::DescriptionLevel description_level) {
2047   LLDB_RECORD_METHOD(bool, SBTarget, GetDescription,
2048                      (lldb::SBStream &, lldb::DescriptionLevel), description,
2049                      description_level);
2050 
2051   Stream &strm = description.ref();
2052 
2053   TargetSP target_sp(GetSP());
2054   if (target_sp) {
2055     target_sp->Dump(&strm, description_level);
2056   } else
2057     strm.PutCString("No value");
2058 
2059   return true;
2060 }
2061 
2062 lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
2063                                                   uint32_t name_type_mask) {
2064   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindFunctions,
2065                      (const char *, uint32_t), name, name_type_mask);
2066 
2067   lldb::SBSymbolContextList sb_sc_list;
2068   if (!name | !name[0])
2069     return LLDB_RECORD_RESULT(sb_sc_list);
2070 
2071   TargetSP target_sp(GetSP());
2072   if (!target_sp)
2073     return LLDB_RECORD_RESULT(sb_sc_list);
2074 
2075   const bool symbols_ok = true;
2076   const bool inlines_ok = true;
2077   const bool append = true;
2078   FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
2079   target_sp->GetImages().FindFunctions(ConstString(name), mask, symbols_ok,
2080                                        inlines_ok, append, *sb_sc_list);
2081   return LLDB_RECORD_RESULT(sb_sc_list);
2082 }
2083 
2084 lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
2085                                                         uint32_t max_matches,
2086                                                         MatchType matchtype) {
2087   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindGlobalFunctions,
2088                      (const char *, uint32_t, lldb::MatchType), name,
2089                      max_matches, matchtype);
2090 
2091   lldb::SBSymbolContextList sb_sc_list;
2092   if (name && name[0]) {
2093     llvm::StringRef name_ref(name);
2094     TargetSP target_sp(GetSP());
2095     if (target_sp) {
2096       std::string regexstr;
2097       switch (matchtype) {
2098       case eMatchTypeRegex:
2099         target_sp->GetImages().FindFunctions(RegularExpression(name_ref), true,
2100                                              true, true, *sb_sc_list);
2101         break;
2102       case eMatchTypeStartsWith:
2103         regexstr = llvm::Regex::escape(name) + ".*";
2104         target_sp->GetImages().FindFunctions(RegularExpression(regexstr), true,
2105                                              true, true, *sb_sc_list);
2106         break;
2107       default:
2108         target_sp->GetImages().FindFunctions(ConstString(name),
2109                                              eFunctionNameTypeAny, true, true,
2110                                              true, *sb_sc_list);
2111         break;
2112       }
2113     }
2114   }
2115   return LLDB_RECORD_RESULT(sb_sc_list);
2116 }
2117 
2118 lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
2119   LLDB_RECORD_METHOD(lldb::SBType, SBTarget, FindFirstType, (const char *),
2120                      typename_cstr);
2121 
2122   TargetSP target_sp(GetSP());
2123   if (typename_cstr && typename_cstr[0] && target_sp) {
2124     ConstString const_typename(typename_cstr);
2125     SymbolContext sc;
2126     const bool exact_match = false;
2127 
2128     const ModuleList &module_list = target_sp->GetImages();
2129     size_t count = module_list.GetSize();
2130     for (size_t idx = 0; idx < count; idx++) {
2131       ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
2132       if (module_sp) {
2133         TypeSP type_sp(
2134             module_sp->FindFirstType(sc, const_typename, exact_match));
2135         if (type_sp)
2136           return LLDB_RECORD_RESULT(SBType(type_sp));
2137       }
2138     }
2139 
2140     // Didn't find the type in the symbols; try the Objective-C runtime if one
2141     // is installed
2142 
2143     ProcessSP process_sp(target_sp->GetProcessSP());
2144 
2145     if (process_sp) {
2146       ObjCLanguageRuntime *objc_language_runtime =
2147           process_sp->GetObjCLanguageRuntime();
2148 
2149       if (objc_language_runtime) {
2150         DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
2151 
2152         if (objc_decl_vendor) {
2153           std::vector<clang::NamedDecl *> decls;
2154 
2155           if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
2156             if (CompilerType type = ClangASTContext::GetTypeForDecl(decls[0])) {
2157               return LLDB_RECORD_RESULT(SBType(type));
2158             }
2159           }
2160         }
2161       }
2162     }
2163 
2164     // No matches, search for basic typename matches
2165     ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2166     if (clang_ast)
2167       return LLDB_RECORD_RESULT(SBType(ClangASTContext::GetBasicType(
2168           clang_ast->getASTContext(), const_typename)));
2169   }
2170   return LLDB_RECORD_RESULT(SBType());
2171 }
2172 
2173 SBType SBTarget::GetBasicType(lldb::BasicType type) {
2174   LLDB_RECORD_METHOD(lldb::SBType, SBTarget, GetBasicType, (lldb::BasicType),
2175                      type);
2176 
2177   TargetSP target_sp(GetSP());
2178   if (target_sp) {
2179     ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2180     if (clang_ast)
2181       return LLDB_RECORD_RESULT(SBType(
2182           ClangASTContext::GetBasicType(clang_ast->getASTContext(), type)));
2183   }
2184   return LLDB_RECORD_RESULT(SBType());
2185 }
2186 
2187 lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
2188   LLDB_RECORD_METHOD(lldb::SBTypeList, SBTarget, FindTypes, (const char *),
2189                      typename_cstr);
2190 
2191   SBTypeList sb_type_list;
2192   TargetSP target_sp(GetSP());
2193   if (typename_cstr && typename_cstr[0] && target_sp) {
2194     ModuleList &images = target_sp->GetImages();
2195     ConstString const_typename(typename_cstr);
2196     bool exact_match = false;
2197     TypeList type_list;
2198     llvm::DenseSet<SymbolFile *> searched_symbol_files;
2199     uint32_t num_matches =
2200         images.FindTypes(nullptr, const_typename, exact_match, UINT32_MAX,
2201                          searched_symbol_files, type_list);
2202 
2203     if (num_matches > 0) {
2204       for (size_t idx = 0; idx < num_matches; idx++) {
2205         TypeSP type_sp(type_list.GetTypeAtIndex(idx));
2206         if (type_sp)
2207           sb_type_list.Append(SBType(type_sp));
2208       }
2209     }
2210 
2211     // Try the Objective-C runtime if one is installed
2212 
2213     ProcessSP process_sp(target_sp->GetProcessSP());
2214 
2215     if (process_sp) {
2216       ObjCLanguageRuntime *objc_language_runtime =
2217           process_sp->GetObjCLanguageRuntime();
2218 
2219       if (objc_language_runtime) {
2220         DeclVendor *objc_decl_vendor = objc_language_runtime->GetDeclVendor();
2221 
2222         if (objc_decl_vendor) {
2223           std::vector<clang::NamedDecl *> decls;
2224 
2225           if (objc_decl_vendor->FindDecls(const_typename, true, 1, decls) > 0) {
2226             for (clang::NamedDecl *decl : decls) {
2227               if (CompilerType type = ClangASTContext::GetTypeForDecl(decl)) {
2228                 sb_type_list.Append(SBType(type));
2229               }
2230             }
2231           }
2232         }
2233       }
2234     }
2235 
2236     if (sb_type_list.GetSize() == 0) {
2237       // No matches, search for basic typename matches
2238       ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2239       if (clang_ast)
2240         sb_type_list.Append(SBType(ClangASTContext::GetBasicType(
2241             clang_ast->getASTContext(), const_typename)));
2242     }
2243   }
2244   return LLDB_RECORD_RESULT(sb_type_list);
2245 }
2246 
2247 SBValueList SBTarget::FindGlobalVariables(const char *name,
2248                                           uint32_t max_matches) {
2249   LLDB_RECORD_METHOD(lldb::SBValueList, SBTarget, FindGlobalVariables,
2250                      (const char *, uint32_t), name, max_matches);
2251 
2252   SBValueList sb_value_list;
2253 
2254   TargetSP target_sp(GetSP());
2255   if (name && target_sp) {
2256     VariableList variable_list;
2257     const uint32_t match_count = target_sp->GetImages().FindGlobalVariables(
2258         ConstString(name), max_matches, variable_list);
2259 
2260     if (match_count > 0) {
2261       ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2262       if (exe_scope == NULL)
2263         exe_scope = target_sp.get();
2264       for (uint32_t i = 0; i < match_count; ++i) {
2265         lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
2266             exe_scope, variable_list.GetVariableAtIndex(i)));
2267         if (valobj_sp)
2268           sb_value_list.Append(SBValue(valobj_sp));
2269       }
2270     }
2271   }
2272 
2273   return LLDB_RECORD_RESULT(sb_value_list);
2274 }
2275 
2276 SBValueList SBTarget::FindGlobalVariables(const char *name,
2277                                           uint32_t max_matches,
2278                                           MatchType matchtype) {
2279   LLDB_RECORD_METHOD(lldb::SBValueList, SBTarget, FindGlobalVariables,
2280                      (const char *, uint32_t, lldb::MatchType), name,
2281                      max_matches, matchtype);
2282 
2283   SBValueList sb_value_list;
2284 
2285   TargetSP target_sp(GetSP());
2286   if (name && target_sp) {
2287     llvm::StringRef name_ref(name);
2288     VariableList variable_list;
2289 
2290     std::string regexstr;
2291     uint32_t match_count;
2292     switch (matchtype) {
2293     case eMatchTypeNormal:
2294       match_count = target_sp->GetImages().FindGlobalVariables(
2295           ConstString(name), max_matches, variable_list);
2296       break;
2297     case eMatchTypeRegex:
2298       match_count = target_sp->GetImages().FindGlobalVariables(
2299           RegularExpression(name_ref), max_matches, variable_list);
2300       break;
2301     case eMatchTypeStartsWith:
2302       regexstr = llvm::Regex::escape(name) + ".*";
2303       match_count = target_sp->GetImages().FindGlobalVariables(
2304           RegularExpression(regexstr), max_matches, variable_list);
2305       break;
2306     }
2307 
2308     if (match_count > 0) {
2309       ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2310       if (exe_scope == NULL)
2311         exe_scope = target_sp.get();
2312       for (uint32_t i = 0; i < match_count; ++i) {
2313         lldb::ValueObjectSP valobj_sp(ValueObjectVariable::Create(
2314             exe_scope, variable_list.GetVariableAtIndex(i)));
2315         if (valobj_sp)
2316           sb_value_list.Append(SBValue(valobj_sp));
2317       }
2318     }
2319   }
2320 
2321   return LLDB_RECORD_RESULT(sb_value_list);
2322 }
2323 
2324 lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
2325   LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, FindFirstGlobalVariable,
2326                      (const char *), name);
2327 
2328   SBValueList sb_value_list(FindGlobalVariables(name, 1));
2329   if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2330     return LLDB_RECORD_RESULT(sb_value_list.GetValueAtIndex(0));
2331   return LLDB_RECORD_RESULT(SBValue());
2332 }
2333 
2334 SBSourceManager SBTarget::GetSourceManager() {
2335   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSourceManager, SBTarget, GetSourceManager);
2336 
2337   SBSourceManager source_manager(*this);
2338   return LLDB_RECORD_RESULT(source_manager);
2339 }
2340 
2341 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
2342                                                    uint32_t count) {
2343   LLDB_RECORD_METHOD(lldb::SBInstructionList, SBTarget, ReadInstructions,
2344                      (lldb::SBAddress, uint32_t), base_addr, count);
2345 
2346   return LLDB_RECORD_RESULT(ReadInstructions(base_addr, count, NULL));
2347 }
2348 
2349 lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
2350                                                    uint32_t count,
2351                                                    const char *flavor_string) {
2352   LLDB_RECORD_METHOD(lldb::SBInstructionList, SBTarget, ReadInstructions,
2353                      (lldb::SBAddress, uint32_t, const char *), base_addr,
2354                      count, flavor_string);
2355 
2356   SBInstructionList sb_instructions;
2357 
2358   TargetSP target_sp(GetSP());
2359   if (target_sp) {
2360     Address *addr_ptr = base_addr.get();
2361 
2362     if (addr_ptr) {
2363       DataBufferHeap data(
2364           target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2365       bool prefer_file_cache = false;
2366       lldb_private::Status error;
2367       lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2368       const size_t bytes_read =
2369           target_sp->ReadMemory(*addr_ptr, prefer_file_cache, data.GetBytes(),
2370                                 data.GetByteSize(), error, &load_addr);
2371       const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2372       sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2373           target_sp->GetArchitecture(), NULL, flavor_string, *addr_ptr,
2374           data.GetBytes(), bytes_read, count, data_from_file));
2375     }
2376   }
2377 
2378   return LLDB_RECORD_RESULT(sb_instructions);
2379 }
2380 
2381 lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
2382                                                   const void *buf,
2383                                                   size_t size) {
2384   return GetInstructionsWithFlavor(base_addr, NULL, buf, size);
2385 }
2386 
2387 lldb::SBInstructionList
2388 SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
2389                                     const char *flavor_string, const void *buf,
2390                                     size_t size) {
2391   SBInstructionList sb_instructions;
2392 
2393   TargetSP target_sp(GetSP());
2394   if (target_sp) {
2395     Address addr;
2396 
2397     if (base_addr.get())
2398       addr = *base_addr.get();
2399 
2400     const bool data_from_file = true;
2401 
2402     sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2403         target_sp->GetArchitecture(), NULL, flavor_string, addr, buf, size,
2404         UINT32_MAX, data_from_file));
2405   }
2406 
2407   return sb_instructions;
2408 }
2409 
2410 lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
2411                                                   const void *buf,
2412                                                   size_t size) {
2413   return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), NULL, buf,
2414                                    size);
2415 }
2416 
2417 lldb::SBInstructionList
2418 SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
2419                                     const char *flavor_string, const void *buf,
2420                                     size_t size) {
2421   return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
2422                                    buf, size);
2423 }
2424 
2425 SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
2426                                         lldb::addr_t section_base_addr) {
2427   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, SetSectionLoadAddress,
2428                      (lldb::SBSection, lldb::addr_t), section,
2429                      section_base_addr);
2430 
2431   SBError sb_error;
2432   TargetSP target_sp(GetSP());
2433   if (target_sp) {
2434     if (!section.IsValid()) {
2435       sb_error.SetErrorStringWithFormat("invalid section");
2436     } else {
2437       SectionSP section_sp(section.GetSP());
2438       if (section_sp) {
2439         if (section_sp->IsThreadSpecific()) {
2440           sb_error.SetErrorString(
2441               "thread specific sections are not yet supported");
2442         } else {
2443           ProcessSP process_sp(target_sp->GetProcessSP());
2444           if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
2445             ModuleSP module_sp(section_sp->GetModule());
2446             if (module_sp) {
2447               ModuleList module_list;
2448               module_list.Append(module_sp);
2449               target_sp->ModulesDidLoad(module_list);
2450             }
2451             // Flush info in the process (stack frames, etc)
2452             if (process_sp)
2453               process_sp->Flush();
2454           }
2455         }
2456       }
2457     }
2458   } else {
2459     sb_error.SetErrorString("invalid target");
2460   }
2461   return LLDB_RECORD_RESULT(sb_error);
2462 }
2463 
2464 SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
2465   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, ClearSectionLoadAddress,
2466                      (lldb::SBSection), section);
2467 
2468   SBError sb_error;
2469 
2470   TargetSP target_sp(GetSP());
2471   if (target_sp) {
2472     if (!section.IsValid()) {
2473       sb_error.SetErrorStringWithFormat("invalid section");
2474     } else {
2475       SectionSP section_sp(section.GetSP());
2476       if (section_sp) {
2477         ProcessSP process_sp(target_sp->GetProcessSP());
2478         if (target_sp->SetSectionUnloaded(section_sp)) {
2479           ModuleSP module_sp(section_sp->GetModule());
2480           if (module_sp) {
2481             ModuleList module_list;
2482             module_list.Append(module_sp);
2483             target_sp->ModulesDidUnload(module_list, false);
2484           }
2485           // Flush info in the process (stack frames, etc)
2486           if (process_sp)
2487             process_sp->Flush();
2488         }
2489       } else {
2490         sb_error.SetErrorStringWithFormat("invalid section");
2491       }
2492     }
2493   } else {
2494     sb_error.SetErrorStringWithFormat("invalid target");
2495   }
2496   return LLDB_RECORD_RESULT(sb_error);
2497 }
2498 
2499 SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2500                                        int64_t slide_offset) {
2501   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, SetModuleLoadAddress,
2502                      (lldb::SBModule, int64_t), module, slide_offset);
2503 
2504   SBError sb_error;
2505 
2506   TargetSP target_sp(GetSP());
2507   if (target_sp) {
2508     ModuleSP module_sp(module.GetSP());
2509     if (module_sp) {
2510       bool changed = false;
2511       if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
2512         // The load was successful, make sure that at least some sections
2513         // changed before we notify that our module was loaded.
2514         if (changed) {
2515           ModuleList module_list;
2516           module_list.Append(module_sp);
2517           target_sp->ModulesDidLoad(module_list);
2518           // Flush info in the process (stack frames, etc)
2519           ProcessSP process_sp(target_sp->GetProcessSP());
2520           if (process_sp)
2521             process_sp->Flush();
2522         }
2523       }
2524     } else {
2525       sb_error.SetErrorStringWithFormat("invalid module");
2526     }
2527 
2528   } else {
2529     sb_error.SetErrorStringWithFormat("invalid target");
2530   }
2531   return LLDB_RECORD_RESULT(sb_error);
2532 }
2533 
2534 SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
2535   LLDB_RECORD_METHOD(lldb::SBError, SBTarget, ClearModuleLoadAddress,
2536                      (lldb::SBModule), module);
2537 
2538   SBError sb_error;
2539 
2540   char path[PATH_MAX];
2541   TargetSP target_sp(GetSP());
2542   if (target_sp) {
2543     ModuleSP module_sp(module.GetSP());
2544     if (module_sp) {
2545       ObjectFile *objfile = module_sp->GetObjectFile();
2546       if (objfile) {
2547         SectionList *section_list = objfile->GetSectionList();
2548         if (section_list) {
2549           ProcessSP process_sp(target_sp->GetProcessSP());
2550 
2551           bool changed = false;
2552           const size_t num_sections = section_list->GetSize();
2553           for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2554             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
2555             if (section_sp)
2556               changed |= target_sp->SetSectionUnloaded(section_sp);
2557           }
2558           if (changed) {
2559             ModuleList module_list;
2560             module_list.Append(module_sp);
2561             target_sp->ModulesDidUnload(module_list, false);
2562             // Flush info in the process (stack frames, etc)
2563             ProcessSP process_sp(target_sp->GetProcessSP());
2564             if (process_sp)
2565               process_sp->Flush();
2566           }
2567         } else {
2568           module_sp->GetFileSpec().GetPath(path, sizeof(path));
2569           sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
2570                                             path);
2571         }
2572       } else {
2573         module_sp->GetFileSpec().GetPath(path, sizeof(path));
2574         sb_error.SetErrorStringWithFormat("no object file for module '%s'",
2575                                           path);
2576       }
2577     } else {
2578       sb_error.SetErrorStringWithFormat("invalid module");
2579     }
2580   } else {
2581     sb_error.SetErrorStringWithFormat("invalid target");
2582   }
2583   return LLDB_RECORD_RESULT(sb_error);
2584 }
2585 
2586 lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
2587                                                 lldb::SymbolType symbol_type) {
2588   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBTarget, FindSymbols,
2589                      (const char *, lldb::SymbolType), name, symbol_type);
2590 
2591   SBSymbolContextList sb_sc_list;
2592   if (name && name[0]) {
2593     TargetSP target_sp(GetSP());
2594     if (target_sp) {
2595       bool append = true;
2596       target_sp->GetImages().FindSymbolsWithNameAndType(
2597           ConstString(name), symbol_type, *sb_sc_list, append);
2598     }
2599   }
2600   return LLDB_RECORD_RESULT(sb_sc_list);
2601 }
2602 
2603 lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
2604   LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, EvaluateExpression,
2605                      (const char *), expr);
2606 
2607   TargetSP target_sp(GetSP());
2608   if (!target_sp)
2609     return LLDB_RECORD_RESULT(SBValue());
2610 
2611   SBExpressionOptions options;
2612   lldb::DynamicValueType fetch_dynamic_value =
2613       target_sp->GetPreferDynamicValue();
2614   options.SetFetchDynamicValue(fetch_dynamic_value);
2615   options.SetUnwindOnError(true);
2616   return LLDB_RECORD_RESULT(EvaluateExpression(expr, options));
2617 }
2618 
2619 lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
2620                                            const SBExpressionOptions &options) {
2621   LLDB_RECORD_METHOD(lldb::SBValue, SBTarget, EvaluateExpression,
2622                      (const char *, const lldb::SBExpressionOptions &), expr,
2623                      options);
2624 
2625   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
2626 #if !defined(LLDB_DISABLE_PYTHON)
2627   Log *expr_log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
2628 #endif
2629   SBValue expr_result;
2630   ExpressionResults exe_results = eExpressionSetupError;
2631   ValueObjectSP expr_value_sp;
2632   TargetSP target_sp(GetSP());
2633   StackFrame *frame = NULL;
2634   if (target_sp) {
2635     if (expr == NULL || expr[0] == '\0') {
2636       if (log)
2637         log->Printf(
2638             "SBTarget::EvaluateExpression called with an empty expression");
2639       return LLDB_RECORD_RESULT(expr_result);
2640     }
2641 
2642     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2643     ExecutionContext exe_ctx(m_opaque_sp.get());
2644 
2645     if (log)
2646       log->Printf("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2647 
2648     frame = exe_ctx.GetFramePtr();
2649     Target *target = exe_ctx.GetTargetPtr();
2650 
2651     if (target) {
2652 #ifdef LLDB_CONFIGURATION_DEBUG
2653       StreamString frame_description;
2654       if (frame)
2655         frame->DumpUsingSettingsFormat(&frame_description);
2656       llvm::PrettyStackTraceFormat stack_trace(
2657           "SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = "
2658           "%u) %s",
2659           expr, options.GetFetchDynamicValue(),
2660           frame_description.GetString().str().c_str());
2661 #endif
2662       exe_results =
2663           target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2664 
2665       expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2666     } else {
2667       if (log)
2668         log->Printf("SBTarget::EvaluateExpression () => error: could not "
2669                     "reconstruct frame object for this SBTarget.");
2670     }
2671   }
2672 #ifndef LLDB_DISABLE_PYTHON
2673   if (expr_log)
2674     expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is "
2675                      "%s, summary %s **",
2676                      expr_result.GetValue(), expr_result.GetSummary());
2677 
2678   if (log)
2679     log->Printf("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) "
2680                 "(execution result=%d)",
2681                 static_cast<void *>(frame), expr,
2682                 static_cast<void *>(expr_value_sp.get()), exe_results);
2683 #endif
2684 
2685   return LLDB_RECORD_RESULT(expr_result);
2686 }
2687 
2688 lldb::addr_t SBTarget::GetStackRedZoneSize() {
2689   LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBTarget, GetStackRedZoneSize);
2690 
2691   TargetSP target_sp(GetSP());
2692   if (target_sp) {
2693     ABISP abi_sp;
2694     ProcessSP process_sp(target_sp->GetProcessSP());
2695     if (process_sp)
2696       abi_sp = process_sp->GetABI();
2697     else
2698       abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
2699     if (abi_sp)
2700       return abi_sp->GetRedZoneSize();
2701   }
2702   return 0;
2703 }
2704 
2705 lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
2706   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBLaunchInfo, SBTarget, GetLaunchInfo);
2707 
2708   lldb::SBLaunchInfo launch_info(NULL);
2709   TargetSP target_sp(GetSP());
2710   if (target_sp)
2711     launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
2712   return LLDB_RECORD_RESULT(launch_info);
2713 }
2714 
2715 void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
2716   LLDB_RECORD_METHOD(void, SBTarget, SetLaunchInfo,
2717                      (const lldb::SBLaunchInfo &), launch_info);
2718 
2719   TargetSP target_sp(GetSP());
2720   if (target_sp)
2721     m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
2722 }
2723