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 const lldb::TargetSP &
503 SBTarget::get_sp () const
504 {
505     return m_opaque_sp;
506 }
507 
508 void
509 SBTarget::reset (const lldb::TargetSP& target_sp)
510 {
511     m_opaque_sp = target_sp;
512 }
513 
514 lldb::SBAddress
515 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
516 {
517     lldb::SBAddress sb_addr;
518     Address &addr = sb_addr.ref();
519     if (m_opaque_sp)
520     {
521         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
522         if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
523             return sb_addr;
524     }
525 
526     // We have a load address that isn't in a section, just return an address
527     // with the offset filled in (the address) and the section set to NULL
528     addr.SetSection(NULL);
529     addr.SetOffset(vm_addr);
530     return sb_addr;
531 }
532 
533 SBSymbolContext
534 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
535 {
536     SBSymbolContext sc;
537     if (m_opaque_sp && addr.IsValid())
538         m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
539     return sc;
540 }
541 
542 
543 SBBreakpoint
544 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
545 {
546     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
547 }
548 
549 SBBreakpoint
550 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
551 {
552     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
553 
554     SBBreakpoint sb_bp;
555     if (m_opaque_sp.get() && line != 0)
556     {
557         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
558         *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
559     }
560 
561     if (log)
562     {
563         SBStream sstr;
564         sb_bp.GetDescription (sstr);
565         char path[PATH_MAX];
566         sb_file_spec->GetPath (path, sizeof(path));
567         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
568                      m_opaque_sp.get(),
569                      path,
570                      line,
571                      sb_bp.get(),
572                      sstr.GetData());
573     }
574 
575     return sb_bp;
576 }
577 
578 SBBreakpoint
579 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
580 {
581     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
582 
583     SBBreakpoint sb_bp;
584     if (m_opaque_sp.get())
585     {
586         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
587         if (module_name && module_name[0])
588         {
589             FileSpecList module_spec_list;
590             module_spec_list.Append (FileSpec (module_name, false));
591             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
592         }
593         else
594         {
595             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeFull | eFunctionNameTypeBase, false);
596         }
597     }
598 
599     if (log)
600     {
601         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
602                      m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
603     }
604 
605     return sb_bp;
606 }
607 
608 lldb::SBBreakpoint
609 SBTarget::BreakpointCreateByName (const char *symbol_name,
610                             const SBFileSpecList &module_list,
611                             const SBFileSpecList &comp_unit_list)
612 {
613     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
614 
615     SBBreakpoint sb_bp;
616     if (m_opaque_sp.get() && symbol_name && symbol_name[0])
617     {
618         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
619         *sb_bp = m_opaque_sp->CreateBreakpoint (module_list.get(),
620                                                 comp_unit_list.get(),
621                                                 symbol_name,
622                                                 eFunctionNameTypeFull | eFunctionNameTypeBase,
623                                                 false);
624     }
625 
626     if (log)
627     {
628         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\") => SBBreakpoint(%p)",
629                      m_opaque_sp.get(), symbol_name, sb_bp.get());
630     }
631 
632     return sb_bp;
633 }
634 
635 
636 SBBreakpoint
637 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
638 {
639     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
640 
641     SBBreakpoint sb_bp;
642     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
643     {
644         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
645         RegularExpression regexp(symbol_name_regex);
646 
647         if (module_name && module_name[0])
648         {
649             FileSpecList module_spec_list;
650             module_spec_list.Append (FileSpec (module_name, false));
651 
652             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, false);
653         }
654         else
655         {
656             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, false);
657         }
658     }
659 
660     if (log)
661     {
662         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
663                      m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
664     }
665 
666     return sb_bp;
667 }
668 
669 lldb::SBBreakpoint
670 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
671                          const SBFileSpecList &module_list,
672                          const SBFileSpecList &comp_unit_list)
673 {
674     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
675 
676     SBBreakpoint sb_bp;
677     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
678     {
679         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
680         RegularExpression regexp(symbol_name_regex);
681 
682         *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, false);
683     }
684 
685     if (log)
686     {
687         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
688                      m_opaque_sp.get(), symbol_name_regex, sb_bp.get());
689     }
690 
691     return sb_bp;
692 }
693 
694 SBBreakpoint
695 SBTarget::BreakpointCreateByAddress (addr_t address)
696 {
697     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
698 
699     SBBreakpoint sb_bp;
700     if (m_opaque_sp.get())
701     {
702         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
703         *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
704     }
705 
706     if (log)
707     {
708         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%llu) => SBBreakpoint(%p)", m_opaque_sp.get(), (uint64_t) address, sb_bp.get());
709     }
710 
711     return sb_bp;
712 }
713 
714 lldb::SBBreakpoint
715 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
716 {
717     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
718 
719     SBBreakpoint sb_bp;
720     if (m_opaque_sp.get() && source_regex && source_regex[0])
721     {
722         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
723         RegularExpression regexp(source_regex);
724         FileSpecList source_file_spec_list;
725         source_file_spec_list.Append (source_file.ref());
726 
727         if (module_name && module_name[0])
728         {
729             FileSpecList module_spec_list;
730             module_spec_list.Append (FileSpec (module_name, false));
731 
732             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
733         }
734         else
735         {
736             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
737         }
738     }
739 
740     if (log)
741     {
742         char path[PATH_MAX];
743         source_file->GetPath (path, sizeof(path));
744         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
745                      m_opaque_sp.get(), source_regex, path, module_name, sb_bp.get());
746     }
747 
748     return sb_bp;
749 }
750 
751 lldb::SBBreakpoint
752 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
753                                const SBFileSpecList &module_list,
754                                const lldb::SBFileSpecList &source_file_list)
755 {
756     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
757 
758     SBBreakpoint sb_bp;
759     if (m_opaque_sp.get() && source_regex && source_regex[0])
760     {
761         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
762         RegularExpression regexp(source_regex);
763         *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
764     }
765 
766     if (log)
767     {
768         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
769                      m_opaque_sp.get(), source_regex, sb_bp.get());
770     }
771 
772     return sb_bp;
773 }
774 
775 uint32_t
776 SBTarget::GetNumBreakpoints () const
777 {
778     if (m_opaque_sp)
779     {
780         // The breakpoint list is thread safe, no need to lock
781         return m_opaque_sp->GetBreakpointList().GetSize();
782     }
783     return 0;
784 }
785 
786 SBBreakpoint
787 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
788 {
789     SBBreakpoint sb_breakpoint;
790     if (m_opaque_sp)
791     {
792         // The breakpoint list is thread safe, no need to lock
793         *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
794     }
795     return sb_breakpoint;
796 }
797 
798 bool
799 SBTarget::BreakpointDelete (break_id_t bp_id)
800 {
801     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
802 
803     bool result = false;
804     if (m_opaque_sp)
805     {
806         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
807         result = m_opaque_sp->RemoveBreakpointByID (bp_id);
808     }
809 
810     if (log)
811     {
812         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
813     }
814 
815     return result;
816 }
817 
818 SBBreakpoint
819 SBTarget::FindBreakpointByID (break_id_t bp_id)
820 {
821     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
822 
823     SBBreakpoint sb_breakpoint;
824     if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
825     {
826         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
827         *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
828     }
829 
830     if (log)
831     {
832         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
833                      m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
834     }
835 
836     return sb_breakpoint;
837 }
838 
839 bool
840 SBTarget::EnableAllBreakpoints ()
841 {
842     if (m_opaque_sp)
843     {
844         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
845         m_opaque_sp->EnableAllBreakpoints ();
846         return true;
847     }
848     return false;
849 }
850 
851 bool
852 SBTarget::DisableAllBreakpoints ()
853 {
854     if (m_opaque_sp)
855     {
856         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
857         m_opaque_sp->DisableAllBreakpoints ();
858         return true;
859     }
860     return false;
861 }
862 
863 bool
864 SBTarget::DeleteAllBreakpoints ()
865 {
866     if (m_opaque_sp)
867     {
868         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
869         m_opaque_sp->RemoveAllBreakpoints ();
870         return true;
871     }
872     return false;
873 }
874 
875 uint32_t
876 SBTarget::GetNumWatchpointLocations () const
877 {
878     if (m_opaque_sp)
879     {
880         // The watchpoint location list is thread safe, no need to lock
881         return m_opaque_sp->GetWatchpointLocationList().GetSize();
882     }
883     return 0;
884 }
885 
886 SBWatchpointLocation
887 SBTarget::GetLastCreatedWatchpointLocation ()
888 {
889     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
890 
891     SBWatchpointLocation sb_watchpoint_location;
892     if (m_opaque_sp)
893     {
894         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
895         sb_watchpoint_location = m_opaque_sp->GetLastCreatedWatchpointLocation();
896     }
897 
898     if (log)
899     {
900         log->Printf ("SBTarget(%p)::GetLastCreateWatchpointLocation () => SBWatchpointLocation(%p)",
901                      m_opaque_sp.get(), sb_watchpoint_location.get());
902     }
903 
904     return sb_watchpoint_location;
905 }
906 
907 SBWatchpointLocation
908 SBTarget::GetWatchpointLocationAtIndex (uint32_t idx) const
909 {
910     SBWatchpointLocation sb_watchpoint_location;
911     if (m_opaque_sp)
912     {
913         // The watchpoint location list is thread safe, no need to lock
914         *sb_watchpoint_location = m_opaque_sp->GetWatchpointLocationList().GetByIndex(idx);
915     }
916     return sb_watchpoint_location;
917 }
918 
919 bool
920 SBTarget::WatchpointLocationDelete (watch_id_t wp_id)
921 {
922     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
923 
924     bool result = false;
925     if (m_opaque_sp)
926     {
927         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
928         result = m_opaque_sp->RemoveWatchpointLocationByID (wp_id);
929     }
930 
931     if (log)
932     {
933         log->Printf ("SBTarget(%p)::WatchpointLocationDelete (wp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) wp_id, result);
934     }
935 
936     return result;
937 }
938 
939 SBWatchpointLocation
940 SBTarget::FindWatchpointLocationByID (watch_id_t wp_id)
941 {
942     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
943 
944     SBWatchpointLocation sb_watchpoint_location;
945     if (m_opaque_sp && wp_id != LLDB_INVALID_WATCH_ID)
946     {
947         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
948         *sb_watchpoint_location = m_opaque_sp->GetWatchpointLocationList().FindByID(wp_id);
949     }
950 
951     if (log)
952     {
953         log->Printf ("SBTarget(%p)::FindWatchpointLocationByID (bp_id=%d) => SBWatchpointLocation(%p)",
954                      m_opaque_sp.get(), (uint32_t) wp_id, sb_watchpoint_location.get());
955     }
956 
957     return sb_watchpoint_location;
958 }
959 
960 bool
961 SBTarget::EnableAllWatchpointLocations ()
962 {
963     if (m_opaque_sp)
964     {
965         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
966         m_opaque_sp->EnableAllWatchpointLocations ();
967         return true;
968     }
969     return false;
970 }
971 
972 bool
973 SBTarget::DisableAllWatchpointLocations ()
974 {
975     if (m_opaque_sp)
976     {
977         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
978         m_opaque_sp->DisableAllWatchpointLocations ();
979         return true;
980     }
981     return false;
982 }
983 
984 bool
985 SBTarget::DeleteAllWatchpointLocations ()
986 {
987     if (m_opaque_sp)
988     {
989         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
990         m_opaque_sp->RemoveAllWatchpointLocations ();
991         return true;
992     }
993     return false;
994 }
995 
996 
997 lldb::SBModule
998 SBTarget::AddModule (const char *path,
999                      const char *triple,
1000                      const char *uuid_cstr)
1001 {
1002     lldb::SBModule sb_module;
1003     if (m_opaque_sp)
1004     {
1005         FileSpec module_file_spec;
1006         UUID module_uuid;
1007         ArchSpec module_arch;
1008 
1009         if (path)
1010             module_file_spec.SetFile(path, false);
1011 
1012         if (uuid_cstr)
1013             module_uuid.SetfromCString(uuid_cstr);
1014 
1015         if (triple)
1016             module_arch.SetTriple (triple, m_opaque_sp->GetPlatform ().get());
1017 
1018         sb_module.SetModule(m_opaque_sp->GetSharedModule (module_file_spec,
1019                                                           module_arch,
1020                                                           uuid_cstr ? &module_uuid : NULL));
1021     }
1022     return sb_module;
1023 }
1024 
1025 bool
1026 SBTarget::AddModule (lldb::SBModule &module)
1027 {
1028     if (m_opaque_sp)
1029     {
1030         m_opaque_sp->GetImages().AppendIfNeeded (module.get_sp());
1031         return true;
1032     }
1033     return false;
1034 }
1035 
1036 lldb::SBModule
1037 AddModule (const char *path,
1038            const char *triple,
1039            const char *uuid);
1040 
1041 
1042 
1043 uint32_t
1044 SBTarget::GetNumModules () const
1045 {
1046     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1047 
1048     uint32_t num = 0;
1049     if (m_opaque_sp)
1050     {
1051         // The module list is thread safe, no need to lock
1052         num = m_opaque_sp->GetImages().GetSize();
1053     }
1054 
1055     if (log)
1056         log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
1057 
1058     return num;
1059 }
1060 
1061 void
1062 SBTarget::Clear ()
1063 {
1064     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1065 
1066     if (log)
1067         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1068 
1069     m_opaque_sp.reset();
1070 }
1071 
1072 
1073 SBModule
1074 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1075 {
1076     SBModule sb_module;
1077     if (m_opaque_sp && sb_file_spec.IsValid())
1078     {
1079         // The module list is thread safe, no need to lock
1080         sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
1081     }
1082     return sb_module;
1083 }
1084 
1085 SBModule
1086 SBTarget::GetModuleAtIndex (uint32_t idx)
1087 {
1088     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1089 
1090     SBModule sb_module;
1091     if (m_opaque_sp)
1092     {
1093         // The module list is thread safe, no need to lock
1094         sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
1095     }
1096 
1097     if (log)
1098     {
1099         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1100                      m_opaque_sp.get(), idx, sb_module.get());
1101     }
1102 
1103     return sb_module;
1104 }
1105 
1106 bool
1107 SBTarget::RemoveModule (lldb::SBModule module)
1108 {
1109     if (m_opaque_sp)
1110         return m_opaque_sp->GetImages().Remove(module.get_sp());
1111     return false;
1112 }
1113 
1114 
1115 SBBroadcaster
1116 SBTarget::GetBroadcaster () const
1117 {
1118     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1119 
1120     SBBroadcaster broadcaster(m_opaque_sp.get(), false);
1121 
1122     if (log)
1123         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1124                      m_opaque_sp.get(), broadcaster.get());
1125 
1126     return broadcaster;
1127 }
1128 
1129 
1130 bool
1131 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1132 {
1133     if (m_opaque_sp)
1134     {
1135         description.ref();
1136         m_opaque_sp->Dump (description.get(), description_level);
1137     }
1138     else
1139         description.Printf ("No value");
1140 
1141     return true;
1142 }
1143 
1144 bool
1145 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level) const
1146 {
1147     if (m_opaque_sp)
1148     {
1149         description.ref();
1150         m_opaque_sp->Dump (description.get(), description_level);
1151     }
1152     else
1153         description.Printf ("No value");
1154 
1155     return true;
1156 }
1157 
1158 
1159 uint32_t
1160 SBTarget::FindFunctions (const char *name,
1161                          uint32_t name_type_mask,
1162                          bool append,
1163                          lldb::SBSymbolContextList& sc_list)
1164 {
1165     if (!append)
1166         sc_list.Clear();
1167     if (m_opaque_sp)
1168     {
1169         const bool symbols_ok = true;
1170         return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
1171                                                        name_type_mask,
1172                                                        symbols_ok,
1173                                                        append,
1174                                                        *sc_list);
1175     }
1176     return 0;
1177 }
1178 
1179 lldb::SBType
1180 SBTarget::FindFirstType (const char* type)
1181 {
1182     if (m_opaque_sp)
1183     {
1184         size_t count = m_opaque_sp->GetImages().GetSize();
1185         for (size_t idx = 0; idx < count; idx++)
1186         {
1187             SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1188 
1189             if (found_at_idx.IsValid())
1190                 return found_at_idx;
1191         }
1192     }
1193     return SBType();
1194 }
1195 
1196 lldb::SBTypeList
1197 SBTarget::FindTypes (const char* type)
1198 {
1199 
1200     SBTypeList retval;
1201 
1202     if (m_opaque_sp)
1203     {
1204         ModuleList& images = m_opaque_sp->GetImages();
1205         ConstString name_const(type);
1206         SymbolContext sc;
1207         TypeList type_list;
1208 
1209         uint32_t num_matches = images.FindTypes(sc,
1210                                                 name_const,
1211                                                 true,
1212                                                 UINT32_MAX,
1213                                                 type_list);
1214 
1215         for (size_t idx = 0; idx < num_matches; idx++)
1216         {
1217             TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1218             if (type_sp)
1219                 retval.Append(SBType(type_sp));
1220         }
1221     }
1222     return retval;
1223 }
1224 
1225 SBValueList
1226 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1227 {
1228     SBValueList sb_value_list;
1229 
1230     if (m_opaque_sp)
1231     {
1232         VariableList variable_list;
1233         const bool append = true;
1234         const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
1235                                                                                    append,
1236                                                                                    max_matches,
1237                                                                                    variable_list);
1238 
1239         if (match_count > 0)
1240         {
1241             ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
1242             if (exe_scope == NULL)
1243                 exe_scope = m_opaque_sp.get();
1244             ValueObjectList &value_object_list = sb_value_list.ref();
1245             for (uint32_t i=0; i<match_count; ++i)
1246             {
1247                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1248                 if (valobj_sp)
1249                     value_object_list.Append(valobj_sp);
1250             }
1251         }
1252     }
1253 
1254     return sb_value_list;
1255 }
1256 
1257 SBSourceManager
1258 SBTarget::GetSourceManager()
1259 {
1260     SBSourceManager source_manager (*this);
1261     return source_manager;
1262 }
1263 
1264 
1265 SBError
1266 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
1267                                  lldb::addr_t section_base_addr)
1268 {
1269     SBError sb_error;
1270 
1271     if (IsValid())
1272     {
1273         if (!section.IsValid())
1274         {
1275             sb_error.SetErrorStringWithFormat ("invalid section");
1276         }
1277         else
1278         {
1279             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section.GetSection(), section_base_addr);
1280         }
1281     }
1282     else
1283     {
1284         sb_error.SetErrorStringWithFormat ("invalid target");
1285     }
1286     return sb_error;
1287 }
1288 
1289 SBError
1290 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
1291 {
1292     SBError sb_error;
1293 
1294     if (IsValid())
1295     {
1296         if (!section.IsValid())
1297         {
1298             sb_error.SetErrorStringWithFormat ("invalid section");
1299         }
1300         else
1301         {
1302             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSection());
1303         }
1304     }
1305     else
1306     {
1307         sb_error.SetErrorStringWithFormat ("invalid target");
1308     }
1309     return sb_error;
1310 }
1311 
1312 SBError
1313 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
1314 {
1315     SBError sb_error;
1316 
1317     char path[PATH_MAX];
1318     if (IsValid())
1319     {
1320         if (!module.IsValid())
1321         {
1322             sb_error.SetErrorStringWithFormat ("invalid module");
1323         }
1324         else
1325         {
1326             ObjectFile *objfile = module->GetObjectFile();
1327             if (objfile)
1328             {
1329                 SectionList *section_list = objfile->GetSectionList();
1330                 if (section_list)
1331                 {
1332                     const size_t num_sections = section_list->GetSize();
1333                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1334                     {
1335                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1336                         if (section_sp)
1337                             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide_offset);
1338                     }
1339                 }
1340                 else
1341                 {
1342                     module->GetFileSpec().GetPath (path, sizeof(path));
1343                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1344                 }
1345             }
1346             else
1347             {
1348                 module->GetFileSpec().GetPath (path, sizeof(path));
1349                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1350             }
1351         }
1352     }
1353     else
1354     {
1355         sb_error.SetErrorStringWithFormat ("invalid target");
1356     }
1357     return sb_error;
1358 }
1359 
1360 SBError
1361 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
1362 {
1363     SBError sb_error;
1364 
1365     char path[PATH_MAX];
1366     if (IsValid())
1367     {
1368         if (!module.IsValid())
1369         {
1370             sb_error.SetErrorStringWithFormat ("invalid module");
1371         }
1372         else
1373         {
1374             ObjectFile *objfile = module->GetObjectFile();
1375             if (objfile)
1376             {
1377                 SectionList *section_list = objfile->GetSectionList();
1378                 if (section_list)
1379                 {
1380                     const size_t num_sections = section_list->GetSize();
1381                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1382                     {
1383                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1384                         if (section_sp)
1385                             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section_sp.get());
1386                     }
1387                 }
1388                 else
1389                 {
1390                     module->GetFileSpec().GetPath (path, sizeof(path));
1391                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1392                 }
1393             }
1394             else
1395             {
1396                 module->GetFileSpec().GetPath (path, sizeof(path));
1397                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1398             }
1399         }
1400     }
1401     else
1402     {
1403         sb_error.SetErrorStringWithFormat ("invalid target");
1404     }
1405     return sb_error;
1406 }
1407 
1408 
1409