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