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 uint32_t
513 SBProcess::GetStopID(bool include_expression_stops)
514 {
515     ProcessSP process_sp(GetSP());
516     if (process_sp)
517     {
518         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
519         if (include_expression_stops)
520             return process_sp->GetStopID();
521         else
522             return process_sp->GetLastNaturalStopID();
523     }
524     return 0;
525 }
526 
527 StateType
528 SBProcess::GetState ()
529 {
530 
531     StateType ret_val = eStateInvalid;
532     ProcessSP process_sp(GetSP());
533     if (process_sp)
534     {
535         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
536         ret_val = process_sp->GetState();
537     }
538 
539     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
540     if (log)
541         log->Printf ("SBProcess(%p)::GetState () => %s",
542                      process_sp.get(),
543                      lldb_private::StateAsCString (ret_val));
544 
545     return ret_val;
546 }
547 
548 
549 int
550 SBProcess::GetExitStatus ()
551 {
552     int exit_status = 0;
553     ProcessSP process_sp(GetSP());
554     if (process_sp)
555     {
556         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
557         exit_status = process_sp->GetExitStatus ();
558     }
559     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
560     if (log)
561         log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
562                      process_sp.get(), exit_status, exit_status);
563 
564     return exit_status;
565 }
566 
567 const char *
568 SBProcess::GetExitDescription ()
569 {
570     const char *exit_desc = NULL;
571     ProcessSP process_sp(GetSP());
572     if (process_sp)
573     {
574         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
575         exit_desc = process_sp->GetExitDescription ();
576     }
577     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
578     if (log)
579         log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
580                      process_sp.get(), exit_desc);
581     return exit_desc;
582 }
583 
584 lldb::pid_t
585 SBProcess::GetProcessID ()
586 {
587     lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
588     ProcessSP process_sp(GetSP());
589     if (process_sp)
590         ret_val = process_sp->GetID();
591 
592     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
593     if (log)
594         log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64, process_sp.get(), ret_val);
595 
596     return ret_val;
597 }
598 
599 ByteOrder
600 SBProcess::GetByteOrder () const
601 {
602     ByteOrder byteOrder = eByteOrderInvalid;
603     ProcessSP process_sp(GetSP());
604     if (process_sp)
605         byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
606 
607     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
608     if (log)
609         log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder);
610 
611     return byteOrder;
612 }
613 
614 uint32_t
615 SBProcess::GetAddressByteSize () const
616 {
617     uint32_t size = 0;
618     ProcessSP process_sp(GetSP());
619     if (process_sp)
620         size =  process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
621 
622     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
623     if (log)
624         log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size);
625 
626     return size;
627 }
628 
629 SBError
630 SBProcess::Continue ()
631 {
632     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
633 
634     SBError sb_error;
635     ProcessSP process_sp(GetSP());
636 
637     if (log)
638         log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get());
639 
640     if (process_sp)
641     {
642         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
643 
644         Error error (process_sp->Resume());
645         if (error.Success())
646         {
647             if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false)
648             {
649                 if (log)
650                     log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get());
651                 process_sp->WaitForProcessToStop (NULL);
652             }
653         }
654         sb_error.SetError(error);
655     }
656     else
657         sb_error.SetErrorString ("SBProcess is invalid");
658 
659     if (log)
660     {
661         SBStream sstr;
662         sb_error.GetDescription (sstr);
663         log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData());
664     }
665 
666     return sb_error;
667 }
668 
669 
670 SBError
671 SBProcess::Destroy ()
672 {
673     SBError sb_error;
674     ProcessSP process_sp(GetSP());
675     if (process_sp)
676     {
677         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
678         sb_error.SetError(process_sp->Destroy());
679     }
680     else
681         sb_error.SetErrorString ("SBProcess is invalid");
682 
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)::Destroy () => SBError (%p): %s",
689                      process_sp.get(),
690                      sb_error.get(),
691                      sstr.GetData());
692     }
693 
694     return sb_error;
695 }
696 
697 
698 SBError
699 SBProcess::Stop ()
700 {
701     SBError sb_error;
702     ProcessSP process_sp(GetSP());
703     if (process_sp)
704     {
705         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
706         sb_error.SetError (process_sp->Halt());
707     }
708     else
709         sb_error.SetErrorString ("SBProcess is invalid");
710 
711     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
712     if (log)
713     {
714         SBStream sstr;
715         sb_error.GetDescription (sstr);
716         log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
717                      process_sp.get(),
718                      sb_error.get(),
719                      sstr.GetData());
720     }
721 
722     return sb_error;
723 }
724 
725 SBError
726 SBProcess::Kill ()
727 {
728     SBError sb_error;
729     ProcessSP process_sp(GetSP());
730     if (process_sp)
731     {
732         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
733         sb_error.SetError (process_sp->Destroy());
734     }
735     else
736         sb_error.SetErrorString ("SBProcess is invalid");
737 
738     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
739     if (log)
740     {
741         SBStream sstr;
742         sb_error.GetDescription (sstr);
743         log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
744                      process_sp.get(),
745                      sb_error.get(),
746                      sstr.GetData());
747     }
748 
749     return sb_error;
750 }
751 
752 SBError
753 SBProcess::Detach ()
754 {
755     SBError sb_error;
756     ProcessSP process_sp(GetSP());
757     if (process_sp)
758     {
759         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
760         sb_error.SetError (process_sp->Detach());
761     }
762     else
763         sb_error.SetErrorString ("SBProcess is invalid");
764 
765     return sb_error;
766 }
767 
768 SBError
769 SBProcess::Signal (int signo)
770 {
771     SBError sb_error;
772     ProcessSP process_sp(GetSP());
773     if (process_sp)
774     {
775         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
776         sb_error.SetError (process_sp->Signal (signo));
777     }
778     else
779         sb_error.SetErrorString ("SBProcess is invalid");
780     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
781     if (log)
782     {
783         SBStream sstr;
784         sb_error.GetDescription (sstr);
785         log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
786                      process_sp.get(),
787                      signo,
788                      sb_error.get(),
789                      sstr.GetData());
790     }
791     return sb_error;
792 }
793 
794 void
795 SBProcess::SendAsyncInterrupt ()
796 {
797     ProcessSP process_sp(GetSP());
798     if (process_sp)
799     {
800         process_sp->SendAsyncInterrupt ();
801     }
802 }
803 
804 SBThread
805 SBProcess::GetThreadByID (tid_t tid)
806 {
807     SBThread sb_thread;
808     ThreadSP thread_sp;
809     ProcessSP process_sp(GetSP());
810     if (process_sp)
811     {
812         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
813         Process::StopLocker stop_locker;
814         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
815         thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
816         sb_thread.SetThread (thread_sp);
817     }
818 
819     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
820     if (log)
821     {
822         log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
823                      process_sp.get(),
824                      tid,
825                      thread_sp.get());
826     }
827 
828     return sb_thread;
829 }
830 
831 SBThread
832 SBProcess::GetThreadByIndexID (uint32_t index_id)
833 {
834     SBThread sb_thread;
835     ThreadSP thread_sp;
836     ProcessSP process_sp(GetSP());
837     if (process_sp)
838     {
839         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
840         Process::StopLocker stop_locker;
841         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
842         thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
843         sb_thread.SetThread (thread_sp);
844     }
845 
846     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
847     if (log)
848     {
849         log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
850                      process_sp.get(),
851                      index_id,
852                      thread_sp.get());
853     }
854 
855     return sb_thread;
856 }
857 
858 StateType
859 SBProcess::GetStateFromEvent (const SBEvent &event)
860 {
861     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
862 
863     StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
864 
865     if (log)
866         log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(),
867                      lldb_private::StateAsCString (ret_val));
868 
869     return ret_val;
870 }
871 
872 bool
873 SBProcess::GetRestartedFromEvent (const SBEvent &event)
874 {
875     return Process::ProcessEventData::GetRestartedFromEvent (event.get());
876 }
877 
878 SBProcess
879 SBProcess::GetProcessFromEvent (const SBEvent &event)
880 {
881     SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
882     return process;
883 }
884 
885 bool
886 SBProcess::EventIsProcessEvent (const SBEvent &event)
887 {
888     return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0;
889 }
890 
891 SBBroadcaster
892 SBProcess::GetBroadcaster () const
893 {
894     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
895 
896     ProcessSP process_sp(GetSP());
897 
898     SBBroadcaster broadcaster(process_sp.get(), false);
899 
900     if (log)
901         log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",  process_sp.get(),
902                      broadcaster.get());
903 
904     return broadcaster;
905 }
906 
907 const char *
908 SBProcess::GetBroadcasterClass ()
909 {
910     return Process::GetStaticBroadcasterClass().AsCString();
911 }
912 
913 size_t
914 SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
915 {
916     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
917 
918     size_t bytes_read = 0;
919 
920     ProcessSP process_sp(GetSP());
921 
922     if (log)
923     {
924         log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
925                      process_sp.get(),
926                      addr,
927                      dst,
928                      (uint64_t)dst_len,
929                      sb_error.get());
930     }
931 
932     if (process_sp)
933     {
934         Process::StopLocker stop_locker;
935         if (stop_locker.TryLock(&process_sp->GetRunLock()))
936         {
937             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
938             bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
939         }
940         else
941         {
942             if (log)
943                 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get());
944             sb_error.SetErrorString("process is running");
945         }
946     }
947     else
948     {
949         sb_error.SetErrorString ("SBProcess is invalid");
950     }
951 
952     if (log)
953     {
954         SBStream sstr;
955         sb_error.GetDescription (sstr);
956         log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
957                      process_sp.get(),
958                      addr,
959                      dst,
960                      (uint64_t)dst_len,
961                      sb_error.get(),
962                      sstr.GetData(),
963                      (uint64_t)bytes_read);
964     }
965 
966     return bytes_read;
967 }
968 
969 size_t
970 SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
971 {
972     size_t bytes_read = 0;
973     ProcessSP process_sp(GetSP());
974     if (process_sp)
975     {
976         Process::StopLocker stop_locker;
977         if (stop_locker.TryLock(&process_sp->GetRunLock()))
978         {
979             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
980             bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
981         }
982         else
983         {
984             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
985             if (log)
986                 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get());
987             sb_error.SetErrorString("process is running");
988         }
989     }
990     else
991     {
992         sb_error.SetErrorString ("SBProcess is invalid");
993     }
994     return bytes_read;
995 }
996 
997 uint64_t
998 SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
999 {
1000     uint64_t value = 0;
1001     ProcessSP process_sp(GetSP());
1002     if (process_sp)
1003     {
1004         Process::StopLocker stop_locker;
1005         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1006         {
1007             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1008             value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
1009         }
1010         else
1011         {
1012             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1013             if (log)
1014                 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get());
1015             sb_error.SetErrorString("process is running");
1016         }
1017     }
1018     else
1019     {
1020         sb_error.SetErrorString ("SBProcess is invalid");
1021     }
1022     return value;
1023 }
1024 
1025 lldb::addr_t
1026 SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
1027 {
1028     lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
1029     ProcessSP process_sp(GetSP());
1030     if (process_sp)
1031     {
1032         Process::StopLocker stop_locker;
1033         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1034         {
1035             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1036             ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
1037         }
1038         else
1039         {
1040             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1041             if (log)
1042                 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get());
1043             sb_error.SetErrorString("process is running");
1044         }
1045     }
1046     else
1047     {
1048         sb_error.SetErrorString ("SBProcess is invalid");
1049     }
1050     return ptr;
1051 }
1052 
1053 size_t
1054 SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
1055 {
1056     size_t bytes_written = 0;
1057 
1058     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1059 
1060     ProcessSP process_sp(GetSP());
1061 
1062     if (log)
1063     {
1064         log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
1065                      process_sp.get(),
1066                      addr,
1067                      src,
1068                      (uint64_t)src_len,
1069                      sb_error.get());
1070     }
1071 
1072     if (process_sp)
1073     {
1074         Process::StopLocker stop_locker;
1075         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1076         {
1077             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1078             bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
1079         }
1080         else
1081         {
1082             if (log)
1083                 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get());
1084             sb_error.SetErrorString("process is running");
1085         }
1086     }
1087 
1088     if (log)
1089     {
1090         SBStream sstr;
1091         sb_error.GetDescription (sstr);
1092         log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
1093                      process_sp.get(),
1094                      addr,
1095                      src,
1096                      (uint64_t)src_len,
1097                      sb_error.get(),
1098                      sstr.GetData(),
1099                      (uint64_t)bytes_written);
1100     }
1101 
1102     return bytes_written;
1103 }
1104 
1105 bool
1106 SBProcess::GetDescription (SBStream &description)
1107 {
1108     Stream &strm = description.ref();
1109 
1110     ProcessSP process_sp(GetSP());
1111     if (process_sp)
1112     {
1113         char path[PATH_MAX];
1114         GetTarget().GetExecutable().GetPath (path, sizeof(path));
1115         Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
1116         const char *exe_name = NULL;
1117         if (exe_module)
1118             exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
1119 
1120         strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
1121                      process_sp->GetID(),
1122                      lldb_private::StateAsCString (GetState()),
1123                      GetNumThreads(),
1124                      exe_name ? ", executable = " : "",
1125                      exe_name ? exe_name : "");
1126     }
1127     else
1128         strm.PutCString ("No value");
1129 
1130     return true;
1131 }
1132 
1133 uint32_t
1134 SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
1135 {
1136     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1137 
1138     uint32_t num = 0;
1139     ProcessSP process_sp(GetSP());
1140     if (process_sp)
1141     {
1142         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1143         sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
1144         if (log)
1145             log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
1146                          process_sp.get(), num);
1147     }
1148     else
1149     {
1150         sb_error.SetErrorString ("SBProcess is invalid");
1151     }
1152     return num;
1153 }
1154 
1155 uint32_t
1156 SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error)
1157 {
1158     ProcessSP process_sp(GetSP());
1159     if (process_sp)
1160     {
1161         Process::StopLocker stop_locker;
1162         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1163         {
1164             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1165             return process_sp->LoadImage (*sb_image_spec, sb_error.ref());
1166         }
1167         else
1168         {
1169             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1170             if (log)
1171                 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get());
1172             sb_error.SetErrorString("process is running");
1173         }
1174     }
1175     return LLDB_INVALID_IMAGE_TOKEN;
1176 }
1177 
1178 lldb::SBError
1179 SBProcess::UnloadImage (uint32_t image_token)
1180 {
1181     lldb::SBError sb_error;
1182     ProcessSP process_sp(GetSP());
1183     if (process_sp)
1184     {
1185         Process::StopLocker stop_locker;
1186         if (stop_locker.TryLock(&process_sp->GetRunLock()))
1187         {
1188             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
1189             sb_error.SetError (process_sp->UnloadImage (image_token));
1190         }
1191         else
1192         {
1193             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1194             if (log)
1195                 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get());
1196             sb_error.SetErrorString("process is running");
1197         }
1198     }
1199     else
1200         sb_error.SetErrorString("invalid process");
1201     return sb_error;
1202 }
1203