OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx | |
78 extern "C" IMAGE_DOS_HEADER __ImageBase; | |
79 | |
80 bool IsBinaryInstrumented() { | |
81 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); | |
82 base::win::PEImage image(this_module); | |
83 | |
84 // This should be self-evident, soon as we're executing. | |
85 DCHECK(image.VerifyMagic()); | |
86 | |
87 // Syzygy-instrumented binaries contain a PE image section named ".thunks", | |
88 // and all Syzygy-modified binaries contain the ".syzygy" image section. | |
89 // This is a very fast check, as it only looks at the image header. | |
90 return (image.GetImageSectionHeaderByName(".thunks") != NULL) && | |
91 (image.GetImageSectionHeaderByName(".syzygy") != NULL); | |
92 } | |
93 | |
94 static bool FindResolutionFunctionInImports(const base::win::PEImage &image, | |
95 const char* module_name, | |
96 PIMAGE_THUNK_DATA name_table, | |
jar (doing other things)
2012/02/27 22:44:10
Why was name_table passed in.... as it is never us
Sigurður Ásgeirsson
2012/02/28 16:31:40
This is a callback function to the PEImage class,
| |
97 PIMAGE_THUNK_DATA iat, | |
jar (doing other things)
2012/02/27 22:44:10
Avoiding the abreviation iat might help with reada
Sigurður Ásgeirsson
2012/02/28 16:31:40
Done.
| |
98 PVOID cookie) { | |
99 ReturnAddressLocationResolver* resolver_func = | |
100 reinterpret_cast<ReturnAddressLocationResolver*>(cookie); | |
101 DCHECK(resolver_func != NULL); | |
102 DCHECK(*resolver_func == NULL); | |
jar (doing other things)
2012/02/27 22:44:10
Could lines 99-102 be moved closer to the first (o
Sigurður Ásgeirsson
2012/02/28 16:31:40
Done.
| |
103 | |
104 // Our import address table contains pointers to the functions we import | |
105 // at this point. Let's retrieve the first such function and use it to | |
106 // find the module we were linked with. | |
107 const wchar_t* function_in_module = | |
108 reinterpret_cast<const wchar_t*>(iat[0].u1.Function); | |
109 | |
110 // Retrieve the module by a function in it. | |
111 const DWORD kFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | |
112 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; | |
113 HMODULE module = NULL; | |
114 if (!::GetModuleHandleEx(kFlags, function_in_module, &module)) { | |
115 // This can happen if someone IAT patches us to a thunk. | |
116 return true; | |
117 } | |
118 | |
119 // See whether this module exports the function we're looking for. | |
120 ReturnAddressLocationResolver exported_func = | |
121 reinterpret_cast<ReturnAddressLocationResolver>( | |
122 ::GetProcAddress(module, "ResolveReturnAddressLocation")); | |
123 | |
124 if (exported_func != NULL) { | |
125 // We found it, return the function and terminate the enumeration. | |
126 *resolver_func = exported_func; | |
127 return false; | |
128 } | |
129 | |
130 // Keep going. | |
131 return true; | |
132 } | |
133 | |
134 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { | |
135 if (!IsBinaryInstrumented()) | |
136 return NULL; | |
137 | |
138 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); | |
139 base::win::PEImage image(this_module); | |
140 | |
141 ReturnAddressLocationResolver resolver_func = NULL; | |
142 image.EnumImportChunks(FindResolutionFunctionInImports, &resolver_func); | |
143 | |
144 return resolver_func; | |
145 } | |
146 | |
147 #else // defined(OS_WIN) | |
148 | |
149 bool IsBinaryInstrumented() { | |
jar (doing other things)
2012/02/27 22:44:10
silly personal nit: I always like the shortest bra
Sigurður Ásgeirsson
2012/02/28 16:31:40
Done.
| |
150 return false; | |
151 } | |
152 | |
153 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { | |
154 return NULL; | |
155 } | |
156 | |
157 #endif // defined(OS_WIN) | |
158 | |
71 } // namespace debug | 159 } // namespace debug |
72 } // namespace base | 160 } // namespace base |
OLD | NEW |