OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/profiler/native_stack_sampler.h" |
| 6 |
| 7 #include <dlfcn.h> |
| 8 #include <libkern/OSByteOrder.h> |
| 9 #include <libunwind.h> |
| 10 #include <mach-o/swap.h> |
| 11 #include <mach/kern_return.h> |
| 12 #include <mach/mach.h> |
| 13 #include <mach/thread_act.h> |
| 14 #include <pthread.h> |
| 15 #include <sys/resource.h> |
| 16 #include <sys/syslimits.h> |
| 17 |
| 18 #include <algorithm> |
| 19 #include <map> |
| 20 #include <memory> |
| 21 |
| 22 #include "base/logging.h" |
| 23 #include "base/mac/mach_logging.h" |
| 24 #include "base/macros.h" |
| 25 #include "base/memory/ptr_util.h" |
| 26 #include "base/strings/string_number_conversions.h" |
| 27 |
| 28 namespace base { |
| 29 |
| 30 namespace { |
| 31 |
| 32 // Miscellaneous -------------------------------------------------------------- |
| 33 |
| 34 size_t StackCopyBufferSize() { |
| 35 static size_t stack_size = 0; |
| 36 if (stack_size) |
| 37 return stack_size; |
| 38 |
| 39 // In platform_thread_mac's GetDefaultThreadStackSize(), RLIMIT_STACK is used |
| 40 // for all stacks, not just the main thread's, so it is good for use here. |
| 41 struct rlimit stack_rlimit; |
| 42 if (getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 && |
| 43 stack_rlimit.rlim_cur != RLIM_INFINITY) { |
| 44 stack_size = stack_rlimit.rlim_cur; |
| 45 return stack_size; |
| 46 } |
| 47 |
| 48 // If getrlimit somehow fails, return the default macOS main thread stack size |
| 49 // of 8 MB (DFLSSIZ in <i386/vmparam.h>) with extra wiggle room. |
| 50 return 12 * 1024 * 1024; |
| 51 } |
| 52 |
| 53 // Stack walking -------------------------------------------------------------- |
| 54 |
| 55 // Fills |state| with |target_thread|'s context. |
| 56 // |
| 57 // Note that this is called while a thread is suspended. Make very very sure |
| 58 // that no shared resources (e.g. memory allocators) are used for the duration |
| 59 // of this function. |
| 60 bool GetThreadState(thread_act_t target_thread, x86_thread_state64_t* state) { |
| 61 mach_msg_type_number_t count = |
| 62 static_cast<mach_msg_type_number_t>(x86_THREAD_STATE64_COUNT); |
| 63 return thread_get_state(target_thread, x86_THREAD_STATE64, |
| 64 reinterpret_cast<thread_state_t>(state), |
| 65 &count) == KERN_SUCCESS; |
| 66 } |
| 67 |
| 68 // If the value at |pointer| points to the original stack, rewrites it to point |
| 69 // to the corresponding location in the copied stack. |
| 70 // |
| 71 // Note that this is called while a thread is suspended. Make very very sure |
| 72 // that no shared resources (e.g. memory allocators) are used for the duration |
| 73 // of this function. |
| 74 uintptr_t RewritePointerIfInOriginalStack( |
| 75 const uintptr_t* original_stack_bottom, |
| 76 const uintptr_t* original_stack_top, |
| 77 uintptr_t* stack_copy_bottom, |
| 78 uintptr_t pointer) { |
| 79 uintptr_t original_stack_bottom_int = |
| 80 reinterpret_cast<uintptr_t>(original_stack_bottom); |
| 81 uintptr_t original_stack_top_int = |
| 82 reinterpret_cast<uintptr_t>(original_stack_top); |
| 83 uintptr_t stack_copy_bottom_int = |
| 84 reinterpret_cast<uintptr_t>(stack_copy_bottom); |
| 85 |
| 86 if ((pointer < original_stack_bottom_int) || |
| 87 (pointer >= original_stack_top_int)) { |
| 88 return pointer; |
| 89 } |
| 90 |
| 91 return stack_copy_bottom_int + (pointer - original_stack_bottom_int); |
| 92 } |
| 93 |
| 94 // Copies the stack to a buffer while rewriting possible pointers to locations |
| 95 // within the stack to point to the corresponding locations in the copy. This is |
| 96 // necessary to handle stack frames with dynamic stack allocation, where a |
| 97 // pointer to the beginning of the dynamic allocation area is stored on the |
| 98 // stack and/or in a non-volatile register. |
| 99 // |
| 100 // Eager rewriting of anything that looks like a pointer to the stack, as done |
| 101 // in this function, does not adversely affect the stack unwinding. The only |
| 102 // other values on the stack the unwinding depends on are return addresses, |
| 103 // which should not point within the stack memory. The rewriting is guaranteed |
| 104 // to catch all pointers because the stacks are guaranteed by the ABI to be |
| 105 // sizeof(void*) aligned. |
| 106 // |
| 107 // Note that this is called while a thread is suspended. Make very very sure |
| 108 // that no shared resources (e.g. memory allocators) are used for the duration |
| 109 // of this function. |
| 110 void CopyStackAndRewritePointers(uintptr_t* stack_copy_bottom, |
| 111 const uintptr_t* original_stack_bottom, |
| 112 const uintptr_t* original_stack_top, |
| 113 x86_thread_state64_t* thread_state) |
| 114 NO_SANITIZE("address") { |
| 115 size_t count = original_stack_top - original_stack_bottom; |
| 116 for (size_t pos = 0; pos < count; ++pos) { |
| 117 stack_copy_bottom[pos] = RewritePointerIfInOriginalStack( |
| 118 original_stack_bottom, original_stack_top, stack_copy_bottom, |
| 119 original_stack_bottom[pos]); |
| 120 } |
| 121 |
| 122 uint64_t* rewrite_registers[] = {&thread_state->__rbx, &thread_state->__rbp, |
| 123 &thread_state->__rsp, &thread_state->__r12, |
| 124 &thread_state->__r13, &thread_state->__r14, |
| 125 &thread_state->__r15}; |
| 126 for (auto* reg : rewrite_registers) { |
| 127 *reg = RewritePointerIfInOriginalStack( |
| 128 original_stack_bottom, original_stack_top, stack_copy_bottom, *reg); |
| 129 } |
| 130 } |
| 131 |
| 132 // Walks the stack represented by |unwind_context|, calling back to the provided |
| 133 // lambda for each frame. Returns false if an error occurred, otherwise returns |
| 134 // true. |
| 135 template <typename StackFrameCallback> |
| 136 bool WalkStackFromContext(unw_context_t* unwind_context, |
| 137 size_t* frame_count, |
| 138 const StackFrameCallback& callback) { |
| 139 unw_cursor_t unwind_cursor; |
| 140 unw_init_local(&unwind_cursor, unwind_context); |
| 141 |
| 142 int step_result; |
| 143 unw_word_t ip; |
| 144 do { |
| 145 ++(*frame_count); |
| 146 unw_get_reg(&unwind_cursor, UNW_REG_IP, &ip); |
| 147 |
| 148 callback(static_cast<uintptr_t>(ip)); |
| 149 |
| 150 step_result = unw_step(&unwind_cursor); |
| 151 } while (step_result > 0); |
| 152 |
| 153 if (step_result != 0) |
| 154 return false; |
| 155 |
| 156 return true; |
| 157 } |
| 158 |
| 159 bool IsIPInValidImage(unw_context_t* unwind_context) { |
| 160 unw_cursor_t unwind_cursor; |
| 161 unw_init_local(&unwind_cursor, unwind_context); |
| 162 unw_proc_info_t proc_info; |
| 163 unw_get_proc_info(&unwind_cursor, &proc_info); |
| 164 return proc_info.extra != 0; |
| 165 } |
| 166 |
| 167 // Walks the stack represented by |thread_state|, calling back to the provided |
| 168 // lambda for each frame. |
| 169 template <typename StackFrameCallback> |
| 170 void WalkStack(const x86_thread_state64_t& thread_state, |
| 171 uintptr_t stack_top, |
| 172 const StackFrameCallback& callback) { |
| 173 size_t frame_count = 0; |
| 174 // This uses libunwind to walk the stack. libunwind is designed to be used for |
| 175 // a thread to walk its own stack. This creates two problems. |
| 176 |
| 177 // Problem 1: There is no official way to create a unw_context other than to |
| 178 // create it from the current state of the current thread's stack. To get |
| 179 // around this, forge a context. A unw_context is just a copy of the 16 main |
| 180 // registers followed by the instruction pointer, nothing more. |
| 181 // Coincidentally, the first 17 items of the x86_thread_state64_t type are |
| 182 // exactly those registers in exactly the same order, so just bulk copy them |
| 183 // over. |
| 184 unw_context_t unwind_context; |
| 185 memcpy(&unwind_context, &thread_state, sizeof(uintptr_t) * 17); |
| 186 bool result = WalkStackFromContext(&unwind_context, &frame_count, callback); |
| 187 |
| 188 if (!result) |
| 189 return; |
| 190 |
| 191 if (frame_count == 1) { |
| 192 // Problem 2: Because libunwind is designed to be triggered by user code on |
| 193 // their own thread, if it hits a library that has no unwind info for the |
| 194 // function that is being executed, it just stops. This isn't a problem in |
| 195 // the normal case, but in this case, it's quite possible that the stack |
| 196 // being walked is stopped in a function that bridges to the kernel and thus |
| 197 // is missing the unwind info. |
| 198 // |
| 199 // If so, cheat by scanning the stack and trying again. Only do this if the |
| 200 // first time using libunwind fails after one frame. |
| 201 bool ip_in_valid_image = false; |
| 202 auto& rsp = unwind_context.data[7]; |
| 203 auto& rip = unwind_context.data[16]; |
| 204 const uintptr_t kMaxScanDepthWords = 16; |
| 205 uintptr_t scan_limit = std::min<uintptr_t>( |
| 206 stack_top, rsp + kMaxScanDepthWords * sizeof(uintptr_t)); |
| 207 do { |
| 208 rip = *reinterpret_cast<uintptr_t*>(rsp); // rip = *rsp |
| 209 rsp += sizeof(uintptr_t); // rsp++ |
| 210 if (rsp % sizeof(uintptr_t)) { |
| 211 // The "stack pointer" isn't aligned. Just give up. |
| 212 return; |
| 213 } |
| 214 |
| 215 ip_in_valid_image = IsIPInValidImage(&unwind_context); |
| 216 } while (!ip_in_valid_image && rsp < scan_limit); |
| 217 |
| 218 if (ip_in_valid_image) |
| 219 WalkStackFromContext(&unwind_context, &frame_count, callback); |
| 220 } |
| 221 } |
| 222 |
| 223 // Module identifiers --------------------------------------------------------- |
| 224 |
| 225 // Returns the hex encoding of a 16-byte ID for the binary loaded at |
| 226 // |module_addr|. Returns an empty string if the UUID cannot be found at |
| 227 // |module_addr|. |
| 228 std::string GetUniqueId(const void* module_addr) { |
| 229 const mach_header_64* mach_header = |
| 230 reinterpret_cast<const mach_header_64*>(module_addr); |
| 231 DCHECK_EQ(MH_MAGIC_64, mach_header->magic); |
| 232 |
| 233 size_t offset = sizeof(mach_header_64); |
| 234 size_t offset_limit = sizeof(mach_header_64) + mach_header->sizeofcmds; |
| 235 for (uint32_t i = 0; (i < mach_header->ncmds) && |
| 236 (offset + sizeof(load_command) < offset_limit); |
| 237 ++i) { |
| 238 const load_command* current_cmd = reinterpret_cast<const load_command*>( |
| 239 reinterpret_cast<const uint8_t*>(mach_header) + offset); |
| 240 |
| 241 if (offset + current_cmd->cmdsize > offset_limit) { |
| 242 // This command runs off the end of the command list. This is malformed. |
| 243 return std::string(); |
| 244 } |
| 245 |
| 246 if (current_cmd->cmd == LC_UUID) { |
| 247 if (current_cmd->cmdsize < sizeof(uuid_command)) { |
| 248 // This "UUID command" is too small. This is malformed. |
| 249 return std::string(); |
| 250 } |
| 251 |
| 252 const uuid_command* uuid_cmd = |
| 253 reinterpret_cast<const uuid_command*>(current_cmd); |
| 254 static_assert(sizeof(uuid_cmd->uuid) == sizeof(uuid_t), |
| 255 "UUID field of UUID command should be 16 bytes."); |
| 256 return HexEncode(&uuid_cmd->uuid, sizeof(uuid_cmd->uuid)); |
| 257 } |
| 258 offset += current_cmd->cmdsize; |
| 259 } |
| 260 return std::string(); |
| 261 } |
| 262 |
| 263 // Gets the index for the Module containing |instruction_pointer| in |
| 264 // |modules|, adding it if it's not already present. Returns |
| 265 // StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be |
| 266 // determined for |module|. |
| 267 size_t GetModuleIndex(const uintptr_t instruction_pointer, |
| 268 std::vector<StackSamplingProfiler::Module>* modules, |
| 269 std::map<const void*, size_t>* profile_module_index) { |
| 270 Dl_info inf; |
| 271 if (!dladdr(reinterpret_cast<const void*>(instruction_pointer), &inf)) |
| 272 return StackSamplingProfiler::Frame::kUnknownModuleIndex; |
| 273 |
| 274 auto module_index = profile_module_index->find(inf.dli_fbase); |
| 275 if (module_index == profile_module_index->end()) { |
| 276 StackSamplingProfiler::Module module( |
| 277 reinterpret_cast<uintptr_t>(inf.dli_fbase), GetUniqueId(inf.dli_fbase), |
| 278 base::FilePath(inf.dli_fname)); |
| 279 modules->push_back(module); |
| 280 module_index = |
| 281 profile_module_index |
| 282 ->insert(std::make_pair(inf.dli_fbase, modules->size() - 1)) |
| 283 .first; |
| 284 } |
| 285 return module_index->second; |
| 286 } |
| 287 |
| 288 // ScopedSuspendThread -------------------------------------------------------- |
| 289 |
| 290 // Suspends a thread for the lifetime of the object. |
| 291 class ScopedSuspendThread { |
| 292 public: |
| 293 explicit ScopedSuspendThread(mach_port_t thread_port) |
| 294 : thread_port_(thread_suspend(thread_port) == KERN_SUCCESS |
| 295 ? thread_port |
| 296 : MACH_PORT_NULL) {} |
| 297 |
| 298 ~ScopedSuspendThread() { |
| 299 if (!was_successful()) |
| 300 return; |
| 301 |
| 302 kern_return_t kr = thread_resume(thread_port_); |
| 303 MACH_CHECK(kr == KERN_SUCCESS, kr) << "thread_resume"; |
| 304 } |
| 305 |
| 306 bool was_successful() const { return thread_port_ != MACH_PORT_NULL; } |
| 307 |
| 308 private: |
| 309 mach_port_t thread_port_; |
| 310 |
| 311 DISALLOW_COPY_AND_ASSIGN(ScopedSuspendThread); |
| 312 }; |
| 313 |
| 314 // NativeStackSamplerMac ------------------------------------------------------ |
| 315 |
| 316 class NativeStackSamplerMac : public NativeStackSampler { |
| 317 public: |
| 318 NativeStackSamplerMac(mach_port_t thread_port, |
| 319 AnnotateCallback annotator, |
| 320 NativeStackSamplerTestDelegate* test_delegate); |
| 321 ~NativeStackSamplerMac() override; |
| 322 |
| 323 // StackSamplingProfiler::NativeStackSampler: |
| 324 void ProfileRecordingStarting( |
| 325 std::vector<StackSamplingProfiler::Module>* modules) override; |
| 326 void RecordStackSample(StackSamplingProfiler::Sample* sample) override; |
| 327 void ProfileRecordingStopped() override; |
| 328 |
| 329 private: |
| 330 // Suspends the thread with |thread_port_|, copies its stack and resumes the |
| 331 // thread, then records the stack frames and associated modules into |sample|. |
| 332 void SuspendThreadAndRecordStack(StackSamplingProfiler::Sample* sample); |
| 333 |
| 334 // Weak reference: Mach port for thread being profiled. |
| 335 mach_port_t thread_port_; |
| 336 |
| 337 const AnnotateCallback annotator_; |
| 338 |
| 339 NativeStackSamplerTestDelegate* const test_delegate_; |
| 340 |
| 341 // The stack base address corresponding to |thread_handle_|. |
| 342 const void* const thread_stack_base_address_; |
| 343 |
| 344 // The size of the |stack_copy_buffer_|. |
| 345 const size_t stack_copy_buffer_size_; |
| 346 |
| 347 // Buffer to use for copies of the stack. We use the same buffer for all the |
| 348 // samples to avoid the overhead of multiple allocations and frees. |
| 349 const std::unique_ptr<unsigned char[]> stack_copy_buffer_; |
| 350 |
| 351 // Weak. Points to the modules associated with the profile being recorded |
| 352 // between ProfileRecordingStarting() and ProfileRecordingStopped(). |
| 353 std::vector<StackSamplingProfiler::Module>* current_modules_ = nullptr; |
| 354 |
| 355 // Maps a module's base address to the corresponding Module's index within |
| 356 // current_modules_. |
| 357 std::map<const void*, size_t> profile_module_index_; |
| 358 |
| 359 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerMac); |
| 360 }; |
| 361 |
| 362 NativeStackSamplerMac::NativeStackSamplerMac( |
| 363 mach_port_t thread_port, |
| 364 AnnotateCallback annotator, |
| 365 NativeStackSamplerTestDelegate* test_delegate) |
| 366 : thread_port_(thread_port), |
| 367 annotator_(annotator), |
| 368 test_delegate_(test_delegate), |
| 369 thread_stack_base_address_( |
| 370 pthread_get_stackaddr_np(pthread_from_mach_thread_np(thread_port))), |
| 371 stack_copy_buffer_size_(StackCopyBufferSize()), |
| 372 stack_copy_buffer_(new unsigned char[stack_copy_buffer_size_]) { |
| 373 DCHECK(annotator_); |
| 374 |
| 375 // This class suspends threads, and those threads might be suspended in dyld. |
| 376 // Therefore, for all the system functions that might be linked in dynamically |
| 377 // that are used while threads are suspended, make calls to them to make sure |
| 378 // that they are linked up. |
| 379 x86_thread_state64_t thread_state; |
| 380 GetThreadState(thread_port_, &thread_state); |
| 381 } |
| 382 |
| 383 NativeStackSamplerMac::~NativeStackSamplerMac() {} |
| 384 |
| 385 void NativeStackSamplerMac::ProfileRecordingStarting( |
| 386 std::vector<StackSamplingProfiler::Module>* modules) { |
| 387 current_modules_ = modules; |
| 388 profile_module_index_.clear(); |
| 389 } |
| 390 |
| 391 void NativeStackSamplerMac::RecordStackSample( |
| 392 StackSamplingProfiler::Sample* sample) { |
| 393 DCHECK(current_modules_); |
| 394 |
| 395 SuspendThreadAndRecordStack(sample); |
| 396 } |
| 397 |
| 398 void NativeStackSamplerMac::ProfileRecordingStopped() { |
| 399 current_modules_ = nullptr; |
| 400 } |
| 401 |
| 402 void NativeStackSamplerMac::SuspendThreadAndRecordStack( |
| 403 StackSamplingProfiler::Sample* sample) { |
| 404 x86_thread_state64_t thread_state; |
| 405 |
| 406 // Copy the stack. |
| 407 |
| 408 uintptr_t new_stack_top = 0; |
| 409 { |
| 410 // IMPORTANT NOTE: Do not do ANYTHING in this in this scope that might |
| 411 // allocate memory, including indirectly via use of DCHECK/CHECK or other |
| 412 // logging statements. Otherwise this code can deadlock on heap locks in the |
| 413 // default heap acquired by the target thread before it was suspended. |
| 414 ScopedSuspendThread suspend_thread(thread_port_); |
| 415 if (!suspend_thread.was_successful()) |
| 416 return; |
| 417 |
| 418 if (!GetThreadState(thread_port_, &thread_state)) |
| 419 return; |
| 420 uintptr_t stack_top = |
| 421 reinterpret_cast<uintptr_t>(thread_stack_base_address_); |
| 422 uintptr_t stack_bottom = thread_state.__rsp; |
| 423 if (stack_bottom >= stack_top) |
| 424 return; |
| 425 uintptr_t stack_size = stack_top - stack_bottom; |
| 426 |
| 427 if (stack_size > stack_copy_buffer_size_) |
| 428 return; |
| 429 |
| 430 (*annotator_)(sample); |
| 431 |
| 432 CopyStackAndRewritePointers( |
| 433 reinterpret_cast<uintptr_t*>(stack_copy_buffer_.get()), |
| 434 reinterpret_cast<uintptr_t*>(stack_bottom), |
| 435 reinterpret_cast<uintptr_t*>(stack_top), &thread_state); |
| 436 |
| 437 new_stack_top = |
| 438 reinterpret_cast<uintptr_t>(stack_copy_buffer_.get()) + stack_size; |
| 439 } // ScopedSuspendThread |
| 440 |
| 441 if (test_delegate_) |
| 442 test_delegate_->OnPreStackWalk(); |
| 443 |
| 444 // Walk the stack and record it. |
| 445 |
| 446 // Reserve enough memory for most stacks, to avoid repeated allocations. |
| 447 // Approximately 99.9% of recorded stacks are 128 frames or fewer. |
| 448 sample->frames.reserve(128); |
| 449 |
| 450 auto* current_modules = current_modules_; |
| 451 auto* profile_module_index = &profile_module_index_; |
| 452 WalkStack( |
| 453 thread_state, new_stack_top, |
| 454 [sample, current_modules, profile_module_index](uintptr_t frame_ip) { |
| 455 sample->frames.push_back(StackSamplingProfiler::Frame( |
| 456 frame_ip, |
| 457 GetModuleIndex(frame_ip, current_modules, profile_module_index))); |
| 458 }); |
| 459 } |
| 460 |
| 461 } // namespace |
| 462 |
| 463 std::unique_ptr<NativeStackSampler> NativeStackSampler::Create( |
| 464 PlatformThreadId thread_id, |
| 465 AnnotateCallback annotator, |
| 466 NativeStackSamplerTestDelegate* test_delegate) { |
| 467 return base::MakeUnique<NativeStackSamplerMac>(thread_id, annotator, |
| 468 test_delegate); |
| 469 } |
| 470 |
| 471 } // namespace base |
OLD | NEW |