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