| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/profiler/native_stack_sampler.h" |
| 6 |
| 5 #include <objbase.h> | 7 #include <objbase.h> |
| 6 #include <windows.h> | 8 #include <windows.h> |
| 7 #include <stddef.h> | 9 #include <stddef.h> |
| 8 #include <winternl.h> | 10 #include <winternl.h> |
| 9 | 11 |
| 10 #include <cstdlib> | 12 #include <cstdlib> |
| 11 #include <map> | 13 #include <map> |
| 14 #include <memory> |
| 12 #include <utility> | 15 #include <utility> |
| 13 #include <vector> | 16 #include <vector> |
| 14 | 17 |
| 15 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 19 #include "base/logging.h" |
| 17 #include "base/macros.h" | 20 #include "base/macros.h" |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/profiler/native_stack_sampler.h" | |
| 20 #include "base/profiler/win32_stack_frame_unwinder.h" | 21 #include "base/profiler/win32_stack_frame_unwinder.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 25 #include "base/win/pe_image.h" | 26 #include "base/win/pe_image.h" |
| 26 #include "base/win/scoped_handle.h" | 27 #include "base/win/scoped_handle.h" |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 | 30 |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 | 408 |
| 408 win::ScopedHandle thread_handle_; | 409 win::ScopedHandle thread_handle_; |
| 409 | 410 |
| 410 NativeStackSamplerTestDelegate* const test_delegate_; | 411 NativeStackSamplerTestDelegate* const test_delegate_; |
| 411 | 412 |
| 412 // The stack base address corresponding to |thread_handle_|. | 413 // The stack base address corresponding to |thread_handle_|. |
| 413 const void* const thread_stack_base_address_; | 414 const void* const thread_stack_base_address_; |
| 414 | 415 |
| 415 // Buffer to use for copies of the stack. We use the same buffer for all the | 416 // Buffer to use for copies of the stack. We use the same buffer for all the |
| 416 // samples to avoid the overhead of multiple allocations and frees. | 417 // samples to avoid the overhead of multiple allocations and frees. |
| 417 const scoped_ptr<unsigned char[]> stack_copy_buffer_; | 418 const std::unique_ptr<unsigned char[]> stack_copy_buffer_; |
| 418 | 419 |
| 419 // Weak. Points to the modules associated with the profile being recorded | 420 // Weak. Points to the modules associated with the profile being recorded |
| 420 // between ProfileRecordingStarting() and ProfileRecordingStopped(). | 421 // between ProfileRecordingStarting() and ProfileRecordingStopped(). |
| 421 std::vector<StackSamplingProfiler::Module>* current_modules_; | 422 std::vector<StackSamplingProfiler::Module>* current_modules_; |
| 422 | 423 |
| 423 // Maps a module handle to the corresponding Module's index within | 424 // Maps a module handle to the corresponding Module's index within |
| 424 // current_modules_. | 425 // current_modules_. |
| 425 std::map<HMODULE, size_t> profile_module_index_; | 426 std::map<HMODULE, size_t> profile_module_index_; |
| 426 | 427 |
| 427 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin); | 428 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 | 513 |
| 513 for (const RecordedFrame& frame : stack) { | 514 for (const RecordedFrame& frame : stack) { |
| 514 sample->push_back(StackSamplingProfiler::Frame( | 515 sample->push_back(StackSamplingProfiler::Frame( |
| 515 reinterpret_cast<uintptr_t>(frame.instruction_pointer), | 516 reinterpret_cast<uintptr_t>(frame.instruction_pointer), |
| 516 GetModuleIndex(frame.module.Get(), modules))); | 517 GetModuleIndex(frame.module.Get(), modules))); |
| 517 } | 518 } |
| 518 } | 519 } |
| 519 | 520 |
| 520 } // namespace | 521 } // namespace |
| 521 | 522 |
| 522 scoped_ptr<NativeStackSampler> NativeStackSampler::Create( | 523 std::unique_ptr<NativeStackSampler> NativeStackSampler::Create( |
| 523 PlatformThreadId thread_id, | 524 PlatformThreadId thread_id, |
| 524 NativeStackSamplerTestDelegate* test_delegate) { | 525 NativeStackSamplerTestDelegate* test_delegate) { |
| 525 #if _WIN64 | 526 #if _WIN64 |
| 526 // Get the thread's handle. | 527 // Get the thread's handle. |
| 527 HANDLE thread_handle = ::OpenThread( | 528 HANDLE thread_handle = ::OpenThread( |
| 528 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, | 529 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, |
| 529 FALSE, | 530 FALSE, |
| 530 thread_id); | 531 thread_id); |
| 531 | 532 |
| 532 if (thread_handle) { | 533 if (thread_handle) { |
| 533 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( | 534 return std::unique_ptr<NativeStackSampler>(new NativeStackSamplerWin( |
| 534 win::ScopedHandle(thread_handle), | 535 win::ScopedHandle(thread_handle), test_delegate)); |
| 535 test_delegate)); | |
| 536 } | 536 } |
| 537 #endif | 537 #endif |
| 538 return scoped_ptr<NativeStackSampler>(); | 538 return std::unique_ptr<NativeStackSampler>(); |
| 539 } | 539 } |
| 540 | 540 |
| 541 } // namespace base | 541 } // namespace base |
| OLD | NEW |