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