Target.h revision 395fc33dc4b06c048ed35047ec461bc092ef2df3
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 lldb_private::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        return m_images;
371    }
372
373    const ModuleList&
374    GetImages () const
375    {
376        return m_images;
377    }
378
379    ArchSpec &
380    GetArchitecture ()
381    {
382        return m_arch_spec;
383    }
384
385    const ArchSpec &
386    GetArchitecture () const
387    {
388        return m_arch_spec;
389    }
390
391    //------------------------------------------------------------------
392    /// Set the architecture for this target.
393    ///
394    /// If the current target has no Images read in, then this just sets the architecture, which will
395    /// be used to select the architecture of the ExecutableModule when that is set.
396    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
397    /// architecture from the currently selected one will reset the ExecutableModule to that slice
398    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
399    /// contain a fork of this architecture, then this code will return false, and the architecture
400    /// won't be changed.
401    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
402    ///
403    /// @param[in] arch_spec
404    ///     The new architecture.
405    ///
406    /// @return
407    ///     \b true if the architecture was successfully set, \bfalse otherwise.
408    //------------------------------------------------------------------
409    bool
410    SetArchitecture (const ArchSpec &arch_spec);
411
412    Debugger &
413    GetDebugger ()
414    {
415        return m_debugger;
416    }
417
418    size_t
419    ReadMemoryFromFileCache (const Address& addr,
420                             void *dst,
421                             size_t dst_len,
422                             Error &error);
423
424    // Reading memory through the target allows us to skip going to the process
425    // for reading memory if possible and it allows us to try and read from
426    // any constant sections in our object files on disk. If you always want
427    // live program memory, read straight from the process. If you possibly
428    // want to read from const sections in object files, read from the target.
429    // This version of ReadMemory will try and read memory from the process
430    // if the process is alive. The order is:
431    // 1 - if (prefer_file_cache == true) then read from object file cache
432    // 2 - if there is a valid process, try and read from its memory
433    // 3 - if (prefer_file_cache == false) then read from object file cache
434    size_t
435    ReadMemory (const Address& addr,
436                bool prefer_file_cache,
437                void *dst,
438                size_t dst_len,
439                Error &error);
440
441    SectionLoadList&
442    GetSectionLoadList()
443    {
444        return m_section_load_list;
445    }
446
447    const SectionLoadList&
448    GetSectionLoadList() const
449    {
450        return m_section_load_list;
451    }
452
453
454    static Target *
455    GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr,
456                           const SymbolContext *sc_ptr);
457
458    //------------------------------------------------------------------
459    // lldb::ExecutionContextScope pure virtual functions
460    //------------------------------------------------------------------
461    virtual Target *
462    CalculateTarget ();
463
464    virtual Process *
465    CalculateProcess ();
466
467    virtual Thread *
468    CalculateThread ();
469
470    virtual StackFrame *
471    CalculateStackFrame ();
472
473    virtual void
474    CalculateExecutionContext (ExecutionContext &exe_ctx);
475
476    PathMappingList &
477    GetImageSearchPathList ();
478
479    ClangASTContext *
480    GetScratchClangASTContext();
481
482    const char *
483    GetExpressionPrefixContentsAsCString ();
484
485    // Since expressions results can persist beyond the lifetime of a process,
486    // and the const expression results are available after a process is gone,
487    // we provide a way for expressions to be evaluated from the Target itself.
488    // If an expression is going to be run, then it should have a frame filled
489    // in in th execution context.
490    lldb::ExecutionResults
491    EvaluateExpression (const char *expression,
492                        StackFrame *frame,
493                        bool unwind_on_error,
494                        bool keep_in_memory,
495                        lldb::ValueObjectSP &result_valobj_sp);
496
497    ClangPersistentVariables &
498    GetPersistentVariables()
499    {
500        return m_persistent_variables;
501    }
502
503
504
505protected:
506    friend class lldb::SBTarget;
507
508    //------------------------------------------------------------------
509    // Member variables.
510    //------------------------------------------------------------------
511    Debugger &      m_debugger;
512    Mutex           m_mutex;            ///< An API mutex that is used by the lldb::SB* classes make the SB interface thread safe
513    ArchSpec        m_arch_spec;
514    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
515    SectionLoadList m_section_load_list;
516    BreakpointList  m_breakpoint_list;
517    BreakpointList  m_internal_breakpoint_list;
518    lldb::BreakpointSP m_last_created_breakpoint;
519    // We want to tightly control the process destruction process so
520    // we can correctly tear down everything that we need to, so the only
521    // class that knows about the process lifespan is this target class.
522    lldb::ProcessSP m_process_sp;
523    lldb::SearchFilterSP  m_search_filter_sp;
524    PathMappingList m_image_search_paths;
525    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
526    ClangPersistentVariables m_persistent_variables;      ///< These are the persistent variables associated with this process for the expression parser.
527
528    //------------------------------------------------------------------
529    // Methods.
530    //------------------------------------------------------------------
531    lldb::SearchFilterSP
532    GetSearchFilterForModule (const FileSpec *containingModule);
533
534    static void
535    ImageSearchPathsChanged (const PathMappingList &path_list,
536                             void *baton);
537
538private:
539    DISALLOW_COPY_AND_ASSIGN (Target);
540};
541
542} // namespace lldb_private
543
544#endif  // liblldb_Target_h_
545