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