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"
9 #include "base/process_util.h"
10 #include "base/string_util.h"
11 #include "base/stringprintf.h"
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"
24 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC)
26 static int profile_count
= 0;
28 void StartProfiling(const std::string
& name
) {
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() {
43 void FlushProfiling() {
47 bool BeingProfiled() {
48 return ProfilingIsEnabledForAllThreads();
51 void RestartProfilingAfterFork() {
52 ProfilerRegisterThread();
57 void StartProfiling(const std::string
& name
) {
60 void StopProfiling() {
63 void FlushProfiling() {
66 bool BeingProfiled() {
70 void RestartProfilingAfterFork() {
77 bool IsBinaryInstrumented() {
81 ReturnAddressLocationResolver
GetProfilerReturnAddrResolutionFunc() {
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
{
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
;
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
,
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.
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
;
161 ReturnAddressLocationResolver
GetProfilerReturnAddrResolutionFunc() {
162 if (!IsBinaryInstrumented())
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)