xref: /trueos/contrib/llvm/tools/lldb/include/lldb/Target/Target.h (revision fa88ea9f26d1e5430901a9ca6c47daebcc5af5de)
1 //===-- Target.h ------------------------------------------------*- 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 #ifndef liblldb_Target_h_
11 #define liblldb_Target_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <list>
16 
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/lldb-public.h"
20 #include "lldb/Breakpoint/BreakpointList.h"
21 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
22 #include "lldb/Breakpoint/WatchpointList.h"
23 #include "lldb/Core/ArchSpec.h"
24 #include "lldb/Core/Broadcaster.h"
25 #include "lldb/Core/Disassembler.h"
26 #include "lldb/Core/Event.h"
27 #include "lldb/Core/ModuleList.h"
28 #include "lldb/Core/UserSettingsController.h"
29 #include "lldb/Expression/ClangPersistentVariables.h"
30 #include "lldb/Interpreter/Args.h"
31 #include "lldb/Interpreter/OptionValueBoolean.h"
32 #include "lldb/Interpreter/OptionValueEnumeration.h"
33 #include "lldb/Interpreter/OptionValueFileSpec.h"
34 #include "lldb/Symbol/SymbolContext.h"
35 #include "lldb/Target/ABI.h"
36 #include "lldb/Target/ExecutionContextScope.h"
37 #include "lldb/Target/PathMappingList.h"
38 #include "lldb/Target/SectionLoadHistory.h"
39 
40 namespace lldb_private {
41 
42 extern OptionEnumValueElement g_dynamic_value_types[];
43 
44 typedef enum InlineStrategy
45 {
46     eInlineBreakpointsNever = 0,
47     eInlineBreakpointsHeaders,
48     eInlineBreakpointsAlways
49 } InlineStrategy;
50 
51 typedef enum LoadScriptFromSymFile
52 {
53     eLoadScriptFromSymFileTrue,
54     eLoadScriptFromSymFileFalse,
55     eLoadScriptFromSymFileWarn
56 } LoadScriptFromSymFile;
57 
58 //----------------------------------------------------------------------
59 // TargetProperties
60 //----------------------------------------------------------------------
61 class TargetProperties : public Properties
62 {
63 public:
64     TargetProperties(Target *target);
65 
66     virtual
67     ~TargetProperties();
68 
69     ArchSpec
70     GetDefaultArchitecture () const;
71 
72     void
73     SetDefaultArchitecture (const ArchSpec& arch);
74 
75     lldb::DynamicValueType
76     GetPreferDynamicValue() const;
77 
78     bool
79     GetDisableASLR () const;
80 
81     void
82     SetDisableASLR (bool b);
83 
84     bool
85     GetDisableSTDIO () const;
86 
87     void
88     SetDisableSTDIO (bool b);
89 
90     const char *
91     GetDisassemblyFlavor() const;
92 
93 //    void
94 //    SetDisassemblyFlavor(const char *flavor);
95 
96     InlineStrategy
97     GetInlineStrategy () const;
98 
99     const char *
100     GetArg0 () const;
101 
102     void
103     SetArg0 (const char *arg);
104 
105     bool
106     GetRunArguments (Args &args) const;
107 
108     void
109     SetRunArguments (const Args &args);
110 
111     size_t
112     GetEnvironmentAsArgs (Args &env) const;
113 
114     bool
115     GetSkipPrologue() const;
116 
117     PathMappingList &
118     GetSourcePathMap () const;
119 
120     FileSpecList &
121     GetExecutableSearchPaths ();
122 
123     FileSpecList &
124     GetDebugFileSearchPaths ();
125 
126     bool
127     GetEnableSyntheticValue () const;
128 
129     uint32_t
130     GetMaximumNumberOfChildrenToDisplay() const;
131 
132     uint32_t
133     GetMaximumSizeOfStringSummary() const;
134 
135     uint32_t
136     GetMaximumMemReadSize () const;
137 
138     FileSpec
139     GetStandardInputPath () const;
140 
141     void
142     SetStandardInputPath (const char *path);
143 
144     FileSpec
145     GetStandardOutputPath () const;
146 
147     void
148     SetStandardOutputPath (const char *path);
149 
150     FileSpec
151     GetStandardErrorPath () const;
152 
153     void
154     SetStandardErrorPath (const char *path);
155 
156     bool
157     GetBreakpointsConsultPlatformAvoidList ();
158 
159     const char *
160     GetExpressionPrefixContentsAsCString ();
161 
162     bool
163     GetUseHexImmediates() const;
164 
165     bool
166     GetUseFastStepping() const;
167 
168     bool
169     GetDisplayExpressionsInCrashlogs () const;
170 
171     LoadScriptFromSymFile
172     GetLoadScriptFromSymbolFile() const;
173 
174     Disassembler::HexImmediateStyle
175     GetHexImmediateStyle() const;
176 
177     MemoryModuleLoadLevel
178     GetMemoryModuleLoadLevel() const;
179 
180     bool
181     GetUserSpecifiedTrapHandlerNames (Args &args) const;
182 
183     void
184     SetUserSpecifiedTrapHandlerNames (const Args &args);
185 };
186 
187 typedef std::shared_ptr<TargetProperties> TargetPropertiesSP;
188 
189 class EvaluateExpressionOptions
190 {
191 public:
192     static const uint32_t default_timeout = 500000;
EvaluateExpressionOptions()193     EvaluateExpressionOptions() :
194         m_execution_policy(eExecutionPolicyOnlyWhenNeeded),
195         m_language (lldb::eLanguageTypeUnknown),
196         m_coerce_to_id(false),
197         m_unwind_on_error(true),
198         m_ignore_breakpoints (false),
199         m_keep_in_memory(false),
200         m_try_others(true),
201         m_stop_others(true),
202         m_debug(false),
203         m_trap_exceptions(true),
204         m_use_dynamic(lldb::eNoDynamicValues),
205         m_timeout_usec(default_timeout)
206     {}
207 
208     ExecutionPolicy
GetExecutionPolicy()209     GetExecutionPolicy () const
210     {
211         return m_execution_policy;
212     }
213 
214     void
215     SetExecutionPolicy (ExecutionPolicy policy = eExecutionPolicyAlways)
216     {
217         m_execution_policy = policy;
218     }
219 
220     lldb::LanguageType
GetLanguage()221     GetLanguage() const
222     {
223         return m_language;
224     }
225 
226     void
SetLanguage(lldb::LanguageType language)227     SetLanguage(lldb::LanguageType language)
228     {
229         m_language = language;
230     }
231 
232     bool
DoesCoerceToId()233     DoesCoerceToId () const
234     {
235         return m_coerce_to_id;
236     }
237 
238     void
239     SetCoerceToId (bool coerce = true)
240     {
241         m_coerce_to_id = coerce;
242     }
243 
244     bool
DoesUnwindOnError()245     DoesUnwindOnError () const
246     {
247         return m_unwind_on_error;
248     }
249 
250     void
251     SetUnwindOnError (bool unwind = false)
252     {
253         m_unwind_on_error = unwind;
254     }
255 
256     bool
DoesIgnoreBreakpoints()257     DoesIgnoreBreakpoints () const
258     {
259         return m_ignore_breakpoints;
260     }
261 
262     void
263     SetIgnoreBreakpoints (bool ignore = false)
264     {
265         m_ignore_breakpoints = ignore;
266     }
267 
268     bool
DoesKeepInMemory()269     DoesKeepInMemory () const
270     {
271         return m_keep_in_memory;
272     }
273 
274     void
275     SetKeepInMemory (bool keep = true)
276     {
277         m_keep_in_memory = keep;
278     }
279 
280     lldb::DynamicValueType
GetUseDynamic()281     GetUseDynamic () const
282     {
283         return m_use_dynamic;
284     }
285 
286     void
287     SetUseDynamic (lldb::DynamicValueType dynamic = lldb::eDynamicCanRunTarget)
288     {
289         m_use_dynamic = dynamic;
290     }
291 
292     uint32_t
GetTimeoutUsec()293     GetTimeoutUsec () const
294     {
295         return m_timeout_usec;
296     }
297 
298     void
299     SetTimeoutUsec (uint32_t timeout = 0)
300     {
301         m_timeout_usec = timeout;
302     }
303 
304     bool
GetTryAllThreads()305     GetTryAllThreads () const
306     {
307         return m_try_others;
308     }
309 
310     void
311     SetTryAllThreads (bool try_others = true)
312     {
313         m_try_others = try_others;
314     }
315 
316     bool
GetStopOthers()317     GetStopOthers () const
318     {
319         return m_stop_others;
320     }
321 
322     void
323     SetStopOthers (bool stop_others = true)
324     {
325         m_stop_others = stop_others;
326     }
327 
328     bool
GetDebug()329     GetDebug() const
330     {
331         return m_debug;
332     }
333 
334     void
SetDebug(bool b)335     SetDebug(bool b)
336     {
337         m_debug = b;
338     }
339 
340     bool
GetTrapExceptions()341     GetTrapExceptions() const
342     {
343         return m_trap_exceptions;
344     }
345 
346     void
SetTrapExceptions(bool b)347     SetTrapExceptions (bool b)
348     {
349         m_trap_exceptions = b;
350     }
351 
352 private:
353     ExecutionPolicy m_execution_policy;
354     lldb::LanguageType m_language;
355     bool m_coerce_to_id;
356     bool m_unwind_on_error;
357     bool m_ignore_breakpoints;
358     bool m_keep_in_memory;
359     bool m_try_others;
360     bool m_stop_others;
361     bool m_debug;
362     bool m_trap_exceptions;
363     lldb::DynamicValueType m_use_dynamic;
364     uint32_t m_timeout_usec;
365 };
366 
367 //----------------------------------------------------------------------
368 // Target
369 //----------------------------------------------------------------------
370 class Target :
371     public std::enable_shared_from_this<Target>,
372     public TargetProperties,
373     public Broadcaster,
374     public ExecutionContextScope,
375     public ModuleList::Notifier
376 {
377 public:
378     friend class TargetList;
379 
380     //------------------------------------------------------------------
381     /// Broadcaster event bits definitions.
382     //------------------------------------------------------------------
383     enum
384     {
385         eBroadcastBitBreakpointChanged  = (1 << 0),
386         eBroadcastBitModulesLoaded      = (1 << 1),
387         eBroadcastBitModulesUnloaded    = (1 << 2),
388         eBroadcastBitWatchpointChanged  = (1 << 3),
389         eBroadcastBitSymbolsLoaded      = (1 << 4)
390     };
391 
392     // These two functions fill out the Broadcaster interface:
393 
394     static ConstString &GetStaticBroadcasterClass ();
395 
GetBroadcasterClass()396     virtual ConstString &GetBroadcasterClass() const
397     {
398         return GetStaticBroadcasterClass();
399     }
400 
401     // This event data class is for use by the TargetList to broadcast new target notifications.
402     class TargetEventData : public EventData
403     {
404     public:
405 
406         static const ConstString &
407         GetFlavorString ();
408 
409         virtual const ConstString &
410         GetFlavor () const;
411 
412         TargetEventData (const lldb::TargetSP &new_target_sp);
413 
414         lldb::TargetSP &
GetTarget()415         GetTarget()
416         {
417             return m_target_sp;
418         }
419 
420         virtual
421         ~TargetEventData();
422 
423         virtual void
424         Dump (Stream *s) const;
425 
426         static const lldb::TargetSP
427         GetTargetFromEvent (const lldb::EventSP &event_sp);
428 
429         static const TargetEventData *
430         GetEventDataFromEvent (const Event *event_sp);
431 
432     private:
433         lldb::TargetSP m_target_sp;
434 
435         DISALLOW_COPY_AND_ASSIGN (TargetEventData);
436     };
437 
438     static void
439     SettingsInitialize ();
440 
441     static void
442     SettingsTerminate ();
443 
444 //    static lldb::UserSettingsControllerSP &
445 //    GetSettingsController ();
446 
447     static FileSpecList
448     GetDefaultExecutableSearchPaths ();
449 
450     static FileSpecList
451     GetDefaultDebugFileSearchPaths ();
452 
453     static ArchSpec
454     GetDefaultArchitecture ();
455 
456     static void
457     SetDefaultArchitecture (const ArchSpec &arch);
458 
459 //    void
460 //    UpdateInstanceName ();
461 
462     lldb::ModuleSP
463     GetSharedModule (const ModuleSpec &module_spec,
464                      Error *error_ptr = NULL);
465 
466     //----------------------------------------------------------------------
467     // Settings accessors
468     //----------------------------------------------------------------------
469 
470     static const TargetPropertiesSP &
471     GetGlobalProperties();
472 
473 
474 private:
475     //------------------------------------------------------------------
476     /// Construct with optional file and arch.
477     ///
478     /// This member is private. Clients must use
479     /// TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
480     /// so all targets can be tracked from the central target list.
481     ///
482     /// @see TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
483     //------------------------------------------------------------------
484     Target (Debugger &debugger,
485             const ArchSpec &target_arch,
486             const lldb::PlatformSP &platform_sp);
487 
488     // Helper function.
489     bool
490     ProcessIsValid ();
491 
492 public:
493     ~Target();
494 
495     Mutex &
GetAPIMutex()496     GetAPIMutex ()
497     {
498         return m_mutex;
499     }
500 
501     void
502     DeleteCurrentProcess ();
503 
504     void
505     CleanupProcess ();
506     //------------------------------------------------------------------
507     /// Dump a description of this object to a Stream.
508     ///
509     /// Dump a description of the contents of this object to the
510     /// supplied stream \a s. The dumped content will be only what has
511     /// been loaded or parsed up to this point at which this function
512     /// is called, so this is a good way to see what has been parsed
513     /// in a target.
514     ///
515     /// @param[in] s
516     ///     The stream to which to dump the object descripton.
517     //------------------------------------------------------------------
518     void
519     Dump (Stream *s, lldb::DescriptionLevel description_level);
520 
521     const lldb::ProcessSP &
522     CreateProcess (Listener &listener,
523                    const char *plugin_name,
524                    const FileSpec *crash_file);
525 
526     const lldb::ProcessSP &
527     GetProcessSP () const;
528 
529     bool
IsValid()530     IsValid()
531     {
532         return m_valid;
533     }
534 
535     void
536     Destroy();
537 
538     Error
539     Launch (Listener &listener,
540             ProcessLaunchInfo &launch_info);
541 
542     //------------------------------------------------------------------
543     // This part handles the breakpoints.
544     //------------------------------------------------------------------
545 
546     BreakpointList &
547     GetBreakpointList(bool internal = false);
548 
549     const BreakpointList &
550     GetBreakpointList(bool internal = false) const;
551 
552     lldb::BreakpointSP
GetLastCreatedBreakpoint()553     GetLastCreatedBreakpoint ()
554     {
555         return m_last_created_breakpoint;
556     }
557 
558     lldb::BreakpointSP
559     GetBreakpointByID (lldb::break_id_t break_id);
560 
561     // Use this to create a file and line breakpoint to a given module or all module it is NULL
562     lldb::BreakpointSP
563     CreateBreakpoint (const FileSpecList *containingModules,
564                       const FileSpec &file,
565                       uint32_t line_no,
566                       LazyBool check_inlines,
567                       LazyBool skip_prologue,
568                       bool internal,
569                       bool request_hardware);
570 
571     // Use this to create breakpoint that matches regex against the source lines in files given in source_file_list:
572     lldb::BreakpointSP
573     CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
574                                  const FileSpecList *source_file_list,
575                                  RegularExpression &source_regex,
576                                  bool internal,
577                                  bool request_hardware);
578 
579     // Use this to create a breakpoint from a load address
580     lldb::BreakpointSP
581     CreateBreakpoint (lldb::addr_t load_addr,
582                       bool internal,
583                       bool request_hardware);
584 
585     // Use this to create Address breakpoints:
586     lldb::BreakpointSP
587     CreateBreakpoint (Address &addr,
588                       bool internal,
589                       bool request_hardware);
590 
591     // Use this to create a function breakpoint by regexp in containingModule/containingSourceFiles, or all modules if it is NULL
592     // When "skip_prologue is set to eLazyBoolCalculate, we use the current target
593     // setting, else we use the values passed in
594     lldb::BreakpointSP
595     CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
596                                const FileSpecList *containingSourceFiles,
597                                RegularExpression &func_regexp,
598                                LazyBool skip_prologue,
599                                bool internal,
600                                bool request_hardware);
601 
602     // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
603     // When "skip_prologue is set to eLazyBoolCalculate, we use the current target
604     // setting, else we use the values passed in
605     lldb::BreakpointSP
606     CreateBreakpoint (const FileSpecList *containingModules,
607                       const FileSpecList *containingSourceFiles,
608                       const char *func_name,
609                       uint32_t func_name_type_mask,
610                       LazyBool skip_prologue,
611                       bool internal,
612                       bool request_hardware);
613 
614     lldb::BreakpointSP
615     CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal);
616 
617     // This is the same as the func_name breakpoint except that you can specify a vector of names.  This is cheaper
618     // than a regular expression breakpoint in the case where you just want to set a breakpoint on a set of names
619     // you already know.
620     lldb::BreakpointSP
621     CreateBreakpoint (const FileSpecList *containingModules,
622                       const FileSpecList *containingSourceFiles,
623                       const char *func_names[],
624                       size_t num_names,
625                       uint32_t func_name_type_mask,
626                       LazyBool skip_prologue,
627                       bool internal,
628                       bool request_hardware);
629 
630     lldb::BreakpointSP
631     CreateBreakpoint (const FileSpecList *containingModules,
632                       const FileSpecList *containingSourceFiles,
633                       const std::vector<std::string> &func_names,
634                       uint32_t func_name_type_mask,
635                       LazyBool skip_prologue,
636                       bool internal,
637                       bool request_hardware);
638 
639 
640     // Use this to create a general breakpoint:
641     lldb::BreakpointSP
642     CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
643                       lldb::BreakpointResolverSP &resolver_sp,
644                       bool internal,
645                       bool request_hardware,
646                       bool resolve_indirect_symbols);
647 
648     // Use this to create a watchpoint:
649     lldb::WatchpointSP
650     CreateWatchpoint (lldb::addr_t addr,
651                       size_t size,
652                       const ClangASTType *type,
653                       uint32_t kind,
654                       Error &error);
655 
656     lldb::WatchpointSP
GetLastCreatedWatchpoint()657     GetLastCreatedWatchpoint ()
658     {
659         return m_last_created_watchpoint;
660     }
661 
662     WatchpointList &
GetWatchpointList()663     GetWatchpointList()
664     {
665         return m_watchpoint_list;
666     }
667 
668     void
669     RemoveAllBreakpoints (bool internal_also = false);
670 
671     void
672     DisableAllBreakpoints (bool internal_also = false);
673 
674     void
675     EnableAllBreakpoints (bool internal_also = false);
676 
677     bool
678     DisableBreakpointByID (lldb::break_id_t break_id);
679 
680     bool
681     EnableBreakpointByID (lldb::break_id_t break_id);
682 
683     bool
684     RemoveBreakpointByID (lldb::break_id_t break_id);
685 
686     // The flag 'end_to_end', default to true, signifies that the operation is
687     // performed end to end, for both the debugger and the debuggee.
688 
689     bool
690     RemoveAllWatchpoints (bool end_to_end = true);
691 
692     bool
693     DisableAllWatchpoints (bool end_to_end = true);
694 
695     bool
696     EnableAllWatchpoints (bool end_to_end = true);
697 
698     bool
699     ClearAllWatchpointHitCounts ();
700 
701     bool
702     IgnoreAllWatchpoints (uint32_t ignore_count);
703 
704     bool
705     DisableWatchpointByID (lldb::watch_id_t watch_id);
706 
707     bool
708     EnableWatchpointByID (lldb::watch_id_t watch_id);
709 
710     bool
711     RemoveWatchpointByID (lldb::watch_id_t watch_id);
712 
713     bool
714     IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count);
715 
716     //------------------------------------------------------------------
717     /// Get \a load_addr as a callable code load address for this target
718     ///
719     /// Take \a load_addr and potentially add any address bits that are
720     /// needed to make the address callable. For ARM this can set bit
721     /// zero (if it already isn't) if \a load_addr is a thumb function.
722     /// If \a addr_class is set to eAddressClassInvalid, then the address
723     /// adjustment will always happen. If it is set to an address class
724     /// that doesn't have code in it, LLDB_INVALID_ADDRESS will be
725     /// returned.
726     //------------------------------------------------------------------
727     lldb::addr_t
728     GetCallableLoadAddress (lldb::addr_t load_addr, lldb::AddressClass addr_class = lldb::eAddressClassInvalid) const;
729 
730     //------------------------------------------------------------------
731     /// Get \a load_addr as an opcode for this target.
732     ///
733     /// Take \a load_addr and potentially strip any address bits that are
734     /// needed to make the address point to an opcode. For ARM this can
735     /// clear bit zero (if it already isn't) if \a load_addr is a
736     /// thumb function and load_addr is in code.
737     /// If \a addr_class is set to eAddressClassInvalid, then the address
738     /// adjustment will always happen. If it is set to an address class
739     /// that doesn't have code in it, LLDB_INVALID_ADDRESS will be
740     /// returned.
741     //------------------------------------------------------------------
742     lldb::addr_t
743     GetOpcodeLoadAddress (lldb::addr_t load_addr, lldb::AddressClass addr_class = lldb::eAddressClassInvalid) const;
744 
745 protected:
746     //------------------------------------------------------------------
747     /// Implementing of ModuleList::Notifier.
748     //------------------------------------------------------------------
749 
750     virtual void
751     ModuleAdded (const ModuleList& module_list, const lldb::ModuleSP& module_sp);
752 
753     virtual void
754     ModuleRemoved (const ModuleList& module_list, const lldb::ModuleSP& module_sp);
755 
756     virtual void
757     ModuleUpdated (const ModuleList& module_list,
758                    const lldb::ModuleSP& old_module_sp,
759                    const lldb::ModuleSP& new_module_sp);
760     virtual void
761     WillClearList (const ModuleList& module_list);
762 
763 public:
764 
765     void
766     ModulesDidLoad (ModuleList &module_list);
767 
768     void
769     ModulesDidUnload (ModuleList &module_list, bool delete_locations);
770 
771     void
772     SymbolsDidLoad (ModuleList &module_list);
773 
774     void
775     ClearModules(bool delete_locations);
776 
777     //------------------------------------------------------------------
778     /// Called as the last function in Process::DidExec().
779     ///
780     /// Process::DidExec() will clear a lot of state in the process,
781     /// then try to reload a dynamic loader plugin to discover what
782     /// binaries are currently available and then this function should
783     /// be called to allow the target to do any cleanup after everything
784     /// has been figured out. It can remove breakpoints that no longer
785     /// make sense as the exec might have changed the target
786     /// architecture, and unloaded some modules that might get deleted.
787     //------------------------------------------------------------------
788     void
789     DidExec ();
790 
791     //------------------------------------------------------------------
792     /// Gets the module for the main executable.
793     ///
794     /// Each process has a notion of a main executable that is the file
795     /// that will be executed or attached to. Executable files can have
796     /// dependent modules that are discovered from the object files, or
797     /// discovered at runtime as things are dynamically loaded.
798     ///
799     /// @return
800     ///     The shared pointer to the executable module which can
801     ///     contains a NULL Module object if no executable has been
802     ///     set.
803     ///
804     /// @see DynamicLoader
805     /// @see ObjectFile::GetDependentModules (FileSpecList&)
806     /// @see Process::SetExecutableModule(lldb::ModuleSP&)
807     //------------------------------------------------------------------
808     lldb::ModuleSP
809     GetExecutableModule ();
810 
811     Module*
812     GetExecutableModulePointer ();
813 
814     //------------------------------------------------------------------
815     /// Set the main executable module.
816     ///
817     /// Each process has a notion of a main executable that is the file
818     /// that will be executed or attached to. Executable files can have
819     /// dependent modules that are discovered from the object files, or
820     /// discovered at runtime as things are dynamically loaded.
821     ///
822     /// Setting the executable causes any of the current dependant
823     /// image information to be cleared and replaced with the static
824     /// dependent image information found by calling
825     /// ObjectFile::GetDependentModules (FileSpecList&) on the main
826     /// executable and any modules on which it depends. Calling
827     /// Process::GetImages() will return the newly found images that
828     /// were obtained from all of the object files.
829     ///
830     /// @param[in] module_sp
831     ///     A shared pointer reference to the module that will become
832     ///     the main executable for this process.
833     ///
834     /// @param[in] get_dependent_files
835     ///     If \b true then ask the object files to track down any
836     ///     known dependent files.
837     ///
838     /// @see ObjectFile::GetDependentModules (FileSpecList&)
839     /// @see Process::GetImages()
840     //------------------------------------------------------------------
841     void
842     SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
843 
844     bool
845     LoadScriptingResources (std::list<Error>& errors,
846                             Stream* feedback_stream = NULL,
847                             bool continue_on_error = true)
848     {
849         return m_images.LoadScriptingResourcesInTarget(this,errors,feedback_stream,continue_on_error);
850     }
851 
852     //------------------------------------------------------------------
853     /// Get accessor for the images for this process.
854     ///
855     /// Each process has a notion of a main executable that is the file
856     /// that will be executed or attached to. Executable files can have
857     /// dependent modules that are discovered from the object files, or
858     /// discovered at runtime as things are dynamically loaded. After
859     /// a main executable has been set, the images will contain a list
860     /// of all the files that the executable depends upon as far as the
861     /// object files know. These images will usually contain valid file
862     /// virtual addresses only. When the process is launched or attached
863     /// to, the DynamicLoader plug-in will discover where these images
864     /// were loaded in memory and will resolve the load virtual
865     /// addresses is each image, and also in images that are loaded by
866     /// code.
867     ///
868     /// @return
869     ///     A list of Module objects in a module list.
870     //------------------------------------------------------------------
871     const ModuleList&
GetImages()872     GetImages () const
873     {
874         return m_images;
875     }
876 
877     ModuleList&
GetImages()878     GetImages ()
879     {
880         return m_images;
881     }
882 
883     //------------------------------------------------------------------
884     /// Return whether this FileSpec corresponds to a module that should be considered for general searches.
885     ///
886     /// This API will be consulted by the SearchFilterForNonModuleSpecificSearches
887     /// and any module that returns \b true will not be searched.  Note the
888     /// SearchFilterForNonModuleSpecificSearches is the search filter that
889     /// gets used in the CreateBreakpoint calls when no modules is provided.
890     ///
891     /// The target call at present just consults the Platform's call of the
892     /// same name.
893     ///
894     /// @param[in] module_sp
895     ///     A shared pointer reference to the module that checked.
896     ///
897     /// @return \b true if the module should be excluded, \b false otherwise.
898     //------------------------------------------------------------------
899     bool
900     ModuleIsExcludedForNonModuleSpecificSearches (const FileSpec &module_spec);
901 
902     //------------------------------------------------------------------
903     /// Return whether this module should be considered for general searches.
904     ///
905     /// This API will be consulted by the SearchFilterForNonModuleSpecificSearches
906     /// and any module that returns \b true will not be searched.  Note the
907     /// SearchFilterForNonModuleSpecificSearches is the search filter that
908     /// gets used in the CreateBreakpoint calls when no modules is provided.
909     ///
910     /// The target call at present just consults the Platform's call of the
911     /// same name.
912     ///
913     /// FIXME: When we get time we should add a way for the user to set modules that they
914     /// don't want searched, in addition to or instead of the platform ones.
915     ///
916     /// @param[in] module_sp
917     ///     A shared pointer reference to the module that checked.
918     ///
919     /// @return \b true if the module should be excluded, \b false otherwise.
920     //------------------------------------------------------------------
921     bool
922     ModuleIsExcludedForNonModuleSpecificSearches (const lldb::ModuleSP &module_sp);
923 
924     ArchSpec &
GetArchitecture()925     GetArchitecture ()
926     {
927         return m_arch;
928     }
929 
930     const ArchSpec &
GetArchitecture()931     GetArchitecture () const
932     {
933         return m_arch;
934     }
935 
936     //------------------------------------------------------------------
937     /// Set the architecture for this target.
938     ///
939     /// If the current target has no Images read in, then this just sets the architecture, which will
940     /// be used to select the architecture of the ExecutableModule when that is set.
941     /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
942     /// architecture from the currently selected one will reset the ExecutableModule to that slice
943     /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
944     /// contain a fork of this architecture, then this code will return false, and the architecture
945     /// won't be changed.
946     /// If the input arch_spec is the same as the already set architecture, this is a no-op.
947     ///
948     /// @param[in] arch_spec
949     ///     The new architecture.
950     ///
951     /// @return
952     ///     \b true if the architecture was successfully set, \bfalse otherwise.
953     //------------------------------------------------------------------
954     bool
955     SetArchitecture (const ArchSpec &arch_spec);
956 
957     Debugger &
GetDebugger()958     GetDebugger ()
959     {
960         return m_debugger;
961     }
962 
963     size_t
964     ReadMemoryFromFileCache (const Address& addr,
965                              void *dst,
966                              size_t dst_len,
967                              Error &error);
968 
969     // Reading memory through the target allows us to skip going to the process
970     // for reading memory if possible and it allows us to try and read from
971     // any constant sections in our object files on disk. If you always want
972     // live program memory, read straight from the process. If you possibly
973     // want to read from const sections in object files, read from the target.
974     // This version of ReadMemory will try and read memory from the process
975     // if the process is alive. The order is:
976     // 1 - if (prefer_file_cache == true) then read from object file cache
977     // 2 - if there is a valid process, try and read from its memory
978     // 3 - if (prefer_file_cache == false) then read from object file cache
979     size_t
980     ReadMemory (const Address& addr,
981                 bool prefer_file_cache,
982                 void *dst,
983                 size_t dst_len,
984                 Error &error,
985                 lldb::addr_t *load_addr_ptr = NULL);
986 
987     size_t
988     ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error);
989 
990     size_t
991     ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error);
992 
993     size_t
994     ReadScalarIntegerFromMemory (const Address& addr,
995                                  bool prefer_file_cache,
996                                  uint32_t byte_size,
997                                  bool is_signed,
998                                  Scalar &scalar,
999                                  Error &error);
1000 
1001     uint64_t
1002     ReadUnsignedIntegerFromMemory (const Address& addr,
1003                                    bool prefer_file_cache,
1004                                    size_t integer_byte_size,
1005                                    uint64_t fail_value,
1006                                    Error &error);
1007 
1008     bool
1009     ReadPointerFromMemory (const Address& addr,
1010                            bool prefer_file_cache,
1011                            Error &error,
1012                            Address &pointer_addr);
1013 
1014     SectionLoadList&
GetSectionLoadList()1015     GetSectionLoadList()
1016     {
1017         return m_section_load_history.GetCurrentSectionLoadList();
1018     }
1019 
1020 //    const SectionLoadList&
1021 //    GetSectionLoadList() const
1022 //    {
1023 //        return const_cast<SectionLoadHistory *>(&m_section_load_history)->GetCurrentSectionLoadList();
1024 //    }
1025 
1026     static Target *
1027     GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr,
1028                            const SymbolContext *sc_ptr);
1029 
1030     //------------------------------------------------------------------
1031     // lldb::ExecutionContextScope pure virtual functions
1032     //------------------------------------------------------------------
1033     virtual lldb::TargetSP
1034     CalculateTarget ();
1035 
1036     virtual lldb::ProcessSP
1037     CalculateProcess ();
1038 
1039     virtual lldb::ThreadSP
1040     CalculateThread ();
1041 
1042     virtual lldb::StackFrameSP
1043     CalculateStackFrame ();
1044 
1045     virtual void
1046     CalculateExecutionContext (ExecutionContext &exe_ctx);
1047 
1048     PathMappingList &
1049     GetImageSearchPathList ();
1050 
1051     ClangASTContext *
1052     GetScratchClangASTContext(bool create_on_demand=true);
1053 
1054     ClangASTImporter *
1055     GetClangASTImporter();
1056 
1057     //----------------------------------------------------------------------
1058     // Install any files through the platform that need be to installed
1059     // prior to launching or attaching.
1060     //----------------------------------------------------------------------
1061     Error
1062     Install(ProcessLaunchInfo *launch_info);
1063 
1064 
1065     bool
1066     ResolveLoadAddress (lldb::addr_t load_addr,
1067                         Address &so_addr,
1068                         uint32_t stop_id = SectionLoadHistory::eStopIDNow);
1069 
1070     bool
1071     SetSectionLoadAddress (const lldb::SectionSP &section,
1072                            lldb::addr_t load_addr,
1073                            bool warn_multiple = false);
1074 
1075     bool
1076     SetSectionUnloaded (const lldb::SectionSP &section_sp);
1077 
1078     bool
1079     SetSectionUnloaded (const lldb::SectionSP &section_sp, lldb::addr_t load_addr);
1080 
1081     void
1082     ClearAllLoadedSections ();
1083 
1084     // Since expressions results can persist beyond the lifetime of a process,
1085     // and the const expression results are available after a process is gone,
1086     // we provide a way for expressions to be evaluated from the Target itself.
1087     // If an expression is going to be run, then it should have a frame filled
1088     // in in th execution context.
1089     ExecutionResults
1090     EvaluateExpression (const char *expression,
1091                         StackFrame *frame,
1092                         lldb::ValueObjectSP &result_valobj_sp,
1093                         const EvaluateExpressionOptions& options = EvaluateExpressionOptions());
1094 
1095     ClangPersistentVariables &
GetPersistentVariables()1096     GetPersistentVariables()
1097     {
1098         return m_persistent_variables;
1099     }
1100 
1101     //------------------------------------------------------------------
1102     // Target Stop Hooks
1103     //------------------------------------------------------------------
1104     class StopHook : public UserID
1105     {
1106     public:
1107         ~StopHook ();
1108 
1109         StopHook (const StopHook &rhs);
1110 
1111         StringList *
GetCommandPointer()1112         GetCommandPointer ()
1113         {
1114             return &m_commands;
1115         }
1116 
1117         const StringList &
GetCommands()1118         GetCommands()
1119         {
1120             return m_commands;
1121         }
1122 
1123         lldb::TargetSP &
GetTarget()1124         GetTarget()
1125         {
1126             return m_target_sp;
1127         }
1128 
1129         void
SetCommands(StringList & in_commands)1130         SetCommands (StringList &in_commands)
1131         {
1132             m_commands = in_commands;
1133         }
1134 
1135         // Set the specifier.  The stop hook will own the specifier, and is responsible for deleting it when we're done.
1136         void
SetSpecifier(SymbolContextSpecifier * specifier)1137         SetSpecifier (SymbolContextSpecifier *specifier)
1138         {
1139             m_specifier_sp.reset (specifier);
1140         }
1141 
1142         SymbolContextSpecifier *
GetSpecifier()1143         GetSpecifier ()
1144         {
1145             return m_specifier_sp.get();
1146         }
1147 
1148         // Set the Thread Specifier.  The stop hook will own the thread specifier, and is responsible for deleting it when we're done.
1149         void
1150         SetThreadSpecifier (ThreadSpec *specifier);
1151 
1152         ThreadSpec *
GetThreadSpecifier()1153         GetThreadSpecifier()
1154         {
1155             return m_thread_spec_ap.get();
1156         }
1157 
1158         bool
IsActive()1159         IsActive()
1160         {
1161             return m_active;
1162         }
1163 
1164         void
SetIsActive(bool is_active)1165         SetIsActive (bool is_active)
1166         {
1167             m_active = is_active;
1168         }
1169 
1170         void
1171         GetDescription (Stream *s, lldb::DescriptionLevel level) const;
1172 
1173     private:
1174         lldb::TargetSP m_target_sp;
1175         StringList   m_commands;
1176         lldb::SymbolContextSpecifierSP m_specifier_sp;
1177         std::unique_ptr<ThreadSpec> m_thread_spec_ap;
1178         bool m_active;
1179 
1180         // Use CreateStopHook to make a new empty stop hook. The GetCommandPointer and fill it with commands,
1181         // and SetSpecifier to set the specifier shared pointer (can be null, that will match anything.)
1182         StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid);
1183         friend class Target;
1184     };
1185     typedef std::shared_ptr<StopHook> StopHookSP;
1186 
1187     // Add an empty stop hook to the Target's stop hook list, and returns a shared pointer to it in new_hook.
1188     // Returns the id of the new hook.
1189     StopHookSP
1190     CreateStopHook ();
1191 
1192     void
1193     RunStopHooks ();
1194 
1195     size_t
1196     GetStopHookSize();
1197 
1198     bool
SetSuppresStopHooks(bool suppress)1199     SetSuppresStopHooks (bool suppress)
1200     {
1201         bool old_value = m_suppress_stop_hooks;
1202         m_suppress_stop_hooks = suppress;
1203         return old_value;
1204     }
1205 
1206     bool
GetSuppressStopHooks()1207     GetSuppressStopHooks ()
1208     {
1209         return m_suppress_stop_hooks;
1210     }
1211 
1212 //    StopHookSP &
1213 //    GetStopHookByIndex (size_t index);
1214 //
1215     bool
1216     RemoveStopHookByID (lldb::user_id_t uid);
1217 
1218     void
1219     RemoveAllStopHooks ();
1220 
1221     StopHookSP
1222     GetStopHookByID (lldb::user_id_t uid);
1223 
1224     bool
1225     SetStopHookActiveStateByID (lldb::user_id_t uid, bool active_state);
1226 
1227     void
1228     SetAllStopHooksActiveState (bool active_state);
1229 
GetNumStopHooks()1230     size_t GetNumStopHooks () const
1231     {
1232         return m_stop_hooks.size();
1233     }
1234 
1235     StopHookSP
GetStopHookAtIndex(size_t index)1236     GetStopHookAtIndex (size_t index)
1237     {
1238         if (index >= GetNumStopHooks())
1239             return StopHookSP();
1240         StopHookCollection::iterator pos = m_stop_hooks.begin();
1241 
1242         while (index > 0)
1243         {
1244             pos++;
1245             index--;
1246         }
1247         return (*pos).second;
1248     }
1249 
1250     lldb::PlatformSP
GetPlatform()1251     GetPlatform ()
1252     {
1253         return m_platform_sp;
1254     }
1255 
1256     void
SetPlatform(const lldb::PlatformSP & platform_sp)1257     SetPlatform (const lldb::PlatformSP &platform_sp)
1258     {
1259         m_platform_sp = platform_sp;
1260     }
1261 
1262     SourceManager &
1263     GetSourceManager ();
1264 
1265     //------------------------------------------------------------------
1266     // Methods.
1267     //------------------------------------------------------------------
1268     lldb::SearchFilterSP
1269     GetSearchFilterForModule (const FileSpec *containingModule);
1270 
1271     lldb::SearchFilterSP
1272     GetSearchFilterForModuleList (const FileSpecList *containingModuleList);
1273 
1274     lldb::SearchFilterSP
1275     GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules, const FileSpecList *containingSourceFiles);
1276 
1277 protected:
1278     //------------------------------------------------------------------
1279     // Member variables.
1280     //------------------------------------------------------------------
1281     Debugger &      m_debugger;
1282     lldb::PlatformSP m_platform_sp;     ///< The platform for this target.
1283     Mutex           m_mutex;            ///< An API mutex that is used by the lldb::SB* classes make the SB interface thread safe
1284     ArchSpec        m_arch;
1285     ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
1286     SectionLoadHistory m_section_load_history;
1287     BreakpointList  m_breakpoint_list;
1288     BreakpointList  m_internal_breakpoint_list;
1289     lldb::BreakpointSP m_last_created_breakpoint;
1290     WatchpointList  m_watchpoint_list;
1291     lldb::WatchpointSP m_last_created_watchpoint;
1292     // We want to tightly control the process destruction process so
1293     // we can correctly tear down everything that we need to, so the only
1294     // class that knows about the process lifespan is this target class.
1295     lldb::ProcessSP m_process_sp;
1296     lldb::SearchFilterSP  m_search_filter_sp;
1297     PathMappingList m_image_search_paths;
1298     std::unique_ptr<ClangASTContext> m_scratch_ast_context_ap;
1299     std::unique_ptr<ClangASTSource> m_scratch_ast_source_ap;
1300     std::unique_ptr<ClangASTImporter> m_ast_importer_ap;
1301     ClangPersistentVariables m_persistent_variables;      ///< These are the persistent variables associated with this process for the expression parser.
1302 
1303     std::unique_ptr<SourceManager> m_source_manager_ap;
1304 
1305     typedef std::map<lldb::user_id_t, StopHookSP> StopHookCollection;
1306     StopHookCollection      m_stop_hooks;
1307     lldb::user_id_t         m_stop_hook_next_id;
1308     bool                    m_valid;
1309     bool                    m_suppress_stop_hooks;
1310 
1311     static void
1312     ImageSearchPathsChanged (const PathMappingList &path_list,
1313                              void *baton);
1314 
1315 private:
1316     DISALLOW_COPY_AND_ASSIGN (Target);
1317 };
1318 
1319 } // namespace lldb_private
1320 
1321 #endif  // liblldb_Target_h_
1322