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. 4 5 #ifndef BASE_DEBUG_PROFILER_H_ 6 #define BASE_DEBUG_PROFILER_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 12 #include "base/base_export.h" 13 14 // The Profiler functions allow usage of the underlying sampling based 15 // profiler. If the application has not been built with the necessary 16 // flags (-DENABLE_PROFILING and not -DNO_TCMALLOC) then these functions 17 // are noops. 18 namespace base { 19 namespace debug { 20 21 // Start profiling with the supplied name. 22 // {pid} will be replaced by the process' pid and {count} will be replaced 23 // by the count of the profile run (starts at 1 with each process). 24 BASE_EXPORT void StartProfiling(const std::string& name); 25 26 // Stop profiling and write out data. 27 BASE_EXPORT void StopProfiling(); 28 29 // Force data to be written to file. 30 BASE_EXPORT void FlushProfiling(); 31 32 // Returns true if process is being profiled. 33 BASE_EXPORT bool BeingProfiled(); 34 35 // Reset profiling after a fork, which disables timers. 36 BASE_EXPORT void RestartProfilingAfterFork(); 37 38 // Returns true iff this executable is instrumented with the Syzygy profiler. 39 BASE_EXPORT bool IsBinaryInstrumented(); 40 41 // Returns true iff this executable supports profiling. 42 BASE_EXPORT bool IsProfilingSupported(); 43 44 // There's a class of profilers that use "return address swizzling" to get a 45 // hook on function exits. This class of profilers uses some form of entry hook, 46 // like e.g. binary instrumentation, or a compiler flag, that calls a hook each 47 // time a function is invoked. The hook then switches the return address on the 48 // stack for the address of an exit hook function, and pushes the original 49 // return address to a shadow stack of some type. When in due course the CPU 50 // executes a return to the exit hook, the exit hook will do whatever work it 51 // does on function exit, then arrange to return to the original return address. 52 // This class of profiler does not play well with programs that look at the 53 // return address, as does e.g. V8. V8 uses the return address to certain 54 // runtime functions to find the JIT code that called it, and from there finds 55 // the V8 data structures associated to the JS function involved. 56 // A return address resolution function is used to fix this. It allows such 57 // programs to resolve a location on stack where a return address originally 58 // resided, to the shadow stack location where the profiler stashed it. 59 typedef uintptr_t (*ReturnAddressLocationResolver)( 60 uintptr_t return_addr_location); 61 62 // This type declaration must match V8's FunctionEntryHook. 63 typedef void (*DynamicFunctionEntryHook)(uintptr_t function, 64 uintptr_t return_addr_location); 65 66 // The functions below here are to support profiling V8-generated code. 67 // V8 has provisions for generating a call to an entry hook for newly generated 68 // JIT code, and it can push symbol information on code generation and advise 69 // when the garbage collector moves code. The functions declarations below here 70 // make glue between V8's facilities and a profiler. 71 72 // This type declaration must match V8's FunctionEntryHook. 73 typedef void (*DynamicFunctionEntryHook)(uintptr_t function, 74 uintptr_t return_addr_location); 75 76 typedef void (*AddDynamicSymbol)(const void* address, 77 size_t length, 78 const char* name, 79 size_t name_len); 80 typedef void (*MoveDynamicSymbol)(const void* address, const void* new_address); 81 82 83 // If this binary is instrumented and the instrumentation supplies a function 84 // for each of those purposes, find and return the function in question. 85 // Otherwise returns NULL. 86 BASE_EXPORT ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc(); 87 BASE_EXPORT DynamicFunctionEntryHook GetProfilerDynamicFunctionEntryHookFunc(); 88 BASE_EXPORT AddDynamicSymbol GetProfilerAddDynamicSymbolFunc(); 89 BASE_EXPORT MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc(); 90 91 } // namespace debug 92 } // namespace base 93 94 #endif // BASE_DEBUG_PROFILER_H_ 95