1 //===-- SBTarget.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/API/SBTarget.h"
11 
12 #include "lldb/lldb-public.h"
13 
14 #include "lldb/API/SBDebugger.h"
15 #include "lldb/API/SBBreakpoint.h"
16 #include "lldb/API/SBFileSpec.h"
17 #include "lldb/API/SBListener.h"
18 #include "lldb/API/SBModule.h"
19 #include "lldb/API/SBSourceManager.h"
20 #include "lldb/API/SBProcess.h"
21 #include "lldb/API/SBStream.h"
22 #include "lldb/API/SBSymbolContextList.h"
23 #include "lldb/Breakpoint/BreakpointID.h"
24 #include "lldb/Breakpoint/BreakpointIDList.h"
25 #include "lldb/Breakpoint/BreakpointList.h"
26 #include "lldb/Breakpoint/BreakpointLocation.h"
27 #include "lldb/Core/Address.h"
28 #include "lldb/Core/AddressResolver.h"
29 #include "lldb/Core/AddressResolverName.h"
30 #include "lldb/Core/ArchSpec.h"
31 #include "lldb/Core/Debugger.h"
32 #include "lldb/Core/Disassembler.h"
33 #include "lldb/Core/Log.h"
34 #include "lldb/Core/RegularExpression.h"
35 #include "lldb/Core/SearchFilter.h"
36 #include "lldb/Core/STLUtils.h"
37 #include "lldb/Core/ValueObjectList.h"
38 #include "lldb/Core/ValueObjectVariable.h"
39 #include "lldb/Host/FileSpec.h"
40 #include "lldb/Host/Host.h"
41 #include "lldb/Interpreter/Args.h"
42 #include "lldb/Symbol/SymbolVendor.h"
43 #include "lldb/Symbol/VariableList.h"
44 #include "lldb/Target/Process.h"
45 #include "lldb/Target/Target.h"
46 #include "lldb/Target/TargetList.h"
47 
48 #include "lldb/Interpreter/CommandReturnObject.h"
49 #include "../source/Commands/CommandObjectBreakpoint.h"
50 
51 
52 using namespace lldb;
53 using namespace lldb_private;
54 
55 #define DEFAULT_DISASM_BYTE_SIZE 32
56 
57 //----------------------------------------------------------------------
58 // SBTarget constructor
59 //----------------------------------------------------------------------
60 SBTarget::SBTarget () :
61     m_opaque_sp ()
62 {
63 }
64 
65 SBTarget::SBTarget (const SBTarget& rhs) :
66     m_opaque_sp (rhs.m_opaque_sp)
67 {
68 }
69 
70 SBTarget::SBTarget(const TargetSP& target_sp) :
71     m_opaque_sp (target_sp)
72 {
73 }
74 
75 const SBTarget&
76 SBTarget::operator = (const SBTarget& rhs)
77 {
78     if (this != &rhs)
79         m_opaque_sp = rhs.m_opaque_sp;
80     return *this;
81 }
82 
83 //----------------------------------------------------------------------
84 // Destructor
85 //----------------------------------------------------------------------
86 SBTarget::~SBTarget()
87 {
88 }
89 
90 bool
91 SBTarget::IsValid () const
92 {
93     return m_opaque_sp.get() != NULL;
94 }
95 
96 SBProcess
97 SBTarget::GetProcess ()
98 {
99 
100     SBProcess sb_process;
101     if (m_opaque_sp)
102     {
103         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
104         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
105     }
106 
107     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
108     if (log)
109     {
110         log->Printf ("SBTarget(%p)::GetProcess () => SBProcess(%p)",
111                      m_opaque_sp.get(), sb_process.get());
112     }
113 
114     return sb_process;
115 }
116 
117 SBDebugger
118 SBTarget::GetDebugger () const
119 {
120     SBDebugger debugger;
121     if (m_opaque_sp)
122         debugger.reset (m_opaque_sp->GetDebugger().GetSP());
123     return debugger;
124 }
125 
126 SBProcess
127 SBTarget::LaunchSimple
128 (
129     char const **argv,
130     char const **envp,
131     const char *working_directory
132 )
133 {
134     char *stdin_path = NULL;
135     char *stdout_path = NULL;
136     char *stderr_path = NULL;
137     uint32_t launch_flags = 0;
138     bool stop_at_entry = false;
139     SBError error;
140     SBListener listener = GetDebugger().GetListener();
141     return Launch (listener,
142                    argv,
143                    envp,
144                    stdin_path,
145                    stdout_path,
146                    stderr_path,
147                    working_directory,
148                    launch_flags,
149                    stop_at_entry,
150                    error);
151 }
152 
153 SBProcess
154 SBTarget::Launch
155 (
156     SBListener &listener,
157     char const **argv,
158     char const **envp,
159     const char *stdin_path,
160     const char *stdout_path,
161     const char *stderr_path,
162     const char *working_directory,
163     uint32_t launch_flags,   // See LaunchFlags
164     bool stop_at_entry,
165     lldb::SBError& error
166 )
167 {
168     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
169 
170     if (log)
171     {
172         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))...",
173                      m_opaque_sp.get(),
174                      argv,
175                      envp,
176                      stdin_path ? stdin_path : "NULL",
177                      stdout_path ? stdout_path : "NULL",
178                      stderr_path ? stderr_path : "NULL",
179                      working_directory ? working_directory : "NULL",
180                      launch_flags,
181                      stop_at_entry,
182                      error.get());
183     }
184     SBProcess sb_process;
185     if (m_opaque_sp)
186     {
187         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
188 
189         if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
190             launch_flags |= eLaunchFlagDisableASLR;
191 
192         StateType state = eStateInvalid;
193         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
194         if (sb_process.IsValid())
195         {
196             state = sb_process->GetState();
197 
198             if (sb_process->IsAlive() && state != eStateConnected)
199             {
200                 if (state == eStateAttaching)
201                     error.SetErrorString ("process attach is in progress");
202                 else
203                     error.SetErrorString ("a process is already being debugged");
204                 sb_process.Clear();
205                 return sb_process;
206             }
207         }
208 
209         if (state == eStateConnected)
210         {
211             // If we are already connected, then we have already specified the
212             // listener, so if a valid listener is supplied, we need to error out
213             // to let the client know.
214             if (listener.IsValid())
215             {
216                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
217                 sb_process.Clear();
218                 return sb_process;
219             }
220         }
221         else
222         {
223             if (listener.IsValid())
224                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
225             else
226                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
227         }
228 
229         if (sb_process.IsValid())
230         {
231             if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
232                 launch_flags |= eLaunchFlagDisableSTDIO;
233 
234             error.SetError (sb_process->Launch (argv, envp, launch_flags, stdin_path, stdout_path, stderr_path, working_directory));
235             if (error.Success())
236             {
237                 // We we are stopping at the entry point, we can return now!
238                 if (stop_at_entry)
239                     return sb_process;
240 
241                 // Make sure we are stopped at the entry
242                 StateType state = sb_process->WaitForProcessToStop (NULL);
243                 if (state == eStateStopped)
244                 {
245                     // resume the process to skip the entry point
246                     error.SetError (sb_process->Resume());
247                     if (error.Success())
248                     {
249                         // If we are doing synchronous mode, then wait for the
250                         // process to stop yet again!
251                         if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
252                             sb_process->WaitForProcessToStop (NULL);
253                     }
254                 }
255             }
256         }
257         else
258         {
259             error.SetErrorString ("unable to create lldb_private::Process");
260         }
261     }
262     else
263     {
264         error.SetErrorString ("SBTarget is invalid");
265     }
266 
267     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
268     if (log)
269     {
270         log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
271                      m_opaque_sp.get(), sb_process.get());
272     }
273 
274     return sb_process;
275 }
276 
277 
278 lldb::SBProcess
279 SBTarget::AttachToProcessWithID
280 (
281     SBListener &listener,
282     lldb::pid_t pid,// The process ID to attach to
283     SBError& error  // An error explaining what went wrong if attach fails
284 )
285 {
286     SBProcess sb_process;
287     if (m_opaque_sp)
288     {
289         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
290 
291         StateType state = eStateInvalid;
292         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
293         if (sb_process.IsValid())
294         {
295             state = sb_process->GetState();
296 
297             if (sb_process->IsAlive() && state != eStateConnected)
298             {
299                 if (state == eStateAttaching)
300                     error.SetErrorString ("process attach is in progress");
301                 else
302                     error.SetErrorString ("a process is already being debugged");
303                 sb_process.Clear();
304                 return sb_process;
305             }
306         }
307 
308         if (state == eStateConnected)
309         {
310             // If we are already connected, then we have already specified the
311             // listener, so if a valid listener is supplied, we need to error out
312             // to let the client know.
313             if (listener.IsValid())
314             {
315                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
316                 sb_process.Clear();
317                 return sb_process;
318             }
319         }
320         else
321         {
322             if (listener.IsValid())
323                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
324             else
325                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
326         }
327 
328         if (sb_process.IsValid())
329         {
330             error.SetError (sb_process->Attach (pid));
331             // If we are doing synchronous mode, then wait for the
332             // process to stop!
333             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
334                 sb_process->WaitForProcessToStop (NULL);
335         }
336         else
337         {
338             error.SetErrorString ("unable to create lldb_private::Process");
339         }
340     }
341     else
342     {
343         error.SetErrorString ("SBTarget is invalid");
344     }
345     return sb_process;
346 
347 }
348 
349 lldb::SBProcess
350 SBTarget::AttachToProcessWithName
351 (
352     SBListener &listener,
353     const char *name,   // basename of process to attach to
354     bool wait_for,      // if true wait for a new instance of "name" to be launched
355     SBError& error      // An error explaining what went wrong if attach fails
356 )
357 {
358     SBProcess sb_process;
359     if (m_opaque_sp)
360     {
361         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
362 
363         StateType state = eStateInvalid;
364         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
365         if (sb_process.IsValid())
366         {
367             state = sb_process->GetState();
368 
369             if (sb_process->IsAlive() && state != eStateConnected)
370             {
371                 if (state == eStateAttaching)
372                     error.SetErrorString ("process attach is in progress");
373                 else
374                     error.SetErrorString ("a process is already being debugged");
375                 sb_process.Clear();
376                 return sb_process;
377             }
378         }
379 
380         if (state == eStateConnected)
381         {
382             // If we are already connected, then we have already specified the
383             // listener, so if a valid listener is supplied, we need to error out
384             // to let the client know.
385             if (listener.IsValid())
386             {
387                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
388                 sb_process.Clear();
389                 return sb_process;
390             }
391         }
392         else
393         {
394             if (listener.IsValid())
395                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
396             else
397                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
398         }
399 
400         if (sb_process.IsValid())
401         {
402             error.SetError (sb_process->Attach (name, wait_for));
403             // If we are doing synchronous mode, then wait for the
404             // process to stop!
405             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
406                 sb_process->WaitForProcessToStop (NULL);
407         }
408         else
409         {
410             error.SetErrorString ("unable to create lldb_private::Process");
411         }
412     }
413     else
414     {
415         error.SetErrorString ("SBTarget is invalid");
416     }
417     return sb_process;
418 
419 }
420 
421 lldb::SBProcess
422 SBTarget::ConnectRemote
423 (
424     SBListener &listener,
425     const char *url,
426     const char *plugin_name,
427     SBError& error
428 )
429 {
430     SBProcess sb_process;
431     if (m_opaque_sp)
432     {
433         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
434         if (listener.IsValid())
435             sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
436         else
437             sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
438 
439 
440         if (sb_process.IsValid())
441         {
442             error.SetError (sb_process->ConnectRemote (url));
443         }
444         else
445         {
446             error.SetErrorString ("unable to create lldb_private::Process");
447         }
448     }
449     else
450     {
451         error.SetErrorString ("SBTarget is invalid");
452     }
453     return sb_process;
454 }
455 
456 SBFileSpec
457 SBTarget::GetExecutable ()
458 {
459 
460     SBFileSpec exe_file_spec;
461     if (m_opaque_sp)
462     {
463         Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
464         if (exe_module)
465             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
466     }
467 
468     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
469     if (log)
470     {
471         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
472                      m_opaque_sp.get(), exe_file_spec.get());
473     }
474 
475     return exe_file_spec;
476 }
477 
478 bool
479 SBTarget::operator == (const SBTarget &rhs) const
480 {
481     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
482 }
483 
484 bool
485 SBTarget::operator != (const SBTarget &rhs) const
486 {
487     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
488 }
489 
490 lldb_private::Target *
491 SBTarget::operator ->() const
492 {
493     return m_opaque_sp.get();
494 }
495 
496 lldb_private::Target *
497 SBTarget::get() const
498 {
499     return m_opaque_sp.get();
500 }
501 
502 void
503 SBTarget::reset (const lldb::TargetSP& target_sp)
504 {
505     m_opaque_sp = target_sp;
506 }
507 
508 lldb::SBAddress
509 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
510 {
511     lldb::SBAddress sb_addr;
512     Address &addr = sb_addr.ref();
513     if (m_opaque_sp)
514     {
515         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
516         if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
517             return sb_addr;
518     }
519 
520     // We have a load address that isn't in a section, just return an address
521     // with the offset filled in (the address) and the section set to NULL
522     addr.SetSection(NULL);
523     addr.SetOffset(vm_addr);
524     return sb_addr;
525 }
526 
527 SBSymbolContext
528 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
529 {
530     SBSymbolContext sc;
531     if (m_opaque_sp && addr.IsValid())
532         m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
533     return sc;
534 }
535 
536 
537 SBBreakpoint
538 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
539 {
540     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
541 }
542 
543 SBBreakpoint
544 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
545 {
546     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
547 
548     SBBreakpoint sb_bp;
549     if (m_opaque_sp.get() && line != 0)
550     {
551         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
552         *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
553     }
554 
555     if (log)
556     {
557         SBStream sstr;
558         sb_bp.GetDescription (sstr);
559         char path[PATH_MAX];
560         sb_file_spec->GetPath (path, sizeof(path));
561         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
562                      m_opaque_sp.get(),
563                      path,
564                      line,
565                      sb_bp.get(),
566                      sstr.GetData());
567     }
568 
569     return sb_bp;
570 }
571 
572 SBBreakpoint
573 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
574 {
575     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
576 
577     SBBreakpoint sb_bp;
578     if (m_opaque_sp.get() && symbol_name && symbol_name[0])
579     {
580         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
581         if (module_name && module_name[0])
582         {
583             FileSpec module_file_spec(module_name, false);
584             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
585         }
586         else
587         {
588             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
589         }
590     }
591 
592     if (log)
593     {
594         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
595                      m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
596     }
597 
598     return sb_bp;
599 }
600 
601 SBBreakpoint
602 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
603 {
604     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
605 
606     SBBreakpoint sb_bp;
607     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
608     {
609         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
610         RegularExpression regexp(symbol_name_regex);
611 
612         if (module_name && module_name[0])
613         {
614             FileSpec module_file_spec(module_name, false);
615 
616             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_file_spec, regexp, false);
617         }
618         else
619         {
620             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, regexp, false);
621         }
622     }
623 
624     if (log)
625     {
626         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
627                      m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
628     }
629 
630     return sb_bp;
631 }
632 
633 
634 
635 SBBreakpoint
636 SBTarget::BreakpointCreateByAddress (addr_t address)
637 {
638     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
639 
640     SBBreakpoint sb_bp;
641     if (m_opaque_sp.get())
642     {
643         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
644         *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
645     }
646 
647     if (log)
648     {
649         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (%p, address=%p) => SBBreakpoint(%p)", m_opaque_sp.get(), address, sb_bp.get());
650     }
651 
652     return sb_bp;
653 }
654 
655 SBBreakpoint
656 SBTarget::FindBreakpointByID (break_id_t bp_id)
657 {
658     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
659 
660     SBBreakpoint sb_breakpoint;
661     if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
662     {
663         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
664         *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
665     }
666 
667     if (log)
668     {
669         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
670                      m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
671     }
672 
673     return sb_breakpoint;
674 }
675 
676 uint32_t
677 SBTarget::GetNumBreakpoints () const
678 {
679     if (m_opaque_sp)
680     {
681         // The breakpoint list is thread safe, no need to lock
682         return m_opaque_sp->GetBreakpointList().GetSize();
683     }
684     return 0;
685 }
686 
687 SBBreakpoint
688 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
689 {
690     SBBreakpoint sb_breakpoint;
691     if (m_opaque_sp)
692     {
693         // The breakpoint list is thread safe, no need to lock
694         *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
695     }
696     return sb_breakpoint;
697 }
698 
699 bool
700 SBTarget::BreakpointDelete (break_id_t bp_id)
701 {
702     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
703 
704     bool result = false;
705     if (m_opaque_sp)
706     {
707         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
708         result = m_opaque_sp->RemoveBreakpointByID (bp_id);
709     }
710 
711     if (log)
712     {
713         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
714     }
715 
716     return result;
717 }
718 
719 bool
720 SBTarget::EnableAllBreakpoints ()
721 {
722     if (m_opaque_sp)
723     {
724         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
725         m_opaque_sp->EnableAllBreakpoints ();
726         return true;
727     }
728     return false;
729 }
730 
731 bool
732 SBTarget::DisableAllBreakpoints ()
733 {
734     if (m_opaque_sp)
735     {
736         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
737         m_opaque_sp->DisableAllBreakpoints ();
738         return true;
739     }
740     return false;
741 }
742 
743 bool
744 SBTarget::DeleteAllBreakpoints ()
745 {
746     if (m_opaque_sp)
747     {
748         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
749         m_opaque_sp->RemoveAllBreakpoints ();
750         return true;
751     }
752     return false;
753 }
754 
755 
756 uint32_t
757 SBTarget::GetNumModules () const
758 {
759     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
760 
761     uint32_t num = 0;
762     if (m_opaque_sp)
763     {
764         // The module list is thread safe, no need to lock
765         num = m_opaque_sp->GetImages().GetSize();
766     }
767 
768     if (log)
769         log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
770 
771     return num;
772 }
773 
774 void
775 SBTarget::Clear ()
776 {
777     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
778 
779     if (log)
780         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
781 
782     m_opaque_sp.reset();
783 }
784 
785 
786 SBModule
787 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
788 {
789     SBModule sb_module;
790     if (m_opaque_sp && sb_file_spec.IsValid())
791     {
792         // The module list is thread safe, no need to lock
793         sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
794     }
795     return sb_module;
796 }
797 
798 SBModule
799 SBTarget::GetModuleAtIndex (uint32_t idx)
800 {
801     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
802 
803     SBModule sb_module;
804     if (m_opaque_sp)
805     {
806         // The module list is thread safe, no need to lock
807         sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
808     }
809 
810     if (log)
811     {
812         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
813                      m_opaque_sp.get(), idx, sb_module.get());
814     }
815 
816     return sb_module;
817 }
818 
819 
820 SBBroadcaster
821 SBTarget::GetBroadcaster () const
822 {
823     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
824 
825     SBBroadcaster broadcaster(m_opaque_sp.get(), false);
826 
827     if (log)
828         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
829                      m_opaque_sp.get(), broadcaster.get());
830 
831     return broadcaster;
832 }
833 
834 
835 bool
836 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
837 {
838     if (m_opaque_sp)
839     {
840         description.ref();
841         m_opaque_sp->Dump (description.get(), description_level);
842     }
843     else
844         description.Printf ("No value");
845 
846     return true;
847 }
848 
849 bool
850 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
851 {
852     if (m_opaque_sp)
853     {
854         description.ref();
855         m_opaque_sp->Dump (description.get(), description_level);
856     }
857     else
858         description.Printf ("No value");
859 
860     return true;
861 }
862 
863 
864 uint32_t
865 SBTarget::FindFunctions (const char *name,
866                          uint32_t name_type_mask,
867                          bool append,
868                          lldb::SBSymbolContextList& sc_list)
869 {
870     if (!append)
871         sc_list.Clear();
872     if (m_opaque_sp)
873     {
874         const bool symbols_ok = true;
875         return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
876                                                        name_type_mask,
877                                                        symbols_ok,
878                                                        append,
879                                                        *sc_list);
880     }
881     return 0;
882 }
883 
884 lldb::SBType
885 SBTarget::FindFirstType (const char* type)
886 {
887     if (m_opaque_sp)
888     {
889         size_t count = m_opaque_sp->GetImages().GetSize();
890         for (size_t idx = 0; idx < count; idx++)
891         {
892             SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
893 
894             if (found_at_idx.IsValid())
895                 return found_at_idx;
896         }
897     }
898     return SBType();
899 }
900 
901 lldb::SBTypeList
902 SBTarget::FindTypes (const char* type)
903 {
904 
905     SBTypeList retval;
906 
907     if (m_opaque_sp)
908     {
909         ModuleList& images = m_opaque_sp->GetImages();
910         ConstString name_const(type);
911         SymbolContext sc;
912         TypeList type_list;
913 
914         uint32_t num_matches = images.FindTypes(sc,
915                                                 name_const,
916                                                 true,
917                                                 UINT32_MAX,
918                                                 type_list);
919 
920         for (size_t idx = 0; idx < num_matches; idx++)
921         {
922             TypeSP type_sp (type_list.GetTypeAtIndex(idx));
923             if (type_sp)
924                 retval.Append(SBType(type_sp));
925         }
926     }
927     return retval;
928 }
929 
930 SBValueList
931 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
932 {
933     SBValueList sb_value_list;
934 
935     if (m_opaque_sp)
936     {
937         VariableList variable_list;
938         const bool append = true;
939         const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
940                                                                                    append,
941                                                                                    max_matches,
942                                                                                    variable_list);
943 
944         if (match_count > 0)
945         {
946             ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
947             if (exe_scope == NULL)
948                 exe_scope = m_opaque_sp.get();
949             ValueObjectList &value_object_list = sb_value_list.ref();
950             for (uint32_t i=0; i<match_count; ++i)
951             {
952                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
953                 if (valobj_sp)
954                     value_object_list.Append(valobj_sp);
955             }
956         }
957     }
958 
959     return sb_value_list;
960 }
961 
962 SBSourceManager
963 SBTarget::GetSourceManager()
964 {
965     SBSourceManager source_manager (*this);
966     return source_manager;
967 }
968