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