SBTarget.cpp revision 444fe998bf707bd076a70c3a779db8575533695e
1//===-- SBTarget.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/SBTarget.h"
11
12#include "lldb/lldb-public.h"
13
14#include "lldb/API/SBDebugger.h"
15#include "lldb/API/SBBreakpoint.h"
16#include "lldb/API/SBFileSpec.h"
17#include "lldb/API/SBListener.h"
18#include "lldb/API/SBModule.h"
19#include "lldb/API/SBSourceManager.h"
20#include "lldb/API/SBProcess.h"
21#include "lldb/API/SBStream.h"
22#include "lldb/API/SBSymbolContextList.h"
23#include "lldb/Breakpoint/BreakpointID.h"
24#include "lldb/Breakpoint/BreakpointIDList.h"
25#include "lldb/Breakpoint/BreakpointList.h"
26#include "lldb/Breakpoint/BreakpointLocation.h"
27#include "lldb/Core/Address.h"
28#include "lldb/Core/AddressResolver.h"
29#include "lldb/Core/AddressResolverName.h"
30#include "lldb/Core/ArchSpec.h"
31#include "lldb/Core/Debugger.h"
32#include "lldb/Core/Disassembler.h"
33#include "lldb/Core/Log.h"
34#include "lldb/Core/RegularExpression.h"
35#include "lldb/Core/SearchFilter.h"
36#include "lldb/Core/STLUtils.h"
37#include "lldb/Core/ValueObjectList.h"
38#include "lldb/Core/ValueObjectVariable.h"
39#include "lldb/Host/FileSpec.h"
40#include "lldb/Host/Host.h"
41#include "lldb/Interpreter/Args.h"
42#include "lldb/Symbol/SymbolVendor.h"
43#include "lldb/Symbol/VariableList.h"
44#include "lldb/Target/Process.h"
45#include "lldb/Target/Target.h"
46#include "lldb/Target/TargetList.h"
47
48#include "lldb/Interpreter/CommandReturnObject.h"
49#include "../source/Commands/CommandObjectBreakpoint.h"
50
51
52using namespace lldb;
53using namespace lldb_private;
54
55#define DEFAULT_DISASM_BYTE_SIZE 32
56
57SBLaunchInfo::SBLaunchInfo (const char **argv) :
58    m_opaque_sp(new ProcessLaunchInfo())
59{
60    m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
61    if (argv && argv[0])
62        m_opaque_sp->GetArguments().SetArguments(argv);
63}
64
65uint32_t
66SBLaunchInfo::GetUserID()
67{
68    return m_opaque_sp->GetUserID();
69}
70
71uint32_t
72SBLaunchInfo::GetGroupID()
73{
74    return m_opaque_sp->GetGroupID();
75}
76
77bool
78SBLaunchInfo::UserIDIsValid ()
79{
80    return m_opaque_sp->UserIDIsValid();
81}
82
83bool
84SBLaunchInfo::GroupIDIsValid ()
85{
86    return m_opaque_sp->GroupIDIsValid();
87}
88
89void
90SBLaunchInfo::SetUserID (uint32_t uid)
91{
92    m_opaque_sp->SetUserID (uid);
93}
94
95void
96SBLaunchInfo::SetGroupID (uint32_t gid)
97{
98    m_opaque_sp->SetGroupID (gid);
99}
100
101uint32_t
102SBLaunchInfo::GetNumArguments ()
103{
104    return m_opaque_sp->GetArguments().GetArgumentCount();
105}
106
107const char *
108SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
109{
110    return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
111}
112
113void
114SBLaunchInfo::SetArguments (const char **argv, bool append)
115{
116    if (append)
117    {
118        if (argv)
119            m_opaque_sp->GetArguments().AppendArguments(argv);
120    }
121    else
122    {
123        if (argv)
124            m_opaque_sp->GetArguments().SetArguments(argv);
125        else
126            m_opaque_sp->GetArguments().Clear();
127    }
128}
129
130uint32_t
131SBLaunchInfo::GetNumEnvironmentEntries ()
132{
133    return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
134}
135
136const char *
137SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
138{
139    return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
140}
141
142void
143SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
144{
145    if (append)
146    {
147        if (envp)
148            m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
149    }
150    else
151    {
152        if (envp)
153            m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
154        else
155            m_opaque_sp->GetEnvironmentEntries().Clear();
156    }
157}
158
159void
160SBLaunchInfo::Clear ()
161{
162    m_opaque_sp->Clear();
163}
164
165const char *
166SBLaunchInfo::GetWorkingDirectory () const
167{
168    return m_opaque_sp->GetWorkingDirectory();
169}
170
171void
172SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
173{
174    m_opaque_sp->SetWorkingDirectory(working_dir);
175}
176
177uint32_t
178SBLaunchInfo::GetLaunchFlags ()
179{
180    return m_opaque_sp->GetFlags().Get();
181}
182
183void
184SBLaunchInfo::SetLaunchFlags (uint32_t flags)
185{
186    m_opaque_sp->GetFlags().Reset(flags);
187}
188
189const char *
190SBLaunchInfo::GetProcessPluginName ()
191{
192    return m_opaque_sp->GetProcessPluginName();
193}
194
195void
196SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
197{
198    return m_opaque_sp->SetProcessPluginName (plugin_name);
199}
200
201const char *
202SBLaunchInfo::GetShell ()
203{
204    return m_opaque_sp->GetShell();
205}
206
207void
208SBLaunchInfo::SetShell (const char * path)
209{
210    m_opaque_sp->SetShell (path);
211}
212
213uint32_t
214SBLaunchInfo::GetResumeCount ()
215{
216    return m_opaque_sp->GetResumeCount();
217}
218
219void
220SBLaunchInfo::SetResumeCount (uint32_t c)
221{
222    m_opaque_sp->SetResumeCount (c);
223}
224
225bool
226SBLaunchInfo::AddCloseFileAction (int fd)
227{
228    return m_opaque_sp->AppendCloseFileAction(fd);
229}
230
231bool
232SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
233{
234    return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
235}
236
237bool
238SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
239{
240    return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
241}
242
243bool
244SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
245{
246    return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
247}
248
249
250SBAttachInfo::SBAttachInfo () :
251m_opaque_sp (new ProcessAttachInfo())
252{
253}
254
255SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
256m_opaque_sp (new ProcessAttachInfo())
257{
258    m_opaque_sp->SetProcessID (pid);
259}
260
261SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
262m_opaque_sp (new ProcessAttachInfo())
263{
264    if (path && path[0])
265        m_opaque_sp->GetExecutableFile().SetFile(path, false);
266    m_opaque_sp->SetWaitForLaunch (wait_for);
267}
268
269SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
270m_opaque_sp (new ProcessAttachInfo())
271{
272    *m_opaque_sp = *rhs.m_opaque_sp;
273}
274
275SBAttachInfo &
276SBAttachInfo::operator = (const SBAttachInfo &rhs)
277{
278    if (this != &rhs)
279        *m_opaque_sp = *rhs.m_opaque_sp;
280    return *this;
281}
282
283lldb::pid_t
284SBAttachInfo::GetProcessID ()
285{
286    return m_opaque_sp->GetProcessID();
287}
288
289void
290SBAttachInfo::SetProcessID (lldb::pid_t pid)
291{
292    m_opaque_sp->SetProcessID (pid);
293}
294
295
296uint32_t
297SBAttachInfo::GetResumeCount ()
298{
299    return m_opaque_sp->GetResumeCount();
300}
301
302void
303SBAttachInfo::SetResumeCount (uint32_t c)
304{
305    m_opaque_sp->SetResumeCount (c);
306}
307
308const char *
309SBAttachInfo::GetProcessPluginName ()
310{
311    return m_opaque_sp->GetProcessPluginName();
312}
313
314void
315SBAttachInfo::SetProcessPluginName (const char *plugin_name)
316{
317    return m_opaque_sp->SetProcessPluginName (plugin_name);
318}
319
320void
321SBAttachInfo::SetExecutable (const char *path)
322{
323    if (path && path[0])
324        m_opaque_sp->GetExecutableFile().SetFile(path, false);
325    else
326        m_opaque_sp->GetExecutableFile().Clear();
327}
328
329void
330SBAttachInfo::SetExecutable (SBFileSpec exe_file)
331{
332    if (exe_file.IsValid())
333        m_opaque_sp->GetExecutableFile() = exe_file.ref();
334    else
335        m_opaque_sp->GetExecutableFile().Clear();
336}
337
338bool
339SBAttachInfo::GetWaitForLaunch ()
340{
341    return m_opaque_sp->GetWaitForLaunch();
342}
343
344void
345SBAttachInfo::SetWaitForLaunch (bool b)
346{
347    m_opaque_sp->SetWaitForLaunch (b);
348}
349
350uint32_t
351SBAttachInfo::GetUserID()
352{
353    return m_opaque_sp->GetUserID();
354}
355
356uint32_t
357SBAttachInfo::GetGroupID()
358{
359    return m_opaque_sp->GetGroupID();
360}
361
362bool
363SBAttachInfo::UserIDIsValid ()
364{
365    return m_opaque_sp->UserIDIsValid();
366}
367
368bool
369SBAttachInfo::GroupIDIsValid ()
370{
371    return m_opaque_sp->GroupIDIsValid();
372}
373
374void
375SBAttachInfo::SetUserID (uint32_t uid)
376{
377    m_opaque_sp->SetUserID (uid);
378}
379
380void
381SBAttachInfo::SetGroupID (uint32_t gid)
382{
383    m_opaque_sp->SetGroupID (gid);
384}
385
386uint32_t
387SBAttachInfo::GetEffectiveUserID()
388{
389    return m_opaque_sp->GetEffectiveUserID();
390}
391
392uint32_t
393SBAttachInfo::GetEffectiveGroupID()
394{
395    return m_opaque_sp->GetEffectiveGroupID();
396}
397
398bool
399SBAttachInfo::EffectiveUserIDIsValid ()
400{
401    return m_opaque_sp->EffectiveUserIDIsValid();
402}
403
404bool
405SBAttachInfo::EffectiveGroupIDIsValid ()
406{
407    return m_opaque_sp->EffectiveGroupIDIsValid ();
408}
409
410void
411SBAttachInfo::SetEffectiveUserID (uint32_t uid)
412{
413    m_opaque_sp->SetEffectiveUserID(uid);
414}
415
416void
417SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
418{
419    m_opaque_sp->SetEffectiveGroupID(gid);
420}
421
422lldb::pid_t
423SBAttachInfo::GetParentProcessID ()
424{
425    return m_opaque_sp->GetParentProcessID();
426}
427
428void
429SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
430{
431    m_opaque_sp->SetParentProcessID (pid);
432}
433
434bool
435SBAttachInfo::ParentProcessIDIsValid()
436{
437    return m_opaque_sp->ParentProcessIDIsValid();
438}
439
440
441//----------------------------------------------------------------------
442// SBTarget constructor
443//----------------------------------------------------------------------
444SBTarget::SBTarget () :
445    m_opaque_sp ()
446{
447}
448
449SBTarget::SBTarget (const SBTarget& rhs) :
450    m_opaque_sp (rhs.m_opaque_sp)
451{
452}
453
454SBTarget::SBTarget(const TargetSP& target_sp) :
455    m_opaque_sp (target_sp)
456{
457}
458
459const SBTarget&
460SBTarget::operator = (const SBTarget& rhs)
461{
462    if (this != &rhs)
463        m_opaque_sp = rhs.m_opaque_sp;
464    return *this;
465}
466
467//----------------------------------------------------------------------
468// Destructor
469//----------------------------------------------------------------------
470SBTarget::~SBTarget()
471{
472}
473
474const char *
475SBTarget::GetBroadcasterClassName ()
476{
477    return Target::GetStaticBroadcasterClass().AsCString();
478}
479
480bool
481SBTarget::IsValid () const
482{
483    return m_opaque_sp.get() != NULL;
484}
485
486SBProcess
487SBTarget::GetProcess ()
488{
489    SBProcess sb_process;
490    ProcessSP process_sp;
491    TargetSP target_sp(GetSP());
492    if (target_sp)
493    {
494        process_sp = target_sp->GetProcessSP();
495        sb_process.SetSP (process_sp);
496    }
497
498    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
499    if (log)
500    {
501        log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
502                     target_sp.get(), process_sp.get());
503    }
504
505    return sb_process;
506}
507
508SBDebugger
509SBTarget::GetDebugger () const
510{
511    SBDebugger debugger;
512    TargetSP target_sp(GetSP());
513    if (target_sp)
514        debugger.reset (target_sp->GetDebugger().shared_from_this());
515    return debugger;
516}
517
518SBProcess
519SBTarget::LaunchSimple
520(
521    char const **argv,
522    char const **envp,
523    const char *working_directory
524)
525{
526    char *stdin_path = NULL;
527    char *stdout_path = NULL;
528    char *stderr_path = NULL;
529    uint32_t launch_flags = 0;
530    bool stop_at_entry = false;
531    SBError error;
532    SBListener listener = GetDebugger().GetListener();
533    return Launch (listener,
534                   argv,
535                   envp,
536                   stdin_path,
537                   stdout_path,
538                   stderr_path,
539                   working_directory,
540                   launch_flags,
541                   stop_at_entry,
542                   error);
543}
544
545SBProcess
546SBTarget::Launch
547(
548    SBListener &listener,
549    char const **argv,
550    char const **envp,
551    const char *stdin_path,
552    const char *stdout_path,
553    const char *stderr_path,
554    const char *working_directory,
555    uint32_t launch_flags,   // See LaunchFlags
556    bool stop_at_entry,
557    lldb::SBError& error
558)
559{
560    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
561
562    SBProcess sb_process;
563    ProcessSP process_sp;
564    TargetSP target_sp(GetSP());
565
566    if (log)
567    {
568        log->Printf ("SBTarget(%p)::Launch (argv=%p, envp=%p, stdin=%s, stdout=%s, stderr=%s, working-dir=%s, launch_flags=0x%x, stop_at_entry=%i, &error (%p))...",
569                     target_sp.get(),
570                     argv,
571                     envp,
572                     stdin_path ? stdin_path : "NULL",
573                     stdout_path ? stdout_path : "NULL",
574                     stderr_path ? stderr_path : "NULL",
575                     working_directory ? working_directory : "NULL",
576                     launch_flags,
577                     stop_at_entry,
578                     error.get());
579    }
580
581    if (target_sp)
582    {
583        Mutex::Locker api_locker (target_sp->GetAPIMutex());
584
585        if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
586            launch_flags |= eLaunchFlagDisableASLR;
587
588        StateType state = eStateInvalid;
589        process_sp = target_sp->GetProcessSP();
590        if (process_sp)
591        {
592            state = process_sp->GetState();
593
594            if (process_sp->IsAlive() && state != eStateConnected)
595            {
596                if (state == eStateAttaching)
597                    error.SetErrorString ("process attach is in progress");
598                else
599                    error.SetErrorString ("a process is already being debugged");
600                return sb_process;
601            }
602        }
603
604        if (state == eStateConnected)
605        {
606            // If we are already connected, then we have already specified the
607            // listener, so if a valid listener is supplied, we need to error out
608            // to let the client know.
609            if (listener.IsValid())
610            {
611                error.SetErrorString ("process is connected and already has a listener, pass empty listener");
612                return sb_process;
613            }
614        }
615        else
616        {
617            if (listener.IsValid())
618                process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
619            else
620                process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
621        }
622
623        if (process_sp)
624        {
625            sb_process.SetSP (process_sp);
626            if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
627                launch_flags |= eLaunchFlagDisableSTDIO;
628
629            ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
630
631            Module *exe_module = target_sp->GetExecutableModulePointer();
632            if (exe_module)
633                launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
634            if (argv)
635                launch_info.GetArguments().AppendArguments (argv);
636            if (envp)
637                launch_info.GetEnvironmentEntries ().SetArguments (envp);
638
639            error.SetError (process_sp->Launch (launch_info));
640            if (error.Success())
641            {
642                // We we are stopping at the entry point, we can return now!
643                if (stop_at_entry)
644                    return sb_process;
645
646                // Make sure we are stopped at the entry
647                StateType state = process_sp->WaitForProcessToStop (NULL);
648                if (state == eStateStopped)
649                {
650                    // resume the process to skip the entry point
651                    error.SetError (process_sp->Resume());
652                    if (error.Success())
653                    {
654                        // If we are doing synchronous mode, then wait for the
655                        // process to stop yet again!
656                        if (target_sp->GetDebugger().GetAsyncExecution () == false)
657                            process_sp->WaitForProcessToStop (NULL);
658                    }
659                }
660            }
661        }
662        else
663        {
664            error.SetErrorString ("unable to create lldb_private::Process");
665        }
666    }
667    else
668    {
669        error.SetErrorString ("SBTarget is invalid");
670    }
671
672    log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
673    if (log)
674    {
675        log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
676                     target_sp.get(), process_sp.get());
677    }
678
679    return sb_process;
680}
681
682SBProcess
683SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
684{
685    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
686
687    SBProcess sb_process;
688    ProcessSP process_sp;
689    TargetSP target_sp(GetSP());
690
691    if (log)
692    {
693        log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
694    }
695
696    if (target_sp)
697    {
698        Mutex::Locker api_locker (target_sp->GetAPIMutex());
699        StateType state = eStateInvalid;
700        process_sp = target_sp->GetProcessSP();
701        if (process_sp)
702        {
703            state = process_sp->GetState();
704
705            if (process_sp->IsAlive() && state != eStateConnected)
706            {
707                if (state == eStateAttaching)
708                    error.SetErrorString ("process attach is in progress");
709                else
710                    error.SetErrorString ("a process is already being debugged");
711                return sb_process;
712            }
713        }
714
715        if (state != eStateConnected)
716            process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
717
718        if (process_sp)
719        {
720            sb_process.SetSP (process_sp);
721            lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
722
723            bool add_exe_as_first_argv = true; //launch_info.GetArguments().GetArgumentCount() == 0;
724            Module *exe_module = target_sp->GetExecutableModulePointer();
725            if (exe_module)
726                launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_as_first_argv);
727
728            const ArchSpec &arch_spec = target_sp->GetArchitecture();
729            if (arch_spec.IsValid())
730                launch_info.GetArchitecture () = arch_spec;
731
732            error.SetError (process_sp->Launch (launch_info));
733            const bool synchronous_execution = target_sp->GetDebugger().GetAsyncExecution () == false;
734            if (error.Success())
735            {
736                StateType state = eStateInvalid;
737                if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
738                {
739                    // If we are doing synchronous mode, then wait for the initial
740                    // stop to happen, else, return and let the caller watch for
741                    // the stop
742                    if (synchronous_execution)
743                         state = process_sp->WaitForProcessToStop (NULL);
744                    // We we are stopping at the entry point, we can return now!
745                    return sb_process;
746                }
747
748                // Make sure we are stopped at the entry
749                state = process_sp->WaitForProcessToStop (NULL);
750                if (state == eStateStopped)
751                {
752                    // resume the process to skip the entry point
753                    error.SetError (process_sp->Resume());
754                    if (error.Success())
755                    {
756                        // If we are doing synchronous mode, then wait for the
757                        // process to stop yet again!
758                        if (synchronous_execution)
759                            process_sp->WaitForProcessToStop (NULL);
760                    }
761                }
762            }
763        }
764        else
765        {
766            error.SetErrorString ("unable to create lldb_private::Process");
767        }
768    }
769    else
770    {
771        error.SetErrorString ("SBTarget is invalid");
772    }
773
774    log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
775    if (log)
776    {
777        log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
778                     target_sp.get(), process_sp.get());
779    }
780
781    return sb_process;
782}
783
784lldb::SBProcess
785SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
786{
787    SBProcess sb_process;
788    ProcessSP process_sp;
789    TargetSP target_sp(GetSP());
790    if (target_sp)
791    {
792        Mutex::Locker api_locker (target_sp->GetAPIMutex());
793
794        StateType state = eStateInvalid;
795        process_sp = target_sp->GetProcessSP();
796        if (process_sp)
797        {
798            state = process_sp->GetState();
799
800            if (process_sp->IsAlive() && state != eStateConnected)
801            {
802                if (state == eStateAttaching)
803                    error.SetErrorString ("process attach is in progress");
804                else
805                    error.SetErrorString ("a process is already being debugged");
806                return sb_process;
807            }
808        }
809
810        if (state != eStateConnected)
811            process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
812
813        if (process_sp)
814        {
815            sb_process.SetSP (process_sp);
816
817            ProcessAttachInfo &attach_info = sb_attach_info.ref();
818            error.SetError (process_sp->Attach (attach_info));
819            // If we are doing synchronous mode, then wait for the
820            // process to stop!
821            if (target_sp->GetDebugger().GetAsyncExecution () == false)
822                process_sp->WaitForProcessToStop (NULL);
823        }
824        else
825        {
826            error.SetErrorString ("unable to create lldb_private::Process");
827        }
828    }
829    else
830    {
831        error.SetErrorString ("SBTarget is invalid");
832    }
833    return sb_process;
834}
835
836
837#if defined(__APPLE__)
838
839lldb::SBProcess
840SBTarget::AttachToProcessWithID (SBListener &listener,
841                                ::pid_t pid,
842                                 lldb::SBError& error)
843{
844    return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
845}
846
847#endif // #if defined(__APPLE__)
848
849lldb::SBProcess
850SBTarget::AttachToProcessWithID
851(
852    SBListener &listener,
853    lldb::pid_t pid,// The process ID to attach to
854    SBError& error  // An error explaining what went wrong if attach fails
855)
856{
857    SBProcess sb_process;
858    ProcessSP process_sp;
859    TargetSP target_sp(GetSP());
860    if (target_sp)
861    {
862        Mutex::Locker api_locker (target_sp->GetAPIMutex());
863
864        StateType state = eStateInvalid;
865        process_sp = target_sp->GetProcessSP();
866        if (process_sp)
867        {
868            state = process_sp->GetState();
869
870            if (process_sp->IsAlive() && state != eStateConnected)
871            {
872                if (state == eStateAttaching)
873                    error.SetErrorString ("process attach is in progress");
874                else
875                    error.SetErrorString ("a process is already being debugged");
876                return sb_process;
877            }
878        }
879
880        if (state == eStateConnected)
881        {
882            // If we are already connected, then we have already specified the
883            // listener, so if a valid listener is supplied, we need to error out
884            // to let the client know.
885            if (listener.IsValid())
886            {
887                error.SetErrorString ("process is connected and already has a listener, pass empty listener");
888                return sb_process;
889            }
890        }
891        else
892        {
893            if (listener.IsValid())
894                process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
895            else
896                process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
897        }
898        if (process_sp)
899        {
900            sb_process.SetSP (process_sp);
901
902            ProcessAttachInfo attach_info;
903            attach_info.SetProcessID (pid);
904
905            PlatformSP platform_sp = target_sp->GetPlatform();
906            ProcessInstanceInfo instance_info;
907            if (platform_sp->GetProcessInfo(pid, instance_info))
908            {
909                attach_info.SetUserID(instance_info.GetEffectiveUserID());
910
911            }
912            error.SetError (process_sp->Attach (attach_info));
913            // If we are doing synchronous mode, then wait for the
914            // process to stop!
915            if (target_sp->GetDebugger().GetAsyncExecution () == false)
916                process_sp->WaitForProcessToStop (NULL);
917        }
918        else
919        {
920            error.SetErrorString ("unable to create lldb_private::Process");
921        }
922    }
923    else
924    {
925        error.SetErrorString ("SBTarget is invalid");
926    }
927    return sb_process;
928
929}
930
931lldb::SBProcess
932SBTarget::AttachToProcessWithName
933(
934    SBListener &listener,
935    const char *name,   // basename of process to attach to
936    bool wait_for,      // if true wait for a new instance of "name" to be launched
937    SBError& error      // An error explaining what went wrong if attach fails
938)
939{
940    SBProcess sb_process;
941    ProcessSP process_sp;
942    TargetSP target_sp(GetSP());
943    if (name && target_sp)
944    {
945        Mutex::Locker api_locker (target_sp->GetAPIMutex());
946
947        StateType state = eStateInvalid;
948        process_sp = target_sp->GetProcessSP();
949        if (process_sp)
950        {
951            state = process_sp->GetState();
952
953            if (process_sp->IsAlive() && state != eStateConnected)
954            {
955                if (state == eStateAttaching)
956                    error.SetErrorString ("process attach is in progress");
957                else
958                    error.SetErrorString ("a process is already being debugged");
959                return sb_process;
960            }
961        }
962
963        if (state == eStateConnected)
964        {
965            // If we are already connected, then we have already specified the
966            // listener, so if a valid listener is supplied, we need to error out
967            // to let the client know.
968            if (listener.IsValid())
969            {
970                error.SetErrorString ("process is connected and already has a listener, pass empty listener");
971                return sb_process;
972            }
973        }
974        else
975        {
976            if (listener.IsValid())
977                process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
978            else
979                process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
980        }
981
982        if (process_sp)
983        {
984            sb_process.SetSP (process_sp);
985            ProcessAttachInfo attach_info;
986            attach_info.GetExecutableFile().SetFile(name, false);
987            attach_info.SetWaitForLaunch(wait_for);
988            error.SetError (process_sp->Attach (attach_info));
989            // If we are doing synchronous mode, then wait for the
990            // process to stop!
991            if (target_sp->GetDebugger().GetAsyncExecution () == false)
992                process_sp->WaitForProcessToStop (NULL);
993        }
994        else
995        {
996            error.SetErrorString ("unable to create lldb_private::Process");
997        }
998    }
999    else
1000    {
1001        error.SetErrorString ("SBTarget is invalid");
1002    }
1003    return sb_process;
1004
1005}
1006
1007lldb::SBProcess
1008SBTarget::ConnectRemote
1009(
1010    SBListener &listener,
1011    const char *url,
1012    const char *plugin_name,
1013    SBError& error
1014)
1015{
1016    SBProcess sb_process;
1017    ProcessSP process_sp;
1018    TargetSP target_sp(GetSP());
1019    if (target_sp)
1020    {
1021        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1022        if (listener.IsValid())
1023            process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
1024        else
1025            process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
1026
1027
1028        if (process_sp)
1029        {
1030            sb_process.SetSP (process_sp);
1031            error.SetError (process_sp->ConnectRemote (url));
1032        }
1033        else
1034        {
1035            error.SetErrorString ("unable to create lldb_private::Process");
1036        }
1037    }
1038    else
1039    {
1040        error.SetErrorString ("SBTarget is invalid");
1041    }
1042    return sb_process;
1043}
1044
1045SBFileSpec
1046SBTarget::GetExecutable ()
1047{
1048
1049    SBFileSpec exe_file_spec;
1050    TargetSP target_sp(GetSP());
1051    if (target_sp)
1052    {
1053        Module *exe_module = target_sp->GetExecutableModulePointer();
1054        if (exe_module)
1055            exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
1056    }
1057
1058    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1059    if (log)
1060    {
1061        log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1062                     target_sp.get(), exe_file_spec.get());
1063    }
1064
1065    return exe_file_spec;
1066}
1067
1068bool
1069SBTarget::operator == (const SBTarget &rhs) const
1070{
1071    return m_opaque_sp.get() == rhs.m_opaque_sp.get();
1072}
1073
1074bool
1075SBTarget::operator != (const SBTarget &rhs) const
1076{
1077    return m_opaque_sp.get() != rhs.m_opaque_sp.get();
1078}
1079
1080lldb::TargetSP
1081SBTarget::GetSP () const
1082{
1083    return m_opaque_sp;
1084}
1085
1086void
1087SBTarget::SetSP (const lldb::TargetSP& target_sp)
1088{
1089    m_opaque_sp = target_sp;
1090}
1091
1092lldb::SBAddress
1093SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
1094{
1095    lldb::SBAddress sb_addr;
1096    Address &addr = sb_addr.ref();
1097    TargetSP target_sp(GetSP());
1098    if (target_sp)
1099    {
1100        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1101        if (target_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
1102            return sb_addr;
1103    }
1104
1105    // We have a load address that isn't in a section, just return an address
1106    // with the offset filled in (the address) and the section set to NULL
1107    addr.SetRawAddress(vm_addr);
1108    return sb_addr;
1109}
1110
1111SBSymbolContext
1112SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
1113{
1114    SBSymbolContext sc;
1115    if (addr.IsValid())
1116    {
1117        TargetSP target_sp(GetSP());
1118        if (target_sp)
1119            target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1120    }
1121    return sc;
1122}
1123
1124
1125SBBreakpoint
1126SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
1127{
1128    return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
1129}
1130
1131SBBreakpoint
1132SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
1133{
1134    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1135
1136    SBBreakpoint sb_bp;
1137    TargetSP target_sp(GetSP());
1138    if (target_sp && line != 0)
1139    {
1140        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1141        *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
1142    }
1143
1144    if (log)
1145    {
1146        SBStream sstr;
1147        sb_bp.GetDescription (sstr);
1148        char path[PATH_MAX];
1149        sb_file_spec->GetPath (path, sizeof(path));
1150        log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1151                     target_sp.get(),
1152                     path,
1153                     line,
1154                     sb_bp.get(),
1155                     sstr.GetData());
1156    }
1157
1158    return sb_bp;
1159}
1160
1161SBBreakpoint
1162SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
1163{
1164    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1165
1166    SBBreakpoint sb_bp;
1167    TargetSP target_sp(GetSP());
1168    if (target_sp.get())
1169    {
1170        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1171        if (module_name && module_name[0])
1172        {
1173            FileSpecList module_spec_list;
1174            module_spec_list.Append (FileSpec (module_name, false));
1175            *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, false);
1176        }
1177        else
1178        {
1179            *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, false);
1180        }
1181    }
1182
1183    if (log)
1184    {
1185        log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1186                     target_sp.get(), symbol_name, module_name, sb_bp.get());
1187    }
1188
1189    return sb_bp;
1190}
1191
1192lldb::SBBreakpoint
1193SBTarget::BreakpointCreateByName (const char *symbol_name,
1194                            const SBFileSpecList &module_list,
1195                            const SBFileSpecList &comp_unit_list)
1196{
1197    uint32_t name_type_mask = eFunctionNameTypeAuto;
1198    return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1199}
1200
1201lldb::SBBreakpoint
1202SBTarget::BreakpointCreateByName (const char *symbol_name,
1203                            uint32_t name_type_mask,
1204                            const SBFileSpecList &module_list,
1205                            const SBFileSpecList &comp_unit_list)
1206{
1207    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1208
1209    SBBreakpoint sb_bp;
1210    TargetSP target_sp(GetSP());
1211    if (target_sp && symbol_name && symbol_name[0])
1212    {
1213        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1214        *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1215                                                comp_unit_list.get(),
1216                                                symbol_name,
1217                                                name_type_mask,
1218                                                false);
1219    }
1220
1221    if (log)
1222    {
1223        log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1224                     target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
1225    }
1226
1227    return sb_bp;
1228}
1229
1230
1231SBBreakpoint
1232SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
1233{
1234    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1235
1236    SBBreakpoint sb_bp;
1237    TargetSP target_sp(GetSP());
1238    if (target_sp && symbol_name_regex && symbol_name_regex[0])
1239    {
1240        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1241        RegularExpression regexp(symbol_name_regex);
1242
1243        if (module_name && module_name[0])
1244        {
1245            FileSpecList module_spec_list;
1246            module_spec_list.Append (FileSpec (module_name, false));
1247
1248            *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, false);
1249        }
1250        else
1251        {
1252            *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, false);
1253        }
1254    }
1255
1256    if (log)
1257    {
1258        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1259                     target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
1260    }
1261
1262    return sb_bp;
1263}
1264
1265lldb::SBBreakpoint
1266SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1267                         const SBFileSpecList &module_list,
1268                         const SBFileSpecList &comp_unit_list)
1269{
1270    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1271
1272    SBBreakpoint sb_bp;
1273    TargetSP target_sp(GetSP());
1274    if (target_sp && symbol_name_regex && symbol_name_regex[0])
1275    {
1276        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1277        RegularExpression regexp(symbol_name_regex);
1278
1279        *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, false);
1280    }
1281
1282    if (log)
1283    {
1284        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1285                     target_sp.get(), symbol_name_regex, sb_bp.get());
1286    }
1287
1288    return sb_bp;
1289}
1290
1291SBBreakpoint
1292SBTarget::BreakpointCreateByAddress (addr_t address)
1293{
1294    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1295
1296    SBBreakpoint sb_bp;
1297    TargetSP target_sp(GetSP());
1298    if (target_sp)
1299    {
1300        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1301        *sb_bp = target_sp->CreateBreakpoint (address, false);
1302    }
1303
1304    if (log)
1305    {
1306        log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%llu) => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
1307    }
1308
1309    return sb_bp;
1310}
1311
1312lldb::SBBreakpoint
1313SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
1314{
1315    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1316
1317    SBBreakpoint sb_bp;
1318    TargetSP target_sp(GetSP());
1319    if (target_sp && source_regex && source_regex[0])
1320    {
1321        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1322        RegularExpression regexp(source_regex);
1323        FileSpecList source_file_spec_list;
1324        source_file_spec_list.Append (source_file.ref());
1325
1326        if (module_name && module_name[0])
1327        {
1328            FileSpecList module_spec_list;
1329            module_spec_list.Append (FileSpec (module_name, false));
1330
1331            *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
1332        }
1333        else
1334        {
1335            *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
1336        }
1337    }
1338
1339    if (log)
1340    {
1341        char path[PATH_MAX];
1342        source_file->GetPath (path, sizeof(path));
1343        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1344                     target_sp.get(), source_regex, path, module_name, sb_bp.get());
1345    }
1346
1347    return sb_bp;
1348}
1349
1350lldb::SBBreakpoint
1351SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1352                               const SBFileSpecList &module_list,
1353                               const lldb::SBFileSpecList &source_file_list)
1354{
1355    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1356
1357    SBBreakpoint sb_bp;
1358    TargetSP target_sp(GetSP());
1359    if (target_sp && source_regex && source_regex[0])
1360    {
1361        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1362        RegularExpression regexp(source_regex);
1363        *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
1364    }
1365
1366    if (log)
1367    {
1368        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1369                     target_sp.get(), source_regex, sb_bp.get());
1370    }
1371
1372    return sb_bp;
1373}
1374
1375uint32_t
1376SBTarget::GetNumBreakpoints () const
1377{
1378    TargetSP target_sp(GetSP());
1379    if (target_sp)
1380    {
1381        // The breakpoint list is thread safe, no need to lock
1382        return target_sp->GetBreakpointList().GetSize();
1383    }
1384    return 0;
1385}
1386
1387SBBreakpoint
1388SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1389{
1390    SBBreakpoint sb_breakpoint;
1391    TargetSP target_sp(GetSP());
1392    if (target_sp)
1393    {
1394        // The breakpoint list is thread safe, no need to lock
1395        *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1396    }
1397    return sb_breakpoint;
1398}
1399
1400bool
1401SBTarget::BreakpointDelete (break_id_t bp_id)
1402{
1403    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1404
1405    bool result = false;
1406    TargetSP target_sp(GetSP());
1407    if (target_sp)
1408    {
1409        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1410        result = target_sp->RemoveBreakpointByID (bp_id);
1411    }
1412
1413    if (log)
1414    {
1415        log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
1416    }
1417
1418    return result;
1419}
1420
1421SBBreakpoint
1422SBTarget::FindBreakpointByID (break_id_t bp_id)
1423{
1424    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1425
1426    SBBreakpoint sb_breakpoint;
1427    TargetSP target_sp(GetSP());
1428    if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1429    {
1430        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1431        *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1432    }
1433
1434    if (log)
1435    {
1436        log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1437                     target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
1438    }
1439
1440    return sb_breakpoint;
1441}
1442
1443bool
1444SBTarget::EnableAllBreakpoints ()
1445{
1446    TargetSP target_sp(GetSP());
1447    if (target_sp)
1448    {
1449        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1450        target_sp->EnableAllBreakpoints ();
1451        return true;
1452    }
1453    return false;
1454}
1455
1456bool
1457SBTarget::DisableAllBreakpoints ()
1458{
1459    TargetSP target_sp(GetSP());
1460    if (target_sp)
1461    {
1462        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1463        target_sp->DisableAllBreakpoints ();
1464        return true;
1465    }
1466    return false;
1467}
1468
1469bool
1470SBTarget::DeleteAllBreakpoints ()
1471{
1472    TargetSP target_sp(GetSP());
1473    if (target_sp)
1474    {
1475        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1476        target_sp->RemoveAllBreakpoints ();
1477        return true;
1478    }
1479    return false;
1480}
1481
1482uint32_t
1483SBTarget::GetNumWatchpoints () const
1484{
1485    TargetSP target_sp(GetSP());
1486    if (target_sp)
1487    {
1488        // The watchpoint list is thread safe, no need to lock
1489        return target_sp->GetWatchpointList().GetSize();
1490    }
1491    return 0;
1492}
1493
1494SBWatchpoint
1495SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1496{
1497    SBWatchpoint sb_watchpoint;
1498    TargetSP target_sp(GetSP());
1499    if (target_sp)
1500    {
1501        // The watchpoint list is thread safe, no need to lock
1502        sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1503    }
1504    return sb_watchpoint;
1505}
1506
1507bool
1508SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1509{
1510    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1511
1512    bool result = false;
1513    TargetSP target_sp(GetSP());
1514    if (target_sp)
1515    {
1516        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1517        result = target_sp->RemoveWatchpointByID (wp_id);
1518    }
1519
1520    if (log)
1521    {
1522        log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
1523    }
1524
1525    return result;
1526}
1527
1528SBWatchpoint
1529SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1530{
1531    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1532
1533    SBWatchpoint sb_watchpoint;
1534    lldb::WatchpointSP watchpoint_sp;
1535    TargetSP target_sp(GetSP());
1536    if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1537    {
1538        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1539        watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1540        sb_watchpoint.SetSP (watchpoint_sp);
1541    }
1542
1543    if (log)
1544    {
1545        log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1546                     target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
1547    }
1548
1549    return sb_watchpoint;
1550}
1551
1552lldb::SBWatchpoint
1553SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write)
1554{
1555    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1556
1557    SBWatchpoint sb_watchpoint;
1558    lldb::WatchpointSP watchpoint_sp;
1559    TargetSP target_sp(GetSP());
1560    if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1561    {
1562        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1563        uint32_t watch_type = 0;
1564        if (read)
1565            watch_type |= LLDB_WATCH_TYPE_READ;
1566        if (write)
1567            watch_type |= LLDB_WATCH_TYPE_WRITE;
1568        watchpoint_sp = target_sp->CreateWatchpoint(addr, size, watch_type);
1569        sb_watchpoint.SetSP (watchpoint_sp);
1570    }
1571
1572    if (log)
1573    {
1574        log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%llx, 0x%u) => SBWatchpoint(%p)",
1575                     target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1576    }
1577
1578    return sb_watchpoint;
1579}
1580
1581bool
1582SBTarget::EnableAllWatchpoints ()
1583{
1584    TargetSP target_sp(GetSP());
1585    if (target_sp)
1586    {
1587        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1588        target_sp->EnableAllWatchpoints ();
1589        return true;
1590    }
1591    return false;
1592}
1593
1594bool
1595SBTarget::DisableAllWatchpoints ()
1596{
1597    TargetSP target_sp(GetSP());
1598    if (target_sp)
1599    {
1600        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1601        target_sp->DisableAllWatchpoints ();
1602        return true;
1603    }
1604    return false;
1605}
1606
1607bool
1608SBTarget::DeleteAllWatchpoints ()
1609{
1610    TargetSP target_sp(GetSP());
1611    if (target_sp)
1612    {
1613        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1614        target_sp->RemoveAllWatchpoints ();
1615        return true;
1616    }
1617    return false;
1618}
1619
1620
1621lldb::SBModule
1622SBTarget::AddModule (const char *path,
1623                     const char *triple,
1624                     const char *uuid_cstr)
1625{
1626    lldb::SBModule sb_module;
1627    TargetSP target_sp(GetSP());
1628    if (target_sp)
1629    {
1630        ModuleSpec module_spec;
1631        if (path)
1632            module_spec.GetFileSpec().SetFile(path, false);
1633
1634        if (uuid_cstr)
1635            module_spec.GetUUID().SetfromCString(uuid_cstr);
1636
1637        if (triple)
1638            module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1639
1640        sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1641    }
1642    return sb_module;
1643}
1644
1645bool
1646SBTarget::AddModule (lldb::SBModule &module)
1647{
1648    TargetSP target_sp(GetSP());
1649    if (target_sp)
1650    {
1651        target_sp->GetImages().AppendIfNeeded (module.GetSP());
1652        return true;
1653    }
1654    return false;
1655}
1656
1657uint32_t
1658SBTarget::GetNumModules () const
1659{
1660    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1661
1662    uint32_t num = 0;
1663    TargetSP target_sp(GetSP());
1664    if (target_sp)
1665    {
1666        // The module list is thread safe, no need to lock
1667        num = target_sp->GetImages().GetSize();
1668    }
1669
1670    if (log)
1671        log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
1672
1673    return num;
1674}
1675
1676void
1677SBTarget::Clear ()
1678{
1679    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1680
1681    if (log)
1682        log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1683
1684    m_opaque_sp.reset();
1685}
1686
1687
1688SBModule
1689SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1690{
1691    SBModule sb_module;
1692    TargetSP target_sp(GetSP());
1693    if (target_sp && sb_file_spec.IsValid())
1694    {
1695        ModuleSpec module_spec(*sb_file_spec);
1696        // The module list is thread safe, no need to lock
1697        sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
1698    }
1699    return sb_module;
1700}
1701
1702lldb::ByteOrder
1703SBTarget::GetByteOrder ()
1704{
1705    TargetSP target_sp(GetSP());
1706    if (target_sp)
1707        return target_sp->GetArchitecture().GetByteOrder();
1708    return eByteOrderInvalid;
1709}
1710
1711const char *
1712SBTarget::GetTriple ()
1713{
1714    TargetSP target_sp(GetSP());
1715    if (target_sp)
1716    {
1717        std::string triple (target_sp->GetArchitecture().GetTriple().str());
1718        // Unique the string so we don't run into ownership issues since
1719        // the const strings put the string into the string pool once and
1720        // the strings never comes out
1721        ConstString const_triple (triple.c_str());
1722        return const_triple.GetCString();
1723    }
1724    return NULL;
1725}
1726
1727uint32_t
1728SBTarget::GetAddressByteSize()
1729{
1730    TargetSP target_sp(GetSP());
1731    if (target_sp)
1732        return target_sp->GetArchitecture().GetAddressByteSize();
1733    return sizeof(void*);
1734}
1735
1736
1737SBModule
1738SBTarget::GetModuleAtIndex (uint32_t idx)
1739{
1740    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1741
1742    SBModule sb_module;
1743    ModuleSP module_sp;
1744    TargetSP target_sp(GetSP());
1745    if (target_sp)
1746    {
1747        // The module list is thread safe, no need to lock
1748        module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
1749        sb_module.SetSP (module_sp);
1750    }
1751
1752    if (log)
1753    {
1754        log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1755                     target_sp.get(), idx, module_sp.get());
1756    }
1757
1758    return sb_module;
1759}
1760
1761bool
1762SBTarget::RemoveModule (lldb::SBModule module)
1763{
1764    TargetSP target_sp(GetSP());
1765    if (target_sp)
1766        return target_sp->GetImages().Remove(module.GetSP());
1767    return false;
1768}
1769
1770
1771SBBroadcaster
1772SBTarget::GetBroadcaster () const
1773{
1774    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1775
1776    TargetSP target_sp(GetSP());
1777    SBBroadcaster broadcaster(target_sp.get(), false);
1778
1779    if (log)
1780        log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1781                     target_sp.get(), broadcaster.get());
1782
1783    return broadcaster;
1784}
1785
1786bool
1787SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1788{
1789    Stream &strm = description.ref();
1790
1791    TargetSP target_sp(GetSP());
1792    if (target_sp)
1793    {
1794        target_sp->Dump (&strm, description_level);
1795    }
1796    else
1797        strm.PutCString ("No value");
1798
1799    return true;
1800}
1801
1802lldb::SBSymbolContextList
1803SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
1804{
1805    lldb::SBSymbolContextList sb_sc_list;
1806    if (name && name[0])
1807    {
1808        TargetSP target_sp(GetSP());
1809        if (target_sp)
1810        {
1811            const bool symbols_ok = true;
1812            const bool inlines_ok = true;
1813            const bool append = true;
1814            target_sp->GetImages().FindFunctions (ConstString(name),
1815                                                  name_type_mask,
1816                                                  symbols_ok,
1817                                                  inlines_ok,
1818                                                  append,
1819                                                  *sb_sc_list);
1820        }
1821    }
1822    return sb_sc_list;
1823}
1824
1825lldb::SBType
1826SBTarget::FindFirstType (const char* type)
1827{
1828    TargetSP target_sp(GetSP());
1829    if (type && target_sp)
1830    {
1831        size_t count = target_sp->GetImages().GetSize();
1832        for (size_t idx = 0; idx < count; idx++)
1833        {
1834            SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1835
1836            if (found_at_idx.IsValid())
1837                return found_at_idx;
1838        }
1839    }
1840    return SBType();
1841}
1842
1843lldb::SBTypeList
1844SBTarget::FindTypes (const char* type)
1845{
1846
1847    SBTypeList retval;
1848
1849    TargetSP target_sp(GetSP());
1850    if (type && target_sp)
1851    {
1852        ModuleList& images = target_sp->GetImages();
1853        ConstString name_const(type);
1854        SymbolContext sc;
1855        TypeList type_list;
1856
1857        uint32_t num_matches = images.FindTypes(sc,
1858                                                name_const,
1859                                                true,
1860                                                UINT32_MAX,
1861                                                type_list);
1862
1863        for (size_t idx = 0; idx < num_matches; idx++)
1864        {
1865            TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1866            if (type_sp)
1867                retval.Append(SBType(type_sp));
1868        }
1869    }
1870    return retval;
1871}
1872
1873SBValueList
1874SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1875{
1876    SBValueList sb_value_list;
1877
1878    TargetSP target_sp(GetSP());
1879    if (name && target_sp)
1880    {
1881        VariableList variable_list;
1882        const bool append = true;
1883        const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
1884                                                                                 append,
1885                                                                                 max_matches,
1886                                                                                 variable_list);
1887
1888        if (match_count > 0)
1889        {
1890            ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
1891            if (exe_scope == NULL)
1892                exe_scope = target_sp.get();
1893            ValueObjectList &value_object_list = sb_value_list.ref();
1894            for (uint32_t i=0; i<match_count; ++i)
1895            {
1896                lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1897                if (valobj_sp)
1898                    value_object_list.Append(valobj_sp);
1899            }
1900        }
1901    }
1902
1903    return sb_value_list;
1904}
1905
1906SBSourceManager
1907SBTarget::GetSourceManager()
1908{
1909    SBSourceManager source_manager (*this);
1910    return source_manager;
1911}
1912
1913lldb::SBInstructionList
1914SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
1915{
1916    SBInstructionList sb_instructions;
1917
1918    TargetSP target_sp(GetSP());
1919    if (target_sp)
1920    {
1921        Address addr;
1922
1923        if (base_addr.get())
1924            addr = *base_addr.get();
1925
1926        sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
1927                                                                         NULL,
1928                                                                         addr,
1929                                                                         buf,
1930                                                                         size));
1931    }
1932
1933    return sb_instructions;
1934}
1935
1936lldb::SBInstructionList
1937SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
1938{
1939    return GetInstructions (ResolveLoadAddress(base_addr), buf, size);
1940}
1941
1942SBError
1943SBTarget::SetSectionLoadAddress (lldb::SBSection section,
1944                                 lldb::addr_t section_base_addr)
1945{
1946    SBError sb_error;
1947    TargetSP target_sp(GetSP());
1948    if (target_sp)
1949    {
1950        if (!section.IsValid())
1951        {
1952            sb_error.SetErrorStringWithFormat ("invalid section");
1953        }
1954        else
1955        {
1956            target_sp->GetSectionLoadList().SetSectionLoadAddress (section.GetSP().get(), section_base_addr);
1957        }
1958    }
1959    else
1960    {
1961        sb_error.SetErrorStringWithFormat ("invalid target");
1962    }
1963    return sb_error;
1964}
1965
1966SBError
1967SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
1968{
1969    SBError sb_error;
1970
1971    TargetSP target_sp(GetSP());
1972    if (target_sp)
1973    {
1974        if (!section.IsValid())
1975        {
1976            sb_error.SetErrorStringWithFormat ("invalid section");
1977        }
1978        else
1979        {
1980            target_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSP().get());
1981        }
1982    }
1983    else
1984    {
1985        sb_error.SetErrorStringWithFormat ("invalid target");
1986    }
1987    return sb_error;
1988}
1989
1990SBError
1991SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
1992{
1993    SBError sb_error;
1994
1995    char path[PATH_MAX];
1996    TargetSP target_sp(GetSP());
1997    if (target_sp)
1998    {
1999        ModuleSP module_sp (module.GetSP());
2000        if (module_sp)
2001        {
2002            ObjectFile *objfile = module_sp->GetObjectFile();
2003            if (objfile)
2004            {
2005                SectionList *section_list = objfile->GetSectionList();
2006                if (section_list)
2007                {
2008                    const size_t num_sections = section_list->GetSize();
2009                    for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2010                    {
2011                        SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2012                        if (section_sp)
2013                            target_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide_offset);
2014                    }
2015                }
2016                else
2017                {
2018                    module_sp->GetFileSpec().GetPath (path, sizeof(path));
2019                    sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2020                }
2021            }
2022            else
2023            {
2024                module_sp->GetFileSpec().GetPath (path, sizeof(path));
2025                sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2026            }
2027        }
2028        else
2029        {
2030            sb_error.SetErrorStringWithFormat ("invalid module");
2031        }
2032
2033    }
2034    else
2035    {
2036        sb_error.SetErrorStringWithFormat ("invalid target");
2037    }
2038    return sb_error;
2039}
2040
2041SBError
2042SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2043{
2044    SBError sb_error;
2045
2046    char path[PATH_MAX];
2047    TargetSP target_sp(GetSP());
2048    if (target_sp)
2049    {
2050        ModuleSP module_sp (module.GetSP());
2051        if (module_sp)
2052        {
2053            ObjectFile *objfile = module_sp->GetObjectFile();
2054            if (objfile)
2055            {
2056                SectionList *section_list = objfile->GetSectionList();
2057                if (section_list)
2058                {
2059                    const size_t num_sections = section_list->GetSize();
2060                    for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2061                    {
2062                        SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2063                        if (section_sp)
2064                            target_sp->GetSectionLoadList().SetSectionUnloaded (section_sp.get());
2065                    }
2066                }
2067                else
2068                {
2069                    module_sp->GetFileSpec().GetPath (path, sizeof(path));
2070                    sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2071                }
2072            }
2073            else
2074            {
2075                module_sp->GetFileSpec().GetPath (path, sizeof(path));
2076                sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2077            }
2078        }
2079        else
2080        {
2081            sb_error.SetErrorStringWithFormat ("invalid module");
2082        }
2083    }
2084    else
2085    {
2086        sb_error.SetErrorStringWithFormat ("invalid target");
2087    }
2088    return sb_error;
2089}
2090
2091
2092