Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/mac/process_reader.h" | 15 #include "util/mac/process_reader.h" |
| 16 | 16 |
| 17 #include <AvailabilityMacros.h> | 17 #include <AvailabilityMacros.h> |
| 18 #include <mach/mach_vm.h> | 18 #include <mach/mach_vm.h> |
| 19 #include <mach-o/loader.h> | 19 #include <mach-o/loader.h> |
| 20 | 20 |
| 21 #include <algorithm> | 21 #include <algorithm> |
| 22 | 22 |
| 23 #include "base/logging.h" | 23 #include "base/logging.h" |
| 24 #include "base/mac/mach_logging.h" | 24 #include "base/mac/mach_logging.h" |
| 25 #include "base/mac/scoped_mach_port.h" | 25 #include "base/mac/scoped_mach_port.h" |
| 26 #include "base/mac/scoped_mach_vm.h" | 26 #include "base/mac/scoped_mach_vm.h" |
| 27 #include "base/strings/stringprintf.h" | |
| 27 #include "util/mac/mach_o_image_reader.h" | 28 #include "util/mac/mach_o_image_reader.h" |
| 28 #include "util/mac/process_types.h" | 29 #include "util/mac/process_types.h" |
| 29 #include "util/misc/scoped_forbid_return.h" | 30 #include "util/misc/scoped_forbid_return.h" |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 void MachTimeValueToTimeval(const time_value& mach, timeval* tv) { | 34 void MachTimeValueToTimeval(const time_value& mach, timeval* tv) { |
| 34 tv->tv_sec = mach.seconds; | 35 tv->tv_sec = mach.seconds; |
| 35 tv->tv_usec = mach.microseconds; | 36 tv->tv_usec = mach.microseconds; |
| 36 } | 37 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 debug_context(), | 76 debug_context(), |
| 76 id(0), | 77 id(0), |
| 77 stack_region_address(0), | 78 stack_region_address(0), |
| 78 stack_region_size(0), | 79 stack_region_size(0), |
| 79 thread_specific_data_address(0), | 80 thread_specific_data_address(0), |
| 80 port(MACH_PORT_NULL), | 81 port(MACH_PORT_NULL), |
| 81 suspend_count(0), | 82 suspend_count(0), |
| 82 priority(0) { | 83 priority(0) { |
| 83 } | 84 } |
| 84 | 85 |
| 85 ProcessReader::Module::Module() : name(), address(0), timestamp(0) { | 86 ProcessReader::Module::Module() : name(), reader(NULL), timestamp(0) { |
| 86 } | 87 } |
| 87 | 88 |
| 88 ProcessReader::Module::~Module() { | 89 ProcessReader::Module::~Module() { |
| 89 } | 90 } |
| 90 | 91 |
| 91 ProcessReader::ProcessReader() | 92 ProcessReader::ProcessReader() |
| 92 : kern_proc_info_(), | 93 : kern_proc_info_(), |
| 93 threads_(), | 94 threads_(), |
| 94 modules_(), | 95 modules_(), |
| 96 module_readers_(), | |
| 95 task_memory_(), | 97 task_memory_(), |
| 96 task_(MACH_PORT_NULL), | 98 task_(MACH_PORT_NULL), |
| 97 initialized_(), | 99 initialized_(), |
| 98 is_64_bit_(false), | 100 is_64_bit_(false), |
| 99 initialized_threads_(false), | 101 initialized_threads_(false), |
| 100 initialized_modules_(false) { | 102 initialized_modules_(false) { |
| 101 } | 103 } |
| 102 | 104 |
| 103 ProcessReader::~ProcessReader() { | 105 ProcessReader::~ProcessReader() { |
| 104 for (const Thread& thread : threads_) { | 106 for (const Thread& thread : threads_) { |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 } | 384 } |
| 383 } | 385 } |
| 384 #endif | 386 #endif |
| 385 | 387 |
| 386 process_types::dyld_all_image_infos all_image_infos; | 388 process_types::dyld_all_image_infos all_image_infos; |
| 387 if (!all_image_infos.Read(this, dyld_info.all_image_info_addr)) { | 389 if (!all_image_infos.Read(this, dyld_info.all_image_info_addr)) { |
| 388 LOG(WARNING) << "could not read dyld_all_image_infos"; | 390 LOG(WARNING) << "could not read dyld_all_image_infos"; |
| 389 return; | 391 return; |
| 390 } | 392 } |
| 391 | 393 |
| 394 DCHECK_GE(all_image_infos.version, 1u); | |
| 395 | |
| 392 // Note that all_image_infos.infoArrayCount may be 0 if a crash occurred while | 396 // Note that all_image_infos.infoArrayCount may be 0 if a crash occurred while |
| 393 // dyld was loading the executable. This can happen if a required dynamic | 397 // dyld was loading the executable. This can happen if a required dynamic |
| 394 // library was not found. | 398 // library was not found. Similarly, all_image_infos.infoArray may be NULL if |
| 395 DCHECK_GE(all_image_infos.version, 1u); | 399 // a crash occurred while dyld was updating it. |
| 396 DCHECK_NE(all_image_infos.infoArray, static_cast<mach_vm_address_t>(NULL)); | 400 // |
| 401 // TODO(mark): It may be possible to recover from these situations by looking | |
| 402 // through memory mappings for Mach-O images. | |
| 403 if (all_image_infos.infoArrayCount == 0) { | |
| 404 LOG(WARNING) << "all_image_infos.infoArrayCount is zero"; | |
| 405 return; | |
| 406 } | |
| 407 if (!all_image_infos.infoArray) { | |
| 408 LOG(WARNING) << "all_image_infos.infoArray is NULL"; | |
| 409 return; | |
| 410 } | |
| 397 | 411 |
| 398 std::vector<process_types::dyld_image_info> image_info_vector( | 412 std::vector<process_types::dyld_image_info> image_info_vector( |
| 399 all_image_infos.infoArrayCount); | 413 all_image_infos.infoArrayCount); |
| 400 if (!process_types::dyld_image_info::ReadArrayInto(this, | 414 if (!process_types::dyld_image_info::ReadArrayInto(this, |
| 401 all_image_infos.infoArray, | 415 all_image_infos.infoArray, |
| 402 image_info_vector.size(), | 416 image_info_vector.size(), |
| 403 &image_info_vector[0])) { | 417 &image_info_vector[0])) { |
| 404 LOG(WARNING) << "could not read dyld_image_info array"; | 418 LOG(WARNING) << "could not read dyld_image_info array"; |
| 405 return; | 419 return; |
| 406 } | 420 } |
| 407 | 421 |
| 422 size_t main_executable_count = 0; | |
| 408 bool found_dyld = false; | 423 bool found_dyld = false; |
| 424 modules_.reserve(image_info_vector.size()); | |
| 409 for (const process_types::dyld_image_info& image_info : image_info_vector) { | 425 for (const process_types::dyld_image_info& image_info : image_info_vector) { |
| 410 Module module; | 426 Module module; |
| 411 module.address = image_info.imageLoadAddress; | |
| 412 module.timestamp = image_info.imageFileModDate; | 427 module.timestamp = image_info.imageFileModDate; |
| 428 | |
| 413 if (!task_memory_->ReadCString(image_info.imageFilePath, &module.name)) { | 429 if (!task_memory_->ReadCString(image_info.imageFilePath, &module.name)) { |
| 414 LOG(WARNING) << "could not read dyld_image_info::imageFilePath"; | 430 LOG(WARNING) << "could not read dyld_image_info::imageFilePath"; |
| 415 // Proceed anyway with an empty module name. | 431 // Proceed anyway with an empty module name. |
| 416 } | 432 } |
| 417 | 433 |
| 434 scoped_ptr<MachOImageReader> reader(new MachOImageReader()); | |
| 435 if (!reader->Initialize(this, image_info.imageLoadAddress, module.name)) { | |
| 436 reader.reset(); | |
| 437 } | |
| 438 | |
| 439 module.reader = reader.get(); | |
| 440 | |
| 441 uint32_t file_type = reader ? reader->FileType() : 0; | |
| 442 | |
| 443 module_readers_.push_back(reader.release()); | |
| 418 modules_.push_back(module); | 444 modules_.push_back(module); |
| 419 | 445 |
| 420 if (all_image_infos.version >= 2 && all_image_infos.dyldImageLoadAddress && | 446 if (all_image_infos.version >= 2 && all_image_infos.dyldImageLoadAddress && |
| 421 image_info.imageLoadAddress == all_image_infos.dyldImageLoadAddress) { | 447 image_info.imageLoadAddress == all_image_infos.dyldImageLoadAddress) { |
| 422 found_dyld = true; | 448 found_dyld = true; |
| 449 | |
| 450 LOG_IF(WARNING, file_type != MH_DYLINKER) | |
| 451 << base::StringPrintf("dylinker (%s) has unexpected Mach-O type %d", | |
| 452 module.name.c_str(), | |
| 453 file_type); | |
| 454 } | |
| 455 | |
| 456 if (file_type == MH_EXECUTE) { | |
| 457 // On Mac OS X 10.6, the main executable does not normally show up at | |
| 458 // index 0. This is because of how 10.6.8 dyld-132.13/src/dyld.cpp | |
| 459 // notifyGDB(), the function resposible for causing | |
| 460 // dyld_all_image_infos::infoArray to be updated, is called. It is | |
| 461 // registered to be called when all dependents of an image have been | |
| 462 // mapped (dyld_image_state_dependents_mapped), meaning that the main | |
| 463 // executable won’t be added to the list until all of the libraries it | |
| 464 // depends on are, even though dyld begins looking at the main executable | |
| 465 // first. This changed in later versions of dyld, including those present | |
| 466 // in 10.7. 10.9.4 dyld-239.4/src/dyld.cpp updateAllImages() (renamed from | |
| 467 // notifyGDB()) is registered to be called when an image itself has been | |
| 468 // mapped (dyld_image_state_mapped), regardless of the libraries that it | |
| 469 // depends on. | |
| 470 // | |
| 471 // The interface requires that the main executable be first in the list, | |
| 472 // so swap it into the right position. | |
| 473 size_t index = modules_.size() - 1; | |
| 474 if (main_executable_count == 0) { | |
| 475 std::swap(modules_[0], modules_[index]); | |
| 476 } else { | |
| 477 LOG(WARNING) | |
| 478 << base::StringPrintf("multiple MH_EXECUTE modules (%s, %s)", | |
| 479 modules_[0].name.c_str(), | |
| 480 modules_[index].name.c_str()); | |
| 481 } | |
| 482 ++main_executable_count; | |
| 423 } | 483 } |
| 424 } | 484 } |
| 425 | 485 |
| 486 LOG_IF(WARNING, main_executable_count == 0) << "no MH_EXECUTE modules"; | |
| 487 | |
| 426 // all_image_infos.infoArray doesn’t include an entry for dyld, but dyld is | 488 // all_image_infos.infoArray doesn’t include an entry for dyld, but dyld is |
| 427 // loaded into the process’ address space as a module. Its load address is | 489 // loaded into the process’ address space as a module. Its load address is |
| 428 // easily known given a sufficiently recent all_image_infos.version, but the | 490 // easily known given a sufficiently recent all_image_infos.version, but the |
| 429 // timestamp and pathname are not given as they are for other modules. | 491 // timestamp and pathname are not given as they are for other modules. |
| 430 // | 492 // |
| 431 // The timestamp is a lost cause, because the kernel doesn’t record the | 493 // The timestamp is a lost cause, because the kernel doesn’t record the |
| 432 // timestamp of the dynamic linker at the time it’s loaded in the same way | 494 // timestamp of the dynamic linker at the time it’s loaded in the same way |
| 433 // that dyld records the timestamps of other modules when they’re loaded. (The | 495 // that dyld records the timestamps of other modules when they’re loaded. (The |
| 434 // timestamp for the main executable is also not reported and appears as 0 | 496 // timestamp for the main executable is also not reported and appears as 0 |
| 435 // even when accessed via dyld APIs, because it’s loaded by the kernel, not by | 497 // even when accessed via dyld APIs, because it’s loaded by the kernel, not by |
| 436 // dyld.) | 498 // dyld.) |
| 437 // | 499 // |
| 438 // The name can be determined, but it’s not as simple as hardcoding the | 500 // The name can be determined, but it’s not as simple as hardcoding the |
| 439 // default "/usr/lib/dyld" because an executable could have specified anything | 501 // default "/usr/lib/dyld" because an executable could have specified anything |
| 440 // in its LC_LOAD_DYLINKER command. | 502 // in its LC_LOAD_DYLINKER command. |
| 441 if (!found_dyld && all_image_infos.version >= 2 && | 503 if (!found_dyld && all_image_infos.version >= 2 && |
| 442 all_image_infos.dyldImageLoadAddress) { | 504 all_image_infos.dyldImageLoadAddress) { |
| 443 Module module; | 505 Module module; |
| 444 module.address = all_image_infos.dyldImageLoadAddress; | |
| 445 module.timestamp = 0; | 506 module.timestamp = 0; |
| 446 | 507 |
| 447 // Examine the executable’s LC_LOAD_DYLINKER load command to find the path | 508 // Examine the executable’s LC_LOAD_DYLINKER load command to find the path |
| 448 // used to load dyld. | 509 // used to load dyld. |
| 449 MachOImageReader executable; | 510 if (all_image_infos.infoArrayCount >= 1 && main_executable_count >= 1) { |
| 450 if (all_image_infos.infoArrayCount >= 1 && | 511 module.name = modules_[0].reader->DylinkerName(); |
| 451 executable.Initialize(this, modules_[0].address, modules_[0].name) && | 512 } |
| 452 executable.FileType() == MH_EXECUTE && | 513 std::string module_name = !module.name.empty() ? module.name : "(dyld)"; |
| 453 !executable.DylinkerName().empty()) { | 514 |
| 454 module.name = executable.DylinkerName(); | 515 scoped_ptr<MachOImageReader> reader(new MachOImageReader()); |
| 455 } else { | 516 if (!reader->Initialize( |
| 517 this, all_image_infos.dyldImageLoadAddress, module_name)) { | |
|
Robert Sesek
2014/09/22 16:31:41
nit: indent +4
| |
| 518 reader.reset(); | |
| 519 } | |
| 520 | |
| 521 module.reader = reader.get(); | |
| 522 | |
| 523 uint32_t file_type = reader ? reader->FileType() : 0; | |
| 524 | |
| 525 LOG_IF(WARNING, file_type != MH_DYLINKER) | |
| 526 << base::StringPrintf("dylinker (%s) has unexpected Mach-O type %d", | |
| 527 module.name.c_str(), | |
| 528 file_type); | |
| 529 | |
| 530 if (module.name.empty() && file_type == MH_DYLINKER) { | |
| 456 // Look inside dyld directly to find its preferred path. | 531 // Look inside dyld directly to find its preferred path. |
| 457 MachOImageReader dyld; | 532 module.name = reader->DylinkerName(); |
| 458 if (dyld.Initialize(this, module.address, "(dyld)") && | 533 } |
| 459 dyld.FileType() == MH_DYLINKER && !dyld.DylinkerName().empty()) { | 534 |
| 460 module.name = dyld.DylinkerName(); | 535 if (module.name.empty()) { |
| 461 } | 536 module.name = "(dyld)"; |
| 462 } | 537 } |
| 463 | 538 |
| 464 // dyld is loaded in the process even if its path can’t be determined. | 539 // dyld is loaded in the process even if its path can’t be determined. |
| 540 module_readers_.push_back(reader.release()); | |
| 465 modules_.push_back(module); | 541 modules_.push_back(module); |
| 466 } | 542 } |
| 467 } | 543 } |
| 468 | 544 |
| 469 mach_vm_address_t ProcessReader::CalculateStackRegion( | 545 mach_vm_address_t ProcessReader::CalculateStackRegion( |
| 470 mach_vm_address_t stack_pointer, | 546 mach_vm_address_t stack_pointer, |
| 471 mach_vm_size_t* stack_region_size) { | 547 mach_vm_size_t* stack_region_size) { |
| 472 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 548 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 473 | 549 |
| 474 // For pthreads, it may be possible to compute the stack region based on the | 550 // For pthreads, it may be possible to compute the stack region based on the |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 631 // The red zone would go lower into another region in memory, but no | 707 // The red zone would go lower into another region in memory, but no |
| 632 // region was found. Memory can only be captured to an address as low as | 708 // region was found. Memory can only be captured to an address as low as |
| 633 // the base address of the region already found. | 709 // the base address of the region already found. |
| 634 *start_address = *region_base; | 710 *start_address = *region_base; |
| 635 } | 711 } |
| 636 } | 712 } |
| 637 #endif | 713 #endif |
| 638 } | 714 } |
| 639 | 715 |
| 640 } // namespace crashpad | 716 } // namespace crashpad |
| OLD | NEW |