Target.h revision bcb5b454767121980d937d2610ba762fdb575c45
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-private.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
35class 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    //------------------------------------------------------------------
136    /// Broadcaster event bits definitions.
137    //------------------------------------------------------------------
138    enum
139    {
140        eBroadcastBitBreakpointChanged  = (1 << 0),
141        eBroadcastBitModulesLoaded      = (1 << 1),
142        eBroadcastBitModulesUnloaded    = (1 << 2)
143    };
144
145    lldb::ModuleSP
146    GetSharedModule (const FileSpec& file_spec,
147                     const ArchSpec& arch,
148                     const UUID *uuid = NULL,
149                     const ConstString *object_name = NULL,
150                     off_t object_offset = 0,
151                     Error *error_ptr = NULL);
152private:
153    //------------------------------------------------------------------
154    /// Construct with optional file and arch.
155    ///
156    /// This member is private. Clients must use
157    /// TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
158    /// so all targets can be tracked from the central target list.
159    ///
160    /// @see TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
161    //------------------------------------------------------------------
162    Target(Debugger &debugger);
163
164public:
165    ~Target();
166
167    void
168    DeleteCurrentProcess ();
169
170    //------------------------------------------------------------------
171    /// Dump a description of this object to a Stream.
172    ///
173    /// Dump a description of the contents of this object to the
174    /// supplied stream \a s. The dumped content will be only what has
175    /// been loaded or parsed up to this point at which this function
176    /// is called, so this is a good way to see what has been parsed
177    /// in a target.
178    ///
179    /// @param[in] s
180    ///     The stream to which to dump the object descripton.
181    //------------------------------------------------------------------
182    void
183    Dump (Stream *s);
184
185    const lldb::ProcessSP &
186    CreateProcess (Listener &listener, const char *plugin_name = NULL);
187
188    const lldb::ProcessSP &
189    GetProcessSP () const;
190
191    lldb::TargetSP
192    GetSP();
193
194
195    //------------------------------------------------------------------
196    // This part handles the breakpoints.
197    //------------------------------------------------------------------
198
199    BreakpointList &
200    GetBreakpointList(bool internal = false);
201
202    const BreakpointList &
203    GetBreakpointList(bool internal = false) const;
204
205    lldb::BreakpointSP
206    GetBreakpointByID (lldb::break_id_t break_id);
207
208    // Use this to create a file and line breakpoint to a given module or all module it is NULL
209    lldb::BreakpointSP
210    CreateBreakpoint (const FileSpec *containingModule,
211                      const FileSpec &file,
212                      uint32_t line_no,
213                      bool check_inlines,
214                      bool internal = false);
215
216    // Use this to create a breakpoint from a load address
217    lldb::BreakpointSP
218    CreateBreakpoint (lldb::addr_t load_addr,
219                      bool internal = false);
220
221    // Use this to create Address breakpoints:
222    lldb::BreakpointSP
223    CreateBreakpoint (Address &addr,
224                      bool internal = false);
225
226    // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL
227    lldb::BreakpointSP
228    CreateBreakpoint (FileSpec *containingModule,
229                      RegularExpression &func_regexp,
230                      bool internal = false);
231
232    // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
233    lldb::BreakpointSP
234    CreateBreakpoint (FileSpec *containingModule,
235                      const char *func_name,
236                      uint32_t func_name_type_mask,
237                      bool internal = false);
238
239    // Use this to create a general breakpoint:
240    lldb::BreakpointSP
241    CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
242                      lldb::BreakpointResolverSP &resolver_sp,
243                      bool internal = false);
244
245    void
246    RemoveAllBreakpoints (bool internal_also = false);
247
248    void
249    DisableAllBreakpoints (bool internal_also = false);
250
251    void
252    EnableAllBreakpoints (bool internal_also = false);
253
254    bool
255    DisableBreakpointByID (lldb::break_id_t break_id);
256
257    bool
258    EnableBreakpointByID (lldb::break_id_t break_id);
259
260    bool
261    RemoveBreakpointByID (lldb::break_id_t break_id);
262
263    void
264    ModulesDidLoad (ModuleList &module_list);
265
266    void
267    ModulesDidUnload (ModuleList &module_list);
268
269protected:
270    void
271    ModuleAdded (lldb::ModuleSP &module_sp);
272
273    void
274    ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp);
275
276public:
277    //------------------------------------------------------------------
278    /// Gets the module for the main executable.
279    ///
280    /// Each process has a notion of a main executable that is the file
281    /// that will be executed or attached to. Executable files can have
282    /// dependent modules that are discovered from the object files, or
283    /// discovered at runtime as things are dynamically loaded.
284    ///
285    /// @return
286    ///     The shared pointer to the executable module which can
287    ///     contains a NULL Module object if no executable has been
288    ///     set.
289    ///
290    /// @see DynamicLoader
291    /// @see ObjectFile::GetDependentModules (FileSpecList&)
292    /// @see Process::SetExecutableModule(lldb::ModuleSP&)
293    //------------------------------------------------------------------
294    lldb::ModuleSP
295    GetExecutableModule ();
296
297    //------------------------------------------------------------------
298    /// Set the main executable module.
299    ///
300    /// Each process has a notion of a main executable that is the file
301    /// that will be executed or attached to. Executable files can have
302    /// dependent modules that are discovered from the object files, or
303    /// discovered at runtime as things are dynamically loaded.
304    ///
305    /// Setting the executable causes any of the current dependant
306    /// image information to be cleared and replaced with the static
307    /// dependent image information found by calling
308    /// ObjectFile::GetDependentModules (FileSpecList&) on the main
309    /// executable and any modules on which it depends. Calling
310    /// Process::GetImages() will return the newly found images that
311    /// were obtained from all of the object files.
312    ///
313    /// @param[in] module_sp
314    ///     A shared pointer reference to the module that will become
315    ///     the main executable for this process.
316    ///
317    /// @param[in] get_dependent_files
318    ///     If \b true then ask the object files to track down any
319    ///     known dependent files.
320    ///
321    /// @see ObjectFile::GetDependentModules (FileSpecList&)
322    /// @see Process::GetImages()
323    //------------------------------------------------------------------
324    void
325    SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
326
327    //------------------------------------------------------------------
328    /// Get accessor for the images for this process.
329    ///
330    /// Each process has a notion of a main executable that is the file
331    /// that will be executed or attached to. Executable files can have
332    /// dependent modules that are discovered from the object files, or
333    /// discovered at runtime as things are dynamically loaded. After
334    /// a main executable has been set, the images will contain a list
335    /// of all the files that the executable depends upon as far as the
336    /// object files know. These images will usually contain valid file
337    /// virtual addresses only. When the process is launched or attached
338    /// to, the DynamicLoader plug-in will discover where these images
339    /// were loaded in memory and will resolve the load virtual
340    /// addresses is each image, and also in images that are loaded by
341    /// code.
342    ///
343    /// @return
344    ///     A list of Module objects in a module list.
345    //------------------------------------------------------------------
346    ModuleList&
347    GetImages ();
348
349    ArchSpec
350    GetArchitecture () const;
351
352    //------------------------------------------------------------------
353    /// Set the architecture for this target.
354    ///
355    /// If the current target has no Images read in, then this just sets the architecture, which will
356    /// be used to select the architecture of the ExecutableModule when that is set.
357    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
358    /// architecture from the currently selected one will reset the ExecutableModule to that slice
359    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
360    /// contain a fork of this architecture, then this code will return false, and the architecture
361    /// won't be changed.
362    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
363    ///
364    /// @param[in] arch_spec
365    ///     The new architecture.
366    ///
367    /// @return
368    ///     \b true if the architecture was successfully set, \bfalse otherwise.
369    //------------------------------------------------------------------
370    bool
371    SetArchitecture (const ArchSpec &arch_spec);
372
373    Debugger &
374    GetDebugger ()
375    {
376        return m_debugger;
377    }
378
379    bool
380    GetTargetTriple (ConstString &target_triple);
381
382    size_t
383    ReadMemory (const Address& addr,
384                void *dst,
385                size_t dst_len,
386                Error &error);
387
388
389
390    SectionLoadList&
391    GetSectionLoadList()
392    {
393        return m_section_load_list;
394    }
395
396    const SectionLoadList&
397    GetSectionLoadList() const
398    {
399        return m_section_load_list;
400    }
401
402    //------------------------------------------------------------------
403    // lldb::ExecutionContextScope pure virtual functions
404    //------------------------------------------------------------------
405    virtual Target *
406    CalculateTarget ();
407
408    virtual Process *
409    CalculateProcess ();
410
411    virtual Thread *
412    CalculateThread ();
413
414    virtual StackFrame *
415    CalculateStackFrame ();
416
417    virtual void
418    Calculate (ExecutionContext &exe_ctx);
419
420    PathMappingList &
421    GetImageSearchPathList ();
422
423    ClangASTContext *
424    GetScratchClangASTContext();
425
426protected:
427    friend class lldb::SBTarget;
428
429    //------------------------------------------------------------------
430    // Member variables.
431    //------------------------------------------------------------------
432    Debugger &      m_debugger;
433    ArchSpec        m_arch_spec;
434    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
435    SectionLoadList m_section_load_list;
436    BreakpointList  m_breakpoint_list;
437    BreakpointList  m_internal_breakpoint_list;
438    // We want to tightly control the process destruction process so
439    // we can correctly tear down everything that we need to, so the only
440    // class that knows about the process lifespan is this target class.
441    lldb::ProcessSP m_process_sp;
442    ConstString     m_triple;       ///< The target triple ("x86_64-apple-darwin10")
443    lldb::SearchFilterSP  m_search_filter_sp;
444    PathMappingList m_image_search_paths;
445    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
446    //------------------------------------------------------------------
447    // Methods.
448    //------------------------------------------------------------------
449    lldb::SearchFilterSP
450    GetSearchFilterForModule (const FileSpec *containingModule);
451
452    static void
453    ImageSearchPathsChanged (const PathMappingList &path_list,
454                             void *baton);
455
456private:
457    DISALLOW_COPY_AND_ASSIGN (Target);
458};
459
460} // namespace lldb_private
461
462#endif  // liblldb_Target_h_
463