blob: 2ea3bb17312193cbc8da67c38dc8aea97d86463f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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/ObjectFile.h"
Greg Claytonc749eb82011-07-11 05:12:02 +000014#include "lldb/Symbol/Variable.h"
15#include "lldb/Symbol/VariableList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000016#include "lldb/Target/ExecutionContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
19
Greg Clayton3f5c08f2011-05-18 22:01:49 +000020#include "llvm/ADT/Triple.h"
21
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022using namespace lldb;
23using namespace lldb_private;
24
25static size_t
26ReadBytes (ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len)
27{
28 if (exe_scope == NULL)
29 return 0;
30
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031 Target *target = exe_scope->CalculateTarget();
32 if (target)
33 {
34 Error error;
Greg Claytondb598232011-01-07 01:57:07 +000035 bool prefer_file_cache = false;
36 return target->ReadMemory (address, prefer_file_cache, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037 }
38 return 0;
39}
40
41static bool
42GetByteOrderAndAddressSize (ExecutionContextScope *exe_scope, const Address &address, ByteOrder& byte_order, uint32_t& addr_size)
43{
44 byte_order = eByteOrderInvalid;
45 addr_size = 0;
46 if (exe_scope == NULL)
47 return false;
48
Greg Clayton514487e2011-02-15 21:59:32 +000049 Target *target = exe_scope->CalculateTarget();
50 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 {
Greg Clayton514487e2011-02-15 21:59:32 +000052 byte_order = target->GetArchitecture().GetByteOrder();
53 addr_size = target->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 }
55
56 if (byte_order == eByteOrderInvalid || addr_size == 0)
57 {
58 Module *module = address.GetModule();
59 if (module)
60 {
Greg Clayton514487e2011-02-15 21:59:32 +000061 byte_order = module->GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 addr_size = module->GetArchitecture().GetAddressByteSize();
63 }
64 }
65 return byte_order != eByteOrderInvalid && addr_size != 0;
66}
67
68static uint64_t
69ReadUIntMax64 (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, bool &success)
70{
71 uint64_t uval64 = 0;
72 if (exe_scope == NULL || byte_size > sizeof(uint64_t))
73 {
74 success = false;
75 return 0;
76 }
Johnny Chen40e35922011-08-25 17:40:39 +000077 uint64_t buf = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
79 success = ReadBytes (exe_scope, address, &buf, byte_size) == byte_size;
80 if (success)
81 {
82 ByteOrder byte_order = eByteOrderInvalid;
83 uint32_t addr_size = 0;
84 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size))
85 {
86 DataExtractor data (&buf, sizeof(buf), byte_order, addr_size);
87 uint32_t offset = 0;
88 uval64 = data.GetU64(&offset);
89 }
90 else
91 success = false;
92 }
93 return uval64;
94}
95
96static bool
97ReadAddress (ExecutionContextScope *exe_scope, const Address &address, uint32_t pointer_size, Address &deref_so_addr)
98{
99 if (exe_scope == NULL)
100 return false;
101
102
103 bool success = false;
104 addr_t deref_addr = ReadUIntMax64 (exe_scope, address, pointer_size, success);
105 if (success)
106 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000107 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000108 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Claytonf5e56de2010-09-14 23:36:40 +0000109 // If we have any sections that are loaded, try and resolve using the
110 // section load list
Greg Claytonc14ee322011-09-22 04:58:26 +0000111 Target *target = exe_ctx.GetTargetPtr();
112 if (target && !target->GetSectionLoadList().IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000114 if (target->GetSectionLoadList().ResolveLoadAddress (deref_addr, deref_so_addr))
Greg Claytonf5e56de2010-09-14 23:36:40 +0000115 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 }
117 else
118 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000119 // If we were not running, yet able to read an integer, we must
120 // have a module
121 Module *module = address.GetModule();
122 assert (module);
123 if (module->ResolveFileAddress(deref_addr, deref_so_addr))
124 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000126
127 // We couldn't make "deref_addr" into a section offset value, but we were
128 // able to read the address, so we return a section offset address with
129 // no section and "deref_addr" as the offset (address).
130 deref_so_addr.SetSection(NULL);
131 deref_so_addr.SetOffset(deref_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 return true;
133 }
134 return false;
135}
136
137static bool
138DumpUInt (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream* strm)
139{
Greg Clayton471b31c2010-07-20 22:52:08 +0000140 if (exe_scope == NULL || byte_size == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 return 0;
142 std::vector<uint8_t> buf(byte_size, 0);
143
144 if (ReadBytes (exe_scope, address, &buf[0], buf.size()) == buf.size())
145 {
146 ByteOrder byte_order = eByteOrderInvalid;
147 uint32_t addr_size = 0;
148 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size))
149 {
Greg Clayton471b31c2010-07-20 22:52:08 +0000150 DataExtractor data (&buf.front(), buf.size(), byte_order, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151
152 data.Dump (strm,
153 0, // Start offset in "data"
154 eFormatHex, // Print as characters
155 buf.size(), // Size of item
156 1, // Items count
157 UINT32_MAX, // num per line
158 LLDB_INVALID_ADDRESS,// base address
159 0, // bitfield bit size
160 0); // bitfield bit offset
161
162 return true;
163 }
164 }
165 return false;
166}
167
168
169static size_t
170ReadCStringFromMemory (ExecutionContextScope *exe_scope, const Address &address, Stream *strm)
171{
172 if (exe_scope == NULL)
173 return 0;
174 const size_t k_buf_len = 256;
175 char buf[k_buf_len+1];
176 buf[k_buf_len] = '\0'; // NULL terminate
177
Greg Clayton710dd5a2011-01-08 20:28:42 +0000178 // Byte order and address size don't matter for C string dumping..
Greg Clayton7fb56d02011-02-01 01:31:41 +0000179 DataExtractor data (buf, sizeof(buf), lldb::endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 size_t total_len = 0;
181 size_t bytes_read;
182 Address curr_address(address);
183 strm->PutChar ('"');
184 while ((bytes_read = ReadBytes (exe_scope, curr_address, buf, k_buf_len)) > 0)
185 {
186 size_t len = strlen(buf);
187 if (len == 0)
188 break;
189 if (len > bytes_read)
190 len = bytes_read;
191
192 data.Dump (strm,
193 0, // Start offset in "data"
194 eFormatChar, // Print as characters
195 1, // Size of item (1 byte for a char!)
196 len, // How many bytes to print?
197 UINT32_MAX, // num per line
198 LLDB_INVALID_ADDRESS,// base address
199 0, // bitfield bit size
200
201 0); // bitfield bit offset
202
203 total_len += bytes_read;
204
205 if (len < k_buf_len)
206 break;
207 curr_address.SetOffset (curr_address.GetOffset() + bytes_read);
208 }
209 strm->PutChar ('"');
210 return total_len;
211}
212
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213Address::Address (addr_t address, const SectionList * sections) :
214 m_section (NULL),
215 m_offset (LLDB_INVALID_ADDRESS)
216{
217 ResolveAddressUsingFileSections(address, sections);
218}
219
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220const Address&
221Address::operator= (const Address& rhs)
222{
223 if (this != &rhs)
224 {
225 m_section = rhs.m_section;
226 m_offset = rhs.m_offset;
227 }
228 return *this;
229}
230
231bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232Address::ResolveAddressUsingFileSections (addr_t addr, const SectionList *sections)
233{
234 if (sections)
235 m_section = sections->FindSectionContainingFileAddress(addr).get();
236 else
237 m_section = NULL;
238
239 if (m_section != NULL)
240 {
241 assert( m_section->ContainsFileAddress(addr) );
242 m_offset = addr - m_section->GetFileAddress();
243 return true; // Successfully transformed addr into a section offset address
244 }
245
246 m_offset = addr;
247 return false; // Failed to resolve this address to a section offset value
248}
249
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250Module *
251Address::GetModule () const
252{
253 if (m_section)
254 return m_section->GetModule();
255 return NULL;
256}
257
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258addr_t
259Address::GetFileAddress () const
260{
261 if (m_section != NULL)
262 {
263 addr_t sect_file_addr = m_section->GetFileAddress();
264 if (sect_file_addr == LLDB_INVALID_ADDRESS)
265 {
266 // Section isn't resolved, we can't return a valid file address
267 return LLDB_INVALID_ADDRESS;
268 }
269 // We have a valid file range, so we can return the file based
270 // address by adding the file base address to our offset
271 return sect_file_addr + m_offset;
272 }
273 // No section, we just return the offset since it is the value in this case
274 return m_offset;
275}
276
277addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000278Address::GetLoadAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000280 if (m_section == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000282 // No section, we just return the offset since it is the value in this case
283 return m_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000285
286 if (target)
287 {
288 addr_t sect_load_addr = m_section->GetLoadBaseAddress (target);
289
290 if (sect_load_addr != LLDB_INVALID_ADDRESS)
291 {
292 // We have a valid file range, so we can return the file based
293 // address by adding the file base address to our offset
294 return sect_load_addr + m_offset;
295 }
296 }
297 // The section isn't resolved or no process was supplied so we can't
298 // return a valid file address.
299 return LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300}
301
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000302addr_t
303Address::GetCallableLoadAddress (Target *target) const
304{
305 addr_t code_addr = GetLoadAddress (target);
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000306
307 if (target)
308 return target->GetCallableLoadAddress (code_addr, GetAddressClass());
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000309 return code_addr;
310}
311
Greg Claytoncff851a2011-05-22 04:32:55 +0000312bool
313Address::SetCallableLoadAddress (lldb::addr_t load_addr, Target *target)
314{
315 if (SetLoadAddress (load_addr, target))
316 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000317 if (target)
318 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
Greg Claytoncff851a2011-05-22 04:32:55 +0000319 return true;
320 }
321 return false;
322}
323
Greg Clayton92bb12c2011-05-19 18:17:41 +0000324addr_t
325Address::GetOpcodeLoadAddress (Target *target) const
326{
327 addr_t code_addr = GetLoadAddress (target);
Greg Clayton92bb12c2011-05-19 18:17:41 +0000328 if (code_addr != LLDB_INVALID_ADDRESS)
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000329 code_addr = target->GetOpcodeLoadAddress (code_addr, GetAddressClass());
Greg Clayton92bb12c2011-05-19 18:17:41 +0000330 return code_addr;
331}
332
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333bool
Greg Claytoncff851a2011-05-22 04:32:55 +0000334Address::SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target)
335{
336 if (SetLoadAddress (load_addr, target))
337 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000338 if (target)
339 m_offset = target->GetOpcodeLoadAddress (m_offset, GetAddressClass());
Greg Claytoncff851a2011-05-22 04:32:55 +0000340 return true;
341 }
342 return false;
343}
344
345bool
Greg Claytondda4f7b2010-06-30 23:03:03 +0000346Address::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347{
348 // If the section was NULL, only load address is going to work.
349 if (m_section == NULL)
350 style = DumpStyleLoadAddress;
351
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000352 Target *target = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353 Process *process = NULL;
354 if (exe_scope)
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000355 {
356 target = exe_scope->CalculateTarget();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 process = exe_scope->CalculateProcess();
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000358 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000359 // If addr_byte_size is UINT32_MAX, then determine the correct address
360 // byte size for the process or default to the size of addr_t
361 if (addr_size == UINT32_MAX)
362 {
363 if (process)
Greg Clayton514487e2011-02-15 21:59:32 +0000364 addr_size = target->GetArchitecture().GetAddressByteSize ();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000365 else
366 addr_size = sizeof(addr_t);
367 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
Greg Claytonc9800662010-09-10 01:30:46 +0000369 Address so_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 switch (style)
371 {
Greg Claytonc982c762010-07-09 20:39:50 +0000372 case DumpStyleInvalid:
373 return false;
374
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 case DumpStyleSectionNameOffset:
376 if (m_section != NULL)
377 {
378 m_section->DumpName(s);
379 s->Printf (" + %llu", m_offset);
380 }
381 else
382 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000383 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 }
385 break;
386
387 case DumpStyleSectionPointerOffset:
Jason Molendafd54b362011-09-20 21:44:10 +0000388 s->Printf("(Section *)%p + ", m_section);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000389 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 break;
391
392 case DumpStyleModuleWithFileAddress:
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000393 if (m_section)
394 s->Printf("%s[", m_section->GetModule()->GetFileSpec().GetFilename().AsCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 // Fall through
396 case DumpStyleFileAddress:
397 {
398 addr_t file_addr = GetFileAddress();
399 if (file_addr == LLDB_INVALID_ADDRESS)
400 {
401 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000402 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 return false;
404 }
405 s->Address (file_addr, addr_size);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000406 if (style == DumpStyleModuleWithFileAddress && m_section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 s->PutChar(']');
408 }
409 break;
410
411 case DumpStyleLoadAddress:
412 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000413 addr_t load_addr = GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 if (load_addr == LLDB_INVALID_ADDRESS)
415 {
416 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000417 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 return false;
419 }
420 s->Address (load_addr, addr_size);
421 }
422 break;
423
424 case DumpStyleResolvedDescription:
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000425 case DumpStyleResolvedDescriptionNoModule:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 if (IsSectionOffset())
427 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000428 AddressType addr_type = eAddressTypeLoad;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000429 addr_t addr = GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 if (addr == LLDB_INVALID_ADDRESS)
431 {
432 addr = GetFileAddress();
433 addr_type = eAddressTypeFile;
434 }
435
436 uint32_t pointer_size = 4;
Greg Claytonc9800662010-09-10 01:30:46 +0000437 Module *module = GetModule();
Greg Clayton514487e2011-02-15 21:59:32 +0000438 if (target)
439 pointer_size = target->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 else if (module)
441 pointer_size = module->GetArchitecture().GetAddressByteSize();
442
443 bool showed_info = false;
444 const Section *section = GetSection();
445 if (section)
446 {
Greg Clayton70e33eb2010-07-21 21:49:46 +0000447 SectionType sect_type = section->GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 switch (sect_type)
449 {
Greg Clayton89411422010-10-08 00:21:05 +0000450 case eSectionTypeData:
451 if (module)
452 {
453 ObjectFile *objfile = module->GetObjectFile();
454 if (objfile)
455 {
456 Symtab *symtab = objfile->GetSymtab();
457 if (symtab)
458 {
459 const addr_t file_Addr = GetFileAddress();
460 Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr);
461 if (symbol)
462 {
463 const char *symbol_name = symbol->GetName().AsCString();
464 if (symbol_name)
465 {
466 s->PutCString(symbol_name);
467 addr_t delta = file_Addr - symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress();
468 if (delta)
469 s->Printf(" + %llu", delta);
470 showed_info = true;
471 }
472 }
473 }
474 }
475 }
476 break;
477
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 case eSectionTypeDataCString:
479 // Read the C string from memory and display it
480 showed_info = true;
481 ReadCStringFromMemory (exe_scope, *this, s);
482 break;
483
484 case eSectionTypeDataCStringPointers:
485 {
486 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
487 {
488#if VERBOSE_OUTPUT
489 s->PutCString("(char *)");
490 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
491 s->PutCString(": ");
492#endif
493 showed_info = true;
494 ReadCStringFromMemory (exe_scope, so_addr, s);
495 }
496 }
497 break;
498
499 case eSectionTypeDataObjCMessageRefs:
500 {
501 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
502 {
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000503 if (target && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504 {
Greg Claytonc9800662010-09-10 01:30:46 +0000505 SymbolContext func_sc;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000506 target->GetImages().ResolveSymbolContextForAddress (so_addr,
507 eSymbolContextEverything,
508 func_sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 if (func_sc.function || func_sc.symbol)
510 {
511 showed_info = true;
512#if VERBOSE_OUTPUT
513 s->PutCString ("(objc_msgref *) -> { (func*)");
514 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
515#else
516 s->PutCString ("{ ");
517#endif
518 Address cstr_addr(*this);
519 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
Greg Clayton6dadd502010-09-02 21:44:10 +0000520 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 if (ReadAddress (exe_scope, cstr_addr, pointer_size, so_addr))
522 {
523#if VERBOSE_OUTPUT
524 s->PutCString("), (char *)");
525 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
526 s->PutCString(" (");
527#else
528 s->PutCString(", ");
529#endif
530 ReadCStringFromMemory (exe_scope, so_addr, s);
531 }
532#if VERBOSE_OUTPUT
533 s->PutCString(") }");
534#else
535 s->PutCString(" }");
536#endif
537 }
538 }
539 }
540 }
541 break;
542
543 case eSectionTypeDataObjCCFStrings:
544 {
545 Address cfstring_data_addr(*this);
546 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size));
547 if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr))
548 {
549#if VERBOSE_OUTPUT
550 s->PutCString("(CFString *) ");
551 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
552 s->PutCString(" -> @");
553#else
554 s->PutChar('@');
555#endif
556 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
557 showed_info = true;
558 }
559 }
560 break;
561
562 case eSectionTypeData4:
563 // Read the 4 byte data and display it
564 showed_info = true;
565 s->PutCString("(uint32_t) ");
566 DumpUInt (exe_scope, *this, 4, s);
567 break;
568
569 case eSectionTypeData8:
570 // Read the 8 byte data and display it
571 showed_info = true;
572 s->PutCString("(uint64_t) ");
573 DumpUInt (exe_scope, *this, 8, s);
574 break;
575
576 case eSectionTypeData16:
577 // Read the 16 byte data and display it
578 showed_info = true;
579 s->PutCString("(uint128_t) ");
580 DumpUInt (exe_scope, *this, 16, s);
581 break;
582
583 case eSectionTypeDataPointers:
584 // Read the pointer data and display it
585 {
586 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
587 {
588 s->PutCString ("(void *)");
589 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
590
591 showed_info = true;
592 if (so_addr.IsSectionOffset())
593 {
Greg Claytonc9800662010-09-10 01:30:46 +0000594 SymbolContext pointer_sc;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000595 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000597 target->GetImages().ResolveSymbolContextForAddress (so_addr,
598 eSymbolContextEverything,
599 pointer_sc);
600 if (pointer_sc.function || pointer_sc.symbol)
601 {
602 s->PutCString(": ");
Greg Clayton6dadd502010-09-02 21:44:10 +0000603 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000604 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 }
606 }
607 }
608 }
609 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000610
611 default:
612 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 }
614 }
615
616 if (!showed_info)
617 {
618 if (module)
619 {
Greg Claytonc9800662010-09-10 01:30:46 +0000620 SymbolContext sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 module->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc);
622 if (sc.function || sc.symbol)
623 {
624 bool show_stop_context = true;
Greg Clayton8dc0a982010-09-07 21:56:53 +0000625 const bool show_module = (style == DumpStyleResolvedDescription);
626 const bool show_fullpaths = false;
Greg Clayton513c26c2011-01-29 07:10:55 +0000627 const bool show_inlined_frames = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 if (sc.function == NULL && sc.symbol != NULL)
629 {
630 // If we have just a symbol make sure it is in the right section
631 if (sc.symbol->GetAddressRangePtr())
632 {
633 if (sc.symbol->GetAddressRangePtr()->GetBaseAddress().GetSection() != GetSection())
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000634 {
635 // don't show the module if the symbol is a trampoline symbol
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636 show_stop_context = false;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000637 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 }
639 }
640 if (show_stop_context)
641 {
642 // We have a function or a symbol from the same
643 // sections as this address.
Greg Clayton8dc0a982010-09-07 21:56:53 +0000644 sc.DumpStopContext (s,
645 exe_scope,
646 *this,
647 show_fullpaths,
648 show_module,
649 show_inlined_frames);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 }
651 else
652 {
653 // We found a symbol but it was in a different
654 // section so it isn't the symbol we should be
655 // showing, just show the section name + offset
656 Dump (s, exe_scope, DumpStyleSectionNameOffset);
657 }
658 }
659 }
660 }
661 }
662 else
663 {
664 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000665 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 return false;
667 }
668 break;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000669
670 case DumpStyleDetailedSymbolContext:
671 if (IsSectionOffset())
672 {
Greg Claytonc9800662010-09-10 01:30:46 +0000673 Module *module = GetModule();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000674 if (module)
675 {
Greg Claytonc9800662010-09-10 01:30:46 +0000676 SymbolContext sc;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000677 module->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc);
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000678 if (sc.symbol)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000679 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000680 // If we have just a symbol make sure it is in the same section
681 // as our address. If it isn't, then we might have just found
682 // the last symbol that came before the address that we are
683 // looking up that has nothing to do with our address lookup.
684 if (sc.symbol->GetAddressRangePtr() && sc.symbol->GetAddressRangePtr()->GetBaseAddress().GetSection() != GetSection())
685 sc.symbol = NULL;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000686 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000687 sc.GetDescription(s, eDescriptionLevelBrief, target);
Greg Claytonc749eb82011-07-11 05:12:02 +0000688
689 if (sc.block)
690 {
691 bool can_create = true;
692 bool get_parent_variables = true;
693 bool stop_if_block_is_inlined_function = false;
694 VariableList variable_list;
695 sc.block->AppendVariables (can_create,
696 get_parent_variables,
697 stop_if_block_is_inlined_function,
698 &variable_list);
699
700 uint32_t num_variables = variable_list.GetSize();
701 for (uint32_t var_idx = 0; var_idx < num_variables; ++var_idx)
702 {
703 Variable *var = variable_list.GetVariableAtIndex (var_idx).get();
704 if (var && var->LocationIsValidForAddress (*this))
705 {
Greg Clayton81c22f62011-10-19 18:09:39 +0000706 s->Printf (" Variable: id = {0x%8.8llx}, name = \"%s\", type= \"%s\", location =",
Greg Claytonc749eb82011-07-11 05:12:02 +0000707 var->GetID(),
708 var->GetName().GetCString(),
709 var->GetType()->GetName().GetCString());
710 var->DumpLocationForAddress(s, *this);
711 s->PutCString(", decl = ");
712 var->GetDeclaration().DumpStopContext(s, false);
713 s->EOL();
714 }
715 }
716 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000717 }
718 }
Greg Claytonc749eb82011-07-11 05:12:02 +0000719 else
720 {
721 if (fallback_style != DumpStyleInvalid)
722 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
723 return false;
724 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000725 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 }
727
728 return true;
729}
730
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000731uint32_t
Greg Clayton6f6bf262011-12-10 21:05:26 +0000732Address::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733{
734 sc->Clear();
735 // Absolute addresses don't have enough information to reconstruct even their target.
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000736 if (m_section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 {
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000738 Module *address_module = m_section->GetModule();
739 if (address_module)
740 {
Greg Claytona2eee182011-09-17 07:23:18 +0000741 sc->module_sp = address_module;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000742 if (sc->module_sp)
743 return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc);
744 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000746 return 0;
747}
748
749Module *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000750Address::CalculateSymbolContextModule () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000751{
752 if (m_section)
753 return m_section->GetModule();
754 return NULL;
755}
756
757CompileUnit *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000758Address::CalculateSymbolContextCompileUnit () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000759{
760 if (m_section)
761 {
762 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000763 sc.module_sp = m_section->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000764 if (sc.module_sp)
765 {
766 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc);
767 return sc.comp_unit;
768 }
769 }
770 return NULL;
771}
772
773Function *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000774Address::CalculateSymbolContextFunction () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000775{
776 if (m_section)
777 {
778 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000779 sc.module_sp = m_section->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000780 if (sc.module_sp)
781 {
782 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc);
783 return sc.function;
784 }
785 }
786 return NULL;
787}
788
789Block *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000790Address::CalculateSymbolContextBlock () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000791{
792 if (m_section)
793 {
794 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000795 sc.module_sp = m_section->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000796 if (sc.module_sp)
797 {
798 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc);
799 return sc.block;
800 }
801 }
802 return NULL;
803}
804
805Symbol *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000806Address::CalculateSymbolContextSymbol () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000807{
808 if (m_section)
809 {
810 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000811 sc.module_sp = m_section->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000812 if (sc.module_sp)
813 {
814 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc);
815 return sc.symbol;
816 }
817 }
818 return NULL;
819}
820
821bool
Greg Clayton6f6bf262011-12-10 21:05:26 +0000822Address::CalculateSymbolContextLineEntry (LineEntry &line_entry) const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000823{
824 if (m_section)
825 {
826 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +0000827 sc.module_sp = m_section->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000828 if (sc.module_sp)
829 {
830 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc);
831 if (sc.line_entry.IsValid())
832 {
833 line_entry = sc.line_entry;
834 return true;
835 }
836 }
837 }
838 line_entry.Clear();
839 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840}
841
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842int
843Address::CompareFileAddress (const Address& a, const Address& b)
844{
845 addr_t a_file_addr = a.GetFileAddress();
846 addr_t b_file_addr = b.GetFileAddress();
847 if (a_file_addr < b_file_addr)
848 return -1;
849 if (a_file_addr > b_file_addr)
850 return +1;
851 return 0;
852}
853
854
855int
Greg Claytonf5e56de2010-09-14 23:36:40 +0000856Address::CompareLoadAddress (const Address& a, const Address& b, Target *target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000858 assert (target != NULL);
859 addr_t a_load_addr = a.GetLoadAddress (target);
860 addr_t b_load_addr = b.GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 if (a_load_addr < b_load_addr)
862 return -1;
863 if (a_load_addr > b_load_addr)
864 return +1;
865 return 0;
866}
867
868int
869Address::CompareModulePointerAndOffset (const Address& a, const Address& b)
870{
871 Module *a_module = a.GetModule ();
872 Module *b_module = b.GetModule ();
873 if (a_module < b_module)
874 return -1;
875 if (a_module > b_module)
876 return +1;
877 // Modules are the same, just compare the file address since they should
878 // be unique
879 addr_t a_file_addr = a.GetFileAddress();
880 addr_t b_file_addr = b.GetFileAddress();
881 if (a_file_addr < b_file_addr)
882 return -1;
883 if (a_file_addr > b_file_addr)
884 return +1;
885 return 0;
886}
887
888
889size_t
890Address::MemorySize () const
891{
892 // Noting special for the memory size of a single Address object,
893 // it is just the size of itself.
894 return sizeof(Address);
895}
896
897
Greg Claytonb0848c52011-01-08 00:05:12 +0000898//----------------------------------------------------------------------
899// NOTE: Be careful using this operator. It can correctly compare two
900// addresses from the same Module correctly. It can't compare two
901// addresses from different modules in any meaningful way, but it will
902// compare the module pointers.
903//
904// To sum things up:
905// - works great for addresses within the same module
906// - it works for addresses across multiple modules, but don't expect the
907// address results to make much sense
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908//
Greg Claytonb0848c52011-01-08 00:05:12 +0000909// This basically lets Address objects be used in ordered collection
910// classes.
911//----------------------------------------------------------------------
912
913bool
914lldb_private::operator< (const Address& lhs, const Address& rhs)
915{
916 Module *lhs_module = lhs.GetModule();
917 Module *rhs_module = rhs.GetModule();
918 if (lhs_module == rhs_module)
919 {
920 // Addresses are in the same module, just compare the file addresses
921 return lhs.GetFileAddress() < rhs.GetFileAddress();
922 }
923 else
924 {
925 // The addresses are from different modules, just use the module
926 // pointer value to get consistent ordering
927 return lhs_module < rhs_module;
928 }
929}
930
931bool
932lldb_private::operator> (const Address& lhs, const Address& rhs)
933{
934 Module *lhs_module = lhs.GetModule();
935 Module *rhs_module = rhs.GetModule();
936 if (lhs_module == rhs_module)
937 {
938 // Addresses are in the same module, just compare the file addresses
939 return lhs.GetFileAddress() > rhs.GetFileAddress();
940 }
941 else
942 {
943 // The addresses are from different modules, just use the module
944 // pointer value to get consistent ordering
945 return lhs_module > rhs_module;
946 }
947}
948
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949
950// The operator == checks for exact equality only (same section, same offset)
951bool
952lldb_private::operator== (const Address& a, const Address& rhs)
953{
954 return a.GetSection() == rhs.GetSection() &&
955 a.GetOffset() == rhs.GetOffset();
956}
957// The operator != checks for exact inequality only (differing section, or
958// different offset)
959bool
960lldb_private::operator!= (const Address& a, const Address& rhs)
961{
962 return a.GetSection() != rhs.GetSection() ||
963 a.GetOffset() != rhs.GetOffset();
964}
965
966bool
967Address::IsLinkedAddress () const
968{
969 return m_section && m_section->GetLinkedSection();
970}
971
972
973void
974Address::ResolveLinkedAddress ()
975{
976 if (m_section)
977 {
978 const Section *linked_section = m_section->GetLinkedSection();
979 if (linked_section)
980 {
981 m_offset += m_section->GetLinkedOffset();
982 m_section = linked_section;
983 }
984 }
985}
Greg Claytonded470d2011-03-19 01:12:21 +0000986
Greg Claytone0d378b2011-03-24 21:19:54 +0000987AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000988Address::GetAddressClass () const
989{
990 Module *module = GetModule();
991 if (module)
992 {
993 ObjectFile *obj_file = module->GetObjectFile();
994 if (obj_file)
995 return obj_file->GetAddressClass (GetFileAddress());
996 }
997 return eAddressClassUnknown;
998}
Greg Claytoncd482e32011-05-18 01:58:14 +0000999
1000bool
1001Address::SetLoadAddress (lldb::addr_t load_addr, Target *target)
1002{
1003 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this))
1004 return true;
1005 m_section = NULL;
1006 m_offset = load_addr;
1007 return false;
1008}
1009