OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/debug/profiler.h" | 5 #include "base/debug/profiler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 | 12 |
| 13 #if defined(OS_WIN) |
| 14 #include "base/win/pe_image.h" |
| 15 #endif // defined(OS_WIN) |
| 16 |
13 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC) | 17 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC) |
14 #include "third_party/tcmalloc/chromium/src/google/profiler.h" | 18 #include "third_party/tcmalloc/chromium/src/google/profiler.h" |
15 #endif | 19 #endif |
16 | 20 |
17 namespace base { | 21 namespace base { |
18 namespace debug { | 22 namespace debug { |
19 | 23 |
20 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC) | 24 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC) |
21 | 25 |
22 static int profile_count = 0; | 26 static int profile_count = 0; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 65 |
62 bool BeingProfiled() { | 66 bool BeingProfiled() { |
63 return false; | 67 return false; |
64 } | 68 } |
65 | 69 |
66 void RestartProfilingAfterFork() { | 70 void RestartProfilingAfterFork() { |
67 } | 71 } |
68 | 72 |
69 #endif | 73 #endif |
70 | 74 |
| 75 #if !defined(OS_WIN) |
| 76 |
| 77 bool IsBinaryInstrumented() { |
| 78 return false; |
| 79 } |
| 80 |
| 81 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 #else // defined(OS_WIN) |
| 86 |
| 87 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
| 88 extern "C" IMAGE_DOS_HEADER __ImageBase; |
| 89 |
| 90 bool IsBinaryInstrumented() { |
| 91 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); |
| 92 base::win::PEImage image(this_module); |
| 93 |
| 94 // This should be self-evident, soon as we're executing. |
| 95 DCHECK(image.VerifyMagic()); |
| 96 |
| 97 // Syzygy-instrumented binaries contain a PE image section named ".thunks", |
| 98 // and all Syzygy-modified binaries contain the ".syzygy" image section. |
| 99 // This is a very fast check, as it only looks at the image header. |
| 100 return (image.GetImageSectionHeaderByName(".thunks") != NULL) && |
| 101 (image.GetImageSectionHeaderByName(".syzygy") != NULL); |
| 102 } |
| 103 |
| 104 // Callback function to PEImage::EnumImportChunks. |
| 105 static bool FindResolutionFunctionInImports( |
| 106 const base::win::PEImage &image, const char* module_name, |
| 107 PIMAGE_THUNK_DATA unused_name_table, PIMAGE_THUNK_DATA import_address_table, |
| 108 PVOID cookie) { |
| 109 // Our import address table contains pointers to the functions we import |
| 110 // at this point. Let's retrieve the first such function and use it to |
| 111 // find the module this import was resolved to by the loader. |
| 112 const wchar_t* function_in_module = |
| 113 reinterpret_cast<const wchar_t*>(import_address_table->u1.Function); |
| 114 |
| 115 // Retrieve the module by a function in the module. |
| 116 const DWORD kFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 117 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; |
| 118 HMODULE module = NULL; |
| 119 if (!::GetModuleHandleEx(kFlags, function_in_module, &module)) { |
| 120 // This can happen if someone IAT patches us to a thunk. |
| 121 return true; |
| 122 } |
| 123 |
| 124 // See whether this module exports the function we're looking for. |
| 125 ReturnAddressLocationResolver exported_func = |
| 126 reinterpret_cast<ReturnAddressLocationResolver>( |
| 127 ::GetProcAddress(module, "ResolveReturnAddressLocation")); |
| 128 |
| 129 if (exported_func != NULL) { |
| 130 ReturnAddressLocationResolver* resolver_func = |
| 131 reinterpret_cast<ReturnAddressLocationResolver*>(cookie); |
| 132 DCHECK(resolver_func != NULL); |
| 133 DCHECK(*resolver_func == NULL); |
| 134 |
| 135 // We found it, return the function and terminate the enumeration. |
| 136 *resolver_func = exported_func; |
| 137 return false; |
| 138 } |
| 139 |
| 140 // Keep going. |
| 141 return true; |
| 142 } |
| 143 |
| 144 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { |
| 145 if (!IsBinaryInstrumented()) |
| 146 return NULL; |
| 147 |
| 148 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); |
| 149 base::win::PEImage image(this_module); |
| 150 |
| 151 ReturnAddressLocationResolver resolver_func = NULL; |
| 152 image.EnumImportChunks(FindResolutionFunctionInImports, &resolver_func); |
| 153 |
| 154 return resolver_func; |
| 155 } |
| 156 |
| 157 #endif // defined(OS_WIN) |
| 158 |
71 } // namespace debug | 159 } // namespace debug |
72 } // namespace base | 160 } // namespace base |
OLD | NEW |