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