[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBProcess.cpp
blob9773144723c34c47bf2cd6cb69eebced25926cee
1 //===-- SBProcess.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 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBProcess.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include <cinttypes>
14 #include "lldb/lldb-defines.h"
15 #include "lldb/lldb-types.h"
17 #include "lldb/Core/AddressRangeListImpl.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/StructuredDataImpl.h"
22 #include "lldb/Host/StreamFile.h"
23 #include "lldb/Target/MemoryRegionInfo.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/SystemRuntime.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 #include "lldb/Utility/Args.h"
30 #include "lldb/Utility/LLDBLog.h"
31 #include "lldb/Utility/ProcessInfo.h"
32 #include "lldb/Utility/State.h"
33 #include "lldb/Utility/Stream.h"
35 #include "lldb/API/SBBroadcaster.h"
36 #include "lldb/API/SBCommandReturnObject.h"
37 #include "lldb/API/SBDebugger.h"
38 #include "lldb/API/SBEvent.h"
39 #include "lldb/API/SBFile.h"
40 #include "lldb/API/SBFileSpec.h"
41 #include "lldb/API/SBMemoryRegionInfo.h"
42 #include "lldb/API/SBMemoryRegionInfoList.h"
43 #include "lldb/API/SBSaveCoreOptions.h"
44 #include "lldb/API/SBScriptObject.h"
45 #include "lldb/API/SBStream.h"
46 #include "lldb/API/SBStringList.h"
47 #include "lldb/API/SBStructuredData.h"
48 #include "lldb/API/SBThread.h"
49 #include "lldb/API/SBThreadCollection.h"
50 #include "lldb/API/SBTrace.h"
51 #include "lldb/API/SBUnixSignals.h"
53 using namespace lldb;
54 using namespace lldb_private;
56 SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }
58 // SBProcess constructor
60 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
61 LLDB_INSTRUMENT_VA(this, rhs);
64 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
65 : m_opaque_wp(process_sp) {
66 LLDB_INSTRUMENT_VA(this, process_sp);
69 const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
70 LLDB_INSTRUMENT_VA(this, rhs);
72 if (this != &rhs)
73 m_opaque_wp = rhs.m_opaque_wp;
74 return *this;
77 // Destructor
78 SBProcess::~SBProcess() = default;
80 const char *SBProcess::GetBroadcasterClassName() {
81 LLDB_INSTRUMENT();
83 return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
86 const char *SBProcess::GetPluginName() {
87 LLDB_INSTRUMENT_VA(this);
89 ProcessSP process_sp(GetSP());
90 if (process_sp) {
91 return ConstString(process_sp->GetPluginName()).GetCString();
93 return "<Unknown>";
96 const char *SBProcess::GetShortPluginName() {
97 LLDB_INSTRUMENT_VA(this);
99 ProcessSP process_sp(GetSP());
100 if (process_sp) {
101 return ConstString(process_sp->GetPluginName()).GetCString();
103 return "<Unknown>";
106 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
108 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
110 void SBProcess::Clear() {
111 LLDB_INSTRUMENT_VA(this);
113 m_opaque_wp.reset();
116 bool SBProcess::IsValid() const {
117 LLDB_INSTRUMENT_VA(this);
118 return this->operator bool();
120 SBProcess::operator bool() const {
121 LLDB_INSTRUMENT_VA(this);
123 ProcessSP process_sp(m_opaque_wp.lock());
124 return ((bool)process_sp && process_sp->IsValid());
127 bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
128 const char *stdin_path, const char *stdout_path,
129 const char *stderr_path,
130 const char *working_directory,
131 uint32_t launch_flags, bool stop_at_entry,
132 lldb::SBError &error) {
133 LLDB_INSTRUMENT_VA(this, argv, envp, stdin_path, stdout_path, stderr_path,
134 working_directory, launch_flags, stop_at_entry, error);
136 ProcessSP process_sp(GetSP());
137 if (process_sp) {
138 std::lock_guard<std::recursive_mutex> guard(
139 process_sp->GetTarget().GetAPIMutex());
140 if (process_sp->GetState() == eStateConnected) {
141 if (stop_at_entry)
142 launch_flags |= eLaunchFlagStopAtEntry;
143 ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),
144 FileSpec(stderr_path),
145 FileSpec(working_directory), launch_flags);
146 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
147 if (exe_module)
148 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
149 if (argv)
150 launch_info.GetArguments().AppendArguments(argv);
151 if (envp)
152 launch_info.GetEnvironment() = Environment(envp);
153 error.SetError(process_sp->Launch(launch_info));
154 } else {
155 error = Status::FromErrorString(
156 "must be in eStateConnected to call RemoteLaunch");
158 } else {
159 error = Status::FromErrorString("unable to attach pid");
162 return error.Success();
165 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
166 lldb::SBError &error) {
167 LLDB_INSTRUMENT_VA(this, pid, error);
169 ProcessSP process_sp(GetSP());
170 if (process_sp) {
171 std::lock_guard<std::recursive_mutex> guard(
172 process_sp->GetTarget().GetAPIMutex());
173 if (process_sp->GetState() == eStateConnected) {
174 ProcessAttachInfo attach_info;
175 attach_info.SetProcessID(pid);
176 error.SetError(process_sp->Attach(attach_info));
177 } else {
178 error = Status::FromErrorString(
179 "must be in eStateConnected to call RemoteAttachToProcessWithID");
181 } else {
182 error = Status::FromErrorString("unable to attach pid");
185 return error.Success();
188 uint32_t SBProcess::GetNumThreads() {
189 LLDB_INSTRUMENT_VA(this);
191 uint32_t num_threads = 0;
192 ProcessSP process_sp(GetSP());
193 if (process_sp) {
194 Process::StopLocker stop_locker;
196 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
197 std::lock_guard<std::recursive_mutex> guard(
198 process_sp->GetTarget().GetAPIMutex());
199 num_threads = process_sp->GetThreadList().GetSize(can_update);
202 return num_threads;
205 SBThread SBProcess::GetSelectedThread() const {
206 LLDB_INSTRUMENT_VA(this);
208 SBThread sb_thread;
209 ThreadSP thread_sp;
210 ProcessSP process_sp(GetSP());
211 if (process_sp) {
212 std::lock_guard<std::recursive_mutex> guard(
213 process_sp->GetTarget().GetAPIMutex());
214 thread_sp = process_sp->GetThreadList().GetSelectedThread();
215 sb_thread.SetThread(thread_sp);
218 return sb_thread;
221 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
222 lldb::addr_t context) {
223 LLDB_INSTRUMENT_VA(this, tid, context);
225 SBThread sb_thread;
226 ThreadSP thread_sp;
227 ProcessSP process_sp(GetSP());
228 if (process_sp) {
229 std::lock_guard<std::recursive_mutex> guard(
230 process_sp->GetTarget().GetAPIMutex());
231 thread_sp = process_sp->CreateOSPluginThread(tid, context);
232 sb_thread.SetThread(thread_sp);
235 return sb_thread;
238 SBTarget SBProcess::GetTarget() const {
239 LLDB_INSTRUMENT_VA(this);
241 SBTarget sb_target;
242 TargetSP target_sp;
243 ProcessSP process_sp(GetSP());
244 if (process_sp) {
245 target_sp = process_sp->GetTarget().shared_from_this();
246 sb_target.SetSP(target_sp);
249 return sb_target;
252 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
253 LLDB_INSTRUMENT_VA(this, src, src_len);
255 size_t ret_val = 0;
256 ProcessSP process_sp(GetSP());
257 if (process_sp) {
258 Status error;
259 ret_val = process_sp->PutSTDIN(src, src_len, error);
262 return ret_val;
265 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
266 LLDB_INSTRUMENT_VA(this, dst, dst_len);
268 size_t bytes_read = 0;
269 ProcessSP process_sp(GetSP());
270 if (process_sp) {
271 Status error;
272 bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
275 return bytes_read;
278 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
279 LLDB_INSTRUMENT_VA(this, dst, dst_len);
281 size_t bytes_read = 0;
282 ProcessSP process_sp(GetSP());
283 if (process_sp) {
284 Status error;
285 bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
288 return bytes_read;
291 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
292 LLDB_INSTRUMENT_VA(this, dst, dst_len);
294 size_t bytes_read = 0;
295 ProcessSP process_sp(GetSP());
296 if (process_sp) {
297 Status error;
298 bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
301 return bytes_read;
304 void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const {
305 LLDB_INSTRUMENT_VA(this, event, out);
307 return ReportEventState(event, out.m_opaque_sp);
310 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
311 LLDB_INSTRUMENT_VA(this, event, out);
312 FileSP outfile = std::make_shared<NativeFile>(out, false);
313 return ReportEventState(event, outfile);
316 void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const {
318 LLDB_INSTRUMENT_VA(this, event, out);
320 if (!out || !out->IsValid())
321 return;
323 ProcessSP process_sp(GetSP());
324 if (process_sp) {
325 StreamFile stream(out);
326 const StateType event_state = SBProcess::GetStateFromEvent(event);
327 stream.Printf("Process %" PRIu64 " %s\n", process_sp->GetID(),
328 SBDebugger::StateAsCString(event_state));
332 void SBProcess::AppendEventStateReport(const SBEvent &event,
333 SBCommandReturnObject &result) {
334 LLDB_INSTRUMENT_VA(this, event, result);
336 ProcessSP process_sp(GetSP());
337 if (process_sp) {
338 const StateType event_state = SBProcess::GetStateFromEvent(event);
339 char message[1024];
340 ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
341 process_sp->GetID(), SBDebugger::StateAsCString(event_state));
343 result.AppendMessage(message);
347 bool SBProcess::SetSelectedThread(const SBThread &thread) {
348 LLDB_INSTRUMENT_VA(this, thread);
350 ProcessSP process_sp(GetSP());
351 if (process_sp) {
352 std::lock_guard<std::recursive_mutex> guard(
353 process_sp->GetTarget().GetAPIMutex());
354 return process_sp->GetThreadList().SetSelectedThreadByID(
355 thread.GetThreadID());
357 return false;
360 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
361 LLDB_INSTRUMENT_VA(this, tid);
363 bool ret_val = false;
364 ProcessSP process_sp(GetSP());
365 if (process_sp) {
366 std::lock_guard<std::recursive_mutex> guard(
367 process_sp->GetTarget().GetAPIMutex());
368 ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
371 return ret_val;
374 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
375 LLDB_INSTRUMENT_VA(this, index_id);
377 bool ret_val = false;
378 ProcessSP process_sp(GetSP());
379 if (process_sp) {
380 std::lock_guard<std::recursive_mutex> guard(
381 process_sp->GetTarget().GetAPIMutex());
382 ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
385 return ret_val;
388 SBThread SBProcess::GetThreadAtIndex(size_t index) {
389 LLDB_INSTRUMENT_VA(this, index);
391 SBThread sb_thread;
392 ThreadSP thread_sp;
393 ProcessSP process_sp(GetSP());
394 if (process_sp) {
395 Process::StopLocker stop_locker;
396 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
397 std::lock_guard<std::recursive_mutex> guard(
398 process_sp->GetTarget().GetAPIMutex());
399 thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
400 sb_thread.SetThread(thread_sp);
403 return sb_thread;
406 uint32_t SBProcess::GetNumQueues() {
407 LLDB_INSTRUMENT_VA(this);
409 uint32_t num_queues = 0;
410 ProcessSP process_sp(GetSP());
411 if (process_sp) {
412 Process::StopLocker stop_locker;
413 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
414 std::lock_guard<std::recursive_mutex> guard(
415 process_sp->GetTarget().GetAPIMutex());
416 num_queues = process_sp->GetQueueList().GetSize();
420 return num_queues;
423 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
424 LLDB_INSTRUMENT_VA(this, index);
426 SBQueue sb_queue;
427 QueueSP queue_sp;
428 ProcessSP process_sp(GetSP());
429 if (process_sp) {
430 Process::StopLocker stop_locker;
431 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
432 std::lock_guard<std::recursive_mutex> guard(
433 process_sp->GetTarget().GetAPIMutex());
434 queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
435 sb_queue.SetQueue(queue_sp);
439 return sb_queue;
442 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
443 LLDB_INSTRUMENT_VA(this, include_expression_stops);
445 ProcessSP process_sp(GetSP());
446 if (process_sp) {
447 std::lock_guard<std::recursive_mutex> guard(
448 process_sp->GetTarget().GetAPIMutex());
449 if (include_expression_stops)
450 return process_sp->GetStopID();
451 else
452 return process_sp->GetLastNaturalStopID();
454 return 0;
457 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
458 LLDB_INSTRUMENT_VA(this, stop_id);
460 SBEvent sb_event;
461 EventSP event_sp;
462 ProcessSP process_sp(GetSP());
463 if (process_sp) {
464 std::lock_guard<std::recursive_mutex> guard(
465 process_sp->GetTarget().GetAPIMutex());
466 event_sp = process_sp->GetStopEventForStopID(stop_id);
467 sb_event.reset(event_sp);
470 return sb_event;
473 void SBProcess::ForceScriptedState(StateType new_state) {
474 LLDB_INSTRUMENT_VA(this, new_state);
476 if (ProcessSP process_sp = GetSP()) {
477 std::lock_guard<std::recursive_mutex> guard(
478 process_sp->GetTarget().GetAPIMutex());
479 process_sp->ForceScriptedState(new_state);
483 StateType SBProcess::GetState() {
484 LLDB_INSTRUMENT_VA(this);
486 StateType ret_val = eStateInvalid;
487 ProcessSP process_sp(GetSP());
488 if (process_sp) {
489 std::lock_guard<std::recursive_mutex> guard(
490 process_sp->GetTarget().GetAPIMutex());
491 ret_val = process_sp->GetState();
494 return ret_val;
497 int SBProcess::GetExitStatus() {
498 LLDB_INSTRUMENT_VA(this);
500 int exit_status = 0;
501 ProcessSP process_sp(GetSP());
502 if (process_sp) {
503 std::lock_guard<std::recursive_mutex> guard(
504 process_sp->GetTarget().GetAPIMutex());
505 exit_status = process_sp->GetExitStatus();
508 return exit_status;
511 const char *SBProcess::GetExitDescription() {
512 LLDB_INSTRUMENT_VA(this);
514 ProcessSP process_sp(GetSP());
515 if (!process_sp)
516 return nullptr;
518 std::lock_guard<std::recursive_mutex> guard(
519 process_sp->GetTarget().GetAPIMutex());
520 return ConstString(process_sp->GetExitDescription()).GetCString();
523 lldb::pid_t SBProcess::GetProcessID() {
524 LLDB_INSTRUMENT_VA(this);
526 lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
527 ProcessSP process_sp(GetSP());
528 if (process_sp)
529 ret_val = process_sp->GetID();
531 return ret_val;
534 uint32_t SBProcess::GetUniqueID() {
535 LLDB_INSTRUMENT_VA(this);
537 uint32_t ret_val = 0;
538 ProcessSP process_sp(GetSP());
539 if (process_sp)
540 ret_val = process_sp->GetUniqueID();
541 return ret_val;
544 ByteOrder SBProcess::GetByteOrder() const {
545 LLDB_INSTRUMENT_VA(this);
547 ByteOrder byteOrder = eByteOrderInvalid;
548 ProcessSP process_sp(GetSP());
549 if (process_sp)
550 byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
552 return byteOrder;
555 uint32_t SBProcess::GetAddressByteSize() const {
556 LLDB_INSTRUMENT_VA(this);
558 uint32_t size = 0;
559 ProcessSP process_sp(GetSP());
560 if (process_sp)
561 size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
563 return size;
566 SBError SBProcess::Continue() {
567 LLDB_INSTRUMENT_VA(this);
569 SBError sb_error;
570 ProcessSP process_sp(GetSP());
572 if (process_sp) {
573 std::lock_guard<std::recursive_mutex> guard(
574 process_sp->GetTarget().GetAPIMutex());
576 if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
577 sb_error.ref() = process_sp->Resume();
578 else
579 sb_error.ref() = process_sp->ResumeSynchronous(nullptr);
580 } else
581 sb_error = Status::FromErrorString("SBProcess is invalid");
583 return sb_error;
586 SBError SBProcess::Destroy() {
587 LLDB_INSTRUMENT_VA(this);
589 SBError sb_error;
590 ProcessSP process_sp(GetSP());
591 if (process_sp) {
592 std::lock_guard<std::recursive_mutex> guard(
593 process_sp->GetTarget().GetAPIMutex());
594 sb_error.SetError(process_sp->Destroy(false));
595 } else
596 sb_error = Status::FromErrorString("SBProcess is invalid");
598 return sb_error;
601 SBError SBProcess::Stop() {
602 LLDB_INSTRUMENT_VA(this);
604 SBError sb_error;
605 ProcessSP process_sp(GetSP());
606 if (process_sp) {
607 std::lock_guard<std::recursive_mutex> guard(
608 process_sp->GetTarget().GetAPIMutex());
609 sb_error.SetError(process_sp->Halt());
610 } else
611 sb_error = Status::FromErrorString("SBProcess is invalid");
613 return sb_error;
616 SBError SBProcess::Kill() {
617 LLDB_INSTRUMENT_VA(this);
619 SBError sb_error;
620 ProcessSP process_sp(GetSP());
621 if (process_sp) {
622 std::lock_guard<std::recursive_mutex> guard(
623 process_sp->GetTarget().GetAPIMutex());
624 sb_error.SetError(process_sp->Destroy(true));
625 } else
626 sb_error = Status::FromErrorString("SBProcess is invalid");
628 return sb_error;
631 SBError SBProcess::Detach() {
632 LLDB_INSTRUMENT_VA(this);
634 // FIXME: This should come from a process default.
635 bool keep_stopped = false;
636 return Detach(keep_stopped);
639 SBError SBProcess::Detach(bool keep_stopped) {
640 LLDB_INSTRUMENT_VA(this, keep_stopped);
642 SBError sb_error;
643 ProcessSP process_sp(GetSP());
644 if (process_sp) {
645 std::lock_guard<std::recursive_mutex> guard(
646 process_sp->GetTarget().GetAPIMutex());
647 sb_error.SetError(process_sp->Detach(keep_stopped));
648 } else
649 sb_error = Status::FromErrorString("SBProcess is invalid");
651 return sb_error;
654 SBError SBProcess::Signal(int signo) {
655 LLDB_INSTRUMENT_VA(this, signo);
657 SBError sb_error;
658 ProcessSP process_sp(GetSP());
659 if (process_sp) {
660 std::lock_guard<std::recursive_mutex> guard(
661 process_sp->GetTarget().GetAPIMutex());
662 sb_error.SetError(process_sp->Signal(signo));
663 } else
664 sb_error = Status::FromErrorString("SBProcess is invalid");
666 return sb_error;
669 SBUnixSignals SBProcess::GetUnixSignals() {
670 LLDB_INSTRUMENT_VA(this);
672 if (auto process_sp = GetSP())
673 return SBUnixSignals{process_sp};
675 return SBUnixSignals{};
678 void SBProcess::SendAsyncInterrupt() {
679 LLDB_INSTRUMENT_VA(this);
681 ProcessSP process_sp(GetSP());
682 if (process_sp) {
683 process_sp->SendAsyncInterrupt();
687 SBThread SBProcess::GetThreadByID(tid_t tid) {
688 LLDB_INSTRUMENT_VA(this, tid);
690 SBThread sb_thread;
691 ThreadSP thread_sp;
692 ProcessSP process_sp(GetSP());
693 if (process_sp) {
694 Process::StopLocker stop_locker;
695 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
696 std::lock_guard<std::recursive_mutex> guard(
697 process_sp->GetTarget().GetAPIMutex());
698 thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
699 sb_thread.SetThread(thread_sp);
702 return sb_thread;
705 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
706 LLDB_INSTRUMENT_VA(this, index_id);
708 SBThread sb_thread;
709 ThreadSP thread_sp;
710 ProcessSP process_sp(GetSP());
711 if (process_sp) {
712 Process::StopLocker stop_locker;
713 const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
714 std::lock_guard<std::recursive_mutex> guard(
715 process_sp->GetTarget().GetAPIMutex());
716 thread_sp =
717 process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
718 sb_thread.SetThread(thread_sp);
721 return sb_thread;
724 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
725 LLDB_INSTRUMENT_VA(event);
727 StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
729 return ret_val;
732 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
733 LLDB_INSTRUMENT_VA(event);
735 bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
737 return ret_val;
740 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
741 LLDB_INSTRUMENT_VA(event);
743 return Process::ProcessEventData::GetNumRestartedReasons(event.get());
746 const char *
747 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
748 size_t idx) {
749 LLDB_INSTRUMENT_VA(event, idx);
751 return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex(
752 event.get(), idx))
753 .GetCString();
756 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
757 LLDB_INSTRUMENT_VA(event);
759 ProcessSP process_sp =
760 Process::ProcessEventData::GetProcessFromEvent(event.get());
761 if (!process_sp) {
762 // StructuredData events also know the process they come from. Try that.
763 process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
766 return SBProcess(process_sp);
769 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
770 LLDB_INSTRUMENT_VA(event);
772 return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
775 lldb::SBStructuredData
776 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
777 LLDB_INSTRUMENT_VA(event);
779 return SBStructuredData(event.GetSP());
782 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
783 LLDB_INSTRUMENT_VA(event);
785 return Process::ProcessEventData::GetEventDataFromEvent(event.get()) !=
786 nullptr;
789 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
790 LLDB_INSTRUMENT_VA(event);
792 EventSP event_sp = event.GetSP();
793 EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
794 return event_data && (event_data->GetFlavor() ==
795 EventDataStructuredData::GetFlavorString());
798 SBBroadcaster SBProcess::GetBroadcaster() const {
799 LLDB_INSTRUMENT_VA(this);
801 ProcessSP process_sp(GetSP());
803 SBBroadcaster broadcaster(process_sp.get(), false);
805 return broadcaster;
808 const char *SBProcess::GetBroadcasterClass() {
809 LLDB_INSTRUMENT();
811 return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
814 lldb::SBAddressRangeList SBProcess::FindRangesInMemory(
815 const void *buf, uint64_t size, const SBAddressRangeList &ranges,
816 uint32_t alignment, uint32_t max_matches, SBError &error) {
817 LLDB_INSTRUMENT_VA(this, buf, size, ranges, alignment, max_matches, error);
819 lldb::SBAddressRangeList matches;
821 ProcessSP process_sp(GetSP());
822 if (!process_sp) {
823 error = Status::FromErrorString("SBProcess is invalid");
824 return matches;
826 Process::StopLocker stop_locker;
827 if (!stop_locker.TryLock(&process_sp->GetRunLock())) {
828 error = Status::FromErrorString("process is running");
829 return matches;
831 std::lock_guard<std::recursive_mutex> guard(
832 process_sp->GetTarget().GetAPIMutex());
833 matches.m_opaque_up->ref() = process_sp->FindRangesInMemory(
834 reinterpret_cast<const uint8_t *>(buf), size, ranges.ref().ref(),
835 alignment, max_matches, error.ref());
836 return matches;
839 lldb::addr_t SBProcess::FindInMemory(const void *buf, uint64_t size,
840 const SBAddressRange &range,
841 uint32_t alignment, SBError &error) {
842 LLDB_INSTRUMENT_VA(this, buf, size, range, alignment, error);
844 ProcessSP process_sp(GetSP());
846 if (!process_sp) {
847 error = Status::FromErrorString("SBProcess is invalid");
848 return LLDB_INVALID_ADDRESS;
851 Process::StopLocker stop_locker;
852 if (!stop_locker.TryLock(&process_sp->GetRunLock())) {
853 error = Status::FromErrorString("process is running");
854 return LLDB_INVALID_ADDRESS;
857 std::lock_guard<std::recursive_mutex> guard(
858 process_sp->GetTarget().GetAPIMutex());
859 return process_sp->FindInMemory(reinterpret_cast<const uint8_t *>(buf), size,
860 range.ref(), alignment, error.ref());
863 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
864 SBError &sb_error) {
865 LLDB_INSTRUMENT_VA(this, addr, dst, dst_len, sb_error);
867 if (!dst) {
868 sb_error = Status::FromErrorStringWithFormat(
869 "no buffer provided to read %zu bytes into", dst_len);
870 return 0;
873 size_t bytes_read = 0;
874 ProcessSP process_sp(GetSP());
877 if (process_sp) {
878 Process::StopLocker stop_locker;
879 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
880 std::lock_guard<std::recursive_mutex> guard(
881 process_sp->GetTarget().GetAPIMutex());
882 bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
883 } else {
884 sb_error = Status::FromErrorString("process is running");
886 } else {
887 sb_error = Status::FromErrorString("SBProcess is invalid");
890 return bytes_read;
893 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
894 lldb::SBError &sb_error) {
895 LLDB_INSTRUMENT_VA(this, addr, buf, size, sb_error);
897 size_t bytes_read = 0;
898 ProcessSP process_sp(GetSP());
899 if (process_sp) {
900 Process::StopLocker stop_locker;
901 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
902 std::lock_guard<std::recursive_mutex> guard(
903 process_sp->GetTarget().GetAPIMutex());
904 bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
905 sb_error.ref());
906 } else {
907 sb_error = Status::FromErrorString("process is running");
909 } else {
910 sb_error = Status::FromErrorString("SBProcess is invalid");
912 return bytes_read;
915 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
916 lldb::SBError &sb_error) {
917 LLDB_INSTRUMENT_VA(this, addr, byte_size, sb_error);
919 uint64_t value = 0;
920 ProcessSP process_sp(GetSP());
921 if (process_sp) {
922 Process::StopLocker stop_locker;
923 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
924 std::lock_guard<std::recursive_mutex> guard(
925 process_sp->GetTarget().GetAPIMutex());
926 value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
927 sb_error.ref());
928 } else {
929 sb_error = Status::FromErrorString("process is running");
931 } else {
932 sb_error = Status::FromErrorString("SBProcess is invalid");
934 return value;
937 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
938 lldb::SBError &sb_error) {
939 LLDB_INSTRUMENT_VA(this, addr, sb_error);
941 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
942 ProcessSP process_sp(GetSP());
943 if (process_sp) {
944 Process::StopLocker stop_locker;
945 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
946 std::lock_guard<std::recursive_mutex> guard(
947 process_sp->GetTarget().GetAPIMutex());
948 ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
949 } else {
950 sb_error = Status::FromErrorString("process is running");
952 } else {
953 sb_error = Status::FromErrorString("SBProcess is invalid");
955 return ptr;
958 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
959 SBError &sb_error) {
960 LLDB_INSTRUMENT_VA(this, addr, src, src_len, sb_error);
962 size_t bytes_written = 0;
964 ProcessSP process_sp(GetSP());
966 if (process_sp) {
967 Process::StopLocker stop_locker;
968 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
969 std::lock_guard<std::recursive_mutex> guard(
970 process_sp->GetTarget().GetAPIMutex());
971 bytes_written =
972 process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
973 } else {
974 sb_error = Status::FromErrorString("process is running");
978 return bytes_written;
981 void SBProcess::GetStatus(SBStream &status) {
982 LLDB_INSTRUMENT_VA(this, status);
984 ProcessSP process_sp(GetSP());
985 if (process_sp)
986 process_sp->GetStatus(status.ref());
989 bool SBProcess::GetDescription(SBStream &description) {
990 LLDB_INSTRUMENT_VA(this, description);
992 Stream &strm = description.ref();
994 ProcessSP process_sp(GetSP());
995 if (process_sp) {
996 char path[PATH_MAX];
997 GetTarget().GetExecutable().GetPath(path, sizeof(path));
998 Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
999 const char *exe_name = nullptr;
1000 if (exe_module)
1001 exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1003 strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1004 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1005 GetNumThreads(), exe_name ? ", executable = " : "",
1006 exe_name ? exe_name : "");
1007 } else
1008 strm.PutCString("No value");
1010 return true;
1013 SBStructuredData SBProcess::GetExtendedCrashInformation() {
1014 LLDB_INSTRUMENT_VA(this);
1015 SBStructuredData data;
1016 ProcessSP process_sp(GetSP());
1017 if (!process_sp)
1018 return data;
1020 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1022 if (!platform_sp)
1023 return data;
1025 auto expected_data =
1026 platform_sp->FetchExtendedCrashInformation(*process_sp.get());
1028 if (!expected_data)
1029 return data;
1031 StructuredData::ObjectSP fetched_data = *expected_data;
1032 data.m_impl_up->SetObjectSP(fetched_data);
1033 return data;
1036 uint32_t
1037 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1038 LLDB_INSTRUMENT_VA(this, sb_error);
1040 uint32_t num = 0;
1041 ProcessSP process_sp(GetSP());
1042 if (process_sp) {
1043 std::lock_guard<std::recursive_mutex> guard(
1044 process_sp->GetTarget().GetAPIMutex());
1045 std::optional<uint32_t> actual_num = process_sp->GetWatchpointSlotCount();
1046 if (actual_num) {
1047 num = *actual_num;
1048 } else {
1049 sb_error =
1050 Status::FromErrorString("Unable to determine number of watchpoints");
1052 } else {
1053 sb_error = Status::FromErrorString("SBProcess is invalid");
1055 return num;
1058 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1059 lldb::SBError &sb_error) {
1060 LLDB_INSTRUMENT_VA(this, sb_remote_image_spec, sb_error);
1062 return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1065 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1066 const lldb::SBFileSpec &sb_remote_image_spec,
1067 lldb::SBError &sb_error) {
1068 LLDB_INSTRUMENT_VA(this, sb_local_image_spec, sb_remote_image_spec, sb_error);
1070 ProcessSP process_sp(GetSP());
1071 if (process_sp) {
1072 Process::StopLocker stop_locker;
1073 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1074 std::lock_guard<std::recursive_mutex> guard(
1075 process_sp->GetTarget().GetAPIMutex());
1076 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1077 return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1078 *sb_remote_image_spec, sb_error.ref());
1079 } else {
1080 sb_error = Status::FromErrorString("process is running");
1082 } else {
1083 sb_error = Status::FromErrorString("process is invalid");
1085 return LLDB_INVALID_IMAGE_TOKEN;
1088 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
1089 SBStringList &paths,
1090 lldb::SBFileSpec &loaded_path,
1091 lldb::SBError &error) {
1092 LLDB_INSTRUMENT_VA(this, image_spec, paths, loaded_path, error);
1094 ProcessSP process_sp(GetSP());
1095 if (process_sp) {
1096 Process::StopLocker stop_locker;
1097 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1098 std::lock_guard<std::recursive_mutex> guard(
1099 process_sp->GetTarget().GetAPIMutex());
1100 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1101 size_t num_paths = paths.GetSize();
1102 std::vector<std::string> paths_vec;
1103 paths_vec.reserve(num_paths);
1104 for (size_t i = 0; i < num_paths; i++)
1105 paths_vec.push_back(paths.GetStringAtIndex(i));
1106 FileSpec loaded_spec;
1108 uint32_t token = platform_sp->LoadImageUsingPaths(
1109 process_sp.get(), *image_spec, paths_vec, error.ref(), &loaded_spec);
1110 if (token != LLDB_INVALID_IMAGE_TOKEN)
1111 loaded_path = loaded_spec;
1112 return token;
1113 } else {
1114 error = Status::FromErrorString("process is running");
1116 } else {
1117 error = Status::FromErrorString("process is invalid");
1120 return LLDB_INVALID_IMAGE_TOKEN;
1123 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1124 LLDB_INSTRUMENT_VA(this, image_token);
1126 lldb::SBError sb_error;
1127 ProcessSP process_sp(GetSP());
1128 if (process_sp) {
1129 Process::StopLocker stop_locker;
1130 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1131 std::lock_guard<std::recursive_mutex> guard(
1132 process_sp->GetTarget().GetAPIMutex());
1133 PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1134 sb_error.SetError(
1135 platform_sp->UnloadImage(process_sp.get(), image_token));
1136 } else {
1137 sb_error = Status::FromErrorString("process is running");
1139 } else
1140 sb_error = Status::FromErrorString("invalid process");
1141 return sb_error;
1144 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1145 LLDB_INSTRUMENT_VA(this, event_data);
1147 lldb::SBError sb_error;
1148 ProcessSP process_sp(GetSP());
1149 if (process_sp) {
1150 Process::StopLocker stop_locker;
1151 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1152 std::lock_guard<std::recursive_mutex> guard(
1153 process_sp->GetTarget().GetAPIMutex());
1154 sb_error.SetError(process_sp->SendEventData(event_data));
1155 } else {
1156 sb_error = Status::FromErrorString("process is running");
1158 } else
1159 sb_error = Status::FromErrorString("invalid process");
1160 return sb_error;
1163 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1164 LLDB_INSTRUMENT_VA(this);
1166 ProcessSP process_sp(GetSP());
1167 if (process_sp && process_sp->GetSystemRuntime()) {
1168 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1169 return runtime->GetExtendedBacktraceTypes().size();
1171 return 0;
1174 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1175 LLDB_INSTRUMENT_VA(this, idx);
1177 ProcessSP process_sp(GetSP());
1178 if (process_sp && process_sp->GetSystemRuntime()) {
1179 SystemRuntime *runtime = process_sp->GetSystemRuntime();
1180 const std::vector<ConstString> &names =
1181 runtime->GetExtendedBacktraceTypes();
1182 if (idx < names.size()) {
1183 return names[idx].AsCString();
1186 return nullptr;
1189 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1190 LLDB_INSTRUMENT_VA(this, addr);
1192 ProcessSP process_sp(GetSP());
1193 SBThreadCollection threads;
1194 if (process_sp) {
1195 threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1197 return threads;
1200 bool SBProcess::IsInstrumentationRuntimePresent(
1201 InstrumentationRuntimeType type) {
1202 LLDB_INSTRUMENT_VA(this, type);
1204 ProcessSP process_sp(GetSP());
1205 if (!process_sp)
1206 return false;
1208 std::lock_guard<std::recursive_mutex> guard(
1209 process_sp->GetTarget().GetAPIMutex());
1211 InstrumentationRuntimeSP runtime_sp =
1212 process_sp->GetInstrumentationRuntime(type);
1214 if (!runtime_sp.get())
1215 return false;
1217 return runtime_sp->IsActive();
1220 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1221 LLDB_INSTRUMENT_VA(this, file_name);
1222 SBSaveCoreOptions options;
1223 options.SetOutputFile(SBFileSpec(file_name));
1224 options.SetStyle(SaveCoreStyle::eSaveCoreFull);
1225 return SaveCore(options);
1228 lldb::SBError SBProcess::SaveCore(const char *file_name,
1229 const char *flavor,
1230 SaveCoreStyle core_style) {
1231 LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style);
1232 SBSaveCoreOptions options;
1233 options.SetOutputFile(SBFileSpec(file_name));
1234 options.SetStyle(core_style);
1235 SBError error = options.SetPluginName(flavor);
1236 if (error.Fail())
1237 return error;
1238 return SaveCore(options);
1241 lldb::SBError SBProcess::SaveCore(SBSaveCoreOptions &options) {
1243 LLDB_INSTRUMENT_VA(this, options);
1245 lldb::SBError error;
1246 ProcessSP process_sp(GetSP());
1247 if (!process_sp) {
1248 error = Status::FromErrorString("SBProcess is invalid");
1249 return error;
1252 std::lock_guard<std::recursive_mutex> guard(
1253 process_sp->GetTarget().GetAPIMutex());
1255 if (process_sp->GetState() != eStateStopped) {
1256 error = Status::FromErrorString("the process is not stopped");
1257 return error;
1260 error.ref() = PluginManager::SaveCore(process_sp, options.ref());
1262 return error;
1265 lldb::SBError
1266 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1267 SBMemoryRegionInfo &sb_region_info) {
1268 LLDB_INSTRUMENT_VA(this, load_addr, sb_region_info);
1270 lldb::SBError sb_error;
1271 ProcessSP process_sp(GetSP());
1272 if (process_sp) {
1273 Process::StopLocker stop_locker;
1274 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1275 std::lock_guard<std::recursive_mutex> guard(
1276 process_sp->GetTarget().GetAPIMutex());
1278 sb_error.ref() =
1279 process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref());
1280 } else {
1281 sb_error = Status::FromErrorString("process is running");
1283 } else {
1284 sb_error = Status::FromErrorString("SBProcess is invalid");
1286 return sb_error;
1289 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1290 LLDB_INSTRUMENT_VA(this);
1292 lldb::SBMemoryRegionInfoList sb_region_list;
1294 ProcessSP process_sp(GetSP());
1295 Process::StopLocker stop_locker;
1296 if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1297 std::lock_guard<std::recursive_mutex> guard(
1298 process_sp->GetTarget().GetAPIMutex());
1300 process_sp->GetMemoryRegions(sb_region_list.ref());
1303 return sb_region_list;
1306 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1307 LLDB_INSTRUMENT_VA(this);
1309 lldb::SBProcessInfo sb_proc_info;
1310 ProcessSP process_sp(GetSP());
1311 ProcessInstanceInfo proc_info;
1312 if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1313 sb_proc_info.SetProcessInfo(proc_info);
1315 return sb_proc_info;
1318 lldb::SBFileSpec SBProcess::GetCoreFile() {
1319 LLDB_INSTRUMENT_VA(this);
1321 ProcessSP process_sp(GetSP());
1322 FileSpec core_file;
1323 if (process_sp) {
1324 core_file = process_sp->GetCoreFile();
1326 return SBFileSpec(core_file);
1329 addr_t SBProcess::GetAddressMask(AddressMaskType type,
1330 AddressMaskRange addr_range) {
1331 LLDB_INSTRUMENT_VA(this, type, addr_range);
1333 if (ProcessSP process_sp = GetSP()) {
1334 switch (type) {
1335 case eAddressMaskTypeCode:
1336 if (addr_range == eAddressMaskRangeHigh)
1337 return process_sp->GetHighmemCodeAddressMask();
1338 else
1339 return process_sp->GetCodeAddressMask();
1340 case eAddressMaskTypeData:
1341 if (addr_range == eAddressMaskRangeHigh)
1342 return process_sp->GetHighmemDataAddressMask();
1343 else
1344 return process_sp->GetDataAddressMask();
1345 case eAddressMaskTypeAny:
1346 if (addr_range == eAddressMaskRangeHigh)
1347 return process_sp->GetHighmemDataAddressMask();
1348 else
1349 return process_sp->GetDataAddressMask();
1352 return LLDB_INVALID_ADDRESS_MASK;
1355 void SBProcess::SetAddressMask(AddressMaskType type, addr_t mask,
1356 AddressMaskRange addr_range) {
1357 LLDB_INSTRUMENT_VA(this, type, mask, addr_range);
1359 if (ProcessSP process_sp = GetSP()) {
1360 switch (type) {
1361 case eAddressMaskTypeCode:
1362 if (addr_range == eAddressMaskRangeAll) {
1363 process_sp->SetCodeAddressMask(mask);
1364 process_sp->SetHighmemCodeAddressMask(mask);
1365 } else if (addr_range == eAddressMaskRangeHigh) {
1366 process_sp->SetHighmemCodeAddressMask(mask);
1367 } else {
1368 process_sp->SetCodeAddressMask(mask);
1370 break;
1371 case eAddressMaskTypeData:
1372 if (addr_range == eAddressMaskRangeAll) {
1373 process_sp->SetDataAddressMask(mask);
1374 process_sp->SetHighmemDataAddressMask(mask);
1375 } else if (addr_range == eAddressMaskRangeHigh) {
1376 process_sp->SetHighmemDataAddressMask(mask);
1377 } else {
1378 process_sp->SetDataAddressMask(mask);
1380 break;
1381 case eAddressMaskTypeAll:
1382 if (addr_range == eAddressMaskRangeAll) {
1383 process_sp->SetCodeAddressMask(mask);
1384 process_sp->SetDataAddressMask(mask);
1385 process_sp->SetHighmemCodeAddressMask(mask);
1386 process_sp->SetHighmemDataAddressMask(mask);
1387 } else if (addr_range == eAddressMaskRangeHigh) {
1388 process_sp->SetHighmemCodeAddressMask(mask);
1389 process_sp->SetHighmemDataAddressMask(mask);
1390 } else {
1391 process_sp->SetCodeAddressMask(mask);
1392 process_sp->SetDataAddressMask(mask);
1394 break;
1399 void SBProcess::SetAddressableBits(AddressMaskType type, uint32_t num_bits,
1400 AddressMaskRange addr_range) {
1401 LLDB_INSTRUMENT_VA(this, type, num_bits, addr_range);
1403 SetAddressMask(type, AddressableBits::AddressableBitToMask(num_bits),
1404 addr_range);
1407 addr_t SBProcess::FixAddress(addr_t addr, AddressMaskType type) {
1408 LLDB_INSTRUMENT_VA(this, addr, type);
1410 if (ProcessSP process_sp = GetSP()) {
1411 if (type == eAddressMaskTypeAny)
1412 return process_sp->FixAnyAddress(addr);
1413 else if (type == eAddressMaskTypeData)
1414 return process_sp->FixDataAddress(addr);
1415 else if (type == eAddressMaskTypeCode)
1416 return process_sp->FixCodeAddress(addr);
1418 return addr;
1421 lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions,
1422 lldb::SBError &sb_error) {
1423 LLDB_INSTRUMENT_VA(this, size, permissions, sb_error);
1425 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
1426 ProcessSP process_sp(GetSP());
1427 if (process_sp) {
1428 Process::StopLocker stop_locker;
1429 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1430 std::lock_guard<std::recursive_mutex> guard(
1431 process_sp->GetTarget().GetAPIMutex());
1432 addr = process_sp->AllocateMemory(size, permissions, sb_error.ref());
1433 } else {
1434 sb_error = Status::FromErrorString("process is running");
1436 } else {
1437 sb_error = Status::FromErrorString("SBProcess is invalid");
1439 return addr;
1442 lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) {
1443 LLDB_INSTRUMENT_VA(this, ptr);
1445 lldb::SBError sb_error;
1446 ProcessSP process_sp(GetSP());
1447 if (process_sp) {
1448 Process::StopLocker stop_locker;
1449 if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1450 std::lock_guard<std::recursive_mutex> guard(
1451 process_sp->GetTarget().GetAPIMutex());
1452 Status error = process_sp->DeallocateMemory(ptr);
1453 sb_error.SetError(std::move(error));
1454 } else {
1455 sb_error = Status::FromErrorString("process is running");
1457 } else {
1458 sb_error = Status::FromErrorString("SBProcess is invalid");
1460 return sb_error;
1463 lldb::SBScriptObject SBProcess::GetScriptedImplementation() {
1464 LLDB_INSTRUMENT_VA(this);
1465 ProcessSP process_sp(GetSP());
1466 return lldb::SBScriptObject((process_sp) ? process_sp->GetImplementation()
1467 : nullptr,
1468 eScriptLanguageDefault);