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