xref: /llvm-project/lldb/source/API/SBProcess.cpp (revision 22561cfb443267905d4190f0e2a738e6b412457f)
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 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBProcess.h"
10 #include "lldb/Utility/Instrumentation.h"
11 
12 #include <cinttypes>
13 
14 #include "lldb/lldb-defines.h"
15 #include "lldb/lldb-types.h"
16 
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"
34 
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"
52 
53 using namespace lldb;
54 using namespace lldb_private;
55 
56 SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }
57 
58 // SBProcess constructor
59 
60 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
61   LLDB_INSTRUMENT_VA(this, rhs);
62 }
63 
64 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
65     : m_opaque_wp(process_sp) {
66   LLDB_INSTRUMENT_VA(this, process_sp);
67 }
68 
69 const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
70   LLDB_INSTRUMENT_VA(this, rhs);
71 
72   if (this != &rhs)
73     m_opaque_wp = rhs.m_opaque_wp;
74   return *this;
75 }
76 
77 // Destructor
78 SBProcess::~SBProcess() = default;
79 
80 const char *SBProcess::GetBroadcasterClassName() {
81   LLDB_INSTRUMENT();
82 
83   return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
84 }
85 
86 const char *SBProcess::GetPluginName() {
87   LLDB_INSTRUMENT_VA(this);
88 
89   ProcessSP process_sp(GetSP());
90   if (process_sp) {
91     return ConstString(process_sp->GetPluginName()).GetCString();
92   }
93   return "<Unknown>";
94 }
95 
96 const char *SBProcess::GetShortPluginName() {
97   LLDB_INSTRUMENT_VA(this);
98 
99   ProcessSP process_sp(GetSP());
100   if (process_sp) {
101     return ConstString(process_sp->GetPluginName()).GetCString();
102   }
103   return "<Unknown>";
104 }
105 
106 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
107 
108 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
109 
110 void SBProcess::Clear() {
111   LLDB_INSTRUMENT_VA(this);
112 
113   m_opaque_wp.reset();
114 }
115 
116 bool SBProcess::IsValid() const {
117   LLDB_INSTRUMENT_VA(this);
118   return this->operator bool();
119 }
120 SBProcess::operator bool() const {
121   LLDB_INSTRUMENT_VA(this);
122 
123   ProcessSP process_sp(m_opaque_wp.lock());
124   return ((bool)process_sp && process_sp->IsValid());
125 }
126 
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);
135 
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");
157     }
158   } else {
159     error = Status::FromErrorString("unable to attach pid");
160   }
161 
162   return error.Success();
163 }
164 
165 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
166                                             lldb::SBError &error) {
167   LLDB_INSTRUMENT_VA(this, pid, error);
168 
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");
180     }
181   } else {
182     error = Status::FromErrorString("unable to attach pid");
183   }
184 
185   return error.Success();
186 }
187 
188 uint32_t SBProcess::GetNumThreads() {
189   LLDB_INSTRUMENT_VA(this);
190 
191   uint32_t num_threads = 0;
192   ProcessSP process_sp(GetSP());
193   if (process_sp) {
194     Process::StopLocker stop_locker;
195 
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);
200   }
201 
202   return num_threads;
203 }
204 
205 SBThread SBProcess::GetSelectedThread() const {
206   LLDB_INSTRUMENT_VA(this);
207 
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);
216   }
217 
218   return sb_thread;
219 }
220 
221 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
222                                          lldb::addr_t context) {
223   LLDB_INSTRUMENT_VA(this, tid, context);
224 
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);
233   }
234 
235   return sb_thread;
236 }
237 
238 SBTarget SBProcess::GetTarget() const {
239   LLDB_INSTRUMENT_VA(this);
240 
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);
247   }
248 
249   return sb_target;
250 }
251 
252 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
253   LLDB_INSTRUMENT_VA(this, src, src_len);
254 
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);
260   }
261 
262   return ret_val;
263 }
264 
265 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
266   LLDB_INSTRUMENT_VA(this, dst, dst_len);
267 
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);
273   }
274 
275   return bytes_read;
276 }
277 
278 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
279   LLDB_INSTRUMENT_VA(this, dst, dst_len);
280 
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);
286   }
287 
288   return bytes_read;
289 }
290 
291 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
292   LLDB_INSTRUMENT_VA(this, dst, dst_len);
293 
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);
299   }
300 
301   return bytes_read;
302 }
303 
304 void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const {
305   LLDB_INSTRUMENT_VA(this, event, out);
306 
307   return ReportEventState(event, out.m_opaque_sp);
308 }
309 
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);
314 }
315 
316 void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const {
317 
318   LLDB_INSTRUMENT_VA(this, event, out);
319 
320   if (!out || !out->IsValid())
321     return;
322 
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));
329   }
330 }
331 
332 void SBProcess::AppendEventStateReport(const SBEvent &event,
333                                        SBCommandReturnObject &result) {
334   LLDB_INSTRUMENT_VA(this, event, result);
335 
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));
342 
343     result.AppendMessage(message);
344   }
345 }
346 
347 bool SBProcess::SetSelectedThread(const SBThread &thread) {
348   LLDB_INSTRUMENT_VA(this, thread);
349 
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());
356   }
357   return false;
358 }
359 
360 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
361   LLDB_INSTRUMENT_VA(this, tid);
362 
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);
369   }
370 
371   return ret_val;
372 }
373 
374 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
375   LLDB_INSTRUMENT_VA(this, index_id);
376 
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);
383   }
384 
385   return ret_val;
386 }
387 
388 SBThread SBProcess::GetThreadAtIndex(size_t index) {
389   LLDB_INSTRUMENT_VA(this, index);
390 
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);
401   }
402 
403   return sb_thread;
404 }
405 
406 uint32_t SBProcess::GetNumQueues() {
407   LLDB_INSTRUMENT_VA(this);
408 
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();
417     }
418   }
419 
420   return num_queues;
421 }
422 
423 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
424   LLDB_INSTRUMENT_VA(this, index);
425 
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);
436     }
437   }
438 
439   return sb_queue;
440 }
441 
442 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
443   LLDB_INSTRUMENT_VA(this, include_expression_stops);
444 
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();
453   }
454   return 0;
455 }
456 
457 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
458   LLDB_INSTRUMENT_VA(this, stop_id);
459 
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);
468   }
469 
470   return sb_event;
471 }
472 
473 void SBProcess::ForceScriptedState(StateType new_state) {
474   LLDB_INSTRUMENT_VA(this, new_state);
475 
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);
480   }
481 }
482 
483 StateType SBProcess::GetState() {
484   LLDB_INSTRUMENT_VA(this);
485 
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();
492   }
493 
494   return ret_val;
495 }
496 
497 int SBProcess::GetExitStatus() {
498   LLDB_INSTRUMENT_VA(this);
499 
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();
506   }
507 
508   return exit_status;
509 }
510 
511 const char *SBProcess::GetExitDescription() {
512   LLDB_INSTRUMENT_VA(this);
513 
514   ProcessSP process_sp(GetSP());
515   if (!process_sp)
516     return nullptr;
517 
518   std::lock_guard<std::recursive_mutex> guard(
519       process_sp->GetTarget().GetAPIMutex());
520   return ConstString(process_sp->GetExitDescription()).GetCString();
521 }
522 
523 lldb::pid_t SBProcess::GetProcessID() {
524   LLDB_INSTRUMENT_VA(this);
525 
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();
530 
531   return ret_val;
532 }
533 
534 uint32_t SBProcess::GetUniqueID() {
535   LLDB_INSTRUMENT_VA(this);
536 
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;
542 }
543 
544 ByteOrder SBProcess::GetByteOrder() const {
545   LLDB_INSTRUMENT_VA(this);
546 
547   ByteOrder byteOrder = eByteOrderInvalid;
548   ProcessSP process_sp(GetSP());
549   if (process_sp)
550     byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
551 
552   return byteOrder;
553 }
554 
555 uint32_t SBProcess::GetAddressByteSize() const {
556   LLDB_INSTRUMENT_VA(this);
557 
558   uint32_t size = 0;
559   ProcessSP process_sp(GetSP());
560   if (process_sp)
561     size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
562 
563   return size;
564 }
565 
566 SBError SBProcess::Continue() {
567   LLDB_INSTRUMENT_VA(this);
568 
569   SBError sb_error;
570   ProcessSP process_sp(GetSP());
571 
572   if (process_sp) {
573     std::lock_guard<std::recursive_mutex> guard(
574         process_sp->GetTarget().GetAPIMutex());
575 
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");
582 
583   return sb_error;
584 }
585 
586 SBError SBProcess::Destroy() {
587   LLDB_INSTRUMENT_VA(this);
588 
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");
597 
598   return sb_error;
599 }
600 
601 SBError SBProcess::Stop() {
602   LLDB_INSTRUMENT_VA(this);
603 
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");
612 
613   return sb_error;
614 }
615 
616 SBError SBProcess::Kill() {
617   LLDB_INSTRUMENT_VA(this);
618 
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");
627 
628   return sb_error;
629 }
630 
631 SBError SBProcess::Detach() {
632   LLDB_INSTRUMENT_VA(this);
633 
634   // FIXME: This should come from a process default.
635   bool keep_stopped = false;
636   return Detach(keep_stopped);
637 }
638 
639 SBError SBProcess::Detach(bool keep_stopped) {
640   LLDB_INSTRUMENT_VA(this, keep_stopped);
641 
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");
650 
651   return sb_error;
652 }
653 
654 SBError SBProcess::Signal(int signo) {
655   LLDB_INSTRUMENT_VA(this, signo);
656 
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");
665 
666   return sb_error;
667 }
668 
669 SBUnixSignals SBProcess::GetUnixSignals() {
670   LLDB_INSTRUMENT_VA(this);
671 
672   if (auto process_sp = GetSP())
673     return SBUnixSignals{process_sp};
674 
675   return SBUnixSignals{};
676 }
677 
678 void SBProcess::SendAsyncInterrupt() {
679   LLDB_INSTRUMENT_VA(this);
680 
681   ProcessSP process_sp(GetSP());
682   if (process_sp) {
683     process_sp->SendAsyncInterrupt();
684   }
685 }
686 
687 SBThread SBProcess::GetThreadByID(tid_t tid) {
688   LLDB_INSTRUMENT_VA(this, tid);
689 
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);
700   }
701 
702   return sb_thread;
703 }
704 
705 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
706   LLDB_INSTRUMENT_VA(this, index_id);
707 
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);
719   }
720 
721   return sb_thread;
722 }
723 
724 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
725   LLDB_INSTRUMENT_VA(event);
726 
727   StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
728 
729   return ret_val;
730 }
731 
732 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
733   LLDB_INSTRUMENT_VA(event);
734 
735   bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
736 
737   return ret_val;
738 }
739 
740 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
741   LLDB_INSTRUMENT_VA(event);
742 
743   return Process::ProcessEventData::GetNumRestartedReasons(event.get());
744 }
745 
746 const char *
747 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
748                                               size_t idx) {
749   LLDB_INSTRUMENT_VA(event, idx);
750 
751   return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex(
752                          event.get(), idx))
753       .GetCString();
754 }
755 
756 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
757   LLDB_INSTRUMENT_VA(event);
758 
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());
764   }
765 
766   return SBProcess(process_sp);
767 }
768 
769 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
770   LLDB_INSTRUMENT_VA(event);
771 
772   return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
773 }
774 
775 lldb::SBStructuredData
776 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
777   LLDB_INSTRUMENT_VA(event);
778 
779   return SBStructuredData(event.GetSP());
780 }
781 
782 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
783   LLDB_INSTRUMENT_VA(event);
784 
785   return Process::ProcessEventData::GetEventDataFromEvent(event.get()) !=
786          nullptr;
787 }
788 
789 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
790   LLDB_INSTRUMENT_VA(event);
791 
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());
796 }
797 
798 SBBroadcaster SBProcess::GetBroadcaster() const {
799   LLDB_INSTRUMENT_VA(this);
800 
801   ProcessSP process_sp(GetSP());
802 
803   SBBroadcaster broadcaster(process_sp.get(), false);
804 
805   return broadcaster;
806 }
807 
808 const char *SBProcess::GetBroadcasterClass() {
809   LLDB_INSTRUMENT();
810 
811   return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
812 }
813 
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);
818 
819   lldb::SBAddressRangeList matches;
820 
821   ProcessSP process_sp(GetSP());
822   if (!process_sp) {
823     error = Status::FromErrorString("SBProcess is invalid");
824     return matches;
825   }
826   Process::StopLocker stop_locker;
827   if (!stop_locker.TryLock(&process_sp->GetRunLock())) {
828     error = Status::FromErrorString("process is running");
829     return matches;
830   }
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;
837 }
838 
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);
843 
844   ProcessSP process_sp(GetSP());
845 
846   if (!process_sp) {
847     error = Status::FromErrorString("SBProcess is invalid");
848     return LLDB_INVALID_ADDRESS;
849   }
850 
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;
855   }
856 
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());
861 }
862 
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);
866 
867   if (!dst) {
868     sb_error = Status::FromErrorStringWithFormat(
869         "no buffer provided to read %zu bytes into", dst_len);
870     return 0;
871   }
872 
873   size_t bytes_read = 0;
874   ProcessSP process_sp(GetSP());
875 
876 
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");
885     }
886   } else {
887     sb_error = Status::FromErrorString("SBProcess is invalid");
888   }
889 
890   return bytes_read;
891 }
892 
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);
896 
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");
908     }
909   } else {
910     sb_error = Status::FromErrorString("SBProcess is invalid");
911   }
912   return bytes_read;
913 }
914 
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);
918 
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");
930     }
931   } else {
932     sb_error = Status::FromErrorString("SBProcess is invalid");
933   }
934   return value;
935 }
936 
937 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
938                                               lldb::SBError &sb_error) {
939   LLDB_INSTRUMENT_VA(this, addr, sb_error);
940 
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");
951     }
952   } else {
953     sb_error = Status::FromErrorString("SBProcess is invalid");
954   }
955   return ptr;
956 }
957 
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);
961 
962   size_t bytes_written = 0;
963 
964   ProcessSP process_sp(GetSP());
965 
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");
975     }
976   }
977 
978   return bytes_written;
979 }
980 
981 void SBProcess::GetStatus(SBStream &status) {
982   LLDB_INSTRUMENT_VA(this, status);
983 
984   ProcessSP process_sp(GetSP());
985   if (process_sp)
986     process_sp->GetStatus(status.ref());
987 }
988 
989 bool SBProcess::GetDescription(SBStream &description) {
990   LLDB_INSTRUMENT_VA(this, description);
991 
992   Stream &strm = description.ref();
993 
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();
1002 
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");
1009 
1010   return true;
1011 }
1012 
1013 SBStructuredData SBProcess::GetExtendedCrashInformation() {
1014   LLDB_INSTRUMENT_VA(this);
1015   SBStructuredData data;
1016   ProcessSP process_sp(GetSP());
1017   if (!process_sp)
1018     return data;
1019 
1020   PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1021 
1022   if (!platform_sp)
1023     return data;
1024 
1025   auto expected_data =
1026       platform_sp->FetchExtendedCrashInformation(*process_sp.get());
1027 
1028   if (!expected_data)
1029     return data;
1030 
1031   StructuredData::ObjectSP fetched_data = *expected_data;
1032   data.m_impl_up->SetObjectSP(fetched_data);
1033   return data;
1034 }
1035 
1036 uint32_t
1037 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1038   LLDB_INSTRUMENT_VA(this, sb_error);
1039 
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");
1051     }
1052   } else {
1053     sb_error = Status::FromErrorString("SBProcess is invalid");
1054   }
1055   return num;
1056 }
1057 
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);
1061 
1062   return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1063 }
1064 
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);
1069 
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");
1081     }
1082   } else {
1083     sb_error = Status::FromErrorString("process is invalid");
1084   }
1085   return LLDB_INVALID_IMAGE_TOKEN;
1086 }
1087 
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);
1093 
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;
1107 
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");
1115     }
1116   } else {
1117     error = Status::FromErrorString("process is invalid");
1118   }
1119 
1120   return LLDB_INVALID_IMAGE_TOKEN;
1121 }
1122 
1123 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1124   LLDB_INSTRUMENT_VA(this, image_token);
1125 
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");
1138     }
1139   } else
1140     sb_error = Status::FromErrorString("invalid process");
1141   return sb_error;
1142 }
1143 
1144 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1145   LLDB_INSTRUMENT_VA(this, event_data);
1146 
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");
1157     }
1158   } else
1159     sb_error = Status::FromErrorString("invalid process");
1160   return sb_error;
1161 }
1162 
1163 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1164   LLDB_INSTRUMENT_VA(this);
1165 
1166   ProcessSP process_sp(GetSP());
1167   if (process_sp && process_sp->GetSystemRuntime()) {
1168     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1169     return runtime->GetExtendedBacktraceTypes().size();
1170   }
1171   return 0;
1172 }
1173 
1174 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1175   LLDB_INSTRUMENT_VA(this, idx);
1176 
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();
1184     }
1185   }
1186   return nullptr;
1187 }
1188 
1189 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1190   LLDB_INSTRUMENT_VA(this, addr);
1191 
1192   ProcessSP process_sp(GetSP());
1193   SBThreadCollection threads;
1194   if (process_sp) {
1195     threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1196   }
1197   return threads;
1198 }
1199 
1200 bool SBProcess::IsInstrumentationRuntimePresent(
1201     InstrumentationRuntimeType type) {
1202   LLDB_INSTRUMENT_VA(this, type);
1203 
1204   ProcessSP process_sp(GetSP());
1205   if (!process_sp)
1206     return false;
1207 
1208   std::lock_guard<std::recursive_mutex> guard(
1209       process_sp->GetTarget().GetAPIMutex());
1210 
1211   InstrumentationRuntimeSP runtime_sp =
1212       process_sp->GetInstrumentationRuntime(type);
1213 
1214   if (!runtime_sp.get())
1215     return false;
1216 
1217   return runtime_sp->IsActive();
1218 }
1219 
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);
1226 }
1227 
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);
1239 }
1240 
1241 lldb::SBError SBProcess::SaveCore(SBSaveCoreOptions &options) {
1242 
1243   LLDB_INSTRUMENT_VA(this, options);
1244 
1245   lldb::SBError error;
1246   ProcessSP process_sp(GetSP());
1247   if (!process_sp) {
1248     error = Status::FromErrorString("SBProcess is invalid");
1249     return error;
1250   }
1251 
1252   std::lock_guard<std::recursive_mutex> guard(
1253       process_sp->GetTarget().GetAPIMutex());
1254 
1255   if (process_sp->GetState() != eStateStopped) {
1256     error = Status::FromErrorString("the process is not stopped");
1257     return error;
1258   }
1259 
1260   error.ref() = PluginManager::SaveCore(process_sp, options.ref());
1261 
1262   return error;
1263 }
1264 
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);
1269 
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());
1277 
1278       sb_error.ref() =
1279           process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref());
1280     } else {
1281       sb_error = Status::FromErrorString("process is running");
1282     }
1283   } else {
1284     sb_error = Status::FromErrorString("SBProcess is invalid");
1285   }
1286   return sb_error;
1287 }
1288 
1289 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1290   LLDB_INSTRUMENT_VA(this);
1291 
1292   lldb::SBMemoryRegionInfoList sb_region_list;
1293 
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());
1299 
1300     process_sp->GetMemoryRegions(sb_region_list.ref());
1301   }
1302 
1303   return sb_region_list;
1304 }
1305 
1306 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1307   LLDB_INSTRUMENT_VA(this);
1308 
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);
1314   }
1315   return sb_proc_info;
1316 }
1317 
1318 lldb::SBFileSpec SBProcess::GetCoreFile() {
1319   LLDB_INSTRUMENT_VA(this);
1320 
1321   ProcessSP process_sp(GetSP());
1322   FileSpec core_file;
1323   if (process_sp) {
1324     core_file = process_sp->GetCoreFile();
1325   }
1326   return SBFileSpec(core_file);
1327 }
1328 
1329 addr_t SBProcess::GetAddressMask(AddressMaskType type,
1330                                  AddressMaskRange addr_range) {
1331   LLDB_INSTRUMENT_VA(this, type, addr_range);
1332 
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();
1350     }
1351   }
1352   return LLDB_INVALID_ADDRESS_MASK;
1353 }
1354 
1355 void SBProcess::SetAddressMask(AddressMaskType type, addr_t mask,
1356                                AddressMaskRange addr_range) {
1357   LLDB_INSTRUMENT_VA(this, type, mask, addr_range);
1358 
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);
1369       }
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);
1379       }
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);
1393       }
1394       break;
1395     }
1396   }
1397 }
1398 
1399 void SBProcess::SetAddressableBits(AddressMaskType type, uint32_t num_bits,
1400                                    AddressMaskRange addr_range) {
1401   LLDB_INSTRUMENT_VA(this, type, num_bits, addr_range);
1402 
1403   SetAddressMask(type, AddressableBits::AddressableBitToMask(num_bits),
1404                  addr_range);
1405 }
1406 
1407 addr_t SBProcess::FixAddress(addr_t addr, AddressMaskType type) {
1408   LLDB_INSTRUMENT_VA(this, addr, type);
1409 
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);
1417   }
1418   return addr;
1419 }
1420 
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);
1424 
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");
1435     }
1436   } else {
1437     sb_error = Status::FromErrorString("SBProcess is invalid");
1438   }
1439   return addr;
1440 }
1441 
1442 lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) {
1443   LLDB_INSTRUMENT_VA(this, ptr);
1444 
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");
1456     }
1457   } else {
1458     sb_error = Status::FromErrorString("SBProcess is invalid");
1459   }
1460   return sb_error;
1461 }
1462 
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);
1469 }
1470