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