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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         std::lock_guard<std::recursive_mutex> guard(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         if (stop_locker.TryLock(&process_sp->GetRunLock()))
553         {
554             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
555             num_queues = process_sp->GetQueueList().GetSize();
556         }
557     }
558 
559     if (log)
560         log->Printf ("SBProcess(%p)::GetNumQueues () => %d",
561                      static_cast<void*>(process_sp.get()), num_queues);
562 
563     return num_queues;
564 }
565 
566 SBQueue
567 SBProcess::GetQueueAtIndex (size_t index)
568 {
569     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
570 
571     SBQueue sb_queue;
572     QueueSP queue_sp;
573     ProcessSP process_sp(GetSP());
574     if (process_sp)
575     {
576         Process::StopLocker stop_locker;
577         if (stop_locker.TryLock(&process_sp->GetRunLock()))
578         {
579             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
580             queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);
581             sb_queue.SetQueue (queue_sp);
582         }
583     }
584 
585     if (log)
586         log->Printf ("SBProcess(%p)::GetQueueAtIndex (index=%d) => SBQueue(%p)",
587                      static_cast<void*>(process_sp.get()),
588                      static_cast<uint32_t>(index),
589                      static_cast<void*>(queue_sp.get()));
590 
591     return sb_queue;
592 }
593 
594 
595 uint32_t
596 SBProcess::GetStopID(bool include_expression_stops)
597 {
598     ProcessSP process_sp(GetSP());
599     if (process_sp)
600     {
601         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
602         if (include_expression_stops)
603             return process_sp->GetStopID();
604         else
605             return process_sp->GetLastNaturalStopID();
606     }
607     return 0;
608 }
609 
610 SBEvent
611 SBProcess::GetStopEventForStopID(uint32_t stop_id)
612 {
613     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
614 
615     SBEvent sb_event;
616     EventSP event_sp;
617     ProcessSP process_sp(GetSP());
618     if (process_sp)
619     {
620         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
621         event_sp = process_sp->GetStopEventForStopID(stop_id);
622         sb_event.reset(event_sp);
623     }
624 
625     if (log)
626         log->Printf ("SBProcess(%p)::GetStopEventForStopID (stop_id=%" PRIu32 ") => SBEvent(%p)",
627                      static_cast<void*>(process_sp.get()),
628                      stop_id,
629                      static_cast<void*>(event_sp.get()));
630 
631     return sb_event;
632 }
633 
634 StateType
635 SBProcess::GetState ()
636 {
637 
638     StateType ret_val = eStateInvalid;
639     ProcessSP process_sp(GetSP());
640     if (process_sp)
641     {
642         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
643         ret_val = process_sp->GetState();
644     }
645 
646     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
647     if (log)
648         log->Printf ("SBProcess(%p)::GetState () => %s",
649                      static_cast<void*>(process_sp.get()),
650                      lldb_private::StateAsCString (ret_val));
651 
652     return ret_val;
653 }
654 
655 
656 int
657 SBProcess::GetExitStatus ()
658 {
659     int exit_status = 0;
660     ProcessSP process_sp(GetSP());
661     if (process_sp)
662     {
663         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
664         exit_status = process_sp->GetExitStatus ();
665     }
666     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
667     if (log)
668         log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
669                      static_cast<void*>(process_sp.get()), exit_status,
670                      exit_status);
671 
672     return exit_status;
673 }
674 
675 const char *
676 SBProcess::GetExitDescription ()
677 {
678     const char *exit_desc = NULL;
679     ProcessSP process_sp(GetSP());
680     if (process_sp)
681     {
682         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
683         exit_desc = process_sp->GetExitDescription ();
684     }
685     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
686     if (log)
687         log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
688                      static_cast<void*>(process_sp.get()), exit_desc);
689     return exit_desc;
690 }
691 
692 lldb::pid_t
693 SBProcess::GetProcessID ()
694 {
695     lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
696     ProcessSP process_sp(GetSP());
697     if (process_sp)
698         ret_val = process_sp->GetID();
699 
700     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
701     if (log)
702         log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64,
703                      static_cast<void*>(process_sp.get()), ret_val);
704 
705     return ret_val;
706 }
707 
708 uint32_t
709 SBProcess::GetUniqueID()
710 {
711     uint32_t ret_val = 0;
712     ProcessSP process_sp(GetSP());
713     if (process_sp)
714         ret_val = process_sp->GetUniqueID();
715     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
716     if (log)
717         log->Printf ("SBProcess(%p)::GetUniqueID () => %" PRIu32,
718                      static_cast<void*>(process_sp.get()), ret_val);
719     return ret_val;
720 }
721 
722 ByteOrder
723 SBProcess::GetByteOrder () const
724 {
725     ByteOrder byteOrder = eByteOrderInvalid;
726     ProcessSP process_sp(GetSP());
727     if (process_sp)
728         byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
729 
730     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
731     if (log)
732         log->Printf ("SBProcess(%p)::GetByteOrder () => %d",
733                      static_cast<void*>(process_sp.get()), byteOrder);
734 
735     return byteOrder;
736 }
737 
738 uint32_t
739 SBProcess::GetAddressByteSize () const
740 {
741     uint32_t size = 0;
742     ProcessSP process_sp(GetSP());
743     if (process_sp)
744         size =  process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
745 
746     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
747     if (log)
748         log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d",
749                      static_cast<void*>(process_sp.get()), size);
750 
751     return size;
752 }
753 
754 SBError
755 SBProcess::Continue ()
756 {
757     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
758 
759     SBError sb_error;
760     ProcessSP process_sp(GetSP());
761 
762     if (log)
763         log->Printf ("SBProcess(%p)::Continue ()...",
764                      static_cast<void*>(process_sp.get()));
765 
766     if (process_sp)
767     {
768         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
769 
770         if (process_sp->GetTarget().GetDebugger().GetAsyncExecution ())
771             sb_error.ref() = process_sp->Resume ();
772         else
773             sb_error.ref() = process_sp->ResumeSynchronous (NULL);
774     }
775     else
776         sb_error.SetErrorString ("SBProcess is invalid");
777 
778     if (log)
779     {
780         SBStream sstr;
781         sb_error.GetDescription (sstr);
782         log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s",
783                      static_cast<void*>(process_sp.get()),
784                      static_cast<void*>(sb_error.get()), sstr.GetData());
785     }
786 
787     return sb_error;
788 }
789 
790 
791 SBError
792 SBProcess::Destroy ()
793 {
794     SBError sb_error;
795     ProcessSP process_sp(GetSP());
796     if (process_sp)
797     {
798         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
799         sb_error.SetError(process_sp->Destroy(false));
800     }
801     else
802         sb_error.SetErrorString ("SBProcess is invalid");
803 
804     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
805     if (log)
806     {
807         SBStream sstr;
808         sb_error.GetDescription (sstr);
809         log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s",
810                      static_cast<void*>(process_sp.get()),
811                      static_cast<void*>(sb_error.get()), sstr.GetData());
812     }
813 
814     return sb_error;
815 }
816 
817 
818 SBError
819 SBProcess::Stop ()
820 {
821     SBError sb_error;
822     ProcessSP process_sp(GetSP());
823     if (process_sp)
824     {
825         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
826         sb_error.SetError (process_sp->Halt());
827     }
828     else
829         sb_error.SetErrorString ("SBProcess is invalid");
830 
831     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
832     if (log)
833     {
834         SBStream sstr;
835         sb_error.GetDescription (sstr);
836         log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
837                      static_cast<void*>(process_sp.get()),
838                      static_cast<void*>(sb_error.get()), sstr.GetData());
839     }
840 
841     return sb_error;
842 }
843 
844 SBError
845 SBProcess::Kill ()
846 {
847     SBError sb_error;
848     ProcessSP process_sp(GetSP());
849     if (process_sp)
850     {
851         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
852         sb_error.SetError (process_sp->Destroy(true));
853     }
854     else
855         sb_error.SetErrorString ("SBProcess is invalid");
856 
857     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
858     if (log)
859     {
860         SBStream sstr;
861         sb_error.GetDescription (sstr);
862         log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
863                      static_cast<void*>(process_sp.get()),
864                      static_cast<void*>(sb_error.get()), sstr.GetData());
865     }
866 
867     return sb_error;
868 }
869 
870 SBError
871 SBProcess::Detach ()
872 {
873     // FIXME: This should come from a process default.
874     bool keep_stopped = false;
875     return Detach (keep_stopped);
876 }
877 
878 SBError
879 SBProcess::Detach (bool keep_stopped)
880 {
881     SBError sb_error;
882     ProcessSP process_sp(GetSP());
883     if (process_sp)
884     {
885         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
886         sb_error.SetError (process_sp->Detach(keep_stopped));
887     }
888     else
889         sb_error.SetErrorString ("SBProcess is invalid");
890 
891     return sb_error;
892 }
893 
894 SBError
895 SBProcess::Signal (int signo)
896 {
897     SBError sb_error;
898     ProcessSP process_sp(GetSP());
899     if (process_sp)
900     {
901         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
902         sb_error.SetError (process_sp->Signal (signo));
903     }
904     else
905         sb_error.SetErrorString ("SBProcess is invalid");
906     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
907     if (log)
908     {
909         SBStream sstr;
910         sb_error.GetDescription (sstr);
911         log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
912                      static_cast<void*>(process_sp.get()), signo,
913                      static_cast<void*>(sb_error.get()), sstr.GetData());
914     }
915     return sb_error;
916 }
917 
918 SBUnixSignals
919 SBProcess::GetUnixSignals()
920 {
921     if (auto process_sp = GetSP())
922         return SBUnixSignals{process_sp};
923 
924     return {};
925 }
926 
927 void
928 SBProcess::SendAsyncInterrupt ()
929 {
930     ProcessSP process_sp(GetSP());
931     if (process_sp)
932     {
933         process_sp->SendAsyncInterrupt ();
934     }
935 }
936 
937 SBThread
938 SBProcess::GetThreadByID (tid_t tid)
939 {
940     SBThread sb_thread;
941     ThreadSP thread_sp;
942     ProcessSP process_sp(GetSP());
943     if (process_sp)
944     {
945         Process::StopLocker stop_locker;
946         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
947         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
948         thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
949         sb_thread.SetThread (thread_sp);
950     }
951 
952     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
953     if (log)
954         log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
955                      static_cast<void*>(process_sp.get()), tid,
956                      static_cast<void*>(thread_sp.get()));
957 
958     return sb_thread;
959 }
960 
961 SBThread
962 SBProcess::GetThreadByIndexID (uint32_t index_id)
963 {
964     SBThread sb_thread;
965     ThreadSP thread_sp;
966     ProcessSP process_sp(GetSP());
967     if (process_sp)
968     {
969         Process::StopLocker stop_locker;
970         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
971         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
972         thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
973         sb_thread.SetThread (thread_sp);
974     }
975 
976     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
977     if (log)
978         log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
979                      static_cast<void*>(process_sp.get()), index_id,
980                      static_cast<void*>(thread_sp.get()));
981 
982     return sb_thread;
983 }
984 
985 StateType
986 SBProcess::GetStateFromEvent (const SBEvent &event)
987 {
988     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
989 
990     StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
991 
992     if (log)
993         log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s",
994                      static_cast<void*>(event.get()),
995                      lldb_private::StateAsCString (ret_val));
996 
997     return ret_val;
998 }
999 
1000 bool
1001 SBProcess::GetRestartedFromEvent (const SBEvent &event)
1002 {
1003     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1004 
1005     bool ret_val = Process::ProcessEventData::GetRestartedFromEvent (event.get());
1006 
1007     if (log)
1008         log->Printf ("SBProcess::%s (event.sp=%p) => %d", __FUNCTION__,
1009                      static_cast<void*>(event.get()), ret_val);
1010 
1011     return ret_val;
1012 }
1013 
1014 size_t
1015 SBProcess::GetNumRestartedReasonsFromEvent (const lldb::SBEvent &event)
1016 {
1017     return Process::ProcessEventData::GetNumRestartedReasons(event.get());
1018 }
1019 
1020 const char *
1021 SBProcess::GetRestartedReasonAtIndexFromEvent (const lldb::SBEvent &event, size_t idx)
1022 {
1023     return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
1024 }
1025 
1026 SBProcess
1027 SBProcess::GetProcessFromEvent (const SBEvent &event)
1028 {
1029     SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
1030     return process;
1031 }
1032 
1033 bool
1034 SBProcess::GetInterruptedFromEvent (const SBEvent &event)
1035 {
1036     return Process::ProcessEventData::GetInterruptedFromEvent(event.get());
1037 }
1038 
1039 bool
1040 SBProcess::EventIsProcessEvent (const SBEvent &event)
1041 {
1042     return event.GetBroadcasterClass() == SBProcess::GetBroadcasterClass();
1043 }
1044 
1045 SBBroadcaster
1046 SBProcess::GetBroadcaster () const
1047 {
1048     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1049 
1050     ProcessSP process_sp(GetSP());
1051 
1052     SBBroadcaster broadcaster(process_sp.get(), false);
1053 
1054     if (log)
1055         log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",
1056                      static_cast<void*>(process_sp.get()),
1057                      static_cast<void*>(broadcaster.get()));
1058 
1059     return broadcaster;
1060 }
1061 
1062 const char *
1063 SBProcess::GetBroadcasterClass ()
1064 {
1065     return Process::GetStaticBroadcasterClass().AsCString();
1066 }
1067 
1068 size_t
1069 SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
1070 {
1071     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1072 
1073     size_t bytes_read = 0;
1074 
1075     ProcessSP process_sp(GetSP());
1076 
1077     if (log)
1078         log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
1079                      static_cast<void*>(process_sp.get()), addr,
1080                      static_cast<void*>(dst), static_cast<uint64_t>(dst_len),
1081                      static_cast<void*>(sb_error.get()));
1082 
1083     if (process_sp)
1084     {
1085         Process::StopLocker stop_locker;
1086         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1087         {
1088             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1089             bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
1090         }
1091         else
1092         {
1093             if (log)
1094                 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running",
1095                              static_cast<void*>(process_sp.get()));
1096             sb_error.SetErrorString("process is running");
1097         }
1098     }
1099     else
1100     {
1101         sb_error.SetErrorString ("SBProcess is invalid");
1102     }
1103 
1104     if (log)
1105     {
1106         SBStream sstr;
1107         sb_error.GetDescription (sstr);
1108         log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1109                      static_cast<void*>(process_sp.get()), addr,
1110                      static_cast<void*>(dst), static_cast<uint64_t>(dst_len),
1111                      static_cast<void*>(sb_error.get()), sstr.GetData(),
1112                      static_cast<uint64_t>(bytes_read));
1113     }
1114 
1115     return bytes_read;
1116 }
1117 
1118 size_t
1119 SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
1120 {
1121     size_t bytes_read = 0;
1122     ProcessSP process_sp(GetSP());
1123     if (process_sp)
1124     {
1125         Process::StopLocker stop_locker;
1126         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1127         {
1128             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1129             bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
1130         }
1131         else
1132         {
1133             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1134             if (log)
1135                 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running",
1136                              static_cast<void*>(process_sp.get()));
1137             sb_error.SetErrorString("process is running");
1138         }
1139     }
1140     else
1141     {
1142         sb_error.SetErrorString ("SBProcess is invalid");
1143     }
1144     return bytes_read;
1145 }
1146 
1147 uint64_t
1148 SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
1149 {
1150     uint64_t value = 0;
1151     ProcessSP process_sp(GetSP());
1152     if (process_sp)
1153     {
1154         Process::StopLocker stop_locker;
1155         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1156         {
1157             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1158             value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
1159         }
1160         else
1161         {
1162             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1163             if (log)
1164                 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running",
1165                              static_cast<void*>(process_sp.get()));
1166             sb_error.SetErrorString("process is running");
1167         }
1168     }
1169     else
1170     {
1171         sb_error.SetErrorString ("SBProcess is invalid");
1172     }
1173     return value;
1174 }
1175 
1176 lldb::addr_t
1177 SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
1178 {
1179     lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1180     ProcessSP process_sp(GetSP());
1181     if (process_sp)
1182     {
1183         Process::StopLocker stop_locker;
1184         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1185         {
1186             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1187             ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
1188         }
1189         else
1190         {
1191             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1192             if (log)
1193                 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running",
1194                              static_cast<void*>(process_sp.get()));
1195             sb_error.SetErrorString("process is running");
1196         }
1197     }
1198     else
1199     {
1200         sb_error.SetErrorString ("SBProcess is invalid");
1201     }
1202     return ptr;
1203 }
1204 
1205 size_t
1206 SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
1207 {
1208     size_t bytes_written = 0;
1209 
1210     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1211 
1212     ProcessSP process_sp(GetSP());
1213 
1214     if (log)
1215         log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1216                      static_cast<void*>(process_sp.get()), addr,
1217                      static_cast<const void*>(src),
1218                      static_cast<uint64_t>(src_len),
1219                      static_cast<void*>(sb_error.get()));
1220 
1221     if (process_sp)
1222     {
1223         Process::StopLocker stop_locker;
1224         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1225         {
1226             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1227             bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
1228         }
1229         else
1230         {
1231             if (log)
1232                 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running",
1233                              static_cast<void*>(process_sp.get()));
1234             sb_error.SetErrorString("process is running");
1235         }
1236     }
1237 
1238     if (log)
1239     {
1240         SBStream sstr;
1241         sb_error.GetDescription (sstr);
1242         log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1243                      static_cast<void*>(process_sp.get()), addr,
1244                      static_cast<const void*>(src),
1245                      static_cast<uint64_t>(src_len),
1246                      static_cast<void*>(sb_error.get()), sstr.GetData(),
1247                      static_cast<uint64_t>(bytes_written));
1248     }
1249 
1250     return bytes_written;
1251 }
1252 
1253 bool
1254 SBProcess::GetDescription (SBStream &description)
1255 {
1256     Stream &strm = description.ref();
1257 
1258     ProcessSP process_sp(GetSP());
1259     if (process_sp)
1260     {
1261         char path[PATH_MAX];
1262         GetTarget().GetExecutable().GetPath (path, sizeof(path));
1263         Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1264         const char *exe_name = NULL;
1265         if (exe_module)
1266             exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1267 
1268         strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1269                      process_sp->GetID(),
1270                      lldb_private::StateAsCString (GetState()),
1271                      GetNumThreads(),
1272                      exe_name ? ", executable = " : "",
1273                      exe_name ? exe_name : "");
1274     }
1275     else
1276         strm.PutCString ("No value");
1277 
1278     return true;
1279 }
1280 
1281 uint32_t
1282 SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
1283 {
1284     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1285 
1286     uint32_t num = 0;
1287     ProcessSP process_sp(GetSP());
1288     if (process_sp)
1289     {
1290         std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1291         sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
1292         if (log)
1293             log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1294                          static_cast<void*>(process_sp.get()), num);
1295     }
1296     else
1297     {
1298         sb_error.SetErrorString ("SBProcess is invalid");
1299     }
1300     return num;
1301 }
1302 
1303 uint32_t
1304 SBProcess::LoadImage (lldb::SBFileSpec &sb_remote_image_spec, lldb::SBError &sb_error)
1305 {
1306     return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);
1307 }
1308 
1309 uint32_t
1310 SBProcess::LoadImage (const lldb::SBFileSpec &sb_local_image_spec,
1311                       const lldb::SBFileSpec &sb_remote_image_spec,
1312                       lldb::SBError &sb_error)
1313 {
1314     ProcessSP process_sp(GetSP());
1315     if (process_sp)
1316     {
1317         Process::StopLocker stop_locker;
1318         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1319         {
1320             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1321             PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1322             return platform_sp->LoadImage (process_sp.get(),
1323                                            *sb_local_image_spec,
1324                                            *sb_remote_image_spec,
1325                                            sb_error.ref());
1326         }
1327         else
1328         {
1329             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1330             if (log)
1331                 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running",
1332                              static_cast<void*>(process_sp.get()));
1333             sb_error.SetErrorString("process is running");
1334         }
1335     }
1336     return LLDB_INVALID_IMAGE_TOKEN;
1337 }
1338 
1339 lldb::SBError
1340 SBProcess::UnloadImage (uint32_t image_token)
1341 {
1342     lldb::SBError sb_error;
1343     ProcessSP process_sp(GetSP());
1344     if (process_sp)
1345     {
1346         Process::StopLocker stop_locker;
1347         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1348         {
1349             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1350             PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1351             sb_error.SetError (platform_sp->UnloadImage (process_sp.get(), image_token));
1352         }
1353         else
1354         {
1355             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1356             if (log)
1357                 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running",
1358                              static_cast<void*>(process_sp.get()));
1359             sb_error.SetErrorString("process is running");
1360         }
1361     }
1362     else
1363         sb_error.SetErrorString("invalid process");
1364     return sb_error;
1365 }
1366 
1367 lldb::SBError
1368 SBProcess::SendEventData (const char *event_data)
1369 {
1370     lldb::SBError sb_error;
1371     ProcessSP process_sp(GetSP());
1372     if (process_sp)
1373     {
1374         Process::StopLocker stop_locker;
1375         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1376         {
1377             std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1378             sb_error.SetError (process_sp->SendEventData (event_data));
1379         }
1380         else
1381         {
1382             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1383             if (log)
1384                 log->Printf ("SBProcess(%p)::SendEventData() => error: process is running",
1385                              static_cast<void*>(process_sp.get()));
1386             sb_error.SetErrorString("process is running");
1387         }
1388     }
1389     else
1390         sb_error.SetErrorString("invalid process");
1391     return sb_error;
1392 }
1393 
1394 uint32_t
1395 SBProcess::GetNumExtendedBacktraceTypes ()
1396 {
1397     ProcessSP process_sp(GetSP());
1398     if (process_sp && process_sp->GetSystemRuntime())
1399     {
1400         SystemRuntime *runtime = process_sp->GetSystemRuntime();
1401         return runtime->GetExtendedBacktraceTypes().size();
1402     }
1403     return 0;
1404 }
1405 
1406 const char *
1407 SBProcess::GetExtendedBacktraceTypeAtIndex (uint32_t idx)
1408 {
1409     ProcessSP process_sp(GetSP());
1410     if (process_sp && process_sp->GetSystemRuntime())
1411     {
1412         SystemRuntime *runtime = process_sp->GetSystemRuntime();
1413         const std::vector<ConstString> &names = runtime->GetExtendedBacktraceTypes();
1414         if (idx < names.size())
1415         {
1416             return names[idx].AsCString();
1417         }
1418         else
1419         {
1420             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1421             if (log)
1422                 log->Printf("SBProcess(%p)::GetExtendedBacktraceTypeAtIndex() => error: requested extended backtrace name out of bounds",
1423                             static_cast<void*>(process_sp.get()));
1424         }
1425     }
1426     return NULL;
1427 }
1428 
1429 SBThreadCollection
1430 SBProcess::GetHistoryThreads (addr_t addr)
1431 {
1432     ProcessSP process_sp(GetSP());
1433     SBThreadCollection threads;
1434     if (process_sp)
1435     {
1436         threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));
1437     }
1438     return threads;
1439 }
1440 
1441 bool
1442 SBProcess::IsInstrumentationRuntimePresent(InstrumentationRuntimeType type)
1443 {
1444     ProcessSP process_sp(GetSP());
1445     if (! process_sp)
1446         return false;
1447 
1448     InstrumentationRuntimeSP runtime_sp = process_sp->GetInstrumentationRuntime(type);
1449 
1450     if (! runtime_sp.get())
1451         return false;
1452 
1453     return runtime_sp->IsActive();
1454 }
1455 
1456 lldb::SBError
1457 SBProcess::SaveCore(const char *file_name)
1458 {
1459     lldb::SBError error;
1460     ProcessSP process_sp(GetSP());
1461     if (!process_sp)
1462     {
1463         error.SetErrorString("SBProcess is invalid");
1464         return error;
1465     }
1466 
1467     std::lock_guard<std::recursive_mutex> guard(process_sp->GetTarget().GetAPIMutex());
1468 
1469     if (process_sp->GetState() != eStateStopped)
1470     {
1471         error.SetErrorString("the process is not stopped");
1472         return error;
1473     }
1474 
1475     FileSpec core_file(file_name, false);
1476     error.ref() = PluginManager::SaveCore(process_sp, core_file);
1477     return error;
1478 }
1479