xref: /freebsd/contrib/llvm-project/lldb/source/API/SBProcess.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
SBProcess()56 SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }
57 
58 // SBProcess constructor
59 
SBProcess(const SBProcess & rhs)60 SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
61   LLDB_INSTRUMENT_VA(this, rhs);
62 }
63 
SBProcess(const lldb::ProcessSP & process_sp)64 SBProcess::SBProcess(const lldb::ProcessSP &process_sp)
65     : m_opaque_wp(process_sp) {
66   LLDB_INSTRUMENT_VA(this, process_sp);
67 }
68 
operator =(const SBProcess & rhs)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 
GetBroadcasterClassName()80 const char *SBProcess::GetBroadcasterClassName() {
81   LLDB_INSTRUMENT();
82 
83   return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
84 }
85 
GetPluginName()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 
GetShortPluginName()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 
GetSP() const106 lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }
107 
SetSP(const ProcessSP & process_sp)108 void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
109 
Clear()110 void SBProcess::Clear() {
111   LLDB_INSTRUMENT_VA(this);
112 
113   m_opaque_wp.reset();
114 }
115 
IsValid() const116 bool SBProcess::IsValid() const {
117   LLDB_INSTRUMENT_VA(this);
118   return this->operator bool();
119 }
operator bool() const120 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 
RemoteLaunch(char const ** argv,char const ** envp,const char * stdin_path,const char * stdout_path,const char * stderr_path,const char * working_directory,uint32_t launch_flags,bool stop_at_entry,lldb::SBError & error)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.SetErrorString("must be in eStateConnected to call RemoteLaunch");
156     }
157   } else {
158     error.SetErrorString("unable to attach pid");
159   }
160 
161   return error.Success();
162 }
163 
RemoteAttachToProcessWithID(lldb::pid_t pid,lldb::SBError & error)164 bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,
165                                             lldb::SBError &error) {
166   LLDB_INSTRUMENT_VA(this, pid, error);
167 
168   ProcessSP process_sp(GetSP());
169   if (process_sp) {
170     std::lock_guard<std::recursive_mutex> guard(
171         process_sp->GetTarget().GetAPIMutex());
172     if (process_sp->GetState() == eStateConnected) {
173       ProcessAttachInfo attach_info;
174       attach_info.SetProcessID(pid);
175       error.SetError(process_sp->Attach(attach_info));
176     } else {
177       error.SetErrorString(
178           "must be in eStateConnected to call RemoteAttachToProcessWithID");
179     }
180   } else {
181     error.SetErrorString("unable to attach pid");
182   }
183 
184   return error.Success();
185 }
186 
GetNumThreads()187 uint32_t SBProcess::GetNumThreads() {
188   LLDB_INSTRUMENT_VA(this);
189 
190   uint32_t num_threads = 0;
191   ProcessSP process_sp(GetSP());
192   if (process_sp) {
193     Process::StopLocker stop_locker;
194 
195     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
196     std::lock_guard<std::recursive_mutex> guard(
197         process_sp->GetTarget().GetAPIMutex());
198     num_threads = process_sp->GetThreadList().GetSize(can_update);
199   }
200 
201   return num_threads;
202 }
203 
GetSelectedThread() const204 SBThread SBProcess::GetSelectedThread() const {
205   LLDB_INSTRUMENT_VA(this);
206 
207   SBThread sb_thread;
208   ThreadSP thread_sp;
209   ProcessSP process_sp(GetSP());
210   if (process_sp) {
211     std::lock_guard<std::recursive_mutex> guard(
212         process_sp->GetTarget().GetAPIMutex());
213     thread_sp = process_sp->GetThreadList().GetSelectedThread();
214     sb_thread.SetThread(thread_sp);
215   }
216 
217   return sb_thread;
218 }
219 
CreateOSPluginThread(lldb::tid_t tid,lldb::addr_t context)220 SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,
221                                          lldb::addr_t context) {
222   LLDB_INSTRUMENT_VA(this, tid, context);
223 
224   SBThread sb_thread;
225   ThreadSP thread_sp;
226   ProcessSP process_sp(GetSP());
227   if (process_sp) {
228     std::lock_guard<std::recursive_mutex> guard(
229         process_sp->GetTarget().GetAPIMutex());
230     thread_sp = process_sp->CreateOSPluginThread(tid, context);
231     sb_thread.SetThread(thread_sp);
232   }
233 
234   return sb_thread;
235 }
236 
GetTarget() const237 SBTarget SBProcess::GetTarget() const {
238   LLDB_INSTRUMENT_VA(this);
239 
240   SBTarget sb_target;
241   TargetSP target_sp;
242   ProcessSP process_sp(GetSP());
243   if (process_sp) {
244     target_sp = process_sp->GetTarget().shared_from_this();
245     sb_target.SetSP(target_sp);
246   }
247 
248   return sb_target;
249 }
250 
PutSTDIN(const char * src,size_t src_len)251 size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {
252   LLDB_INSTRUMENT_VA(this, src, src_len);
253 
254   size_t ret_val = 0;
255   ProcessSP process_sp(GetSP());
256   if (process_sp) {
257     Status error;
258     ret_val = process_sp->PutSTDIN(src, src_len, error);
259   }
260 
261   return ret_val;
262 }
263 
GetSTDOUT(char * dst,size_t dst_len) const264 size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {
265   LLDB_INSTRUMENT_VA(this, dst, dst_len);
266 
267   size_t bytes_read = 0;
268   ProcessSP process_sp(GetSP());
269   if (process_sp) {
270     Status error;
271     bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);
272   }
273 
274   return bytes_read;
275 }
276 
GetSTDERR(char * dst,size_t dst_len) const277 size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {
278   LLDB_INSTRUMENT_VA(this, dst, dst_len);
279 
280   size_t bytes_read = 0;
281   ProcessSP process_sp(GetSP());
282   if (process_sp) {
283     Status error;
284     bytes_read = process_sp->GetSTDERR(dst, dst_len, error);
285   }
286 
287   return bytes_read;
288 }
289 
GetAsyncProfileData(char * dst,size_t dst_len) const290 size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {
291   LLDB_INSTRUMENT_VA(this, dst, dst_len);
292 
293   size_t bytes_read = 0;
294   ProcessSP process_sp(GetSP());
295   if (process_sp) {
296     Status error;
297     bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);
298   }
299 
300   return bytes_read;
301 }
302 
ReportEventState(const SBEvent & event,SBFile out) const303 void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const {
304   LLDB_INSTRUMENT_VA(this, event, out);
305 
306   return ReportEventState(event, out.m_opaque_sp);
307 }
308 
ReportEventState(const SBEvent & event,FILE * out) const309 void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {
310   LLDB_INSTRUMENT_VA(this, event, out);
311   FileSP outfile = std::make_shared<NativeFile>(out, false);
312   return ReportEventState(event, outfile);
313 }
314 
ReportEventState(const SBEvent & event,FileSP out) const315 void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const {
316 
317   LLDB_INSTRUMENT_VA(this, event, out);
318 
319   if (!out || !out->IsValid())
320     return;
321 
322   ProcessSP process_sp(GetSP());
323   if (process_sp) {
324     StreamFile stream(out);
325     const StateType event_state = SBProcess::GetStateFromEvent(event);
326     stream.Printf("Process %" PRIu64 " %s\n", process_sp->GetID(),
327                   SBDebugger::StateAsCString(event_state));
328   }
329 }
330 
AppendEventStateReport(const SBEvent & event,SBCommandReturnObject & result)331 void SBProcess::AppendEventStateReport(const SBEvent &event,
332                                        SBCommandReturnObject &result) {
333   LLDB_INSTRUMENT_VA(this, event, result);
334 
335   ProcessSP process_sp(GetSP());
336   if (process_sp) {
337     const StateType event_state = SBProcess::GetStateFromEvent(event);
338     char message[1024];
339     ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",
340                process_sp->GetID(), SBDebugger::StateAsCString(event_state));
341 
342     result.AppendMessage(message);
343   }
344 }
345 
SetSelectedThread(const SBThread & thread)346 bool SBProcess::SetSelectedThread(const SBThread &thread) {
347   LLDB_INSTRUMENT_VA(this, thread);
348 
349   ProcessSP process_sp(GetSP());
350   if (process_sp) {
351     std::lock_guard<std::recursive_mutex> guard(
352         process_sp->GetTarget().GetAPIMutex());
353     return process_sp->GetThreadList().SetSelectedThreadByID(
354         thread.GetThreadID());
355   }
356   return false;
357 }
358 
SetSelectedThreadByID(lldb::tid_t tid)359 bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {
360   LLDB_INSTRUMENT_VA(this, tid);
361 
362   bool ret_val = false;
363   ProcessSP process_sp(GetSP());
364   if (process_sp) {
365     std::lock_guard<std::recursive_mutex> guard(
366         process_sp->GetTarget().GetAPIMutex());
367     ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);
368   }
369 
370   return ret_val;
371 }
372 
SetSelectedThreadByIndexID(uint32_t index_id)373 bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {
374   LLDB_INSTRUMENT_VA(this, index_id);
375 
376   bool ret_val = false;
377   ProcessSP process_sp(GetSP());
378   if (process_sp) {
379     std::lock_guard<std::recursive_mutex> guard(
380         process_sp->GetTarget().GetAPIMutex());
381     ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);
382   }
383 
384   return ret_val;
385 }
386 
GetThreadAtIndex(size_t index)387 SBThread SBProcess::GetThreadAtIndex(size_t index) {
388   LLDB_INSTRUMENT_VA(this, index);
389 
390   SBThread sb_thread;
391   ThreadSP thread_sp;
392   ProcessSP process_sp(GetSP());
393   if (process_sp) {
394     Process::StopLocker stop_locker;
395     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
396     std::lock_guard<std::recursive_mutex> guard(
397         process_sp->GetTarget().GetAPIMutex());
398     thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
399     sb_thread.SetThread(thread_sp);
400   }
401 
402   return sb_thread;
403 }
404 
GetNumQueues()405 uint32_t SBProcess::GetNumQueues() {
406   LLDB_INSTRUMENT_VA(this);
407 
408   uint32_t num_queues = 0;
409   ProcessSP process_sp(GetSP());
410   if (process_sp) {
411     Process::StopLocker stop_locker;
412     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
413       std::lock_guard<std::recursive_mutex> guard(
414           process_sp->GetTarget().GetAPIMutex());
415       num_queues = process_sp->GetQueueList().GetSize();
416     }
417   }
418 
419   return num_queues;
420 }
421 
GetQueueAtIndex(size_t index)422 SBQueue SBProcess::GetQueueAtIndex(size_t index) {
423   LLDB_INSTRUMENT_VA(this, index);
424 
425   SBQueue sb_queue;
426   QueueSP queue_sp;
427   ProcessSP process_sp(GetSP());
428   if (process_sp) {
429     Process::StopLocker stop_locker;
430     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
431       std::lock_guard<std::recursive_mutex> guard(
432           process_sp->GetTarget().GetAPIMutex());
433       queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
434       sb_queue.SetQueue(queue_sp);
435     }
436   }
437 
438   return sb_queue;
439 }
440 
GetStopID(bool include_expression_stops)441 uint32_t SBProcess::GetStopID(bool include_expression_stops) {
442   LLDB_INSTRUMENT_VA(this, include_expression_stops);
443 
444   ProcessSP process_sp(GetSP());
445   if (process_sp) {
446     std::lock_guard<std::recursive_mutex> guard(
447         process_sp->GetTarget().GetAPIMutex());
448     if (include_expression_stops)
449       return process_sp->GetStopID();
450     else
451       return process_sp->GetLastNaturalStopID();
452   }
453   return 0;
454 }
455 
GetStopEventForStopID(uint32_t stop_id)456 SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {
457   LLDB_INSTRUMENT_VA(this, stop_id);
458 
459   SBEvent sb_event;
460   EventSP event_sp;
461   ProcessSP process_sp(GetSP());
462   if (process_sp) {
463     std::lock_guard<std::recursive_mutex> guard(
464         process_sp->GetTarget().GetAPIMutex());
465     event_sp = process_sp->GetStopEventForStopID(stop_id);
466     sb_event.reset(event_sp);
467   }
468 
469   return sb_event;
470 }
471 
ForceScriptedState(StateType new_state)472 void SBProcess::ForceScriptedState(StateType new_state) {
473   LLDB_INSTRUMENT_VA(this, new_state);
474 
475   if (ProcessSP process_sp = GetSP()) {
476     std::lock_guard<std::recursive_mutex> guard(
477         process_sp->GetTarget().GetAPIMutex());
478     process_sp->ForceScriptedState(new_state);
479   }
480 }
481 
GetState()482 StateType SBProcess::GetState() {
483   LLDB_INSTRUMENT_VA(this);
484 
485   StateType ret_val = eStateInvalid;
486   ProcessSP process_sp(GetSP());
487   if (process_sp) {
488     std::lock_guard<std::recursive_mutex> guard(
489         process_sp->GetTarget().GetAPIMutex());
490     ret_val = process_sp->GetState();
491   }
492 
493   return ret_val;
494 }
495 
GetExitStatus()496 int SBProcess::GetExitStatus() {
497   LLDB_INSTRUMENT_VA(this);
498 
499   int exit_status = 0;
500   ProcessSP process_sp(GetSP());
501   if (process_sp) {
502     std::lock_guard<std::recursive_mutex> guard(
503         process_sp->GetTarget().GetAPIMutex());
504     exit_status = process_sp->GetExitStatus();
505   }
506 
507   return exit_status;
508 }
509 
GetExitDescription()510 const char *SBProcess::GetExitDescription() {
511   LLDB_INSTRUMENT_VA(this);
512 
513   ProcessSP process_sp(GetSP());
514   if (!process_sp)
515     return nullptr;
516 
517   std::lock_guard<std::recursive_mutex> guard(
518       process_sp->GetTarget().GetAPIMutex());
519   return ConstString(process_sp->GetExitDescription()).GetCString();
520 }
521 
GetProcessID()522 lldb::pid_t SBProcess::GetProcessID() {
523   LLDB_INSTRUMENT_VA(this);
524 
525   lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
526   ProcessSP process_sp(GetSP());
527   if (process_sp)
528     ret_val = process_sp->GetID();
529 
530   return ret_val;
531 }
532 
GetUniqueID()533 uint32_t SBProcess::GetUniqueID() {
534   LLDB_INSTRUMENT_VA(this);
535 
536   uint32_t ret_val = 0;
537   ProcessSP process_sp(GetSP());
538   if (process_sp)
539     ret_val = process_sp->GetUniqueID();
540   return ret_val;
541 }
542 
GetByteOrder() const543 ByteOrder SBProcess::GetByteOrder() const {
544   LLDB_INSTRUMENT_VA(this);
545 
546   ByteOrder byteOrder = eByteOrderInvalid;
547   ProcessSP process_sp(GetSP());
548   if (process_sp)
549     byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
550 
551   return byteOrder;
552 }
553 
GetAddressByteSize() const554 uint32_t SBProcess::GetAddressByteSize() const {
555   LLDB_INSTRUMENT_VA(this);
556 
557   uint32_t size = 0;
558   ProcessSP process_sp(GetSP());
559   if (process_sp)
560     size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
561 
562   return size;
563 }
564 
Continue()565 SBError SBProcess::Continue() {
566   LLDB_INSTRUMENT_VA(this);
567 
568   SBError sb_error;
569   ProcessSP process_sp(GetSP());
570 
571   if (process_sp) {
572     std::lock_guard<std::recursive_mutex> guard(
573         process_sp->GetTarget().GetAPIMutex());
574 
575     if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
576       sb_error.ref() = process_sp->Resume();
577     else
578       sb_error.ref() = process_sp->ResumeSynchronous(nullptr);
579   } else
580     sb_error.SetErrorString("SBProcess is invalid");
581 
582   return sb_error;
583 }
584 
Destroy()585 SBError SBProcess::Destroy() {
586   LLDB_INSTRUMENT_VA(this);
587 
588   SBError sb_error;
589   ProcessSP process_sp(GetSP());
590   if (process_sp) {
591     std::lock_guard<std::recursive_mutex> guard(
592         process_sp->GetTarget().GetAPIMutex());
593     sb_error.SetError(process_sp->Destroy(false));
594   } else
595     sb_error.SetErrorString("SBProcess is invalid");
596 
597   return sb_error;
598 }
599 
Stop()600 SBError SBProcess::Stop() {
601   LLDB_INSTRUMENT_VA(this);
602 
603   SBError sb_error;
604   ProcessSP process_sp(GetSP());
605   if (process_sp) {
606     std::lock_guard<std::recursive_mutex> guard(
607         process_sp->GetTarget().GetAPIMutex());
608     sb_error.SetError(process_sp->Halt());
609   } else
610     sb_error.SetErrorString("SBProcess is invalid");
611 
612   return sb_error;
613 }
614 
Kill()615 SBError SBProcess::Kill() {
616   LLDB_INSTRUMENT_VA(this);
617 
618   SBError sb_error;
619   ProcessSP process_sp(GetSP());
620   if (process_sp) {
621     std::lock_guard<std::recursive_mutex> guard(
622         process_sp->GetTarget().GetAPIMutex());
623     sb_error.SetError(process_sp->Destroy(true));
624   } else
625     sb_error.SetErrorString("SBProcess is invalid");
626 
627   return sb_error;
628 }
629 
Detach()630 SBError SBProcess::Detach() {
631   LLDB_INSTRUMENT_VA(this);
632 
633   // FIXME: This should come from a process default.
634   bool keep_stopped = false;
635   return Detach(keep_stopped);
636 }
637 
Detach(bool keep_stopped)638 SBError SBProcess::Detach(bool keep_stopped) {
639   LLDB_INSTRUMENT_VA(this, keep_stopped);
640 
641   SBError sb_error;
642   ProcessSP process_sp(GetSP());
643   if (process_sp) {
644     std::lock_guard<std::recursive_mutex> guard(
645         process_sp->GetTarget().GetAPIMutex());
646     sb_error.SetError(process_sp->Detach(keep_stopped));
647   } else
648     sb_error.SetErrorString("SBProcess is invalid");
649 
650   return sb_error;
651 }
652 
Signal(int signo)653 SBError SBProcess::Signal(int signo) {
654   LLDB_INSTRUMENT_VA(this, signo);
655 
656   SBError sb_error;
657   ProcessSP process_sp(GetSP());
658   if (process_sp) {
659     std::lock_guard<std::recursive_mutex> guard(
660         process_sp->GetTarget().GetAPIMutex());
661     sb_error.SetError(process_sp->Signal(signo));
662   } else
663     sb_error.SetErrorString("SBProcess is invalid");
664 
665   return sb_error;
666 }
667 
GetUnixSignals()668 SBUnixSignals SBProcess::GetUnixSignals() {
669   LLDB_INSTRUMENT_VA(this);
670 
671   if (auto process_sp = GetSP())
672     return SBUnixSignals{process_sp};
673 
674   return SBUnixSignals{};
675 }
676 
SendAsyncInterrupt()677 void SBProcess::SendAsyncInterrupt() {
678   LLDB_INSTRUMENT_VA(this);
679 
680   ProcessSP process_sp(GetSP());
681   if (process_sp) {
682     process_sp->SendAsyncInterrupt();
683   }
684 }
685 
GetThreadByID(tid_t tid)686 SBThread SBProcess::GetThreadByID(tid_t tid) {
687   LLDB_INSTRUMENT_VA(this, tid);
688 
689   SBThread sb_thread;
690   ThreadSP thread_sp;
691   ProcessSP process_sp(GetSP());
692   if (process_sp) {
693     Process::StopLocker stop_locker;
694     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
695     std::lock_guard<std::recursive_mutex> guard(
696         process_sp->GetTarget().GetAPIMutex());
697     thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);
698     sb_thread.SetThread(thread_sp);
699   }
700 
701   return sb_thread;
702 }
703 
GetThreadByIndexID(uint32_t index_id)704 SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {
705   LLDB_INSTRUMENT_VA(this, index_id);
706 
707   SBThread sb_thread;
708   ThreadSP thread_sp;
709   ProcessSP process_sp(GetSP());
710   if (process_sp) {
711     Process::StopLocker stop_locker;
712     const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
713     std::lock_guard<std::recursive_mutex> guard(
714         process_sp->GetTarget().GetAPIMutex());
715     thread_sp =
716         process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);
717     sb_thread.SetThread(thread_sp);
718   }
719 
720   return sb_thread;
721 }
722 
GetStateFromEvent(const SBEvent & event)723 StateType SBProcess::GetStateFromEvent(const SBEvent &event) {
724   LLDB_INSTRUMENT_VA(event);
725 
726   StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());
727 
728   return ret_val;
729 }
730 
GetRestartedFromEvent(const SBEvent & event)731 bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {
732   LLDB_INSTRUMENT_VA(event);
733 
734   bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());
735 
736   return ret_val;
737 }
738 
GetNumRestartedReasonsFromEvent(const lldb::SBEvent & event)739 size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {
740   LLDB_INSTRUMENT_VA(event);
741 
742   return Process::ProcessEventData::GetNumRestartedReasons(event.get());
743 }
744 
745 const char *
GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent & event,size_t idx)746 SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,
747                                               size_t idx) {
748   LLDB_INSTRUMENT_VA(event, idx);
749 
750   return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex(
751                          event.get(), idx))
752       .GetCString();
753 }
754 
GetProcessFromEvent(const SBEvent & event)755 SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {
756   LLDB_INSTRUMENT_VA(event);
757 
758   ProcessSP process_sp =
759       Process::ProcessEventData::GetProcessFromEvent(event.get());
760   if (!process_sp) {
761     // StructuredData events also know the process they come from. Try that.
762     process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());
763   }
764 
765   return SBProcess(process_sp);
766 }
767 
GetInterruptedFromEvent(const SBEvent & event)768 bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {
769   LLDB_INSTRUMENT_VA(event);
770 
771   return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
772 }
773 
774 lldb::SBStructuredData
GetStructuredDataFromEvent(const lldb::SBEvent & event)775 SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {
776   LLDB_INSTRUMENT_VA(event);
777 
778   return SBStructuredData(event.GetSP());
779 }
780 
EventIsProcessEvent(const SBEvent & event)781 bool SBProcess::EventIsProcessEvent(const SBEvent &event) {
782   LLDB_INSTRUMENT_VA(event);
783 
784   return Process::ProcessEventData::GetEventDataFromEvent(event.get()) !=
785          nullptr;
786 }
787 
EventIsStructuredDataEvent(const lldb::SBEvent & event)788 bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {
789   LLDB_INSTRUMENT_VA(event);
790 
791   EventSP event_sp = event.GetSP();
792   EventData *event_data = event_sp ? event_sp->GetData() : nullptr;
793   return event_data && (event_data->GetFlavor() ==
794                         EventDataStructuredData::GetFlavorString());
795 }
796 
GetBroadcaster() const797 SBBroadcaster SBProcess::GetBroadcaster() const {
798   LLDB_INSTRUMENT_VA(this);
799 
800   ProcessSP process_sp(GetSP());
801 
802   SBBroadcaster broadcaster(process_sp.get(), false);
803 
804   return broadcaster;
805 }
806 
GetBroadcasterClass()807 const char *SBProcess::GetBroadcasterClass() {
808   LLDB_INSTRUMENT();
809 
810   return ConstString(Process::GetStaticBroadcasterClass()).AsCString();
811 }
812 
FindRangesInMemory(const void * buf,uint64_t size,const SBAddressRangeList & ranges,uint32_t alignment,uint32_t max_matches,SBError & error)813 lldb::SBAddressRangeList SBProcess::FindRangesInMemory(
814     const void *buf, uint64_t size, const SBAddressRangeList &ranges,
815     uint32_t alignment, uint32_t max_matches, SBError &error) {
816   LLDB_INSTRUMENT_VA(this, buf, size, ranges, alignment, max_matches, error);
817 
818   lldb::SBAddressRangeList matches;
819 
820   ProcessSP process_sp(GetSP());
821   if (!process_sp) {
822     error.SetErrorString("SBProcess is invalid");
823     return matches;
824   }
825   Process::StopLocker stop_locker;
826   if (!stop_locker.TryLock(&process_sp->GetRunLock())) {
827     error.SetErrorString("process is running");
828     return matches;
829   }
830   std::lock_guard<std::recursive_mutex> guard(
831       process_sp->GetTarget().GetAPIMutex());
832   matches.m_opaque_up->ref() = process_sp->FindRangesInMemory(
833       reinterpret_cast<const uint8_t *>(buf), size, ranges.ref().ref(),
834       alignment, max_matches, error.ref());
835   return matches;
836 }
837 
FindInMemory(const void * buf,uint64_t size,const SBAddressRange & range,uint32_t alignment,SBError & error)838 lldb::addr_t SBProcess::FindInMemory(const void *buf, uint64_t size,
839                                      const SBAddressRange &range,
840                                      uint32_t alignment, SBError &error) {
841   LLDB_INSTRUMENT_VA(this, buf, size, range, alignment, error);
842 
843   ProcessSP process_sp(GetSP());
844 
845   if (!process_sp) {
846     error.SetErrorString("SBProcess is invalid");
847     return LLDB_INVALID_ADDRESS;
848   }
849 
850   Process::StopLocker stop_locker;
851   if (!stop_locker.TryLock(&process_sp->GetRunLock())) {
852     error.SetErrorString("process is running");
853     return LLDB_INVALID_ADDRESS;
854   }
855 
856   std::lock_guard<std::recursive_mutex> guard(
857       process_sp->GetTarget().GetAPIMutex());
858   return process_sp->FindInMemory(reinterpret_cast<const uint8_t *>(buf), size,
859                                   range.ref(), alignment, error.ref());
860 }
861 
ReadMemory(addr_t addr,void * dst,size_t dst_len,SBError & sb_error)862 size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,
863                              SBError &sb_error) {
864   LLDB_INSTRUMENT_VA(this, addr, dst, dst_len, sb_error);
865 
866   if (!dst) {
867     sb_error.SetErrorStringWithFormat(
868         "no buffer provided to read %zu bytes into", dst_len);
869     return 0;
870   }
871 
872   size_t bytes_read = 0;
873   ProcessSP process_sp(GetSP());
874 
875 
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       bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());
882     } else {
883       sb_error.SetErrorString("process is running");
884     }
885   } else {
886     sb_error.SetErrorString("SBProcess is invalid");
887   }
888 
889   return bytes_read;
890 }
891 
ReadCStringFromMemory(addr_t addr,void * buf,size_t size,lldb::SBError & sb_error)892 size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,
893                                         lldb::SBError &sb_error) {
894   LLDB_INSTRUMENT_VA(this, addr, buf, size, sb_error);
895 
896   size_t bytes_read = 0;
897   ProcessSP process_sp(GetSP());
898   if (process_sp) {
899     Process::StopLocker stop_locker;
900     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
901       std::lock_guard<std::recursive_mutex> guard(
902           process_sp->GetTarget().GetAPIMutex());
903       bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,
904                                                      sb_error.ref());
905     } else {
906       sb_error.SetErrorString("process is running");
907     }
908   } else {
909     sb_error.SetErrorString("SBProcess is invalid");
910   }
911   return bytes_read;
912 }
913 
ReadUnsignedFromMemory(addr_t addr,uint32_t byte_size,lldb::SBError & sb_error)914 uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,
915                                            lldb::SBError &sb_error) {
916   LLDB_INSTRUMENT_VA(this, addr, byte_size, sb_error);
917 
918   uint64_t value = 0;
919   ProcessSP process_sp(GetSP());
920   if (process_sp) {
921     Process::StopLocker stop_locker;
922     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
923       std::lock_guard<std::recursive_mutex> guard(
924           process_sp->GetTarget().GetAPIMutex());
925       value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,
926                                                         sb_error.ref());
927     } else {
928       sb_error.SetErrorString("process is running");
929     }
930   } else {
931     sb_error.SetErrorString("SBProcess is invalid");
932   }
933   return value;
934 }
935 
ReadPointerFromMemory(addr_t addr,lldb::SBError & sb_error)936 lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,
937                                               lldb::SBError &sb_error) {
938   LLDB_INSTRUMENT_VA(this, addr, sb_error);
939 
940   lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
941   ProcessSP process_sp(GetSP());
942   if (process_sp) {
943     Process::StopLocker stop_locker;
944     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
945       std::lock_guard<std::recursive_mutex> guard(
946           process_sp->GetTarget().GetAPIMutex());
947       ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());
948     } else {
949       sb_error.SetErrorString("process is running");
950     }
951   } else {
952     sb_error.SetErrorString("SBProcess is invalid");
953   }
954   return ptr;
955 }
956 
WriteMemory(addr_t addr,const void * src,size_t src_len,SBError & sb_error)957 size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,
958                               SBError &sb_error) {
959   LLDB_INSTRUMENT_VA(this, addr, src, src_len, sb_error);
960 
961   size_t bytes_written = 0;
962 
963   ProcessSP process_sp(GetSP());
964 
965   if (process_sp) {
966     Process::StopLocker stop_locker;
967     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
968       std::lock_guard<std::recursive_mutex> guard(
969           process_sp->GetTarget().GetAPIMutex());
970       bytes_written =
971           process_sp->WriteMemory(addr, src, src_len, sb_error.ref());
972     } else {
973       sb_error.SetErrorString("process is running");
974     }
975   }
976 
977   return bytes_written;
978 }
979 
GetStatus(SBStream & status)980 void SBProcess::GetStatus(SBStream &status) {
981   LLDB_INSTRUMENT_VA(this, status);
982 
983   ProcessSP process_sp(GetSP());
984   if (process_sp)
985     process_sp->GetStatus(status.ref());
986 }
987 
GetDescription(SBStream & description)988 bool SBProcess::GetDescription(SBStream &description) {
989   LLDB_INSTRUMENT_VA(this, description);
990 
991   Stream &strm = description.ref();
992 
993   ProcessSP process_sp(GetSP());
994   if (process_sp) {
995     char path[PATH_MAX];
996     GetTarget().GetExecutable().GetPath(path, sizeof(path));
997     Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
998     const char *exe_name = nullptr;
999     if (exe_module)
1000       exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1001 
1002     strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1003                 process_sp->GetID(), lldb_private::StateAsCString(GetState()),
1004                 GetNumThreads(), exe_name ? ", executable = " : "",
1005                 exe_name ? exe_name : "");
1006   } else
1007     strm.PutCString("No value");
1008 
1009   return true;
1010 }
1011 
GetExtendedCrashInformation()1012 SBStructuredData SBProcess::GetExtendedCrashInformation() {
1013   LLDB_INSTRUMENT_VA(this);
1014   SBStructuredData data;
1015   ProcessSP process_sp(GetSP());
1016   if (!process_sp)
1017     return data;
1018 
1019   PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1020 
1021   if (!platform_sp)
1022     return data;
1023 
1024   auto expected_data =
1025       platform_sp->FetchExtendedCrashInformation(*process_sp.get());
1026 
1027   if (!expected_data)
1028     return data;
1029 
1030   StructuredData::ObjectSP fetched_data = *expected_data;
1031   data.m_impl_up->SetObjectSP(fetched_data);
1032   return data;
1033 }
1034 
1035 uint32_t
GetNumSupportedHardwareWatchpoints(lldb::SBError & sb_error) const1036 SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
1037   LLDB_INSTRUMENT_VA(this, sb_error);
1038 
1039   uint32_t num = 0;
1040   ProcessSP process_sp(GetSP());
1041   if (process_sp) {
1042     std::lock_guard<std::recursive_mutex> guard(
1043         process_sp->GetTarget().GetAPIMutex());
1044     std::optional<uint32_t> actual_num = process_sp->GetWatchpointSlotCount();
1045     if (actual_num) {
1046       num = *actual_num;
1047     } else {
1048       sb_error.SetErrorString("Unable to determine number of watchpoints");
1049     }
1050   } else {
1051     sb_error.SetErrorString("SBProcess is invalid");
1052   }
1053   return num;
1054 }
1055 
LoadImage(lldb::SBFileSpec & sb_remote_image_spec,lldb::SBError & sb_error)1056 uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,
1057                               lldb::SBError &sb_error) {
1058   LLDB_INSTRUMENT_VA(this, sb_remote_image_spec, sb_error);
1059 
1060   return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1061 }
1062 
LoadImage(const lldb::SBFileSpec & sb_local_image_spec,const lldb::SBFileSpec & sb_remote_image_spec,lldb::SBError & sb_error)1063 uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,
1064                               const lldb::SBFileSpec &sb_remote_image_spec,
1065                               lldb::SBError &sb_error) {
1066   LLDB_INSTRUMENT_VA(this, sb_local_image_spec, sb_remote_image_spec, sb_error);
1067 
1068   ProcessSP process_sp(GetSP());
1069   if (process_sp) {
1070     Process::StopLocker stop_locker;
1071     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1072       std::lock_guard<std::recursive_mutex> guard(
1073         process_sp->GetTarget().GetAPIMutex());
1074       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1075       return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,
1076                                     *sb_remote_image_spec, sb_error.ref());
1077     } else {
1078       sb_error.SetErrorString("process is running");
1079     }
1080   } else {
1081     sb_error.SetErrorString("process is invalid");
1082   }
1083   return LLDB_INVALID_IMAGE_TOKEN;
1084 }
1085 
LoadImageUsingPaths(const lldb::SBFileSpec & image_spec,SBStringList & paths,lldb::SBFileSpec & loaded_path,lldb::SBError & error)1086 uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,
1087                                         SBStringList &paths,
1088                                         lldb::SBFileSpec &loaded_path,
1089                                         lldb::SBError &error) {
1090   LLDB_INSTRUMENT_VA(this, image_spec, paths, loaded_path, error);
1091 
1092   ProcessSP process_sp(GetSP());
1093   if (process_sp) {
1094     Process::StopLocker stop_locker;
1095     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1096       std::lock_guard<std::recursive_mutex> guard(
1097         process_sp->GetTarget().GetAPIMutex());
1098       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1099       size_t num_paths = paths.GetSize();
1100       std::vector<std::string> paths_vec;
1101       paths_vec.reserve(num_paths);
1102       for (size_t i = 0; i < num_paths; i++)
1103         paths_vec.push_back(paths.GetStringAtIndex(i));
1104       FileSpec loaded_spec;
1105 
1106       uint32_t token = platform_sp->LoadImageUsingPaths(
1107           process_sp.get(), *image_spec, paths_vec, error.ref(), &loaded_spec);
1108       if (token != LLDB_INVALID_IMAGE_TOKEN)
1109         loaded_path = loaded_spec;
1110       return token;
1111     } else {
1112       error.SetErrorString("process is running");
1113     }
1114   } else {
1115     error.SetErrorString("process is invalid");
1116   }
1117 
1118   return LLDB_INVALID_IMAGE_TOKEN;
1119 }
1120 
UnloadImage(uint32_t image_token)1121 lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {
1122   LLDB_INSTRUMENT_VA(this, image_token);
1123 
1124   lldb::SBError sb_error;
1125   ProcessSP process_sp(GetSP());
1126   if (process_sp) {
1127     Process::StopLocker stop_locker;
1128     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1129       std::lock_guard<std::recursive_mutex> guard(
1130           process_sp->GetTarget().GetAPIMutex());
1131       PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1132       sb_error.SetError(
1133           platform_sp->UnloadImage(process_sp.get(), image_token));
1134     } else {
1135       sb_error.SetErrorString("process is running");
1136     }
1137   } else
1138     sb_error.SetErrorString("invalid process");
1139   return sb_error;
1140 }
1141 
SendEventData(const char * event_data)1142 lldb::SBError SBProcess::SendEventData(const char *event_data) {
1143   LLDB_INSTRUMENT_VA(this, event_data);
1144 
1145   lldb::SBError sb_error;
1146   ProcessSP process_sp(GetSP());
1147   if (process_sp) {
1148     Process::StopLocker stop_locker;
1149     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1150       std::lock_guard<std::recursive_mutex> guard(
1151           process_sp->GetTarget().GetAPIMutex());
1152       sb_error.SetError(process_sp->SendEventData(event_data));
1153     } else {
1154       sb_error.SetErrorString("process is running");
1155     }
1156   } else
1157     sb_error.SetErrorString("invalid process");
1158   return sb_error;
1159 }
1160 
GetNumExtendedBacktraceTypes()1161 uint32_t SBProcess::GetNumExtendedBacktraceTypes() {
1162   LLDB_INSTRUMENT_VA(this);
1163 
1164   ProcessSP process_sp(GetSP());
1165   if (process_sp && process_sp->GetSystemRuntime()) {
1166     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1167     return runtime->GetExtendedBacktraceTypes().size();
1168   }
1169   return 0;
1170 }
1171 
GetExtendedBacktraceTypeAtIndex(uint32_t idx)1172 const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {
1173   LLDB_INSTRUMENT_VA(this, idx);
1174 
1175   ProcessSP process_sp(GetSP());
1176   if (process_sp && process_sp->GetSystemRuntime()) {
1177     SystemRuntime *runtime = process_sp->GetSystemRuntime();
1178     const std::vector<ConstString> &names =
1179         runtime->GetExtendedBacktraceTypes();
1180     if (idx < names.size()) {
1181       return names[idx].AsCString();
1182     }
1183   }
1184   return nullptr;
1185 }
1186 
GetHistoryThreads(addr_t addr)1187 SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {
1188   LLDB_INSTRUMENT_VA(this, addr);
1189 
1190   ProcessSP process_sp(GetSP());
1191   SBThreadCollection threads;
1192   if (process_sp) {
1193     threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1194   }
1195   return threads;
1196 }
1197 
IsInstrumentationRuntimePresent(InstrumentationRuntimeType type)1198 bool SBProcess::IsInstrumentationRuntimePresent(
1199     InstrumentationRuntimeType type) {
1200   LLDB_INSTRUMENT_VA(this, type);
1201 
1202   ProcessSP process_sp(GetSP());
1203   if (!process_sp)
1204     return false;
1205 
1206   std::lock_guard<std::recursive_mutex> guard(
1207       process_sp->GetTarget().GetAPIMutex());
1208 
1209   InstrumentationRuntimeSP runtime_sp =
1210       process_sp->GetInstrumentationRuntime(type);
1211 
1212   if (!runtime_sp.get())
1213     return false;
1214 
1215   return runtime_sp->IsActive();
1216 }
1217 
SaveCore(const char * file_name)1218 lldb::SBError SBProcess::SaveCore(const char *file_name) {
1219   LLDB_INSTRUMENT_VA(this, file_name);
1220   SBSaveCoreOptions options;
1221   options.SetOutputFile(SBFileSpec(file_name));
1222   options.SetStyle(SaveCoreStyle::eSaveCoreFull);
1223   return SaveCore(options);
1224 }
1225 
SaveCore(const char * file_name,const char * flavor,SaveCoreStyle core_style)1226 lldb::SBError SBProcess::SaveCore(const char *file_name,
1227                                   const char *flavor,
1228                                   SaveCoreStyle core_style) {
1229   LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style);
1230   SBSaveCoreOptions options;
1231   options.SetOutputFile(SBFileSpec(file_name));
1232   options.SetStyle(core_style);
1233   SBError error = options.SetPluginName(flavor);
1234   if (error.Fail())
1235     return error;
1236   return SaveCore(options);
1237 }
1238 
SaveCore(SBSaveCoreOptions & options)1239 lldb::SBError SBProcess::SaveCore(SBSaveCoreOptions &options) {
1240 
1241   LLDB_INSTRUMENT_VA(this, options);
1242 
1243   lldb::SBError error;
1244   ProcessSP process_sp(GetSP());
1245   if (!process_sp) {
1246     error.SetErrorString("SBProcess is invalid");
1247     return error;
1248   }
1249 
1250   std::lock_guard<std::recursive_mutex> guard(
1251       process_sp->GetTarget().GetAPIMutex());
1252 
1253   if (process_sp->GetState() != eStateStopped) {
1254     error.SetErrorString("the process is not stopped");
1255     return error;
1256   }
1257 
1258   error.ref() = PluginManager::SaveCore(process_sp, options.ref());
1259 
1260   return error;
1261 }
1262 
1263 lldb::SBError
GetMemoryRegionInfo(lldb::addr_t load_addr,SBMemoryRegionInfo & sb_region_info)1264 SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
1265                                SBMemoryRegionInfo &sb_region_info) {
1266   LLDB_INSTRUMENT_VA(this, load_addr, sb_region_info);
1267 
1268   lldb::SBError sb_error;
1269   ProcessSP process_sp(GetSP());
1270   if (process_sp) {
1271     Process::StopLocker stop_locker;
1272     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1273       std::lock_guard<std::recursive_mutex> guard(
1274           process_sp->GetTarget().GetAPIMutex());
1275 
1276       sb_error.ref() =
1277           process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref());
1278     } else {
1279       sb_error.SetErrorString("process is running");
1280     }
1281   } else {
1282     sb_error.SetErrorString("SBProcess is invalid");
1283   }
1284   return sb_error;
1285 }
1286 
GetMemoryRegions()1287 lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {
1288   LLDB_INSTRUMENT_VA(this);
1289 
1290   lldb::SBMemoryRegionInfoList sb_region_list;
1291 
1292   ProcessSP process_sp(GetSP());
1293   Process::StopLocker stop_locker;
1294   if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {
1295     std::lock_guard<std::recursive_mutex> guard(
1296         process_sp->GetTarget().GetAPIMutex());
1297 
1298     process_sp->GetMemoryRegions(sb_region_list.ref());
1299   }
1300 
1301   return sb_region_list;
1302 }
1303 
GetProcessInfo()1304 lldb::SBProcessInfo SBProcess::GetProcessInfo() {
1305   LLDB_INSTRUMENT_VA(this);
1306 
1307   lldb::SBProcessInfo sb_proc_info;
1308   ProcessSP process_sp(GetSP());
1309   ProcessInstanceInfo proc_info;
1310   if (process_sp && process_sp->GetProcessInfo(proc_info)) {
1311     sb_proc_info.SetProcessInfo(proc_info);
1312   }
1313   return sb_proc_info;
1314 }
1315 
GetCoreFile()1316 lldb::SBFileSpec SBProcess::GetCoreFile() {
1317   LLDB_INSTRUMENT_VA(this);
1318 
1319   ProcessSP process_sp(GetSP());
1320   FileSpec core_file;
1321   if (process_sp) {
1322     core_file = process_sp->GetCoreFile();
1323   }
1324   return SBFileSpec(core_file);
1325 }
1326 
GetAddressMask(AddressMaskType type,AddressMaskRange addr_range)1327 addr_t SBProcess::GetAddressMask(AddressMaskType type,
1328                                  AddressMaskRange addr_range) {
1329   LLDB_INSTRUMENT_VA(this, type, addr_range);
1330 
1331   if (ProcessSP process_sp = GetSP()) {
1332     switch (type) {
1333     case eAddressMaskTypeCode:
1334       if (addr_range == eAddressMaskRangeHigh)
1335         return process_sp->GetHighmemCodeAddressMask();
1336       else
1337         return process_sp->GetCodeAddressMask();
1338     case eAddressMaskTypeData:
1339       if (addr_range == eAddressMaskRangeHigh)
1340         return process_sp->GetHighmemDataAddressMask();
1341       else
1342         return process_sp->GetDataAddressMask();
1343     case eAddressMaskTypeAny:
1344       if (addr_range == eAddressMaskRangeHigh)
1345         return process_sp->GetHighmemDataAddressMask();
1346       else
1347         return process_sp->GetDataAddressMask();
1348     }
1349   }
1350   return LLDB_INVALID_ADDRESS_MASK;
1351 }
1352 
SetAddressMask(AddressMaskType type,addr_t mask,AddressMaskRange addr_range)1353 void SBProcess::SetAddressMask(AddressMaskType type, addr_t mask,
1354                                AddressMaskRange addr_range) {
1355   LLDB_INSTRUMENT_VA(this, type, mask, addr_range);
1356 
1357   if (ProcessSP process_sp = GetSP()) {
1358     switch (type) {
1359     case eAddressMaskTypeCode:
1360       if (addr_range == eAddressMaskRangeAll) {
1361         process_sp->SetCodeAddressMask(mask);
1362         process_sp->SetHighmemCodeAddressMask(mask);
1363       } else if (addr_range == eAddressMaskRangeHigh) {
1364         process_sp->SetHighmemCodeAddressMask(mask);
1365       } else {
1366         process_sp->SetCodeAddressMask(mask);
1367       }
1368       break;
1369     case eAddressMaskTypeData:
1370       if (addr_range == eAddressMaskRangeAll) {
1371         process_sp->SetDataAddressMask(mask);
1372         process_sp->SetHighmemDataAddressMask(mask);
1373       } else if (addr_range == eAddressMaskRangeHigh) {
1374         process_sp->SetHighmemDataAddressMask(mask);
1375       } else {
1376         process_sp->SetDataAddressMask(mask);
1377       }
1378       break;
1379     case eAddressMaskTypeAll:
1380       if (addr_range == eAddressMaskRangeAll) {
1381         process_sp->SetCodeAddressMask(mask);
1382         process_sp->SetDataAddressMask(mask);
1383         process_sp->SetHighmemCodeAddressMask(mask);
1384         process_sp->SetHighmemDataAddressMask(mask);
1385       } else if (addr_range == eAddressMaskRangeHigh) {
1386         process_sp->SetHighmemCodeAddressMask(mask);
1387         process_sp->SetHighmemDataAddressMask(mask);
1388       } else {
1389         process_sp->SetCodeAddressMask(mask);
1390         process_sp->SetDataAddressMask(mask);
1391       }
1392       break;
1393     }
1394   }
1395 }
1396 
SetAddressableBits(AddressMaskType type,uint32_t num_bits,AddressMaskRange addr_range)1397 void SBProcess::SetAddressableBits(AddressMaskType type, uint32_t num_bits,
1398                                    AddressMaskRange addr_range) {
1399   LLDB_INSTRUMENT_VA(this, type, num_bits, addr_range);
1400 
1401   SetAddressMask(type, AddressableBits::AddressableBitToMask(num_bits),
1402                  addr_range);
1403 }
1404 
FixAddress(addr_t addr,AddressMaskType type)1405 addr_t SBProcess::FixAddress(addr_t addr, AddressMaskType type) {
1406   LLDB_INSTRUMENT_VA(this, addr, type);
1407 
1408   if (ProcessSP process_sp = GetSP()) {
1409     if (type == eAddressMaskTypeAny)
1410       return process_sp->FixAnyAddress(addr);
1411     else if (type == eAddressMaskTypeData)
1412       return process_sp->FixDataAddress(addr);
1413     else if (type == eAddressMaskTypeCode)
1414       return process_sp->FixCodeAddress(addr);
1415   }
1416   return addr;
1417 }
1418 
AllocateMemory(size_t size,uint32_t permissions,lldb::SBError & sb_error)1419 lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions,
1420                                        lldb::SBError &sb_error) {
1421   LLDB_INSTRUMENT_VA(this, size, permissions, sb_error);
1422 
1423   lldb::addr_t addr = LLDB_INVALID_ADDRESS;
1424   ProcessSP process_sp(GetSP());
1425   if (process_sp) {
1426     Process::StopLocker stop_locker;
1427     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1428       std::lock_guard<std::recursive_mutex> guard(
1429           process_sp->GetTarget().GetAPIMutex());
1430       addr = process_sp->AllocateMemory(size, permissions, sb_error.ref());
1431     } else {
1432       sb_error.SetErrorString("process is running");
1433     }
1434   } else {
1435     sb_error.SetErrorString("SBProcess is invalid");
1436   }
1437   return addr;
1438 }
1439 
DeallocateMemory(lldb::addr_t ptr)1440 lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) {
1441   LLDB_INSTRUMENT_VA(this, ptr);
1442 
1443   lldb::SBError sb_error;
1444   ProcessSP process_sp(GetSP());
1445   if (process_sp) {
1446     Process::StopLocker stop_locker;
1447     if (stop_locker.TryLock(&process_sp->GetRunLock())) {
1448       std::lock_guard<std::recursive_mutex> guard(
1449           process_sp->GetTarget().GetAPIMutex());
1450       Status error = process_sp->DeallocateMemory(ptr);
1451       sb_error.SetError(error);
1452     } else {
1453       sb_error.SetErrorString("process is running");
1454     }
1455   } else {
1456     sb_error.SetErrorString("SBProcess is invalid");
1457   }
1458   return sb_error;
1459 }
1460 
GetScriptedImplementation()1461 lldb::SBScriptObject SBProcess::GetScriptedImplementation() {
1462   LLDB_INSTRUMENT_VA(this);
1463   ProcessSP process_sp(GetSP());
1464   return lldb::SBScriptObject((process_sp) ? process_sp->GetImplementation()
1465                                            : nullptr,
1466                               eScriptLanguageDefault);
1467 }
1468