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