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