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