13a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Copyright (c) 2012 The Chromium Authors. All rights reserved. 23a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Use of this source code is governed by a BSD-style license that can be 33a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// found in the LICENSE file. 43a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 53a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli#ifndef BASE_DEBUG_PROFILER_H_ 63a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli#define BASE_DEBUG_PROFILER_H_ 73a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 83a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli#include <stddef.h> 93a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 103a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli#include <string> 113a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 123a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli#include "base/base_export.h" 133a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 143a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// The Profiler functions allow usage of the underlying sampling based 153a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// profiler. If the application has not been built with the necessary 163a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// flags (-DENABLE_PROFILING and not -DNO_TCMALLOC) then these functions 173a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// are noops. 183a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellinamespace base { 193a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellinamespace debug { 203a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 213a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Start profiling with the supplied name. 223a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// {pid} will be replaced by the process' pid and {count} will be replaced 233a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// by the count of the profile run (starts at 1 with each process). 243a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT void StartProfiling(const std::string& name); 253a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 263a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Stop profiling and write out data. 273a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT void StopProfiling(); 283a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 293a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Force data to be written to file. 303a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT void FlushProfiling(); 313a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 323a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Returns true if process is being profiled. 333a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT bool BeingProfiled(); 343a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 353a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Reset profiling after a fork, which disables timers. 363a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT void RestartProfilingAfterFork(); 373a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 383a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Returns true iff this executable is instrumented with the Syzygy profiler. 393a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT bool IsBinaryInstrumented(); 403a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 413a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Returns true iff this executable supports profiling. 423a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT bool IsProfilingSupported(); 433a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 443a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// There's a class of profilers that use "return address swizzling" to get a 453a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// hook on function exits. This class of profilers uses some form of entry hook, 463a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// like e.g. binary instrumentation, or a compiler flag, that calls a hook each 473a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// time a function is invoked. The hook then switches the return address on the 483a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// stack for the address of an exit hook function, and pushes the original 493a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// return address to a shadow stack of some type. When in due course the CPU 503a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// executes a return to the exit hook, the exit hook will do whatever work it 513a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// does on function exit, then arrange to return to the original return address. 523a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// This class of profiler does not play well with programs that look at the 533a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// return address, as does e.g. V8. V8 uses the return address to certain 543a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// runtime functions to find the JIT code that called it, and from there finds 553a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// the V8 data structures associated to the JS function involved. 563a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// A return address resolution function is used to fix this. It allows such 573a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// programs to resolve a location on stack where a return address originally 583a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// resided, to the shadow stack location where the profiler stashed it. 593a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellitypedef uintptr_t (*ReturnAddressLocationResolver)( 603a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli uintptr_t return_addr_location); 613a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 623a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// This type declaration must match V8's FunctionEntryHook. 633a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellitypedef void (*DynamicFunctionEntryHook)(uintptr_t function, 643a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli uintptr_t return_addr_location); 653a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 663a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// The functions below here are to support profiling V8-generated code. 673a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// V8 has provisions for generating a call to an entry hook for newly generated 683a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// JIT code, and it can push symbol information on code generation and advise 693a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// when the garbage collector moves code. The functions declarations below here 703a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// make glue between V8's facilities and a profiler. 713a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 723a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// This type declaration must match V8's FunctionEntryHook. 733a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellitypedef void (*DynamicFunctionEntryHook)(uintptr_t function, 743a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli uintptr_t return_addr_location); 753a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 763a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellitypedef void (*AddDynamicSymbol)(const void* address, 773a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli size_t length, 783a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli const char* name, 793a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli size_t name_len); 803a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civellitypedef void (*MoveDynamicSymbol)(const void* address, const void* new_address); 813a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 823a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 833a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// If this binary is instrumented and the instrumentation supplies a function 843a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// for each of those purposes, find and return the function in question. 853a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli// Otherwise returns NULL. 863a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc(); 873a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT DynamicFunctionEntryHook GetProfilerDynamicFunctionEntryHookFunc(); 883a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT AddDynamicSymbol GetProfilerAddDynamicSymbolFunc(); 893a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay CivelliBASE_EXPORT MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc(); 903a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 913a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli} // namespace debug 923a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli} // namespace base 933a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli 943a83cddbf6d8fe9c9d70d01e008ff8e86a823cb6Jay Civelli#endif // BASE_DEBUG_PROFILER_H_ 95