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