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