Target.h revision 7826c8894803dc729f29789ebc038956a94d3e7a
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    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, lldb::DescriptionLevel description_level);
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    GetLastCreatedBreakpoint ()
210    {
211        return m_last_created_breakpoint;
212    }
213
214    lldb::BreakpointSP
215    GetBreakpointByID (lldb::break_id_t break_id);
216
217    // Use this to create a file and line breakpoint to a given module or all module it is NULL
218    lldb::BreakpointSP
219    CreateBreakpoint (const FileSpec *containingModule,
220                      const FileSpec &file,
221                      uint32_t line_no,
222                      bool check_inlines,
223                      bool internal = false);
224
225    // Use this to create a breakpoint from a load address
226    lldb::BreakpointSP
227    CreateBreakpoint (lldb::addr_t load_addr,
228                      bool internal = false);
229
230    // Use this to create Address breakpoints:
231    lldb::BreakpointSP
232    CreateBreakpoint (Address &addr,
233                      bool internal = false);
234
235    // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL
236    lldb::BreakpointSP
237    CreateBreakpoint (FileSpec *containingModule,
238                      RegularExpression &func_regexp,
239                      bool internal = false);
240
241    // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
242    lldb::BreakpointSP
243    CreateBreakpoint (FileSpec *containingModule,
244                      const char *func_name,
245                      uint32_t func_name_type_mask,
246                      bool internal = false);
247
248    // Use this to create a general breakpoint:
249    lldb::BreakpointSP
250    CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
251                      lldb::BreakpointResolverSP &resolver_sp,
252                      bool internal = false);
253
254    void
255    RemoveAllBreakpoints (bool internal_also = false);
256
257    void
258    DisableAllBreakpoints (bool internal_also = false);
259
260    void
261    EnableAllBreakpoints (bool internal_also = false);
262
263    bool
264    DisableBreakpointByID (lldb::break_id_t break_id);
265
266    bool
267    EnableBreakpointByID (lldb::break_id_t break_id);
268
269    bool
270    RemoveBreakpointByID (lldb::break_id_t break_id);
271
272    void
273    ModulesDidLoad (ModuleList &module_list);
274
275    void
276    ModulesDidUnload (ModuleList &module_list);
277
278protected:
279    void
280    ModuleAdded (lldb::ModuleSP &module_sp);
281
282    void
283    ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp);
284
285public:
286    //------------------------------------------------------------------
287    /// Gets the module for the main executable.
288    ///
289    /// Each process has a notion of a main executable that is the file
290    /// that will be executed or attached to. Executable files can have
291    /// dependent modules that are discovered from the object files, or
292    /// discovered at runtime as things are dynamically loaded.
293    ///
294    /// @return
295    ///     The shared pointer to the executable module which can
296    ///     contains a NULL Module object if no executable has been
297    ///     set.
298    ///
299    /// @see DynamicLoader
300    /// @see ObjectFile::GetDependentModules (FileSpecList&)
301    /// @see Process::SetExecutableModule(lldb::ModuleSP&)
302    //------------------------------------------------------------------
303    lldb::ModuleSP
304    GetExecutableModule ();
305
306    //------------------------------------------------------------------
307    /// Set the main executable module.
308    ///
309    /// Each process has a notion of a main executable that is the file
310    /// that will be executed or attached to. Executable files can have
311    /// dependent modules that are discovered from the object files, or
312    /// discovered at runtime as things are dynamically loaded.
313    ///
314    /// Setting the executable causes any of the current dependant
315    /// image information to be cleared and replaced with the static
316    /// dependent image information found by calling
317    /// ObjectFile::GetDependentModules (FileSpecList&) on the main
318    /// executable and any modules on which it depends. Calling
319    /// Process::GetImages() will return the newly found images that
320    /// were obtained from all of the object files.
321    ///
322    /// @param[in] module_sp
323    ///     A shared pointer reference to the module that will become
324    ///     the main executable for this process.
325    ///
326    /// @param[in] get_dependent_files
327    ///     If \b true then ask the object files to track down any
328    ///     known dependent files.
329    ///
330    /// @see ObjectFile::GetDependentModules (FileSpecList&)
331    /// @see Process::GetImages()
332    //------------------------------------------------------------------
333    void
334    SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
335
336    //------------------------------------------------------------------
337    /// Get accessor for the images for this process.
338    ///
339    /// Each process has a notion of a main executable that is the file
340    /// that will be executed or attached to. Executable files can have
341    /// dependent modules that are discovered from the object files, or
342    /// discovered at runtime as things are dynamically loaded. After
343    /// a main executable has been set, the images will contain a list
344    /// of all the files that the executable depends upon as far as the
345    /// object files know. These images will usually contain valid file
346    /// virtual addresses only. When the process is launched or attached
347    /// to, the DynamicLoader plug-in will discover where these images
348    /// were loaded in memory and will resolve the load virtual
349    /// addresses is each image, and also in images that are loaded by
350    /// code.
351    ///
352    /// @return
353    ///     A list of Module objects in a module list.
354    //------------------------------------------------------------------
355    ModuleList&
356    GetImages ();
357
358    ArchSpec
359    GetArchitecture () const;
360
361    //------------------------------------------------------------------
362    /// Set the architecture for this target.
363    ///
364    /// If the current target has no Images read in, then this just sets the architecture, which will
365    /// be used to select the architecture of the ExecutableModule when that is set.
366    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
367    /// architecture from the currently selected one will reset the ExecutableModule to that slice
368    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
369    /// contain a fork of this architecture, then this code will return false, and the architecture
370    /// won't be changed.
371    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
372    ///
373    /// @param[in] arch_spec
374    ///     The new architecture.
375    ///
376    /// @return
377    ///     \b true if the architecture was successfully set, \bfalse otherwise.
378    //------------------------------------------------------------------
379    bool
380    SetArchitecture (const ArchSpec &arch_spec);
381
382    Debugger &
383    GetDebugger ()
384    {
385        return m_debugger;
386    }
387
388    bool
389    GetTargetTriple (ConstString &target_triple);
390
391    size_t
392    ReadMemory (const Address& addr,
393                void *dst,
394                size_t dst_len,
395                Error &error);
396
397
398
399    SectionLoadList&
400    GetSectionLoadList()
401    {
402        return m_section_load_list;
403    }
404
405    const SectionLoadList&
406    GetSectionLoadList() const
407    {
408        return m_section_load_list;
409    }
410
411
412    static Target *
413    GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr,
414                           const SymbolContext *sc_ptr);
415
416    //------------------------------------------------------------------
417    // lldb::ExecutionContextScope pure virtual functions
418    //------------------------------------------------------------------
419    virtual Target *
420    CalculateTarget ();
421
422    virtual Process *
423    CalculateProcess ();
424
425    virtual Thread *
426    CalculateThread ();
427
428    virtual StackFrame *
429    CalculateStackFrame ();
430
431    virtual void
432    CalculateExecutionContext (ExecutionContext &exe_ctx);
433
434    PathMappingList &
435    GetImageSearchPathList ();
436
437    ClangASTContext *
438    GetScratchClangASTContext();
439
440protected:
441    friend class lldb::SBTarget;
442
443    //------------------------------------------------------------------
444    // Member variables.
445    //------------------------------------------------------------------
446    Debugger &      m_debugger;
447    ArchSpec        m_arch_spec;
448    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
449    SectionLoadList m_section_load_list;
450    BreakpointList  m_breakpoint_list;
451    BreakpointList  m_internal_breakpoint_list;
452    lldb::BreakpointSP m_last_created_breakpoint;
453    // We want to tightly control the process destruction process so
454    // we can correctly tear down everything that we need to, so the only
455    // class that knows about the process lifespan is this target class.
456    lldb::ProcessSP m_process_sp;
457    ConstString     m_triple;       ///< The target triple ("x86_64-apple-darwin10")
458    lldb::SearchFilterSP  m_search_filter_sp;
459    PathMappingList m_image_search_paths;
460    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
461    //------------------------------------------------------------------
462    // Methods.
463    //------------------------------------------------------------------
464    lldb::SearchFilterSP
465    GetSearchFilterForModule (const FileSpec *containingModule);
466
467    static void
468    ImageSearchPathsChanged (const PathMappingList &path_list,
469                             void *baton);
470
471private:
472    DISALLOW_COPY_AND_ASSIGN (Target);
473};
474
475} // namespace lldb_private
476
477#endif  // liblldb_Target_h_
478