Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: base/debug/profiler.cc

Issue 1852143002: win: Remove GetModuleFromAddress, deduplicate __ImageBase code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/debug/debugging_flags.h" 9 #include "base/debug/debugging_flags.h"
10 #include "base/process/process_handle.h" 10 #include "base/process/process_handle.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 14
15 #if defined(OS_WIN) 15 #if defined(OS_WIN)
16 #include "base/win/current_module.h"
16 #include "base/win/pe_image.h" 17 #include "base/win/pe_image.h"
17 #endif // defined(OS_WIN) 18 #endif // defined(OS_WIN)
18 19
19 // TODO(peria): Enable profiling on Windows. 20 // TODO(peria): Enable profiling on Windows.
20 #if BUILDFLAG(ENABLE_PROFILING) && !defined(NO_TCMALLOC) && !defined(OS_WIN) 21 #if BUILDFLAG(ENABLE_PROFILING) && !defined(NO_TCMALLOC) && !defined(OS_WIN)
21 #include "third_party/tcmalloc/chromium/src/gperftools/profiler.h" 22 #include "third_party/tcmalloc/chromium/src/gperftools/profiler.h"
22 #endif 23 #endif
23 24
24 namespace base { 25 namespace base {
25 namespace debug { 26 namespace debug {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 AddDynamicSymbol GetProfilerAddDynamicSymbolFunc() { 102 AddDynamicSymbol GetProfilerAddDynamicSymbolFunc() {
102 return NULL; 103 return NULL;
103 } 104 }
104 105
105 MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() { 106 MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() {
106 return NULL; 107 return NULL;
107 } 108 }
108 109
109 #else // defined(OS_WIN) 110 #else // defined(OS_WIN)
110 111
111 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
112 extern "C" IMAGE_DOS_HEADER __ImageBase;
113
114 bool IsBinaryInstrumented() { 112 bool IsBinaryInstrumented() {
115 enum InstrumentationCheckState { 113 enum InstrumentationCheckState {
116 UNINITIALIZED, 114 UNINITIALIZED,
117 INSTRUMENTED_IMAGE, 115 INSTRUMENTED_IMAGE,
118 NON_INSTRUMENTED_IMAGE, 116 NON_INSTRUMENTED_IMAGE,
119 }; 117 };
120 118
121 static InstrumentationCheckState state = UNINITIALIZED; 119 static InstrumentationCheckState state = UNINITIALIZED;
122 120
123 if (state == UNINITIALIZED) { 121 if (state == UNINITIALIZED) {
124 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); 122 base::win::PEImage image(CURRENT_MODULE());
125 base::win::PEImage image(this_module);
126 123
127 // Check to be sure our image is structured as we'd expect. 124 // Check to be sure our image is structured as we'd expect.
128 DCHECK(image.VerifyMagic()); 125 DCHECK(image.VerifyMagic());
129 126
130 // Syzygy-instrumented binaries contain a PE image section named ".thunks", 127 // Syzygy-instrumented binaries contain a PE image section named ".thunks",
131 // and all Syzygy-modified binaries contain the ".syzygy" image section. 128 // and all Syzygy-modified binaries contain the ".syzygy" image section.
132 // This is a very fast check, as it only looks at the image header. 129 // This is a very fast check, as it only looks at the image header.
133 if ((image.GetImageSectionHeaderByName(".thunks") != NULL) && 130 if ((image.GetImageSectionHeaderByName(".thunks") != NULL) &&
134 (image.GetImageSectionHeaderByName(".syzygy") != NULL)) { 131 (image.GetImageSectionHeaderByName(".syzygy") != NULL)) {
135 state = INSTRUMENTED_IMAGE; 132 state = INSTRUMENTED_IMAGE;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 182
186 // Keep going. 183 // Keep going.
187 return true; 184 return true;
188 } 185 }
189 186
190 template <typename FunctionType> 187 template <typename FunctionType>
191 FunctionType FindFunctionInImports(const char* function_name) { 188 FunctionType FindFunctionInImports(const char* function_name) {
192 if (!IsBinaryInstrumented()) 189 if (!IsBinaryInstrumented())
193 return NULL; 190 return NULL;
194 191
195 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); 192 base::win::PEImage image(CURRENT_MODULE());
196 base::win::PEImage image(this_module);
197 193
198 FunctionSearchContext ctx = { function_name, NULL }; 194 FunctionSearchContext ctx = { function_name, NULL };
199 image.EnumImportChunks(FindResolutionFunctionInImports, &ctx); 195 image.EnumImportChunks(FindResolutionFunctionInImports, &ctx);
200 196
201 return reinterpret_cast<FunctionType>(ctx.function); 197 return reinterpret_cast<FunctionType>(ctx.function);
202 } 198 }
203 199
204 } // namespace 200 } // namespace
205 201
206 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { 202 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() {
(...skipping 13 matching lines...) Expand all
220 216
221 MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() { 217 MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() {
222 return FindFunctionInImports<MoveDynamicSymbol>( 218 return FindFunctionInImports<MoveDynamicSymbol>(
223 "MoveDynamicSymbol"); 219 "MoveDynamicSymbol");
224 } 220 }
225 221
226 #endif // defined(OS_WIN) 222 #endif // defined(OS_WIN)
227 223
228 } // namespace debug 224 } // namespace debug
229 } // namespace base 225 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698