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/lldb-python.h"
11
12#include "lldb/API/SBTarget.h"
13
14#include "lldb/lldb-public.h"
15
16#include "lldb/API/SBDebugger.h"
17#include "lldb/API/SBBreakpoint.h"
18#include "lldb/API/SBExpressionOptions.h"
19#include "lldb/API/SBFileSpec.h"
20#include "lldb/API/SBListener.h"
21#include "lldb/API/SBModule.h"
22#include "lldb/API/SBModuleSpec.h"
23#include "lldb/API/SBSourceManager.h"
24#include "lldb/API/SBProcess.h"
25#include "lldb/API/SBStream.h"
26#include "lldb/API/SBSymbolContextList.h"
27#include "lldb/Breakpoint/BreakpointID.h"
28#include "lldb/Breakpoint/BreakpointIDList.h"
29#include "lldb/Breakpoint/BreakpointList.h"
30#include "lldb/Breakpoint/BreakpointLocation.h"
31#include "lldb/Core/Address.h"
32#include "lldb/Core/AddressResolver.h"
33#include "lldb/Core/AddressResolverName.h"
34#include "lldb/Core/ArchSpec.h"
35#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Disassembler.h"
37#include "lldb/Core/Log.h"
38#include "lldb/Core/Module.h"
39#include "lldb/Core/ModuleSpec.h"
40#include "lldb/Core/RegularExpression.h"
41#include "lldb/Core/SearchFilter.h"
42#include "lldb/Core/Section.h"
43#include "lldb/Core/STLUtils.h"
44#include "lldb/Core/ValueObjectConstResult.h"
45#include "lldb/Core/ValueObjectList.h"
46#include "lldb/Core/ValueObjectVariable.h"
47#include "lldb/Host/FileSpec.h"
48#include "lldb/Host/Host.h"
49#include "lldb/Interpreter/Args.h"
50#include "lldb/Symbol/ObjectFile.h"
51#include "lldb/Symbol/SymbolVendor.h"
52#include "lldb/Symbol/VariableList.h"
53#include "lldb/Target/LanguageRuntime.h"
54#include "lldb/Target/Process.h"
55
56#include "lldb/Target/Target.h"
57#include "lldb/Target/TargetList.h"
58
59#include "lldb/Interpreter/CommandReturnObject.h"
60#include "../source/Commands/CommandObjectBreakpoint.h"
61
62
63using namespace lldb;
64using namespace lldb_private;
65
66#define DEFAULT_DISASM_BYTE_SIZE 32
67
68SBLaunchInfo::SBLaunchInfo (const char **argv) :
69    m_opaque_sp(new ProcessLaunchInfo())
70{
71    m_opaque_sp->GetFlags().Reset (eLaunchFlagDebug | eLaunchFlagDisableASLR);
72    if (argv && argv[0])
73        m_opaque_sp->GetArguments().SetArguments(argv);
74}
75
76SBLaunchInfo::~SBLaunchInfo()
77{
78}
79
80lldb_private::ProcessLaunchInfo &
81SBLaunchInfo::ref ()
82{
83    return *m_opaque_sp;
84}
85
86
87uint32_t
88SBLaunchInfo::GetUserID()
89{
90    return m_opaque_sp->GetUserID();
91}
92
93uint32_t
94SBLaunchInfo::GetGroupID()
95{
96    return m_opaque_sp->GetGroupID();
97}
98
99bool
100SBLaunchInfo::UserIDIsValid ()
101{
102    return m_opaque_sp->UserIDIsValid();
103}
104
105bool
106SBLaunchInfo::GroupIDIsValid ()
107{
108    return m_opaque_sp->GroupIDIsValid();
109}
110
111void
112SBLaunchInfo::SetUserID (uint32_t uid)
113{
114    m_opaque_sp->SetUserID (uid);
115}
116
117void
118SBLaunchInfo::SetGroupID (uint32_t gid)
119{
120    m_opaque_sp->SetGroupID (gid);
121}
122
123uint32_t
124SBLaunchInfo::GetNumArguments ()
125{
126    return m_opaque_sp->GetArguments().GetArgumentCount();
127}
128
129const char *
130SBLaunchInfo::GetArgumentAtIndex (uint32_t idx)
131{
132    return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
133}
134
135void
136SBLaunchInfo::SetArguments (const char **argv, bool append)
137{
138    if (append)
139    {
140        if (argv)
141            m_opaque_sp->GetArguments().AppendArguments(argv);
142    }
143    else
144    {
145        if (argv)
146            m_opaque_sp->GetArguments().SetArguments(argv);
147        else
148            m_opaque_sp->GetArguments().Clear();
149    }
150}
151
152uint32_t
153SBLaunchInfo::GetNumEnvironmentEntries ()
154{
155    return m_opaque_sp->GetEnvironmentEntries().GetArgumentCount();
156}
157
158const char *
159SBLaunchInfo::GetEnvironmentEntryAtIndex (uint32_t idx)
160{
161    return m_opaque_sp->GetEnvironmentEntries().GetArgumentAtIndex(idx);
162}
163
164void
165SBLaunchInfo::SetEnvironmentEntries (const char **envp, bool append)
166{
167    if (append)
168    {
169        if (envp)
170            m_opaque_sp->GetEnvironmentEntries().AppendArguments(envp);
171    }
172    else
173    {
174        if (envp)
175            m_opaque_sp->GetEnvironmentEntries().SetArguments(envp);
176        else
177            m_opaque_sp->GetEnvironmentEntries().Clear();
178    }
179}
180
181void
182SBLaunchInfo::Clear ()
183{
184    m_opaque_sp->Clear();
185}
186
187const char *
188SBLaunchInfo::GetWorkingDirectory () const
189{
190    return m_opaque_sp->GetWorkingDirectory();
191}
192
193void
194SBLaunchInfo::SetWorkingDirectory (const char *working_dir)
195{
196    m_opaque_sp->SetWorkingDirectory(working_dir);
197}
198
199uint32_t
200SBLaunchInfo::GetLaunchFlags ()
201{
202    return m_opaque_sp->GetFlags().Get();
203}
204
205void
206SBLaunchInfo::SetLaunchFlags (uint32_t flags)
207{
208    m_opaque_sp->GetFlags().Reset(flags);
209}
210
211const char *
212SBLaunchInfo::GetProcessPluginName ()
213{
214    return m_opaque_sp->GetProcessPluginName();
215}
216
217void
218SBLaunchInfo::SetProcessPluginName (const char *plugin_name)
219{
220    return m_opaque_sp->SetProcessPluginName (plugin_name);
221}
222
223const char *
224SBLaunchInfo::GetShell ()
225{
226    return m_opaque_sp->GetShell();
227}
228
229void
230SBLaunchInfo::SetShell (const char * path)
231{
232    m_opaque_sp->SetShell (path);
233}
234
235uint32_t
236SBLaunchInfo::GetResumeCount ()
237{
238    return m_opaque_sp->GetResumeCount();
239}
240
241void
242SBLaunchInfo::SetResumeCount (uint32_t c)
243{
244    m_opaque_sp->SetResumeCount (c);
245}
246
247bool
248SBLaunchInfo::AddCloseFileAction (int fd)
249{
250    return m_opaque_sp->AppendCloseFileAction(fd);
251}
252
253bool
254SBLaunchInfo::AddDuplicateFileAction (int fd, int dup_fd)
255{
256    return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
257}
258
259bool
260SBLaunchInfo::AddOpenFileAction (int fd, const char *path, bool read, bool write)
261{
262    return m_opaque_sp->AppendOpenFileAction(fd, path, read, write);
263}
264
265bool
266SBLaunchInfo::AddSuppressFileAction (int fd, bool read, bool write)
267{
268    return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
269}
270
271
272SBAttachInfo::SBAttachInfo () :
273    m_opaque_sp (new ProcessAttachInfo())
274{
275}
276
277SBAttachInfo::SBAttachInfo (lldb::pid_t pid) :
278    m_opaque_sp (new ProcessAttachInfo())
279{
280    m_opaque_sp->SetProcessID (pid);
281}
282
283SBAttachInfo::SBAttachInfo (const char *path, bool wait_for) :
284    m_opaque_sp (new ProcessAttachInfo())
285{
286    if (path && path[0])
287        m_opaque_sp->GetExecutableFile().SetFile(path, false);
288    m_opaque_sp->SetWaitForLaunch (wait_for);
289}
290
291SBAttachInfo::SBAttachInfo (const SBAttachInfo &rhs) :
292    m_opaque_sp (new ProcessAttachInfo())
293{
294    *m_opaque_sp = *rhs.m_opaque_sp;
295}
296
297SBAttachInfo::~SBAttachInfo()
298{
299}
300
301lldb_private::ProcessAttachInfo &
302SBAttachInfo::ref ()
303{
304    return *m_opaque_sp;
305}
306
307SBAttachInfo &
308SBAttachInfo::operator = (const SBAttachInfo &rhs)
309{
310    if (this != &rhs)
311        *m_opaque_sp = *rhs.m_opaque_sp;
312    return *this;
313}
314
315lldb::pid_t
316SBAttachInfo::GetProcessID ()
317{
318    return m_opaque_sp->GetProcessID();
319}
320
321void
322SBAttachInfo::SetProcessID (lldb::pid_t pid)
323{
324    m_opaque_sp->SetProcessID (pid);
325}
326
327
328uint32_t
329SBAttachInfo::GetResumeCount ()
330{
331    return m_opaque_sp->GetResumeCount();
332}
333
334void
335SBAttachInfo::SetResumeCount (uint32_t c)
336{
337    m_opaque_sp->SetResumeCount (c);
338}
339
340const char *
341SBAttachInfo::GetProcessPluginName ()
342{
343    return m_opaque_sp->GetProcessPluginName();
344}
345
346void
347SBAttachInfo::SetProcessPluginName (const char *plugin_name)
348{
349    return m_opaque_sp->SetProcessPluginName (plugin_name);
350}
351
352void
353SBAttachInfo::SetExecutable (const char *path)
354{
355    if (path && path[0])
356        m_opaque_sp->GetExecutableFile().SetFile(path, false);
357    else
358        m_opaque_sp->GetExecutableFile().Clear();
359}
360
361void
362SBAttachInfo::SetExecutable (SBFileSpec exe_file)
363{
364    if (exe_file.IsValid())
365        m_opaque_sp->GetExecutableFile() = exe_file.ref();
366    else
367        m_opaque_sp->GetExecutableFile().Clear();
368}
369
370bool
371SBAttachInfo::GetWaitForLaunch ()
372{
373    return m_opaque_sp->GetWaitForLaunch();
374}
375
376void
377SBAttachInfo::SetWaitForLaunch (bool b)
378{
379    m_opaque_sp->SetWaitForLaunch (b);
380}
381
382bool
383SBAttachInfo::GetIgnoreExisting ()
384{
385    return m_opaque_sp->GetIgnoreExisting();
386}
387
388void
389SBAttachInfo::SetIgnoreExisting (bool b)
390{
391    m_opaque_sp->SetIgnoreExisting (b);
392}
393
394uint32_t
395SBAttachInfo::GetUserID()
396{
397    return m_opaque_sp->GetUserID();
398}
399
400uint32_t
401SBAttachInfo::GetGroupID()
402{
403    return m_opaque_sp->GetGroupID();
404}
405
406bool
407SBAttachInfo::UserIDIsValid ()
408{
409    return m_opaque_sp->UserIDIsValid();
410}
411
412bool
413SBAttachInfo::GroupIDIsValid ()
414{
415    return m_opaque_sp->GroupIDIsValid();
416}
417
418void
419SBAttachInfo::SetUserID (uint32_t uid)
420{
421    m_opaque_sp->SetUserID (uid);
422}
423
424void
425SBAttachInfo::SetGroupID (uint32_t gid)
426{
427    m_opaque_sp->SetGroupID (gid);
428}
429
430uint32_t
431SBAttachInfo::GetEffectiveUserID()
432{
433    return m_opaque_sp->GetEffectiveUserID();
434}
435
436uint32_t
437SBAttachInfo::GetEffectiveGroupID()
438{
439    return m_opaque_sp->GetEffectiveGroupID();
440}
441
442bool
443SBAttachInfo::EffectiveUserIDIsValid ()
444{
445    return m_opaque_sp->EffectiveUserIDIsValid();
446}
447
448bool
449SBAttachInfo::EffectiveGroupIDIsValid ()
450{
451    return m_opaque_sp->EffectiveGroupIDIsValid ();
452}
453
454void
455SBAttachInfo::SetEffectiveUserID (uint32_t uid)
456{
457    m_opaque_sp->SetEffectiveUserID(uid);
458}
459
460void
461SBAttachInfo::SetEffectiveGroupID (uint32_t gid)
462{
463    m_opaque_sp->SetEffectiveGroupID(gid);
464}
465
466lldb::pid_t
467SBAttachInfo::GetParentProcessID ()
468{
469    return m_opaque_sp->GetParentProcessID();
470}
471
472void
473SBAttachInfo::SetParentProcessID (lldb::pid_t pid)
474{
475    m_opaque_sp->SetParentProcessID (pid);
476}
477
478bool
479SBAttachInfo::ParentProcessIDIsValid()
480{
481    return m_opaque_sp->ParentProcessIDIsValid();
482}
483
484
485//----------------------------------------------------------------------
486// SBTarget constructor
487//----------------------------------------------------------------------
488SBTarget::SBTarget () :
489    m_opaque_sp ()
490{
491}
492
493SBTarget::SBTarget (const SBTarget& rhs) :
494    m_opaque_sp (rhs.m_opaque_sp)
495{
496}
497
498SBTarget::SBTarget(const TargetSP& target_sp) :
499    m_opaque_sp (target_sp)
500{
501}
502
503const SBTarget&
504SBTarget::operator = (const SBTarget& rhs)
505{
506    if (this != &rhs)
507        m_opaque_sp = rhs.m_opaque_sp;
508    return *this;
509}
510
511//----------------------------------------------------------------------
512// Destructor
513//----------------------------------------------------------------------
514SBTarget::~SBTarget()
515{
516}
517
518const char *
519SBTarget::GetBroadcasterClassName ()
520{
521    return Target::GetStaticBroadcasterClass().AsCString();
522}
523
524bool
525SBTarget::IsValid () const
526{
527    return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid();
528}
529
530SBProcess
531SBTarget::GetProcess ()
532{
533    SBProcess sb_process;
534    ProcessSP process_sp;
535    TargetSP target_sp(GetSP());
536    if (target_sp)
537    {
538        process_sp = target_sp->GetProcessSP();
539        sb_process.SetSP (process_sp);
540    }
541
542    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
543    if (log)
544    {
545        log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
546                     target_sp.get(), process_sp.get());
547    }
548
549    return sb_process;
550}
551
552SBDebugger
553SBTarget::GetDebugger () const
554{
555    SBDebugger debugger;
556    TargetSP target_sp(GetSP());
557    if (target_sp)
558        debugger.reset (target_sp->GetDebugger().shared_from_this());
559    return debugger;
560}
561
562SBProcess
563SBTarget::LoadCore (const char *core_file)
564{
565    SBProcess sb_process;
566    TargetSP target_sp(GetSP());
567    if (target_sp)
568    {
569        FileSpec filespec(core_file, true);
570        ProcessSP process_sp (target_sp->CreateProcess(target_sp->GetDebugger().GetListener(),
571                                                       NULL,
572                                                       &filespec));
573        if (process_sp)
574        {
575            process_sp->LoadCore();
576            sb_process.SetSP (process_sp);
577        }
578    }
579    return sb_process;
580}
581
582SBProcess
583SBTarget::LaunchSimple
584(
585    char const **argv,
586    char const **envp,
587    const char *working_directory
588)
589{
590    char *stdin_path = NULL;
591    char *stdout_path = NULL;
592    char *stderr_path = NULL;
593    uint32_t launch_flags = 0;
594    bool stop_at_entry = false;
595    SBError error;
596    SBListener listener = GetDebugger().GetListener();
597    return Launch (listener,
598                   argv,
599                   envp,
600                   stdin_path,
601                   stdout_path,
602                   stderr_path,
603                   working_directory,
604                   launch_flags,
605                   stop_at_entry,
606                   error);
607}
608
609SBError
610SBTarget::Install()
611{
612    SBError sb_error;
613    TargetSP target_sp(GetSP());
614    if (target_sp)
615    {
616        Mutex::Locker api_locker (target_sp->GetAPIMutex());
617        sb_error.ref() = target_sp->Install(NULL);
618    }
619    return sb_error;
620}
621
622SBProcess
623SBTarget::Launch
624(
625    SBListener &listener,
626    char const **argv,
627    char const **envp,
628    const char *stdin_path,
629    const char *stdout_path,
630    const char *stderr_path,
631    const char *working_directory,
632    uint32_t launch_flags,   // See LaunchFlags
633    bool stop_at_entry,
634    lldb::SBError& error
635)
636{
637    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
638
639    SBProcess sb_process;
640    ProcessSP process_sp;
641    TargetSP target_sp(GetSP());
642
643    if (log)
644    {
645        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))...",
646                     target_sp.get(),
647                     argv,
648                     envp,
649                     stdin_path ? stdin_path : "NULL",
650                     stdout_path ? stdout_path : "NULL",
651                     stderr_path ? stderr_path : "NULL",
652                     working_directory ? working_directory : "NULL",
653                     launch_flags,
654                     stop_at_entry,
655                     error.get());
656    }
657
658    if (target_sp)
659    {
660        Mutex::Locker api_locker (target_sp->GetAPIMutex());
661
662        if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
663            launch_flags |= eLaunchFlagDisableASLR;
664
665        StateType state = eStateInvalid;
666        process_sp = target_sp->GetProcessSP();
667        if (process_sp)
668        {
669            state = process_sp->GetState();
670
671            if (process_sp->IsAlive() && state != eStateConnected)
672            {
673                if (state == eStateAttaching)
674                    error.SetErrorString ("process attach is in progress");
675                else
676                    error.SetErrorString ("a process is already being debugged");
677                return sb_process;
678            }
679        }
680
681        if (state == eStateConnected)
682        {
683            // If we are already connected, then we have already specified the
684            // listener, so if a valid listener is supplied, we need to error out
685            // to let the client know.
686            if (listener.IsValid())
687            {
688                error.SetErrorString ("process is connected and already has a listener, pass empty listener");
689                return sb_process;
690            }
691        }
692
693        if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
694            launch_flags |= eLaunchFlagDisableSTDIO;
695
696        ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
697
698        Module *exe_module = target_sp->GetExecutableModulePointer();
699        if (exe_module)
700            launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
701        if (argv)
702            launch_info.GetArguments().AppendArguments (argv);
703        if (envp)
704            launch_info.GetEnvironmentEntries ().SetArguments (envp);
705
706        if (listener.IsValid())
707            error.SetError (target_sp->Launch(listener.ref(), launch_info));
708        else
709            error.SetError (target_sp->Launch(target_sp->GetDebugger().GetListener(), launch_info));
710
711        sb_process.SetSP(target_sp->GetProcessSP());
712    }
713    else
714    {
715        error.SetErrorString ("SBTarget is invalid");
716    }
717
718    log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
719    if (log)
720    {
721        log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
722                     target_sp.get(), sb_process.GetSP().get());
723    }
724
725    return sb_process;
726}
727
728SBProcess
729SBTarget::Launch (SBLaunchInfo &sb_launch_info, SBError& error)
730{
731    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
732
733    SBProcess sb_process;
734    TargetSP target_sp(GetSP());
735
736    if (log)
737    {
738        log->Printf ("SBTarget(%p)::Launch (launch_info, error)...", target_sp.get());
739    }
740
741    if (target_sp)
742    {
743        Mutex::Locker api_locker (target_sp->GetAPIMutex());
744        StateType state = eStateInvalid;
745        {
746        ProcessSP process_sp = target_sp->GetProcessSP();
747        if (process_sp)
748        {
749            state = process_sp->GetState();
750
751            if (process_sp->IsAlive() && state != eStateConnected)
752            {
753                if (state == eStateAttaching)
754                    error.SetErrorString ("process attach is in progress");
755                else
756                    error.SetErrorString ("a process is already being debugged");
757                return sb_process;
758            }
759        }
760        }
761
762        lldb_private::ProcessLaunchInfo &launch_info = sb_launch_info.ref();
763
764        Module *exe_module = target_sp->GetExecutableModulePointer();
765        if (exe_module)
766            launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
767
768        const ArchSpec &arch_spec = target_sp->GetArchitecture();
769        if (arch_spec.IsValid())
770            launch_info.GetArchitecture () = arch_spec;
771
772        error.SetError (target_sp->Launch (target_sp->GetDebugger().GetListener(), launch_info));
773        sb_process.SetSP(target_sp->GetProcessSP());
774    }
775    else
776    {
777        error.SetErrorString ("SBTarget is invalid");
778    }
779
780    log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
781    if (log)
782    {
783        log->Printf ("SBTarget(%p)::Launch (...) => SBProcess(%p)",
784                     target_sp.get(), sb_process.GetSP().get());
785    }
786
787    return sb_process;
788}
789
790lldb::SBProcess
791SBTarget::Attach (SBAttachInfo &sb_attach_info, SBError& error)
792{
793    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
794
795    SBProcess sb_process;
796    ProcessSP process_sp;
797    TargetSP target_sp(GetSP());
798
799    if (log)
800    {
801        log->Printf ("SBTarget(%p)::Attach (sb_attach_info, error)...", target_sp.get());
802    }
803
804    if (target_sp)
805    {
806        Mutex::Locker api_locker (target_sp->GetAPIMutex());
807
808        StateType state = eStateInvalid;
809        process_sp = target_sp->GetProcessSP();
810        if (process_sp)
811        {
812            state = process_sp->GetState();
813
814            if (process_sp->IsAlive() && state != eStateConnected)
815            {
816                if (state == eStateAttaching)
817                    error.SetErrorString ("process attach is in progress");
818                else
819                    error.SetErrorString ("a process is already being debugged");
820                if (log)
821                {
822                    log->Printf ("SBTarget(%p)::Attach (...) => error %s",
823                                 target_sp.get(), error.GetCString());
824                }
825                return sb_process;
826            }
827        }
828
829        if (state != eStateConnected)
830            process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
831
832        if (process_sp)
833        {
834            ProcessAttachInfo &attach_info = sb_attach_info.ref();
835            if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid())
836            {
837                PlatformSP platform_sp = target_sp->GetPlatform();
838                // See if we can pre-verify if a process exists or not
839                if (platform_sp && platform_sp->IsConnected())
840                {
841                    lldb::pid_t attach_pid = attach_info.GetProcessID();
842                    ProcessInstanceInfo instance_info;
843                    if (platform_sp->GetProcessInfo(attach_pid, instance_info))
844                    {
845                        attach_info.SetUserID(instance_info.GetEffectiveUserID());
846                    }
847                    else
848                    {
849                        error.ref().SetErrorStringWithFormat("no process found with process ID %" PRIu64, attach_pid);
850                        if (log)
851                        {
852                            log->Printf ("SBTarget(%p)::Attach (...) => error %s",
853                                         target_sp.get(), error.GetCString());
854                        }
855                        return sb_process;
856                    }
857                }
858            }
859            error.SetError (process_sp->Attach (attach_info));
860            if (error.Success())
861            {
862                sb_process.SetSP (process_sp);
863                // If we are doing synchronous mode, then wait for the
864                // process to stop!
865                if (target_sp->GetDebugger().GetAsyncExecution () == false)
866                    process_sp->WaitForProcessToStop (NULL);
867            }
868        }
869        else
870        {
871            error.SetErrorString ("unable to create lldb_private::Process");
872        }
873    }
874    else
875    {
876        error.SetErrorString ("SBTarget is invalid");
877    }
878
879    if (log)
880    {
881        log->Printf ("SBTarget(%p)::Attach (...) => SBProcess(%p)",
882                     target_sp.get(), process_sp.get());
883    }
884
885    return sb_process;
886}
887
888
889#if defined(__APPLE__)
890
891lldb::SBProcess
892SBTarget::AttachToProcessWithID (SBListener &listener,
893                                ::pid_t pid,
894                                 lldb::SBError& error)
895{
896    return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
897}
898
899#endif // #if defined(__APPLE__)
900
901lldb::SBProcess
902SBTarget::AttachToProcessWithID
903(
904    SBListener &listener,
905    lldb::pid_t pid,// The process ID to attach to
906    SBError& error  // An error explaining what went wrong if attach fails
907)
908{
909    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
910
911    SBProcess sb_process;
912    ProcessSP process_sp;
913    TargetSP target_sp(GetSP());
914
915    if (log)
916    {
917        log->Printf ("SBTarget(%p)::AttachToProcessWithID (listener, pid=%" PRId64 ", error)...", target_sp.get(), pid);
918    }
919
920    if (target_sp)
921    {
922        Mutex::Locker api_locker (target_sp->GetAPIMutex());
923
924        StateType state = eStateInvalid;
925        process_sp = target_sp->GetProcessSP();
926        if (process_sp)
927        {
928            state = process_sp->GetState();
929
930            if (process_sp->IsAlive() && state != eStateConnected)
931            {
932                if (state == eStateAttaching)
933                    error.SetErrorString ("process attach is in progress");
934                else
935                    error.SetErrorString ("a process is already being debugged");
936                return sb_process;
937            }
938        }
939
940        if (state == eStateConnected)
941        {
942            // If we are already connected, then we have already specified the
943            // listener, so if a valid listener is supplied, we need to error out
944            // to let the client know.
945            if (listener.IsValid())
946            {
947                error.SetErrorString ("process is connected and already has a listener, pass empty listener");
948                return sb_process;
949            }
950        }
951        else
952        {
953            if (listener.IsValid())
954                process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
955            else
956                process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
957        }
958        if (process_sp)
959        {
960            sb_process.SetSP (process_sp);
961
962            ProcessAttachInfo attach_info;
963            attach_info.SetProcessID (pid);
964
965            PlatformSP platform_sp = target_sp->GetPlatform();
966            ProcessInstanceInfo instance_info;
967            if (platform_sp->GetProcessInfo(pid, instance_info))
968            {
969                attach_info.SetUserID(instance_info.GetEffectiveUserID());
970            }
971            error.SetError (process_sp->Attach (attach_info));
972            if (error.Success())
973            {
974                // If we are doing synchronous mode, then wait for the
975                // process to stop!
976                if (target_sp->GetDebugger().GetAsyncExecution () == false)
977                process_sp->WaitForProcessToStop (NULL);
978            }
979        }
980        else
981        {
982            error.SetErrorString ("unable to create lldb_private::Process");
983        }
984    }
985    else
986    {
987        error.SetErrorString ("SBTarget is invalid");
988    }
989
990    if (log)
991    {
992        log->Printf ("SBTarget(%p)::AttachToProcessWithID (...) => SBProcess(%p)",
993                     target_sp.get(), process_sp.get());
994    }
995    return sb_process;
996}
997
998lldb::SBProcess
999SBTarget::AttachToProcessWithName
1000(
1001    SBListener &listener,
1002    const char *name,   // basename of process to attach to
1003    bool wait_for,      // if true wait for a new instance of "name" to be launched
1004    SBError& error      // An error explaining what went wrong if attach fails
1005)
1006{
1007    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1008
1009    SBProcess sb_process;
1010    ProcessSP process_sp;
1011    TargetSP target_sp(GetSP());
1012
1013    if (log)
1014    {
1015        log->Printf ("SBTarget(%p)::AttachToProcessWithName (listener, name=%s, wait_for=%s, error)...", target_sp.get(), name, wait_for ? "true" : "false");
1016    }
1017
1018    if (name && target_sp)
1019    {
1020        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1021
1022        StateType state = eStateInvalid;
1023        process_sp = target_sp->GetProcessSP();
1024        if (process_sp)
1025        {
1026            state = process_sp->GetState();
1027
1028            if (process_sp->IsAlive() && state != eStateConnected)
1029            {
1030                if (state == eStateAttaching)
1031                    error.SetErrorString ("process attach is in progress");
1032                else
1033                    error.SetErrorString ("a process is already being debugged");
1034                return sb_process;
1035            }
1036        }
1037
1038        if (state == eStateConnected)
1039        {
1040            // If we are already connected, then we have already specified the
1041            // listener, so if a valid listener is supplied, we need to error out
1042            // to let the client know.
1043            if (listener.IsValid())
1044            {
1045                error.SetErrorString ("process is connected and already has a listener, pass empty listener");
1046                return sb_process;
1047            }
1048        }
1049        else
1050        {
1051            if (listener.IsValid())
1052                process_sp = target_sp->CreateProcess (listener.ref(), NULL, NULL);
1053            else
1054                process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), NULL, NULL);
1055        }
1056
1057        if (process_sp)
1058        {
1059            sb_process.SetSP (process_sp);
1060            ProcessAttachInfo attach_info;
1061            attach_info.GetExecutableFile().SetFile(name, false);
1062            attach_info.SetWaitForLaunch(wait_for);
1063            error.SetError (process_sp->Attach (attach_info));
1064            if (error.Success())
1065            {
1066                // If we are doing synchronous mode, then wait for the
1067                // process to stop!
1068                if (target_sp->GetDebugger().GetAsyncExecution () == false)
1069                    process_sp->WaitForProcessToStop (NULL);
1070            }
1071        }
1072        else
1073        {
1074            error.SetErrorString ("unable to create lldb_private::Process");
1075        }
1076    }
1077    else
1078    {
1079        error.SetErrorString ("SBTarget is invalid");
1080    }
1081
1082    if (log)
1083    {
1084        log->Printf ("SBTarget(%p)::AttachToPorcessWithName (...) => SBProcess(%p)",
1085                     target_sp.get(), process_sp.get());
1086    }
1087    return sb_process;
1088}
1089
1090lldb::SBProcess
1091SBTarget::ConnectRemote
1092(
1093    SBListener &listener,
1094    const char *url,
1095    const char *plugin_name,
1096    SBError& error
1097)
1098{
1099    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1100
1101    SBProcess sb_process;
1102    ProcessSP process_sp;
1103    TargetSP target_sp(GetSP());
1104
1105    if (log)
1106    {
1107        log->Printf ("SBTarget(%p)::ConnectRemote (listener, url=%s, plugin_name=%s, error)...", target_sp.get(), url, plugin_name);
1108    }
1109
1110    if (target_sp)
1111    {
1112        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1113        if (listener.IsValid())
1114            process_sp = target_sp->CreateProcess (listener.ref(), plugin_name, NULL);
1115        else
1116            process_sp = target_sp->CreateProcess (target_sp->GetDebugger().GetListener(), plugin_name, NULL);
1117
1118
1119        if (process_sp)
1120        {
1121            sb_process.SetSP (process_sp);
1122            error.SetError (process_sp->ConnectRemote (NULL, url));
1123        }
1124        else
1125        {
1126            error.SetErrorString ("unable to create lldb_private::Process");
1127        }
1128    }
1129    else
1130    {
1131        error.SetErrorString ("SBTarget is invalid");
1132    }
1133
1134    if (log)
1135    {
1136        log->Printf ("SBTarget(%p)::ConnectRemote (...) => SBProcess(%p)",
1137                     target_sp.get(), process_sp.get());
1138    }
1139    return sb_process;
1140}
1141
1142SBFileSpec
1143SBTarget::GetExecutable ()
1144{
1145
1146    SBFileSpec exe_file_spec;
1147    TargetSP target_sp(GetSP());
1148    if (target_sp)
1149    {
1150        Module *exe_module = target_sp->GetExecutableModulePointer();
1151        if (exe_module)
1152            exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
1153    }
1154
1155    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1156    if (log)
1157    {
1158        log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
1159                     target_sp.get(), exe_file_spec.get());
1160    }
1161
1162    return exe_file_spec;
1163}
1164
1165bool
1166SBTarget::operator == (const SBTarget &rhs) const
1167{
1168    return m_opaque_sp.get() == rhs.m_opaque_sp.get();
1169}
1170
1171bool
1172SBTarget::operator != (const SBTarget &rhs) const
1173{
1174    return m_opaque_sp.get() != rhs.m_opaque_sp.get();
1175}
1176
1177lldb::TargetSP
1178SBTarget::GetSP () const
1179{
1180    return m_opaque_sp;
1181}
1182
1183void
1184SBTarget::SetSP (const lldb::TargetSP& target_sp)
1185{
1186    m_opaque_sp = target_sp;
1187}
1188
1189lldb::SBAddress
1190SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
1191{
1192    lldb::SBAddress sb_addr;
1193    Address &addr = sb_addr.ref();
1194    TargetSP target_sp(GetSP());
1195    if (target_sp)
1196    {
1197        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1198        if (target_sp->ResolveLoadAddress (vm_addr, addr))
1199            return sb_addr;
1200    }
1201
1202    // We have a load address that isn't in a section, just return an address
1203    // with the offset filled in (the address) and the section set to NULL
1204    addr.SetRawAddress(vm_addr);
1205    return sb_addr;
1206}
1207
1208
1209lldb::SBAddress
1210SBTarget::ResolvePastLoadAddress (uint32_t stop_id, lldb::addr_t vm_addr)
1211{
1212    lldb::SBAddress sb_addr;
1213    Address &addr = sb_addr.ref();
1214    TargetSP target_sp(GetSP());
1215    if (target_sp)
1216    {
1217        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1218        if (target_sp->ResolveLoadAddress (vm_addr, addr))
1219            return sb_addr;
1220    }
1221
1222    // We have a load address that isn't in a section, just return an address
1223    // with the offset filled in (the address) and the section set to NULL
1224    addr.SetRawAddress(vm_addr);
1225    return sb_addr;
1226}
1227
1228SBSymbolContext
1229SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr,
1230                                          uint32_t resolve_scope)
1231{
1232    SBSymbolContext sc;
1233    if (addr.IsValid())
1234    {
1235        TargetSP target_sp(GetSP());
1236        if (target_sp)
1237            target_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
1238    }
1239    return sc;
1240}
1241
1242
1243SBBreakpoint
1244SBTarget::BreakpointCreateByLocation (const char *file,
1245                                      uint32_t line)
1246{
1247    return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
1248}
1249
1250SBBreakpoint
1251SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec,
1252                                      uint32_t line)
1253{
1254    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1255
1256    SBBreakpoint sb_bp;
1257    TargetSP target_sp(GetSP());
1258    if (target_sp && line != 0)
1259    {
1260        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1261
1262        const LazyBool check_inlines = eLazyBoolCalculate;
1263        const LazyBool skip_prologue = eLazyBoolCalculate;
1264        const bool internal = false;
1265        const bool hardware = false;
1266        *sb_bp = target_sp->CreateBreakpoint (NULL, *sb_file_spec, line, check_inlines, skip_prologue, internal, hardware);
1267    }
1268
1269    if (log)
1270    {
1271        SBStream sstr;
1272        sb_bp.GetDescription (sstr);
1273        char path[PATH_MAX];
1274        sb_file_spec->GetPath (path, sizeof(path));
1275        log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
1276                     target_sp.get(),
1277                     path,
1278                     line,
1279                     sb_bp.get(),
1280                     sstr.GetData());
1281    }
1282
1283    return sb_bp;
1284}
1285
1286SBBreakpoint
1287SBTarget::BreakpointCreateByName (const char *symbol_name,
1288                                  const char *module_name)
1289{
1290    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1291
1292    SBBreakpoint sb_bp;
1293    TargetSP target_sp(GetSP());
1294    if (target_sp.get())
1295    {
1296        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1297
1298        const bool internal = false;
1299        const bool hardware = false;
1300        const LazyBool skip_prologue = eLazyBoolCalculate;
1301        if (module_name && module_name[0])
1302        {
1303            FileSpecList module_spec_list;
1304            module_spec_list.Append (FileSpec (module_name, false));
1305            *sb_bp = target_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
1306        }
1307        else
1308        {
1309            *sb_bp = target_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, skip_prologue, internal, hardware);
1310        }
1311    }
1312
1313    if (log)
1314    {
1315        log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
1316                     target_sp.get(), symbol_name, module_name, sb_bp.get());
1317    }
1318
1319    return sb_bp;
1320}
1321
1322lldb::SBBreakpoint
1323SBTarget::BreakpointCreateByName (const char *symbol_name,
1324                                  const SBFileSpecList &module_list,
1325                                  const SBFileSpecList &comp_unit_list)
1326{
1327    uint32_t name_type_mask = eFunctionNameTypeAuto;
1328    return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
1329}
1330
1331lldb::SBBreakpoint
1332SBTarget::BreakpointCreateByName (const char *symbol_name,
1333                                  uint32_t name_type_mask,
1334                                  const SBFileSpecList &module_list,
1335                                  const SBFileSpecList &comp_unit_list)
1336{
1337    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1338
1339    SBBreakpoint sb_bp;
1340    TargetSP target_sp(GetSP());
1341    if (target_sp && symbol_name && symbol_name[0])
1342    {
1343        const bool internal = false;
1344        const bool hardware = false;
1345        const LazyBool skip_prologue = eLazyBoolCalculate;
1346        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1347        *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1348                                              comp_unit_list.get(),
1349                                              symbol_name,
1350                                              name_type_mask,
1351                                              skip_prologue,
1352                                              internal,
1353                                              hardware);
1354    }
1355
1356    if (log)
1357    {
1358        log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
1359                     target_sp.get(), symbol_name, name_type_mask, sb_bp.get());
1360    }
1361
1362    return sb_bp;
1363}
1364
1365lldb::SBBreakpoint
1366SBTarget::BreakpointCreateByNames (const char *symbol_names[],
1367                                   uint32_t num_names,
1368                                   uint32_t name_type_mask,
1369                                   const SBFileSpecList &module_list,
1370                                   const SBFileSpecList &comp_unit_list)
1371{
1372    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1373
1374    SBBreakpoint sb_bp;
1375    TargetSP target_sp(GetSP());
1376    if (target_sp && num_names > 0)
1377    {
1378        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1379        const bool internal = false;
1380        const bool hardware = false;
1381        const LazyBool skip_prologue = eLazyBoolCalculate;
1382        *sb_bp = target_sp->CreateBreakpoint (module_list.get(),
1383                                                comp_unit_list.get(),
1384                                                symbol_names,
1385                                                num_names,
1386                                                name_type_mask,
1387                                                skip_prologue,
1388                                                internal,
1389                                                hardware);
1390    }
1391
1392    if (log)
1393    {
1394        log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbols={", target_sp.get());
1395        for (uint32_t i = 0 ; i < num_names; i++)
1396        {
1397            char sep;
1398            if (i < num_names - 1)
1399                sep = ',';
1400            else
1401                sep = '}';
1402            if (symbol_names[i] != NULL)
1403                log->Printf ("\"%s\"%c ", symbol_names[i], sep);
1404            else
1405                log->Printf ("\"<NULL>\"%c ", sep);
1406
1407        }
1408        log->Printf ("name_type: %d) => SBBreakpoint(%p)", name_type_mask, sb_bp.get());
1409    }
1410
1411    return sb_bp;
1412}
1413
1414SBBreakpoint
1415SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1416                                   const char *module_name)
1417{
1418    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1419
1420    SBBreakpoint sb_bp;
1421    TargetSP target_sp(GetSP());
1422    if (target_sp && symbol_name_regex && symbol_name_regex[0])
1423    {
1424        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1425        RegularExpression regexp(symbol_name_regex);
1426        const bool internal = false;
1427        const bool hardware = false;
1428        const LazyBool skip_prologue = eLazyBoolCalculate;
1429
1430        if (module_name && module_name[0])
1431        {
1432            FileSpecList module_spec_list;
1433            module_spec_list.Append (FileSpec (module_name, false));
1434
1435            *sb_bp = target_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, skip_prologue, internal, hardware);
1436        }
1437        else
1438        {
1439            *sb_bp = target_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, skip_prologue, internal, hardware);
1440        }
1441    }
1442
1443    if (log)
1444    {
1445        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1446                     target_sp.get(), symbol_name_regex, module_name, sb_bp.get());
1447    }
1448
1449    return sb_bp;
1450}
1451
1452lldb::SBBreakpoint
1453SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
1454                                   const SBFileSpecList &module_list,
1455                                   const SBFileSpecList &comp_unit_list)
1456{
1457    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1458
1459    SBBreakpoint sb_bp;
1460    TargetSP target_sp(GetSP());
1461    if (target_sp && symbol_name_regex && symbol_name_regex[0])
1462    {
1463        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1464        RegularExpression regexp(symbol_name_regex);
1465        const bool internal = false;
1466        const bool hardware = false;
1467        const LazyBool skip_prologue = eLazyBoolCalculate;
1468
1469        *sb_bp = target_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, skip_prologue, internal, hardware);
1470    }
1471
1472    if (log)
1473    {
1474        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
1475                     target_sp.get(), symbol_name_regex, sb_bp.get());
1476    }
1477
1478    return sb_bp;
1479}
1480
1481SBBreakpoint
1482SBTarget::BreakpointCreateByAddress (addr_t address)
1483{
1484    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1485
1486    SBBreakpoint sb_bp;
1487    TargetSP target_sp(GetSP());
1488    if (target_sp)
1489    {
1490        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1491        const bool hardware = false;
1492        *sb_bp = target_sp->CreateBreakpoint (address, false, hardware);
1493    }
1494
1495    if (log)
1496    {
1497        log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%" PRIu64 ") => SBBreakpoint(%p)", target_sp.get(), (uint64_t) address, sb_bp.get());
1498    }
1499
1500    return sb_bp;
1501}
1502
1503lldb::SBBreakpoint
1504SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1505                                         const lldb::SBFileSpec &source_file,
1506                                         const char *module_name)
1507{
1508    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1509
1510    SBBreakpoint sb_bp;
1511    TargetSP target_sp(GetSP());
1512    if (target_sp && source_regex && source_regex[0])
1513    {
1514        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1515        RegularExpression regexp(source_regex);
1516        FileSpecList source_file_spec_list;
1517        const bool hardware = false;
1518        source_file_spec_list.Append (source_file.ref());
1519
1520        if (module_name && module_name[0])
1521        {
1522            FileSpecList module_spec_list;
1523            module_spec_list.Append (FileSpec (module_name, false));
1524
1525            *sb_bp = target_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false, hardware);
1526        }
1527        else
1528        {
1529            *sb_bp = target_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false, hardware);
1530        }
1531    }
1532
1533    if (log)
1534    {
1535        char path[PATH_MAX];
1536        source_file->GetPath (path, sizeof(path));
1537        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
1538                     target_sp.get(), source_regex, path, module_name, sb_bp.get());
1539    }
1540
1541    return sb_bp;
1542}
1543
1544lldb::SBBreakpoint
1545SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
1546                                         const SBFileSpecList &module_list,
1547                                         const lldb::SBFileSpecList &source_file_list)
1548{
1549    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1550
1551    SBBreakpoint sb_bp;
1552    TargetSP target_sp(GetSP());
1553    if (target_sp && source_regex && source_regex[0])
1554    {
1555        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1556        const bool hardware = false;
1557        RegularExpression regexp(source_regex);
1558        *sb_bp = target_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false, hardware);
1559    }
1560
1561    if (log)
1562    {
1563        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
1564                     target_sp.get(), source_regex, sb_bp.get());
1565    }
1566
1567    return sb_bp;
1568}
1569
1570lldb::SBBreakpoint
1571SBTarget::BreakpointCreateForException  (lldb::LanguageType language,
1572                                         bool catch_bp,
1573                                         bool throw_bp)
1574{
1575    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1576
1577    SBBreakpoint sb_bp;
1578    TargetSP target_sp(GetSP());
1579    if (target_sp)
1580    {
1581        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1582        const bool hardware = false;
1583        *sb_bp = target_sp->CreateExceptionBreakpoint (language, catch_bp, throw_bp, hardware);
1584    }
1585
1586    if (log)
1587    {
1588        log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (Language: %s, catch: %s throw: %s) => SBBreakpoint(%p)",
1589                     target_sp.get(),
1590                     LanguageRuntime::GetNameForLanguageType(language),
1591                     catch_bp ? "on" : "off",
1592                     throw_bp ? "on" : "off",
1593                     sb_bp.get());
1594    }
1595
1596    return sb_bp;
1597}
1598
1599uint32_t
1600SBTarget::GetNumBreakpoints () const
1601{
1602    TargetSP target_sp(GetSP());
1603    if (target_sp)
1604    {
1605        // The breakpoint list is thread safe, no need to lock
1606        return target_sp->GetBreakpointList().GetSize();
1607    }
1608    return 0;
1609}
1610
1611SBBreakpoint
1612SBTarget::GetBreakpointAtIndex (uint32_t idx) const
1613{
1614    SBBreakpoint sb_breakpoint;
1615    TargetSP target_sp(GetSP());
1616    if (target_sp)
1617    {
1618        // The breakpoint list is thread safe, no need to lock
1619        *sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
1620    }
1621    return sb_breakpoint;
1622}
1623
1624bool
1625SBTarget::BreakpointDelete (break_id_t bp_id)
1626{
1627    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1628
1629    bool result = false;
1630    TargetSP target_sp(GetSP());
1631    if (target_sp)
1632    {
1633        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1634        result = target_sp->RemoveBreakpointByID (bp_id);
1635    }
1636
1637    if (log)
1638    {
1639        log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", target_sp.get(), (uint32_t) bp_id, result);
1640    }
1641
1642    return result;
1643}
1644
1645SBBreakpoint
1646SBTarget::FindBreakpointByID (break_id_t bp_id)
1647{
1648    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1649
1650    SBBreakpoint sb_breakpoint;
1651    TargetSP target_sp(GetSP());
1652    if (target_sp && bp_id != LLDB_INVALID_BREAK_ID)
1653    {
1654        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1655        *sb_breakpoint = target_sp->GetBreakpointByID (bp_id);
1656    }
1657
1658    if (log)
1659    {
1660        log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
1661                     target_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
1662    }
1663
1664    return sb_breakpoint;
1665}
1666
1667bool
1668SBTarget::EnableAllBreakpoints ()
1669{
1670    TargetSP target_sp(GetSP());
1671    if (target_sp)
1672    {
1673        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1674        target_sp->EnableAllBreakpoints ();
1675        return true;
1676    }
1677    return false;
1678}
1679
1680bool
1681SBTarget::DisableAllBreakpoints ()
1682{
1683    TargetSP target_sp(GetSP());
1684    if (target_sp)
1685    {
1686        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1687        target_sp->DisableAllBreakpoints ();
1688        return true;
1689    }
1690    return false;
1691}
1692
1693bool
1694SBTarget::DeleteAllBreakpoints ()
1695{
1696    TargetSP target_sp(GetSP());
1697    if (target_sp)
1698    {
1699        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1700        target_sp->RemoveAllBreakpoints ();
1701        return true;
1702    }
1703    return false;
1704}
1705
1706uint32_t
1707SBTarget::GetNumWatchpoints () const
1708{
1709    TargetSP target_sp(GetSP());
1710    if (target_sp)
1711    {
1712        // The watchpoint list is thread safe, no need to lock
1713        return target_sp->GetWatchpointList().GetSize();
1714    }
1715    return 0;
1716}
1717
1718SBWatchpoint
1719SBTarget::GetWatchpointAtIndex (uint32_t idx) const
1720{
1721    SBWatchpoint sb_watchpoint;
1722    TargetSP target_sp(GetSP());
1723    if (target_sp)
1724    {
1725        // The watchpoint list is thread safe, no need to lock
1726        sb_watchpoint.SetSP (target_sp->GetWatchpointList().GetByIndex(idx));
1727    }
1728    return sb_watchpoint;
1729}
1730
1731bool
1732SBTarget::DeleteWatchpoint (watch_id_t wp_id)
1733{
1734    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1735
1736    bool result = false;
1737    TargetSP target_sp(GetSP());
1738    if (target_sp)
1739    {
1740        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1741        Mutex::Locker locker;
1742        target_sp->GetWatchpointList().GetListMutex(locker);
1743        result = target_sp->RemoveWatchpointByID (wp_id);
1744    }
1745
1746    if (log)
1747    {
1748        log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", target_sp.get(), (uint32_t) wp_id, result);
1749    }
1750
1751    return result;
1752}
1753
1754SBWatchpoint
1755SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
1756{
1757    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1758
1759    SBWatchpoint sb_watchpoint;
1760    lldb::WatchpointSP watchpoint_sp;
1761    TargetSP target_sp(GetSP());
1762    if (target_sp && wp_id != LLDB_INVALID_WATCH_ID)
1763    {
1764        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1765        Mutex::Locker locker;
1766        target_sp->GetWatchpointList().GetListMutex(locker);
1767        watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
1768        sb_watchpoint.SetSP (watchpoint_sp);
1769    }
1770
1771    if (log)
1772    {
1773        log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
1774                     target_sp.get(), (uint32_t) wp_id, watchpoint_sp.get());
1775    }
1776
1777    return sb_watchpoint;
1778}
1779
1780lldb::SBWatchpoint
1781SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write, SBError &error)
1782{
1783    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1784
1785    SBWatchpoint sb_watchpoint;
1786    lldb::WatchpointSP watchpoint_sp;
1787    TargetSP target_sp(GetSP());
1788    if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS && size > 0)
1789    {
1790        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1791        uint32_t watch_type = 0;
1792        if (read)
1793            watch_type |= LLDB_WATCH_TYPE_READ;
1794        if (write)
1795            watch_type |= LLDB_WATCH_TYPE_WRITE;
1796        if (watch_type == 0)
1797        {
1798            error.SetErrorString("Can't create a watchpoint that is neither read nor write.");
1799            return sb_watchpoint;
1800        }
1801
1802        // Target::CreateWatchpoint() is thread safe.
1803        Error cw_error;
1804        // This API doesn't take in a type, so we can't figure out what it is.
1805        ClangASTType *type = NULL;
1806        watchpoint_sp = target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
1807        error.SetError(cw_error);
1808        sb_watchpoint.SetSP (watchpoint_sp);
1809    }
1810
1811    if (log)
1812    {
1813        log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%" PRIx64 ", 0x%u) => SBWatchpoint(%p)",
1814                     target_sp.get(), addr, (uint32_t) size, watchpoint_sp.get());
1815    }
1816
1817    return sb_watchpoint;
1818}
1819
1820bool
1821SBTarget::EnableAllWatchpoints ()
1822{
1823    TargetSP target_sp(GetSP());
1824    if (target_sp)
1825    {
1826        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1827        Mutex::Locker locker;
1828        target_sp->GetWatchpointList().GetListMutex(locker);
1829        target_sp->EnableAllWatchpoints ();
1830        return true;
1831    }
1832    return false;
1833}
1834
1835bool
1836SBTarget::DisableAllWatchpoints ()
1837{
1838    TargetSP target_sp(GetSP());
1839    if (target_sp)
1840    {
1841        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1842        Mutex::Locker locker;
1843        target_sp->GetWatchpointList().GetListMutex(locker);
1844        target_sp->DisableAllWatchpoints ();
1845        return true;
1846    }
1847    return false;
1848}
1849
1850SBValue
1851SBTarget::CreateValueFromAddress (const char *name, SBAddress addr, SBType type)
1852{
1853    SBValue sb_value;
1854    lldb::ValueObjectSP new_value_sp;
1855    if (IsValid() && name && *name && addr.IsValid() && type.IsValid())
1856    {
1857        lldb::addr_t address(addr.GetLoadAddress(*this));
1858        lldb::TypeImplSP type_impl_sp (type.GetSP());
1859        ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(true).GetPointerType ());
1860        if (pointer_ast_type)
1861        {
1862            lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
1863
1864            ExecutionContext exe_ctx (ExecutionContextRef(ExecutionContext(m_opaque_sp.get(),false)));
1865            ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
1866                                                                               pointer_ast_type,
1867                                                                               ConstString(name),
1868                                                                               buffer,
1869                                                                               exe_ctx.GetByteOrder(),
1870                                                                               exe_ctx.GetAddressByteSize()));
1871
1872            if (ptr_result_valobj_sp)
1873            {
1874                ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
1875                Error err;
1876                new_value_sp = ptr_result_valobj_sp->Dereference(err);
1877                if (new_value_sp)
1878                    new_value_sp->SetName(ConstString(name));
1879            }
1880        }
1881    }
1882    sb_value.SetSP(new_value_sp);
1883    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1884    if (log)
1885    {
1886        if (new_value_sp)
1887            log->Printf ("SBTarget(%p)::CreateValueFromAddress => \"%s\"", m_opaque_sp.get(), new_value_sp->GetName().AsCString());
1888        else
1889            log->Printf ("SBTarget(%p)::CreateValueFromAddress => NULL", m_opaque_sp.get());
1890    }
1891    return sb_value;
1892}
1893
1894bool
1895SBTarget::DeleteAllWatchpoints ()
1896{
1897    TargetSP target_sp(GetSP());
1898    if (target_sp)
1899    {
1900        Mutex::Locker api_locker (target_sp->GetAPIMutex());
1901        Mutex::Locker locker;
1902        target_sp->GetWatchpointList().GetListMutex(locker);
1903        target_sp->RemoveAllWatchpoints ();
1904        return true;
1905    }
1906    return false;
1907}
1908
1909
1910lldb::SBModule
1911SBTarget::AddModule (const char *path,
1912                     const char *triple,
1913                     const char *uuid_cstr)
1914{
1915    return AddModule (path, triple, uuid_cstr, NULL);
1916}
1917
1918lldb::SBModule
1919SBTarget::AddModule (const char *path,
1920                     const char *triple,
1921                     const char *uuid_cstr,
1922                     const char *symfile)
1923{
1924    lldb::SBModule sb_module;
1925    TargetSP target_sp(GetSP());
1926    if (target_sp)
1927    {
1928        ModuleSpec module_spec;
1929        if (path)
1930            module_spec.GetFileSpec().SetFile(path, false);
1931
1932        if (uuid_cstr)
1933            module_spec.GetUUID().SetFromCString(uuid_cstr);
1934
1935        if (triple)
1936            module_spec.GetArchitecture().SetTriple (triple, target_sp->GetPlatform ().get());
1937        else
1938            module_spec.GetArchitecture() = target_sp->GetArchitecture();
1939
1940        if (symfile)
1941            module_spec.GetSymbolFileSpec ().SetFile(symfile, false);
1942
1943        sb_module.SetSP(target_sp->GetSharedModule (module_spec));
1944    }
1945    return sb_module;
1946}
1947
1948lldb::SBModule
1949SBTarget::AddModule (const SBModuleSpec &module_spec)
1950{
1951    lldb::SBModule sb_module;
1952    TargetSP target_sp(GetSP());
1953    if (target_sp)
1954        sb_module.SetSP(target_sp->GetSharedModule (*module_spec.m_opaque_ap));
1955    return sb_module;
1956}
1957
1958bool
1959SBTarget::AddModule (lldb::SBModule &module)
1960{
1961    TargetSP target_sp(GetSP());
1962    if (target_sp)
1963    {
1964        target_sp->GetImages().AppendIfNeeded (module.GetSP());
1965        return true;
1966    }
1967    return false;
1968}
1969
1970uint32_t
1971SBTarget::GetNumModules () const
1972{
1973    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1974
1975    uint32_t num = 0;
1976    TargetSP target_sp(GetSP());
1977    if (target_sp)
1978    {
1979        // The module list is thread safe, no need to lock
1980        num = target_sp->GetImages().GetSize();
1981    }
1982
1983    if (log)
1984        log->Printf ("SBTarget(%p)::GetNumModules () => %d", target_sp.get(), num);
1985
1986    return num;
1987}
1988
1989void
1990SBTarget::Clear ()
1991{
1992    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1993
1994    if (log)
1995        log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1996
1997    m_opaque_sp.reset();
1998}
1999
2000
2001SBModule
2002SBTarget::FindModule (const SBFileSpec &sb_file_spec)
2003{
2004    SBModule sb_module;
2005    TargetSP target_sp(GetSP());
2006    if (target_sp && sb_file_spec.IsValid())
2007    {
2008        ModuleSpec module_spec(*sb_file_spec);
2009        // The module list is thread safe, no need to lock
2010        sb_module.SetSP (target_sp->GetImages().FindFirstModule (module_spec));
2011    }
2012    return sb_module;
2013}
2014
2015lldb::ByteOrder
2016SBTarget::GetByteOrder ()
2017{
2018    TargetSP target_sp(GetSP());
2019    if (target_sp)
2020        return target_sp->GetArchitecture().GetByteOrder();
2021    return eByteOrderInvalid;
2022}
2023
2024const char *
2025SBTarget::GetTriple ()
2026{
2027    TargetSP target_sp(GetSP());
2028    if (target_sp)
2029    {
2030        std::string triple (target_sp->GetArchitecture().GetTriple().str());
2031        // Unique the string so we don't run into ownership issues since
2032        // the const strings put the string into the string pool once and
2033        // the strings never comes out
2034        ConstString const_triple (triple.c_str());
2035        return const_triple.GetCString();
2036    }
2037    return NULL;
2038}
2039
2040uint32_t
2041SBTarget::GetAddressByteSize()
2042{
2043    TargetSP target_sp(GetSP());
2044    if (target_sp)
2045        return target_sp->GetArchitecture().GetAddressByteSize();
2046    return sizeof(void*);
2047}
2048
2049
2050SBModule
2051SBTarget::GetModuleAtIndex (uint32_t idx)
2052{
2053    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2054
2055    SBModule sb_module;
2056    ModuleSP module_sp;
2057    TargetSP target_sp(GetSP());
2058    if (target_sp)
2059    {
2060        // The module list is thread safe, no need to lock
2061        module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
2062        sb_module.SetSP (module_sp);
2063    }
2064
2065    if (log)
2066    {
2067        log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
2068                     target_sp.get(), idx, module_sp.get());
2069    }
2070
2071    return sb_module;
2072}
2073
2074bool
2075SBTarget::RemoveModule (lldb::SBModule module)
2076{
2077    TargetSP target_sp(GetSP());
2078    if (target_sp)
2079        return target_sp->GetImages().Remove(module.GetSP());
2080    return false;
2081}
2082
2083
2084SBBroadcaster
2085SBTarget::GetBroadcaster () const
2086{
2087    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2088
2089    TargetSP target_sp(GetSP());
2090    SBBroadcaster broadcaster(target_sp.get(), false);
2091
2092    if (log)
2093        log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
2094                     target_sp.get(), broadcaster.get());
2095
2096    return broadcaster;
2097}
2098
2099bool
2100SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
2101{
2102    Stream &strm = description.ref();
2103
2104    TargetSP target_sp(GetSP());
2105    if (target_sp)
2106    {
2107        target_sp->Dump (&strm, description_level);
2108    }
2109    else
2110        strm.PutCString ("No value");
2111
2112    return true;
2113}
2114
2115lldb::SBSymbolContextList
2116SBTarget::FindFunctions (const char *name, uint32_t name_type_mask)
2117{
2118    lldb::SBSymbolContextList sb_sc_list;
2119    if (name && name[0])
2120    {
2121        TargetSP target_sp(GetSP());
2122        if (target_sp)
2123        {
2124            const bool symbols_ok = true;
2125            const bool inlines_ok = true;
2126            const bool append = true;
2127            target_sp->GetImages().FindFunctions (ConstString(name),
2128                                                  name_type_mask,
2129                                                  symbols_ok,
2130                                                  inlines_ok,
2131                                                  append,
2132                                                  *sb_sc_list);
2133        }
2134    }
2135    return sb_sc_list;
2136}
2137
2138lldb::SBType
2139SBTarget::FindFirstType (const char* typename_cstr)
2140{
2141    TargetSP target_sp(GetSP());
2142    if (typename_cstr && typename_cstr[0] && target_sp)
2143    {
2144        ConstString const_typename(typename_cstr);
2145        SymbolContext sc;
2146        const bool exact_match = false;
2147
2148        const ModuleList &module_list = target_sp->GetImages();
2149        size_t count = module_list.GetSize();
2150        for (size_t idx = 0; idx < count; idx++)
2151        {
2152            ModuleSP module_sp (module_list.GetModuleAtIndex(idx));
2153            if (module_sp)
2154            {
2155                TypeSP type_sp (module_sp->FindFirstType(sc, const_typename, exact_match));
2156                if (type_sp)
2157                    return SBType(type_sp);
2158            }
2159        }
2160
2161        // Didn't find the type in the symbols; try the Objective-C runtime
2162        // if one is installed
2163
2164        ProcessSP process_sp(target_sp->GetProcessSP());
2165
2166        if (process_sp)
2167        {
2168            ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2169
2170            if (objc_language_runtime)
2171            {
2172                TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2173
2174                if (objc_type_vendor)
2175                {
2176                    std::vector <ClangASTType> types;
2177
2178                    if (objc_type_vendor->FindTypes(const_typename, true, 1, types) > 0)
2179                        return SBType(types[0]);
2180                }
2181            }
2182        }
2183
2184        // No matches, search for basic typename matches
2185        ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2186        if (clang_ast)
2187            return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename));
2188    }
2189    return SBType();
2190}
2191
2192SBType
2193SBTarget::GetBasicType(lldb::BasicType type)
2194{
2195    TargetSP target_sp(GetSP());
2196    if (target_sp)
2197    {
2198        ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2199        if (clang_ast)
2200            return SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), type));
2201    }
2202    return SBType();
2203}
2204
2205
2206lldb::SBTypeList
2207SBTarget::FindTypes (const char* typename_cstr)
2208{
2209    SBTypeList sb_type_list;
2210    TargetSP target_sp(GetSP());
2211    if (typename_cstr && typename_cstr[0] && target_sp)
2212    {
2213        ModuleList& images = target_sp->GetImages();
2214        ConstString const_typename(typename_cstr);
2215        bool exact_match = false;
2216        SymbolContext sc;
2217        TypeList type_list;
2218
2219        uint32_t num_matches = images.FindTypes (sc,
2220                                                 const_typename,
2221                                                 exact_match,
2222                                                 UINT32_MAX,
2223                                                 type_list);
2224
2225        if (num_matches > 0)
2226        {
2227            for (size_t idx = 0; idx < num_matches; idx++)
2228            {
2229                TypeSP type_sp (type_list.GetTypeAtIndex(idx));
2230                if (type_sp)
2231                    sb_type_list.Append(SBType(type_sp));
2232            }
2233        }
2234
2235        // Try the Objective-C runtime if one is installed
2236
2237        ProcessSP process_sp(target_sp->GetProcessSP());
2238
2239        if (process_sp)
2240        {
2241            ObjCLanguageRuntime *objc_language_runtime = process_sp->GetObjCLanguageRuntime();
2242
2243            if (objc_language_runtime)
2244            {
2245                TypeVendor *objc_type_vendor = objc_language_runtime->GetTypeVendor();
2246
2247                if (objc_type_vendor)
2248                {
2249                    std::vector <ClangASTType> types;
2250
2251                    if (objc_type_vendor->FindTypes(const_typename, true, UINT32_MAX, types))
2252                    {
2253                        for (ClangASTType &type : types)
2254                        {
2255                            sb_type_list.Append(SBType(type));
2256                        }
2257                    }
2258                }
2259            }
2260        }
2261
2262        if (sb_type_list.GetSize() == 0)
2263        {
2264            // No matches, search for basic typename matches
2265            ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
2266            if (clang_ast)
2267                sb_type_list.Append (SBType (ClangASTContext::GetBasicType (clang_ast->getASTContext(), const_typename)));
2268        }
2269    }
2270    return sb_type_list;
2271}
2272
2273SBValueList
2274SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
2275{
2276    SBValueList sb_value_list;
2277
2278    TargetSP target_sp(GetSP());
2279    if (name && target_sp)
2280    {
2281        VariableList variable_list;
2282        const bool append = true;
2283        const uint32_t match_count = target_sp->GetImages().FindGlobalVariables (ConstString (name),
2284                                                                                 append,
2285                                                                                 max_matches,
2286                                                                                 variable_list);
2287
2288        if (match_count > 0)
2289        {
2290            ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
2291            if (exe_scope == NULL)
2292                exe_scope = target_sp.get();
2293            for (uint32_t i=0; i<match_count; ++i)
2294            {
2295                lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
2296                if (valobj_sp)
2297                    sb_value_list.Append(SBValue(valobj_sp));
2298            }
2299        }
2300    }
2301
2302    return sb_value_list;
2303}
2304
2305lldb::SBValue
2306SBTarget::FindFirstGlobalVariable (const char* name)
2307{
2308    SBValueList sb_value_list(FindGlobalVariables(name, 1));
2309    if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
2310        return sb_value_list.GetValueAtIndex(0);
2311    return SBValue();
2312}
2313
2314SBSourceManager
2315SBTarget::GetSourceManager()
2316{
2317    SBSourceManager source_manager (*this);
2318    return source_manager;
2319}
2320
2321lldb::SBInstructionList
2322SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count)
2323{
2324    return ReadInstructions (base_addr, count, NULL);
2325}
2326
2327lldb::SBInstructionList
2328SBTarget::ReadInstructions (lldb::SBAddress base_addr, uint32_t count, const char *flavor_string)
2329{
2330    SBInstructionList sb_instructions;
2331
2332    TargetSP target_sp(GetSP());
2333    if (target_sp)
2334    {
2335        Address *addr_ptr = base_addr.get();
2336
2337        if (addr_ptr)
2338        {
2339            DataBufferHeap data (target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
2340            bool prefer_file_cache = false;
2341            lldb_private::Error error;
2342            lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
2343            const size_t bytes_read = target_sp->ReadMemory(*addr_ptr,
2344                                                            prefer_file_cache,
2345                                                            data.GetBytes(),
2346                                                            data.GetByteSize(),
2347                                                            error,
2348                                                            &load_addr);
2349            const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
2350            sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2351                                                                             NULL,
2352                                                                             flavor_string,
2353                                                                             *addr_ptr,
2354                                                                             data.GetBytes(),
2355                                                                             bytes_read,
2356                                                                             count,
2357                                                                             data_from_file));
2358        }
2359    }
2360
2361    return sb_instructions;
2362
2363}
2364
2365lldb::SBInstructionList
2366SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
2367{
2368    return GetInstructionsWithFlavor (base_addr, NULL, buf, size);
2369}
2370
2371lldb::SBInstructionList
2372SBTarget::GetInstructionsWithFlavor (lldb::SBAddress base_addr, const char *flavor_string, const void *buf, size_t size)
2373{
2374    SBInstructionList sb_instructions;
2375
2376    TargetSP target_sp(GetSP());
2377    if (target_sp)
2378    {
2379        Address addr;
2380
2381        if (base_addr.get())
2382            addr = *base_addr.get();
2383
2384        const bool data_from_file = true;
2385
2386        sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (target_sp->GetArchitecture(),
2387                                                                         NULL,
2388                                                                         flavor_string,
2389                                                                         addr,
2390                                                                         buf,
2391                                                                         size,
2392                                                                         UINT32_MAX,
2393                                                                         data_from_file));
2394    }
2395
2396    return sb_instructions;
2397}
2398
2399lldb::SBInstructionList
2400SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
2401{
2402    return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), NULL, buf, size);
2403}
2404
2405lldb::SBInstructionList
2406SBTarget::GetInstructionsWithFlavor (lldb::addr_t base_addr, const char *flavor_string, const void *buf, size_t size)
2407{
2408    return GetInstructionsWithFlavor (ResolveLoadAddress(base_addr), flavor_string, buf, size);
2409}
2410
2411SBError
2412SBTarget::SetSectionLoadAddress (lldb::SBSection section,
2413                                 lldb::addr_t section_base_addr)
2414{
2415    SBError sb_error;
2416    TargetSP target_sp(GetSP());
2417    if (target_sp)
2418    {
2419        if (!section.IsValid())
2420        {
2421            sb_error.SetErrorStringWithFormat ("invalid section");
2422        }
2423        else
2424        {
2425            SectionSP section_sp (section.GetSP());
2426            if (section_sp)
2427            {
2428                if (section_sp->IsThreadSpecific())
2429                {
2430                    sb_error.SetErrorString ("thread specific sections are not yet supported");
2431                }
2432                else
2433                {
2434                    ProcessSP process_sp (target_sp->GetProcessSP());
2435                    uint32_t stop_id = 0;
2436                    if (process_sp)
2437                        stop_id = process_sp->GetStopID();
2438
2439                    if (target_sp->SetSectionLoadAddress (section_sp, section_base_addr))
2440                    {
2441                        // Flush info in the process (stack frames, etc)
2442                        if (process_sp)
2443                            process_sp->Flush();
2444                    }
2445                }
2446            }
2447        }
2448    }
2449    else
2450    {
2451        sb_error.SetErrorString ("invalid target");
2452    }
2453    return sb_error;
2454}
2455
2456SBError
2457SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
2458{
2459    SBError sb_error;
2460
2461    TargetSP target_sp(GetSP());
2462    if (target_sp)
2463    {
2464        if (!section.IsValid())
2465        {
2466            sb_error.SetErrorStringWithFormat ("invalid section");
2467        }
2468        else
2469        {
2470            ProcessSP process_sp (target_sp->GetProcessSP());
2471            uint32_t stop_id = 0;
2472            if (process_sp)
2473                stop_id = process_sp->GetStopID();
2474
2475            if (target_sp->SetSectionUnloaded (section.GetSP()))
2476            {
2477                // Flush info in the process (stack frames, etc)
2478                if (process_sp)
2479                    process_sp->Flush();
2480            }
2481        }
2482    }
2483    else
2484    {
2485        sb_error.SetErrorStringWithFormat ("invalid target");
2486    }
2487    return sb_error;
2488}
2489
2490SBError
2491SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
2492{
2493    SBError sb_error;
2494
2495    TargetSP target_sp(GetSP());
2496    if (target_sp)
2497    {
2498        ModuleSP module_sp (module.GetSP());
2499        if (module_sp)
2500        {
2501            bool changed = false;
2502            if (module_sp->SetLoadAddress (*target_sp, slide_offset, true, changed))
2503            {
2504                // The load was successful, make sure that at least some sections
2505                // changed before we notify that our module was loaded.
2506                if (changed)
2507                {
2508                    ModuleList module_list;
2509                    module_list.Append(module_sp);
2510                    target_sp->ModulesDidLoad (module_list);
2511                    // Flush info in the process (stack frames, etc)
2512                    ProcessSP process_sp (target_sp->GetProcessSP());
2513                    if (process_sp)
2514                        process_sp->Flush();
2515                }
2516            }
2517        }
2518        else
2519        {
2520            sb_error.SetErrorStringWithFormat ("invalid module");
2521        }
2522
2523    }
2524    else
2525    {
2526        sb_error.SetErrorStringWithFormat ("invalid target");
2527    }
2528    return sb_error;
2529}
2530
2531SBError
2532SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
2533{
2534    SBError sb_error;
2535
2536    char path[PATH_MAX];
2537    TargetSP target_sp(GetSP());
2538    if (target_sp)
2539    {
2540        ModuleSP module_sp (module.GetSP());
2541        if (module_sp)
2542        {
2543            ObjectFile *objfile = module_sp->GetObjectFile();
2544            if (objfile)
2545            {
2546                SectionList *section_list = objfile->GetSectionList();
2547                if (section_list)
2548                {
2549                    ProcessSP process_sp (target_sp->GetProcessSP());
2550                    uint32_t stop_id = 0;
2551                    if (process_sp)
2552                        stop_id = process_sp->GetStopID();
2553
2554                    bool changed = false;
2555                    const size_t num_sections = section_list->GetSize();
2556                    for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2557                    {
2558                        SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2559                        if (section_sp)
2560                            changed |= target_sp->SetSectionUnloaded (section_sp) > 0;
2561                    }
2562                    if (changed)
2563                    {
2564                        // Flush info in the process (stack frames, etc)
2565                        ProcessSP process_sp (target_sp->GetProcessSP());
2566                        if (process_sp)
2567                            process_sp->Flush();
2568                    }
2569                }
2570                else
2571                {
2572                    module_sp->GetFileSpec().GetPath (path, sizeof(path));
2573                    sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
2574                }
2575            }
2576            else
2577            {
2578                module_sp->GetFileSpec().GetPath (path, sizeof(path));
2579                sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
2580            }
2581        }
2582        else
2583        {
2584            sb_error.SetErrorStringWithFormat ("invalid module");
2585        }
2586    }
2587    else
2588    {
2589        sb_error.SetErrorStringWithFormat ("invalid target");
2590    }
2591    return sb_error;
2592}
2593
2594
2595lldb::SBSymbolContextList
2596SBTarget::FindSymbols (const char *name, lldb::SymbolType symbol_type)
2597{
2598    SBSymbolContextList sb_sc_list;
2599    if (name && name[0])
2600    {
2601        TargetSP target_sp(GetSP());
2602        if (target_sp)
2603        {
2604            bool append = true;
2605            target_sp->GetImages().FindSymbolsWithNameAndType (ConstString(name),
2606                                                               symbol_type,
2607                                                               *sb_sc_list,
2608                                                               append);
2609        }
2610    }
2611    return sb_sc_list;
2612
2613}
2614
2615
2616lldb::SBValue
2617SBTarget::EvaluateExpression (const char *expr, const SBExpressionOptions &options)
2618{
2619    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
2620    Log * expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2621    SBValue expr_result;
2622    ExecutionResults exe_results = eExecutionSetupError;
2623    ValueObjectSP expr_value_sp;
2624    TargetSP target_sp(GetSP());
2625    StackFrame *frame = NULL;
2626    if (target_sp)
2627    {
2628        if (expr == NULL || expr[0] == '\0')
2629        {
2630            if (log)
2631                log->Printf ("SBTarget::EvaluateExpression called with an empty expression");
2632            return expr_result;
2633        }
2634
2635        Mutex::Locker api_locker (target_sp->GetAPIMutex());
2636        ExecutionContext exe_ctx (m_opaque_sp.get());
2637
2638        if (log)
2639            log->Printf ("SBTarget()::EvaluateExpression (expr=\"%s\")...", expr);
2640
2641        frame = exe_ctx.GetFramePtr();
2642        Target *target = exe_ctx.GetTargetPtr();
2643
2644        if (target)
2645        {
2646#ifdef LLDB_CONFIGURATION_DEBUG
2647            StreamString frame_description;
2648            if (frame)
2649                frame->DumpUsingSettingsFormat (&frame_description);
2650            Host::SetCrashDescriptionWithFormat ("SBTarget::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
2651                                                 expr, options.GetFetchDynamicValue(), frame_description.GetString().c_str());
2652#endif
2653            exe_results = target->EvaluateExpression (expr,
2654                                                      frame,
2655                                                      expr_value_sp,
2656                                                      options.ref());
2657
2658            expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
2659#ifdef LLDB_CONFIGURATION_DEBUG
2660            Host::SetCrashDescription (NULL);
2661#endif
2662        }
2663        else
2664        {
2665            if (log)
2666                log->Printf ("SBTarget::EvaluateExpression () => error: could not reconstruct frame object for this SBTarget.");
2667        }
2668    }
2669#ifndef LLDB_DISABLE_PYTHON
2670    if (expr_log)
2671        expr_log->Printf("** [SBTarget::EvaluateExpression] Expression result is %s, summary %s **",
2672                         expr_result.GetValue(),
2673                         expr_result.GetSummary());
2674
2675    if (log)
2676        log->Printf ("SBTarget(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
2677                     frame,
2678                     expr,
2679                     expr_value_sp.get(),
2680                     exe_results);
2681#endif
2682
2683    return expr_result;
2684}
2685
2686
2687lldb::addr_t
2688SBTarget::GetStackRedZoneSize()
2689{
2690    TargetSP target_sp(GetSP());
2691    if (target_sp)
2692    {
2693        ABISP abi_sp;
2694        ProcessSP process_sp (target_sp->GetProcessSP());
2695        if (process_sp)
2696            abi_sp = process_sp->GetABI();
2697        else
2698            abi_sp = ABI::FindPlugin(target_sp->GetArchitecture());
2699        if (abi_sp)
2700            return abi_sp->GetRedZoneSize();
2701    }
2702    return 0;
2703}
2704
2705