Target.h revision eea264007bc5fb42c8f3239726a9d28ae42e1b7b
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/Symbol/SymbolContext.h"
25#include "lldb/Target/ABI.h"
26#include "lldb/Target/ExecutionContextScope.h"
27#include "lldb/Target/PathMappingList.h"
28#include "lldb/Target/SectionLoadList.h"
29
30#include "lldb/API/SBTarget.h"
31
32namespace lldb_private {
33
34class Target :
35    public Broadcaster,
36    public ExecutionContextScope
37{
38public:
39    friend class TargetList;
40
41    //------------------------------------------------------------------
42    /// Broadcaster event bits definitions.
43    //------------------------------------------------------------------
44    enum
45    {
46        eBroadcastBitBreakpointChanged  = (1 << 0),
47        eBroadcastBitModulesLoaded      = (1 << 1),
48        eBroadcastBitModulesUnloaded    = (1 << 2)
49    };
50
51    lldb::ModuleSP
52    GetSharedModule (const FileSpec& file_spec,
53                     const ArchSpec& arch,
54                     const UUID *uuid = NULL,
55                     const ConstString *object_name = NULL,
56                     off_t object_offset = 0,
57                     Error *error_ptr = NULL);
58private:
59    //------------------------------------------------------------------
60    /// Construct with optional file and arch.
61    ///
62    /// This member is private. Clients must use
63    /// TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
64    /// so all targets can be tracked from the central target list.
65    ///
66    /// @see TargetList::CreateTarget(const FileSpec*, const ArchSpec*)
67    //------------------------------------------------------------------
68    Target(Debugger &debugger);
69
70public:
71    ~Target();
72
73    void
74    DeleteCurrentProcess ();
75
76    //------------------------------------------------------------------
77    /// Dump a description of this object to a Stream.
78    ///
79    /// Dump a description of the contents of this object to the
80    /// supplied stream \a s. The dumped content will be only what has
81    /// been loaded or parsed up to this point at which this function
82    /// is called, so this is a good way to see what has been parsed
83    /// in a target.
84    ///
85    /// @param[in] s
86    ///     The stream to which to dump the object descripton.
87    //------------------------------------------------------------------
88    void
89    Dump (Stream *s);
90
91    const lldb::ProcessSP &
92    CreateProcess (Listener &listener, const char *plugin_name = NULL);
93
94    const lldb::ProcessSP &
95    GetProcessSP () const;
96
97    lldb::TargetSP
98    GetSP();
99
100
101    //------------------------------------------------------------------
102    // This part handles the breakpoints.
103    //------------------------------------------------------------------
104
105    BreakpointList &
106    GetBreakpointList(bool internal = false);
107
108    const BreakpointList &
109    GetBreakpointList(bool internal = false) const;
110
111    lldb::BreakpointSP
112    GetBreakpointByID (lldb::break_id_t break_id);
113
114    // Use this to create a file and line breakpoint to a given module or all module it is NULL
115    lldb::BreakpointSP
116    CreateBreakpoint (const FileSpec *containingModule,
117                      const FileSpec &file,
118                      uint32_t line_no,
119                      bool check_inlines,
120                      bool internal = false);
121
122    // Use this to create a breakpoint from a load address
123    lldb::BreakpointSP
124    CreateBreakpoint (lldb::addr_t load_addr,
125                      bool internal = false);
126
127    // Use this to create Address breakpoints:
128    lldb::BreakpointSP
129    CreateBreakpoint (Address &addr,
130                      bool internal = false);
131
132    // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL
133    lldb::BreakpointSP
134    CreateBreakpoint (FileSpec *containingModule,
135                      RegularExpression &func_regexp,
136                      bool internal = false);
137
138    // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL
139    lldb::BreakpointSP
140    CreateBreakpoint (FileSpec *containingModule,
141                      const char *func_name,
142                      uint32_t func_name_type_mask,
143                      bool internal = false);
144
145    // Use this to create a general breakpoint:
146    lldb::BreakpointSP
147    CreateBreakpoint (lldb::SearchFilterSP &filter_sp,
148                      lldb::BreakpointResolverSP &resolver_sp,
149                      bool internal = false);
150
151    void
152    RemoveAllBreakpoints (bool internal_also = false);
153
154    void
155    DisableAllBreakpoints (bool internal_also = false);
156
157    void
158    EnableAllBreakpoints (bool internal_also = false);
159
160    bool
161    DisableBreakpointByID (lldb::break_id_t break_id);
162
163    bool
164    EnableBreakpointByID (lldb::break_id_t break_id);
165
166    bool
167    RemoveBreakpointByID (lldb::break_id_t break_id);
168
169    void
170    ModulesDidLoad (ModuleList &module_list);
171
172    void
173    ModulesDidUnload (ModuleList &module_list);
174
175protected:
176    void
177    ModuleAdded (lldb::ModuleSP &module_sp);
178
179    void
180    ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp);
181
182public:
183    //------------------------------------------------------------------
184    /// Gets the module for the main executable.
185    ///
186    /// Each process has a notion of a main executable that is the file
187    /// that will be executed or attached to. Executable files can have
188    /// dependent modules that are discovered from the object files, or
189    /// discovered at runtime as things are dynamically loaded.
190    ///
191    /// @return
192    ///     The shared pointer to the executable module which can
193    ///     contains a NULL Module object if no executable has been
194    ///     set.
195    ///
196    /// @see DynamicLoader
197    /// @see ObjectFile::GetDependentModules (FileSpecList&)
198    /// @see Process::SetExecutableModule(lldb::ModuleSP&)
199    //------------------------------------------------------------------
200    lldb::ModuleSP
201    GetExecutableModule ();
202
203    //------------------------------------------------------------------
204    /// Set the main executable module.
205    ///
206    /// Each process has a notion of a main executable that is the file
207    /// that will be executed or attached to. Executable files can have
208    /// dependent modules that are discovered from the object files, or
209    /// discovered at runtime as things are dynamically loaded.
210    ///
211    /// Setting the executable causes any of the current dependant
212    /// image information to be cleared and replaced with the static
213    /// dependent image information found by calling
214    /// ObjectFile::GetDependentModules (FileSpecList&) on the main
215    /// executable and any modules on which it depends. Calling
216    /// Process::GetImages() will return the newly found images that
217    /// were obtained from all of the object files.
218    ///
219    /// @param[in] module_sp
220    ///     A shared pointer reference to the module that will become
221    ///     the main executable for this process.
222    ///
223    /// @param[in] get_dependent_files
224    ///     If \b true then ask the object files to track down any
225    ///     known dependent files.
226    ///
227    /// @see ObjectFile::GetDependentModules (FileSpecList&)
228    /// @see Process::GetImages()
229    //------------------------------------------------------------------
230    void
231    SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files);
232
233    //------------------------------------------------------------------
234    /// Get accessor for the images for this process.
235    ///
236    /// Each process has a notion of a main executable that is the file
237    /// that will be executed or attached to. Executable files can have
238    /// dependent modules that are discovered from the object files, or
239    /// discovered at runtime as things are dynamically loaded. After
240    /// a main executable has been set, the images will contain a list
241    /// of all the files that the executable depends upon as far as the
242    /// object files know. These images will usually contain valid file
243    /// virtual addresses only. When the process is launched or attached
244    /// to, the DynamicLoader plug-in will discover where these images
245    /// were loaded in memory and will resolve the load virtual
246    /// addresses is each image, and also in images that are loaded by
247    /// code.
248    ///
249    /// @return
250    ///     A list of Module objects in a module list.
251    //------------------------------------------------------------------
252    ModuleList&
253    GetImages ();
254
255    ArchSpec
256    GetArchitecture () const;
257
258    //------------------------------------------------------------------
259    /// Set the architecture for this target.
260    ///
261    /// If the current target has no Images read in, then this just sets the architecture, which will
262    /// be used to select the architecture of the ExecutableModule when that is set.
263    /// If the current target has an ExecutableModule, then calling SetArchitecture with a different
264    /// architecture from the currently selected one will reset the ExecutableModule to that slice
265    /// of the file backing the ExecutableModule.  If the file backing the ExecutableModule does not
266    /// contain a fork of this architecture, then this code will return false, and the architecture
267    /// won't be changed.
268    /// If the input arch_spec is the same as the already set architecture, this is a no-op.
269    ///
270    /// @param[in] arch_spec
271    ///     The new architecture.
272    ///
273    /// @return
274    ///     \b true if the architecture was successfully set, \bfalse otherwise.
275    //------------------------------------------------------------------
276    bool
277    SetArchitecture (const ArchSpec &arch_spec);
278
279    Debugger &
280    GetDebugger ()
281    {
282        return m_debugger;
283    }
284
285    bool
286    GetTargetTriple (ConstString &target_triple);
287
288    size_t
289    ReadMemory (const Address& addr,
290                void *dst,
291                size_t dst_len,
292                Error &error);
293
294
295
296    SectionLoadList&
297    GetSectionLoadList()
298    {
299        return m_section_load_list;
300    }
301
302    const SectionLoadList&
303    GetSectionLoadList() const
304    {
305        return m_section_load_list;
306    }
307
308    //------------------------------------------------------------------
309    // lldb::ExecutionContextScope pure virtual functions
310    //------------------------------------------------------------------
311    virtual Target *
312    CalculateTarget ();
313
314    virtual Process *
315    CalculateProcess ();
316
317    virtual Thread *
318    CalculateThread ();
319
320    virtual StackFrame *
321    CalculateStackFrame ();
322
323    virtual void
324    Calculate (ExecutionContext &exe_ctx);
325
326    PathMappingList &
327    GetImageSearchPathList ();
328
329    ClangASTContext *
330    GetScratchClangASTContext();
331
332protected:
333    friend class lldb::SBTarget;
334
335    //------------------------------------------------------------------
336    // Member variables.
337    //------------------------------------------------------------------
338    Debugger &      m_debugger;
339    ArchSpec        m_arch_spec;
340    ModuleList      m_images;           ///< The list of images for this process (shared libraries and anything dynamically loaded).
341    SectionLoadList m_section_load_list;
342    BreakpointList  m_breakpoint_list;
343    BreakpointList  m_internal_breakpoint_list;
344    // We want to tightly control the process destruction process so
345    // we can correctly tear down everything that we need to, so the only
346    // class that knows about the process lifespan is this target class.
347    lldb::ProcessSP m_process_sp;
348    ConstString     m_triple;       ///< The target triple ("x86_64-apple-darwin10")
349    lldb::SearchFilterSP  m_search_filter_sp;
350    PathMappingList m_image_search_paths;
351    std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap;
352    //------------------------------------------------------------------
353    // Methods.
354    //------------------------------------------------------------------
355    lldb::SearchFilterSP
356    GetSearchFilterForModule (const FileSpec *containingModule);
357
358    static void
359    ImageSearchPathsChanged (const PathMappingList &path_list,
360                             void *baton);
361
362private:
363    DISALLOW_COPY_AND_ASSIGN (Target);
364};
365
366} // namespace lldb_private
367
368#endif  // liblldb_Target_h_
369