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 "util/mac/mach_o_image_reader.h" | |
| 28 #include "util/mac/process_types.h" | |
| 27 #include "util/misc/scoped_forbid_return.h" | 29 #include "util/misc/scoped_forbid_return.h" |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 void MachTimeValueToTimeval(const time_value& mach, timeval* tv) { | 33 void MachTimeValueToTimeval(const time_value& mach, timeval* tv) { |
| 32 tv->tv_sec = mach.seconds; | 34 tv->tv_sec = mach.seconds; |
| 33 tv->tv_usec = mach.microseconds; | 35 tv->tv_usec = mach.microseconds; |
| 34 } | 36 } |
| 35 | 37 |
| 36 kern_return_t MachVMRegionRecurseDeepest(mach_port_t task, | 38 kern_return_t MachVMRegionRecurseDeepest(mach_port_t task, |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 | 347 |
| 346 threads_need_owners.Disarm(); | 348 threads_need_owners.Disarm(); |
| 347 } | 349 } |
| 348 | 350 |
| 349 void ProcessReader::InitializeModules() { | 351 void ProcessReader::InitializeModules() { |
| 350 DCHECK(!initialized_modules_); | 352 DCHECK(!initialized_modules_); |
| 351 DCHECK(modules_.empty()); | 353 DCHECK(modules_.empty()); |
| 352 | 354 |
| 353 initialized_modules_ = true; | 355 initialized_modules_ = true; |
| 354 | 356 |
| 355 // TODO(mark): Complete this implementation. The implementation depends on | 357 // This API only works on Mac OS X 10.6 and higher. On Mac OS X 10.5, find the |
| 356 // process_types, which cannot land yet because it depends on this file, | 358 // “_dyld_all_image_infos” symbol in the loaded LC_LOAD_DYLINKER (dyld). |
| 357 // process_reader. This temporary “cut” was made to avoid a review that’s too | 359 task_dyld_info_data_t dyld_info; |
| 358 // large. Yes, this circular dependency is unfortunate. Suggestions are | 360 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; |
| 359 // welcome. | 361 kern_return_t kr = task_info( |
| 362 task_, TASK_DYLD_INFO, reinterpret_cast<task_info_t>(&dyld_info), &count); | |
| 363 if (kr != KERN_SUCCESS) { | |
| 364 MACH_LOG(WARNING, kr) << "task_info"; | |
| 365 return; | |
| 366 } | |
| 367 | |
| 368 // TODO(mark): Deal with statically linked executables which don’t use dyld. | |
| 369 // This may look for the module that matches the executable path in the same | |
| 370 // data set that vmmap uses. | |
| 371 | |
| 372 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 | |
| 373 // The task_dyld_info_data_t struct grew in 10.7, adding the format field. | |
| 374 // Don’t check this field if it’s not present, which can happen when either | |
| 375 // the SDK used at compile time or the kernel at run time are too old and | |
| 376 // don’t know about it. | |
| 377 if (count >= TASK_DYLD_INFO_COUNT) { | |
| 378 const integer_t kExpectedFormat = | |
| 379 !Is64Bit() ? TASK_DYLD_ALL_IMAGE_INFO_32 : TASK_DYLD_ALL_IMAGE_INFO_64; | |
| 380 if (dyld_info.all_image_info_format != kExpectedFormat) { | |
| 381 LOG(WARNING) << "unexpected task_dyld_info_data_t::all_image_info_format " | |
| 382 << dyld_info.all_image_info_format; | |
| 383 DCHECK_EQ(dyld_info.all_image_info_format, kExpectedFormat); | |
| 384 return; | |
| 385 } | |
| 386 } | |
| 387 #endif | |
| 388 | |
| 389 process_types::dyld_all_image_infos all_image_infos; | |
| 390 if (!all_image_infos.Read(this, dyld_info.all_image_info_addr)) { | |
| 391 LOG(WARNING) << "could not read dyld_all_image_infos"; | |
| 392 return; | |
| 393 } | |
| 394 | |
| 395 // Note that all_image_infos.infoArrayCount may be 0 if a crash occurred while | |
| 396 // dyld was loading the executable. This can happen if a required dynamic | |
| 397 // library was not found. | |
| 398 DCHECK_GE(all_image_infos.version, 1u); | |
| 399 DCHECK_NE(all_image_infos.infoArray, 0u); | |
|
Robert Sesek
2014/09/05 17:14:58
Why not NULL instead of 0u?
| |
| 400 | |
| 401 std::vector<process_types::dyld_image_info> image_info_vector( | |
| 402 all_image_infos.infoArrayCount); | |
| 403 if (!process_types::dyld_image_info::ReadArrayInto(this, | |
| 404 all_image_infos.infoArray, | |
| 405 image_info_vector.size(), | |
| 406 &image_info_vector[0])) { | |
| 407 LOG(WARNING) << "could not read dyld_image_info array"; | |
| 408 return; | |
| 409 } | |
| 410 | |
| 411 bool found_dyld = false; | |
| 412 for (const process_types::dyld_image_info& image_info : image_info_vector) { | |
| 413 ProcessReaderModule module; | |
| 414 module.address = image_info.imageLoadAddress; | |
| 415 module.timestamp = image_info.imageFileModDate; | |
| 416 if (!task_memory_->ReadCString(image_info.imageFilePath, &module.name)) { | |
| 417 LOG(WARNING) << "could not read dyld_image_info::imageFilePath"; | |
| 418 // Proceed anyway with an empty module name. | |
| 419 } | |
| 420 | |
| 421 modules_.push_back(module); | |
| 422 | |
| 423 if (all_image_infos.version >= 2 && all_image_infos.dyldImageLoadAddress && | |
| 424 image_info.imageLoadAddress == all_image_infos.dyldImageLoadAddress) { | |
| 425 found_dyld = true; | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 // all_image_infos.infoArray doesn’t include an entry for dyld, but dyld is | |
| 430 // loaded into the process’ address space as a module. Its load address is | |
| 431 // easily known given a sufficiently recent all_image_infos.version, but the | |
| 432 // timestamp and pathname are not given as they are for other modules. | |
| 433 // | |
| 434 // The timestamp is a lost cause, because the kernel doesn’t record the | |
| 435 // timestamp of the dynamic linker at the time it’s loaded in the same way | |
| 436 // that dyld records the timestamps of other modules when they’re loaded. (The | |
| 437 // timestamp for the main executable is also not reported and appears as 0 | |
| 438 // even when accessed via dyld APIs, because it’s loaded by the kernel, not by | |
| 439 // dyld.) | |
| 440 // | |
| 441 // The name can be determined, but it’s not as simple as hardcoding the | |
| 442 // default "/usr/lib/dyld" because an executable could have specified anything | |
| 443 // in its LC_LOAD_DYLINKER command. | |
| 444 if (!found_dyld && all_image_infos.version >= 2 && | |
| 445 all_image_infos.dyldImageLoadAddress) { | |
| 446 ProcessReaderModule module; | |
| 447 module.address = all_image_infos.dyldImageLoadAddress; | |
| 448 module.timestamp = 0; | |
| 449 | |
| 450 // Examine the executable’s LC_LOAD_DYLINKER load command to find the path | |
| 451 // used to load dyld. | |
| 452 MachOImageReader executable; | |
| 453 if (all_image_infos.infoArrayCount >= 1 && | |
| 454 executable.Initialize(this, modules_[0].address, modules_[0].name) && | |
| 455 executable.FileType() == MH_EXECUTE && | |
| 456 !executable.DylinkerName().empty()) { | |
| 457 module.name = executable.DylinkerName(); | |
| 458 } else { | |
| 459 // Look inside dyld directly to find its preferred path. | |
| 460 MachOImageReader dyld; | |
| 461 if (dyld.Initialize(this, module.address, "(dyld)") && | |
| 462 dyld.FileType() == MH_DYLINKER && !dyld.DylinkerName().empty()) { | |
| 463 module.name = dyld.DylinkerName(); | |
| 464 } | |
| 465 } | |
| 466 | |
| 467 // dyld is loaded in the process even if its path can’t be determined. | |
| 468 modules_.push_back(module); | |
| 469 } | |
| 360 } | 470 } |
| 361 | 471 |
| 362 mach_vm_address_t ProcessReader::CalculateStackRegion( | 472 mach_vm_address_t ProcessReader::CalculateStackRegion( |
| 363 mach_vm_address_t stack_pointer, | 473 mach_vm_address_t stack_pointer, |
| 364 mach_vm_size_t* stack_region_size) { | 474 mach_vm_size_t* stack_region_size) { |
| 365 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 475 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 366 | 476 |
| 367 // For pthreads, it may be possible to compute the stack region based on the | 477 // For pthreads, it may be possible to compute the stack region based on the |
| 368 // internal _pthread::stackaddr and _pthread::stacksize. The _pthread struct | 478 // internal _pthread::stackaddr and _pthread::stacksize. The _pthread struct |
| 369 // for a thread can be located at TSD slot 0, or the known offsets of | 479 // for a thread can be located at TSD slot 0, or the known offsets of |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 // The red zone would go lower into another region in memory, but no | 634 // The red zone would go lower into another region in memory, but no |
| 525 // region was found. Memory can only be captured to an address as low as | 635 // region was found. Memory can only be captured to an address as low as |
| 526 // the base address of the region already found. | 636 // the base address of the region already found. |
| 527 *start_address = *region_base; | 637 *start_address = *region_base; |
| 528 } | 638 } |
| 529 } | 639 } |
| 530 #endif | 640 #endif |
| 531 } | 641 } |
| 532 | 642 |
| 533 } // namespace crashpad | 643 } // namespace crashpad |
| OLD | NEW |