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