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