xref: /freebsd/contrib/llvm-project/lldb/source/API/SBProcess.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/StructuredDataImpl.h"
22 #include "lldb/Target/MemoryRegionInfo.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/SystemRuntime.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Utility/Args.h"
29 #include "lldb/Utility/ProcessInfo.h"
30 #include "lldb/Utility/State.h"
31 #include "lldb/Utility/Stream.h"
32 
33 #include "lldb/API/SBBroadcaster.h"
34 #include "lldb/API/SBCommandReturnObject.h"
35 #include "lldb/API/SBDebugger.h"
36 #include "lldb/API/SBEvent.h"
37 #include "lldb/API/SBFile.h"
38 #include "lldb/API/SBFileSpec.h"
39 #include "lldb/API/SBMemoryRegionInfo.h"
40 #include "lldb/API/SBMemoryRegionInfoList.h"
41 #include "lldb/API/SBStream.h"
42 #include "lldb/API/SBStringList.h"
43 #include "lldb/API/SBStructuredData.h"
44 #include "lldb/API/SBThread.h"
45 #include "lldb/API/SBThreadCollection.h"
46 #include "lldb/API/SBTrace.h"
47 #include "lldb/API/SBUnixSignals.h"
48 
49 using namespace lldb;
50 using namespace lldb_private;
51 
52 SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }
53 
54 // SBProcess constructor
55 
56 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
57   LLDB_INSTRUMENT_VA(this, rhs);
58 }
59 
60 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
61     : m_opaque_wp(process_sp) {
62   LLDB_INSTRUMENT_VA(this, process_sp);
63 }
64 
65 const SBProcess &SBProcess::operator=(const SBProcess &rhs) {
66   LLDB_INSTRUMENT_VA(this, rhs);
67 
68   if (this != &rhs)
69     m_opaque_wp = rhs.m_opaque_wp;
70   return *this;
71 }
72 
73 // Destructor
74 SBProcess::~SBProcess() = default;
75 
76 const char *SBProcess::GetBroadcasterClassName() {
77   LLDB_INSTRUMENT();
78 
79   return Process::GetStaticBroadcasterClass().AsCString();
80 }
81 
82 const char *SBProcess::GetPluginName() {
83   LLDB_INSTRUMENT_VA(this);
84 
85   ProcessSP process_sp(GetSP());
86   if (process_sp) {
87     return ConstString(process_sp->GetPluginName()).GetCString();
88   }
89   return "<Unknown>";
90 }
91 
92 const char *SBProcess::GetShortPluginName() {
93   LLDB_INSTRUMENT_VA(this);
94 
95   ProcessSP process_sp(GetSP());
96   if (process_sp) {
97     return ConstString(process_sp->GetPluginName()).GetCString();
98   }
99   return "<Unknown>";
100 }
101 
102 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
103 
104 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
105 
106 void SBProcess::Clear() {
107   LLDB_INSTRUMENT_VA(this);
108 
109   m_opaque_wp.reset();
110 }
111 
112 bool SBProcess::IsValid() const {
113   LLDB_INSTRUMENT_VA(this);
114   return this->operator bool();
115 }
116 SBProcess::operator bool() const {
117   LLDB_INSTRUMENT_VA(this);
118 
119   ProcessSP process_sp(m_opaque_wp.lock());
120   return ((bool)process_sp && process_sp->IsValid());
121 }
122 
123 bool SBProcess::RemoteLaunch(char const **argv, char const **envp,
124                              const char *stdin_path, const char *stdout_path,
125                              const char *stderr_path,
126                              const char *working_directory,
127                              uint32_t launch_flags, bool stop_at_entry,
128                              lldb::SBError &error) {
129   LLDB_INSTRUMENT_VA(this, argv, envp, stdin_path, stdout_path, stderr_path,
130                      working_directory, launch_flags, stop_at_entry, error);
131 
132   ProcessSP process_sp(GetSP());
133   if (process_sp) {
134     std::lock_guard<std::recursive_mutex> guard(
135         process_sp->GetTarget().GetAPIMutex());
136     if (process_sp->GetState() == eStateConnected) {
137       if (stop_at_entry)
138         launch_flags |= eLaunchFlagStopAtEntry;
139       ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),
140                                     FileSpec(stderr_path),
141                                     FileSpec(working_directory), launch_flags);
142       Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
143       if (exe_module)
144         launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
145       if (argv)
146         launch_info.GetArguments().AppendArguments(argv);
147       if (envp)
148         launch_info.GetEnvironment() = Environment(envp);
149       error.SetError(process_sp->Launch(launch_info));
150     } else {
151       error.SetErrorString("must be in eStateConnected to call RemoteLaunch");
152     }
153   } else {
154     error.SetErrorString("unable to attach pid");
155   }
156 
157   return error.Success();
158 }
159 
160 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
161                                             lldb::SBError &error) {
162   LLDB_INSTRUMENT_VA(this, pid, error);
163 
164   ProcessSP process_sp(GetSP());
165   if (process_sp) {
166     std::lock_guard<std::recursive_mutex> guard(
167         process_sp->GetTarget().GetAPIMutex());
168     if (process_sp->GetState() == eStateConnected) {
169       ProcessAttachInfo attach_info;
170       attach_info.SetProcessID(pid);
171       error.SetError(process_sp->Attach(attach_info));
172     } else {
173       error.SetErrorString(
174           "must be in eStateConnected to call RemoteAttachToProcessWithID");
175     }
176   } else {
177     error.SetErrorString("unable to attach pid");
178   }
179 
180   return error.Success();
181 }
182 
183 uint32_t SBProcess::GetNumThreads() {
184   LLDB_INSTRUMENT_VA(this);
185 
186   uint32_t num_threads = 0;
187   ProcessSP process_sp(GetSP());
188   if (process_sp) {
189     Process::StopLocker stop_locker;
190 
191     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
192     std::lock_guard<std::recursive_mutex> guard(
193         process_sp->GetTarget().GetAPIMutex());
194     num_threads = process_sp->GetThreadList().GetSize(can_update);
195   }
196 
197   return num_threads;
198 }
199 
200 SBThread SBProcess::GetSelectedThread() const {
201   LLDB_INSTRUMENT_VA(this);
202 
203   SBThread sb_thread;
204   ThreadSP thread_sp;
205   ProcessSP process_sp(GetSP());
206   if (process_sp) {
207     std::lock_guard<std::recursive_mutex> guard(
208         process_sp->GetTarget().GetAPIMutex());
209     thread_sp = process_sp->GetThreadList().GetSelectedThread();
210     sb_thread.SetThread(thread_sp);
211   }
212 
213   return sb_thread;
214 }
215 
216 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
217                                          lldb::addr_t context) {
218   LLDB_INSTRUMENT_VA(this, tid, context);
219 
220   SBThread sb_thread;
221   ThreadSP thread_sp;
222   ProcessSP process_sp(GetSP());
223   if (process_sp) {
224     std::lock_guard<std::recursive_mutex> guard(
225         process_sp->GetTarget().GetAPIMutex());
226     thread_sp = process_sp->CreateOSPluginThread(tid, context);
227     sb_thread.SetThread(thread_sp);
228   }
229 
230   return sb_thread;
231 }
232 
233 SBTarget SBProcess::GetTarget() const {
234   LLDB_INSTRUMENT_VA(this);
235 
236   SBTarget sb_target;
237   TargetSP target_sp;
238   ProcessSP process_sp(GetSP());
239   if (process_sp) {
240     target_sp = process_sp->GetTarget().shared_from_this();
241     sb_target.SetSP(target_sp);
242   }
243 
244   return sb_target;
245 }
246 
247 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
248   LLDB_INSTRUMENT_VA(this, src, src_len);
249 
250   size_t ret_val = 0;
251   ProcessSP process_sp(GetSP());
252   if (process_sp) {
253     Status error;
254     ret_val = process_sp->PutSTDIN(src, src_len, error);
255   }
256 
257   return ret_val;
258 }
259 
260 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
261   LLDB_INSTRUMENT_VA(this, dst, dst_len);
262 
263   size_t bytes_read = 0;
264   ProcessSP process_sp(GetSP());
265   if (process_sp) {
266     Status error;
267     bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
268   }
269 
270   return bytes_read;
271 }
272 
273 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
274   LLDB_INSTRUMENT_VA(this, dst, dst_len);
275 
276   size_t bytes_read = 0;
277   ProcessSP process_sp(GetSP());
278   if (process_sp) {
279     Status error;
280     bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
281   }
282 
283   return bytes_read;
284 }
285 
286 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
287   LLDB_INSTRUMENT_VA(this, dst, dst_len);
288 
289   size_t bytes_read = 0;
290   ProcessSP process_sp(GetSP());
291   if (process_sp) {
292     Status error;
293     bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
294   }
295 
296   return bytes_read;
297 }
298 
299 void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const {
300   LLDB_INSTRUMENT_VA(this, event, out);
301 
302   return ReportEventState(event, out.m_opaque_sp);
303 }
304 
305 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
306   LLDB_INSTRUMENT_VA(this, event, out);
307   FileSP outfile = std::make_shared<NativeFile>(out, false);
308   return ReportEventState(event, outfile);
309 }
310 
311 void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const {
312 
313   LLDB_INSTRUMENT_VA(this, event, out);
314 
315   if (!out || !out->IsValid())
316     return;
317 
318   ProcessSP process_sp(GetSP());
319   if (process_sp) {
320     StreamFile stream(out);
321     const StateType event_state = SBProcess::GetStateFromEvent(event);
322     stream.Printf("Process %" PRIu64 " %s\n",
323         process_sp->GetID(), SBDebugger::StateAsCString(event_state));
324   }
325 }
326 
327 void SBProcess::AppendEventStateReport(const SBEvent &event,
328                                        SBCommandReturnObject &result) {
329   LLDB_INSTRUMENT_VA(this, event, result);
330 
331   ProcessSP process_sp(GetSP());
332   if (process_sp) {
333     const StateType event_state = SBProcess::GetStateFromEvent(event);
334     char message[1024];
335     ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
336                process_sp->GetID(), SBDebugger::StateAsCString(event_state));
337 
338     result.AppendMessage(message);
339   }
340 }
341 
342 bool SBProcess::SetSelectedThread(const SBThread &thread) {
343   LLDB_INSTRUMENT_VA(this, thread);
344 
345   ProcessSP process_sp(GetSP());
346   if (process_sp) {
347     std::lock_guard<std::recursive_mutex> guard(
348         process_sp->GetTarget().GetAPIMutex());
349     return process_sp->GetThreadList().SetSelectedThreadByID(
350         thread.GetThreadID());
351   }
352   return false;
353 }
354 
355 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
356   LLDB_INSTRUMENT_VA(this, tid);
357 
358   bool ret_val = false;
359   ProcessSP process_sp(GetSP());
360   if (process_sp) {
361     std::lock_guard<std::recursive_mutex> guard(
362         process_sp->GetTarget().GetAPIMutex());
363     ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
364   }
365 
366   return ret_val;
367 }
368 
369 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
370   LLDB_INSTRUMENT_VA(this, index_id);
371 
372   bool ret_val = false;
373   ProcessSP process_sp(GetSP());
374   if (process_sp) {
375     std::lock_guard<std::recursive_mutex> guard(
376         process_sp->GetTarget().GetAPIMutex());
377     ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
378   }
379 
380 
381   return ret_val;
382 }
383 
384 SBThread SBProcess::GetThreadAtIndex(size_t index) {
385   LLDB_INSTRUMENT_VA(this, index);
386 
387   SBThread sb_thread;
388   ThreadSP thread_sp;
389   ProcessSP process_sp(GetSP());
390   if (process_sp) {
391     Process::StopLocker stop_locker;
392     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
393     std::lock_guard<std::recursive_mutex> guard(
394         process_sp->GetTarget().GetAPIMutex());
395     thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
396     sb_thread.SetThread(thread_sp);
397   }
398 
399   return sb_thread;
400 }
401 
402 uint32_t SBProcess::GetNumQueues() {
403   LLDB_INSTRUMENT_VA(this);
404 
405   uint32_t num_queues = 0;
406   ProcessSP process_sp(GetSP());
407   if (process_sp) {
408     Process::StopLocker stop_locker;
409     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
410       std::lock_guard<std::recursive_mutex> guard(
411           process_sp->GetTarget().GetAPIMutex());
412       num_queues = process_sp->GetQueueList().GetSize();
413     }
414   }
415 
416   return num_queues;
417 }
418 
419 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
420   LLDB_INSTRUMENT_VA(this, index);
421 
422   SBQueue sb_queue;
423   QueueSP queue_sp;
424   ProcessSP process_sp(GetSP());
425   if (process_sp) {
426     Process::StopLocker stop_locker;
427     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
428       std::lock_guard<std::recursive_mutex> guard(
429           process_sp->GetTarget().GetAPIMutex());
430       queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
431       sb_queue.SetQueue(queue_sp);
432     }
433   }
434 
435   return sb_queue;
436 }
437 
438 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
439   LLDB_INSTRUMENT_VA(this, include_expression_stops);
440 
441   ProcessSP process_sp(GetSP());
442   if (process_sp) {
443     std::lock_guard<std::recursive_mutex> guard(
444         process_sp->GetTarget().GetAPIMutex());
445     if (include_expression_stops)
446       return process_sp->GetStopID();
447     else
448       return process_sp->GetLastNaturalStopID();
449   }
450   return 0;
451 }
452 
453 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
454   LLDB_INSTRUMENT_VA(this, stop_id);
455 
456   SBEvent sb_event;
457   EventSP event_sp;
458   ProcessSP process_sp(GetSP());
459   if (process_sp) {
460     std::lock_guard<std::recursive_mutex> guard(
461         process_sp->GetTarget().GetAPIMutex());
462     event_sp = process_sp->GetStopEventForStopID(stop_id);
463     sb_event.reset(event_sp);
464   }
465 
466   return sb_event;
467 }
468 
469 StateType SBProcess::GetState() {
470   LLDB_INSTRUMENT_VA(this);
471 
472   StateType ret_val = eStateInvalid;
473   ProcessSP process_sp(GetSP());
474   if (process_sp) {
475     std::lock_guard<std::recursive_mutex> guard(
476         process_sp->GetTarget().GetAPIMutex());
477     ret_val = process_sp->GetState();
478   }
479 
480   return ret_val;
481 }
482 
483 int SBProcess::GetExitStatus() {
484   LLDB_INSTRUMENT_VA(this);
485 
486   int exit_status = 0;
487   ProcessSP process_sp(GetSP());
488   if (process_sp) {
489     std::lock_guard<std::recursive_mutex> guard(
490         process_sp->GetTarget().GetAPIMutex());
491     exit_status = process_sp->GetExitStatus();
492   }
493 
494   return exit_status;
495 }
496 
497 const char *SBProcess::GetExitDescription() {
498   LLDB_INSTRUMENT_VA(this);
499 
500   const char *exit_desc = nullptr;
501   ProcessSP process_sp(GetSP());
502   if (process_sp) {
503     std::lock_guard<std::recursive_mutex> guard(
504         process_sp->GetTarget().GetAPIMutex());
505     exit_desc = process_sp->GetExitDescription();
506   }
507   return exit_desc;
508 }
509 
510 lldb::pid_t SBProcess::GetProcessID() {
511   LLDB_INSTRUMENT_VA(this);
512 
513   lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
514   ProcessSP process_sp(GetSP());
515   if (process_sp)
516     ret_val = process_sp->GetID();
517 
518   return ret_val;
519 }
520 
521 uint32_t SBProcess::GetUniqueID() {
522   LLDB_INSTRUMENT_VA(this);
523 
524   uint32_t ret_val = 0;
525   ProcessSP process_sp(GetSP());
526   if (process_sp)
527     ret_val = process_sp->GetUniqueID();
528   return ret_val;
529 }
530 
531 ByteOrder SBProcess::GetByteOrder() const {
532   LLDB_INSTRUMENT_VA(this);
533 
534   ByteOrder byteOrder = eByteOrderInvalid;
535   ProcessSP process_sp(GetSP());
536   if (process_sp)
537     byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
538 
539 
540   return byteOrder;
541 }
542 
543 uint32_t SBProcess::GetAddressByteSize() const {
544   LLDB_INSTRUMENT_VA(this);
545 
546   uint32_t size = 0;
547   ProcessSP process_sp(GetSP());
548   if (process_sp)
549     size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
550 
551 
552   return size;
553 }
554 
555 SBError SBProcess::Continue() {
556   LLDB_INSTRUMENT_VA(this);
557 
558   SBError sb_error;
559   ProcessSP process_sp(GetSP());
560 
561   if (process_sp) {
562     std::lock_guard<std::recursive_mutex> guard(
563         process_sp->GetTarget().GetAPIMutex());
564 
565     if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
566       sb_error.ref() = process_sp->Resume();
567     else
568       sb_error.ref() = process_sp->ResumeSynchronous(nullptr);
569   } else
570     sb_error.SetErrorString("SBProcess is invalid");
571 
572   return sb_error;
573 }
574 
575 SBError SBProcess::Destroy() {
576   LLDB_INSTRUMENT_VA(this);
577 
578   SBError sb_error;
579   ProcessSP process_sp(GetSP());
580   if (process_sp) {
581     std::lock_guard<std::recursive_mutex> guard(
582         process_sp->GetTarget().GetAPIMutex());
583     sb_error.SetError(process_sp->Destroy(false));
584   } else
585     sb_error.SetErrorString("SBProcess is invalid");
586 
587   return sb_error;
588 }
589 
590 SBError SBProcess::Stop() {
591   LLDB_INSTRUMENT_VA(this);
592 
593   SBError sb_error;
594   ProcessSP process_sp(GetSP());
595   if (process_sp) {
596     std::lock_guard<std::recursive_mutex> guard(
597         process_sp->GetTarget().GetAPIMutex());
598     sb_error.SetError(process_sp->Halt());
599   } else
600     sb_error.SetErrorString("SBProcess is invalid");
601 
602   return sb_error;
603 }
604 
605 SBError SBProcess::Kill() {
606   LLDB_INSTRUMENT_VA(this);
607 
608   SBError sb_error;
609   ProcessSP process_sp(GetSP());
610   if (process_sp) {
611     std::lock_guard<std::recursive_mutex> guard(
612         process_sp->GetTarget().GetAPIMutex());
613     sb_error.SetError(process_sp->Destroy(true));
614   } else
615     sb_error.SetErrorString("SBProcess is invalid");
616 
617   return sb_error;
618 }
619 
620 SBError SBProcess::Detach() {
621   LLDB_INSTRUMENT_VA(this);
622 
623   // FIXME: This should come from a process default.
624   bool keep_stopped = false;
625   return Detach(keep_stopped);
626 }
627 
628 SBError SBProcess::Detach(bool keep_stopped) {
629   LLDB_INSTRUMENT_VA(this, keep_stopped);
630 
631   SBError sb_error;
632   ProcessSP process_sp(GetSP());
633   if (process_sp) {
634     std::lock_guard<std::recursive_mutex> guard(
635         process_sp->GetTarget().GetAPIMutex());
636     sb_error.SetError(process_sp->Detach(keep_stopped));
637   } else
638     sb_error.SetErrorString("SBProcess is invalid");
639 
640   return sb_error;
641 }
642 
643 SBError SBProcess::Signal(int signo) {
644   LLDB_INSTRUMENT_VA(this, signo);
645 
646   SBError sb_error;
647   ProcessSP process_sp(GetSP());
648   if (process_sp) {
649     std::lock_guard<std::recursive_mutex> guard(
650         process_sp->GetTarget().GetAPIMutex());
651     sb_error.SetError(process_sp->Signal(signo));
652   } else
653     sb_error.SetErrorString("SBProcess is invalid");
654 
655   return sb_error;
656 }
657 
658 SBUnixSignals SBProcess::GetUnixSignals() {
659   LLDB_INSTRUMENT_VA(this);
660 
661   if (auto process_sp = GetSP())
662     return SBUnixSignals{process_sp};
663 
664   return SBUnixSignals{};
665 }
666 
667 void SBProcess::SendAsyncInterrupt() {
668   LLDB_INSTRUMENT_VA(this);
669 
670   ProcessSP process_sp(GetSP());
671   if (process_sp) {
672     process_sp->SendAsyncInterrupt();
673   }
674 }
675 
676 SBThread SBProcess::GetThreadByID(tid_t tid) {
677   LLDB_INSTRUMENT_VA(this, tid);
678 
679   SBThread sb_thread;
680   ThreadSP thread_sp;
681   ProcessSP process_sp(GetSP());
682   if (process_sp) {
683     Process::StopLocker stop_locker;
684     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
685     std::lock_guard<std::recursive_mutex> guard(
686         process_sp->GetTarget().GetAPIMutex());
687     thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
688     sb_thread.SetThread(thread_sp);
689   }
690 
691   return sb_thread;
692 }
693 
694 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
695   LLDB_INSTRUMENT_VA(this, index_id);
696 
697   SBThread sb_thread;
698   ThreadSP thread_sp;
699   ProcessSP process_sp(GetSP());
700   if (process_sp) {
701     Process::StopLocker stop_locker;
702     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
703     std::lock_guard<std::recursive_mutex> guard(
704         process_sp->GetTarget().GetAPIMutex());
705     thread_sp =
706         process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
707     sb_thread.SetThread(thread_sp);
708   }
709 
710   return sb_thread;
711 }
712 
713 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
714   LLDB_INSTRUMENT_VA(event);
715 
716   StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
717 
718   return ret_val;
719 }
720 
721 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
722   LLDB_INSTRUMENT_VA(event);
723 
724   bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
725 
726   return ret_val;
727 }
728 
729 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
730   LLDB_INSTRUMENT_VA(event);
731 
732   return Process::ProcessEventData::GetNumRestartedReasons(event.get());
733 }
734 
735 const char *
736 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
737                                               size_t idx) {
738   LLDB_INSTRUMENT_VA(event, idx);
739 
740   return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
741 }
742 
743 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
744   LLDB_INSTRUMENT_VA(event);
745 
746   ProcessSP process_sp =
747       Process::ProcessEventData::GetProcessFromEvent(event.get());
748   if (!process_sp) {
749     // StructuredData events also know the process they come from. Try that.
750     process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
751   }
752 
753   return SBProcess(process_sp);
754 }
755 
756 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
757   LLDB_INSTRUMENT_VA(event);
758 
759   return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
760 }
761 
762 lldb::SBStructuredData
763 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
764   LLDB_INSTRUMENT_VA(event);
765 
766   return SBStructuredData(event.GetSP());
767 }
768 
769 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
770   LLDB_INSTRUMENT_VA(event);
771 
772   return (event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass()) &&
773          !EventIsStructuredDataEvent(event);
774 }
775 
776 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
777   LLDB_INSTRUMENT_VA(event);
778 
779   EventSP event_sp = event.GetSP();
780   EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
781   return event_data && (event_data->GetFlavor() ==
782                         EventDataStructuredData::GetFlavorString());
783 }
784 
785 SBBroadcaster SBProcess::GetBroadcaster() const {
786   LLDB_INSTRUMENT_VA(this);
787 
788   ProcessSP process_sp(GetSP());
789 
790   SBBroadcaster broadcaster(process_sp.get(), false);
791 
792   return broadcaster;
793 }
794 
795 const char *SBProcess::GetBroadcasterClass() {
796   LLDB_INSTRUMENT();
797 
798   return Process::GetStaticBroadcasterClass().AsCString();
799 }
800 
801 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
802                              SBError &sb_error) {
803   LLDB_INSTRUMENT_VA(this, addr, dst, dst_len, sb_error);
804 
805   size_t bytes_read = 0;
806 
807   ProcessSP process_sp(GetSP());
808 
809 
810   if (process_sp) {
811     Process::StopLocker stop_locker;
812     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
813       std::lock_guard<std::recursive_mutex> guard(
814           process_sp->GetTarget().GetAPIMutex());
815       bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
816     } else {
817       sb_error.SetErrorString("process is running");
818     }
819   } else {
820     sb_error.SetErrorString("SBProcess is invalid");
821   }
822 
823   return bytes_read;
824 }
825 
826 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
827                                         lldb::SBError &sb_error) {
828   LLDB_INSTRUMENT_VA(this, addr, buf, size, sb_error);
829 
830   size_t bytes_read = 0;
831   ProcessSP process_sp(GetSP());
832   if (process_sp) {
833     Process::StopLocker stop_locker;
834     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
835       std::lock_guard<std::recursive_mutex> guard(
836           process_sp->GetTarget().GetAPIMutex());
837       bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
838                                                      sb_error.ref());
839     } else {
840       sb_error.SetErrorString("process is running");
841     }
842   } else {
843     sb_error.SetErrorString("SBProcess is invalid");
844   }
845   return bytes_read;
846 }
847 
848 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
849                                            lldb::SBError &sb_error) {
850   LLDB_INSTRUMENT_VA(this, addr, byte_size, sb_error);
851 
852   uint64_t value = 0;
853   ProcessSP process_sp(GetSP());
854   if (process_sp) {
855     Process::StopLocker stop_locker;
856     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
857       std::lock_guard<std::recursive_mutex> guard(
858           process_sp->GetTarget().GetAPIMutex());
859       value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
860                                                         sb_error.ref());
861     } else {
862       sb_error.SetErrorString("process is running");
863     }
864   } else {
865     sb_error.SetErrorString("SBProcess is invalid");
866   }
867   return value;
868 }
869 
870 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
871                                               lldb::SBError &sb_error) {
872   LLDB_INSTRUMENT_VA(this, addr, sb_error);
873 
874   lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
875   ProcessSP process_sp(GetSP());
876   if (process_sp) {
877     Process::StopLocker stop_locker;
878     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
879       std::lock_guard<std::recursive_mutex> guard(
880           process_sp->GetTarget().GetAPIMutex());
881       ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
882     } else {
883       sb_error.SetErrorString("process is running");
884     }
885   } else {
886     sb_error.SetErrorString("SBProcess is invalid");
887   }
888   return ptr;
889 }
890 
891 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
892                               SBError &sb_error) {
893   LLDB_INSTRUMENT_VA(this, addr, src, src_len, sb_error);
894 
895   size_t bytes_written = 0;
896 
897   ProcessSP process_sp(GetSP());
898 
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_written =
905           process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
906     } else {
907       sb_error.SetErrorString("process is running");
908     }
909   }
910 
911   return bytes_written;
912 }
913 
914 bool SBProcess::GetDescription(SBStream &description) {
915   LLDB_INSTRUMENT_VA(this, description);
916 
917   Stream &strm = description.ref();
918 
919   ProcessSP process_sp(GetSP());
920   if (process_sp) {
921     char path[PATH_MAX];
922     GetTarget().GetExecutable().GetPath(path, sizeof(path));
923     Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
924     const char *exe_name = nullptr;
925     if (exe_module)
926       exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
927 
928     strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
929                 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
930                 GetNumThreads(), exe_name ? ", executable = " : "",
931                 exe_name ? exe_name : "");
932   } else
933     strm.PutCString("No value");
934 
935   return true;
936 }
937 
938 SBStructuredData SBProcess::GetExtendedCrashInformation() {
939   LLDB_INSTRUMENT_VA(this);
940   SBStructuredData data;
941   ProcessSP process_sp(GetSP());
942   if (!process_sp)
943     return data;
944 
945   PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
946 
947   if (!platform_sp)
948     return data;
949 
950   auto expected_data =
951       platform_sp->FetchExtendedCrashInformation(*process_sp.get());
952 
953   if (!expected_data)
954     return data;
955 
956   StructuredData::ObjectSP fetched_data = *expected_data;
957   data.m_impl_up->SetObjectSP(fetched_data);
958   return data;
959 }
960 
961 uint32_t
962 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
963   LLDB_INSTRUMENT_VA(this, sb_error);
964 
965   uint32_t num = 0;
966   ProcessSP process_sp(GetSP());
967   if (process_sp) {
968     std::lock_guard<std::recursive_mutex> guard(
969         process_sp->GetTarget().GetAPIMutex());
970     sb_error.SetError(process_sp->GetWatchpointSupportInfo(num));
971   } else {
972     sb_error.SetErrorString("SBProcess is invalid");
973   }
974   return num;
975 }
976 
977 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
978                               lldb::SBError &sb_error) {
979   LLDB_INSTRUMENT_VA(this, sb_remote_image_spec, sb_error);
980 
981   return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
982 }
983 
984 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
985                               const lldb::SBFileSpec &sb_remote_image_spec,
986                               lldb::SBError &sb_error) {
987   LLDB_INSTRUMENT_VA(this, sb_local_image_spec, sb_remote_image_spec, sb_error);
988 
989   ProcessSP process_sp(GetSP());
990   if (process_sp) {
991     Process::StopLocker stop_locker;
992     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
993       std::lock_guard<std::recursive_mutex> guard(
994         process_sp->GetTarget().GetAPIMutex());
995       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
996       return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
997                                     *sb_remote_image_spec, sb_error.ref());
998     } else {
999       sb_error.SetErrorString("process is running");
1000     }
1001   } else {
1002     sb_error.SetErrorString("process is invalid");
1003   }
1004   return LLDB_INVALID_IMAGE_TOKEN;
1005 }
1006 
1007 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
1008                                         SBStringList &paths,
1009                                         lldb::SBFileSpec &loaded_path,
1010                                         lldb::SBError &error) {
1011   LLDB_INSTRUMENT_VA(this, image_spec, paths, loaded_path, error);
1012 
1013   ProcessSP process_sp(GetSP());
1014   if (process_sp) {
1015     Process::StopLocker stop_locker;
1016     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1017       std::lock_guard<std::recursive_mutex> guard(
1018         process_sp->GetTarget().GetAPIMutex());
1019       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1020       size_t num_paths = paths.GetSize();
1021       std::vector<std::string> paths_vec;
1022       paths_vec.reserve(num_paths);
1023       for (size_t i = 0; i < num_paths; i++)
1024         paths_vec.push_back(paths.GetStringAtIndex(i));
1025       FileSpec loaded_spec;
1026 
1027       uint32_t token = platform_sp->LoadImageUsingPaths(
1028           process_sp.get(), *image_spec, paths_vec, error.ref(), &loaded_spec);
1029       if (token != LLDB_INVALID_IMAGE_TOKEN)
1030         loaded_path = loaded_spec;
1031       return token;
1032     } else {
1033       error.SetErrorString("process is running");
1034     }
1035   } else {
1036     error.SetErrorString("process is invalid");
1037   }
1038 
1039   return LLDB_INVALID_IMAGE_TOKEN;
1040 }
1041 
1042 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1043   LLDB_INSTRUMENT_VA(this, image_token);
1044 
1045   lldb::SBError sb_error;
1046   ProcessSP process_sp(GetSP());
1047   if (process_sp) {
1048     Process::StopLocker stop_locker;
1049     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1050       std::lock_guard<std::recursive_mutex> guard(
1051           process_sp->GetTarget().GetAPIMutex());
1052       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1053       sb_error.SetError(
1054           platform_sp->UnloadImage(process_sp.get(), image_token));
1055     } else {
1056       sb_error.SetErrorString("process is running");
1057     }
1058   } else
1059     sb_error.SetErrorString("invalid process");
1060   return sb_error;
1061 }
1062 
1063 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1064   LLDB_INSTRUMENT_VA(this, event_data);
1065 
1066   lldb::SBError sb_error;
1067   ProcessSP process_sp(GetSP());
1068   if (process_sp) {
1069     Process::StopLocker stop_locker;
1070     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1071       std::lock_guard<std::recursive_mutex> guard(
1072           process_sp->GetTarget().GetAPIMutex());
1073       sb_error.SetError(process_sp->SendEventData(event_data));
1074     } else {
1075       sb_error.SetErrorString("process is running");
1076     }
1077   } else
1078     sb_error.SetErrorString("invalid process");
1079   return sb_error;
1080 }
1081 
1082 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1083   LLDB_INSTRUMENT_VA(this);
1084 
1085   ProcessSP process_sp(GetSP());
1086   if (process_sp && process_sp->GetSystemRuntime()) {
1087     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1088     return runtime->GetExtendedBacktraceTypes().size();
1089   }
1090   return 0;
1091 }
1092 
1093 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1094   LLDB_INSTRUMENT_VA(this, idx);
1095 
1096   ProcessSP process_sp(GetSP());
1097   if (process_sp && process_sp->GetSystemRuntime()) {
1098     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1099     const std::vector<ConstString> &names =
1100         runtime->GetExtendedBacktraceTypes();
1101     if (idx < names.size()) {
1102       return names[idx].AsCString();
1103     }
1104   }
1105   return nullptr;
1106 }
1107 
1108 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1109   LLDB_INSTRUMENT_VA(this, addr);
1110 
1111   ProcessSP process_sp(GetSP());
1112   SBThreadCollection threads;
1113   if (process_sp) {
1114     threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1115   }
1116   return threads;
1117 }
1118 
1119 bool SBProcess::IsInstrumentationRuntimePresent(
1120     InstrumentationRuntimeType type) {
1121   LLDB_INSTRUMENT_VA(this, type);
1122 
1123   ProcessSP process_sp(GetSP());
1124   if (!process_sp)
1125     return false;
1126 
1127   std::lock_guard<std::recursive_mutex> guard(
1128       process_sp->GetTarget().GetAPIMutex());
1129 
1130   InstrumentationRuntimeSP runtime_sp =
1131       process_sp->GetInstrumentationRuntime(type);
1132 
1133   if (!runtime_sp.get())
1134     return false;
1135 
1136   return runtime_sp->IsActive();
1137 }
1138 
1139 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1140   LLDB_INSTRUMENT_VA(this, file_name);
1141   return SaveCore(file_name, "", SaveCoreStyle::eSaveCoreFull);
1142 }
1143 
1144 lldb::SBError SBProcess::SaveCore(const char *file_name,
1145                                   const char *flavor,
1146                                   SaveCoreStyle core_style) {
1147   LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style);
1148 
1149   lldb::SBError error;
1150   ProcessSP process_sp(GetSP());
1151   if (!process_sp) {
1152     error.SetErrorString("SBProcess is invalid");
1153     return error;
1154   }
1155 
1156   std::lock_guard<std::recursive_mutex> guard(
1157       process_sp->GetTarget().GetAPIMutex());
1158 
1159   if (process_sp->GetState() != eStateStopped) {
1160     error.SetErrorString("the process is not stopped");
1161     return error;
1162   }
1163 
1164   FileSpec core_file(file_name);
1165   error.ref() = PluginManager::SaveCore(process_sp, core_file, core_style,
1166                                         flavor);
1167 
1168   return error;
1169 }
1170 
1171 lldb::SBError
1172 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1173                                SBMemoryRegionInfo &sb_region_info) {
1174   LLDB_INSTRUMENT_VA(this, load_addr, sb_region_info);
1175 
1176   lldb::SBError sb_error;
1177   ProcessSP process_sp(GetSP());
1178   if (process_sp) {
1179     Process::StopLocker stop_locker;
1180     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1181       std::lock_guard<std::recursive_mutex> guard(
1182           process_sp->GetTarget().GetAPIMutex());
1183 
1184       sb_error.ref() =
1185           process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref());
1186     } else {
1187       sb_error.SetErrorString("process is running");
1188     }
1189   } else {
1190     sb_error.SetErrorString("SBProcess is invalid");
1191   }
1192   return sb_error;
1193 }
1194 
1195 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1196   LLDB_INSTRUMENT_VA(this);
1197 
1198   lldb::SBMemoryRegionInfoList sb_region_list;
1199 
1200   ProcessSP process_sp(GetSP());
1201   Process::StopLocker stop_locker;
1202   if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1203     std::lock_guard<std::recursive_mutex> guard(
1204         process_sp->GetTarget().GetAPIMutex());
1205 
1206     process_sp->GetMemoryRegions(sb_region_list.ref());
1207   }
1208 
1209   return sb_region_list;
1210 }
1211 
1212 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1213   LLDB_INSTRUMENT_VA(this);
1214 
1215   lldb::SBProcessInfo sb_proc_info;
1216   ProcessSP process_sp(GetSP());
1217   ProcessInstanceInfo proc_info;
1218   if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1219     sb_proc_info.SetProcessInfo(proc_info);
1220   }
1221   return sb_proc_info;
1222 }
1223 
1224 lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions,
1225                                        lldb::SBError &sb_error) {
1226   LLDB_INSTRUMENT_VA(this, size, permissions, sb_error);
1227 
1228   lldb::addr_t addr = LLDB_INVALID_ADDRESS;
1229   ProcessSP process_sp(GetSP());
1230   if (process_sp) {
1231     Process::StopLocker stop_locker;
1232     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1233       std::lock_guard<std::recursive_mutex> guard(
1234           process_sp->GetTarget().GetAPIMutex());
1235       addr = process_sp->AllocateMemory(size, permissions, sb_error.ref());
1236     } else {
1237       sb_error.SetErrorString("process is running");
1238     }
1239   } else {
1240     sb_error.SetErrorString("SBProcess is invalid");
1241   }
1242   return addr;
1243 }
1244 
1245 lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) {
1246   LLDB_INSTRUMENT_VA(this, ptr);
1247 
1248   lldb::SBError sb_error;
1249   ProcessSP process_sp(GetSP());
1250   if (process_sp) {
1251     Process::StopLocker stop_locker;
1252     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1253       std::lock_guard<std::recursive_mutex> guard(
1254           process_sp->GetTarget().GetAPIMutex());
1255       Status error = process_sp->DeallocateMemory(ptr);
1256       sb_error.SetError(error);
1257     } else {
1258       sb_error.SetErrorString("process is running");
1259     }
1260   } else {
1261     sb_error.SetErrorString("SBProcess is invalid");
1262   }
1263   return sb_error;
1264 }
1265