| 6 |
| 7 | #include <string> |
| 8 |
| 9 | #include "base/allocator/buildflags.h" |
| 10 | #include "base/debug/debugging_buildflags.h" |
| 11 | #include "base/process/process_handle.h" |
| 12 | #include "base/strings/string_number_conversions.h" |
| 13 | #include "base/strings/string_util.h" |
| 14 | #include "build/build_config.h" |
| 15 |
| 16 | #if defined(OS_WIN) |
| 17 | #include "base/win/current_module.h" |
| 18 | #include "base/win/pe_image.h" |
| 19 | #endif // defined(OS_WIN) |
| 20 |
| 21 | // TODO(peria): Enable profiling on Windows. |
| 22 | #if BUILDFLAG(ENABLE_PROFILING) && BUILDFLAG(USE_TCMALLOC) && !defined(OS_WIN) |
| 23 | #include "third_party/tcmalloc/chromium/src/gperftools/profiler.h" |
| 24 | #endif |
| 25 |
| 26 | namespace base { |
| 27 | namespace debug { |
| 28 |
| 29 | // TODO(peria): Enable profiling on Windows. |
| 30 | #if BUILDFLAG(ENABLE_PROFILING) && BUILDFLAG(USE_TCMALLOC) && !defined(OS_WIN) |
| 31 |
| 32 | static int profile_count = 0; |
| 33 |
| 34 | void StartProfiling(const std::string& name) { |
| 35 | ++profile_count; |
| 36 | std::string full_name(name); |
| 37 | std::string pid = NumberToString(GetCurrentProcId()); |
| 38 | std::string count = NumberToString(profile_count); |
| 39 | ReplaceSubstringsAfterOffset(&full_name, 0, "{pid}", pid); |
| 40 | ReplaceSubstringsAfterOffset(&full_name, 0, "{count}", count); |
| 41 | ProfilerStart(full_name.c_str()); |
| 42 | } |
| 43 |
| 44 | void StopProfiling() { |
| 45 | ProfilerFlush(); |
| 46 | ProfilerStop(); |
| 47 | } |
| 48 |
| 49 | void FlushProfiling() { |
| 50 | ProfilerFlush(); |
| 51 | } |
| 52 |
| 53 | bool BeingProfiled() { |
| 54 | return ProfilingIsEnabledForAllThreads(); |
| 55 | } |
| 56 |
| 57 | void RestartProfilingAfterFork() { |
| 58 | ProfilerRegisterThread(); |
| 59 | } |
| 60 |
| 61 | bool IsProfilingSupported() { |
| 62 | return true; |
| 63 | } |
| 64 |
| 65 | #else |
| 66 |
| 67 | void StartProfiling(const std::string& name) { |
| 68 | } |
| 69 |
| 70 | void StopProfiling() { |
| 71 | } |
| 72 |
| 73 | void FlushProfiling() { |
| 74 | } |
| 75 |
| 76 | bool BeingProfiled() { |
| 77 | return false; |
| 78 | } |
| 79 |
| 80 | void RestartProfilingAfterFork() { |
| 81 | } |
| 82 |
| 83 | bool IsProfilingSupported() { |
| 84 | return false; |
| 85 | } |
| 86 |
| 87 | #endif |
| 88 |
| 89 | #if !defined(OS_WIN) |
| 90 |
| 91 | ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { |
| 92 | return nullptr; |
| 93 | } |
| 94 |
| 95 | AddDynamicSymbol GetProfilerAddDynamicSymbolFunc() { |
| 96 | return nullptr; |
| 97 | } |
| 98 |
| 99 | MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() { |
| 100 | return nullptr; |
| 101 | } |
| 102 |
| 103 | #else // defined(OS_WIN) |
| 104 |
| 105 | namespace { |
| 106 |
| 107 | struct FunctionSearchContext { |
| 108 | const char* name; |
| 109 | FARPROC function; |
| 110 | }; |
| 111 |
| 112 | // Callback function to PEImage::EnumImportChunks. |
| 113 | bool FindResolutionFunctionInImports( |
| 114 | const base::win::PEImage &image, const char* module_name, |
| 115 | PIMAGE_THUNK_DATA unused_name_table, PIMAGE_THUNK_DATA import_address_table, |
| 116 | PVOID cookie) { |
| 117 | FunctionSearchContext* context = |
| 118 | reinterpret_cast<FunctionSearchContext*>(cookie); |
| 119 |
| 120 | DCHECK(context); |
| 121 | DCHECK(!context->function); |
| 122 |
| 123 | // Our import address table contains pointers to the functions we import |
| 124 | // at this point. Let's retrieve the first such function and use it to |
| 125 | // find the module this import was resolved to by the loader. |
| 126 | const wchar_t* function_in_module = |
| 127 | reinterpret_cast<const wchar_t*>(import_address_table->u1.Function); |
| 128 |
| 129 | // Retrieve the module by a function in the module. |
| 130 | const DWORD kFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 131 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT; |
| 132 | HMODULE module = NULL; |
| 133 | if (!::GetModuleHandleEx(kFlags, function_in_module, &module)) { |
| 134 | // This can happen if someone IAT patches us to a thunk. |
| 135 | return true; |
| 136 | } |
| 137 |
| 138 | // See whether this module exports the function we're looking for. |
| 139 | FARPROC exported_func = ::GetProcAddress(module, context->name); |
| 140 | if (exported_func != NULL) { |
| 141 | // We found it, return the function and terminate the enumeration. |
| 142 | context->function = exported_func; |
| 143 | return false; |
| 144 | } |
| 145 |
| 146 | // Keep going. |
| 147 | return true; |
| 148 | } |
| 149 |
| 150 | template <typename FunctionType> |
| 151 | FunctionType FindFunctionInImports(const char* function_name) { |
| 152 | base::win::PEImage image(CURRENT_MODULE()); |
| 153 |
| 154 | FunctionSearchContext ctx = { function_name, NULL }; |
| 155 | image.EnumImportChunks(FindResolutionFunctionInImports, &ctx, nullptr); |
| 156 |
| 157 | return reinterpret_cast<FunctionType>(ctx.function); |
| 158 | } |
| 159 |
| 160 | } // namespace |
| 161 |
| 162 | ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc() { |
| 163 | return FindFunctionInImports<ReturnAddressLocationResolver>( |
| 164 | "ResolveReturnAddressLocation"); |
| 165 | } |
| 166 |
| 167 | AddDynamicSymbol GetProfilerAddDynamicSymbolFunc() { |
| 168 | return FindFunctionInImports<AddDynamicSymbol>( |
| 169 | "AddDynamicSymbol"); |
| 170 | } |
| 171 |
| 172 | MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc() { |
| 173 | return FindFunctionInImports<MoveDynamicSymbol>( |
| 174 | "MoveDynamicSymbol"); |
| 175 | } |
| 176 |
| 177 | #endif // defined(OS_WIN) |
| 178 |
| 179 | } // namespace debug |
| 180 | } // namespace base |
| 181 |