@@ -245,6 +245,8 @@ bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
245
245
246
246
LLDB_LOGV (log, " [MemoryReader] asked to read {0} bytes at address {1:x}" ,
247
247
size, address.getAddressData ());
248
+ if (readBytesFromSymbolObjectFile (address.getAddressData (), dest, size))
249
+ return true ;
248
250
249
251
llvm::Optional<Address> maybeAddr =
250
252
resolveRemoteAddress (address.getAddressData ());
@@ -349,10 +351,15 @@ void LLDBMemoryReader::popLocalBuffer() {
349
351
}
350
352
351
353
llvm::Optional<std::pair<uint64_t , uint64_t >>
352
- LLDBMemoryReader::addModuleToAddressMap (ModuleSP module ) {
354
+ LLDBMemoryReader::addModuleToAddressMap (ModuleSP module , bool register_symbol_obj_file ) {
353
355
if (!readMetadataFromFileCacheEnabled ())
354
356
return {};
355
357
358
+ assert (register_symbol_obj_file <=
359
+ m_process.GetTarget ().GetSwiftReadMetadataFromDSYM () &&
360
+ " Trying to register symbol object file, but reading from it is "
361
+ " disabled!" );
362
+
356
363
// The first available address is the mask, since subsequent images are mapped
357
364
// in ascending order, all of them will contain this mask.
358
365
uint64_t module_start_address = LLDB_FILE_ADDRESS_BIT;
@@ -374,7 +381,20 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module) {
374
381
" LLDB file address bit clashes with an obj-c bit!" );
375
382
#endif
376
383
377
- SectionList *section_list = module ->GetObjectFile ()->GetSectionList ();
384
+ ObjectFile *object_file;
385
+ if (register_symbol_obj_file) {
386
+ auto *symbol_file = module ->GetSymbolFile ();
387
+ if (!symbol_file)
388
+ return {};
389
+ object_file = symbol_file->GetObjectFile ();
390
+ } else {
391
+ object_file = module ->GetObjectFile ();
392
+ }
393
+
394
+ if (!object_file)
395
+ return {};
396
+
397
+ SectionList *section_list = object_file->GetSectionList ();
378
398
379
399
auto section_list_size = section_list->GetSize ();
380
400
if (section_list_size == 0 )
@@ -391,22 +411,27 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module) {
391
411
// available after the end of the current image.
392
412
uint64_t next_module_start_address = llvm::alignTo (module_end_address, 8 );
393
413
m_range_module_map.emplace_back (next_module_start_address, module );
414
+
415
+ if (register_symbol_obj_file)
416
+ m_modules_with_metadata_in_symbol_obj_file.insert (module );
417
+
394
418
return {{module_start_address, module_end_address}};
395
419
}
396
420
397
- llvm::Optional<Address>
398
- LLDBMemoryReader::resolveRemoteAddress (uint64_t address) const {
399
- Log *log = GetLog (LLDBLog::Types);
421
+ llvm::Optional<std::pair<uint64_t , lldb::ModuleSP>>
422
+ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress (
423
+ uint64_t tagged_address) const {
424
+ Log *log (GetLog (LLDBLog::Types));
400
425
401
426
if (!readMetadataFromFileCacheEnabled ())
402
- return Address (address) ;
427
+ return {} ;
403
428
404
429
// If the address contains our mask, this is an image we registered.
405
- if (!(address & LLDB_FILE_ADDRESS_BIT))
406
- return Address (address) ;
430
+ if (!(tagged_address & LLDB_FILE_ADDRESS_BIT))
431
+ return {} ;
407
432
408
433
// Dummy pair with the address we're looking for.
409
- auto comparison_pair = std::make_pair (address , ModuleSP ());
434
+ auto comparison_pair = std::make_pair (tagged_address , ModuleSP ());
410
435
411
436
// Explicitly compare only the addresses, never the modules in the pairs.
412
437
auto pair_iterator = std::lower_bound (
@@ -418,7 +443,7 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
418
443
LLDB_LOG (log,
419
444
" [MemoryReader] Address {0:x} is larger than the upper bound "
420
445
" address of the mapped in modules" ,
421
- address );
446
+ tagged_address );
422
447
return {};
423
448
}
424
449
@@ -427,15 +452,31 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
427
452
if (pair_iterator == m_range_module_map.begin ())
428
453
// Since this is the first registered module,
429
454
// clearing the tag bit will give the virtual file address.
430
- file_address = address & ~LLDB_FILE_ADDRESS_BIT;
455
+ file_address = tagged_address & ~LLDB_FILE_ADDRESS_BIT;
431
456
else
432
457
// The end of the previous section is the start of the current one.
433
- file_address = address - std::prev (pair_iterator)->first ;
458
+ file_address = tagged_address - std::prev (pair_iterator)->first ;
434
459
435
460
LLDB_LOGV (log,
436
461
" [MemoryReader] Successfully resolved mapped address {0:x} into "
437
462
" file address {1:x}" ,
438
- address, file_address);
463
+ tagged_address, file_address);
464
+ return {{file_address, module }};
465
+ }
466
+
467
+ llvm::Optional<Address>
468
+ LLDBMemoryReader::resolveRemoteAddress (uint64_t address) const {
469
+ Log *log (GetLog (LLDBLog::Types));
470
+ auto maybe_pair = getFileAddressAndModuleForTaggedAddress (address);
471
+ if (!maybe_pair)
472
+ return Address (address);
473
+
474
+ uint64_t file_address = maybe_pair->first ;
475
+ ModuleSP module = maybe_pair->second ;
476
+
477
+ if (m_modules_with_metadata_in_symbol_obj_file.count (module ))
478
+ return Address (address);
479
+
439
480
auto *object_file = module ->GetObjectFile ();
440
481
if (!object_file)
441
482
return {};
@@ -456,6 +497,47 @@ LLDBMemoryReader::resolveRemoteAddress(uint64_t address) const {
456
497
return resolved;
457
498
}
458
499
500
+ bool LLDBMemoryReader::readBytesFromSymbolObjectFile (uint64_t address, uint8_t *dest,
501
+ uint64_t size) const {
502
+ Log *log (GetLog (LLDBLog::Types));
503
+
504
+ if (!m_process.GetTarget ().GetSwiftReadMetadataFromDSYM ())
505
+ return false ;
506
+
507
+ auto maybe_pair = getFileAddressAndModuleForTaggedAddress (address);
508
+ if (!maybe_pair)
509
+ return false ;
510
+
511
+ uint64_t file_address = maybe_pair->first ;
512
+ ModuleSP module = maybe_pair->second ;
513
+
514
+ if (!m_modules_with_metadata_in_symbol_obj_file.count (module ))
515
+ return false ;
516
+
517
+ auto *symbol_file = module ->GetSymbolFile ();
518
+ if (!symbol_file)
519
+ return false ;
520
+
521
+ auto *object_file = symbol_file->GetObjectFile ();
522
+ if (!object_file)
523
+ return false ;
524
+
525
+ Address resolved (file_address, object_file->GetSectionList ());
526
+ if (!resolved.IsSectionOffset ()) {
527
+ LLDB_LOG (log,
528
+ " [MemoryReader] Could not make a real address out of file address "
529
+ " {0:x} and object file {1}" ,
530
+ file_address, object_file->GetFileSpec ().GetFilename ());
531
+ return false ;
532
+ }
533
+
534
+ LLDB_LOGV (log, " [MemoryReader] Reading memory from symbol rich binary" );
535
+
536
+ auto section = resolved.GetSection ();
537
+ return object_file->ReadSectionData (section.get (), resolved.GetOffset (),
538
+ dest, size);
539
+ }
540
+
459
541
bool LLDBMemoryReader::readMetadataFromFileCacheEnabled () const {
460
542
auto &triple = m_process.GetTarget ().GetArchitecture ().GetTriple ();
461
543
0 commit comments