LLDB mainline
Address.cpp
Go to the documentation of this file.
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"
12#include "lldb/Core/Module.h"
14#include "lldb/Core/Section.h"
15#include "lldb/Symbol/Block.h"
18#include "lldb/Symbol/Symbol.h"
21#include "lldb/Symbol/Symtab.h"
22#include "lldb/Symbol/Type.h"
25#include "lldb/Target/ABI.h"
28#include "lldb/Target/Process.h"
30#include "lldb/Target/Target.h"
33#include "lldb/Utility/Endian.h"
35#include "lldb/Utility/Status.h"
36#include "lldb/Utility/Stream.h"
38
39#include "llvm/ADT/StringRef.h"
40#include "llvm/Support/Compiler.h"
41#include "llvm/TargetParser/Triple.h"
42
43#include <cstdint>
44#include <memory>
45#include <vector>
46
47#include <cassert>
48#include <cinttypes>
49#include <cstring>
50
51namespace lldb_private {
52class CompileUnit;
53}
54namespace lldb_private {
55class Function;
56}
57
58using namespace lldb;
59using namespace lldb_private;
60
61static 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) {
69 bool force_live_memory = true;
70 return target_sp->ReadMemory(address, dst, dst_len, error,
71 force_live_memory);
72 }
73 return 0;
74}
75
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
101static 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
125static 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
162static 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 false;
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
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
233Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
234
235Address::Address(addr_t address, const SectionList *section_list)
236 : m_section_wp() {
237 ResolveAddressUsingFileSections(address, section_list);
238}
239
241 if (this != &rhs) {
243 m_offset = rhs.m_offset;
244 }
245 return *this;
246}
247
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/// if "addr_range_ptr" is not NULL, then fill in with the address range of the function.
267 AddressRange *addr_range_ptr) {
268 constexpr SymbolContextItem resolve_scope =
269 eSymbolContextFunction | eSymbolContextSymbol;
270
271 if (!(CalculateSymbolContext(&sym_ctx, resolve_scope) & resolve_scope)) {
272 if (addr_range_ptr)
273 addr_range_ptr->Clear();
274 return false;
275 }
276
277 if (!addr_range_ptr)
278 return true;
279
280 return sym_ctx.GetAddressRange(resolve_scope, 0, false, *addr_range_ptr);
281}
282
283ModuleSP Address::GetModule() const {
284 lldb::ModuleSP module_sp;
285 SectionSP section_sp(GetSection());
286 if (section_sp)
287 module_sp = section_sp->GetModule();
288 return module_sp;
289}
290
292 SectionSP section_sp(GetSection());
293 if (section_sp) {
294 addr_t sect_file_addr = section_sp->GetFileAddress();
295 if (sect_file_addr == LLDB_INVALID_ADDRESS) {
296 // Section isn't resolved, we can't return a valid file address
298 }
299 // We have a valid file range, so we can return the file based address by
300 // adding the file base address to our offset
301 return sect_file_addr + m_offset;
302 } else if (SectionWasDeletedPrivate()) {
303 // Used to have a valid section but it got deleted so the offset doesn't
304 // mean anything without the section
306 }
307 // No section, we just return the offset since it is the value in this case
308 return m_offset;
309}
310
312 SectionSP section_sp(GetSection());
313 if (section_sp) {
314 if (target) {
315 addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target);
316
317 if (sect_load_addr != LLDB_INVALID_ADDRESS) {
318 // We have a valid file range, so we can return the file based address
319 // by adding the file base address to our offset
320 return sect_load_addr + m_offset;
321 }
322 }
323 } else if (SectionWasDeletedPrivate()) {
324 // Used to have a valid section but it got deleted so the offset doesn't
325 // mean anything without the section
327 } else {
328 // We don't have a section so the offset is the load address
329 return m_offset;
330 }
331 // The section isn't resolved or an invalid target was passed in so we can't
332 // return a valid load address.
334}
335
336addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const {
337 addr_t code_addr = LLDB_INVALID_ADDRESS;
338
339 if (is_indirect && target) {
340 ProcessSP processSP = target->GetProcessSP();
342 if (processSP) {
343 code_addr = processSP->ResolveIndirectFunction(this, error);
344 if (!error.Success())
345 code_addr = LLDB_INVALID_ADDRESS;
346 }
347 } else {
348 code_addr = GetLoadAddress(target);
349 }
350
351 if (code_addr == LLDB_INVALID_ADDRESS)
352 return code_addr;
353
354 if (target)
355 return target->GetCallableLoadAddress(code_addr, GetAddressClass());
356 return code_addr;
357}
358
360 if (SetLoadAddress(load_addr, target)) {
361 if (target)
363 return true;
364 }
365 return false;
366}
367
369 AddressClass addr_class) const {
370 addr_t code_addr = GetLoadAddress(target);
371 if (code_addr != LLDB_INVALID_ADDRESS) {
372 if (addr_class == AddressClass::eInvalid)
373 addr_class = GetAddressClass();
374 code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class);
375 }
376 return code_addr;
377}
378
380 AddressClass addr_class,
381 bool allow_section_end) {
382 if (SetLoadAddress(load_addr, target, allow_section_end)) {
383 if (target) {
384 if (addr_class == AddressClass::eInvalid)
385 addr_class = GetAddressClass();
386 m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class);
387 }
388 return true;
389 }
390 return false;
391}
392
394 DescriptionLevel level) const {
395 assert(level == eDescriptionLevelBrief &&
396 "Non-brief descriptions not implemented");
397 LineEntry line_entry;
398 if (CalculateSymbolContextLineEntry(line_entry)) {
399 s.Printf(" (%s:%u:%u)", line_entry.file.GetFilename().GetCString(),
400 line_entry.line, line_entry.column);
401 return true;
402 }
403 return false;
404}
405
407 DumpStyle fallback_style, uint32_t addr_size,
408 bool all_ranges) const {
409 // If the section was nullptr, only load address is going to work unless we
410 // are trying to deref a pointer
411 SectionSP section_sp(GetSection());
412 if (!section_sp && style != DumpStyleResolvedPointerDescription)
413 style = DumpStyleLoadAddress;
414
415 ExecutionContext exe_ctx(exe_scope);
416 Target *target = exe_ctx.GetTargetPtr();
417 // If addr_byte_size is UINT32_MAX, then determine the correct address byte
418 // size for the process or default to the size of addr_t
419 if (addr_size == UINT32_MAX) {
420 if (target)
421 addr_size = target->GetArchitecture().GetAddressByteSize();
422 else
423 addr_size = sizeof(addr_t);
424 }
425
426 Address so_addr;
427 switch (style) {
428 case DumpStyleInvalid:
429 return false;
430
432 if (section_sp) {
433 section_sp->DumpName(s->AsRawOstream());
434 s->Printf(" + %" PRIu64, m_offset);
435 } else {
436 DumpAddress(s->AsRawOstream(), m_offset, addr_size);
437 }
438 break;
439
441 s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
442 DumpAddress(s->AsRawOstream(), m_offset, addr_size);
443 break;
444
446 if (section_sp) {
447 ModuleSP module_sp = section_sp->GetModule();
448 if (module_sp)
449 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
450 "<Unknown>"));
451 else
452 s->Printf("%s[", "<Unknown>");
453 }
454 [[fallthrough]];
456 addr_t file_addr = GetFileAddress();
457 if (file_addr == LLDB_INVALID_ADDRESS) {
458 if (fallback_style != DumpStyleInvalid)
459 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
460 return false;
461 }
462 DumpAddress(s->AsRawOstream(), file_addr, addr_size);
463 if (style == DumpStyleModuleWithFileAddress && section_sp)
464 s->PutChar(']');
465 } break;
466
468 addr_t load_addr = GetLoadAddress(target);
469
470 /*
471 * MIPS:
472 * Display address in compressed form for MIPS16 or microMIPS
473 * if the address belongs to AddressClass::eCodeAlternateISA.
474 */
475 if (target) {
476 const llvm::Triple::ArchType llvm_arch =
477 target->GetArchitecture().GetMachine();
478 if (llvm_arch == llvm::Triple::mips ||
479 llvm_arch == llvm::Triple::mipsel ||
480 llvm_arch == llvm::Triple::mips64 ||
481 llvm_arch == llvm::Triple::mips64el)
482 load_addr = GetCallableLoadAddress(target);
483 }
484
485 if (load_addr == LLDB_INVALID_ADDRESS) {
486 if (fallback_style != DumpStyleInvalid)
487 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
488 return false;
489 }
490 DumpAddress(s->AsRawOstream(), load_addr, addr_size);
491 } break;
492
497 if (IsSectionOffset()) {
498 uint32_t pointer_size = 4;
499 ModuleSP module_sp(GetModule());
500 if (target)
501 pointer_size = target->GetArchitecture().GetAddressByteSize();
502 else if (module_sp)
503 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
504
505 bool showed_info = false;
506 if (section_sp) {
507 SectionType sect_type = section_sp->GetType();
508 switch (sect_type) {
509 case eSectionTypeData:
510 if (module_sp) {
511 if (Symtab *symtab = module_sp->GetSymtab()) {
512 const addr_t file_Addr = GetFileAddress();
513 Symbol *symbol =
514 symtab->FindSymbolContainingFileAddress(file_Addr);
515 if (symbol) {
516 const char *symbol_name = symbol->GetName().AsCString();
517 if (symbol_name) {
518 s->PutCString(symbol_name);
519 addr_t delta =
520 file_Addr - symbol->GetAddressRef().GetFileAddress();
521 if (delta)
522 s->Printf(" + %" PRIu64, delta);
523 showed_info = true;
524 }
525 }
526 }
527 }
528 break;
529
531 // Read the C string from memory and display it
532 showed_info = true;
533 ReadCStringFromMemory(exe_scope, *this, s);
534 break;
535
537 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
538#if VERBOSE_OUTPUT
539 s->PutCString("(char *)");
540 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
542 s->PutCString(": ");
543#endif
544 showed_info = true;
545 ReadCStringFromMemory(exe_scope, so_addr, s);
546 }
547 break;
548
550 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
551 if (target && so_addr.IsSectionOffset()) {
552 SymbolContext func_sc;
554 so_addr, eSymbolContextEverything, func_sc);
555 if (func_sc.function != nullptr || func_sc.symbol != nullptr) {
556 showed_info = true;
557#if VERBOSE_OUTPUT
558 s->PutCString("(objc_msgref *) -> { (func*)");
559 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
561#else
562 s->PutCString("{ ");
563#endif
564 Address cstr_addr(*this);
565 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
566 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true,
567 false, true, true);
568 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
569#if VERBOSE_OUTPUT
570 s->PutCString("), (char *)");
571 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
573 s->PutCString(" (");
574#else
575 s->PutCString(", ");
576#endif
577 ReadCStringFromMemory(exe_scope, so_addr, s);
578 }
579#if VERBOSE_OUTPUT
580 s->PutCString(") }");
581#else
582 s->PutCString(" }");
583#endif
584 }
585 }
586 }
587 break;
588
590 Address cfstring_data_addr(*this);
591 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
592 (2 * pointer_size));
593 if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
594 so_addr)) {
595#if VERBOSE_OUTPUT
596 s->PutCString("(CFString *) ");
597 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
599 s->PutCString(" -> @");
600#else
601 s->PutChar('@');
602#endif
603 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
604 showed_info = true;
605 }
606 } break;
607
609 // Read the 4 byte data and display it
610 showed_info = true;
611 s->PutCString("(uint32_t) ");
612 DumpUInt(exe_scope, *this, 4, s);
613 break;
614
616 // Read the 8 byte data and display it
617 showed_info = true;
618 s->PutCString("(uint64_t) ");
619 DumpUInt(exe_scope, *this, 8, s);
620 break;
621
623 // Read the 16 byte data and display it
624 showed_info = true;
625 s->PutCString("(uint128_t) ");
626 DumpUInt(exe_scope, *this, 16, s);
627 break;
628
630 // Read the pointer data and display it
631 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
632 s->PutCString("(void *)");
633 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
635
636 showed_info = true;
637 if (so_addr.IsSectionOffset()) {
638 SymbolContext pointer_sc;
639 if (target) {
641 so_addr, eSymbolContextEverything, pointer_sc);
642 if (pointer_sc.function != nullptr ||
643 pointer_sc.symbol != nullptr) {
644 s->PutCString(": ");
645 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
646 false, true, true);
647 }
648 }
649 }
650 }
651 break;
652
653 default:
654 break;
655 }
656 }
657
658 if (!showed_info) {
659 if (module_sp) {
660 SymbolContext sc;
661 module_sp->ResolveSymbolContextForAddress(
662 *this, eSymbolContextEverything, sc);
663 if (sc.function || sc.symbol) {
664 bool show_stop_context = true;
665 const bool show_module = (style == DumpStyleResolvedDescription);
666 const bool show_fullpaths = false;
667 const bool show_inlined_frames = true;
668 const bool show_function_arguments =
670 const bool show_function_name = (style != DumpStyleNoFunctionName);
671 if (sc.function == nullptr && sc.symbol != nullptr) {
672 // If we have just a symbol make sure it is in the right section
673 if (sc.symbol->ValueIsAddress()) {
674 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) {
675 // don't show the module if the symbol is a trampoline symbol
676 show_stop_context = false;
677 }
678 }
679 }
680 if (show_stop_context) {
681 // We have a function or a symbol from the same sections as this
682 // address.
683 sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
684 show_module, show_inlined_frames,
685 show_function_arguments, show_function_name);
686 } else {
687 // We found a symbol but it was in a different section so it
688 // isn't the symbol we should be showing, just show the section
689 // name + offset
690 Dump(s, exe_scope, DumpStyleSectionNameOffset);
691 }
692 }
693 }
694 }
695 } else {
696 if (fallback_style != DumpStyleInvalid)
697 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
698 return false;
699 }
700 break;
701
703 if (IsSectionOffset()) {
704 ModuleSP module_sp(GetModule());
705 if (module_sp) {
706 SymbolContext sc;
707 module_sp->ResolveSymbolContextForAddress(
708 *this, eSymbolContextEverything | eSymbolContextVariable, sc);
709 if (sc.symbol) {
710 // If we have just a symbol make sure it is in the same section as
711 // our address. If it isn't, then we might have just found the last
712 // symbol that came before the address that we are looking up that
713 // has nothing to do with our address lookup.
714 if (sc.symbol->ValueIsAddress() &&
716 sc.symbol = nullptr;
717 }
719
720 if (sc.block) {
721 bool can_create = true;
722 bool get_parent_variables = true;
723 bool stop_if_block_is_inlined_function = false;
724 VariableList variable_list;
725 addr_t file_addr = GetFileAddress();
727 can_create, get_parent_variables,
728 stop_if_block_is_inlined_function,
729 [&](Variable *var) {
730 return var && var->LocationIsValidForAddress(*this);
731 },
732 &variable_list);
733 ABISP abi =
734 ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
735 for (const VariableSP &var_sp : variable_list) {
736 s->Indent();
737 s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
738 var_sp->GetID(), var_sp->GetName().GetCString());
739 Type *type = var_sp->GetType();
740 if (type)
741 s->Printf(", type = \"%s\"", type->GetName().GetCString());
742 else
743 s->PutCString(", type = <unknown>");
744 s->PutCString(", valid ranges = ");
745 if (var_sp->GetScopeRange().IsEmpty())
746 s->PutCString("<block>");
747 else if (all_ranges) {
748 for (auto range : var_sp->GetScopeRange())
749 DumpAddressRange(s->AsRawOstream(), range.GetRangeBase(),
750 range.GetRangeEnd(), addr_size);
751 } else if (auto *range =
752 var_sp->GetScopeRange().FindEntryThatContains(
753 file_addr))
754 DumpAddressRange(s->AsRawOstream(), range->GetRangeBase(),
755 range->GetRangeEnd(), addr_size);
756 s->PutCString(", location = ");
757 var_sp->DumpLocations(s, all_ranges ? LLDB_INVALID_ADDRESS : *this);
758 s->PutCString(", decl = ");
759 var_sp->GetDeclaration().DumpStopContext(s, false);
760 s->EOL();
761 }
762 }
763 }
764 } else {
765 if (fallback_style != DumpStyleInvalid)
766 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
767 return false;
768 }
769 break;
770
772 Process *process = exe_ctx.GetProcessPtr();
773 if (process) {
774 addr_t load_addr = GetLoadAddress(target);
775 if (load_addr != LLDB_INVALID_ADDRESS) {
776 Status memory_error;
777 addr_t dereferenced_load_addr =
778 process->ReadPointerFromMemory(load_addr, memory_error);
779 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) {
780 Address dereferenced_addr;
781 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr,
782 target)) {
783 StreamString strm;
784 if (dereferenced_addr.Dump(&strm, exe_scope,
786 DumpStyleInvalid, addr_size)) {
787 DumpAddress(s->AsRawOstream(), dereferenced_load_addr, addr_size,
788 " -> ", " ");
789 s->Write(strm.GetString().data(), strm.GetSize());
790 return true;
791 }
792 }
793 }
794 }
795 }
796 if (fallback_style != DumpStyleInvalid)
797 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
798 return false;
799 } break;
800 }
801
802 return true;
803}
804
806 if (GetSection())
807 return false;
809}
810
812 lldb::SectionWP empty_section_wp;
813
814 // If either call to "std::weak_ptr::owner_before(...) value returns true,
815 // this indicates that m_section_wp once contained (possibly still does) a
816 // reference to a valid shared pointer. This helps us know if we had a valid
817 // reference to a section which is now invalid because the module it was in
818 // was unloaded/deleted, or if the address doesn't have a valid reference to
819 // a section.
820 return empty_section_wp.owner_before(m_section_wp) ||
821 m_section_wp.owner_before(empty_section_wp);
822}
823
826 SymbolContextItem resolve_scope) const {
827 sc->Clear(false);
828 // Absolute addresses don't have enough information to reconstruct even their
829 // target.
830
831 SectionSP section_sp(GetSection());
832 if (section_sp) {
833 ModuleSP module_sp(section_sp->GetModule());
834 if (module_sp) {
835 sc->module_sp = module_sp;
836 if (sc->module_sp)
837 return sc->module_sp->ResolveSymbolContextForAddress(
838 *this, resolve_scope, *sc);
839 }
840 }
841 return 0;
842}
843
845 SectionSP section_sp(GetSection());
846 if (section_sp)
847 return section_sp->GetModule();
848 return ModuleSP();
849}
850
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,
858 eSymbolContextCompUnit, sc);
859 return sc.comp_unit;
860 }
861 }
862 return nullptr;
863}
864
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 eSymbolContextFunction, sc);
873 return sc.function;
874 }
875 }
876 return nullptr;
877}
878
880 SectionSP section_sp(GetSection());
881 if (section_sp) {
882 SymbolContext sc;
883 sc.module_sp = section_sp->GetModule();
884 if (sc.module_sp) {
885 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
886 sc);
887 return sc.block;
888 }
889 }
890 return nullptr;
891}
892
894 SectionSP section_sp(GetSection());
895 if (section_sp) {
896 SymbolContext sc;
897 sc.module_sp = section_sp->GetModule();
898 if (sc.module_sp) {
899 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
900 sc);
901 return sc.symbol;
902 }
903 }
904 return nullptr;
905}
906
908 SectionSP section_sp(GetSection());
909 if (section_sp) {
910 SymbolContext sc;
911 sc.module_sp = section_sp->GetModule();
912 if (sc.module_sp) {
913 sc.module_sp->ResolveSymbolContextForAddress(*this,
914 eSymbolContextLineEntry, sc);
915 if (sc.line_entry.IsValid()) {
916 line_entry = sc.line_entry;
917 return true;
918 }
919 }
920 }
921 line_entry.Clear();
922 return false;
923}
924
926 addr_t a_file_addr = a.GetFileAddress();
927 addr_t b_file_addr = b.GetFileAddress();
928 if (a_file_addr < b_file_addr)
929 return -1;
930 if (a_file_addr > b_file_addr)
931 return +1;
932 return 0;
933}
934
936 Target *target) {
937 assert(target != nullptr);
938 addr_t a_load_addr = a.GetLoadAddress(target);
939 addr_t b_load_addr = b.GetLoadAddress(target);
940 if (a_load_addr < b_load_addr)
941 return -1;
942 if (a_load_addr > b_load_addr)
943 return +1;
944 return 0;
945}
946
948 ModuleSP a_module_sp(a.GetModule());
949 ModuleSP b_module_sp(b.GetModule());
950 Module *a_module = a_module_sp.get();
951 Module *b_module = b_module_sp.get();
952 if (a_module < b_module)
953 return -1;
954 if (a_module > b_module)
955 return +1;
956 // Modules are the same, just compare the file address since they should be
957 // unique
958 addr_t a_file_addr = a.GetFileAddress();
959 addr_t b_file_addr = b.GetFileAddress();
960 if (a_file_addr < b_file_addr)
961 return -1;
962 if (a_file_addr > b_file_addr)
963 return +1;
964 return 0;
965}
966
967size_t Address::MemorySize() const {
968 // Noting special for the memory size of a single Address object, it is just
969 // the size of itself.
970 return sizeof(Address);
971}
972
973// NOTE: Be careful using this operator. It can correctly compare two
974// addresses from the same Module correctly. It can't compare two addresses
975// from different modules in any meaningful way, but it will compare the module
976// pointers.
977//
978// To sum things up:
979// - works great for addresses within the same module - it works for addresses
980// across multiple modules, but don't expect the
981// address results to make much sense
982//
983// This basically lets Address objects be used in ordered collection classes.
984
985bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
986 ModuleSP lhs_module_sp(lhs.GetModule());
987 ModuleSP rhs_module_sp(rhs.GetModule());
988 Module *lhs_module = lhs_module_sp.get();
989 Module *rhs_module = rhs_module_sp.get();
990 if (lhs_module == rhs_module) {
991 // Addresses are in the same module, just compare the file addresses
992 return lhs.GetFileAddress() < rhs.GetFileAddress();
993 } else {
994 // The addresses are from different modules, just use the module pointer
995 // value to get consistent ordering
996 return lhs_module < rhs_module;
997 }
998}
999
1000bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
1001 ModuleSP lhs_module_sp(lhs.GetModule());
1002 ModuleSP rhs_module_sp(rhs.GetModule());
1003 Module *lhs_module = lhs_module_sp.get();
1004 Module *rhs_module = rhs_module_sp.get();
1005 if (lhs_module == rhs_module) {
1006 // Addresses are in the same module, just compare the file addresses
1007 return lhs.GetFileAddress() > rhs.GetFileAddress();
1008 } else {
1009 // The addresses are from different modules, just use the module pointer
1010 // value to get consistent ordering
1011 return lhs_module > rhs_module;
1012 }
1013}
1014
1015// The operator == checks for exact equality only (same section, same offset)
1016bool lldb_private::operator==(const Address &a, const Address &rhs) {
1017 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection();
1018}
1019
1020// The operator != checks for exact inequality only (differing section, or
1021// different offset)
1022bool lldb_private::operator!=(const Address &a, const Address &rhs) {
1023 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
1024}
1025
1027 ModuleSP module_sp(GetModule());
1028 if (module_sp) {
1029 ObjectFile *obj_file = module_sp->GetObjectFile();
1030 if (obj_file) {
1031 // Give the symbol file a chance to add to the unified section list
1032 // and to the symtab.
1033 module_sp->GetSymtab();
1034 return obj_file->GetAddressClass(GetFileAddress());
1035 }
1036 }
1038}
1039
1041 bool allow_section_end) {
1042 if (target && target->GetSectionLoadList().ResolveLoadAddress(
1043 load_addr, *this, allow_section_end))
1044 return true;
1045 m_section_wp.reset();
1046 m_offset = load_addr;
1047 return false;
1048}
static bool DumpUInt(ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream *strm)
Definition: Address.cpp:162
static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope, const Address &address, Stream *strm)
Definition: Address.cpp:190
static size_t ReadBytes(ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len)
Definition: Address.cpp:61
static bool ReadAddress(ExecutionContextScope *exe_scope, const Address &address, uint32_t pointer_size, Address &deref_so_addr)
Definition: Address.cpp:125
static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, bool &success)
Definition: Address.cpp:101
static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope, const Address &address, ByteOrder &byte_order, uint32_t &addr_size)
Definition: Address.cpp:76
static llvm::raw_ostream & error(Stream &strm)
static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch)
Definition: ABI.cpp:27
A section + offset based address range class.
Definition: AddressRange.h:25
void Clear()
Clear the object's state.
A section + offset based address class.
Definition: Address.h:59
static int CompareFileAddress(const Address &lhs, const Address &rhs)
Compare two Address objects.
Definition: Address.cpp:925
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:311
lldb::SectionWP m_section_wp
The section for the address, can be NULL.
Definition: Address.h:502
bool ResolveAddressUsingFileSections(lldb::addr_t addr, const SectionList *sections)
Resolve a file virtual address using a section list.
Definition: Address.cpp:248
lldb::addr_t GetCallableLoadAddress(Target *target, bool is_indirect=false) const
Get the load address as a callable code load address.
Definition: Address.cpp:336
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition: Address.cpp:1040
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:429
bool SectionWasDeleted() const
Definition: Address.cpp:805
static int CompareLoadAddress(const Address &lhs, const Address &rhs, Target *target)
Definition: Address.cpp:935
uint32_t CalculateSymbolContext(SymbolContext *sc, lldb::SymbolContextItem resolve_scope=lldb::eSymbolContextEverything) const
Reconstruct a symbol context from an address.
Definition: Address.cpp:825
const Address & operator=(const Address &rhs)
Assignment operator.
Definition: Address.cpp:240
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
Definition: Address.cpp:368
lldb::addr_t m_offset
Offset into section if m_section_wp is valid...
Definition: Address.h:503
bool SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target, AddressClass addr_class=AddressClass::eInvalid, bool allow_section_end=false)
Definition: Address.cpp:379
bool ResolveFunctionScope(lldb_private::SymbolContext &sym_ctx, lldb_private::AddressRange *addr_range_ptr=nullptr)
Resolve this address to its containing function and optionally get that function's address range.
Definition: Address.cpp:266
Function * CalculateSymbolContextFunction() const
Definition: Address.cpp:865
static int CompareModulePointerAndOffset(const Address &lhs, const Address &rhs)
Definition: Address.cpp:947
bool SectionWasDeletedPrivate() const
Definition: Address.cpp:811
size_t MemorySize() const
Get the memory cost of this object.
Definition: Address.cpp:967
void SetRawAddress(lldb::addr_t addr)
Definition: Address.h:444
DumpStyle
Dump styles allow the Address::Dump(Stream *,DumpStyle) const function to display Address contents in...
Definition: Address.h:63
@ DumpStyleFileAddress
Display as the file address (if any).
Definition: Address.h:84
@ DumpStyleSectionNameOffset
Display as the section name + offset.
Definition: Address.h:71
@ DumpStyleNoFunctionName
Elide the function name; display an offset into the current function.
Definition: Address.h:106
@ DumpStyleResolvedDescriptionNoFunctionArguments
Definition: Address.h:103
@ DumpStyleDetailedSymbolContext
Detailed symbol context information for an address for all symbol context members.
Definition: Address.h:109
@ DumpStyleInvalid
Invalid dump style.
Definition: Address.h:65
@ DumpStyleModuleWithFileAddress
Display as the file address with the module name prepended (if any).
Definition: Address.h:90
@ DumpStyleSectionPointerOffset
Display as the section pointer + offset (debug output).
Definition: Address.h:77
@ DumpStyleResolvedDescription
Display the details about what an address resolves to.
Definition: Address.h:101
@ DumpStyleLoadAddress
Display as the load address (if resolved).
Definition: Address.h:96
@ DumpStyleResolvedDescriptionNoModule
Definition: Address.h:102
@ DumpStyleResolvedPointerDescription
Dereference a pointer at the current address and then lookup the dereferenced address using DumpStyle...
Definition: Address.h:112
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false) const
Dump a description of this object to a Stream.
Definition: Address.cpp:406
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:291
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:319
bool GetDescription(Stream &s, Target &target, lldb::DescriptionLevel level) const
Write a description of this object to a Stream.
Definition: Address.cpp:393
bool IsSectionOffset() const
Check if an address is section offset.
Definition: Address.h:332
bool CalculateSymbolContextLineEntry(LineEntry &line_entry) const
Definition: Address.cpp:907
CompileUnit * CalculateSymbolContextCompileUnit() const
Definition: Address.cpp:851
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:438
Address()=default
Default constructor.
Block * CalculateSymbolContextBlock() const
Definition: Address.cpp:879
bool SetCallableLoadAddress(lldb::addr_t load_addr, Target *target)
Definition: Address.cpp:359
AddressClass GetAddressClass() const
Definition: Address.cpp:1026
Symbol * CalculateSymbolContextSymbol() const
Definition: Address.cpp:893
lldb::ModuleSP CalculateSymbolContextModule() const
Definition: Address.cpp:844
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:691
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:683
A class that describes a single lexical block.
Definition: Block.h:41
uint32_t AppendVariables(bool can_create, bool get_parent_variables, bool stop_if_block_is_inlined_function, const std::function< bool(Variable *)> &filter, VariableList *variable_list)
Appends the variables from this block, and optionally from all parent blocks, to variable_list.
Definition: Block.cpp:443
A class that describes a compilation unit.
Definition: CompileUnit.h:41
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:195
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:218
An data extractor class.
Definition: DataExtractor.h:48
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
virtual void CalculateExecutionContext(ExecutionContext &exe_ctx)=0
Reconstruct the object's execution context into sc.
virtual lldb::TargetSP CalculateTarget()=0
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
A class that describes a function.
Definition: Function.h:409
uint32_t ResolveSymbolContextForAddress(const Address &so_addr, lldb::SymbolContextItem resolve_scope, SymbolContext &sc) const
Resolve the symbol context for the given address. (const Address&,uint32_t,SymbolContext&)
Definition: ModuleList.cpp:671
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:43
Symtab * GetSymtab()
Gets the symbol table for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:725
virtual AddressClass GetAddressClass(lldb::addr_t file_addr)
Get the address type given a file address in an object file.
Definition: ObjectFile.cpp:295
A plug-in interface definition class for debugging a process.
Definition: Process.h:333
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2092
lldb::SectionSP FindSectionContainingFileAddress(lldb::addr_t addr, uint32_t depth=UINT32_MAX) const
Definition: Section.cpp:605
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, bool allow_section_end=false) const
An error handling class.
Definition: Status.h:44
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:101
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
size_t PutChar(char ch)
Definition: Stream.cpp:104
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
bool DumpStopContext(Stream *s, ExecutionContextScope *exe_scope, const Address &so_addr, bool show_fullpaths, bool show_module, bool show_inlined_frames, bool show_function_arguments, bool show_function_name) const
Dump the stop context in this object to a Stream.
Function * function
The Function for a given query.
Block * block
The Block for a given query.
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
void Clear(bool clear_target)
Clear the object's state.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Symbol * symbol
The Symbol for a given query.
LineEntry line_entry
The LineEntry for a given query.
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) const
bool ValueIsAddress() const
Definition: Symbol.cpp:167
Address & GetAddressRef()
Definition: Symbol.h:71
ConstString GetName() const
Definition: Symbol.cpp:544
lldb::addr_t GetCallableLoadAddress(lldb::addr_t load_addr, AddressClass addr_class=AddressClass::eInvalid) const
Get load_addr as a callable code load address for this target.
Definition: Target.cpp:2701
lldb::addr_t GetOpcodeLoadAddress(lldb::addr_t load_addr, AddressClass addr_class=AddressClass::eInvalid) const
Get load_addr as an opcode for this target.
Definition: Target.cpp:2709
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1109
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:219
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:954
const ArchSpec & GetArchitecture() const
Definition: Target.h:996
ConstString GetName()
Definition: Type.cpp:303
bool LocationIsValidForAddress(const Address &address)
Definition: Variable.cpp:236
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
void DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address range to this stream.
Definition: Stream.cpp:93
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1022
bool operator>(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1000
void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:81
lldb::offset_t DumpDataExtractor(const DataExtractor &DE, Stream *s, lldb::offset_t offset, lldb::Format item_format, size_t item_byte_size, size_t item_count, size_t num_per_line, uint64_t base_addr, uint32_t item_bit_size, uint32_t item_bit_offset, ExecutionContextScope *exe_scope=nullptr, bool show_memory_tags=false)
Dumps item_count objects into the stream s.
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1016
bool operator<(const Address &lhs, const Address &rhs)
Definition: Address.cpp:985
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
uint64_t offset_t
Definition: lldb-types.h:83
ByteOrder
Byte ordering definitions.
@ eByteOrderInvalid
uint64_t addr_t
Definition: lldb-types.h:79
@ eSectionTypeData
@ eSectionTypeDataObjCCFStrings
Objective-C const CFString/NSString objects.
@ eSectionTypeData16
@ eSectionTypeDataObjCMessageRefs
Pointer to function pointer + selector.
@ eSectionTypeData4
@ eSectionTypeDataCStringPointers
Pointers to C string data.
@ eSectionTypeData8
@ eSectionTypeDataPointers
@ eSectionTypeDataCString
Inlined C string data.
A line table entry class.
Definition: LineEntry.h:20
uint16_t column
The column number of the source line, or zero if there is no column information.
Definition: LineEntry.h:146
void Clear()
Clear the object's state.
Definition: LineEntry.cpp:33
bool IsValid() const
Check if a line entry object is valid.
Definition: LineEntry.cpp:46
FileSpec file
The source file, possibly mapped by the target.source-map setting.
Definition: LineEntry.h:140
uint32_t line
The source line number, or zero if there is no line number information.
Definition: LineEntry.h:143