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