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