Target.h revision d284b663aa7d08b4f767de5ffa6289f33fcbcce7
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
16// Other libraries and framework includes
17// Project includes
18#include "lldb/lldb-include.h"
19#include "lldb/Breakpoint/BreakpointList.h"
20#include "lldb/Breakpoint/BreakpointLocationCollection.h"
21#include "lldb/Core/Broadcaster.h"
22#include "lldb/Core/Event.h"
23#include "lldb/Core/ModuleList.h"
24#include "lldb/Core/UserSettingsController.h"
25#include "lldb/Symbol/SymbolContext.h"
26#include "lldb/Target/ABI.h"
27#include "lldb/Target/ExecutionContextScope.h"
28#include "lldb/Target/PathMappingList.h"
29#include "lldb/Target/SectionLoadList.h"
30
31#include "lldb/API/SBTarget.h"
32
33namespace lldb_private {
34
35//----------------------------------------------------------------------
36// TargetInstanceSettings
37//----------------------------------------------------------------------
38class TargetInstanceSettings : public InstanceSettings
39{
40public:
41
42    TargetInstanceSettings (UserSettingsController &owner, bool live_instance = true, const char *name = NULL);
43
44    TargetInstanceSettings (const TargetInstanceSettings &rhs);
45
46    virtual
47    ~TargetInstanceSettings ();
48
49    TargetInstanceSettings&
50    operator= (const TargetInstanceSettings &rhs);
51
52    void
53    UpdateInstanceSettingsVariable (const ConstString &var_name,
54                                    const char *index_value,
55                                    const char *value,
56                                    const ConstString &instance_name,
57                                    const SettingEntry &entry,
58                                    lldb::VarSetOperationType op,
59                                    Error &err,
60                                    bool pending);
61
62    bool
63    GetInstanceSettingsValue (const SettingEntry &entry,
64                              const ConstString &var_name,
65                              StringList &value,
66                              Error *err);
67
68    lldb::ExecutionLevel
69    GetExecutionLevel () const
70    {
71        return m_execution_level;
72    }
73
74    void
75    SetExecutionLevel (lldb::ExecutionLevel execution_level)
76    {
77        m_execution_level = execution_level;
78    }
79
80    lldb::ExecutionMode
81    GetExecutionMode () const
82    {
83        return m_execution_mode;
84    }
85
86    void
87    SetExecutionMode (lldb::ExecutionMode execution_mode)
88    {
89        m_execution_mode = execution_mode;
90    }
91
92    lldb::ExecutionOSType
93    GetExecutionOSType () const
94    {
95        return m_execution_os_type;
96    }
97
98    void
99    SetExecutionOSType (lldb::ExecutionOSType execution_os_type)
100    {
101        m_execution_os_type = execution_os_type;
102    }
103
104
105protected:
106
107    void
108    CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
109                          bool pending);
110
111    const ConstString
112    CreateInstanceName ();
113
114    std::string m_expr_prefix_path;
115    std::string m_expr_prefix_contents;
116    lldb::ExecutionLevel m_execution_level;
117    lldb::ExecutionMode m_execution_mode;
118    lldb::ExecutionOSType m_execution_os_type;
119
120};
121
122//----------------------------------------------------------------------
123// Target
124//----------------------------------------------------------------------
125class Target :
126    public Broadcaster,
127    public ExecutionContextScope,
128    public TargetInstanceSettings
129{
130public:
131    friend class TargetList;
132
133    //------------------------------------------------------------------
134    /// Broadcaster event bits definitions.
135    //------------------------------------------------------------------
136    enum
137    {
138        eBroadcastBitBreakpointChanged  = (1 << 0),
139        eBroadcastBitModulesLoaded      = (1 << 1),
140        eBroadcastBitModulesUnloaded    = (1 << 2)
141    };
142
143    static void
144    Initialize ();
145
146    static void
147    Terminate ();
148
149    static lldb::UserSettingsControllerSP &
150    GetSettingsController ();
151
152    static ArchSpec
153    GetDefaultArchitecture ();
154
155    static void
156    SetDefaultArchitecture (ArchSpec new_arch);
157
158    void
159    UpdateInstanceName ();
160
161    lldb::ModuleSP
162    GetSharedModule (const FileSpec& file_spec,
163                     const ArchSpec& arch,
164                     const lldb_private::UUID *uuid = NULL,
165                     const ConstString *object_name = NULL,
166                     off_t object_offset = 0,
167                     Error *error_ptr = NULL);
168private:
169    //------------------------------------------------------------------
170    /// Construct with optional file and arch.
171    ///
172    /// This member is private. Clients must use
173    /// TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
174    /// so all targets can be tracked from the central target list.
175    ///
176    /// @see TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
177    //------------------------------------------------------------------
178    Target(Debugger &debugger);
179
180public:
181    ~Target();
182
183    Mutex &
184    GetAPIMutex ()
185    {
186        return m_mutex;
187    }
188
189    void
190    DeleteCurrentProcess ();
191
192    //------------------------------------------------------------------
193    /// Dump a description of this object to a Stream.
194    ///
195    /// Dump a description of the contents of this object to the
196    /// supplied stream \a s. The dumped content will be only what has
197    /// been loaded or parsed up to this point at which this function
198    /// is called, so this is a good way to see what has been parsed
199    /// in a target.
200    ///
201    /// @param[in] s
202    ///     The stream to which to dump the object descripton.
203    //------------------------------------------------------------------
204    void
205    Dump (Stream *s, lldb::DescriptionLevel description_level);
206
207    const lldb::ProcessSP &
208    CreateProcess (Listener &listener, const char *plugin_name = NULL);
209
210    const lldb::ProcessSP &
211    GetProcessSP () const;
212
213    lldb::TargetSP
214    GetSP();
215
216    //------------------------------------------------------------------
217    // This part handles the breakpoints.
218    //------------------------------------------------------------------
219
220    BreakpointList &
221    GetBreakpointList(bool internal = false);
222
223    const BreakpointList &
224    GetBreakpointList(bool internal = false) const;
225
226    lldb::BreakpointSP
227    GetLastCreatedBreakpoint ()
228    {
229        return m_last_created_breakpoint;
230    }
231
232    lldb::BreakpointSP
233    GetBreakpointByID (lldb::break_id_t break_id);
234
235    // Use this to create a file and line breakpoint to a given module or all module it is NULL
236    lldb::BreakpointSP
237    CreateBreakpoint (const FileSpec *containingModule,
238                      const FileSpec &file,
239                      uint32_t line_no,
240                      bool check_inlines,
241                      bool internal = false);
242
243    // Use this to create a breakpoint from a load address
244    lldb::BreakpointSP
245    CreateBreakpoint (lldb::addr_t load_addr,
246                      bool internal = false);
247
248    // Use this to create Address breakpoints:
249    lldb::BreakpointSP
250    CreateBreakpoint (Address &addr,
251                      bool internal = false);
252
253    // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL
254    lldb::BreakpointSP
255    CreateBreakpoint (FileSpec *containingModule,
256                      RegularExpression &func_regexp,
257                      bool internal = false);
258
259    // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
260    lldb::BreakpointSP
261    CreateBreakpoint (FileSpec *containingModule,
262                      const char *func_name,
263                      uint32_t func_name_type_mask,
264                      bool internal = false);
265
266    // Use this to create a general breakpoint:
267    lldb::BreakpointSP
268    CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
269                      lldb::BreakpointResolverSP &resolver_sp,
270                      bool internal = false);
271
272    void
273    RemoveAllBreakpoints (bool internal_also = false);
274
275    void
276    DisableAllBreakpoints (bool internal_also = false);
277
278    void
279    EnableAllBreakpoints (bool internal_also = false);
280
281    bool
282    DisableBreakpointByID (lldb::break_id_t break_id);
283
284    bool
285    EnableBreakpointByID (lldb::break_id_t break_id);
286
287    bool
288    RemoveBreakpointByID (lldb::break_id_t break_id);
289
290    void
291    ModulesDidLoad (ModuleList &module_list);
292
293    void
294    ModulesDidUnload (ModuleList &module_list);
295
296protected:
297    void
298    ModuleAdded (lldb::ModuleSP &module_sp);
299
300    void
301    ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp);
302
303public:
304    //------------------------------------------------------------------
305    /// Gets the module for the main executable.
306    ///
307    /// Each process has a notion of a main executable that is the file
308    /// that will be executed or attached to. Executable files can have
309    /// dependent modules that are discovered from the object files, or
310    /// discovered at runtime as things are dynamically loaded.
311    ///
312    /// @return
313    ///     The shared pointer to the executable module which can
314    ///     contains a NULL Module object if no executable has been
315    ///     set.
316    ///
317    /// @see DynamicLoader
318    /// @see ObjectFile::GetDependentModules (FileSpecList&)
319    /// @see Process::SetExecutableModule(lldb::ModuleSP&)
320    //------------------------------------------------------------------
321    lldb::ModuleSP
322    GetExecutableModule ();
323
324    //------------------------------------------------------------------
325    /// Set the main executable module.
326    ///
327    /// Each process has a notion of a main executable that is the file
328    /// that will be executed or attached to. Executable files can have
329    /// dependent modules that are discovered from the object files, or
330    /// discovered at runtime as things are dynamically loaded.
331    ///
332    /// Setting the executable causes any of the current dependant
333    /// image information to be cleared and replaced with the static
334    /// dependent image information found by calling
335    /// ObjectFile::GetDependentModules (FileSpecList&) on the main
336    /// executable and any modules on which it depends. Calling
337    /// Process::GetImages() will return the newly found images that
338    /// were obtained from all of the object files.
339    ///
340    /// @param[in] module_sp
341    ///     A shared pointer reference to the module that will become
342    ///     the main executable for this process.
343    ///
344    /// @param[in] get_dependent_files
345    ///     If \b true then ask the object files to track down any
346    ///     known dependent files.
347    ///
348    /// @see ObjectFile::GetDependentModules (FileSpecList&)
349    /// @see Process::GetImages()
350    //------------------------------------------------------------------
351    void
352    SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
353
354    //------------------------------------------------------------------
355    /// Get accessor for the images for this process.
356    ///
357    /// Each process has a notion of a main executable that is the file
358    /// that will be executed or attached to. Executable files can have
359    /// dependent modules that are discovered from the object files, or
360    /// discovered at runtime as things are dynamically loaded. After
361    /// a main executable has been set, the images will contain a list
362    /// of all the files that the executable depends upon as far as the
363    /// object files know. These images will usually contain valid file
364    /// virtual addresses only. When the process is launched or attached
365    /// to, the DynamicLoader plug-in will discover where these images
366    /// were loaded in memory and will resolve the load virtual
367    /// addresses is each image, and also in images that are loaded by
368    /// code.
369    ///
370    /// @return
371    ///     A list of Module objects in a module list.
372    //------------------------------------------------------------------
373    ModuleList&
374    GetImages ()
375    {
376        return m_images;
377    }
378
379    const ModuleList&
380    GetImages () const
381    {
382        return m_images;
383    }
384
385    ArchSpec &
386    GetArchitecture ()
387    {
388        return m_arch_spec;
389    }
390
391    const ArchSpec &
392    GetArchitecture () const
393    {
394        return m_arch_spec;
395    }
396
397    //------------------------------------------------------------------
398    /// Set the architecture for this target.
399    ///
400    /// If the current target has no Images read in, then this just sets the architecture, which will
401    /// be used to select the architecture of the ExecutableModule when that is set.
402    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
403    /// architecture from the currently selected one will reset the ExecutableModule to that slice
404    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
405    /// contain a fork of this architecture, then this code will return false, and the architecture
406    /// won't be changed.
407    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
408    ///
409    /// @param[in] arch_spec
410    ///     The new architecture.
411    ///
412    /// @return
413    ///     \b true if the architecture was successfully set, \bfalse otherwise.
414    //------------------------------------------------------------------
415    bool
416    SetArchitecture (const ArchSpec &arch_spec);
417
418    Debugger &
419    GetDebugger ()
420    {
421        return m_debugger;
422    }
423
424    size_t
425    ReadMemoryFromFileCache (const Address& addr,
426                             void *dst,
427                             size_t dst_len,
428                             Error &error);
429
430    // Reading memory through the target allows us to skip going to the process
431    // for reading memory if possible and it allows us to try and read from
432    // any constant sections in our object files on disk. If you always want
433    // live program memory, read straight from the process. If you possibly
434    // want to read from const sections in object files, read from the target.
435    // This version of ReadMemory will try and read memory from the process
436    // if the process is alive. The order is:
437    // 1 - if (prefer_file_cache == true) then read from object file cache
438    // 2 - if there is a valid process, try and read from its memory
439    // 3 - if (prefer_file_cache == false) then read from object file cache
440    size_t
441    ReadMemory (const Address& addr,
442                bool prefer_file_cache,
443                void *dst,
444                size_t dst_len,
445                Error &error);
446
447    SectionLoadList&
448    GetSectionLoadList()
449    {
450        return m_section_load_list;
451    }
452
453    const SectionLoadList&
454    GetSectionLoadList() const
455    {
456        return m_section_load_list;
457    }
458
459
460    static Target *
461    GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr,
462                           const SymbolContext *sc_ptr);
463
464    //------------------------------------------------------------------
465    // lldb::ExecutionContextScope pure virtual functions
466    //------------------------------------------------------------------
467    virtual Target *
468    CalculateTarget ();
469
470    virtual Process *
471    CalculateProcess ();
472
473    virtual Thread *
474    CalculateThread ();
475
476    virtual StackFrame *
477    CalculateStackFrame ();
478
479    virtual void
480    CalculateExecutionContext (ExecutionContext &exe_ctx);
481
482    PathMappingList &
483    GetImageSearchPathList ();
484
485    ClangASTContext *
486    GetScratchClangASTContext();
487
488    const char *
489    GetExpressionPrefixContentsAsCString ();
490
491    // Since expressions results can persist beyond the lifetime of a process,
492    // and the const expression results are available after a process is gone,
493    // we provide a way for expressions to be evaluated from the Target itself.
494    // If an expression is going to be run, then it should have a frame filled
495    // in in th execution context.
496    lldb::ExecutionResults
497    EvaluateExpression (const char *expression,
498                        StackFrame *frame,
499                        bool unwind_on_error,
500                        bool keep_in_memory,
501                        lldb::ValueObjectSP &result_valobj_sp);
502
503    ClangPersistentVariables &
504    GetPersistentVariables()
505    {
506        return m_persistent_variables;
507    }
508
509
510    //------------------------------------------------------------------
511    // Target::SettingsController
512    //------------------------------------------------------------------
513    class SettingsController : public UserSettingsController
514    {
515    public:
516        SettingsController ();
517
518        virtual
519        ~SettingsController ();
520
521        bool
522        SetGlobalVariable (const ConstString &var_name,
523                           const char *index_value,
524                           const char *value,
525                           const SettingEntry &entry,
526                           const lldb::VarSetOperationType op,
527                           Error&err);
528
529        bool
530        GetGlobalVariable (const ConstString &var_name,
531                           StringList &value,
532                           Error &err);
533
534        static SettingEntry global_settings_table[];
535        static SettingEntry instance_settings_table[];
536
537    protected:
538
539        lldb::InstanceSettingsSP
540        CreateInstanceSettings (const char *instance_name);
541
542    private:
543
544        // Class-wide settings.
545        ArchSpec m_default_architecture;
546
547        DISALLOW_COPY_AND_ASSIGN (SettingsController);
548    };
549
550
551protected:
552    friend class lldb::SBTarget;
553
554    //------------------------------------------------------------------
555    // Member variables.
556    //------------------------------------------------------------------
557    Debugger &      m_debugger;
558    Mutex           m_mutex;            ///< An API mutex that is used by the lldb::SB* classes make the SB interface thread safe
559    ArchSpec        m_arch_spec;
560    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
561    SectionLoadList m_section_load_list;
562    BreakpointList  m_breakpoint_list;
563    BreakpointList  m_internal_breakpoint_list;
564    lldb::BreakpointSP m_last_created_breakpoint;
565    // We want to tightly control the process destruction process so
566    // we can correctly tear down everything that we need to, so the only
567    // class that knows about the process lifespan is this target class.
568    lldb::ProcessSP m_process_sp;
569    lldb::SearchFilterSP  m_search_filter_sp;
570    PathMappingList m_image_search_paths;
571    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
572    ClangPersistentVariables m_persistent_variables;      ///< These are the persistent variables associated with this process for the expression parser.
573
574    //------------------------------------------------------------------
575    // Methods.
576    //------------------------------------------------------------------
577    lldb::SearchFilterSP
578    GetSearchFilterForModule (const FileSpec *containingModule);
579
580    static void
581    ImageSearchPathsChanged (const PathMappingList &path_list,
582                             void *baton);
583
584private:
585    DISALLOW_COPY_AND_ASSIGN (Target);
586};
587
588} // namespace lldb_private
589
590#endif  // liblldb_Target_h_
591