]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Core/Address.h
Import LLDB as of upstream SVN 241361 (git 612c075f)
[FreeBSD/FreeBSD.git] / include / lldb / Core / Address.h
1 //===-- Address.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_Address_h_
11 #define liblldb_Address_h_
12
13 // C Includes
14 // C++ Includes
15 #include <atomic>
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/lldb-private.h"
19 #include "lldb/Symbol/SymbolContextScope.h"
20
21 namespace lldb_private {
22
23 //----------------------------------------------------------------------
24 /// @class Address Address.h "lldb/Core/Address.h"
25 /// @brief A section + offset based address class.
26 ///
27 /// The Address class allows addresses to be relative to a section
28 /// that can move during runtime due to images (executables, shared
29 /// libraries, bundles, frameworks) being loaded at different
30 /// addresses than the addresses found in the object file that
31 /// represents them on disk. There are currently two types of addresses
32 /// for a section:
33 ///     @li file addresses
34 ///     @li load addresses
35 ///
36 /// File addresses represent the virtual addresses that are in the "on
37 /// disk" object files. These virtual addresses are converted to be
38 /// relative to unique sections scoped to the object file so that
39 /// when/if the addresses slide when the images are loaded/unloaded
40 /// in memory, we can easily track these changes without having to
41 /// update every object (compile unit ranges, line tables, function
42 /// address ranges, lexical block and inlined subroutine address
43 /// ranges, global and static variables) each time an image is loaded or
44 /// unloaded.
45 ///
46 /// Load addresses represent the virtual addresses where each section
47 /// ends up getting loaded at runtime. Before executing a program, it
48 /// is common for all of the load addresses to be unresolved. When a
49 /// DynamicLoader plug-in receives notification that shared libraries
50 /// have been loaded/unloaded, the load addresses of the main executable
51 /// and any images (shared libraries) will be  resolved/unresolved. When
52 /// this happens, breakpoints that are in one of these sections can be
53 /// set/cleared.
54 //----------------------------------------------------------------------
55 class Address
56 {
57 public:
58     //------------------------------------------------------------------
59     /// Dump styles allow the Address::Dump(Stream *,DumpStyle) const
60     /// function to display Address contents in a variety of ways.
61     //------------------------------------------------------------------
62     typedef enum {
63         DumpStyleInvalid,               ///< Invalid dump style
64         DumpStyleSectionNameOffset,     ///< Display as the section name + offset.
65                                         ///< \code
66                                         /// // address for printf in libSystem.B.dylib as a section name + offset
67                                         /// libSystem.B.dylib.__TEXT.__text + 0x0005cfdf
68                                         /// \endcode
69         DumpStyleSectionPointerOffset,  ///< Display as the section pointer + offset (debug output).
70                                         ///< \code
71                                         /// // address for printf in libSystem.B.dylib as a section pointer + offset
72                                         /// (lldb::Section *)0x35cc50 + 0x000000000005cfdf \endcode
73         DumpStyleFileAddress,           ///< Display as the file address (if any).
74                                         ///< \code
75                                         /// // address for printf in libSystem.B.dylib as a file address
76                                         /// 0x000000000005dcff \endcode
77         DumpStyleModuleWithFileAddress, ///< Display as the file address with the module name prepended (if any).
78                                         ///< \code
79                                         /// // address for printf in libSystem.B.dylib as a file address
80                                         /// libSystem.B.dylib[0x000000000005dcff] \endcode
81         DumpStyleLoadAddress,           ///< Display as the load address (if resolved).
82                                         ///< \code
83                                         /// // address for printf in libSystem.B.dylib as a load address
84                                         /// 0x00007fff8306bcff \endcode
85         DumpStyleResolvedDescription,   ///< Display the details about what an address resolves to. This can
86                                         ///< be anything from a symbol context summary (module, function/symbol, 
87                                         ///< and file and line), to information about what the pointer points to
88                                         ///< if the address is in a section (section of pointers, c strings, etc).
89         DumpStyleResolvedDescriptionNoModule,
90         DumpStyleResolvedDescriptionNoFunctionArguments,
91         DumpStyleNoFunctionName,        ///< Elide the function name; display an offset into the current function.
92                                         ///< Used primarily in disassembly symbolication
93         DumpStyleDetailedSymbolContext, ///< Detailed symbol context information for an address for all symbol
94                                         ///< context members.
95         DumpStyleResolvedPointerDescription ///< Dereference a pointer at the current address and then lookup the
96                                              ///< dereferenced address using DumpStyleResolvedDescription
97     } DumpStyle;
98
99     //------------------------------------------------------------------
100     /// Default constructor.
101     ///
102     /// Initialize with a invalid section (NULL) and an invalid
103     /// offset (LLDB_INVALID_ADDRESS).
104     //------------------------------------------------------------------
105     Address () :
106         m_section_wp (),
107         m_offset (LLDB_INVALID_ADDRESS)
108     {
109     }
110
111
112     //------------------------------------------------------------------
113     /// Copy constructor
114     ///
115     /// Makes a copy of the another Address object \a rhs.
116     ///
117     /// @param[in] rhs
118     ///     A const Address object reference to copy.
119     //------------------------------------------------------------------
120     Address (const Address& rhs) :
121         m_section_wp (rhs.m_section_wp),
122         m_offset(rhs.m_offset.load())
123     {
124     }
125
126     //------------------------------------------------------------------
127     /// Construct with a section pointer and offset.
128     ///
129     /// Initialize the address with the supplied \a section and \a
130     /// offset.
131     ///
132     /// @param[in] section
133     ///     A section pointer to a valid lldb::Section, or NULL if the
134     ///     address doesn't have a section or will get resolved later.
135     ///
136     /// @param[in] offset
137     ///     The offset in bytes into \a section.
138     //------------------------------------------------------------------
139     Address (const lldb::SectionSP &section_sp, lldb::addr_t offset) :
140         m_section_wp (), // Don't init with section_sp in case section_sp is invalid (the weak_ptr will throw)
141         m_offset (offset)
142     {
143         if (section_sp)
144             m_section_wp = section_sp;
145     }
146
147     //------------------------------------------------------------------
148     /// Construct with a virtual address and section list.
149     ///
150     /// Initialize and resolve the address with the supplied virtual
151     /// address \a file_addr.
152     ///
153     /// @param[in] file_addr
154     ///     A virtual file address.
155     ///
156     /// @param[in] section_list
157     ///     A list of sections, one of which may contain the \a file_addr.
158     //------------------------------------------------------------------
159     Address (lldb::addr_t file_addr, const SectionList * section_list);
160
161     Address (lldb::addr_t abs_addr);
162
163     //------------------------------------------------------------------
164     /// Assignment operator.
165     ///
166     /// Copies the address value from another Address object \a rhs
167     /// into \a this object.
168     ///
169     /// @param[in] rhs
170     ///     A const Address object reference to copy.
171     ///
172     /// @return
173     ///     A const Address object reference to \a this.
174     //------------------------------------------------------------------
175 #ifndef SWIG
176     const Address&
177     operator= (const Address& rhs);
178 #endif
179     //------------------------------------------------------------------
180     /// Clear the object's state.
181     ///
182     /// Sets the section to an invalid value (NULL) and an invalid
183     /// offset (LLDB_INVALID_ADDRESS).
184     //------------------------------------------------------------------
185     void
186     Clear ()
187     {
188         m_section_wp.reset();
189         m_offset = LLDB_INVALID_ADDRESS;
190     }
191
192     //------------------------------------------------------------------
193     /// Compare two Address objects.
194     ///
195     /// @param[in] lhs
196     ///     The Left Hand Side const Address object reference.
197     ///
198     /// @param[in] rhs
199     ///     The Right Hand Side const Address object reference.
200     ///
201     /// @return
202     ///     @li -1 if lhs < rhs
203     ///     @li 0 if lhs == rhs
204     ///     @li 1 if lhs > rhs
205     //------------------------------------------------------------------
206     static int
207     CompareFileAddress (const Address& lhs, const Address& rhs);
208
209     static int
210     CompareLoadAddress (const Address& lhs, const Address& rhs, Target *target);
211
212     static int
213     CompareModulePointerAndOffset (const Address& lhs, const Address& rhs);
214
215     // For use with std::map, std::multi_map
216     class ModulePointerAndOffsetLessThanFunctionObject
217     {
218     public:
219         ModulePointerAndOffsetLessThanFunctionObject () {}
220
221         bool
222         operator() (const Address& a, const Address& b) const
223         {
224             return Address::CompareModulePointerAndOffset(a, b) < 0;
225         }
226     };
227
228     //------------------------------------------------------------------
229     /// Dump a description of this object to a Stream.
230     ///
231     /// Dump a description of the contents of this object to the
232     /// supplied stream \a s. There are many ways to display a section
233     /// offset based address, and \a style lets the user choose.
234     ///
235     /// @param[in] s
236     ///     The stream to which to dump the object description.
237     ///
238     /// @param[in] style
239     ///     The display style for the address.
240     ///
241     /// @param[in] fallback_style
242     ///     The display style for the address.
243     ///
244     /// @return
245     ///     Returns \b true if the address was able to be displayed.
246     ///     File and load addresses may be unresolved and it may not be
247     ///     possible to display a valid value, \b false will be returned
248     ///     in such cases.
249     ///
250     /// @see Address::DumpStyle
251     //------------------------------------------------------------------
252     bool
253     Dump (Stream *s,
254           ExecutionContextScope *exe_scope,
255           DumpStyle style,
256           DumpStyle fallback_style = DumpStyleInvalid,
257           uint32_t addr_byte_size = UINT32_MAX) const;
258
259     lldb::AddressClass
260     GetAddressClass () const;
261     
262     //------------------------------------------------------------------
263     /// Get the file address.
264     ///
265     /// If an address comes from a file on disk that has section
266     /// relative addresses, then it has a virtual address that is
267     /// relative to unique section in the object file.
268     ///
269     /// @return
270     ///     The valid file virtual address, or LLDB_INVALID_ADDRESS if
271     ///     the address doesn't have a file virtual address (image is
272     ///     from memory only with no representation on disk).
273     //------------------------------------------------------------------
274     lldb::addr_t
275     GetFileAddress () const;
276
277     //------------------------------------------------------------------
278     /// Get the load address.
279     ///
280     /// If an address comes from a file on disk that has section
281     /// relative addresses, then it has a virtual address that is
282     /// relative to unique section in the object file. Sections get
283     /// resolved at runtime by DynamicLoader plug-ins as images
284     /// (executables and shared libraries) get loaded/unloaded. If a
285     /// section is loaded, then the load address can be resolved.
286     ///
287     /// @return
288     ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
289     ///     the address is currently not loaded.
290     //------------------------------------------------------------------
291     lldb::addr_t
292     GetLoadAddress (Target *target) const;
293     
294     //------------------------------------------------------------------
295     /// Get the load address as a callable code load address.
296     ///
297     /// This function will first resolve its address to a load address.
298     /// Then, if the address turns out to be in code address, return the
299     /// load address that would be required to call or return to. The
300     /// address might have extra bits set (bit zero will be set to Thumb
301     /// functions for an ARM target) that are required when changing the
302     /// program counter to setting a return address.
303     ///
304     /// @return
305     ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
306     ///     the address is currently not loaded.
307     //------------------------------------------------------------------
308     lldb::addr_t
309     GetCallableLoadAddress (Target *target, bool is_indirect = false) const;
310
311     //------------------------------------------------------------------
312     /// Get the load address as an opcode load address.
313     ///
314     /// This function will first resolve its address to a load address.
315     /// Then, if the address turns out to be in code address, return the
316     /// load address for an opcode. This address object might have 
317     /// extra bits set (bit zero will be set to Thumb functions for an
318     /// ARM target) that are required for changing the program counter
319     /// and this function will remove any bits that are intended for
320     /// these special purposes. The result of this function can be used
321     /// to safely write a software breakpoint trap to memory.
322     ///
323     /// @return
324     ///     The valid load virtual address with extra callable bits 
325     ///     removed, or LLDB_INVALID_ADDRESS if the address is currently
326     ///     not loaded.
327     //------------------------------------------------------------------
328     lldb::addr_t
329     GetOpcodeLoadAddress (Target *target) const;
330
331     //------------------------------------------------------------------
332     /// Get the section relative offset value.
333     ///
334     /// @return
335     ///     The current offset, or LLDB_INVALID_ADDRESS if this address
336     ///     doesn't contain a valid offset.
337     //------------------------------------------------------------------
338     lldb::addr_t
339     GetOffset () const { return m_offset; }
340
341     //------------------------------------------------------------------
342     /// Check if an address is section offset.
343     ///
344     /// When converting a virtual file or load address into a section
345     /// offset based address, we often need to know if, given a section
346     /// list, if the address was able to be converted to section offset.
347     /// This function returns true if the current value contained in
348     /// this object is section offset based.
349     ///
350     /// @return
351     ///     Returns \b true if the address has a valid section and
352     ///     offset, \b false otherwise.
353     //------------------------------------------------------------------
354     bool
355     IsSectionOffset() const
356     {
357         return IsValid() && (GetSection().get() != NULL);
358     }
359
360     //------------------------------------------------------------------
361     /// Check if the object state is valid.
362     ///
363     /// A valid Address object contains either a section pointer and
364     /// and offset (for section offset based addresses), or just a valid
365     /// offset (for absolute addresses that have no section).
366     ///
367     /// @return
368     ///     Returns \b true if the offset is valid, \b false
369     ///     otherwise.
370     //------------------------------------------------------------------
371     bool
372     IsValid() const
373     {
374         return m_offset != LLDB_INVALID_ADDRESS;
375     }
376
377
378     //------------------------------------------------------------------
379     /// Get the memory cost of this object.
380     ///
381     /// @return
382     ///     The number of bytes that this object occupies in memory.
383     //------------------------------------------------------------------
384     size_t
385     MemorySize () const;
386
387     //------------------------------------------------------------------
388     /// Resolve a file virtual address using a section list.
389     ///
390     /// Given a list of sections, attempt to resolve \a addr as a
391     /// an offset into one of the file sections.
392     ///
393     /// @return
394     ///     Returns \b true if \a addr was able to be resolved, \b false
395     ///     otherwise.
396     //------------------------------------------------------------------
397     bool
398     ResolveAddressUsingFileSections (lldb::addr_t addr, const SectionList *sections);
399
400     //------------------------------------------------------------------
401     /// Set the address to represent \a load_addr.
402     ///
403     /// The address will attempt to find a loaded section within
404     /// \a target that contains \a load_addr. If successful, this 
405     /// address object will have a valid section and offset. Else this
406     /// address object will have no section (NULL) and the offset will
407     /// be \a load_addr.
408     ///
409     /// @param[in] load_addr
410     ///     A load address from a current process.
411     ///
412     /// @param[in] target
413     ///     The target to use when trying resolve the address into
414     ///     a section + offset. The Target's SectionLoadList object
415     ///     is used to resolve the address.
416     ///
417     /// @return
418     ///     Returns \b true if the load address was resolved to be 
419     ///     section/offset, \b false otherwise. It is often ok for an 
420     ///     address no not resolve to a section in a module, this often
421     ///     happens for JIT'ed code, or any load addresses on the stack
422     ///     or heap.
423     //------------------------------------------------------------------
424     bool
425     SetLoadAddress (lldb::addr_t load_addr, Target *target);
426     
427     bool
428     SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target);
429
430     bool
431     SetCallableLoadAddress (lldb::addr_t load_addr, Target *target);
432
433     //------------------------------------------------------------------
434     /// Get accessor for the module for this address.
435     ///
436     /// @return
437     ///     Returns the Module pointer that this address is an offset
438     ///     in, or NULL if this address doesn't belong in a module, or
439     ///     isn't resolved yet.
440     //------------------------------------------------------------------
441     lldb::ModuleSP
442     GetModule () const;
443
444     //------------------------------------------------------------------
445     /// Get const accessor for the section.
446     ///
447     /// @return
448     ///     Returns the const lldb::Section pointer that this address is an
449     ///     offset in, or NULL if this address is absolute.
450     //------------------------------------------------------------------
451     lldb::SectionSP
452     GetSection () const { return m_section_wp.lock(); }
453
454     //------------------------------------------------------------------
455     /// Set accessor for the offset.
456     ///
457     /// @param[in] offset
458     ///     A new offset value for this object.
459     ///
460     /// @return
461     ///     Returns \b true if the offset changed, \b false otherwise.
462     //------------------------------------------------------------------
463     bool
464     SetOffset (lldb::addr_t offset)
465     {
466         bool changed = m_offset != offset;
467         m_offset = offset;
468         return changed;
469     }
470     
471     void
472     SetRawAddress (lldb::addr_t addr)
473     {
474         m_section_wp.reset();
475         m_offset = addr;
476     }
477
478     bool
479     Slide (int64_t offset)
480     {
481         if (m_offset != LLDB_INVALID_ADDRESS)
482         {
483             m_offset += offset;
484             return true;
485         }
486         return false;
487     }
488
489     //------------------------------------------------------------------
490     /// Set accessor for the section.
491     ///
492     /// @param[in] section
493     ///     A new lldb::Section pointer to use as the section base. Can
494     ///     be NULL for absolute addresses that are not relative to
495     ///     any section.
496     //------------------------------------------------------------------
497     void
498     SetSection (const lldb::SectionSP &section_sp) 
499     {
500         m_section_wp = section_sp; 
501     }
502
503     void
504     ClearSection ()
505     {
506         m_section_wp.reset();
507     }
508     //------------------------------------------------------------------
509     /// Reconstruct a symbol context from an address.
510     ///
511     /// This class doesn't inherit from SymbolContextScope because many
512     /// address objects have short lifespans. Address objects that are
513     /// section offset can reconstruct their symbol context by looking
514     /// up the address in the module found in the section.
515     ///
516     /// @see SymbolContextScope::CalculateSymbolContext(SymbolContext*)
517     //------------------------------------------------------------------
518     uint32_t
519     CalculateSymbolContext (SymbolContext *sc, 
520                             uint32_t resolve_scope = lldb::eSymbolContextEverything) const;
521
522     lldb::ModuleSP
523     CalculateSymbolContextModule () const;
524     
525     CompileUnit *
526     CalculateSymbolContextCompileUnit () const;
527     
528     Function *
529     CalculateSymbolContextFunction () const;
530     
531     Block *
532     CalculateSymbolContextBlock () const;
533
534     Symbol *
535     CalculateSymbolContextSymbol () const;
536
537     bool
538     CalculateSymbolContextLineEntry (LineEntry &line_entry) const;
539
540     //------------------------------------------------------------------
541     // Returns true if the section should be valid, but isn't because
542     // the shared pointer to the section can't be reconstructed from
543     // a weak pointer that contains a valid weak reference to a section.
544     // Returns false if the section weak pointer has no reference to
545     // a section, or if the section is still valid
546     //------------------------------------------------------------------
547     bool
548     SectionWasDeleted() const;
549
550 protected:
551     //------------------------------------------------------------------
552     // Member variables.
553     //------------------------------------------------------------------
554     lldb::SectionWP m_section_wp;   ///< The section for the address, can be NULL.
555     std::atomic<lldb::addr_t> m_offset;      ///< Offset into section if \a m_section_wp is valid...
556     
557     //------------------------------------------------------------------
558     // Returns true if the m_section_wp once had a reference to a valid
559     // section shared pointer, but no longer does. This can happen if
560     // we have an address from a module that gets unloaded and deleted.
561     // This function should only be called if GetSection() returns an
562     // empty shared pointer and you want to know if this address used to
563     // have a valid section.
564     //------------------------------------------------------------------
565     bool
566     SectionWasDeletedPrivate() const;
567
568 };
569
570
571 //----------------------------------------------------------------------
572 // NOTE: Be careful using this operator. It can correctly compare two 
573 // addresses from the same Module correctly. It can't compare two 
574 // addresses from different modules in any meaningful way, but it will
575 // compare the module pointers.
576 // 
577 // To sum things up:
578 // - works great for addresses within the same module
579 // - it works for addresses across multiple modules, but don't expect the
580 //   address results to make much sense
581 //
582 // This basically lets Address objects be used in ordered collection 
583 // classes.
584 //----------------------------------------------------------------------
585 bool operator<  (const Address& lhs, const Address& rhs);
586 bool operator>  (const Address& lhs, const Address& rhs);
587
588
589
590 bool operator== (const Address& lhs, const Address& rhs);
591 bool operator!= (const Address& lhs, const Address& rhs);
592
593 } // namespace lldb_private
594
595 #endif  // liblldb_Address_h_