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