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