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