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             ProcessLaunchInfo launch_info (stdin_path, stdout_path, stderr_path, working_directory, launch_flags);
235 
236             Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
237             if (exe_module)
238                 launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
239             if (argv)
240                 launch_info.GetArguments().AppendArguments (argv);
241             if (envp)
242                 launch_info.GetEnvironmentEntries ().SetArguments (envp);
243 
244             error.SetError (sb_process->Launch (launch_info));
245             if (error.Success())
246             {
247                 // We we are stopping at the entry point, we can return now!
248                 if (stop_at_entry)
249                     return sb_process;
250 
251                 // Make sure we are stopped at the entry
252                 StateType state = sb_process->WaitForProcessToStop (NULL);
253                 if (state == eStateStopped)
254                 {
255                     // resume the process to skip the entry point
256                     error.SetError (sb_process->Resume());
257                     if (error.Success())
258                     {
259                         // If we are doing synchronous mode, then wait for the
260                         // process to stop yet again!
261                         if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
262                             sb_process->WaitForProcessToStop (NULL);
263                     }
264                 }
265             }
266         }
267         else
268         {
269             error.SetErrorString ("unable to create lldb_private::Process");
270         }
271     }
272     else
273     {
274         error.SetErrorString ("SBTarget is invalid");
275     }
276 
277     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
278     if (log)
279     {
280         log->Printf ("SBTarget(%p)::Launch (...) => SBProceess(%p)",
281                      m_opaque_sp.get(), sb_process.get());
282     }
283 
284     return sb_process;
285 }
286 
287 #if defined(__APPLE__)
288 
289 lldb::SBProcess
290 SBTarget::AttachToProcessWithID (SBListener &listener,
291                                 ::pid_t pid,
292                                  lldb::SBError& error)
293 {
294     return AttachToProcessWithID (listener, (lldb::pid_t)pid, error);
295 }
296 
297 #endif // #if defined(__APPLE__)
298 
299 lldb::SBProcess
300 SBTarget::AttachToProcessWithID
301 (
302     SBListener &listener,
303     lldb::pid_t pid,// The process ID to attach to
304     SBError& error  // An error explaining what went wrong if attach fails
305 )
306 {
307     SBProcess sb_process;
308     if (m_opaque_sp)
309     {
310         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
311 
312         StateType state = eStateInvalid;
313         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
314         if (sb_process.IsValid())
315         {
316             state = sb_process->GetState();
317 
318             if (sb_process->IsAlive() && state != eStateConnected)
319             {
320                 if (state == eStateAttaching)
321                     error.SetErrorString ("process attach is in progress");
322                 else
323                     error.SetErrorString ("a process is already being debugged");
324                 sb_process.Clear();
325                 return sb_process;
326             }
327         }
328 
329         if (state == eStateConnected)
330         {
331             // If we are already connected, then we have already specified the
332             // listener, so if a valid listener is supplied, we need to error out
333             // to let the client know.
334             if (listener.IsValid())
335             {
336                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
337                 sb_process.Clear();
338                 return sb_process;
339             }
340         }
341         else
342         {
343             if (listener.IsValid())
344                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
345             else
346                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
347         }
348 
349         if (sb_process.IsValid())
350         {
351             ProcessAttachInfo attach_info;
352             attach_info.SetProcessID (pid);
353             error.SetError (sb_process->Attach (attach_info));
354             // If we are doing synchronous mode, then wait for the
355             // process to stop!
356             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
357                 sb_process->WaitForProcessToStop (NULL);
358         }
359         else
360         {
361             error.SetErrorString ("unable to create lldb_private::Process");
362         }
363     }
364     else
365     {
366         error.SetErrorString ("SBTarget is invalid");
367     }
368     return sb_process;
369 
370 }
371 
372 lldb::SBProcess
373 SBTarget::AttachToProcessWithName
374 (
375     SBListener &listener,
376     const char *name,   // basename of process to attach to
377     bool wait_for,      // if true wait for a new instance of "name" to be launched
378     SBError& error      // An error explaining what went wrong if attach fails
379 )
380 {
381     SBProcess sb_process;
382     if (name && m_opaque_sp)
383     {
384         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
385 
386         StateType state = eStateInvalid;
387         sb_process.SetProcess (m_opaque_sp->GetProcessSP());
388         if (sb_process.IsValid())
389         {
390             state = sb_process->GetState();
391 
392             if (sb_process->IsAlive() && state != eStateConnected)
393             {
394                 if (state == eStateAttaching)
395                     error.SetErrorString ("process attach is in progress");
396                 else
397                     error.SetErrorString ("a process is already being debugged");
398                 sb_process.Clear();
399                 return sb_process;
400             }
401         }
402 
403         if (state == eStateConnected)
404         {
405             // If we are already connected, then we have already specified the
406             // listener, so if a valid listener is supplied, we need to error out
407             // to let the client know.
408             if (listener.IsValid())
409             {
410                 error.SetErrorString ("process is connected and already has a listener, pass empty listener");
411                 sb_process.Clear();
412                 return sb_process;
413             }
414         }
415         else
416         {
417             if (listener.IsValid())
418                 sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref()));
419             else
420                 sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener()));
421         }
422 
423         if (sb_process.IsValid())
424         {
425             ProcessAttachInfo attach_info;
426             attach_info.GetExecutableFile().SetFile(name, false);
427             attach_info.SetWaitForLaunch(wait_for);
428             error.SetError (sb_process->Attach (attach_info));
429             // If we are doing synchronous mode, then wait for the
430             // process to stop!
431             if (m_opaque_sp->GetDebugger().GetAsyncExecution () == false)
432                 sb_process->WaitForProcessToStop (NULL);
433         }
434         else
435         {
436             error.SetErrorString ("unable to create lldb_private::Process");
437         }
438     }
439     else
440     {
441         error.SetErrorString ("SBTarget is invalid");
442     }
443     return sb_process;
444 
445 }
446 
447 lldb::SBProcess
448 SBTarget::ConnectRemote
449 (
450     SBListener &listener,
451     const char *url,
452     const char *plugin_name,
453     SBError& error
454 )
455 {
456     SBProcess sb_process;
457     if (m_opaque_sp)
458     {
459         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
460         if (listener.IsValid())
461             sb_process.SetProcess (m_opaque_sp->CreateProcess (listener.ref(), plugin_name));
462         else
463             sb_process.SetProcess (m_opaque_sp->CreateProcess (m_opaque_sp->GetDebugger().GetListener(), plugin_name));
464 
465 
466         if (sb_process.IsValid())
467         {
468             error.SetError (sb_process->ConnectRemote (url));
469         }
470         else
471         {
472             error.SetErrorString ("unable to create lldb_private::Process");
473         }
474     }
475     else
476     {
477         error.SetErrorString ("SBTarget is invalid");
478     }
479     return sb_process;
480 }
481 
482 SBFileSpec
483 SBTarget::GetExecutable ()
484 {
485 
486     SBFileSpec exe_file_spec;
487     if (m_opaque_sp)
488     {
489         Module *exe_module = m_opaque_sp->GetExecutableModulePointer();
490         if (exe_module)
491             exe_file_spec.SetFileSpec (exe_module->GetFileSpec());
492     }
493 
494     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
495     if (log)
496     {
497         log->Printf ("SBTarget(%p)::GetExecutable () => SBFileSpec(%p)",
498                      m_opaque_sp.get(), exe_file_spec.get());
499     }
500 
501     return exe_file_spec;
502 }
503 
504 bool
505 SBTarget::operator == (const SBTarget &rhs) const
506 {
507     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
508 }
509 
510 bool
511 SBTarget::operator != (const SBTarget &rhs) const
512 {
513     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
514 }
515 
516 lldb_private::Target *
517 SBTarget::operator ->() const
518 {
519     return m_opaque_sp.get();
520 }
521 
522 lldb_private::Target *
523 SBTarget::get() const
524 {
525     return m_opaque_sp.get();
526 }
527 
528 const lldb::TargetSP &
529 SBTarget::get_sp () const
530 {
531     return m_opaque_sp;
532 }
533 
534 void
535 SBTarget::reset (const lldb::TargetSP& target_sp)
536 {
537     m_opaque_sp = target_sp;
538 }
539 
540 lldb::SBAddress
541 SBTarget::ResolveLoadAddress (lldb::addr_t vm_addr)
542 {
543     lldb::SBAddress sb_addr;
544     Address &addr = sb_addr.ref();
545     if (m_opaque_sp)
546     {
547         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
548         if (m_opaque_sp->GetSectionLoadList().ResolveLoadAddress (vm_addr, addr))
549             return sb_addr;
550     }
551 
552     // We have a load address that isn't in a section, just return an address
553     // with the offset filled in (the address) and the section set to NULL
554     addr.SetSection(NULL);
555     addr.SetOffset(vm_addr);
556     return sb_addr;
557 }
558 
559 SBSymbolContext
560 SBTarget::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
561 {
562     SBSymbolContext sc;
563     if (m_opaque_sp && addr.IsValid())
564         m_opaque_sp->GetImages().ResolveSymbolContextForAddress (addr.ref(), resolve_scope, sc.ref());
565     return sc;
566 }
567 
568 
569 SBBreakpoint
570 SBTarget::BreakpointCreateByLocation (const char *file, uint32_t line)
571 {
572     return SBBreakpoint(BreakpointCreateByLocation (SBFileSpec (file, false), line));
573 }
574 
575 SBBreakpoint
576 SBTarget::BreakpointCreateByLocation (const SBFileSpec &sb_file_spec, uint32_t line)
577 {
578     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
579 
580     SBBreakpoint sb_bp;
581     if (m_opaque_sp.get() && line != 0)
582     {
583         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
584         *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, *sb_file_spec, line, true, false);
585     }
586 
587     if (log)
588     {
589         SBStream sstr;
590         sb_bp.GetDescription (sstr);
591         char path[PATH_MAX];
592         sb_file_spec->GetPath (path, sizeof(path));
593         log->Printf ("SBTarget(%p)::BreakpointCreateByLocation ( %s:%u ) => SBBreakpoint(%p): %s",
594                      m_opaque_sp.get(),
595                      path,
596                      line,
597                      sb_bp.get(),
598                      sstr.GetData());
599     }
600 
601     return sb_bp;
602 }
603 
604 SBBreakpoint
605 SBTarget::BreakpointCreateByName (const char *symbol_name, const char *module_name)
606 {
607     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
608 
609     SBBreakpoint sb_bp;
610     if (m_opaque_sp.get())
611     {
612         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
613         if (module_name && module_name[0])
614         {
615             FileSpecList module_spec_list;
616             module_spec_list.Append (FileSpec (module_name, false));
617             *sb_bp = m_opaque_sp->CreateBreakpoint (&module_spec_list, NULL, symbol_name, eFunctionNameTypeAuto, false);
618         }
619         else
620         {
621             *sb_bp = m_opaque_sp->CreateBreakpoint (NULL, NULL, symbol_name, eFunctionNameTypeAuto, false);
622         }
623     }
624 
625     if (log)
626     {
627         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", module=\"%s\") => SBBreakpoint(%p)",
628                      m_opaque_sp.get(), symbol_name, module_name, sb_bp.get());
629     }
630 
631     return sb_bp;
632 }
633 
634 lldb::SBBreakpoint
635 SBTarget::BreakpointCreateByName (const char *symbol_name,
636                             const SBFileSpecList &module_list,
637                             const SBFileSpecList &comp_unit_list)
638 {
639     uint32_t name_type_mask = eFunctionNameTypeAuto;
640     return BreakpointCreateByName (symbol_name, name_type_mask, module_list, comp_unit_list);
641 }
642 
643 lldb::SBBreakpoint
644 SBTarget::BreakpointCreateByName (const char *symbol_name,
645                             uint32_t name_type_mask,
646                             const SBFileSpecList &module_list,
647                             const SBFileSpecList &comp_unit_list)
648 {
649     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
650 
651     SBBreakpoint sb_bp;
652     if (m_opaque_sp.get() && symbol_name && symbol_name[0])
653     {
654         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
655         *sb_bp = m_opaque_sp->CreateBreakpoint (module_list.get(),
656                                                 comp_unit_list.get(),
657                                                 symbol_name,
658                                                 name_type_mask,
659                                                 false);
660     }
661 
662     if (log)
663     {
664         log->Printf ("SBTarget(%p)::BreakpointCreateByName (symbol=\"%s\", name_type: %d) => SBBreakpoint(%p)",
665                      m_opaque_sp.get(), symbol_name, name_type_mask, sb_bp.get());
666     }
667 
668     return sb_bp;
669 }
670 
671 
672 SBBreakpoint
673 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex, const char *module_name)
674 {
675     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
676 
677     SBBreakpoint sb_bp;
678     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
679     {
680         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
681         RegularExpression regexp(symbol_name_regex);
682 
683         if (module_name && module_name[0])
684         {
685             FileSpecList module_spec_list;
686             module_spec_list.Append (FileSpec (module_name, false));
687 
688             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (&module_spec_list, NULL, regexp, false);
689         }
690         else
691         {
692             *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (NULL, NULL, regexp, false);
693         }
694     }
695 
696     if (log)
697     {
698         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
699                      m_opaque_sp.get(), symbol_name_regex, module_name, sb_bp.get());
700     }
701 
702     return sb_bp;
703 }
704 
705 lldb::SBBreakpoint
706 SBTarget::BreakpointCreateByRegex (const char *symbol_name_regex,
707                          const SBFileSpecList &module_list,
708                          const SBFileSpecList &comp_unit_list)
709 {
710     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
711 
712     SBBreakpoint sb_bp;
713     if (m_opaque_sp.get() && symbol_name_regex && symbol_name_regex[0])
714     {
715         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
716         RegularExpression regexp(symbol_name_regex);
717 
718         *sb_bp = m_opaque_sp->CreateFuncRegexBreakpoint (module_list.get(), comp_unit_list.get(), regexp, false);
719     }
720 
721     if (log)
722     {
723         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (symbol_regex=\"%s\") => SBBreakpoint(%p)",
724                      m_opaque_sp.get(), symbol_name_regex, sb_bp.get());
725     }
726 
727     return sb_bp;
728 }
729 
730 SBBreakpoint
731 SBTarget::BreakpointCreateByAddress (addr_t address)
732 {
733     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
734 
735     SBBreakpoint sb_bp;
736     if (m_opaque_sp.get())
737     {
738         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
739         *sb_bp = m_opaque_sp->CreateBreakpoint (address, false);
740     }
741 
742     if (log)
743     {
744         log->Printf ("SBTarget(%p)::BreakpointCreateByAddress (address=%llu) => SBBreakpoint(%p)", m_opaque_sp.get(), (uint64_t) address, sb_bp.get());
745     }
746 
747     return sb_bp;
748 }
749 
750 lldb::SBBreakpoint
751 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex, const lldb::SBFileSpec &source_file, const char *module_name)
752 {
753     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
754 
755     SBBreakpoint sb_bp;
756     if (m_opaque_sp.get() && source_regex && source_regex[0])
757     {
758         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
759         RegularExpression regexp(source_regex);
760         FileSpecList source_file_spec_list;
761         source_file_spec_list.Append (source_file.ref());
762 
763         if (module_name && module_name[0])
764         {
765             FileSpecList module_spec_list;
766             module_spec_list.Append (FileSpec (module_name, false));
767 
768             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (&module_spec_list, &source_file_spec_list, regexp, false);
769         }
770         else
771         {
772             *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (NULL, &source_file_spec_list, regexp, false);
773         }
774     }
775 
776     if (log)
777     {
778         char path[PATH_MAX];
779         source_file->GetPath (path, sizeof(path));
780         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\", file=\"%s\", module_name=\"%s\") => SBBreakpoint(%p)",
781                      m_opaque_sp.get(), source_regex, path, module_name, sb_bp.get());
782     }
783 
784     return sb_bp;
785 }
786 
787 lldb::SBBreakpoint
788 SBTarget::BreakpointCreateBySourceRegex (const char *source_regex,
789                                const SBFileSpecList &module_list,
790                                const lldb::SBFileSpecList &source_file_list)
791 {
792     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
793 
794     SBBreakpoint sb_bp;
795     if (m_opaque_sp.get() && source_regex && source_regex[0])
796     {
797         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
798         RegularExpression regexp(source_regex);
799         *sb_bp = m_opaque_sp->CreateSourceRegexBreakpoint (module_list.get(), source_file_list.get(), regexp, false);
800     }
801 
802     if (log)
803     {
804         log->Printf ("SBTarget(%p)::BreakpointCreateByRegex (source_regex=\"%s\") => SBBreakpoint(%p)",
805                      m_opaque_sp.get(), source_regex, sb_bp.get());
806     }
807 
808     return sb_bp;
809 }
810 
811 uint32_t
812 SBTarget::GetNumBreakpoints () const
813 {
814     if (m_opaque_sp)
815     {
816         // The breakpoint list is thread safe, no need to lock
817         return m_opaque_sp->GetBreakpointList().GetSize();
818     }
819     return 0;
820 }
821 
822 SBBreakpoint
823 SBTarget::GetBreakpointAtIndex (uint32_t idx) const
824 {
825     SBBreakpoint sb_breakpoint;
826     if (m_opaque_sp)
827     {
828         // The breakpoint list is thread safe, no need to lock
829         *sb_breakpoint = m_opaque_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
830     }
831     return sb_breakpoint;
832 }
833 
834 bool
835 SBTarget::BreakpointDelete (break_id_t bp_id)
836 {
837     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
838 
839     bool result = false;
840     if (m_opaque_sp)
841     {
842         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
843         result = m_opaque_sp->RemoveBreakpointByID (bp_id);
844     }
845 
846     if (log)
847     {
848         log->Printf ("SBTarget(%p)::BreakpointDelete (bp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) bp_id, result);
849     }
850 
851     return result;
852 }
853 
854 SBBreakpoint
855 SBTarget::FindBreakpointByID (break_id_t bp_id)
856 {
857     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
858 
859     SBBreakpoint sb_breakpoint;
860     if (m_opaque_sp && bp_id != LLDB_INVALID_BREAK_ID)
861     {
862         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
863         *sb_breakpoint = m_opaque_sp->GetBreakpointByID (bp_id);
864     }
865 
866     if (log)
867     {
868         log->Printf ("SBTarget(%p)::FindBreakpointByID (bp_id=%d) => SBBreakpoint(%p)",
869                      m_opaque_sp.get(), (uint32_t) bp_id, sb_breakpoint.get());
870     }
871 
872     return sb_breakpoint;
873 }
874 
875 bool
876 SBTarget::EnableAllBreakpoints ()
877 {
878     if (m_opaque_sp)
879     {
880         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
881         m_opaque_sp->EnableAllBreakpoints ();
882         return true;
883     }
884     return false;
885 }
886 
887 bool
888 SBTarget::DisableAllBreakpoints ()
889 {
890     if (m_opaque_sp)
891     {
892         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
893         m_opaque_sp->DisableAllBreakpoints ();
894         return true;
895     }
896     return false;
897 }
898 
899 bool
900 SBTarget::DeleteAllBreakpoints ()
901 {
902     if (m_opaque_sp)
903     {
904         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
905         m_opaque_sp->RemoveAllBreakpoints ();
906         return true;
907     }
908     return false;
909 }
910 
911 uint32_t
912 SBTarget::GetNumWatchpoints () const
913 {
914     if (m_opaque_sp)
915     {
916         // The watchpoint list is thread safe, no need to lock
917         return m_opaque_sp->GetWatchpointList().GetSize();
918     }
919     return 0;
920 }
921 
922 SBWatchpoint
923 SBTarget::GetWatchpointAtIndex (uint32_t idx) const
924 {
925     SBWatchpoint sb_watchpoint;
926     if (m_opaque_sp)
927     {
928         // The watchpoint list is thread safe, no need to lock
929         *sb_watchpoint = m_opaque_sp->GetWatchpointList().GetByIndex(idx);
930     }
931     return sb_watchpoint;
932 }
933 
934 bool
935 SBTarget::DeleteWatchpoint (watch_id_t wp_id)
936 {
937     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
938 
939     bool result = false;
940     if (m_opaque_sp)
941     {
942         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
943         result = m_opaque_sp->RemoveWatchpointByID (wp_id);
944     }
945 
946     if (log)
947     {
948         log->Printf ("SBTarget(%p)::WatchpointDelete (wp_id=%d) => %i", m_opaque_sp.get(), (uint32_t) wp_id, result);
949     }
950 
951     return result;
952 }
953 
954 SBWatchpoint
955 SBTarget::FindWatchpointByID (lldb::watch_id_t wp_id)
956 {
957     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
958 
959     SBWatchpoint sb_watchpoint;
960     if (m_opaque_sp && wp_id != LLDB_INVALID_WATCH_ID)
961     {
962         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
963         *sb_watchpoint = m_opaque_sp->GetWatchpointList().FindByID(wp_id);
964     }
965 
966     if (log)
967     {
968         log->Printf ("SBTarget(%p)::FindWatchpointByID (bp_id=%d) => SBWatchpoint(%p)",
969                      m_opaque_sp.get(), (uint32_t) wp_id, sb_watchpoint.get());
970     }
971 
972     return sb_watchpoint;
973 }
974 
975 lldb::SBWatchpoint
976 SBTarget::WatchAddress (lldb::addr_t addr, size_t size, bool read, bool write)
977 {
978     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
979 
980     SBWatchpoint sb_watchpoint;
981     if (m_opaque_sp)
982     {
983         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
984         uint32_t watch_type = (read ? LLDB_WATCH_TYPE_READ : 0) |
985             (write ? LLDB_WATCH_TYPE_WRITE : 0);
986         sb_watchpoint = m_opaque_sp->CreateWatchpoint(addr, size, watch_type);
987     }
988 
989     if (log)
990     {
991         log->Printf ("SBTarget(%p)::WatchAddress (addr=0x%llx, 0x%u) => SBWatchpoint(%p)",
992                      m_opaque_sp.get(), addr, (uint32_t) size, sb_watchpoint.get());
993     }
994 
995     return sb_watchpoint;
996 }
997 
998 bool
999 SBTarget::EnableAllWatchpoints ()
1000 {
1001     if (m_opaque_sp)
1002     {
1003         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1004         m_opaque_sp->EnableAllWatchpoints ();
1005         return true;
1006     }
1007     return false;
1008 }
1009 
1010 bool
1011 SBTarget::DisableAllWatchpoints ()
1012 {
1013     if (m_opaque_sp)
1014     {
1015         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1016         m_opaque_sp->DisableAllWatchpoints ();
1017         return true;
1018     }
1019     return false;
1020 }
1021 
1022 bool
1023 SBTarget::DeleteAllWatchpoints ()
1024 {
1025     if (m_opaque_sp)
1026     {
1027         Mutex::Locker api_locker (m_opaque_sp->GetAPIMutex());
1028         m_opaque_sp->RemoveAllWatchpoints ();
1029         return true;
1030     }
1031     return false;
1032 }
1033 
1034 
1035 lldb::SBModule
1036 SBTarget::AddModule (const char *path,
1037                      const char *triple,
1038                      const char *uuid_cstr)
1039 {
1040     lldb::SBModule sb_module;
1041     if (m_opaque_sp)
1042     {
1043         FileSpec module_file_spec;
1044         UUID module_uuid;
1045         ArchSpec module_arch;
1046 
1047         if (path)
1048             module_file_spec.SetFile(path, false);
1049 
1050         if (uuid_cstr)
1051             module_uuid.SetfromCString(uuid_cstr);
1052 
1053         if (triple)
1054             module_arch.SetTriple (triple, m_opaque_sp->GetPlatform ().get());
1055 
1056         sb_module.SetModule(m_opaque_sp->GetSharedModule (module_file_spec,
1057                                                           module_arch,
1058                                                           uuid_cstr ? &module_uuid : NULL));
1059     }
1060     return sb_module;
1061 }
1062 
1063 bool
1064 SBTarget::AddModule (lldb::SBModule &module)
1065 {
1066     if (m_opaque_sp)
1067     {
1068         m_opaque_sp->GetImages().AppendIfNeeded (module.get_sp());
1069         return true;
1070     }
1071     return false;
1072 }
1073 
1074 uint32_t
1075 SBTarget::GetNumModules () const
1076 {
1077     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1078 
1079     uint32_t num = 0;
1080     if (m_opaque_sp)
1081     {
1082         // The module list is thread safe, no need to lock
1083         num = m_opaque_sp->GetImages().GetSize();
1084     }
1085 
1086     if (log)
1087         log->Printf ("SBTarget(%p)::GetNumModules () => %d", m_opaque_sp.get(), num);
1088 
1089     return num;
1090 }
1091 
1092 void
1093 SBTarget::Clear ()
1094 {
1095     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1096 
1097     if (log)
1098         log->Printf ("SBTarget(%p)::Clear ()", m_opaque_sp.get());
1099 
1100     m_opaque_sp.reset();
1101 }
1102 
1103 
1104 SBModule
1105 SBTarget::FindModule (const SBFileSpec &sb_file_spec)
1106 {
1107     SBModule sb_module;
1108     if (m_opaque_sp && sb_file_spec.IsValid())
1109     {
1110         // The module list is thread safe, no need to lock
1111         sb_module.SetModule (m_opaque_sp->GetImages().FindFirstModuleForFileSpec (*sb_file_spec, NULL, NULL));
1112     }
1113     return sb_module;
1114 }
1115 
1116 SBModule
1117 SBTarget::GetModuleAtIndex (uint32_t idx)
1118 {
1119     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1120 
1121     SBModule sb_module;
1122     if (m_opaque_sp)
1123     {
1124         // The module list is thread safe, no need to lock
1125         sb_module.SetModule(m_opaque_sp->GetImages().GetModuleAtIndex(idx));
1126     }
1127 
1128     if (log)
1129     {
1130         log->Printf ("SBTarget(%p)::GetModuleAtIndex (idx=%d) => SBModule(%p)",
1131                      m_opaque_sp.get(), idx, sb_module.get());
1132     }
1133 
1134     return sb_module;
1135 }
1136 
1137 bool
1138 SBTarget::RemoveModule (lldb::SBModule module)
1139 {
1140     if (m_opaque_sp)
1141         return m_opaque_sp->GetImages().Remove(module.get_sp());
1142     return false;
1143 }
1144 
1145 
1146 SBBroadcaster
1147 SBTarget::GetBroadcaster () const
1148 {
1149     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1150 
1151     SBBroadcaster broadcaster(m_opaque_sp.get(), false);
1152 
1153     if (log)
1154         log->Printf ("SBTarget(%p)::GetBroadcaster () => SBBroadcaster(%p)",
1155                      m_opaque_sp.get(), broadcaster.get());
1156 
1157     return broadcaster;
1158 }
1159 
1160 
1161 bool
1162 SBTarget::GetDescription (SBStream &description, lldb::DescriptionLevel description_level)
1163 {
1164     Stream &strm = description.ref();
1165 
1166     if (m_opaque_sp)
1167     {
1168         m_opaque_sp->Dump (&strm, description_level);
1169     }
1170     else
1171         strm.PutCString ("No value");
1172 
1173     return true;
1174 }
1175 
1176 uint32_t
1177 SBTarget::FindFunctions (const char *name,
1178                          uint32_t name_type_mask,
1179                          bool append,
1180                          lldb::SBSymbolContextList& sc_list)
1181 {
1182     if (!append)
1183         sc_list.Clear();
1184     if (name && m_opaque_sp)
1185     {
1186         const bool symbols_ok = true;
1187         return m_opaque_sp->GetImages().FindFunctions (ConstString(name),
1188                                                        name_type_mask,
1189                                                        symbols_ok,
1190                                                        append,
1191                                                        *sc_list);
1192     }
1193     return 0;
1194 }
1195 
1196 lldb::SBType
1197 SBTarget::FindFirstType (const char* type)
1198 {
1199     if (type && m_opaque_sp)
1200     {
1201         size_t count = m_opaque_sp->GetImages().GetSize();
1202         for (size_t idx = 0; idx < count; idx++)
1203         {
1204             SBType found_at_idx = GetModuleAtIndex(idx).FindFirstType(type);
1205 
1206             if (found_at_idx.IsValid())
1207                 return found_at_idx;
1208         }
1209     }
1210     return SBType();
1211 }
1212 
1213 lldb::SBTypeList
1214 SBTarget::FindTypes (const char* type)
1215 {
1216 
1217     SBTypeList retval;
1218 
1219     if (type && m_opaque_sp)
1220     {
1221         ModuleList& images = m_opaque_sp->GetImages();
1222         ConstString name_const(type);
1223         SymbolContext sc;
1224         TypeList type_list;
1225 
1226         uint32_t num_matches = images.FindTypes(sc,
1227                                                 name_const,
1228                                                 true,
1229                                                 UINT32_MAX,
1230                                                 type_list);
1231 
1232         for (size_t idx = 0; idx < num_matches; idx++)
1233         {
1234             TypeSP type_sp (type_list.GetTypeAtIndex(idx));
1235             if (type_sp)
1236                 retval.Append(SBType(type_sp));
1237         }
1238     }
1239     return retval;
1240 }
1241 
1242 SBValueList
1243 SBTarget::FindGlobalVariables (const char *name, uint32_t max_matches)
1244 {
1245     SBValueList sb_value_list;
1246 
1247     if (name && m_opaque_sp)
1248     {
1249         VariableList variable_list;
1250         const bool append = true;
1251         const uint32_t match_count = m_opaque_sp->GetImages().FindGlobalVariables (ConstString (name),
1252                                                                                    append,
1253                                                                                    max_matches,
1254                                                                                    variable_list);
1255 
1256         if (match_count > 0)
1257         {
1258             ExecutionContextScope *exe_scope = m_opaque_sp->GetProcessSP().get();
1259             if (exe_scope == NULL)
1260                 exe_scope = m_opaque_sp.get();
1261             ValueObjectList &value_object_list = sb_value_list.ref();
1262             for (uint32_t i=0; i<match_count; ++i)
1263             {
1264                 lldb::ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_scope, variable_list.GetVariableAtIndex(i)));
1265                 if (valobj_sp)
1266                     value_object_list.Append(valobj_sp);
1267             }
1268         }
1269     }
1270 
1271     return sb_value_list;
1272 }
1273 
1274 SBSourceManager
1275 SBTarget::GetSourceManager()
1276 {
1277     SBSourceManager source_manager (*this);
1278     return source_manager;
1279 }
1280 
1281 lldb::SBInstructionList
1282 SBTarget::GetInstructions (lldb::SBAddress base_addr, const void *buf, size_t size)
1283 {
1284     SBInstructionList sb_instructions;
1285 
1286     if (m_opaque_sp)
1287     {
1288         Address addr;
1289 
1290         if (base_addr.get())
1291             addr = *base_addr.get();
1292 
1293         sb_instructions.SetDisassembler (Disassembler::DisassembleBytes (m_opaque_sp->GetArchitecture(),
1294                                                                          NULL,
1295                                                                          addr,
1296                                                                          buf,
1297                                                                          size));
1298     }
1299 
1300     return sb_instructions;
1301 }
1302 
1303 lldb::SBInstructionList
1304 SBTarget::GetInstructions (lldb::addr_t base_addr, const void *buf, size_t size)
1305 {
1306     return GetInstructions (ResolveLoadAddress(base_addr), buf, size);
1307 }
1308 
1309 SBError
1310 SBTarget::SetSectionLoadAddress (lldb::SBSection section,
1311                                  lldb::addr_t section_base_addr)
1312 {
1313     SBError sb_error;
1314 
1315     if (IsValid())
1316     {
1317         if (!section.IsValid())
1318         {
1319             sb_error.SetErrorStringWithFormat ("invalid section");
1320         }
1321         else
1322         {
1323             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section.GetSection(), section_base_addr);
1324         }
1325     }
1326     else
1327     {
1328         sb_error.SetErrorStringWithFormat ("invalid target");
1329     }
1330     return sb_error;
1331 }
1332 
1333 SBError
1334 SBTarget::ClearSectionLoadAddress (lldb::SBSection section)
1335 {
1336     SBError sb_error;
1337 
1338     if (IsValid())
1339     {
1340         if (!section.IsValid())
1341         {
1342             sb_error.SetErrorStringWithFormat ("invalid section");
1343         }
1344         else
1345         {
1346             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section.GetSection());
1347         }
1348     }
1349     else
1350     {
1351         sb_error.SetErrorStringWithFormat ("invalid target");
1352     }
1353     return sb_error;
1354 }
1355 
1356 SBError
1357 SBTarget::SetModuleLoadAddress (lldb::SBModule module, int64_t slide_offset)
1358 {
1359     SBError sb_error;
1360 
1361     char path[PATH_MAX];
1362     if (IsValid())
1363     {
1364         if (!module.IsValid())
1365         {
1366             sb_error.SetErrorStringWithFormat ("invalid module");
1367         }
1368         else
1369         {
1370             ObjectFile *objfile = module->GetObjectFile();
1371             if (objfile)
1372             {
1373                 SectionList *section_list = objfile->GetSectionList();
1374                 if (section_list)
1375                 {
1376                     const size_t num_sections = section_list->GetSize();
1377                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1378                     {
1379                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1380                         if (section_sp)
1381                             m_opaque_sp->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide_offset);
1382                     }
1383                 }
1384                 else
1385                 {
1386                     module->GetFileSpec().GetPath (path, sizeof(path));
1387                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1388                 }
1389             }
1390             else
1391             {
1392                 module->GetFileSpec().GetPath (path, sizeof(path));
1393                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1394             }
1395         }
1396     }
1397     else
1398     {
1399         sb_error.SetErrorStringWithFormat ("invalid target");
1400     }
1401     return sb_error;
1402 }
1403 
1404 SBError
1405 SBTarget::ClearModuleLoadAddress (lldb::SBModule module)
1406 {
1407     SBError sb_error;
1408 
1409     char path[PATH_MAX];
1410     if (IsValid())
1411     {
1412         if (!module.IsValid())
1413         {
1414             sb_error.SetErrorStringWithFormat ("invalid module");
1415         }
1416         else
1417         {
1418             ObjectFile *objfile = module->GetObjectFile();
1419             if (objfile)
1420             {
1421                 SectionList *section_list = objfile->GetSectionList();
1422                 if (section_list)
1423                 {
1424                     const size_t num_sections = section_list->GetSize();
1425                     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
1426                     {
1427                         SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
1428                         if (section_sp)
1429                             m_opaque_sp->GetSectionLoadList().SetSectionUnloaded (section_sp.get());
1430                     }
1431                 }
1432                 else
1433                 {
1434                     module->GetFileSpec().GetPath (path, sizeof(path));
1435                     sb_error.SetErrorStringWithFormat ("no sections in object file '%s'", path);
1436                 }
1437             }
1438             else
1439             {
1440                 module->GetFileSpec().GetPath (path, sizeof(path));
1441                 sb_error.SetErrorStringWithFormat ("no object file for module '%s'", path);
1442             }
1443         }
1444     }
1445     else
1446     {
1447         sb_error.SetErrorStringWithFormat ("invalid target");
1448     }
1449     return sb_error;
1450 }
1451 
1452 
1453