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