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