Protect WebURLLoaderImpl::Context while receiving responses.
[chromium-blink-merge.git] / base / debug / profiler.cc
blobde7912437263903e0833244bb4ed140419e7acb1
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
3 // found in the LICENSE file.
5 #include "base/debug/profiler.h"
7 #include <string>
9 #include "base/process_util.h"
10 #include "base/string_util.h"
11 #include "base/stringprintf.h"
13 #if defined(OS_WIN)
14 #include "base/win/pe_image.h"
15 #endif // defined(OS_WIN)
17 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC)
18 #include "third_party/tcmalloc/chromium/src/gperftools/profiler.h"
19 #endif
21 namespace base {
22 namespace debug {
24 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC)
26 static int profile_count = 0;
28 void StartProfiling(const std::string& name) {
29 ++profile_count;
30 std::string full_name(name);
31 std::string pid = StringPrintf("%d", GetCurrentProcId());
32 std::string count = StringPrintf("%d", profile_count);
33 ReplaceSubstringsAfterOffset(&full_name, 0, "{pid}", pid);
34 ReplaceSubstringsAfterOffset(&full_name, 0, "{count}", count);
35 ProfilerStart(full_name.c_str());
38 void StopProfiling() {
39 ProfilerFlush();
40 ProfilerStop();
43 void FlushProfiling() {
44 ProfilerFlush();
47 bool BeingProfiled() {
48 return ProfilingIsEnabledForAllThreads();
51 void RestartProfilingAfterFork() {
52 ProfilerRegisterThread();
55 #else
57 void StartProfiling(const std::string& name) {
60 void StopProfiling() {
63 void FlushProfiling() {
66 bool BeingProfiled() {
67 return false;
70 void RestartProfilingAfterFork() {
73 #endif
75 #if !defined(OS_WIN)
77 bool IsBinaryInstrumented() {
78 return false;
81 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() {
82 return NULL;
85 #else // defined(OS_WIN)
87 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
88 extern "C" IMAGE_DOS_HEADER __ImageBase;
90 bool IsBinaryInstrumented() {
91 enum InstrumentationCheckState {
92 UNINITIALIZED,
93 INSTRUMENTED_IMAGE,
94 NON_INSTRUMENTED_IMAGE,
97 static InstrumentationCheckState state = UNINITIALIZED;
99 if (state == UNINITIALIZED) {
100 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
101 base::win::PEImage image(this_module);
103 // Check to be sure our image is structured as we'd expect.
104 DCHECK(image.VerifyMagic());
106 // Syzygy-instrumented binaries contain a PE image section named ".thunks",
107 // and all Syzygy-modified binaries contain the ".syzygy" image section.
108 // This is a very fast check, as it only looks at the image header.
109 if ((image.GetImageSectionHeaderByName(".thunks") != NULL) &&
110 (image.GetImageSectionHeaderByName(".syzygy") != NULL)) {
111 state = INSTRUMENTED_IMAGE;
112 } else {
113 state = NON_INSTRUMENTED_IMAGE;
116 DCHECK(state != UNINITIALIZED);
118 return state == INSTRUMENTED_IMAGE;
121 // Callback function to PEImage::EnumImportChunks.
122 static bool FindResolutionFunctionInImports(
123 const base::win::PEImage &image, const char* module_name,
124 PIMAGE_THUNK_DATA unused_name_table, PIMAGE_THUNK_DATA import_address_table,
125 PVOID cookie) {
126 // 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
128 // find the module this import was resolved to by the loader.
129 const wchar_t* function_in_module =
130 reinterpret_cast<const wchar_t*>(import_address_table->u1.Function);
132 // Retrieve the module by a function in the module.
133 const DWORD kFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
134 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
135 HMODULE module = NULL;
136 if (!::GetModuleHandleEx(kFlags, function_in_module, &module)) {
137 // This can happen if someone IAT patches us to a thunk.
138 return true;
141 // See whether this module exports the function we're looking for.
142 ReturnAddressLocationResolver exported_func =
143 reinterpret_cast<ReturnAddressLocationResolver>(
144 ::GetProcAddress(module, "ResolveReturnAddressLocation"));
146 if (exported_func != NULL) {
147 ReturnAddressLocationResolver* resolver_func =
148 reinterpret_cast<ReturnAddressLocationResolver*>(cookie);
149 DCHECK(resolver_func != NULL);
150 DCHECK(*resolver_func == NULL);
152 // We found it, return the function and terminate the enumeration.
153 *resolver_func = exported_func;
154 return false;
157 // Keep going.
158 return true;
161 ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() {
162 if (!IsBinaryInstrumented())
163 return NULL;
165 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
166 base::win::PEImage image(this_module);
168 ReturnAddressLocationResolver resolver_func = NULL;
169 image.EnumImportChunks(FindResolutionFunctionInImports, &resolver_func);
171 return resolver_func;
174 #endif // defined(OS_WIN)
176 } // namespace debug
177 } // namespace base