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