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