1 //===-- SBTarget.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBTarget.h"
10 #include "lldb/Utility/Instrumentation.h"
11 #include "lldb/Utility/LLDBLog.h"
12 #include "lldb/lldb-public.h"
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/PluginManager.h"
41 #include "lldb/Core/SearchFilter.h"
42 #include "lldb/Core/Section.h"
43 #include "lldb/Core/StructuredDataImpl.h"
44 #include "lldb/Host/Host.h"
45 #include "lldb/Symbol/DeclVendor.h"
46 #include "lldb/Symbol/ObjectFile.h"
47 #include "lldb/Symbol/SymbolFile.h"
48 #include "lldb/Symbol/SymbolVendor.h"
49 #include "lldb/Symbol/TypeSystem.h"
50 #include "lldb/Symbol/VariableList.h"
51 #include "lldb/Target/ABI.h"
52 #include "lldb/Target/Language.h"
53 #include "lldb/Target/LanguageRuntime.h"
54 #include "lldb/Target/Process.h"
55 #include "lldb/Target/StackFrame.h"
56 #include "lldb/Target/Target.h"
57 #include "lldb/Target/TargetList.h"
58 #include "lldb/Utility/ArchSpec.h"
59 #include "lldb/Utility/Args.h"
60 #include "lldb/Utility/FileSpec.h"
61 #include "lldb/Utility/ProcessInfo.h"
62 #include "lldb/Utility/RegularExpression.h"
63 #include "lldb/ValueObject/ValueObjectConstResult.h"
64 #include "lldb/ValueObject/ValueObjectList.h"
65 #include "lldb/ValueObject/ValueObjectVariable.h"
67 #include "Commands/CommandObjectBreakpoint.h"
68 #include "lldb/Interpreter/CommandReturnObject.h"
69 #include "llvm/Support/PrettyStackTrace.h"
70 #include "llvm/Support/Regex.h"
73 using namespace lldb_private
;
75 #define DEFAULT_DISASM_BYTE_SIZE 32
77 static Status
AttachToProcess(ProcessAttachInfo
&attach_info
, Target
&target
) {
78 std::lock_guard
<std::recursive_mutex
> guard(target
.GetAPIMutex());
80 auto process_sp
= target
.GetProcessSP();
82 const auto state
= process_sp
->GetState();
83 if (process_sp
->IsAlive() && state
== eStateConnected
) {
84 // If we are already connected, then we have already specified the
85 // listener, so if a valid listener is supplied, we need to error out to
86 // let the client know.
87 if (attach_info
.GetListener())
88 return Status::FromErrorString(
89 "process is connected and already has a listener, pass "
94 return target
.Attach(attach_info
, nullptr);
97 // SBTarget constructor
98 SBTarget::SBTarget() { LLDB_INSTRUMENT_VA(this); }
100 SBTarget::SBTarget(const SBTarget
&rhs
) : m_opaque_sp(rhs
.m_opaque_sp
) {
101 LLDB_INSTRUMENT_VA(this, rhs
);
104 SBTarget::SBTarget(const TargetSP
&target_sp
) : m_opaque_sp(target_sp
) {
105 LLDB_INSTRUMENT_VA(this, target_sp
);
108 const SBTarget
&SBTarget::operator=(const SBTarget
&rhs
) {
109 LLDB_INSTRUMENT_VA(this, rhs
);
112 m_opaque_sp
= rhs
.m_opaque_sp
;
117 SBTarget::~SBTarget() = default;
119 bool SBTarget::EventIsTargetEvent(const SBEvent
&event
) {
120 LLDB_INSTRUMENT_VA(event
);
122 return Target::TargetEventData::GetEventDataFromEvent(event
.get()) != nullptr;
125 SBTarget
SBTarget::GetTargetFromEvent(const SBEvent
&event
) {
126 LLDB_INSTRUMENT_VA(event
);
128 return Target::TargetEventData::GetTargetFromEvent(event
.get());
131 uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent
&event
) {
132 LLDB_INSTRUMENT_VA(event
);
134 const ModuleList module_list
=
135 Target::TargetEventData::GetModuleListFromEvent(event
.get());
136 return module_list
.GetSize();
139 SBModule
SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx
,
140 const SBEvent
&event
) {
141 LLDB_INSTRUMENT_VA(idx
, event
);
143 const ModuleList module_list
=
144 Target::TargetEventData::GetModuleListFromEvent(event
.get());
145 return SBModule(module_list
.GetModuleAtIndex(idx
));
148 const char *SBTarget::GetBroadcasterClassName() {
151 return ConstString(Target::GetStaticBroadcasterClass()).AsCString();
154 bool SBTarget::IsValid() const {
155 LLDB_INSTRUMENT_VA(this);
156 return this->operator bool();
158 SBTarget::operator bool() const {
159 LLDB_INSTRUMENT_VA(this);
161 return m_opaque_sp
.get() != nullptr && m_opaque_sp
->IsValid();
164 SBProcess
SBTarget::GetProcess() {
165 LLDB_INSTRUMENT_VA(this);
167 SBProcess sb_process
;
168 ProcessSP process_sp
;
169 TargetSP
target_sp(GetSP());
171 process_sp
= target_sp
->GetProcessSP();
172 sb_process
.SetSP(process_sp
);
178 SBPlatform
SBTarget::GetPlatform() {
179 LLDB_INSTRUMENT_VA(this);
181 TargetSP
target_sp(GetSP());
186 platform
.m_opaque_sp
= target_sp
->GetPlatform();
191 SBDebugger
SBTarget::GetDebugger() const {
192 LLDB_INSTRUMENT_VA(this);
195 TargetSP
target_sp(GetSP());
197 debugger
.reset(target_sp
->GetDebugger().shared_from_this());
201 SBStructuredData
SBTarget::GetStatistics() {
202 LLDB_INSTRUMENT_VA(this);
203 SBStatisticsOptions options
;
204 return GetStatistics(options
);
207 SBStructuredData
SBTarget::GetStatistics(SBStatisticsOptions options
) {
208 LLDB_INSTRUMENT_VA(this);
210 SBStructuredData data
;
211 TargetSP
target_sp(GetSP());
214 std::string json_str
=
215 llvm::formatv("{0:2}", DebuggerStats::ReportStatistics(
216 target_sp
->GetDebugger(), target_sp
.get(),
219 data
.m_impl_up
->SetObjectSP(StructuredData::ParseJSON(json_str
));
223 void SBTarget::ResetStatistics() {
224 LLDB_INSTRUMENT_VA(this);
225 TargetSP
target_sp(GetSP());
227 DebuggerStats::ResetStatistics(target_sp
->GetDebugger(), target_sp
.get());
230 void SBTarget::SetCollectingStats(bool v
) {
231 LLDB_INSTRUMENT_VA(this, v
);
233 TargetSP
target_sp(GetSP());
236 return DebuggerStats::SetCollectingStats(v
);
239 bool SBTarget::GetCollectingStats() {
240 LLDB_INSTRUMENT_VA(this);
242 TargetSP
target_sp(GetSP());
245 return DebuggerStats::GetCollectingStats();
248 SBProcess
SBTarget::LoadCore(const char *core_file
) {
249 LLDB_INSTRUMENT_VA(this, core_file
);
251 lldb::SBError error
; // Ignored
252 return LoadCore(core_file
, error
);
255 SBProcess
SBTarget::LoadCore(const char *core_file
, lldb::SBError
&error
) {
256 LLDB_INSTRUMENT_VA(this, core_file
, error
);
258 SBProcess sb_process
;
259 TargetSP
target_sp(GetSP());
261 FileSpec
filespec(core_file
);
262 FileSystem::Instance().Resolve(filespec
);
263 ProcessSP
process_sp(target_sp
->CreateProcess(
264 target_sp
->GetDebugger().GetListener(), "", &filespec
, false));
266 error
.SetError(process_sp
->LoadCore());
268 sb_process
.SetSP(process_sp
);
270 error
.SetErrorString("Failed to create the process");
273 error
.SetErrorString("SBTarget is invalid");
278 SBProcess
SBTarget::LaunchSimple(char const **argv
, char const **envp
,
279 const char *working_directory
) {
280 LLDB_INSTRUMENT_VA(this, argv
, envp
, working_directory
);
282 TargetSP target_sp
= GetSP();
286 SBLaunchInfo launch_info
= GetLaunchInfo();
288 if (Module
*exe_module
= target_sp
->GetExecutableModulePointer())
289 launch_info
.SetExecutableFile(exe_module
->GetPlatformFileSpec(),
290 /*add_as_first_arg*/ true);
292 launch_info
.SetArguments(argv
, /*append*/ true);
294 launch_info
.SetEnvironmentEntries(envp
, /*append*/ false);
295 if (working_directory
)
296 launch_info
.SetWorkingDirectory(working_directory
);
299 return Launch(launch_info
, error
);
302 SBError
SBTarget::Install() {
303 LLDB_INSTRUMENT_VA(this);
306 TargetSP
target_sp(GetSP());
308 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
309 sb_error
.ref() = target_sp
->Install(nullptr);
314 SBProcess
SBTarget::Launch(SBListener
&listener
, char const **argv
,
315 char const **envp
, const char *stdin_path
,
316 const char *stdout_path
, const char *stderr_path
,
317 const char *working_directory
,
318 uint32_t launch_flags
, // See LaunchFlags
319 bool stop_at_entry
, lldb::SBError
&error
) {
320 LLDB_INSTRUMENT_VA(this, listener
, argv
, envp
, stdin_path
, stdout_path
,
321 stderr_path
, working_directory
, launch_flags
,
322 stop_at_entry
, error
);
324 SBProcess sb_process
;
325 ProcessSP process_sp
;
326 TargetSP
target_sp(GetSP());
329 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
332 launch_flags
|= eLaunchFlagStopAtEntry
;
334 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
335 launch_flags
|= eLaunchFlagDisableASLR
;
337 StateType state
= eStateInvalid
;
338 process_sp
= target_sp
->GetProcessSP();
340 state
= process_sp
->GetState();
342 if (process_sp
->IsAlive() && state
!= eStateConnected
) {
343 if (state
== eStateAttaching
)
344 error
.SetErrorString("process attach is in progress");
346 error
.SetErrorString("a process is already being debugged");
351 if (state
== eStateConnected
) {
352 // If we are already connected, then we have already specified the
353 // listener, so if a valid listener is supplied, we need to error out to
354 // let the client know.
355 if (listener
.IsValid()) {
356 error
.SetErrorString("process is connected and already has a listener, "
357 "pass empty listener");
362 if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
363 launch_flags
|= eLaunchFlagDisableSTDIO
;
365 ProcessLaunchInfo
launch_info(FileSpec(stdin_path
), FileSpec(stdout_path
),
366 FileSpec(stderr_path
),
367 FileSpec(working_directory
), launch_flags
);
369 Module
*exe_module
= target_sp
->GetExecutableModulePointer();
371 launch_info
.SetExecutableFile(exe_module
->GetPlatformFileSpec(), true);
373 launch_info
.GetArguments().AppendArguments(argv
);
375 auto default_launch_info
= target_sp
->GetProcessLaunchInfo();
376 launch_info
.GetArguments().AppendArguments(
377 default_launch_info
.GetArguments());
380 launch_info
.GetEnvironment() = Environment(envp
);
382 auto default_launch_info
= target_sp
->GetProcessLaunchInfo();
383 launch_info
.GetEnvironment() = default_launch_info
.GetEnvironment();
386 if (listener
.IsValid())
387 launch_info
.SetListener(listener
.GetSP());
389 error
.SetError(target_sp
->Launch(launch_info
, nullptr));
391 sb_process
.SetSP(target_sp
->GetProcessSP());
393 error
.SetErrorString("SBTarget is invalid");
399 SBProcess
SBTarget::Launch(SBLaunchInfo
&sb_launch_info
, SBError
&error
) {
400 LLDB_INSTRUMENT_VA(this, sb_launch_info
, error
);
402 SBProcess sb_process
;
403 TargetSP
target_sp(GetSP());
406 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
407 StateType state
= eStateInvalid
;
409 ProcessSP process_sp
= target_sp
->GetProcessSP();
411 state
= process_sp
->GetState();
413 if (process_sp
->IsAlive() && state
!= eStateConnected
) {
414 if (state
== eStateAttaching
)
415 error
.SetErrorString("process attach is in progress");
417 error
.SetErrorString("a process is already being debugged");
423 lldb_private::ProcessLaunchInfo launch_info
= sb_launch_info
.ref();
425 if (!launch_info
.GetExecutableFile()) {
426 Module
*exe_module
= target_sp
->GetExecutableModulePointer();
428 launch_info
.SetExecutableFile(exe_module
->GetPlatformFileSpec(), true);
431 const ArchSpec
&arch_spec
= target_sp
->GetArchitecture();
432 if (arch_spec
.IsValid())
433 launch_info
.GetArchitecture() = arch_spec
;
435 error
.SetError(target_sp
->Launch(launch_info
, nullptr));
436 sb_launch_info
.set_ref(launch_info
);
437 sb_process
.SetSP(target_sp
->GetProcessSP());
439 error
.SetErrorString("SBTarget is invalid");
445 lldb::SBProcess
SBTarget::Attach(SBAttachInfo
&sb_attach_info
, SBError
&error
) {
446 LLDB_INSTRUMENT_VA(this, sb_attach_info
, error
);
448 SBProcess sb_process
;
449 TargetSP
target_sp(GetSP());
452 ProcessAttachInfo
&attach_info
= sb_attach_info
.ref();
453 if (attach_info
.ProcessIDIsValid() && !attach_info
.UserIDIsValid() &&
454 !attach_info
.IsScriptedProcess()) {
455 PlatformSP platform_sp
= target_sp
->GetPlatform();
456 // See if we can pre-verify if a process exists or not
457 if (platform_sp
&& platform_sp
->IsConnected()) {
458 lldb::pid_t attach_pid
= attach_info
.GetProcessID();
459 ProcessInstanceInfo instance_info
;
460 if (platform_sp
->GetProcessInfo(attach_pid
, instance_info
)) {
461 attach_info
.SetUserID(instance_info
.GetEffectiveUserID());
463 error
.ref() = Status::FromErrorStringWithFormat(
464 "no process found with process ID %" PRIu64
, attach_pid
);
469 error
.SetError(AttachToProcess(attach_info
, *target_sp
));
471 sb_process
.SetSP(target_sp
->GetProcessSP());
473 error
.SetErrorString("SBTarget is invalid");
479 lldb::SBProcess
SBTarget::AttachToProcessWithID(
480 SBListener
&listener
,
481 lldb::pid_t pid
, // The process ID to attach to
482 SBError
&error
// An error explaining what went wrong if attach fails
484 LLDB_INSTRUMENT_VA(this, listener
, pid
, error
);
486 SBProcess sb_process
;
487 TargetSP
target_sp(GetSP());
490 ProcessAttachInfo attach_info
;
491 attach_info
.SetProcessID(pid
);
492 if (listener
.IsValid())
493 attach_info
.SetListener(listener
.GetSP());
495 ProcessInstanceInfo instance_info
;
496 if (target_sp
->GetPlatform()->GetProcessInfo(pid
, instance_info
))
497 attach_info
.SetUserID(instance_info
.GetEffectiveUserID());
499 error
.SetError(AttachToProcess(attach_info
, *target_sp
));
501 sb_process
.SetSP(target_sp
->GetProcessSP());
503 error
.SetErrorString("SBTarget is invalid");
508 lldb::SBProcess
SBTarget::AttachToProcessWithName(
509 SBListener
&listener
,
510 const char *name
, // basename of process to attach to
511 bool wait_for
, // if true wait for a new instance of "name" to be launched
512 SBError
&error
// An error explaining what went wrong if attach fails
514 LLDB_INSTRUMENT_VA(this, listener
, name
, wait_for
, error
);
516 SBProcess sb_process
;
517 TargetSP
target_sp(GetSP());
519 if (name
&& target_sp
) {
520 ProcessAttachInfo attach_info
;
521 attach_info
.GetExecutableFile().SetFile(name
, FileSpec::Style::native
);
522 attach_info
.SetWaitForLaunch(wait_for
);
523 if (listener
.IsValid())
524 attach_info
.SetListener(listener
.GetSP());
526 error
.SetError(AttachToProcess(attach_info
, *target_sp
));
528 sb_process
.SetSP(target_sp
->GetProcessSP());
530 error
.SetErrorString("SBTarget is invalid");
535 lldb::SBProcess
SBTarget::ConnectRemote(SBListener
&listener
, const char *url
,
536 const char *plugin_name
,
538 LLDB_INSTRUMENT_VA(this, listener
, url
, plugin_name
, error
);
540 SBProcess sb_process
;
541 ProcessSP process_sp
;
542 TargetSP
target_sp(GetSP());
545 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
546 if (listener
.IsValid())
548 target_sp
->CreateProcess(listener
.m_opaque_sp
, plugin_name
, nullptr,
551 process_sp
= target_sp
->CreateProcess(
552 target_sp
->GetDebugger().GetListener(), plugin_name
, nullptr, true);
555 sb_process
.SetSP(process_sp
);
556 error
.SetError(process_sp
->ConnectRemote(url
));
558 error
.SetErrorString("unable to create lldb_private::Process");
561 error
.SetErrorString("SBTarget is invalid");
567 SBFileSpec
SBTarget::GetExecutable() {
568 LLDB_INSTRUMENT_VA(this);
570 SBFileSpec exe_file_spec
;
571 TargetSP
target_sp(GetSP());
573 Module
*exe_module
= target_sp
->GetExecutableModulePointer();
575 exe_file_spec
.SetFileSpec(exe_module
->GetFileSpec());
578 return exe_file_spec
;
581 bool SBTarget::operator==(const SBTarget
&rhs
) const {
582 LLDB_INSTRUMENT_VA(this, rhs
);
584 return m_opaque_sp
.get() == rhs
.m_opaque_sp
.get();
587 bool SBTarget::operator!=(const SBTarget
&rhs
) const {
588 LLDB_INSTRUMENT_VA(this, rhs
);
590 return m_opaque_sp
.get() != rhs
.m_opaque_sp
.get();
593 lldb::TargetSP
SBTarget::GetSP() const { return m_opaque_sp
; }
595 void SBTarget::SetSP(const lldb::TargetSP
&target_sp
) {
596 m_opaque_sp
= target_sp
;
599 lldb::SBAddress
SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr
) {
600 LLDB_INSTRUMENT_VA(this, vm_addr
);
602 lldb::SBAddress sb_addr
;
603 Address
&addr
= sb_addr
.ref();
604 TargetSP
target_sp(GetSP());
606 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
607 if (target_sp
->ResolveLoadAddress(vm_addr
, addr
))
611 // We have a load address that isn't in a section, just return an address
612 // with the offset filled in (the address) and the section set to NULL
613 addr
.SetRawAddress(vm_addr
);
617 lldb::SBAddress
SBTarget::ResolveFileAddress(lldb::addr_t file_addr
) {
618 LLDB_INSTRUMENT_VA(this, file_addr
);
620 lldb::SBAddress sb_addr
;
621 Address
&addr
= sb_addr
.ref();
622 TargetSP
target_sp(GetSP());
624 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
625 if (target_sp
->ResolveFileAddress(file_addr
, addr
))
629 addr
.SetRawAddress(file_addr
);
633 lldb::SBAddress
SBTarget::ResolvePastLoadAddress(uint32_t stop_id
,
634 lldb::addr_t vm_addr
) {
635 LLDB_INSTRUMENT_VA(this, stop_id
, vm_addr
);
637 lldb::SBAddress sb_addr
;
638 Address
&addr
= sb_addr
.ref();
639 TargetSP
target_sp(GetSP());
641 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
642 if (target_sp
->ResolveLoadAddress(vm_addr
, addr
))
646 // We have a load address that isn't in a section, just return an address
647 // with the offset filled in (the address) and the section set to NULL
648 addr
.SetRawAddress(vm_addr
);
653 SBTarget::ResolveSymbolContextForAddress(const SBAddress
&addr
,
654 uint32_t resolve_scope
) {
655 LLDB_INSTRUMENT_VA(this, addr
, resolve_scope
);
658 SymbolContextItem scope
= static_cast<SymbolContextItem
>(resolve_scope
);
659 if (addr
.IsValid()) {
660 TargetSP
target_sp(GetSP());
662 target_sp
->GetImages().ResolveSymbolContextForAddress(addr
.ref(), scope
,
668 size_t SBTarget::ReadMemory(const SBAddress addr
, void *buf
, size_t size
,
669 lldb::SBError
&error
) {
670 LLDB_INSTRUMENT_VA(this, addr
, buf
, size
, error
);
672 size_t bytes_read
= 0;
673 TargetSP
target_sp(GetSP());
675 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
677 target_sp
->ReadMemory(addr
.ref(), buf
, size
, error
.ref(), true);
679 error
.SetErrorString("invalid target");
685 SBBreakpoint
SBTarget::BreakpointCreateByLocation(const char *file
,
687 LLDB_INSTRUMENT_VA(this, file
, line
);
690 BreakpointCreateByLocation(SBFileSpec(file
, false), line
));
694 SBTarget::BreakpointCreateByLocation(const SBFileSpec
&sb_file_spec
,
696 LLDB_INSTRUMENT_VA(this, sb_file_spec
, line
);
698 return BreakpointCreateByLocation(sb_file_spec
, line
, 0);
702 SBTarget::BreakpointCreateByLocation(const SBFileSpec
&sb_file_spec
,
703 uint32_t line
, lldb::addr_t offset
) {
704 LLDB_INSTRUMENT_VA(this, sb_file_spec
, line
, offset
);
706 SBFileSpecList empty_list
;
707 return BreakpointCreateByLocation(sb_file_spec
, line
, offset
, empty_list
);
711 SBTarget::BreakpointCreateByLocation(const SBFileSpec
&sb_file_spec
,
712 uint32_t line
, lldb::addr_t offset
,
713 SBFileSpecList
&sb_module_list
) {
714 LLDB_INSTRUMENT_VA(this, sb_file_spec
, line
, offset
, sb_module_list
);
716 return BreakpointCreateByLocation(sb_file_spec
, line
, 0, offset
,
720 SBBreakpoint
SBTarget::BreakpointCreateByLocation(
721 const SBFileSpec
&sb_file_spec
, uint32_t line
, uint32_t column
,
722 lldb::addr_t offset
, SBFileSpecList
&sb_module_list
) {
723 LLDB_INSTRUMENT_VA(this, sb_file_spec
, line
, column
, offset
, sb_module_list
);
726 TargetSP
target_sp(GetSP());
727 if (target_sp
&& line
!= 0) {
728 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
730 const LazyBool check_inlines
= eLazyBoolCalculate
;
731 const LazyBool skip_prologue
= eLazyBoolCalculate
;
732 const bool internal
= false;
733 const bool hardware
= false;
734 const LazyBool move_to_nearest_code
= eLazyBoolCalculate
;
735 const FileSpecList
*module_list
= nullptr;
736 if (sb_module_list
.GetSize() > 0) {
737 module_list
= sb_module_list
.get();
739 sb_bp
= target_sp
->CreateBreakpoint(
740 module_list
, *sb_file_spec
, line
, column
, offset
, check_inlines
,
741 skip_prologue
, internal
, hardware
, move_to_nearest_code
);
747 SBBreakpoint
SBTarget::BreakpointCreateByLocation(
748 const SBFileSpec
&sb_file_spec
, uint32_t line
, uint32_t column
,
749 lldb::addr_t offset
, SBFileSpecList
&sb_module_list
,
750 bool move_to_nearest_code
) {
751 LLDB_INSTRUMENT_VA(this, sb_file_spec
, line
, column
, offset
, sb_module_list
,
752 move_to_nearest_code
);
755 TargetSP
target_sp(GetSP());
756 if (target_sp
&& line
!= 0) {
757 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
759 const LazyBool check_inlines
= eLazyBoolCalculate
;
760 const LazyBool skip_prologue
= eLazyBoolCalculate
;
761 const bool internal
= false;
762 const bool hardware
= false;
763 const FileSpecList
*module_list
= nullptr;
764 if (sb_module_list
.GetSize() > 0) {
765 module_list
= sb_module_list
.get();
767 sb_bp
= target_sp
->CreateBreakpoint(
768 module_list
, *sb_file_spec
, line
, column
, offset
, check_inlines
,
769 skip_prologue
, internal
, hardware
,
770 move_to_nearest_code
? eLazyBoolYes
: eLazyBoolNo
);
776 SBBreakpoint
SBTarget::BreakpointCreateByName(const char *symbol_name
,
777 const char *module_name
) {
778 LLDB_INSTRUMENT_VA(this, symbol_name
, module_name
);
781 TargetSP
target_sp(GetSP());
782 if (target_sp
.get()) {
783 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
785 const bool internal
= false;
786 const bool hardware
= false;
787 const LazyBool skip_prologue
= eLazyBoolCalculate
;
788 const lldb::addr_t offset
= 0;
789 if (module_name
&& module_name
[0]) {
790 FileSpecList module_spec_list
;
791 module_spec_list
.Append(FileSpec(module_name
));
792 sb_bp
= target_sp
->CreateBreakpoint(
793 &module_spec_list
, nullptr, symbol_name
, eFunctionNameTypeAuto
,
794 eLanguageTypeUnknown
, offset
, skip_prologue
, internal
, hardware
);
796 sb_bp
= target_sp
->CreateBreakpoint(
797 nullptr, nullptr, symbol_name
, eFunctionNameTypeAuto
,
798 eLanguageTypeUnknown
, offset
, skip_prologue
, internal
, hardware
);
806 SBTarget::BreakpointCreateByName(const char *symbol_name
,
807 const SBFileSpecList
&module_list
,
808 const SBFileSpecList
&comp_unit_list
) {
809 LLDB_INSTRUMENT_VA(this, symbol_name
, module_list
, comp_unit_list
);
811 lldb::FunctionNameType name_type_mask
= eFunctionNameTypeAuto
;
812 return BreakpointCreateByName(symbol_name
, name_type_mask
,
813 eLanguageTypeUnknown
, module_list
,
817 lldb::SBBreakpoint
SBTarget::BreakpointCreateByName(
818 const char *symbol_name
, uint32_t name_type_mask
,
819 const SBFileSpecList
&module_list
, const SBFileSpecList
&comp_unit_list
) {
820 LLDB_INSTRUMENT_VA(this, symbol_name
, name_type_mask
, module_list
,
823 return BreakpointCreateByName(symbol_name
, name_type_mask
,
824 eLanguageTypeUnknown
, module_list
,
828 lldb::SBBreakpoint
SBTarget::BreakpointCreateByName(
829 const char *symbol_name
, uint32_t name_type_mask
,
830 LanguageType symbol_language
, const SBFileSpecList
&module_list
,
831 const SBFileSpecList
&comp_unit_list
) {
832 LLDB_INSTRUMENT_VA(this, symbol_name
, name_type_mask
, symbol_language
,
833 module_list
, comp_unit_list
);
836 TargetSP
target_sp(GetSP());
837 if (target_sp
&& symbol_name
&& symbol_name
[0]) {
838 const bool internal
= false;
839 const bool hardware
= false;
840 const LazyBool skip_prologue
= eLazyBoolCalculate
;
841 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
842 FunctionNameType mask
= static_cast<FunctionNameType
>(name_type_mask
);
843 sb_bp
= target_sp
->CreateBreakpoint(module_list
.get(), comp_unit_list
.get(),
844 symbol_name
, mask
, symbol_language
, 0,
845 skip_prologue
, internal
, hardware
);
851 lldb::SBBreakpoint
SBTarget::BreakpointCreateByNames(
852 const char *symbol_names
[], uint32_t num_names
, uint32_t name_type_mask
,
853 const SBFileSpecList
&module_list
, const SBFileSpecList
&comp_unit_list
) {
854 LLDB_INSTRUMENT_VA(this, symbol_names
, num_names
, name_type_mask
, module_list
,
857 return BreakpointCreateByNames(symbol_names
, num_names
, name_type_mask
,
858 eLanguageTypeUnknown
, module_list
,
862 lldb::SBBreakpoint
SBTarget::BreakpointCreateByNames(
863 const char *symbol_names
[], uint32_t num_names
, uint32_t name_type_mask
,
864 LanguageType symbol_language
, const SBFileSpecList
&module_list
,
865 const SBFileSpecList
&comp_unit_list
) {
866 LLDB_INSTRUMENT_VA(this, symbol_names
, num_names
, name_type_mask
,
867 symbol_language
, module_list
, comp_unit_list
);
869 return BreakpointCreateByNames(symbol_names
, num_names
, name_type_mask
,
870 eLanguageTypeUnknown
, 0, module_list
,
874 lldb::SBBreakpoint
SBTarget::BreakpointCreateByNames(
875 const char *symbol_names
[], uint32_t num_names
, uint32_t name_type_mask
,
876 LanguageType symbol_language
, lldb::addr_t offset
,
877 const SBFileSpecList
&module_list
, const SBFileSpecList
&comp_unit_list
) {
878 LLDB_INSTRUMENT_VA(this, symbol_names
, num_names
, name_type_mask
,
879 symbol_language
, offset
, module_list
, comp_unit_list
);
882 TargetSP
target_sp(GetSP());
883 if (target_sp
&& num_names
> 0) {
884 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
885 const bool internal
= false;
886 const bool hardware
= false;
887 FunctionNameType mask
= static_cast<FunctionNameType
>(name_type_mask
);
888 const LazyBool skip_prologue
= eLazyBoolCalculate
;
889 sb_bp
= target_sp
->CreateBreakpoint(
890 module_list
.get(), comp_unit_list
.get(), symbol_names
, num_names
, mask
,
891 symbol_language
, offset
, skip_prologue
, internal
, hardware
);
897 SBBreakpoint
SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex
,
898 const char *module_name
) {
899 LLDB_INSTRUMENT_VA(this, symbol_name_regex
, module_name
);
901 SBFileSpecList module_spec_list
;
902 SBFileSpecList comp_unit_list
;
903 if (module_name
&& module_name
[0]) {
904 module_spec_list
.Append(FileSpec(module_name
));
906 return BreakpointCreateByRegex(symbol_name_regex
, eLanguageTypeUnknown
,
907 module_spec_list
, comp_unit_list
);
911 SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex
,
912 const SBFileSpecList
&module_list
,
913 const SBFileSpecList
&comp_unit_list
) {
914 LLDB_INSTRUMENT_VA(this, symbol_name_regex
, module_list
, comp_unit_list
);
916 return BreakpointCreateByRegex(symbol_name_regex
, eLanguageTypeUnknown
,
917 module_list
, comp_unit_list
);
920 lldb::SBBreakpoint
SBTarget::BreakpointCreateByRegex(
921 const char *symbol_name_regex
, LanguageType symbol_language
,
922 const SBFileSpecList
&module_list
, const SBFileSpecList
&comp_unit_list
) {
923 LLDB_INSTRUMENT_VA(this, symbol_name_regex
, symbol_language
, module_list
,
927 TargetSP
target_sp(GetSP());
928 if (target_sp
&& symbol_name_regex
&& symbol_name_regex
[0]) {
929 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
930 RegularExpression
regexp((llvm::StringRef(symbol_name_regex
)));
931 const bool internal
= false;
932 const bool hardware
= false;
933 const LazyBool skip_prologue
= eLazyBoolCalculate
;
935 sb_bp
= target_sp
->CreateFuncRegexBreakpoint(
936 module_list
.get(), comp_unit_list
.get(), std::move(regexp
),
937 symbol_language
, skip_prologue
, internal
, hardware
);
943 SBBreakpoint
SBTarget::BreakpointCreateByAddress(addr_t address
) {
944 LLDB_INSTRUMENT_VA(this, address
);
947 TargetSP
target_sp(GetSP());
949 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
950 const bool hardware
= false;
951 sb_bp
= target_sp
->CreateBreakpoint(address
, false, hardware
);
957 SBBreakpoint
SBTarget::BreakpointCreateBySBAddress(SBAddress
&sb_address
) {
958 LLDB_INSTRUMENT_VA(this, sb_address
);
961 TargetSP
target_sp(GetSP());
962 if (!sb_address
.IsValid()) {
967 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
968 const bool hardware
= false;
969 sb_bp
= target_sp
->CreateBreakpoint(sb_address
.ref(), false, hardware
);
976 SBTarget::BreakpointCreateBySourceRegex(const char *source_regex
,
977 const lldb::SBFileSpec
&source_file
,
978 const char *module_name
) {
979 LLDB_INSTRUMENT_VA(this, source_regex
, source_file
, module_name
);
981 SBFileSpecList module_spec_list
;
983 if (module_name
&& module_name
[0]) {
984 module_spec_list
.Append(FileSpec(module_name
));
987 SBFileSpecList source_file_list
;
988 if (source_file
.IsValid()) {
989 source_file_list
.Append(source_file
);
992 return BreakpointCreateBySourceRegex(source_regex
, module_spec_list
,
996 lldb::SBBreakpoint
SBTarget::BreakpointCreateBySourceRegex(
997 const char *source_regex
, const SBFileSpecList
&module_list
,
998 const lldb::SBFileSpecList
&source_file_list
) {
999 LLDB_INSTRUMENT_VA(this, source_regex
, module_list
, source_file_list
);
1001 return BreakpointCreateBySourceRegex(source_regex
, module_list
,
1002 source_file_list
, SBStringList());
1005 lldb::SBBreakpoint
SBTarget::BreakpointCreateBySourceRegex(
1006 const char *source_regex
, const SBFileSpecList
&module_list
,
1007 const lldb::SBFileSpecList
&source_file_list
,
1008 const SBStringList
&func_names
) {
1009 LLDB_INSTRUMENT_VA(this, source_regex
, module_list
, source_file_list
,
1013 TargetSP
target_sp(GetSP());
1014 if (target_sp
&& source_regex
&& source_regex
[0]) {
1015 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1016 const bool hardware
= false;
1017 const LazyBool move_to_nearest_code
= eLazyBoolCalculate
;
1018 RegularExpression
regexp((llvm::StringRef(source_regex
)));
1019 std::unordered_set
<std::string
> func_names_set
;
1020 for (size_t i
= 0; i
< func_names
.GetSize(); i
++) {
1021 func_names_set
.insert(func_names
.GetStringAtIndex(i
));
1024 sb_bp
= target_sp
->CreateSourceRegexBreakpoint(
1025 module_list
.get(), source_file_list
.get(), func_names_set
,
1026 std::move(regexp
), false, hardware
, move_to_nearest_code
);
1033 SBTarget::BreakpointCreateForException(lldb::LanguageType language
,
1034 bool catch_bp
, bool throw_bp
) {
1035 LLDB_INSTRUMENT_VA(this, language
, catch_bp
, throw_bp
);
1038 TargetSP
target_sp(GetSP());
1040 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1041 const bool hardware
= false;
1042 sb_bp
= target_sp
->CreateExceptionBreakpoint(language
, catch_bp
, throw_bp
,
1049 lldb::SBBreakpoint
SBTarget::BreakpointCreateFromScript(
1050 const char *class_name
, SBStructuredData
&extra_args
,
1051 const SBFileSpecList
&module_list
, const SBFileSpecList
&file_list
,
1052 bool request_hardware
) {
1053 LLDB_INSTRUMENT_VA(this, class_name
, extra_args
, module_list
, file_list
,
1057 TargetSP
target_sp(GetSP());
1059 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1062 StructuredData::ObjectSP obj_sp
= extra_args
.m_impl_up
->GetObjectSP();
1064 target_sp
->CreateScriptedBreakpoint(class_name
,
1067 false, /* internal */
1076 uint32_t SBTarget::GetNumBreakpoints() const {
1077 LLDB_INSTRUMENT_VA(this);
1079 TargetSP
target_sp(GetSP());
1081 // The breakpoint list is thread safe, no need to lock
1082 return target_sp
->GetBreakpointList().GetSize();
1087 SBBreakpoint
SBTarget::GetBreakpointAtIndex(uint32_t idx
) const {
1088 LLDB_INSTRUMENT_VA(this, idx
);
1090 SBBreakpoint sb_breakpoint
;
1091 TargetSP
target_sp(GetSP());
1093 // The breakpoint list is thread safe, no need to lock
1094 sb_breakpoint
= target_sp
->GetBreakpointList().GetBreakpointAtIndex(idx
);
1096 return sb_breakpoint
;
1099 bool SBTarget::BreakpointDelete(break_id_t bp_id
) {
1100 LLDB_INSTRUMENT_VA(this, bp_id
);
1102 bool result
= false;
1103 TargetSP
target_sp(GetSP());
1105 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1106 result
= target_sp
->RemoveBreakpointByID(bp_id
);
1112 SBBreakpoint
SBTarget::FindBreakpointByID(break_id_t bp_id
) {
1113 LLDB_INSTRUMENT_VA(this, bp_id
);
1115 SBBreakpoint sb_breakpoint
;
1116 TargetSP
target_sp(GetSP());
1117 if (target_sp
&& bp_id
!= LLDB_INVALID_BREAK_ID
) {
1118 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1119 sb_breakpoint
= target_sp
->GetBreakpointByID(bp_id
);
1122 return sb_breakpoint
;
1125 bool SBTarget::FindBreakpointsByName(const char *name
,
1126 SBBreakpointList
&bkpts
) {
1127 LLDB_INSTRUMENT_VA(this, name
, bkpts
);
1129 TargetSP
target_sp(GetSP());
1131 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1132 llvm::Expected
<std::vector
<BreakpointSP
>> expected_vector
=
1133 target_sp
->GetBreakpointList().FindBreakpointsByName(name
);
1134 if (!expected_vector
) {
1135 LLDB_LOG_ERROR(GetLog(LLDBLog::Breakpoints
), expected_vector
.takeError(),
1136 "invalid breakpoint name: {0}");
1139 for (BreakpointSP bkpt_sp
: *expected_vector
) {
1140 bkpts
.AppendByID(bkpt_sp
->GetID());
1146 void SBTarget::GetBreakpointNames(SBStringList
&names
) {
1147 LLDB_INSTRUMENT_VA(this, names
);
1151 TargetSP
target_sp(GetSP());
1153 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1155 std::vector
<std::string
> name_vec
;
1156 target_sp
->GetBreakpointNames(name_vec
);
1157 for (const auto &name
: name_vec
)
1158 names
.AppendString(name
.c_str());
1162 void SBTarget::DeleteBreakpointName(const char *name
) {
1163 LLDB_INSTRUMENT_VA(this, name
);
1165 TargetSP
target_sp(GetSP());
1167 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1168 target_sp
->DeleteBreakpointName(ConstString(name
));
1172 bool SBTarget::EnableAllBreakpoints() {
1173 LLDB_INSTRUMENT_VA(this);
1175 TargetSP
target_sp(GetSP());
1177 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1178 target_sp
->EnableAllowedBreakpoints();
1184 bool SBTarget::DisableAllBreakpoints() {
1185 LLDB_INSTRUMENT_VA(this);
1187 TargetSP
target_sp(GetSP());
1189 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1190 target_sp
->DisableAllowedBreakpoints();
1196 bool SBTarget::DeleteAllBreakpoints() {
1197 LLDB_INSTRUMENT_VA(this);
1199 TargetSP
target_sp(GetSP());
1201 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1202 target_sp
->RemoveAllowedBreakpoints();
1208 lldb::SBError
SBTarget::BreakpointsCreateFromFile(SBFileSpec
&source_file
,
1209 SBBreakpointList
&new_bps
) {
1210 LLDB_INSTRUMENT_VA(this, source_file
, new_bps
);
1212 SBStringList empty_name_list
;
1213 return BreakpointsCreateFromFile(source_file
, empty_name_list
, new_bps
);
1216 lldb::SBError
SBTarget::BreakpointsCreateFromFile(SBFileSpec
&source_file
,
1217 SBStringList
&matching_names
,
1218 SBBreakpointList
&new_bps
) {
1219 LLDB_INSTRUMENT_VA(this, source_file
, matching_names
, new_bps
);
1222 TargetSP
target_sp(GetSP());
1224 sberr
.SetErrorString(
1225 "BreakpointCreateFromFile called with invalid target.");
1228 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1230 BreakpointIDList bp_ids
;
1232 std::vector
<std::string
> name_vector
;
1233 size_t num_names
= matching_names
.GetSize();
1234 for (size_t i
= 0; i
< num_names
; i
++)
1235 name_vector
.push_back(matching_names
.GetStringAtIndex(i
));
1237 sberr
.ref() = target_sp
->CreateBreakpointsFromFile(source_file
.ref(),
1238 name_vector
, bp_ids
);
1242 size_t num_bkpts
= bp_ids
.GetSize();
1243 for (size_t i
= 0; i
< num_bkpts
; i
++) {
1244 BreakpointID bp_id
= bp_ids
.GetBreakpointIDAtIndex(i
);
1245 new_bps
.AppendByID(bp_id
.GetBreakpointID());
1250 lldb::SBError
SBTarget::BreakpointsWriteToFile(SBFileSpec
&dest_file
) {
1251 LLDB_INSTRUMENT_VA(this, dest_file
);
1254 TargetSP
target_sp(GetSP());
1256 sberr
.SetErrorString("BreakpointWriteToFile called with invalid target.");
1259 SBBreakpointList
bkpt_list(*this);
1260 return BreakpointsWriteToFile(dest_file
, bkpt_list
);
1263 lldb::SBError
SBTarget::BreakpointsWriteToFile(SBFileSpec
&dest_file
,
1264 SBBreakpointList
&bkpt_list
,
1266 LLDB_INSTRUMENT_VA(this, dest_file
, bkpt_list
, append
);
1269 TargetSP
target_sp(GetSP());
1271 sberr
.SetErrorString("BreakpointWriteToFile called with invalid target.");
1275 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1276 BreakpointIDList bp_id_list
;
1277 bkpt_list
.CopyToBreakpointIDList(bp_id_list
);
1278 sberr
.ref() = target_sp
->SerializeBreakpointsToFile(dest_file
.ref(),
1279 bp_id_list
, append
);
1283 uint32_t SBTarget::GetNumWatchpoints() const {
1284 LLDB_INSTRUMENT_VA(this);
1286 TargetSP
target_sp(GetSP());
1288 // The watchpoint list is thread safe, no need to lock
1289 return target_sp
->GetWatchpointList().GetSize();
1294 SBWatchpoint
SBTarget::GetWatchpointAtIndex(uint32_t idx
) const {
1295 LLDB_INSTRUMENT_VA(this, idx
);
1297 SBWatchpoint sb_watchpoint
;
1298 TargetSP
target_sp(GetSP());
1300 // The watchpoint list is thread safe, no need to lock
1301 sb_watchpoint
.SetSP(target_sp
->GetWatchpointList().GetByIndex(idx
));
1303 return sb_watchpoint
;
1306 bool SBTarget::DeleteWatchpoint(watch_id_t wp_id
) {
1307 LLDB_INSTRUMENT_VA(this, wp_id
);
1309 bool result
= false;
1310 TargetSP
target_sp(GetSP());
1312 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1313 std::unique_lock
<std::recursive_mutex
> lock
;
1314 target_sp
->GetWatchpointList().GetListMutex(lock
);
1315 result
= target_sp
->RemoveWatchpointByID(wp_id
);
1321 SBWatchpoint
SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id
) {
1322 LLDB_INSTRUMENT_VA(this, wp_id
);
1324 SBWatchpoint sb_watchpoint
;
1325 lldb::WatchpointSP watchpoint_sp
;
1326 TargetSP
target_sp(GetSP());
1327 if (target_sp
&& wp_id
!= LLDB_INVALID_WATCH_ID
) {
1328 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1329 std::unique_lock
<std::recursive_mutex
> lock
;
1330 target_sp
->GetWatchpointList().GetListMutex(lock
);
1331 watchpoint_sp
= target_sp
->GetWatchpointList().FindByID(wp_id
);
1332 sb_watchpoint
.SetSP(watchpoint_sp
);
1335 return sb_watchpoint
;
1338 lldb::SBWatchpoint
SBTarget::WatchAddress(lldb::addr_t addr
, size_t size
,
1339 bool read
, bool modify
,
1341 LLDB_INSTRUMENT_VA(this, addr
, size
, read
, write
, error
);
1343 SBWatchpointOptions options
;
1344 options
.SetWatchpointTypeRead(read
);
1345 options
.SetWatchpointTypeWrite(eWatchpointWriteTypeOnModify
);
1346 return WatchpointCreateByAddress(addr
, size
, options
, error
);
1350 SBTarget::WatchpointCreateByAddress(lldb::addr_t addr
, size_t size
,
1351 SBWatchpointOptions options
,
1353 LLDB_INSTRUMENT_VA(this, addr
, size
, options
, error
);
1355 SBWatchpoint sb_watchpoint
;
1356 lldb::WatchpointSP watchpoint_sp
;
1357 TargetSP
target_sp(GetSP());
1358 uint32_t watch_type
= 0;
1359 if (options
.GetWatchpointTypeRead())
1360 watch_type
|= LLDB_WATCH_TYPE_READ
;
1361 if (options
.GetWatchpointTypeWrite() == eWatchpointWriteTypeAlways
)
1362 watch_type
|= LLDB_WATCH_TYPE_WRITE
;
1363 if (options
.GetWatchpointTypeWrite() == eWatchpointWriteTypeOnModify
)
1364 watch_type
|= LLDB_WATCH_TYPE_MODIFY
;
1365 if (watch_type
== 0) {
1366 error
.SetErrorString("Can't create a watchpoint that is neither read nor "
1367 "write nor modify.");
1368 return sb_watchpoint
;
1370 if (target_sp
&& addr
!= LLDB_INVALID_ADDRESS
&& size
> 0) {
1371 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1372 // Target::CreateWatchpoint() is thread safe.
1374 // This API doesn't take in a type, so we can't figure out what it is.
1375 CompilerType
*type
= nullptr;
1377 target_sp
->CreateWatchpoint(addr
, size
, type
, watch_type
, cw_error
);
1378 error
.SetError(std::move(cw_error
));
1379 sb_watchpoint
.SetSP(watchpoint_sp
);
1382 return sb_watchpoint
;
1385 bool SBTarget::EnableAllWatchpoints() {
1386 LLDB_INSTRUMENT_VA(this);
1388 TargetSP
target_sp(GetSP());
1390 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1391 std::unique_lock
<std::recursive_mutex
> lock
;
1392 target_sp
->GetWatchpointList().GetListMutex(lock
);
1393 target_sp
->EnableAllWatchpoints();
1399 bool SBTarget::DisableAllWatchpoints() {
1400 LLDB_INSTRUMENT_VA(this);
1402 TargetSP
target_sp(GetSP());
1404 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1405 std::unique_lock
<std::recursive_mutex
> lock
;
1406 target_sp
->GetWatchpointList().GetListMutex(lock
);
1407 target_sp
->DisableAllWatchpoints();
1413 SBValue
SBTarget::CreateValueFromAddress(const char *name
, SBAddress addr
,
1415 LLDB_INSTRUMENT_VA(this, name
, addr
, type
);
1418 lldb::ValueObjectSP new_value_sp
;
1419 if (IsValid() && name
&& *name
&& addr
.IsValid() && type
.IsValid()) {
1420 lldb::addr_t
load_addr(addr
.GetLoadAddress(*this));
1421 ExecutionContext
exe_ctx(
1422 ExecutionContextRef(ExecutionContext(m_opaque_sp
.get(), false)));
1423 CompilerType
ast_type(type
.GetSP()->GetCompilerType(true));
1424 new_value_sp
= ValueObject::CreateValueObjectFromAddress(name
, load_addr
,
1427 sb_value
.SetSP(new_value_sp
);
1431 lldb::SBValue
SBTarget::CreateValueFromData(const char *name
, lldb::SBData data
,
1432 lldb::SBType type
) {
1433 LLDB_INSTRUMENT_VA(this, name
, data
, type
);
1436 lldb::ValueObjectSP new_value_sp
;
1437 if (IsValid() && name
&& *name
&& data
.IsValid() && type
.IsValid()) {
1438 DataExtractorSP
extractor(*data
);
1439 ExecutionContext
exe_ctx(
1440 ExecutionContextRef(ExecutionContext(m_opaque_sp
.get(), false)));
1441 CompilerType
ast_type(type
.GetSP()->GetCompilerType(true));
1442 new_value_sp
= ValueObject::CreateValueObjectFromData(name
, *extractor
,
1445 sb_value
.SetSP(new_value_sp
);
1449 lldb::SBValue
SBTarget::CreateValueFromExpression(const char *name
,
1451 LLDB_INSTRUMENT_VA(this, name
, expr
);
1454 lldb::ValueObjectSP new_value_sp
;
1455 if (IsValid() && name
&& *name
&& expr
&& *expr
) {
1456 ExecutionContext
exe_ctx(
1457 ExecutionContextRef(ExecutionContext(m_opaque_sp
.get(), false)));
1459 ValueObject::CreateValueObjectFromExpression(name
, expr
, exe_ctx
);
1461 sb_value
.SetSP(new_value_sp
);
1465 bool SBTarget::DeleteAllWatchpoints() {
1466 LLDB_INSTRUMENT_VA(this);
1468 TargetSP
target_sp(GetSP());
1470 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1471 std::unique_lock
<std::recursive_mutex
> lock
;
1472 target_sp
->GetWatchpointList().GetListMutex(lock
);
1473 target_sp
->RemoveAllWatchpoints();
1479 void SBTarget::AppendImageSearchPath(const char *from
, const char *to
,
1480 lldb::SBError
&error
) {
1481 LLDB_INSTRUMENT_VA(this, from
, to
, error
);
1483 TargetSP
target_sp(GetSP());
1485 return error
.SetErrorString("invalid target");
1487 llvm::StringRef srFrom
= from
, srTo
= to
;
1489 return error
.SetErrorString("<from> path can't be empty");
1491 return error
.SetErrorString("<to> path can't be empty");
1493 target_sp
->GetImageSearchPathList().Append(srFrom
, srTo
, true);
1496 lldb::SBModule
SBTarget::AddModule(const char *path
, const char *triple
,
1497 const char *uuid_cstr
) {
1498 LLDB_INSTRUMENT_VA(this, path
, triple
, uuid_cstr
);
1500 return AddModule(path
, triple
, uuid_cstr
, nullptr);
1503 lldb::SBModule
SBTarget::AddModule(const char *path
, const char *triple
,
1504 const char *uuid_cstr
, const char *symfile
) {
1505 LLDB_INSTRUMENT_VA(this, path
, triple
, uuid_cstr
, symfile
);
1507 TargetSP
target_sp(GetSP());
1511 ModuleSpec module_spec
;
1513 module_spec
.GetFileSpec().SetFile(path
, FileSpec::Style::native
);
1516 module_spec
.GetUUID().SetFromStringRef(uuid_cstr
);
1519 module_spec
.GetArchitecture() =
1520 Platform::GetAugmentedArchSpec(target_sp
->GetPlatform().get(), triple
);
1522 module_spec
.GetArchitecture() = target_sp
->GetArchitecture();
1525 module_spec
.GetSymbolFileSpec().SetFile(symfile
, FileSpec::Style::native
);
1527 SBModuleSpec
sb_modulespec(module_spec
);
1529 return AddModule(sb_modulespec
);
1532 lldb::SBModule
SBTarget::AddModule(const SBModuleSpec
&module_spec
) {
1533 LLDB_INSTRUMENT_VA(this, module_spec
);
1535 lldb::SBModule sb_module
;
1536 TargetSP
target_sp(GetSP());
1538 sb_module
.SetSP(target_sp
->GetOrCreateModule(*module_spec
.m_opaque_up
,
1539 true /* notify */));
1540 if (!sb_module
.IsValid() && module_spec
.m_opaque_up
->GetUUID().IsValid()) {
1542 if (PluginManager::DownloadObjectAndSymbolFile(*module_spec
.m_opaque_up
,
1544 /* force_lookup */ true)) {
1545 if (FileSystem::Instance().Exists(
1546 module_spec
.m_opaque_up
->GetFileSpec())) {
1547 sb_module
.SetSP(target_sp
->GetOrCreateModule(*module_spec
.m_opaque_up
,
1548 true /* notify */));
1553 // If the target hasn't initialized any architecture yet, use the
1554 // binary's architecture.
1555 if (sb_module
.IsValid() && !target_sp
->GetArchitecture().IsValid() &&
1556 sb_module
.GetSP()->GetArchitecture().IsValid())
1557 target_sp
->SetArchitecture(sb_module
.GetSP()->GetArchitecture());
1561 bool SBTarget::AddModule(lldb::SBModule
&module
) {
1562 LLDB_INSTRUMENT_VA(this, module
);
1564 TargetSP
target_sp(GetSP());
1566 target_sp
->GetImages().AppendIfNeeded(module
.GetSP());
1572 uint32_t SBTarget::GetNumModules() const {
1573 LLDB_INSTRUMENT_VA(this);
1576 TargetSP
target_sp(GetSP());
1578 // The module list is thread safe, no need to lock
1579 num
= target_sp
->GetImages().GetSize();
1585 void SBTarget::Clear() {
1586 LLDB_INSTRUMENT_VA(this);
1588 m_opaque_sp
.reset();
1591 SBModule
SBTarget::FindModule(const SBFileSpec
&sb_file_spec
) {
1592 LLDB_INSTRUMENT_VA(this, sb_file_spec
);
1595 TargetSP
target_sp(GetSP());
1596 if (target_sp
&& sb_file_spec
.IsValid()) {
1597 ModuleSpec
module_spec(*sb_file_spec
);
1598 // The module list is thread safe, no need to lock
1599 sb_module
.SetSP(target_sp
->GetImages().FindFirstModule(module_spec
));
1604 SBSymbolContextList
SBTarget::FindCompileUnits(const SBFileSpec
&sb_file_spec
) {
1605 LLDB_INSTRUMENT_VA(this, sb_file_spec
);
1607 SBSymbolContextList sb_sc_list
;
1608 const TargetSP
target_sp(GetSP());
1609 if (target_sp
&& sb_file_spec
.IsValid())
1610 target_sp
->GetImages().FindCompileUnits(*sb_file_spec
, *sb_sc_list
);
1614 lldb::ByteOrder
SBTarget::GetByteOrder() {
1615 LLDB_INSTRUMENT_VA(this);
1617 TargetSP
target_sp(GetSP());
1619 return target_sp
->GetArchitecture().GetByteOrder();
1620 return eByteOrderInvalid
;
1623 const char *SBTarget::GetTriple() {
1624 LLDB_INSTRUMENT_VA(this);
1626 TargetSP
target_sp(GetSP());
1630 std::string
triple(target_sp
->GetArchitecture().GetTriple().str());
1631 // Unique the string so we don't run into ownership issues since the const
1632 // strings put the string into the string pool once and the strings never
1634 ConstString
const_triple(triple
.c_str());
1635 return const_triple
.GetCString();
1638 const char *SBTarget::GetABIName() {
1639 LLDB_INSTRUMENT_VA(this);
1641 TargetSP
target_sp(GetSP());
1645 std::string
abi_name(target_sp
->GetABIName().str());
1646 ConstString
const_name(abi_name
.c_str());
1647 return const_name
.GetCString();
1650 const char *SBTarget::GetLabel() const {
1651 LLDB_INSTRUMENT_VA(this);
1653 TargetSP
target_sp(GetSP());
1657 return ConstString(target_sp
->GetLabel().data()).AsCString();
1660 SBError
SBTarget::SetLabel(const char *label
) {
1661 LLDB_INSTRUMENT_VA(this, label
);
1663 TargetSP
target_sp(GetSP());
1665 return Status::FromErrorString("Couldn't get internal target object.");
1667 return Status::FromError(target_sp
->SetLabel(label
));
1670 uint32_t SBTarget::GetDataByteSize() {
1671 LLDB_INSTRUMENT_VA(this);
1673 TargetSP
target_sp(GetSP());
1675 return target_sp
->GetArchitecture().GetDataByteSize();
1680 uint32_t SBTarget::GetCodeByteSize() {
1681 LLDB_INSTRUMENT_VA(this);
1683 TargetSP
target_sp(GetSP());
1685 return target_sp
->GetArchitecture().GetCodeByteSize();
1690 uint32_t SBTarget::GetMaximumNumberOfChildrenToDisplay() const {
1691 LLDB_INSTRUMENT_VA(this);
1693 TargetSP
target_sp(GetSP());
1695 return target_sp
->GetMaximumNumberOfChildrenToDisplay();
1700 uint32_t SBTarget::GetAddressByteSize() {
1701 LLDB_INSTRUMENT_VA(this);
1703 TargetSP
target_sp(GetSP());
1705 return target_sp
->GetArchitecture().GetAddressByteSize();
1706 return sizeof(void *);
1709 SBModule
SBTarget::GetModuleAtIndex(uint32_t idx
) {
1710 LLDB_INSTRUMENT_VA(this, idx
);
1714 TargetSP
target_sp(GetSP());
1716 // The module list is thread safe, no need to lock
1717 module_sp
= target_sp
->GetImages().GetModuleAtIndex(idx
);
1718 sb_module
.SetSP(module_sp
);
1724 bool SBTarget::RemoveModule(lldb::SBModule module
) {
1725 LLDB_INSTRUMENT_VA(this, module
);
1727 TargetSP
target_sp(GetSP());
1729 return target_sp
->GetImages().Remove(module
.GetSP());
1733 SBBroadcaster
SBTarget::GetBroadcaster() const {
1734 LLDB_INSTRUMENT_VA(this);
1736 TargetSP
target_sp(GetSP());
1737 SBBroadcaster
broadcaster(target_sp
.get(), false);
1742 bool SBTarget::GetDescription(SBStream
&description
,
1743 lldb::DescriptionLevel description_level
) {
1744 LLDB_INSTRUMENT_VA(this, description
, description_level
);
1746 Stream
&strm
= description
.ref();
1748 TargetSP
target_sp(GetSP());
1750 target_sp
->Dump(&strm
, description_level
);
1752 strm
.PutCString("No value");
1757 lldb::SBSymbolContextList
SBTarget::FindFunctions(const char *name
,
1758 uint32_t name_type_mask
) {
1759 LLDB_INSTRUMENT_VA(this, name
, name_type_mask
);
1761 lldb::SBSymbolContextList sb_sc_list
;
1762 if (!name
|| !name
[0])
1765 TargetSP
target_sp(GetSP());
1769 ModuleFunctionSearchOptions function_options
;
1770 function_options
.include_symbols
= true;
1771 function_options
.include_inlines
= true;
1773 FunctionNameType mask
= static_cast<FunctionNameType
>(name_type_mask
);
1774 target_sp
->GetImages().FindFunctions(ConstString(name
), mask
,
1775 function_options
, *sb_sc_list
);
1779 lldb::SBSymbolContextList
SBTarget::FindGlobalFunctions(const char *name
,
1780 uint32_t max_matches
,
1781 MatchType matchtype
) {
1782 LLDB_INSTRUMENT_VA(this, name
, max_matches
, matchtype
);
1784 lldb::SBSymbolContextList sb_sc_list
;
1785 if (name
&& name
[0]) {
1786 llvm::StringRef
name_ref(name
);
1787 TargetSP
target_sp(GetSP());
1789 ModuleFunctionSearchOptions function_options
;
1790 function_options
.include_symbols
= true;
1791 function_options
.include_inlines
= true;
1793 std::string regexstr
;
1794 switch (matchtype
) {
1795 case eMatchTypeRegex
:
1796 target_sp
->GetImages().FindFunctions(RegularExpression(name_ref
),
1797 function_options
, *sb_sc_list
);
1799 case eMatchTypeRegexInsensitive
:
1800 target_sp
->GetImages().FindFunctions(
1801 RegularExpression(name_ref
, llvm::Regex::RegexFlags::IgnoreCase
),
1802 function_options
, *sb_sc_list
);
1804 case eMatchTypeStartsWith
:
1805 regexstr
= llvm::Regex::escape(name
) + ".*";
1806 target_sp
->GetImages().FindFunctions(RegularExpression(regexstr
),
1807 function_options
, *sb_sc_list
);
1810 target_sp
->GetImages().FindFunctions(ConstString(name
),
1811 eFunctionNameTypeAny
,
1812 function_options
, *sb_sc_list
);
1820 lldb::SBType
SBTarget::FindFirstType(const char *typename_cstr
) {
1821 LLDB_INSTRUMENT_VA(this, typename_cstr
);
1823 TargetSP
target_sp(GetSP());
1824 if (typename_cstr
&& typename_cstr
[0] && target_sp
) {
1825 ConstString
const_typename(typename_cstr
);
1826 TypeQuery
query(const_typename
.GetStringRef(),
1827 TypeQueryOptions::e_find_one
);
1828 TypeResults results
;
1829 target_sp
->GetImages().FindTypes(/*search_first=*/nullptr, query
, results
);
1830 TypeSP type_sp
= results
.GetFirstType();
1832 return SBType(type_sp
);
1833 // Didn't find the type in the symbols; Try the loaded language runtimes.
1834 if (auto process_sp
= target_sp
->GetProcessSP()) {
1835 for (auto *runtime
: process_sp
->GetLanguageRuntimes()) {
1836 if (auto vendor
= runtime
->GetDeclVendor()) {
1837 auto types
= vendor
->FindTypes(const_typename
, /*max_matches*/ 1);
1839 return SBType(types
.front());
1844 // No matches, search for basic typename matches.
1845 for (auto type_system_sp
: target_sp
->GetScratchTypeSystems())
1846 if (auto type
= type_system_sp
->GetBuiltinTypeByName(const_typename
))
1847 return SBType(type
);
1853 SBType
SBTarget::GetBasicType(lldb::BasicType type
) {
1854 LLDB_INSTRUMENT_VA(this, type
);
1856 TargetSP
target_sp(GetSP());
1858 for (auto type_system_sp
: target_sp
->GetScratchTypeSystems())
1859 if (auto compiler_type
= type_system_sp
->GetBasicTypeFromAST(type
))
1860 return SBType(compiler_type
);
1865 lldb::SBTypeList
SBTarget::FindTypes(const char *typename_cstr
) {
1866 LLDB_INSTRUMENT_VA(this, typename_cstr
);
1868 SBTypeList sb_type_list
;
1869 TargetSP
target_sp(GetSP());
1870 if (typename_cstr
&& typename_cstr
[0] && target_sp
) {
1871 ModuleList
&images
= target_sp
->GetImages();
1872 ConstString
const_typename(typename_cstr
);
1873 TypeQuery
query(typename_cstr
);
1874 TypeResults results
;
1875 images
.FindTypes(nullptr, query
, results
);
1876 for (const TypeSP
&type_sp
: results
.GetTypeMap().Types())
1877 sb_type_list
.Append(SBType(type_sp
));
1879 // Try the loaded language runtimes
1880 if (auto process_sp
= target_sp
->GetProcessSP()) {
1881 for (auto *runtime
: process_sp
->GetLanguageRuntimes()) {
1882 if (auto *vendor
= runtime
->GetDeclVendor()) {
1884 vendor
->FindTypes(const_typename
, /*max_matches*/ UINT32_MAX
);
1885 for (auto type
: types
)
1886 sb_type_list
.Append(SBType(type
));
1891 if (sb_type_list
.GetSize() == 0) {
1892 // No matches, search for basic typename matches
1893 for (auto type_system_sp
: target_sp
->GetScratchTypeSystems())
1894 if (auto compiler_type
=
1895 type_system_sp
->GetBuiltinTypeByName(const_typename
))
1896 sb_type_list
.Append(SBType(compiler_type
));
1899 return sb_type_list
;
1902 SBValueList
SBTarget::FindGlobalVariables(const char *name
,
1903 uint32_t max_matches
) {
1904 LLDB_INSTRUMENT_VA(this, name
, max_matches
);
1906 SBValueList sb_value_list
;
1908 TargetSP
target_sp(GetSP());
1909 if (name
&& target_sp
) {
1910 VariableList variable_list
;
1911 target_sp
->GetImages().FindGlobalVariables(ConstString(name
), max_matches
,
1913 if (!variable_list
.Empty()) {
1914 ExecutionContextScope
*exe_scope
= target_sp
->GetProcessSP().get();
1915 if (exe_scope
== nullptr)
1916 exe_scope
= target_sp
.get();
1917 for (const VariableSP
&var_sp
: variable_list
) {
1918 lldb::ValueObjectSP
valobj_sp(
1919 ValueObjectVariable::Create(exe_scope
, var_sp
));
1921 sb_value_list
.Append(SBValue(valobj_sp
));
1926 return sb_value_list
;
1929 SBValueList
SBTarget::FindGlobalVariables(const char *name
,
1930 uint32_t max_matches
,
1931 MatchType matchtype
) {
1932 LLDB_INSTRUMENT_VA(this, name
, max_matches
, matchtype
);
1934 SBValueList sb_value_list
;
1936 TargetSP
target_sp(GetSP());
1937 if (name
&& target_sp
) {
1938 llvm::StringRef
name_ref(name
);
1939 VariableList variable_list
;
1941 std::string regexstr
;
1942 switch (matchtype
) {
1943 case eMatchTypeNormal
:
1944 target_sp
->GetImages().FindGlobalVariables(ConstString(name
), max_matches
,
1947 case eMatchTypeRegex
:
1948 target_sp
->GetImages().FindGlobalVariables(RegularExpression(name_ref
),
1949 max_matches
, variable_list
);
1951 case eMatchTypeRegexInsensitive
:
1952 target_sp
->GetImages().FindGlobalVariables(
1953 RegularExpression(name_ref
, llvm::Regex::IgnoreCase
), max_matches
,
1956 case eMatchTypeStartsWith
:
1957 regexstr
= "^" + llvm::Regex::escape(name
) + ".*";
1958 target_sp
->GetImages().FindGlobalVariables(RegularExpression(regexstr
),
1959 max_matches
, variable_list
);
1962 if (!variable_list
.Empty()) {
1963 ExecutionContextScope
*exe_scope
= target_sp
->GetProcessSP().get();
1964 if (exe_scope
== nullptr)
1965 exe_scope
= target_sp
.get();
1966 for (const VariableSP
&var_sp
: variable_list
) {
1967 lldb::ValueObjectSP
valobj_sp(
1968 ValueObjectVariable::Create(exe_scope
, var_sp
));
1970 sb_value_list
.Append(SBValue(valobj_sp
));
1975 return sb_value_list
;
1978 lldb::SBValue
SBTarget::FindFirstGlobalVariable(const char *name
) {
1979 LLDB_INSTRUMENT_VA(this, name
);
1981 SBValueList
sb_value_list(FindGlobalVariables(name
, 1));
1982 if (sb_value_list
.IsValid() && sb_value_list
.GetSize() > 0)
1983 return sb_value_list
.GetValueAtIndex(0);
1987 SBSourceManager
SBTarget::GetSourceManager() {
1988 LLDB_INSTRUMENT_VA(this);
1990 SBSourceManager
source_manager(*this);
1991 return source_manager
;
1994 lldb::SBInstructionList
SBTarget::ReadInstructions(lldb::SBAddress base_addr
,
1996 LLDB_INSTRUMENT_VA(this, base_addr
, count
);
1998 return ReadInstructions(base_addr
, count
, nullptr);
2001 lldb::SBInstructionList
SBTarget::ReadInstructions(lldb::SBAddress base_addr
,
2003 const char *flavor_string
) {
2004 LLDB_INSTRUMENT_VA(this, base_addr
, count
, flavor_string
);
2006 SBInstructionList sb_instructions
;
2008 TargetSP
target_sp(GetSP());
2010 Address
*addr_ptr
= base_addr
.get();
2013 DataBufferHeap
data(
2014 target_sp
->GetArchitecture().GetMaximumOpcodeByteSize() * count
, 0);
2015 bool force_live_memory
= true;
2016 lldb_private::Status error
;
2017 lldb::addr_t load_addr
= LLDB_INVALID_ADDRESS
;
2018 const size_t bytes_read
=
2019 target_sp
->ReadMemory(*addr_ptr
, data
.GetBytes(), data
.GetByteSize(),
2020 error
, force_live_memory
, &load_addr
);
2021 const bool data_from_file
= load_addr
== LLDB_INVALID_ADDRESS
;
2022 sb_instructions
.SetDisassembler(Disassembler::DisassembleBytes(
2023 target_sp
->GetArchitecture(), nullptr, target_sp
->GetDisassemblyCPU(),
2024 target_sp
->GetDisassemblyFeatures(), flavor_string
, *addr_ptr
,
2025 data
.GetBytes(), bytes_read
, count
, data_from_file
));
2029 return sb_instructions
;
2032 lldb::SBInstructionList
SBTarget::ReadInstructions(lldb::SBAddress start_addr
,
2033 lldb::SBAddress end_addr
,
2034 const char *flavor_string
) {
2035 LLDB_INSTRUMENT_VA(this, start_addr
, end_addr
, flavor_string
);
2037 SBInstructionList sb_instructions
;
2039 TargetSP
target_sp(GetSP());
2041 lldb::addr_t start_load_addr
= start_addr
.GetLoadAddress(*this);
2042 lldb::addr_t end_load_addr
= end_addr
.GetLoadAddress(*this);
2043 if (end_load_addr
> start_load_addr
) {
2044 lldb::addr_t size
= end_load_addr
- start_load_addr
;
2046 AddressRange
range(start_load_addr
, size
);
2047 const bool force_live_memory
= true;
2048 sb_instructions
.SetDisassembler(Disassembler::DisassembleRange(
2049 target_sp
->GetArchitecture(), nullptr, flavor_string
,
2050 target_sp
->GetDisassemblyCPU(), target_sp
->GetDisassemblyFeatures(),
2051 *target_sp
, range
, force_live_memory
));
2054 return sb_instructions
;
2057 lldb::SBInstructionList
SBTarget::GetInstructions(lldb::SBAddress base_addr
,
2060 LLDB_INSTRUMENT_VA(this, base_addr
, buf
, size
);
2062 return GetInstructionsWithFlavor(base_addr
, nullptr, buf
, size
);
2065 lldb::SBInstructionList
2066 SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr
,
2067 const char *flavor_string
, const void *buf
,
2069 LLDB_INSTRUMENT_VA(this, base_addr
, flavor_string
, buf
, size
);
2071 SBInstructionList sb_instructions
;
2073 TargetSP
target_sp(GetSP());
2077 if (base_addr
.get())
2078 addr
= *base_addr
.get();
2080 const bool data_from_file
= true;
2082 sb_instructions
.SetDisassembler(Disassembler::DisassembleBytes(
2083 target_sp
->GetArchitecture(), nullptr, flavor_string
,
2084 target_sp
->GetDisassemblyCPU(), target_sp
->GetDisassemblyFeatures(),
2085 addr
, buf
, size
, UINT32_MAX
, data_from_file
));
2088 return sb_instructions
;
2091 lldb::SBInstructionList
SBTarget::GetInstructions(lldb::addr_t base_addr
,
2094 LLDB_INSTRUMENT_VA(this, base_addr
, buf
, size
);
2096 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr
), nullptr, buf
,
2100 lldb::SBInstructionList
2101 SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr
,
2102 const char *flavor_string
, const void *buf
,
2104 LLDB_INSTRUMENT_VA(this, base_addr
, flavor_string
, buf
, size
);
2106 return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr
), flavor_string
,
2110 SBError
SBTarget::SetSectionLoadAddress(lldb::SBSection section
,
2111 lldb::addr_t section_base_addr
) {
2112 LLDB_INSTRUMENT_VA(this, section
, section_base_addr
);
2115 TargetSP
target_sp(GetSP());
2117 if (!section
.IsValid()) {
2118 sb_error
.SetErrorStringWithFormat("invalid section");
2120 SectionSP
section_sp(section
.GetSP());
2122 if (section_sp
->IsThreadSpecific()) {
2123 sb_error
.SetErrorString(
2124 "thread specific sections are not yet supported");
2126 ProcessSP
process_sp(target_sp
->GetProcessSP());
2127 if (target_sp
->SetSectionLoadAddress(section_sp
, section_base_addr
)) {
2128 ModuleSP
module_sp(section_sp
->GetModule());
2130 ModuleList module_list
;
2131 module_list
.Append(module_sp
);
2132 target_sp
->ModulesDidLoad(module_list
);
2134 // Flush info in the process (stack frames, etc)
2136 process_sp
->Flush();
2142 sb_error
.SetErrorString("invalid target");
2147 SBError
SBTarget::ClearSectionLoadAddress(lldb::SBSection section
) {
2148 LLDB_INSTRUMENT_VA(this, section
);
2152 TargetSP
target_sp(GetSP());
2154 if (!section
.IsValid()) {
2155 sb_error
.SetErrorStringWithFormat("invalid section");
2157 SectionSP
section_sp(section
.GetSP());
2159 ProcessSP
process_sp(target_sp
->GetProcessSP());
2160 if (target_sp
->SetSectionUnloaded(section_sp
)) {
2161 ModuleSP
module_sp(section_sp
->GetModule());
2163 ModuleList module_list
;
2164 module_list
.Append(module_sp
);
2165 target_sp
->ModulesDidUnload(module_list
, false);
2167 // Flush info in the process (stack frames, etc)
2169 process_sp
->Flush();
2172 sb_error
.SetErrorStringWithFormat("invalid section");
2176 sb_error
.SetErrorStringWithFormat("invalid target");
2181 SBError
SBTarget::SetModuleLoadAddress(lldb::SBModule module
,
2182 int64_t slide_offset
) {
2183 LLDB_INSTRUMENT_VA(this, module
, slide_offset
);
2185 if (slide_offset
< 0) {
2187 sb_error
.SetErrorStringWithFormat("slide must be positive");
2191 return SetModuleLoadAddress(module
, static_cast<uint64_t>(slide_offset
));
2194 SBError
SBTarget::SetModuleLoadAddress(lldb::SBModule module
,
2195 uint64_t slide_offset
) {
2199 TargetSP
target_sp(GetSP());
2201 ModuleSP
module_sp(module
.GetSP());
2203 bool changed
= false;
2204 if (module_sp
->SetLoadAddress(*target_sp
, slide_offset
, true, changed
)) {
2205 // The load was successful, make sure that at least some sections
2206 // changed before we notify that our module was loaded.
2208 ModuleList module_list
;
2209 module_list
.Append(module_sp
);
2210 target_sp
->ModulesDidLoad(module_list
);
2211 // Flush info in the process (stack frames, etc)
2212 ProcessSP
process_sp(target_sp
->GetProcessSP());
2214 process_sp
->Flush();
2218 sb_error
.SetErrorStringWithFormat("invalid module");
2222 sb_error
.SetErrorStringWithFormat("invalid target");
2227 SBError
SBTarget::ClearModuleLoadAddress(lldb::SBModule module
) {
2228 LLDB_INSTRUMENT_VA(this, module
);
2232 char path
[PATH_MAX
];
2233 TargetSP
target_sp(GetSP());
2235 ModuleSP
module_sp(module
.GetSP());
2237 ObjectFile
*objfile
= module_sp
->GetObjectFile();
2239 SectionList
*section_list
= objfile
->GetSectionList();
2241 ProcessSP
process_sp(target_sp
->GetProcessSP());
2243 bool changed
= false;
2244 const size_t num_sections
= section_list
->GetSize();
2245 for (size_t sect_idx
= 0; sect_idx
< num_sections
; ++sect_idx
) {
2246 SectionSP
section_sp(section_list
->GetSectionAtIndex(sect_idx
));
2248 changed
|= target_sp
->SetSectionUnloaded(section_sp
);
2251 ModuleList module_list
;
2252 module_list
.Append(module_sp
);
2253 target_sp
->ModulesDidUnload(module_list
, false);
2254 // Flush info in the process (stack frames, etc)
2255 ProcessSP
process_sp(target_sp
->GetProcessSP());
2257 process_sp
->Flush();
2260 module_sp
->GetFileSpec().GetPath(path
, sizeof(path
));
2261 sb_error
.SetErrorStringWithFormat("no sections in object file '%s'",
2265 module_sp
->GetFileSpec().GetPath(path
, sizeof(path
));
2266 sb_error
.SetErrorStringWithFormat("no object file for module '%s'",
2270 sb_error
.SetErrorStringWithFormat("invalid module");
2273 sb_error
.SetErrorStringWithFormat("invalid target");
2278 lldb::SBSymbolContextList
SBTarget::FindSymbols(const char *name
,
2279 lldb::SymbolType symbol_type
) {
2280 LLDB_INSTRUMENT_VA(this, name
, symbol_type
);
2282 SBSymbolContextList sb_sc_list
;
2283 if (name
&& name
[0]) {
2284 TargetSP
target_sp(GetSP());
2286 target_sp
->GetImages().FindSymbolsWithNameAndType(
2287 ConstString(name
), symbol_type
, *sb_sc_list
);
2292 lldb::SBValue
SBTarget::EvaluateExpression(const char *expr
) {
2293 LLDB_INSTRUMENT_VA(this, expr
);
2295 TargetSP
target_sp(GetSP());
2299 SBExpressionOptions options
;
2300 lldb::DynamicValueType fetch_dynamic_value
=
2301 target_sp
->GetPreferDynamicValue();
2302 options
.SetFetchDynamicValue(fetch_dynamic_value
);
2303 options
.SetUnwindOnError(true);
2304 return EvaluateExpression(expr
, options
);
2307 lldb::SBValue
SBTarget::EvaluateExpression(const char *expr
,
2308 const SBExpressionOptions
&options
) {
2309 LLDB_INSTRUMENT_VA(this, expr
, options
);
2311 Log
*expr_log
= GetLog(LLDBLog::Expressions
);
2312 SBValue expr_result
;
2313 ValueObjectSP expr_value_sp
;
2314 TargetSP
target_sp(GetSP());
2315 StackFrame
*frame
= nullptr;
2317 if (expr
== nullptr || expr
[0] == '\0') {
2321 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
2322 ExecutionContext
exe_ctx(m_opaque_sp
.get());
2324 frame
= exe_ctx
.GetFramePtr();
2325 Target
*target
= exe_ctx
.GetTargetPtr();
2326 Process
*process
= exe_ctx
.GetProcessPtr();
2329 // If we have a process, make sure to lock the runlock:
2331 Process::StopLocker stop_locker
;
2332 if (stop_locker
.TryLock(&process
->GetRunLock())) {
2333 target
->EvaluateExpression(expr
, frame
, expr_value_sp
, options
.ref());
2336 error
= Status::FromErrorString("can't evaluate expressions when the "
2337 "process is running.");
2339 ValueObjectConstResult::Create(nullptr, std::move(error
));
2342 target
->EvaluateExpression(expr
, frame
, expr_value_sp
, options
.ref());
2345 expr_result
.SetSP(expr_value_sp
, options
.GetFetchDynamicValue());
2349 "** [SBTarget::EvaluateExpression] Expression result is "
2350 "%s, summary %s **",
2351 expr_result
.GetValue(), expr_result
.GetSummary());
2355 lldb::addr_t
SBTarget::GetStackRedZoneSize() {
2356 LLDB_INSTRUMENT_VA(this);
2358 TargetSP
target_sp(GetSP());
2361 ProcessSP
process_sp(target_sp
->GetProcessSP());
2363 abi_sp
= process_sp
->GetABI();
2365 abi_sp
= ABI::FindPlugin(ProcessSP(), target_sp
->GetArchitecture());
2367 return abi_sp
->GetRedZoneSize();
2372 bool SBTarget::IsLoaded(const SBModule
&module
) const {
2373 LLDB_INSTRUMENT_VA(this, module
);
2375 TargetSP
target_sp(GetSP());
2379 ModuleSP
module_sp(module
.GetSP());
2383 return module_sp
->IsLoadedInTarget(target_sp
.get());
2386 lldb::SBLaunchInfo
SBTarget::GetLaunchInfo() const {
2387 LLDB_INSTRUMENT_VA(this);
2389 lldb::SBLaunchInfo
launch_info(nullptr);
2390 TargetSP
target_sp(GetSP());
2392 launch_info
.set_ref(m_opaque_sp
->GetProcessLaunchInfo());
2396 void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo
&launch_info
) {
2397 LLDB_INSTRUMENT_VA(this, launch_info
);
2399 TargetSP
target_sp(GetSP());
2401 m_opaque_sp
->SetProcessLaunchInfo(launch_info
.ref());
2404 SBEnvironment
SBTarget::GetEnvironment() {
2405 LLDB_INSTRUMENT_VA(this);
2406 TargetSP
target_sp(GetSP());
2409 return SBEnvironment(target_sp
->GetEnvironment());
2412 return SBEnvironment();
2415 lldb::SBTrace
SBTarget::GetTrace() {
2416 LLDB_INSTRUMENT_VA(this);
2417 TargetSP
target_sp(GetSP());
2420 return SBTrace(target_sp
->GetTrace());
2425 lldb::SBTrace
SBTarget::CreateTrace(lldb::SBError
&error
) {
2426 LLDB_INSTRUMENT_VA(this, error
);
2427 TargetSP
target_sp(GetSP());
2431 if (llvm::Expected
<lldb::TraceSP
> trace_sp
= target_sp
->CreateTrace()) {
2432 return SBTrace(*trace_sp
);
2434 error
.SetErrorString(llvm::toString(trace_sp
.takeError()).c_str());
2437 error
.SetErrorString("missing target");