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