Target.h revision a830adbcd63d1995a01e6e18da79893c1426ca43
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===-- Target.h ------------------------------------------------*- C++ -*-===//
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// License. See LICENSE.TXT for details.
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===----------------------------------------------------------------------===//
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef liblldb_Target_h_
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define liblldb_Target_h_
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// C Includes
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// C++ Includes
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Other libraries and framework includes
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Project includes
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/lldb-private.h"
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Breakpoint/BreakpointList.h"
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Breakpoint/BreakpointLocationCollection.h"
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Core/Broadcaster.h"
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Core/Event.h"
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Core/ModuleList.h"
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Core/UserSettingsController.h"
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Symbol/SymbolContext.h"
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Target/ABI.h"
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Target/ExecutionContextScope.h"
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Target/PathMappingList.h"
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/Target/SectionLoadList.h"
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "lldb/API/SBTarget.h"
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace lldb_private {
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class TargetInstanceSettings : public InstanceSettings
36{
37public:
38
39    TargetInstanceSettings (UserSettingsController &owner, bool live_instance = true, const char *name = NULL);
40
41    TargetInstanceSettings (const TargetInstanceSettings &rhs);
42
43    virtual
44    ~TargetInstanceSettings ();
45
46    TargetInstanceSettings&
47    operator= (const TargetInstanceSettings &rhs);
48
49    void
50    UpdateInstanceSettingsVariable (const ConstString &var_name,
51                                    const char *index_value,
52                                    const char *value,
53                                    const ConstString &instance_name,
54                                    const SettingEntry &entry,
55                                    lldb::VarSetOperationType op,
56                                    Error &err,
57                                    bool pending);
58
59    bool
60    GetInstanceSettingsValue (const SettingEntry &entry,
61                              const ConstString &var_name,
62                              StringList &value,
63                              Error *err);
64
65protected:
66
67    void
68    CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
69                          bool pending);
70
71    const ConstString
72    CreateInstanceName ();
73
74private:
75
76};
77
78class Target :
79    public Broadcaster,
80    public ExecutionContextScope,
81    public TargetInstanceSettings
82{
83public:
84    friend class TargetList;
85
86    class SettingsController : public UserSettingsController
87    {
88    public:
89        SettingsController ();
90
91        virtual
92        ~SettingsController ();
93
94        bool
95        SetGlobalVariable (const ConstString &var_name,
96                           const char *index_value,
97                           const char *value,
98                           const SettingEntry &entry,
99                           const lldb::VarSetOperationType op,
100                           Error&err);
101
102        bool
103        GetGlobalVariable (const ConstString &var_name,
104                           StringList &value,
105                           Error &err);
106
107        static SettingEntry global_settings_table[];
108        static SettingEntry instance_settings_table[];
109
110    protected:
111
112        lldb::InstanceSettingsSP
113        CreateInstanceSettings (const char *instance_name);
114
115        static const ConstString &
116        DefArchVarName ();
117
118    private:
119
120        // Class-wide settings.
121        ArchSpec m_default_architecture;
122
123        DISALLOW_COPY_AND_ASSIGN (SettingsController);
124    };
125
126    static lldb::UserSettingsControllerSP
127    GetSettingsController (bool finish = false);
128
129    static ArchSpec
130    GetDefaultArchitecture ();
131
132    static void
133    SetDefaultArchitecture (ArchSpec new_arch);
134
135    void
136    UpdateInstanceName ();
137
138    //------------------------------------------------------------------
139    /// Broadcaster event bits definitions.
140    //------------------------------------------------------------------
141    enum
142    {
143        eBroadcastBitBreakpointChanged  = (1 << 0),
144        eBroadcastBitModulesLoaded      = (1 << 1),
145        eBroadcastBitModulesUnloaded    = (1 << 2)
146    };
147
148    lldb::ModuleSP
149    GetSharedModule (const FileSpec& file_spec,
150                     const ArchSpec& arch,
151                     const UUID *uuid = NULL,
152                     const ConstString *object_name = NULL,
153                     off_t object_offset = 0,
154                     Error *error_ptr = NULL);
155private:
156    //------------------------------------------------------------------
157    /// Construct with optional file and arch.
158    ///
159    /// This member is private. Clients must use
160    /// TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
161    /// so all targets can be tracked from the central target list.
162    ///
163    /// @see TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
164    //------------------------------------------------------------------
165    Target(Debugger &debugger);
166
167public:
168    ~Target();
169
170    void
171    DeleteCurrentProcess ();
172
173    //------------------------------------------------------------------
174    /// Dump a description of this object to a Stream.
175    ///
176    /// Dump a description of the contents of this object to the
177    /// supplied stream \a s. The dumped content will be only what has
178    /// been loaded or parsed up to this point at which this function
179    /// is called, so this is a good way to see what has been parsed
180    /// in a target.
181    ///
182    /// @param[in] s
183    ///     The stream to which to dump the object descripton.
184    //------------------------------------------------------------------
185    void
186    Dump (Stream *s);
187
188    const lldb::ProcessSP &
189    CreateProcess (Listener &listener, const char *plugin_name = NULL);
190
191    const lldb::ProcessSP &
192    GetProcessSP () const;
193
194    lldb::TargetSP
195    GetSP();
196
197
198    //------------------------------------------------------------------
199    // This part handles the breakpoints.
200    //------------------------------------------------------------------
201
202    BreakpointList &
203    GetBreakpointList(bool internal = false);
204
205    const BreakpointList &
206    GetBreakpointList(bool internal = false) const;
207
208    lldb::BreakpointSP
209    GetBreakpointByID (lldb::break_id_t break_id);
210
211    // Use this to create a file and line breakpoint to a given module or all module it is NULL
212    lldb::BreakpointSP
213    CreateBreakpoint (const FileSpec *containingModule,
214                      const FileSpec &file,
215                      uint32_t line_no,
216                      bool check_inlines,
217                      bool internal = false);
218
219    // Use this to create a breakpoint from a load address
220    lldb::BreakpointSP
221    CreateBreakpoint (lldb::addr_t load_addr,
222                      bool internal = false);
223
224    // Use this to create Address breakpoints:
225    lldb::BreakpointSP
226    CreateBreakpoint (Address &addr,
227                      bool internal = false);
228
229    // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL
230    lldb::BreakpointSP
231    CreateBreakpoint (FileSpec *containingModule,
232                      RegularExpression &func_regexp,
233                      bool internal = false);
234
235    // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
236    lldb::BreakpointSP
237    CreateBreakpoint (FileSpec *containingModule,
238                      const char *func_name,
239                      uint32_t func_name_type_mask,
240                      bool internal = false);
241
242    // Use this to create a general breakpoint:
243    lldb::BreakpointSP
244    CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
245                      lldb::BreakpointResolverSP &resolver_sp,
246                      bool internal = false);
247
248    void
249    RemoveAllBreakpoints (bool internal_also = false);
250
251    void
252    DisableAllBreakpoints (bool internal_also = false);
253
254    void
255    EnableAllBreakpoints (bool internal_also = false);
256
257    bool
258    DisableBreakpointByID (lldb::break_id_t break_id);
259
260    bool
261    EnableBreakpointByID (lldb::break_id_t break_id);
262
263    bool
264    RemoveBreakpointByID (lldb::break_id_t break_id);
265
266    void
267    ModulesDidLoad (ModuleList &module_list);
268
269    void
270    ModulesDidUnload (ModuleList &module_list);
271
272protected:
273    void
274    ModuleAdded (lldb::ModuleSP &module_sp);
275
276    void
277    ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp);
278
279public:
280    //------------------------------------------------------------------
281    /// Gets the module for the main executable.
282    ///
283    /// Each process has a notion of a main executable that is the file
284    /// that will be executed or attached to. Executable files can have
285    /// dependent modules that are discovered from the object files, or
286    /// discovered at runtime as things are dynamically loaded.
287    ///
288    /// @return
289    ///     The shared pointer to the executable module which can
290    ///     contains a NULL Module object if no executable has been
291    ///     set.
292    ///
293    /// @see DynamicLoader
294    /// @see ObjectFile::GetDependentModules (FileSpecList&)
295    /// @see Process::SetExecutableModule(lldb::ModuleSP&)
296    //------------------------------------------------------------------
297    lldb::ModuleSP
298    GetExecutableModule ();
299
300    //------------------------------------------------------------------
301    /// Set the main executable module.
302    ///
303    /// Each process has a notion of a main executable that is the file
304    /// that will be executed or attached to. Executable files can have
305    /// dependent modules that are discovered from the object files, or
306    /// discovered at runtime as things are dynamically loaded.
307    ///
308    /// Setting the executable causes any of the current dependant
309    /// image information to be cleared and replaced with the static
310    /// dependent image information found by calling
311    /// ObjectFile::GetDependentModules (FileSpecList&) on the main
312    /// executable and any modules on which it depends. Calling
313    /// Process::GetImages() will return the newly found images that
314    /// were obtained from all of the object files.
315    ///
316    /// @param[in] module_sp
317    ///     A shared pointer reference to the module that will become
318    ///     the main executable for this process.
319    ///
320    /// @param[in] get_dependent_files
321    ///     If \b true then ask the object files to track down any
322    ///     known dependent files.
323    ///
324    /// @see ObjectFile::GetDependentModules (FileSpecList&)
325    /// @see Process::GetImages()
326    //------------------------------------------------------------------
327    void
328    SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
329
330    //------------------------------------------------------------------
331    /// Get accessor for the images for this process.
332    ///
333    /// Each process has a notion of a main executable that is the file
334    /// that will be executed or attached to. Executable files can have
335    /// dependent modules that are discovered from the object files, or
336    /// discovered at runtime as things are dynamically loaded. After
337    /// a main executable has been set, the images will contain a list
338    /// of all the files that the executable depends upon as far as the
339    /// object files know. These images will usually contain valid file
340    /// virtual addresses only. When the process is launched or attached
341    /// to, the DynamicLoader plug-in will discover where these images
342    /// were loaded in memory and will resolve the load virtual
343    /// addresses is each image, and also in images that are loaded by
344    /// code.
345    ///
346    /// @return
347    ///     A list of Module objects in a module list.
348    //------------------------------------------------------------------
349    ModuleList&
350    GetImages ();
351
352    ArchSpec
353    GetArchitecture () const;
354
355    //------------------------------------------------------------------
356    /// Set the architecture for this target.
357    ///
358    /// If the current target has no Images read in, then this just sets the architecture, which will
359    /// be used to select the architecture of the ExecutableModule when that is set.
360    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
361    /// architecture from the currently selected one will reset the ExecutableModule to that slice
362    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
363    /// contain a fork of this architecture, then this code will return false, and the architecture
364    /// won't be changed.
365    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
366    ///
367    /// @param[in] arch_spec
368    ///     The new architecture.
369    ///
370    /// @return
371    ///     \b true if the architecture was successfully set, \bfalse otherwise.
372    //------------------------------------------------------------------
373    bool
374    SetArchitecture (const ArchSpec &arch_spec);
375
376    Debugger &
377    GetDebugger ()
378    {
379        return m_debugger;
380    }
381
382    bool
383    GetTargetTriple (ConstString &target_triple);
384
385    size_t
386    ReadMemory (const Address& addr,
387                void *dst,
388                size_t dst_len,
389                Error &error);
390
391
392
393    SectionLoadList&
394    GetSectionLoadList()
395    {
396        return m_section_load_list;
397    }
398
399    const SectionLoadList&
400    GetSectionLoadList() const
401    {
402        return m_section_load_list;
403    }
404
405
406    static Target *
407    GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr,
408                           const SymbolContext *sc_ptr);
409
410    //------------------------------------------------------------------
411    // lldb::ExecutionContextScope pure virtual functions
412    //------------------------------------------------------------------
413    virtual Target *
414    CalculateTarget ();
415
416    virtual Process *
417    CalculateProcess ();
418
419    virtual Thread *
420    CalculateThread ();
421
422    virtual StackFrame *
423    CalculateStackFrame ();
424
425    virtual void
426    CalculateExecutionContext (ExecutionContext &exe_ctx);
427
428    PathMappingList &
429    GetImageSearchPathList ();
430
431    ClangASTContext *
432    GetScratchClangASTContext();
433
434protected:
435    friend class lldb::SBTarget;
436
437    //------------------------------------------------------------------
438    // Member variables.
439    //------------------------------------------------------------------
440    Debugger &      m_debugger;
441    ArchSpec        m_arch_spec;
442    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
443    SectionLoadList m_section_load_list;
444    BreakpointList  m_breakpoint_list;
445    BreakpointList  m_internal_breakpoint_list;
446    // We want to tightly control the process destruction process so
447    // we can correctly tear down everything that we need to, so the only
448    // class that knows about the process lifespan is this target class.
449    lldb::ProcessSP m_process_sp;
450    ConstString     m_triple;       ///< The target triple ("x86_64-apple-darwin10")
451    lldb::SearchFilterSP  m_search_filter_sp;
452    PathMappingList m_image_search_paths;
453    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
454    //------------------------------------------------------------------
455    // Methods.
456    //------------------------------------------------------------------
457    lldb::SearchFilterSP
458    GetSearchFilterForModule (const FileSpec *containingModule);
459
460    static void
461    ImageSearchPathsChanged (const PathMappingList &path_list,
462                             void *baton);
463
464private:
465    DISALLOW_COPY_AND_ASSIGN (Target);
466};
467
468} // namespace lldb_private
469
470#endif  // liblldb_Target_h_
471