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