michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_DEBUG_PROFILER_H michael@0: #define BASE_DEBUG_PROFILER_H michael@0: michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: michael@0: // The Profiler functions allow usage of the underlying sampling based michael@0: // profiler. If the application has not been built with the necessary michael@0: // flags (-DENABLE_PROFILING and not -DNO_TCMALLOC) then these functions michael@0: // are noops. michael@0: namespace base { michael@0: namespace debug { michael@0: michael@0: // Start profiling with the supplied name. michael@0: // {pid} will be replaced by the process' pid and {count} will be replaced michael@0: // by the count of the profile run (starts at 1 with each process). michael@0: BASE_EXPORT void StartProfiling(const std::string& name); michael@0: michael@0: // Stop profiling and write out data. michael@0: BASE_EXPORT void StopProfiling(); michael@0: michael@0: // Force data to be written to file. michael@0: BASE_EXPORT void FlushProfiling(); michael@0: michael@0: // Returns true if process is being profiled. michael@0: BASE_EXPORT bool BeingProfiled(); michael@0: michael@0: // Reset profiling after a fork, which disables timers. michael@0: BASE_EXPORT void RestartProfilingAfterFork(); michael@0: michael@0: // Returns true iff this executable is instrumented with the Syzygy profiler. michael@0: BASE_EXPORT bool IsBinaryInstrumented(); michael@0: michael@0: // There's a class of profilers that use "return address swizzling" to get a michael@0: // hook on function exits. This class of profilers uses some form of entry hook, michael@0: // like e.g. binary instrumentation, or a compiler flag, that calls a hook each michael@0: // time a function is invoked. The hook then switches the return address on the michael@0: // stack for the address of an exit hook function, and pushes the original michael@0: // return address to a shadow stack of some type. When in due course the CPU michael@0: // executes a return to the exit hook, the exit hook will do whatever work it michael@0: // does on function exit, then arrange to return to the original return address. michael@0: // This class of profiler does not play well with programs that look at the michael@0: // return address, as does e.g. V8. V8 uses the return address to certain michael@0: // runtime functions to find the JIT code that called it, and from there finds michael@0: // the V8 data structures associated to the JS function involved. michael@0: // A return address resolution function is used to fix this. It allows such michael@0: // programs to resolve a location on stack where a return address originally michael@0: // resided, to the shadow stack location where the profiler stashed it. michael@0: typedef uintptr_t (*ReturnAddressLocationResolver)( michael@0: uintptr_t return_addr_location); michael@0: michael@0: // This type declaration must match V8's FunctionEntryHook. michael@0: typedef void (*DynamicFunctionEntryHook)(uintptr_t function, michael@0: uintptr_t return_addr_location); michael@0: michael@0: // The functions below here are to support profiling V8-generated code. michael@0: // V8 has provisions for generating a call to an entry hook for newly generated michael@0: // JIT code, and it can push symbol information on code generation and advise michael@0: // when the garbage collector moves code. The functions declarations below here michael@0: // make glue between V8's facilities and a profiler. michael@0: michael@0: // This type declaration must match V8's FunctionEntryHook. michael@0: typedef void (*DynamicFunctionEntryHook)(uintptr_t function, michael@0: uintptr_t return_addr_location); michael@0: michael@0: typedef void (*AddDynamicSymbol)(const void* address, michael@0: size_t length, michael@0: const char* name, michael@0: size_t name_len); michael@0: typedef void (*MoveDynamicSymbol)(const void* address, const void* new_address); michael@0: michael@0: michael@0: // If this binary is instrumented and the instrumentation supplies a function michael@0: // for each of those purposes, find and return the function in question. michael@0: // Otherwise returns NULL. michael@0: BASE_EXPORT ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc(); michael@0: BASE_EXPORT DynamicFunctionEntryHook GetProfilerDynamicFunctionEntryHookFunc(); michael@0: BASE_EXPORT AddDynamicSymbol GetProfilerAddDynamicSymbolFunc(); michael@0: BASE_EXPORT MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc(); michael@0: michael@0: } // namespace debug michael@0: } // namespace base michael@0: michael@0: #endif // BASE_DEBUG_DEBUGGER_H