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"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Symbol/Declaration.h"
17 #include "lldb/Symbol/LineEntry.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolVendor.h"
22 #include "lldb/Symbol/Symtab.h"
23 #include "lldb/Symbol/Type.h"
24 #include "lldb/Symbol/Variable.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/ExecutionContext.h"
27 #include "lldb/Target/ExecutionContextScope.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/SectionLoadList.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Utility/ConstString.h"
32 #include "lldb/Utility/DataExtractor.h"
33 #include "lldb/Utility/Endian.h"
34 #include "lldb/Utility/FileSpec.h"
35 #include "lldb/Utility/Status.h"
36 #include "lldb/Utility/Stream.h"
37 #include "lldb/Utility/StreamString.h"
38
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/Triple.h"
41 #include "llvm/Support/Compiler.h"
42
43 #include <cstdint>
44 #include <memory>
45 #include <vector>
46
47 #include <assert.h>
48 #include <inttypes.h>
49 #include <string.h>
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
ReadBytes(ExecutionContextScope * exe_scope,const Address & address,void * dst,size_t dst_len)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
GetByteOrderAndAddressSize(ExecutionContextScope * exe_scope,const Address & address,ByteOrder & byte_order,uint32_t & addr_size)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
ReadUIntMax64(ExecutionContextScope * exe_scope,const Address & address,uint32_t byte_size,bool & success)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
ReadAddress(ExecutionContextScope * exe_scope,const Address & address,uint32_t pointer_size,Address & deref_so_addr)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
DumpUInt(ExecutionContextScope * exe_scope,const Address & address,uint32_t byte_size,Stream * strm)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
ReadCStringFromMemory(ExecutionContextScope * exe_scope,const Address & address,Stream * strm)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
Address(lldb::addr_t abs_addr)233 Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
234
Address(addr_t address,const SectionList * section_list)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
operator =(const Address & rhs)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
ResolveAddressUsingFileSections(addr_t file_addr,const SectionList * section_list)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
GetModule() const265 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
GetFileAddress() const273 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
GetLoadAddress(Target * target) const293 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
GetCallableLoadAddress(Target * target,bool is_indirect) const318 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
SetCallableLoadAddress(lldb::addr_t load_addr,Target * target)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
GetOpcodeLoadAddress(Target * target,AddressClass addr_class) const350 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
SetOpcodeLoadAddress(lldb::addr_t load_addr,Target * target,AddressClass addr_class,bool allow_section_end)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
Dump(Stream * s,ExecutionContextScope * exe_scope,DumpStyle style,DumpStyle fallback_style,uint32_t addr_size) const375 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
SectionWasDeleted() const763 bool Address::SectionWasDeleted() const {
764 if (GetSection())
765 return false;
766 return SectionWasDeletedPrivate();
767 }
768
SectionWasDeletedPrivate() const769 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
CalculateSymbolContext(SymbolContext * sc,SymbolContextItem resolve_scope) const783 Address::CalculateSymbolContext(SymbolContext *sc,
784 SymbolContextItem resolve_scope) const {
785 sc->Clear(false);
786 // Absolute addresses don't have enough information to reconstruct even their
787 // target.
788
789 SectionSP section_sp(GetSection());
790 if (section_sp) {
791 ModuleSP module_sp(section_sp->GetModule());
792 if (module_sp) {
793 sc->module_sp = module_sp;
794 if (sc->module_sp)
795 return sc->module_sp->ResolveSymbolContextForAddress(
796 *this, resolve_scope, *sc);
797 }
798 }
799 return 0;
800 }
801
CalculateSymbolContextModule() const802 ModuleSP Address::CalculateSymbolContextModule() const {
803 SectionSP section_sp(GetSection());
804 if (section_sp)
805 return section_sp->GetModule();
806 return ModuleSP();
807 }
808
CalculateSymbolContextCompileUnit() const809 CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
810 SectionSP section_sp(GetSection());
811 if (section_sp) {
812 SymbolContext sc;
813 sc.module_sp = section_sp->GetModule();
814 if (sc.module_sp) {
815 sc.module_sp->ResolveSymbolContextForAddress(*this,
816 eSymbolContextCompUnit, sc);
817 return sc.comp_unit;
818 }
819 }
820 return nullptr;
821 }
822
CalculateSymbolContextFunction() const823 Function *Address::CalculateSymbolContextFunction() const {
824 SectionSP section_sp(GetSection());
825 if (section_sp) {
826 SymbolContext sc;
827 sc.module_sp = section_sp->GetModule();
828 if (sc.module_sp) {
829 sc.module_sp->ResolveSymbolContextForAddress(*this,
830 eSymbolContextFunction, sc);
831 return sc.function;
832 }
833 }
834 return nullptr;
835 }
836
CalculateSymbolContextBlock() const837 Block *Address::CalculateSymbolContextBlock() const {
838 SectionSP section_sp(GetSection());
839 if (section_sp) {
840 SymbolContext sc;
841 sc.module_sp = section_sp->GetModule();
842 if (sc.module_sp) {
843 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
844 sc);
845 return sc.block;
846 }
847 }
848 return nullptr;
849 }
850
CalculateSymbolContextSymbol() const851 Symbol *Address::CalculateSymbolContextSymbol() const {
852 SectionSP section_sp(GetSection());
853 if (section_sp) {
854 SymbolContext sc;
855 sc.module_sp = section_sp->GetModule();
856 if (sc.module_sp) {
857 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
858 sc);
859 return sc.symbol;
860 }
861 }
862 return nullptr;
863 }
864
CalculateSymbolContextLineEntry(LineEntry & line_entry) const865 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
866 SectionSP section_sp(GetSection());
867 if (section_sp) {
868 SymbolContext sc;
869 sc.module_sp = section_sp->GetModule();
870 if (sc.module_sp) {
871 sc.module_sp->ResolveSymbolContextForAddress(*this,
872 eSymbolContextLineEntry, sc);
873 if (sc.line_entry.IsValid()) {
874 line_entry = sc.line_entry;
875 return true;
876 }
877 }
878 }
879 line_entry.Clear();
880 return false;
881 }
882
CompareFileAddress(const Address & a,const Address & b)883 int Address::CompareFileAddress(const Address &a, const Address &b) {
884 addr_t a_file_addr = a.GetFileAddress();
885 addr_t b_file_addr = b.GetFileAddress();
886 if (a_file_addr < b_file_addr)
887 return -1;
888 if (a_file_addr > b_file_addr)
889 return +1;
890 return 0;
891 }
892
CompareLoadAddress(const Address & a,const Address & b,Target * target)893 int Address::CompareLoadAddress(const Address &a, const Address &b,
894 Target *target) {
895 assert(target != nullptr);
896 addr_t a_load_addr = a.GetLoadAddress(target);
897 addr_t b_load_addr = b.GetLoadAddress(target);
898 if (a_load_addr < b_load_addr)
899 return -1;
900 if (a_load_addr > b_load_addr)
901 return +1;
902 return 0;
903 }
904
CompareModulePointerAndOffset(const Address & a,const Address & b)905 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) {
906 ModuleSP a_module_sp(a.GetModule());
907 ModuleSP b_module_sp(b.GetModule());
908 Module *a_module = a_module_sp.get();
909 Module *b_module = b_module_sp.get();
910 if (a_module < b_module)
911 return -1;
912 if (a_module > b_module)
913 return +1;
914 // Modules are the same, just compare the file address since they should be
915 // unique
916 addr_t a_file_addr = a.GetFileAddress();
917 addr_t b_file_addr = b.GetFileAddress();
918 if (a_file_addr < b_file_addr)
919 return -1;
920 if (a_file_addr > b_file_addr)
921 return +1;
922 return 0;
923 }
924
MemorySize() const925 size_t Address::MemorySize() const {
926 // Noting special for the memory size of a single Address object, it is just
927 // the size of itself.
928 return sizeof(Address);
929 }
930
931 //----------------------------------------------------------------------
932 // NOTE: Be careful using this operator. It can correctly compare two
933 // addresses from the same Module correctly. It can't compare two addresses
934 // from different modules in any meaningful way, but it will compare the module
935 // pointers.
936 //
937 // To sum things up:
938 // - works great for addresses within the same module - it works for addresses
939 // across multiple modules, but don't expect the
940 // address results to make much sense
941 //
942 // This basically lets Address objects be used in ordered collection classes.
943 //----------------------------------------------------------------------
944
operator <(const Address & lhs,const Address & rhs)945 bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
946 ModuleSP lhs_module_sp(lhs.GetModule());
947 ModuleSP rhs_module_sp(rhs.GetModule());
948 Module *lhs_module = lhs_module_sp.get();
949 Module *rhs_module = rhs_module_sp.get();
950 if (lhs_module == rhs_module) {
951 // Addresses are in the same module, just compare the file addresses
952 return lhs.GetFileAddress() < rhs.GetFileAddress();
953 } else {
954 // The addresses are from different modules, just use the module pointer
955 // value to get consistent ordering
956 return lhs_module < rhs_module;
957 }
958 }
959
operator >(const Address & lhs,const Address & rhs)960 bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
961 ModuleSP lhs_module_sp(lhs.GetModule());
962 ModuleSP rhs_module_sp(rhs.GetModule());
963 Module *lhs_module = lhs_module_sp.get();
964 Module *rhs_module = rhs_module_sp.get();
965 if (lhs_module == rhs_module) {
966 // Addresses are in the same module, just compare the file addresses
967 return lhs.GetFileAddress() > rhs.GetFileAddress();
968 } else {
969 // The addresses are from different modules, just use the module pointer
970 // value to get consistent ordering
971 return lhs_module > rhs_module;
972 }
973 }
974
975 // The operator == checks for exact equality only (same section, same offset)
operator ==(const Address & a,const Address & rhs)976 bool lldb_private::operator==(const Address &a, const Address &rhs) {
977 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection();
978 }
979
980 // The operator != checks for exact inequality only (differing section, or
981 // different offset)
operator !=(const Address & a,const Address & rhs)982 bool lldb_private::operator!=(const Address &a, const Address &rhs) {
983 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
984 }
985
GetAddressClass() const986 AddressClass Address::GetAddressClass() const {
987 ModuleSP module_sp(GetModule());
988 if (module_sp) {
989 ObjectFile *obj_file = module_sp->GetObjectFile();
990 if (obj_file) {
991 // Give the symbol vendor a chance to add to the unified section list
992 // and to symtab from symbol file
993 if (SymbolVendor *vendor = module_sp->GetSymbolVendor())
994 vendor->GetSymtab();
995 return obj_file->GetAddressClass(GetFileAddress());
996 }
997 }
998 return AddressClass::eUnknown;
999 }
1000
SetLoadAddress(lldb::addr_t load_addr,Target * target,bool allow_section_end)1001 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target,
1002 bool allow_section_end) {
1003 if (target && target->GetSectionLoadList().ResolveLoadAddress(
1004 load_addr, *this, allow_section_end))
1005 return true;
1006 m_section_wp.reset();
1007 m_offset = load_addr;
1008 return false;
1009 }
1010