Target.h revision 6a92553d2cc2b7a3b853fcb6da101583435c2dc0
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    Mutex &
177    GetAPIMutex ()
178    {
179        return m_mutex;
180    }
181
182    void
183    DeleteCurrentProcess ();
184
185    //------------------------------------------------------------------
186    /// Dump a description of this object to a Stream.
187    ///
188    /// Dump a description of the contents of this object to the
189    /// supplied stream \a s. The dumped content will be only what has
190    /// been loaded or parsed up to this point at which this function
191    /// is called, so this is a good way to see what has been parsed
192    /// in a target.
193    ///
194    /// @param[in] s
195    ///     The stream to which to dump the object descripton.
196    //------------------------------------------------------------------
197    void
198    Dump (Stream *s, lldb::DescriptionLevel description_level);
199
200    const lldb::ProcessSP &
201    CreateProcess (Listener &listener, const char *plugin_name = NULL);
202
203    const lldb::ProcessSP &
204    GetProcessSP () const;
205
206    lldb::TargetSP
207    GetSP();
208
209
210    //------------------------------------------------------------------
211    // This part handles the breakpoints.
212    //------------------------------------------------------------------
213
214    BreakpointList &
215    GetBreakpointList(bool internal = false);
216
217    const BreakpointList &
218    GetBreakpointList(bool internal = false) const;
219
220    lldb::BreakpointSP
221    GetLastCreatedBreakpoint ()
222    {
223        return m_last_created_breakpoint;
224    }
225
226    lldb::BreakpointSP
227    GetBreakpointByID (lldb::break_id_t break_id);
228
229    // Use this to create a file and line breakpoint to a given module or all module it is NULL
230    lldb::BreakpointSP
231    CreateBreakpoint (const FileSpec *containingModule,
232                      const FileSpec &file,
233                      uint32_t line_no,
234                      bool check_inlines,
235                      bool internal = false);
236
237    // Use this to create a breakpoint from a load address
238    lldb::BreakpointSP
239    CreateBreakpoint (lldb::addr_t load_addr,
240                      bool internal = false);
241
242    // Use this to create Address breakpoints:
243    lldb::BreakpointSP
244    CreateBreakpoint (Address &addr,
245                      bool internal = false);
246
247    // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL
248    lldb::BreakpointSP
249    CreateBreakpoint (FileSpec *containingModule,
250                      RegularExpression &func_regexp,
251                      bool internal = false);
252
253    // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
254    lldb::BreakpointSP
255    CreateBreakpoint (FileSpec *containingModule,
256                      const char *func_name,
257                      uint32_t func_name_type_mask,
258                      bool internal = false);
259
260    // Use this to create a general breakpoint:
261    lldb::BreakpointSP
262    CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
263                      lldb::BreakpointResolverSP &resolver_sp,
264                      bool internal = false);
265
266    void
267    RemoveAllBreakpoints (bool internal_also = false);
268
269    void
270    DisableAllBreakpoints (bool internal_also = false);
271
272    void
273    EnableAllBreakpoints (bool internal_also = false);
274
275    bool
276    DisableBreakpointByID (lldb::break_id_t break_id);
277
278    bool
279    EnableBreakpointByID (lldb::break_id_t break_id);
280
281    bool
282    RemoveBreakpointByID (lldb::break_id_t break_id);
283
284    void
285    ModulesDidLoad (ModuleList &module_list);
286
287    void
288    ModulesDidUnload (ModuleList &module_list);
289
290protected:
291    void
292    ModuleAdded (lldb::ModuleSP &module_sp);
293
294    void
295    ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp);
296
297public:
298    //------------------------------------------------------------------
299    /// Gets the module for the main executable.
300    ///
301    /// Each process has a notion of a main executable that is the file
302    /// that will be executed or attached to. Executable files can have
303    /// dependent modules that are discovered from the object files, or
304    /// discovered at runtime as things are dynamically loaded.
305    ///
306    /// @return
307    ///     The shared pointer to the executable module which can
308    ///     contains a NULL Module object if no executable has been
309    ///     set.
310    ///
311    /// @see DynamicLoader
312    /// @see ObjectFile::GetDependentModules (FileSpecList&)
313    /// @see Process::SetExecutableModule(lldb::ModuleSP&)
314    //------------------------------------------------------------------
315    lldb::ModuleSP
316    GetExecutableModule ();
317
318    //------------------------------------------------------------------
319    /// Set the main executable module.
320    ///
321    /// Each process has a notion of a main executable that is the file
322    /// that will be executed or attached to. Executable files can have
323    /// dependent modules that are discovered from the object files, or
324    /// discovered at runtime as things are dynamically loaded.
325    ///
326    /// Setting the executable causes any of the current dependant
327    /// image information to be cleared and replaced with the static
328    /// dependent image information found by calling
329    /// ObjectFile::GetDependentModules (FileSpecList&) on the main
330    /// executable and any modules on which it depends. Calling
331    /// Process::GetImages() will return the newly found images that
332    /// were obtained from all of the object files.
333    ///
334    /// @param[in] module_sp
335    ///     A shared pointer reference to the module that will become
336    ///     the main executable for this process.
337    ///
338    /// @param[in] get_dependent_files
339    ///     If \b true then ask the object files to track down any
340    ///     known dependent files.
341    ///
342    /// @see ObjectFile::GetDependentModules (FileSpecList&)
343    /// @see Process::GetImages()
344    //------------------------------------------------------------------
345    void
346    SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
347
348    //------------------------------------------------------------------
349    /// Get accessor for the images for this process.
350    ///
351    /// Each process has a notion of a main executable that is the file
352    /// that will be executed or attached to. Executable files can have
353    /// dependent modules that are discovered from the object files, or
354    /// discovered at runtime as things are dynamically loaded. After
355    /// a main executable has been set, the images will contain a list
356    /// of all the files that the executable depends upon as far as the
357    /// object files know. These images will usually contain valid file
358    /// virtual addresses only. When the process is launched or attached
359    /// to, the DynamicLoader plug-in will discover where these images
360    /// were loaded in memory and will resolve the load virtual
361    /// addresses is each image, and also in images that are loaded by
362    /// code.
363    ///
364    /// @return
365    ///     A list of Module objects in a module list.
366    //------------------------------------------------------------------
367    ModuleList&
368    GetImages ();
369
370    ArchSpec
371    GetArchitecture () const;
372
373    //------------------------------------------------------------------
374    /// Set the architecture for this target.
375    ///
376    /// If the current target has no Images read in, then this just sets the architecture, which will
377    /// be used to select the architecture of the ExecutableModule when that is set.
378    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
379    /// architecture from the currently selected one will reset the ExecutableModule to that slice
380    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
381    /// contain a fork of this architecture, then this code will return false, and the architecture
382    /// won't be changed.
383    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
384    ///
385    /// @param[in] arch_spec
386    ///     The new architecture.
387    ///
388    /// @return
389    ///     \b true if the architecture was successfully set, \bfalse otherwise.
390    //------------------------------------------------------------------
391    bool
392    SetArchitecture (const ArchSpec &arch_spec);
393
394    Debugger &
395    GetDebugger ()
396    {
397        return m_debugger;
398    }
399
400    bool
401    GetTargetTriple (ConstString &target_triple);
402
403    size_t
404    ReadMemoryFromFileCache (const Address& addr,
405                             void *dst,
406                             size_t dst_len,
407                             Error &error);
408
409    // Reading memory through the target allows us to skip going to the process
410    // for reading memory if possible and it allows us to try and read from
411    // any constant sections in our object files on disk. If you always want
412    // live program memory, read straight from the process. If you possibly
413    // want to read from const sections in object files, read from the target.
414    // This version of ReadMemory will try and read memory from the process
415    // if the process is alive. The order is:
416    // 1 - if (prefer_file_cache == true) then read from object file cache
417    // 2 - if there is a valid process, try and read from its memory
418    // 3 - if (prefer_file_cache == false) then read from object file cache
419    size_t
420    ReadMemory (const Address& addr,
421                bool prefer_file_cache,
422                void *dst,
423                size_t dst_len,
424                Error &error);
425
426    SectionLoadList&
427    GetSectionLoadList()
428    {
429        return m_section_load_list;
430    }
431
432    const SectionLoadList&
433    GetSectionLoadList() const
434    {
435        return m_section_load_list;
436    }
437
438
439    static Target *
440    GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr,
441                           const SymbolContext *sc_ptr);
442
443    //------------------------------------------------------------------
444    // lldb::ExecutionContextScope pure virtual functions
445    //------------------------------------------------------------------
446    virtual Target *
447    CalculateTarget ();
448
449    virtual Process *
450    CalculateProcess ();
451
452    virtual Thread *
453    CalculateThread ();
454
455    virtual StackFrame *
456    CalculateStackFrame ();
457
458    virtual void
459    CalculateExecutionContext (ExecutionContext &exe_ctx);
460
461    PathMappingList &
462    GetImageSearchPathList ();
463
464    ClangASTContext *
465    GetScratchClangASTContext();
466
467    const char *
468    GetExpressionPrefixContentsAsCString ();
469
470    // Since expressions results can persist beyond the lifetime of a process,
471    // and the const expression results are available after a process is gone,
472    // we provide a way for expressions to be evaluated from the Target itself.
473    // If an expression is going to be run, then it should have a frame filled
474    // in in th execution context.
475    lldb::ExecutionResults
476    EvaluateExpression (const char *expression,
477                        StackFrame *frame,
478                        bool unwind_on_error,
479                        bool keep_in_memory,
480                        lldb::ValueObjectSP &result_valobj_sp);
481
482    ClangPersistentVariables &
483    GetPersistentVariables()
484    {
485        return m_persistent_variables;
486    }
487
488
489
490protected:
491    friend class lldb::SBTarget;
492
493    //------------------------------------------------------------------
494    // Member variables.
495    //------------------------------------------------------------------
496    Debugger &      m_debugger;
497    Mutex           m_mutex;            ///< An API mutex that is used by the lldb::SB* classes make the SB interface thread safe
498    ArchSpec        m_arch_spec;
499    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
500    SectionLoadList m_section_load_list;
501    BreakpointList  m_breakpoint_list;
502    BreakpointList  m_internal_breakpoint_list;
503    lldb::BreakpointSP m_last_created_breakpoint;
504    // We want to tightly control the process destruction process so
505    // we can correctly tear down everything that we need to, so the only
506    // class that knows about the process lifespan is this target class.
507    lldb::ProcessSP m_process_sp;
508    ConstString     m_triple;       ///< The target triple ("x86_64-apple-darwin10")
509    lldb::SearchFilterSP  m_search_filter_sp;
510    PathMappingList m_image_search_paths;
511    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
512    ClangPersistentVariables m_persistent_variables;      ///< These are the persistent variables associated with this process for the expression parser.
513
514    //------------------------------------------------------------------
515    // Methods.
516    //------------------------------------------------------------------
517    lldb::SearchFilterSP
518    GetSearchFilterForModule (const FileSpec *containingModule);
519
520    static void
521    ImageSearchPathsChanged (const PathMappingList &path_list,
522                             void *baton);
523
524private:
525    DISALLOW_COPY_AND_ASSIGN (Target);
526};
527
528} // namespace lldb_private
529
530#endif  // liblldb_Target_h_
531