OLD | NEW |
1 // Copyright (c) 2012 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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 #if !defined(OS_WIN) | 75 #if !defined(OS_WIN) |
76 | 76 |
77 bool IsBinaryInstrumented() { | 77 bool IsBinaryInstrumented() { |
78 return false; | 78 return false; |
79 } | 79 } |
80 | 80 |
81 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { | 81 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { |
82 return NULL; | 82 return NULL; |
83 } | 83 } |
84 | 84 |
| 85 DynamicFunctionEntryHook GetProfilerDynamicFunctionEntryHookFunc() { |
| 86 return NULL; |
| 87 } |
| 88 |
| 89 AddDynamicSymbol GetProfilerAddDynamicSymbolFunc() { |
| 90 return NULL; |
| 91 } |
| 92 |
| 93 MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() { |
| 94 return NULL; |
| 95 } |
| 96 |
85 #else // defined(OS_WIN) | 97 #else // defined(OS_WIN) |
86 | 98 |
87 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx | 99 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
88 extern "C" IMAGE_DOS_HEADER __ImageBase; | 100 extern "C" IMAGE_DOS_HEADER __ImageBase; |
89 | 101 |
90 bool IsBinaryInstrumented() { | 102 bool IsBinaryInstrumented() { |
91 enum InstrumentationCheckState { | 103 enum InstrumentationCheckState { |
92 UNINITIALIZED, | 104 UNINITIALIZED, |
93 INSTRUMENTED_IMAGE, | 105 INSTRUMENTED_IMAGE, |
94 NON_INSTRUMENTED_IMAGE, | 106 NON_INSTRUMENTED_IMAGE, |
(...skipping 16 matching lines...) Expand all Loading... |
111 state = INSTRUMENTED_IMAGE; | 123 state = INSTRUMENTED_IMAGE; |
112 } else { | 124 } else { |
113 state = NON_INSTRUMENTED_IMAGE; | 125 state = NON_INSTRUMENTED_IMAGE; |
114 } | 126 } |
115 } | 127 } |
116 DCHECK(state != UNINITIALIZED); | 128 DCHECK(state != UNINITIALIZED); |
117 | 129 |
118 return state == INSTRUMENTED_IMAGE; | 130 return state == INSTRUMENTED_IMAGE; |
119 } | 131 } |
120 | 132 |
| 133 namespace { |
| 134 |
| 135 struct FunctionSearchContext { |
| 136 const char* name; |
| 137 FARPROC function; |
| 138 }; |
| 139 |
121 // Callback function to PEImage::EnumImportChunks. | 140 // Callback function to PEImage::EnumImportChunks. |
122 static bool FindResolutionFunctionInImports( | 141 bool FindResolutionFunctionInImports( |
123 const base::win::PEImage &image, const char* module_name, | 142 const base::win::PEImage &image, const char* module_name, |
124 PIMAGE_THUNK_DATA unused_name_table, PIMAGE_THUNK_DATA import_address_table, | 143 PIMAGE_THUNK_DATA unused_name_table, PIMAGE_THUNK_DATA import_address_table, |
125 PVOID cookie) { | 144 PVOID cookie) { |
| 145 FunctionSearchContext* context = |
| 146 reinterpret_cast<FunctionSearchContext*>(cookie); |
| 147 |
| 148 DCHECK_NE(static_cast<FunctionSearchContext*>(NULL), context); |
| 149 DCHECK_EQ(static_cast<FARPROC>(NULL), context->function); |
| 150 |
126 // Our import address table contains pointers to the functions we import | 151 // Our import address table contains pointers to the functions we import |
127 // at this point. Let's retrieve the first such function and use it to | 152 // at this point. Let's retrieve the first such function and use it to |
128 // find the module this import was resolved to by the loader. | 153 // find the module this import was resolved to by the loader. |
129 const wchar_t* function_in_module = | 154 const wchar_t* function_in_module = |
130 reinterpret_cast<const wchar_t*>(import_address_table->u1.Function); | 155 reinterpret_cast<const wchar_t*>(import_address_table->u1.Function); |
131 | 156 |
132 // Retrieve the module by a function in the module. | 157 // Retrieve the module by a function in the module. |
133 const DWORD kFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | 158 const DWORD kFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
134 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; | 159 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; |
135 HMODULE module = NULL; | 160 HMODULE module = NULL; |
136 if (!::GetModuleHandleEx(kFlags, function_in_module, &module)) { | 161 if (!::GetModuleHandleEx(kFlags, function_in_module, &module)) { |
137 // This can happen if someone IAT patches us to a thunk. | 162 // This can happen if someone IAT patches us to a thunk. |
138 return true; | 163 return true; |
139 } | 164 } |
140 | 165 |
141 // See whether this module exports the function we're looking for. | 166 // See whether this module exports the function we're looking for. |
142 ReturnAddressLocationResolver exported_func = | 167 FARPROC exported_func = ::GetProcAddress(module, context->name); |
143 reinterpret_cast<ReturnAddressLocationResolver>( | |
144 ::GetProcAddress(module, "ResolveReturnAddressLocation")); | |
145 | |
146 if (exported_func != NULL) { | 168 if (exported_func != NULL) { |
147 ReturnAddressLocationResolver* resolver_func = | |
148 reinterpret_cast<ReturnAddressLocationResolver*>(cookie); | |
149 DCHECK(resolver_func != NULL); | |
150 DCHECK(*resolver_func == NULL); | |
151 | |
152 // We found it, return the function and terminate the enumeration. | 169 // We found it, return the function and terminate the enumeration. |
153 *resolver_func = exported_func; | 170 context->function = exported_func; |
154 return false; | 171 return false; |
155 } | 172 } |
156 | 173 |
157 // Keep going. | 174 // Keep going. |
158 return true; | 175 return true; |
159 } | 176 } |
160 | 177 |
161 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { | 178 template <typename FunctionType> |
| 179 FunctionType FindFunctionInImports(const char* function_name) { |
162 if (!IsBinaryInstrumented()) | 180 if (!IsBinaryInstrumented()) |
163 return NULL; | 181 return NULL; |
164 | 182 |
165 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); | 183 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); |
166 base::win::PEImage image(this_module); | 184 base::win::PEImage image(this_module); |
167 | 185 |
168 ReturnAddressLocationResolver resolver_func = NULL; | 186 FunctionSearchContext ctx = { function_name, NULL }; |
169 image.EnumImportChunks(FindResolutionFunctionInImports, &resolver_func); | 187 image.EnumImportChunks(FindResolutionFunctionInImports, &ctx); |
170 | 188 |
171 return resolver_func; | 189 return reinterpret_cast<FunctionType>(ctx.function); |
| 190 } |
| 191 |
| 192 } // namespace |
| 193 |
| 194 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { |
| 195 return FindFunctionInImports<ReturnAddressLocationResolver>( |
| 196 "ResolveReturnAddressLocation"); |
| 197 } |
| 198 |
| 199 DynamicFunctionEntryHook GetProfilerDynamicFunctionEntryHookFunc() { |
| 200 return FindFunctionInImports<DynamicFunctionEntryHook>( |
| 201 "OnDynamicFunctionEntry"); |
| 202 } |
| 203 |
| 204 AddDynamicSymbol GetProfilerAddDynamicSymbolFunc() { |
| 205 return FindFunctionInImports<AddDynamicSymbol>( |
| 206 "AddDynamicSymbol"); |
| 207 } |
| 208 |
| 209 MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() { |
| 210 return FindFunctionInImports<MoveDynamicSymbol>( |
| 211 "MoveDynamicSymbol"); |
172 } | 212 } |
173 | 213 |
174 #endif // defined(OS_WIN) | 214 #endif // defined(OS_WIN) |
175 | 215 |
176 } // namespace debug | 216 } // namespace debug |
177 } // namespace base | 217 } // namespace base |
OLD | NEW |