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