]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/Address.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / Address.cpp
1 //===-- Address.cpp ---------------------------------------------*- 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 #include "lldb/Core/Address.h"
11 #include "lldb/Core/DumpDataExtractor.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h" // for ModuleList
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Symbol/Declaration.h" // for Declaration
17 #include "lldb/Symbol/LineEntry.h"   // for LineEntry
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/Symbol.h"        // for Symbol
20 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext
21 #include "lldb/Symbol/SymbolVendor.h"
22 #include "lldb/Symbol/Symtab.h" // for Symtab
23 #include "lldb/Symbol/Type.h"   // for Type
24 #include "lldb/Symbol/Variable.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/ExecutionContext.h"
27 #include "lldb/Target/ExecutionContextScope.h" // for ExecutionContextScope
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/SectionLoadList.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Utility/ConstString.h"   // for ConstString
32 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
33 #include "lldb/Utility/Endian.h"        // for InlHostByteOrder
34 #include "lldb/Utility/FileSpec.h"      // for FileSpec
35 #include "lldb/Utility/Status.h"        // for Status
36 #include "lldb/Utility/Stream.h"        // for Stream
37 #include "lldb/Utility/StreamString.h"  // for StreamString
38
39 #include "llvm/ADT/StringRef.h" // for StringRef
40 #include "llvm/ADT/Triple.h"
41 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
42
43 #include <cstdint> // for uint8_t, uint32_t
44 #include <memory>  // for shared_ptr, operator!=
45 #include <vector>  // for vector
46
47 #include <assert.h>   // for assert
48 #include <inttypes.h> // for PRIu64, PRIx64
49 #include <string.h>   // for size_t, strlen
50
51 namespace lldb_private {
52 class CompileUnit;
53 }
54 namespace lldb_private {
55 class Function;
56 }
57
58 using namespace lldb;
59 using namespace lldb_private;
60
61 static size_t ReadBytes(ExecutionContextScope *exe_scope,
62                         const Address &address, void *dst, size_t dst_len) {
63   if (exe_scope == nullptr)
64     return 0;
65
66   TargetSP target_sp(exe_scope->CalculateTarget());
67   if (target_sp) {
68     Status error;
69     bool prefer_file_cache = false;
70     return target_sp->ReadMemory(address, prefer_file_cache, dst, dst_len,
71                                  error);
72   }
73   return 0;
74 }
75
76 static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope,
77                                        const Address &address,
78                                        ByteOrder &byte_order,
79                                        uint32_t &addr_size) {
80   byte_order = eByteOrderInvalid;
81   addr_size = 0;
82   if (exe_scope == nullptr)
83     return false;
84
85   TargetSP target_sp(exe_scope->CalculateTarget());
86   if (target_sp) {
87     byte_order = target_sp->GetArchitecture().GetByteOrder();
88     addr_size = target_sp->GetArchitecture().GetAddressByteSize();
89   }
90
91   if (byte_order == eByteOrderInvalid || addr_size == 0) {
92     ModuleSP module_sp(address.GetModule());
93     if (module_sp) {
94       byte_order = module_sp->GetArchitecture().GetByteOrder();
95       addr_size = module_sp->GetArchitecture().GetAddressByteSize();
96     }
97   }
98   return byte_order != eByteOrderInvalid && addr_size != 0;
99 }
100
101 static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope,
102                               const Address &address, uint32_t byte_size,
103                               bool &success) {
104   uint64_t uval64 = 0;
105   if (exe_scope == nullptr || byte_size > sizeof(uint64_t)) {
106     success = false;
107     return 0;
108   }
109   uint64_t buf = 0;
110
111   success = ReadBytes(exe_scope, address, &buf, byte_size) == byte_size;
112   if (success) {
113     ByteOrder byte_order = eByteOrderInvalid;
114     uint32_t addr_size = 0;
115     if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
116       DataExtractor data(&buf, sizeof(buf), byte_order, addr_size);
117       lldb::offset_t offset = 0;
118       uval64 = data.GetU64(&offset);
119     } else
120       success = false;
121   }
122   return uval64;
123 }
124
125 static bool ReadAddress(ExecutionContextScope *exe_scope,
126                         const Address &address, uint32_t pointer_size,
127                         Address &deref_so_addr) {
128   if (exe_scope == nullptr)
129     return false;
130
131   bool success = false;
132   addr_t deref_addr = ReadUIntMax64(exe_scope, address, pointer_size, success);
133   if (success) {
134     ExecutionContext exe_ctx;
135     exe_scope->CalculateExecutionContext(exe_ctx);
136     // If we have any sections that are loaded, try and resolve using the
137     // section load list
138     Target *target = exe_ctx.GetTargetPtr();
139     if (target && !target->GetSectionLoadList().IsEmpty()) {
140       if (target->GetSectionLoadList().ResolveLoadAddress(deref_addr,
141                                                           deref_so_addr))
142         return true;
143     } else {
144       // If we were not running, yet able to read an integer, we must have a
145       // module
146       ModuleSP module_sp(address.GetModule());
147
148       assert(module_sp);
149       if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
150         return true;
151     }
152
153     // We couldn't make "deref_addr" into a section offset value, but we were
154     // able to read the address, so we return a section offset address with no
155     // section and "deref_addr" as the offset (address).
156     deref_so_addr.SetRawAddress(deref_addr);
157     return true;
158   }
159   return false;
160 }
161
162 static bool DumpUInt(ExecutionContextScope *exe_scope, const Address &address,
163                      uint32_t byte_size, Stream *strm) {
164   if (exe_scope == nullptr || byte_size == 0)
165     return 0;
166   std::vector<uint8_t> buf(byte_size, 0);
167
168   if (ReadBytes(exe_scope, address, &buf[0], buf.size()) == buf.size()) {
169     ByteOrder byte_order = eByteOrderInvalid;
170     uint32_t addr_size = 0;
171     if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
172       DataExtractor data(&buf.front(), buf.size(), byte_order, addr_size);
173
174       DumpDataExtractor(data, strm,
175                         0,                    // Start offset in "data"
176                         eFormatHex,           // Print as characters
177                         buf.size(),           // Size of item
178                         1,                    // Items count
179                         UINT32_MAX,           // num per line
180                         LLDB_INVALID_ADDRESS, // base address
181                         0,                    // bitfield bit size
182                         0);                   // bitfield bit offset
183
184       return true;
185     }
186   }
187   return false;
188 }
189
190 static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope,
191                                     const Address &address, Stream *strm) {
192   if (exe_scope == nullptr)
193     return 0;
194   const size_t k_buf_len = 256;
195   char buf[k_buf_len + 1];
196   buf[k_buf_len] = '\0'; // NULL terminate
197
198   // Byte order and address size don't matter for C string dumping..
199   DataExtractor data(buf, sizeof(buf), endian::InlHostByteOrder(), 4);
200   size_t total_len = 0;
201   size_t bytes_read;
202   Address curr_address(address);
203   strm->PutChar('"');
204   while ((bytes_read = ReadBytes(exe_scope, curr_address, buf, k_buf_len)) >
205          0) {
206     size_t len = strlen(buf);
207     if (len == 0)
208       break;
209     if (len > bytes_read)
210       len = bytes_read;
211
212     DumpDataExtractor(data, strm,
213                       0,                    // Start offset in "data"
214                       eFormatChar,          // Print as characters
215                       1,                    // Size of item (1 byte for a char!)
216                       len,                  // How many bytes to print?
217                       UINT32_MAX,           // num per line
218                       LLDB_INVALID_ADDRESS, // base address
219                       0,                    // bitfield bit size
220
221                       0); // bitfield bit offset
222
223     total_len += bytes_read;
224
225     if (len < k_buf_len)
226       break;
227     curr_address.SetOffset(curr_address.GetOffset() + bytes_read);
228   }
229   strm->PutChar('"');
230   return total_len;
231 }
232
233 Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
234
235 Address::Address(addr_t address, const SectionList *section_list)
236     : m_section_wp(), m_offset(LLDB_INVALID_ADDRESS) {
237   ResolveAddressUsingFileSections(address, section_list);
238 }
239
240 const Address &Address::operator=(const Address &rhs) {
241   if (this != &rhs) {
242     m_section_wp = rhs.m_section_wp;
243     m_offset = rhs.m_offset;
244   }
245   return *this;
246 }
247
248 bool Address::ResolveAddressUsingFileSections(addr_t file_addr,
249                                               const SectionList *section_list) {
250   if (section_list) {
251     SectionSP section_sp(
252         section_list->FindSectionContainingFileAddress(file_addr));
253     m_section_wp = section_sp;
254     if (section_sp) {
255       assert(section_sp->ContainsFileAddress(file_addr));
256       m_offset = file_addr - section_sp->GetFileAddress();
257       return true; // Successfully transformed addr into a section offset
258                    // address
259     }
260   }
261   m_offset = file_addr;
262   return false; // Failed to resolve this address to a section offset value
263 }
264
265 ModuleSP Address::GetModule() const {
266   lldb::ModuleSP module_sp;
267   SectionSP section_sp(GetSection());
268   if (section_sp)
269     module_sp = section_sp->GetModule();
270   return module_sp;
271 }
272
273 addr_t Address::GetFileAddress() const {
274   SectionSP section_sp(GetSection());
275   if (section_sp) {
276     addr_t sect_file_addr = section_sp->GetFileAddress();
277     if (sect_file_addr == LLDB_INVALID_ADDRESS) {
278       // Section isn't resolved, we can't return a valid file address
279       return LLDB_INVALID_ADDRESS;
280     }
281     // We have a valid file range, so we can return the file based address by
282     // adding the file base address to our offset
283     return sect_file_addr + m_offset;
284   } else if (SectionWasDeletedPrivate()) {
285     // Used to have a valid section but it got deleted so the offset doesn't
286     // mean anything without the section
287     return LLDB_INVALID_ADDRESS;
288   }
289   // No section, we just return the offset since it is the value in this case
290   return m_offset;
291 }
292
293 addr_t Address::GetLoadAddress(Target *target) const {
294   SectionSP section_sp(GetSection());
295   if (section_sp) {
296     if (target) {
297       addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target);
298
299       if (sect_load_addr != LLDB_INVALID_ADDRESS) {
300         // We have a valid file range, so we can return the file based address
301         // by adding the file base address to our offset
302         return sect_load_addr + m_offset;
303       }
304     }
305   } else if (SectionWasDeletedPrivate()) {
306     // Used to have a valid section but it got deleted so the offset doesn't
307     // mean anything without the section
308     return LLDB_INVALID_ADDRESS;
309   } else {
310     // We don't have a section so the offset is the load address
311     return m_offset;
312   }
313   // The section isn't resolved or an invalid target was passed in so we can't
314   // return a valid load address.
315   return LLDB_INVALID_ADDRESS;
316 }
317
318 addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const {
319   addr_t code_addr = LLDB_INVALID_ADDRESS;
320
321   if (is_indirect && target) {
322     ProcessSP processSP = target->GetProcessSP();
323     Status error;
324     if (processSP) {
325       code_addr = processSP->ResolveIndirectFunction(this, error);
326       if (!error.Success())
327         code_addr = LLDB_INVALID_ADDRESS;
328     }
329   } else {
330     code_addr = GetLoadAddress(target);
331   }
332
333   if (code_addr == LLDB_INVALID_ADDRESS)
334     return code_addr;
335
336   if (target)
337     return target->GetCallableLoadAddress(code_addr, GetAddressClass());
338   return code_addr;
339 }
340
341 bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) {
342   if (SetLoadAddress(load_addr, target)) {
343     if (target)
344       m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
345     return true;
346   }
347   return false;
348 }
349
350 addr_t Address::GetOpcodeLoadAddress(Target *target,
351                                      AddressClass addr_class) const {
352   addr_t code_addr = GetLoadAddress(target);
353   if (code_addr != LLDB_INVALID_ADDRESS) {
354     if (addr_class == AddressClass::eInvalid)
355       addr_class = GetAddressClass();
356     code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class);
357   }
358   return code_addr;
359 }
360
361 bool Address::SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target,
362                                    AddressClass addr_class,
363                                    bool allow_section_end) {
364   if (SetLoadAddress(load_addr, target, allow_section_end)) {
365     if (target) {
366       if (addr_class == AddressClass::eInvalid)
367         addr_class = GetAddressClass();
368       m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class);
369     }
370     return true;
371   }
372   return false;
373 }
374
375 bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
376                    DumpStyle fallback_style, uint32_t addr_size) const {
377   // If the section was nullptr, only load address is going to work unless we
378   // are trying to deref a pointer
379   SectionSP section_sp(GetSection());
380   if (!section_sp && style != DumpStyleResolvedPointerDescription)
381     style = DumpStyleLoadAddress;
382
383   ExecutionContext exe_ctx(exe_scope);
384   Target *target = exe_ctx.GetTargetPtr();
385   // If addr_byte_size is UINT32_MAX, then determine the correct address byte
386   // size for the process or default to the size of addr_t
387   if (addr_size == UINT32_MAX) {
388     if (target)
389       addr_size = target->GetArchitecture().GetAddressByteSize();
390     else
391       addr_size = sizeof(addr_t);
392   }
393
394   Address so_addr;
395   switch (style) {
396   case DumpStyleInvalid:
397     return false;
398
399   case DumpStyleSectionNameOffset:
400     if (section_sp) {
401       section_sp->DumpName(s);
402       s->Printf(" + %" PRIu64, m_offset);
403     } else {
404       s->Address(m_offset, addr_size);
405     }
406     break;
407
408   case DumpStyleSectionPointerOffset:
409     s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
410     s->Address(m_offset, addr_size);
411     break;
412
413   case DumpStyleModuleWithFileAddress:
414     if (section_sp) {
415       ModuleSP module_sp = section_sp->GetModule();
416       if (module_sp)
417         s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
418                              "<Unknown>"));
419       else
420         s->Printf("%s[", "<Unknown>");
421     }
422     LLVM_FALLTHROUGH;
423   case DumpStyleFileAddress: {
424     addr_t file_addr = GetFileAddress();
425     if (file_addr == LLDB_INVALID_ADDRESS) {
426       if (fallback_style != DumpStyleInvalid)
427         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
428       return false;
429     }
430     s->Address(file_addr, addr_size);
431     if (style == DumpStyleModuleWithFileAddress && section_sp)
432       s->PutChar(']');
433   } break;
434
435   case DumpStyleLoadAddress: {
436     addr_t load_addr = GetLoadAddress(target);
437
438     /*
439      * MIPS:
440      * Display address in compressed form for MIPS16 or microMIPS
441      * if the address belongs to AddressClass::eCodeAlternateISA.
442     */
443     if (target) {
444       const llvm::Triple::ArchType llvm_arch =
445           target->GetArchitecture().GetMachine();
446       if (llvm_arch == llvm::Triple::mips ||
447           llvm_arch == llvm::Triple::mipsel ||
448           llvm_arch == llvm::Triple::mips64 ||
449           llvm_arch == llvm::Triple::mips64el)
450         load_addr = GetCallableLoadAddress(target);
451     }
452
453     if (load_addr == LLDB_INVALID_ADDRESS) {
454       if (fallback_style != DumpStyleInvalid)
455         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
456       return false;
457     }
458     s->Address(load_addr, addr_size);
459   } break;
460
461   case DumpStyleResolvedDescription:
462   case DumpStyleResolvedDescriptionNoModule:
463   case DumpStyleResolvedDescriptionNoFunctionArguments:
464   case DumpStyleNoFunctionName:
465     if (IsSectionOffset()) {
466       uint32_t pointer_size = 4;
467       ModuleSP module_sp(GetModule());
468       if (target)
469         pointer_size = target->GetArchitecture().GetAddressByteSize();
470       else if (module_sp)
471         pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
472
473       bool showed_info = false;
474       if (section_sp) {
475         SectionType sect_type = section_sp->GetType();
476         switch (sect_type) {
477         case eSectionTypeData:
478           if (module_sp) {
479             SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
480             if (sym_vendor) {
481               Symtab *symtab = sym_vendor->GetSymtab();
482               if (symtab) {
483                 const addr_t file_Addr = GetFileAddress();
484                 Symbol *symbol =
485                     symtab->FindSymbolContainingFileAddress(file_Addr);
486                 if (symbol) {
487                   const char *symbol_name = symbol->GetName().AsCString();
488                   if (symbol_name) {
489                     s->PutCString(symbol_name);
490                     addr_t delta =
491                         file_Addr - symbol->GetAddressRef().GetFileAddress();
492                     if (delta)
493                       s->Printf(" + %" PRIu64, delta);
494                     showed_info = true;
495                   }
496                 }
497               }
498             }
499           }
500           break;
501
502         case eSectionTypeDataCString:
503           // Read the C string from memory and display it
504           showed_info = true;
505           ReadCStringFromMemory(exe_scope, *this, s);
506           break;
507
508         case eSectionTypeDataCStringPointers:
509           if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
510 #if VERBOSE_OUTPUT
511             s->PutCString("(char *)");
512             so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
513                          DumpStyleFileAddress);
514             s->PutCString(": ");
515 #endif
516             showed_info = true;
517             ReadCStringFromMemory(exe_scope, so_addr, s);
518           }
519           break;
520
521         case eSectionTypeDataObjCMessageRefs:
522           if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
523             if (target && so_addr.IsSectionOffset()) {
524               SymbolContext func_sc;
525               target->GetImages().ResolveSymbolContextForAddress(
526                   so_addr, eSymbolContextEverything, func_sc);
527               if (func_sc.function != nullptr || func_sc.symbol != nullptr) {
528                 showed_info = true;
529 #if VERBOSE_OUTPUT
530                 s->PutCString("(objc_msgref *) -> { (func*)");
531                 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
532                              DumpStyleFileAddress);
533 #else
534                 s->PutCString("{ ");
535 #endif
536                 Address cstr_addr(*this);
537                 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
538                 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true,
539                                         false, true, true);
540                 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
541 #if VERBOSE_OUTPUT
542                   s->PutCString("), (char *)");
543                   so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
544                                DumpStyleFileAddress);
545                   s->PutCString(" (");
546 #else
547                   s->PutCString(", ");
548 #endif
549                   ReadCStringFromMemory(exe_scope, so_addr, s);
550                 }
551 #if VERBOSE_OUTPUT
552                 s->PutCString(") }");
553 #else
554                 s->PutCString(" }");
555 #endif
556               }
557             }
558           }
559           break;
560
561         case eSectionTypeDataObjCCFStrings: {
562           Address cfstring_data_addr(*this);
563           cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
564                                        (2 * pointer_size));
565           if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
566                           so_addr)) {
567 #if VERBOSE_OUTPUT
568             s->PutCString("(CFString *) ");
569             cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
570                                     DumpStyleFileAddress);
571             s->PutCString(" -> @");
572 #else
573             s->PutChar('@');
574 #endif
575             if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
576               showed_info = true;
577           }
578         } break;
579
580         case eSectionTypeData4:
581           // Read the 4 byte data and display it
582           showed_info = true;
583           s->PutCString("(uint32_t) ");
584           DumpUInt(exe_scope, *this, 4, s);
585           break;
586
587         case eSectionTypeData8:
588           // Read the 8 byte data and display it
589           showed_info = true;
590           s->PutCString("(uint64_t) ");
591           DumpUInt(exe_scope, *this, 8, s);
592           break;
593
594         case eSectionTypeData16:
595           // Read the 16 byte data and display it
596           showed_info = true;
597           s->PutCString("(uint128_t) ");
598           DumpUInt(exe_scope, *this, 16, s);
599           break;
600
601         case eSectionTypeDataPointers:
602           // Read the pointer data and display it
603           if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
604             s->PutCString("(void *)");
605             so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
606                          DumpStyleFileAddress);
607
608             showed_info = true;
609             if (so_addr.IsSectionOffset()) {
610               SymbolContext pointer_sc;
611               if (target) {
612                 target->GetImages().ResolveSymbolContextForAddress(
613                     so_addr, eSymbolContextEverything, pointer_sc);
614                 if (pointer_sc.function != nullptr ||
615                     pointer_sc.symbol != nullptr) {
616                   s->PutCString(": ");
617                   pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
618                                              false, true, true);
619                 }
620               }
621             }
622           }
623           break;
624
625         default:
626           break;
627         }
628       }
629
630       if (!showed_info) {
631         if (module_sp) {
632           SymbolContext sc;
633           module_sp->ResolveSymbolContextForAddress(
634               *this, eSymbolContextEverything, sc);
635           if (sc.function || sc.symbol) {
636             bool show_stop_context = true;
637             const bool show_module = (style == DumpStyleResolvedDescription);
638             const bool show_fullpaths = false;
639             const bool show_inlined_frames = true;
640             const bool show_function_arguments =
641                 (style != DumpStyleResolvedDescriptionNoFunctionArguments);
642             const bool show_function_name = (style != DumpStyleNoFunctionName);
643             if (sc.function == nullptr && sc.symbol != nullptr) {
644               // If we have just a symbol make sure it is in the right section
645               if (sc.symbol->ValueIsAddress()) {
646                 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) {
647                   // don't show the module if the symbol is a trampoline symbol
648                   show_stop_context = false;
649                 }
650               }
651             }
652             if (show_stop_context) {
653               // We have a function or a symbol from the same sections as this
654               // address.
655               sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
656                                  show_module, show_inlined_frames,
657                                  show_function_arguments, show_function_name);
658             } else {
659               // We found a symbol but it was in a different section so it
660               // isn't the symbol we should be showing, just show the section
661               // name + offset
662               Dump(s, exe_scope, DumpStyleSectionNameOffset);
663             }
664           }
665         }
666       }
667     } else {
668       if (fallback_style != DumpStyleInvalid)
669         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
670       return false;
671     }
672     break;
673
674   case DumpStyleDetailedSymbolContext:
675     if (IsSectionOffset()) {
676       ModuleSP module_sp(GetModule());
677       if (module_sp) {
678         SymbolContext sc;
679         module_sp->ResolveSymbolContextForAddress(
680             *this, eSymbolContextEverything | eSymbolContextVariable, sc);
681         if (sc.symbol) {
682           // If we have just a symbol make sure it is in the same section as
683           // our address. If it isn't, then we might have just found the last
684           // symbol that came before the address that we are looking up that
685           // has nothing to do with our address lookup.
686           if (sc.symbol->ValueIsAddress() &&
687               sc.symbol->GetAddressRef().GetSection() != GetSection())
688             sc.symbol = nullptr;
689         }
690         sc.GetDescription(s, eDescriptionLevelBrief, target);
691
692         if (sc.block) {
693           bool can_create = true;
694           bool get_parent_variables = true;
695           bool stop_if_block_is_inlined_function = false;
696           VariableList variable_list;
697           sc.block->AppendVariables(can_create, get_parent_variables,
698                                     stop_if_block_is_inlined_function,
699                                     [](Variable *) { return true; },
700                                     &variable_list);
701
702           const size_t num_variables = variable_list.GetSize();
703           for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) {
704             Variable *var = variable_list.GetVariableAtIndex(var_idx).get();
705             if (var && var->LocationIsValidForAddress(*this)) {
706               s->Indent();
707               s->Printf("   Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
708                         var->GetID(), var->GetName().GetCString());
709               Type *type = var->GetType();
710               if (type)
711                 s->Printf(", type = \"%s\"", type->GetName().GetCString());
712               else
713                 s->PutCString(", type = <unknown>");
714               s->PutCString(", location = ");
715               var->DumpLocationForAddress(s, *this);
716               s->PutCString(", decl = ");
717               var->GetDeclaration().DumpStopContext(s, false);
718               s->EOL();
719             }
720           }
721         }
722       }
723     } else {
724       if (fallback_style != DumpStyleInvalid)
725         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
726       return false;
727     }
728     break;
729
730   case DumpStyleResolvedPointerDescription: {
731     Process *process = exe_ctx.GetProcessPtr();
732     if (process) {
733       addr_t load_addr = GetLoadAddress(target);
734       if (load_addr != LLDB_INVALID_ADDRESS) {
735         Status memory_error;
736         addr_t dereferenced_load_addr =
737             process->ReadPointerFromMemory(load_addr, memory_error);
738         if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) {
739           Address dereferenced_addr;
740           if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr,
741                                                target)) {
742             StreamString strm;
743             if (dereferenced_addr.Dump(&strm, exe_scope,
744                                        DumpStyleResolvedDescription,
745                                        DumpStyleInvalid, addr_size)) {
746               s->Address(dereferenced_load_addr, addr_size, " -> ", " ");
747               s->Write(strm.GetString().data(), strm.GetSize());
748               return true;
749             }
750           }
751         }
752       }
753     }
754     if (fallback_style != DumpStyleInvalid)
755       return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
756     return false;
757   } break;
758   }
759
760   return true;
761 }
762
763 bool Address::SectionWasDeleted() const {
764   if (GetSection())
765     return false;
766   return SectionWasDeletedPrivate();
767 }
768
769 bool Address::SectionWasDeletedPrivate() const {
770   lldb::SectionWP empty_section_wp;
771
772   // If either call to "std::weak_ptr::owner_before(...) value returns true,
773   // this indicates that m_section_wp once contained (possibly still does) a
774   // reference to a valid shared pointer. This helps us know if we had a valid
775   // reference to a section which is now invalid because the module it was in
776   // was unloaded/deleted, or if the address doesn't have a valid reference to
777   // a section.
778   return empty_section_wp.owner_before(m_section_wp) ||
779          m_section_wp.owner_before(empty_section_wp);
780 }
781
782 uint32_t Address::CalculateSymbolContext(SymbolContext *sc,
783                                          uint32_t resolve_scope) const {
784   sc->Clear(false);
785   // Absolute addresses don't have enough information to reconstruct even their
786   // target.
787
788   SectionSP section_sp(GetSection());
789   if (section_sp) {
790     ModuleSP module_sp(section_sp->GetModule());
791     if (module_sp) {
792       sc->module_sp = module_sp;
793       if (sc->module_sp)
794         return sc->module_sp->ResolveSymbolContextForAddress(
795             *this, resolve_scope, *sc);
796     }
797   }
798   return 0;
799 }
800
801 ModuleSP Address::CalculateSymbolContextModule() const {
802   SectionSP section_sp(GetSection());
803   if (section_sp)
804     return section_sp->GetModule();
805   return ModuleSP();
806 }
807
808 CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
809   SectionSP section_sp(GetSection());
810   if (section_sp) {
811     SymbolContext sc;
812     sc.module_sp = section_sp->GetModule();
813     if (sc.module_sp) {
814       sc.module_sp->ResolveSymbolContextForAddress(*this,
815                                                    eSymbolContextCompUnit, sc);
816       return sc.comp_unit;
817     }
818   }
819   return nullptr;
820 }
821
822 Function *Address::CalculateSymbolContextFunction() const {
823   SectionSP section_sp(GetSection());
824   if (section_sp) {
825     SymbolContext sc;
826     sc.module_sp = section_sp->GetModule();
827     if (sc.module_sp) {
828       sc.module_sp->ResolveSymbolContextForAddress(*this,
829                                                    eSymbolContextFunction, sc);
830       return sc.function;
831     }
832   }
833   return nullptr;
834 }
835
836 Block *Address::CalculateSymbolContextBlock() const {
837   SectionSP section_sp(GetSection());
838   if (section_sp) {
839     SymbolContext sc;
840     sc.module_sp = section_sp->GetModule();
841     if (sc.module_sp) {
842       sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
843                                                    sc);
844       return sc.block;
845     }
846   }
847   return nullptr;
848 }
849
850 Symbol *Address::CalculateSymbolContextSymbol() const {
851   SectionSP section_sp(GetSection());
852   if (section_sp) {
853     SymbolContext sc;
854     sc.module_sp = section_sp->GetModule();
855     if (sc.module_sp) {
856       sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
857                                                    sc);
858       return sc.symbol;
859     }
860   }
861   return nullptr;
862 }
863
864 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
865   SectionSP section_sp(GetSection());
866   if (section_sp) {
867     SymbolContext sc;
868     sc.module_sp = section_sp->GetModule();
869     if (sc.module_sp) {
870       sc.module_sp->ResolveSymbolContextForAddress(*this,
871                                                    eSymbolContextLineEntry, sc);
872       if (sc.line_entry.IsValid()) {
873         line_entry = sc.line_entry;
874         return true;
875       }
876     }
877   }
878   line_entry.Clear();
879   return false;
880 }
881
882 int Address::CompareFileAddress(const Address &a, const Address &b) {
883   addr_t a_file_addr = a.GetFileAddress();
884   addr_t b_file_addr = b.GetFileAddress();
885   if (a_file_addr < b_file_addr)
886     return -1;
887   if (a_file_addr > b_file_addr)
888     return +1;
889   return 0;
890 }
891
892 int Address::CompareLoadAddress(const Address &a, const Address &b,
893                                 Target *target) {
894   assert(target != nullptr);
895   addr_t a_load_addr = a.GetLoadAddress(target);
896   addr_t b_load_addr = b.GetLoadAddress(target);
897   if (a_load_addr < b_load_addr)
898     return -1;
899   if (a_load_addr > b_load_addr)
900     return +1;
901   return 0;
902 }
903
904 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) {
905   ModuleSP a_module_sp(a.GetModule());
906   ModuleSP b_module_sp(b.GetModule());
907   Module *a_module = a_module_sp.get();
908   Module *b_module = b_module_sp.get();
909   if (a_module < b_module)
910     return -1;
911   if (a_module > b_module)
912     return +1;
913   // Modules are the same, just compare the file address since they should be
914   // unique
915   addr_t a_file_addr = a.GetFileAddress();
916   addr_t b_file_addr = b.GetFileAddress();
917   if (a_file_addr < b_file_addr)
918     return -1;
919   if (a_file_addr > b_file_addr)
920     return +1;
921   return 0;
922 }
923
924 size_t Address::MemorySize() const {
925   // Noting special for the memory size of a single Address object, it is just
926   // the size of itself.
927   return sizeof(Address);
928 }
929
930 //----------------------------------------------------------------------
931 // NOTE: Be careful using this operator. It can correctly compare two
932 // addresses from the same Module correctly. It can't compare two addresses
933 // from different modules in any meaningful way, but it will compare the module
934 // pointers.
935 //
936 // To sum things up:
937 // - works great for addresses within the same module - it works for addresses
938 // across multiple modules, but don't expect the
939 //   address results to make much sense
940 //
941 // This basically lets Address objects be used in ordered collection classes.
942 //----------------------------------------------------------------------
943
944 bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
945   ModuleSP lhs_module_sp(lhs.GetModule());
946   ModuleSP rhs_module_sp(rhs.GetModule());
947   Module *lhs_module = lhs_module_sp.get();
948   Module *rhs_module = rhs_module_sp.get();
949   if (lhs_module == rhs_module) {
950     // Addresses are in the same module, just compare the file addresses
951     return lhs.GetFileAddress() < rhs.GetFileAddress();
952   } else {
953     // The addresses are from different modules, just use the module pointer
954     // value to get consistent ordering
955     return lhs_module < rhs_module;
956   }
957 }
958
959 bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
960   ModuleSP lhs_module_sp(lhs.GetModule());
961   ModuleSP rhs_module_sp(rhs.GetModule());
962   Module *lhs_module = lhs_module_sp.get();
963   Module *rhs_module = rhs_module_sp.get();
964   if (lhs_module == rhs_module) {
965     // Addresses are in the same module, just compare the file addresses
966     return lhs.GetFileAddress() > rhs.GetFileAddress();
967   } else {
968     // The addresses are from different modules, just use the module pointer
969     // value to get consistent ordering
970     return lhs_module > rhs_module;
971   }
972 }
973
974 // The operator == checks for exact equality only (same section, same offset)
975 bool lldb_private::operator==(const Address &a, const Address &rhs) {
976   return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection();
977 }
978
979 // The operator != checks for exact inequality only (differing section, or
980 // different offset)
981 bool lldb_private::operator!=(const Address &a, const Address &rhs) {
982   return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
983 }
984
985 AddressClass Address::GetAddressClass() const {
986   ModuleSP module_sp(GetModule());
987   if (module_sp) {
988     ObjectFile *obj_file = module_sp->GetObjectFile();
989     if (obj_file) {
990       // Give the symbol vendor a chance to add to the unified section list.
991       module_sp->GetSymbolVendor();
992       return obj_file->GetAddressClass(GetFileAddress());
993     }
994   }
995   return AddressClass::eUnknown;
996 }
997
998 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target,
999                              bool allow_section_end) {
1000   if (target && target->GetSectionLoadList().ResolveLoadAddress(
1001                     load_addr, *this, allow_section_end))
1002     return true;
1003   m_section_wp.reset();
1004   m_offset = load_addr;
1005   return false;
1006 }