Home | History | Annotate | Download | only in API
      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().GetCString();
     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->GetPluginName().GetCString();
    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     Log *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->GetPlatformFileSpec(), 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     Log *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     Log *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     Log *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 SBThread
    282 SBProcess::CreateOSPluginThread (lldb::tid_t tid, lldb::addr_t context)
    283 {
    284     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    285 
    286     SBThread sb_thread;
    287     ThreadSP thread_sp;
    288     ProcessSP process_sp(GetSP());
    289     if (process_sp)
    290     {
    291         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    292         thread_sp = process_sp->CreateOSPluginThread(tid, context);
    293         sb_thread.SetThread (thread_sp);
    294     }
    295 
    296     if (log)
    297         log->Printf ("SBProcess(%p)::CreateOSPluginThread (tid=0x%" PRIx64 ", context=0x%" PRIx64 ") => SBThread(%p)", process_sp.get(), tid, context, thread_sp.get());
    298 
    299     return sb_thread;
    300 }
    301 
    302 SBTarget
    303 SBProcess::GetTarget() const
    304 {
    305     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    306 
    307     SBTarget sb_target;
    308     TargetSP target_sp;
    309     ProcessSP process_sp(GetSP());
    310     if (process_sp)
    311     {
    312         target_sp = process_sp->GetTarget().shared_from_this();
    313         sb_target.SetSP (target_sp);
    314     }
    315 
    316     if (log)
    317         log->Printf ("SBProcess(%p)::GetTarget () => SBTarget(%p)", process_sp.get(), target_sp.get());
    318 
    319     return sb_target;
    320 }
    321 
    322 
    323 size_t
    324 SBProcess::PutSTDIN (const char *src, size_t src_len)
    325 {
    326     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    327 
    328     size_t ret_val = 0;
    329     ProcessSP process_sp(GetSP());
    330     if (process_sp)
    331     {
    332         Error error;
    333         ret_val =  process_sp->PutSTDIN (src, src_len, error);
    334     }
    335 
    336     if (log)
    337         log->Printf ("SBProcess(%p)::PutSTDIN (src=\"%s\", src_len=%d) => %lu",
    338                      process_sp.get(),
    339                      src,
    340                      (uint32_t) src_len,
    341                      ret_val);
    342 
    343     return ret_val;
    344 }
    345 
    346 size_t
    347 SBProcess::GetSTDOUT (char *dst, size_t dst_len) const
    348 {
    349     size_t bytes_read = 0;
    350     ProcessSP process_sp(GetSP());
    351     if (process_sp)
    352     {
    353         Error error;
    354         bytes_read = process_sp->GetSTDOUT (dst, dst_len, error);
    355     }
    356 
    357     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    358     if (log)
    359         log->Printf ("SBProcess(%p)::GetSTDOUT (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
    360                      process_sp.get(),
    361                      (int) bytes_read,
    362                      dst,
    363                      (uint64_t)dst_len,
    364                      (uint64_t)bytes_read);
    365 
    366     return bytes_read;
    367 }
    368 
    369 size_t
    370 SBProcess::GetSTDERR (char *dst, size_t dst_len) const
    371 {
    372     size_t bytes_read = 0;
    373     ProcessSP process_sp(GetSP());
    374     if (process_sp)
    375     {
    376         Error error;
    377         bytes_read = process_sp->GetSTDERR (dst, dst_len, error);
    378     }
    379 
    380     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    381     if (log)
    382         log->Printf ("SBProcess(%p)::GetSTDERR (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
    383                      process_sp.get(),
    384                      (int) bytes_read,
    385                      dst,
    386                      (uint64_t)dst_len,
    387                      (uint64_t)bytes_read);
    388 
    389     return bytes_read;
    390 }
    391 
    392 size_t
    393 SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const
    394 {
    395     size_t bytes_read = 0;
    396     ProcessSP process_sp(GetSP());
    397     if (process_sp)
    398     {
    399         Error error;
    400         bytes_read = process_sp->GetAsyncProfileData (dst, dst_len, error);
    401     }
    402 
    403     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    404     if (log)
    405         log->Printf ("SBProcess(%p)::GetProfileData (dst=\"%.*s\", dst_len=%" PRIu64 ") => %" PRIu64,
    406                      process_sp.get(),
    407                      (int) bytes_read,
    408                      dst,
    409                      (uint64_t)dst_len,
    410                      (uint64_t)bytes_read);
    411 
    412     return bytes_read;
    413 }
    414 
    415 void
    416 SBProcess::ReportEventState (const SBEvent &event, FILE *out) const
    417 {
    418     if (out == NULL)
    419         return;
    420 
    421     ProcessSP process_sp(GetSP());
    422     if (process_sp)
    423     {
    424         const StateType event_state = SBProcess::GetStateFromEvent (event);
    425         char message[1024];
    426         int message_len = ::snprintf (message,
    427                                       sizeof (message),
    428                                       "Process %" PRIu64 " %s\n",
    429                                       process_sp->GetID(),
    430                                       SBDebugger::StateAsCString (event_state));
    431 
    432         if (message_len > 0)
    433             ::fwrite (message, 1, message_len, out);
    434     }
    435 }
    436 
    437 void
    438 SBProcess::AppendEventStateReport (const SBEvent &event, SBCommandReturnObject &result)
    439 {
    440     ProcessSP process_sp(GetSP());
    441     if (process_sp)
    442     {
    443         const StateType event_state = SBProcess::GetStateFromEvent (event);
    444         char message[1024];
    445         ::snprintf (message,
    446                     sizeof (message),
    447                     "Process %" PRIu64 " %s\n",
    448                     process_sp->GetID(),
    449                     SBDebugger::StateAsCString (event_state));
    450 
    451         result.AppendMessage (message);
    452     }
    453 }
    454 
    455 bool
    456 SBProcess::SetSelectedThread (const SBThread &thread)
    457 {
    458     ProcessSP process_sp(GetSP());
    459     if (process_sp)
    460     {
    461         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    462         return process_sp->GetThreadList().SetSelectedThreadByID (thread.GetThreadID());
    463     }
    464     return false;
    465 }
    466 
    467 bool
    468 SBProcess::SetSelectedThreadByID (lldb::tid_t tid)
    469 {
    470     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    471 
    472     bool ret_val = false;
    473     ProcessSP process_sp(GetSP());
    474     if (process_sp)
    475     {
    476         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    477         ret_val = process_sp->GetThreadList().SetSelectedThreadByID (tid);
    478     }
    479 
    480     if (log)
    481         log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%4.4" PRIx64 ") => %s",
    482                      process_sp.get(), tid, (ret_val ? "true" : "false"));
    483 
    484     return ret_val;
    485 }
    486 
    487 bool
    488 SBProcess::SetSelectedThreadByIndexID (uint32_t index_id)
    489 {
    490     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    491 
    492     bool ret_val = false;
    493     ProcessSP process_sp(GetSP());
    494     if (process_sp)
    495     {
    496         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    497         ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID (index_id);
    498     }
    499 
    500     if (log)
    501         log->Printf ("SBProcess(%p)::SetSelectedThreadByID (tid=0x%x) => %s",
    502                      process_sp.get(), index_id, (ret_val ? "true" : "false"));
    503 
    504     return ret_val;
    505 }
    506 
    507 SBThread
    508 SBProcess::GetThreadAtIndex (size_t index)
    509 {
    510     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    511 
    512     SBThread sb_thread;
    513     ThreadSP thread_sp;
    514     ProcessSP process_sp(GetSP());
    515     if (process_sp)
    516     {
    517         Process::StopLocker stop_locker;
    518         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
    519         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    520         thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);
    521         sb_thread.SetThread (thread_sp);
    522     }
    523 
    524     if (log)
    525     {
    526         log->Printf ("SBProcess(%p)::GetThreadAtIndex (index=%d) => SBThread(%p)",
    527                      process_sp.get(), (uint32_t) index, thread_sp.get());
    528     }
    529 
    530     return sb_thread;
    531 }
    532 
    533 uint32_t
    534 SBProcess::GetStopID(bool include_expression_stops)
    535 {
    536     ProcessSP process_sp(GetSP());
    537     if (process_sp)
    538     {
    539         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    540         if (include_expression_stops)
    541             return process_sp->GetStopID();
    542         else
    543             return process_sp->GetLastNaturalStopID();
    544     }
    545     return 0;
    546 }
    547 
    548 StateType
    549 SBProcess::GetState ()
    550 {
    551 
    552     StateType ret_val = eStateInvalid;
    553     ProcessSP process_sp(GetSP());
    554     if (process_sp)
    555     {
    556         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    557         ret_val = process_sp->GetState();
    558     }
    559 
    560     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    561     if (log)
    562         log->Printf ("SBProcess(%p)::GetState () => %s",
    563                      process_sp.get(),
    564                      lldb_private::StateAsCString (ret_val));
    565 
    566     return ret_val;
    567 }
    568 
    569 
    570 int
    571 SBProcess::GetExitStatus ()
    572 {
    573     int exit_status = 0;
    574     ProcessSP process_sp(GetSP());
    575     if (process_sp)
    576     {
    577         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    578         exit_status = process_sp->GetExitStatus ();
    579     }
    580     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    581     if (log)
    582         log->Printf ("SBProcess(%p)::GetExitStatus () => %i (0x%8.8x)",
    583                      process_sp.get(), exit_status, exit_status);
    584 
    585     return exit_status;
    586 }
    587 
    588 const char *
    589 SBProcess::GetExitDescription ()
    590 {
    591     const char *exit_desc = NULL;
    592     ProcessSP process_sp(GetSP());
    593     if (process_sp)
    594     {
    595         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    596         exit_desc = process_sp->GetExitDescription ();
    597     }
    598     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    599     if (log)
    600         log->Printf ("SBProcess(%p)::GetExitDescription () => %s",
    601                      process_sp.get(), exit_desc);
    602     return exit_desc;
    603 }
    604 
    605 lldb::pid_t
    606 SBProcess::GetProcessID ()
    607 {
    608     lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;
    609     ProcessSP process_sp(GetSP());
    610     if (process_sp)
    611         ret_val = process_sp->GetID();
    612 
    613     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    614     if (log)
    615         log->Printf ("SBProcess(%p)::GetProcessID () => %" PRIu64, process_sp.get(), ret_val);
    616 
    617     return ret_val;
    618 }
    619 
    620 uint32_t
    621 SBProcess::GetUniqueID()
    622 {
    623     uint32_t ret_val = 0;
    624     ProcessSP process_sp(GetSP());
    625     if (process_sp)
    626         ret_val = process_sp->GetUniqueID();
    627     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    628     if (log)
    629         log->Printf ("SBProcess(%p)::GetUniqueID () => %" PRIu32, process_sp.get(), ret_val);
    630     return ret_val;
    631 }
    632 
    633 ByteOrder
    634 SBProcess::GetByteOrder () const
    635 {
    636     ByteOrder byteOrder = eByteOrderInvalid;
    637     ProcessSP process_sp(GetSP());
    638     if (process_sp)
    639         byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();
    640 
    641     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    642     if (log)
    643         log->Printf ("SBProcess(%p)::GetByteOrder () => %d", process_sp.get(), byteOrder);
    644 
    645     return byteOrder;
    646 }
    647 
    648 uint32_t
    649 SBProcess::GetAddressByteSize () const
    650 {
    651     uint32_t size = 0;
    652     ProcessSP process_sp(GetSP());
    653     if (process_sp)
    654         size =  process_sp->GetTarget().GetArchitecture().GetAddressByteSize();
    655 
    656     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    657     if (log)
    658         log->Printf ("SBProcess(%p)::GetAddressByteSize () => %d", process_sp.get(), size);
    659 
    660     return size;
    661 }
    662 
    663 SBError
    664 SBProcess::Continue ()
    665 {
    666     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    667 
    668     SBError sb_error;
    669     ProcessSP process_sp(GetSP());
    670 
    671     if (log)
    672         log->Printf ("SBProcess(%p)::Continue ()...", process_sp.get());
    673 
    674     if (process_sp)
    675     {
    676         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    677 
    678         Error error (process_sp->Resume());
    679         if (error.Success())
    680         {
    681             if (process_sp->GetTarget().GetDebugger().GetAsyncExecution () == false)
    682             {
    683                 if (log)
    684                     log->Printf ("SBProcess(%p)::Continue () waiting for process to stop...", process_sp.get());
    685                 process_sp->WaitForProcessToStop (NULL);
    686             }
    687         }
    688         sb_error.SetError(error);
    689     }
    690     else
    691         sb_error.SetErrorString ("SBProcess is invalid");
    692 
    693     if (log)
    694     {
    695         SBStream sstr;
    696         sb_error.GetDescription (sstr);
    697         log->Printf ("SBProcess(%p)::Continue () => SBError (%p): %s", process_sp.get(), sb_error.get(), sstr.GetData());
    698     }
    699 
    700     return sb_error;
    701 }
    702 
    703 
    704 SBError
    705 SBProcess::Destroy ()
    706 {
    707     SBError sb_error;
    708     ProcessSP process_sp(GetSP());
    709     if (process_sp)
    710     {
    711         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    712         sb_error.SetError(process_sp->Destroy());
    713     }
    714     else
    715         sb_error.SetErrorString ("SBProcess is invalid");
    716 
    717     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    718     if (log)
    719     {
    720         SBStream sstr;
    721         sb_error.GetDescription (sstr);
    722         log->Printf ("SBProcess(%p)::Destroy () => SBError (%p): %s",
    723                      process_sp.get(),
    724                      sb_error.get(),
    725                      sstr.GetData());
    726     }
    727 
    728     return sb_error;
    729 }
    730 
    731 
    732 SBError
    733 SBProcess::Stop ()
    734 {
    735     SBError sb_error;
    736     ProcessSP process_sp(GetSP());
    737     if (process_sp)
    738     {
    739         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    740         sb_error.SetError (process_sp->Halt());
    741     }
    742     else
    743         sb_error.SetErrorString ("SBProcess is invalid");
    744 
    745     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    746     if (log)
    747     {
    748         SBStream sstr;
    749         sb_error.GetDescription (sstr);
    750         log->Printf ("SBProcess(%p)::Stop () => SBError (%p): %s",
    751                      process_sp.get(),
    752                      sb_error.get(),
    753                      sstr.GetData());
    754     }
    755 
    756     return sb_error;
    757 }
    758 
    759 SBError
    760 SBProcess::Kill ()
    761 {
    762     SBError sb_error;
    763     ProcessSP process_sp(GetSP());
    764     if (process_sp)
    765     {
    766         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    767         sb_error.SetError (process_sp->Destroy());
    768     }
    769     else
    770         sb_error.SetErrorString ("SBProcess is invalid");
    771 
    772     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    773     if (log)
    774     {
    775         SBStream sstr;
    776         sb_error.GetDescription (sstr);
    777         log->Printf ("SBProcess(%p)::Kill () => SBError (%p): %s",
    778                      process_sp.get(),
    779                      sb_error.get(),
    780                      sstr.GetData());
    781     }
    782 
    783     return sb_error;
    784 }
    785 
    786 SBError
    787 SBProcess::Detach ()
    788 {
    789     // FIXME: This should come from a process default.
    790     bool keep_stopped = false;
    791     return Detach (keep_stopped);
    792 }
    793 
    794 SBError
    795 SBProcess::Detach (bool keep_stopped)
    796 {
    797     SBError sb_error;
    798     ProcessSP process_sp(GetSP());
    799     if (process_sp)
    800     {
    801         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    802         sb_error.SetError (process_sp->Detach(keep_stopped));
    803     }
    804     else
    805         sb_error.SetErrorString ("SBProcess is invalid");
    806 
    807     return sb_error;
    808 }
    809 
    810 SBError
    811 SBProcess::Signal (int signo)
    812 {
    813     SBError sb_error;
    814     ProcessSP process_sp(GetSP());
    815     if (process_sp)
    816     {
    817         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    818         sb_error.SetError (process_sp->Signal (signo));
    819     }
    820     else
    821         sb_error.SetErrorString ("SBProcess is invalid");
    822     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    823     if (log)
    824     {
    825         SBStream sstr;
    826         sb_error.GetDescription (sstr);
    827         log->Printf ("SBProcess(%p)::Signal (signo=%i) => SBError (%p): %s",
    828                      process_sp.get(),
    829                      signo,
    830                      sb_error.get(),
    831                      sstr.GetData());
    832     }
    833     return sb_error;
    834 }
    835 
    836 void
    837 SBProcess::SendAsyncInterrupt ()
    838 {
    839     ProcessSP process_sp(GetSP());
    840     if (process_sp)
    841     {
    842         process_sp->SendAsyncInterrupt ();
    843     }
    844 }
    845 
    846 SBThread
    847 SBProcess::GetThreadByID (tid_t tid)
    848 {
    849     SBThread sb_thread;
    850     ThreadSP thread_sp;
    851     ProcessSP process_sp(GetSP());
    852     if (process_sp)
    853     {
    854         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    855         Process::StopLocker stop_locker;
    856         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
    857         thread_sp = process_sp->GetThreadList().FindThreadByID (tid, can_update);
    858         sb_thread.SetThread (thread_sp);
    859     }
    860 
    861     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    862     if (log)
    863     {
    864         log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%4.4" PRIx64 ") => SBThread (%p)",
    865                      process_sp.get(),
    866                      tid,
    867                      thread_sp.get());
    868     }
    869 
    870     return sb_thread;
    871 }
    872 
    873 SBThread
    874 SBProcess::GetThreadByIndexID (uint32_t index_id)
    875 {
    876     SBThread sb_thread;
    877     ThreadSP thread_sp;
    878     ProcessSP process_sp(GetSP());
    879     if (process_sp)
    880     {
    881         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    882         Process::StopLocker stop_locker;
    883         const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());
    884         thread_sp = process_sp->GetThreadList().FindThreadByIndexID (index_id, can_update);
    885         sb_thread.SetThread (thread_sp);
    886     }
    887 
    888     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    889     if (log)
    890     {
    891         log->Printf ("SBProcess(%p)::GetThreadByID (tid=0x%x) => SBThread (%p)",
    892                      process_sp.get(),
    893                      index_id,
    894                      thread_sp.get());
    895     }
    896 
    897     return sb_thread;
    898 }
    899 
    900 StateType
    901 SBProcess::GetStateFromEvent (const SBEvent &event)
    902 {
    903     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    904 
    905     StateType ret_val = Process::ProcessEventData::GetStateFromEvent (event.get());
    906 
    907     if (log)
    908         log->Printf ("SBProcess::GetStateFromEvent (event.sp=%p) => %s", event.get(),
    909                      lldb_private::StateAsCString (ret_val));
    910 
    911     return ret_val;
    912 }
    913 
    914 bool
    915 SBProcess::GetRestartedFromEvent (const SBEvent &event)
    916 {
    917     return Process::ProcessEventData::GetRestartedFromEvent (event.get());
    918 }
    919 
    920 size_t
    921 SBProcess::GetNumRestartedReasonsFromEvent (const lldb::SBEvent &event)
    922 {
    923     return Process::ProcessEventData::GetNumRestartedReasons(event.get());
    924 }
    925 
    926 const char *
    927 SBProcess::GetRestartedReasonAtIndexFromEvent (const lldb::SBEvent &event, size_t idx)
    928 {
    929     return Process::ProcessEventData::GetRestartedReasonAtIndex(event.get(), idx);
    930 }
    931 
    932 SBProcess
    933 SBProcess::GetProcessFromEvent (const SBEvent &event)
    934 {
    935     SBProcess process(Process::ProcessEventData::GetProcessFromEvent (event.get()));
    936     return process;
    937 }
    938 
    939 bool
    940 SBProcess::EventIsProcessEvent (const SBEvent &event)
    941 {
    942     return strcmp (event.GetBroadcasterClass(), SBProcess::GetBroadcasterClass()) == 0;
    943 }
    944 
    945 SBBroadcaster
    946 SBProcess::GetBroadcaster () const
    947 {
    948     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    949 
    950     ProcessSP process_sp(GetSP());
    951 
    952     SBBroadcaster broadcaster(process_sp.get(), false);
    953 
    954     if (log)
    955         log->Printf ("SBProcess(%p)::GetBroadcaster () => SBBroadcaster (%p)",  process_sp.get(),
    956                      broadcaster.get());
    957 
    958     return broadcaster;
    959 }
    960 
    961 const char *
    962 SBProcess::GetBroadcasterClass ()
    963 {
    964     return Process::GetStaticBroadcasterClass().AsCString();
    965 }
    966 
    967 size_t
    968 SBProcess::ReadMemory (addr_t addr, void *dst, size_t dst_len, SBError &sb_error)
    969 {
    970     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
    971 
    972     size_t bytes_read = 0;
    973 
    974     ProcessSP process_sp(GetSP());
    975 
    976     if (log)
    977     {
    978         log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p))...",
    979                      process_sp.get(),
    980                      addr,
    981                      dst,
    982                      (uint64_t)dst_len,
    983                      sb_error.get());
    984     }
    985 
    986     if (process_sp)
    987     {
    988         Process::StopLocker stop_locker;
    989         if (stop_locker.TryLock(&process_sp->GetRunLock()))
    990         {
    991             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
    992             bytes_read = process_sp->ReadMemory (addr, dst, dst_len, sb_error.ref());
    993         }
    994         else
    995         {
    996             if (log)
    997                 log->Printf ("SBProcess(%p)::ReadMemory() => error: process is running", process_sp.get());
    998             sb_error.SetErrorString("process is running");
    999         }
   1000     }
   1001     else
   1002     {
   1003         sb_error.SetErrorString ("SBProcess is invalid");
   1004     }
   1005 
   1006     if (log)
   1007     {
   1008         SBStream sstr;
   1009         sb_error.GetDescription (sstr);
   1010         log->Printf ("SBProcess(%p)::ReadMemory (addr=0x%" PRIx64 ", dst=%p, dst_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
   1011                      process_sp.get(),
   1012                      addr,
   1013                      dst,
   1014                      (uint64_t)dst_len,
   1015                      sb_error.get(),
   1016                      sstr.GetData(),
   1017                      (uint64_t)bytes_read);
   1018     }
   1019 
   1020     return bytes_read;
   1021 }
   1022 
   1023 size_t
   1024 SBProcess::ReadCStringFromMemory (addr_t addr, void *buf, size_t size, lldb::SBError &sb_error)
   1025 {
   1026     size_t bytes_read = 0;
   1027     ProcessSP process_sp(GetSP());
   1028     if (process_sp)
   1029     {
   1030         Process::StopLocker stop_locker;
   1031         if (stop_locker.TryLock(&process_sp->GetRunLock()))
   1032         {
   1033             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1034             bytes_read = process_sp->ReadCStringFromMemory (addr, (char *)buf, size, sb_error.ref());
   1035         }
   1036         else
   1037         {
   1038             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1039             if (log)
   1040                 log->Printf ("SBProcess(%p)::ReadCStringFromMemory() => error: process is running", process_sp.get());
   1041             sb_error.SetErrorString("process is running");
   1042         }
   1043     }
   1044     else
   1045     {
   1046         sb_error.SetErrorString ("SBProcess is invalid");
   1047     }
   1048     return bytes_read;
   1049 }
   1050 
   1051 uint64_t
   1052 SBProcess::ReadUnsignedFromMemory (addr_t addr, uint32_t byte_size, lldb::SBError &sb_error)
   1053 {
   1054     uint64_t value = 0;
   1055     ProcessSP process_sp(GetSP());
   1056     if (process_sp)
   1057     {
   1058         Process::StopLocker stop_locker;
   1059         if (stop_locker.TryLock(&process_sp->GetRunLock()))
   1060         {
   1061             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1062             value = process_sp->ReadUnsignedIntegerFromMemory (addr, byte_size, 0, sb_error.ref());
   1063         }
   1064         else
   1065         {
   1066             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1067             if (log)
   1068                 log->Printf ("SBProcess(%p)::ReadUnsignedFromMemory() => error: process is running", process_sp.get());
   1069             sb_error.SetErrorString("process is running");
   1070         }
   1071     }
   1072     else
   1073     {
   1074         sb_error.SetErrorString ("SBProcess is invalid");
   1075     }
   1076     return value;
   1077 }
   1078 
   1079 lldb::addr_t
   1080 SBProcess::ReadPointerFromMemory (addr_t addr, lldb::SBError &sb_error)
   1081 {
   1082     lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
   1083     ProcessSP process_sp(GetSP());
   1084     if (process_sp)
   1085     {
   1086         Process::StopLocker stop_locker;
   1087         if (stop_locker.TryLock(&process_sp->GetRunLock()))
   1088         {
   1089             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1090             ptr = process_sp->ReadPointerFromMemory (addr, sb_error.ref());
   1091         }
   1092         else
   1093         {
   1094             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1095             if (log)
   1096                 log->Printf ("SBProcess(%p)::ReadPointerFromMemory() => error: process is running", process_sp.get());
   1097             sb_error.SetErrorString("process is running");
   1098         }
   1099     }
   1100     else
   1101     {
   1102         sb_error.SetErrorString ("SBProcess is invalid");
   1103     }
   1104     return ptr;
   1105 }
   1106 
   1107 size_t
   1108 SBProcess::WriteMemory (addr_t addr, const void *src, size_t src_len, SBError &sb_error)
   1109 {
   1110     size_t bytes_written = 0;
   1111 
   1112     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1113 
   1114     ProcessSP process_sp(GetSP());
   1115 
   1116     if (log)
   1117     {
   1118         log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p))...",
   1119                      process_sp.get(),
   1120                      addr,
   1121                      src,
   1122                      (uint64_t)src_len,
   1123                      sb_error.get());
   1124     }
   1125 
   1126     if (process_sp)
   1127     {
   1128         Process::StopLocker stop_locker;
   1129         if (stop_locker.TryLock(&process_sp->GetRunLock()))
   1130         {
   1131             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1132             bytes_written = process_sp->WriteMemory (addr, src, src_len, sb_error.ref());
   1133         }
   1134         else
   1135         {
   1136             if (log)
   1137                 log->Printf ("SBProcess(%p)::WriteMemory() => error: process is running", process_sp.get());
   1138             sb_error.SetErrorString("process is running");
   1139         }
   1140     }
   1141 
   1142     if (log)
   1143     {
   1144         SBStream sstr;
   1145         sb_error.GetDescription (sstr);
   1146         log->Printf ("SBProcess(%p)::WriteMemory (addr=0x%" PRIx64 ", src=%p, src_len=%" PRIu64 ", SBError (%p): %s) => %" PRIu64,
   1147                      process_sp.get(),
   1148                      addr,
   1149                      src,
   1150                      (uint64_t)src_len,
   1151                      sb_error.get(),
   1152                      sstr.GetData(),
   1153                      (uint64_t)bytes_written);
   1154     }
   1155 
   1156     return bytes_written;
   1157 }
   1158 
   1159 bool
   1160 SBProcess::GetDescription (SBStream &description)
   1161 {
   1162     Stream &strm = description.ref();
   1163 
   1164     ProcessSP process_sp(GetSP());
   1165     if (process_sp)
   1166     {
   1167         char path[PATH_MAX];
   1168         GetTarget().GetExecutable().GetPath (path, sizeof(path));
   1169         Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();
   1170         const char *exe_name = NULL;
   1171         if (exe_module)
   1172             exe_name = exe_module->GetFileSpec().GetFilename().AsCString();
   1173 
   1174         strm.Printf ("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",
   1175                      process_sp->GetID(),
   1176                      lldb_private::StateAsCString (GetState()),
   1177                      GetNumThreads(),
   1178                      exe_name ? ", executable = " : "",
   1179                      exe_name ? exe_name : "");
   1180     }
   1181     else
   1182         strm.PutCString ("No value");
   1183 
   1184     return true;
   1185 }
   1186 
   1187 uint32_t
   1188 SBProcess::GetNumSupportedHardwareWatchpoints (lldb::SBError &sb_error) const
   1189 {
   1190     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1191 
   1192     uint32_t num = 0;
   1193     ProcessSP process_sp(GetSP());
   1194     if (process_sp)
   1195     {
   1196         Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1197         sb_error.SetError(process_sp->GetWatchpointSupportInfo (num));
   1198         if (log)
   1199             log->Printf ("SBProcess(%p)::GetNumSupportedHardwareWatchpoints () => %u",
   1200                          process_sp.get(), num);
   1201     }
   1202     else
   1203     {
   1204         sb_error.SetErrorString ("SBProcess is invalid");
   1205     }
   1206     return num;
   1207 }
   1208 
   1209 uint32_t
   1210 SBProcess::LoadImage (lldb::SBFileSpec &sb_image_spec, lldb::SBError &sb_error)
   1211 {
   1212     ProcessSP process_sp(GetSP());
   1213     if (process_sp)
   1214     {
   1215         Process::StopLocker stop_locker;
   1216         if (stop_locker.TryLock(&process_sp->GetRunLock()))
   1217         {
   1218             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1219             return process_sp->LoadImage (*sb_image_spec, sb_error.ref());
   1220         }
   1221         else
   1222         {
   1223             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1224             if (log)
   1225                 log->Printf ("SBProcess(%p)::LoadImage() => error: process is running", process_sp.get());
   1226             sb_error.SetErrorString("process is running");
   1227         }
   1228     }
   1229     return LLDB_INVALID_IMAGE_TOKEN;
   1230 }
   1231 
   1232 lldb::SBError
   1233 SBProcess::UnloadImage (uint32_t image_token)
   1234 {
   1235     lldb::SBError sb_error;
   1236     ProcessSP process_sp(GetSP());
   1237     if (process_sp)
   1238     {
   1239         Process::StopLocker stop_locker;
   1240         if (stop_locker.TryLock(&process_sp->GetRunLock()))
   1241         {
   1242             Mutex::Locker api_locker (process_sp->GetTarget().GetAPIMutex());
   1243             sb_error.SetError (process_sp->UnloadImage (image_token));
   1244         }
   1245         else
   1246         {
   1247             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
   1248             if (log)
   1249                 log->Printf ("SBProcess(%p)::UnloadImage() => error: process is running", process_sp.get());
   1250             sb_error.SetErrorString("process is running");
   1251         }
   1252     }
   1253     else
   1254         sb_error.SetErrorString("invalid process");
   1255     return sb_error;
   1256 }
   1257