profiler.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef BASE_DEBUG_PROFILER_H_
  5. #define BASE_DEBUG_PROFILER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/base_export.h"
  9. // The Profiler functions allow usage of the underlying sampling based
  10. // profiler. If the application has not been built with the necessary
  11. // flags (-DENABLE_PROFILING and not -DNO_TCMALLOC) then these functions
  12. // are noops.
  13. namespace base {
  14. namespace debug {
  15. // Start profiling with the supplied name.
  16. // {pid} will be replaced by the process' pid and {count} will be replaced
  17. // by the count of the profile run (starts at 1 with each process).
  18. BASE_EXPORT void StartProfiling(const std::string& name);
  19. // Stop profiling and write out data.
  20. BASE_EXPORT void StopProfiling();
  21. // Force data to be written to file.
  22. BASE_EXPORT void FlushProfiling();
  23. // Returns true if process is being profiled.
  24. BASE_EXPORT bool BeingProfiled();
  25. // Reset profiling after a fork, which disables timers.
  26. BASE_EXPORT void RestartProfilingAfterFork();
  27. // Returns true iff this executable supports profiling.
  28. BASE_EXPORT bool IsProfilingSupported();
  29. // There's a class of profilers that use "return address swizzling" to get a
  30. // hook on function exits. This class of profilers uses some form of entry hook,
  31. // like e.g. binary instrumentation, or a compiler flag, that calls a hook each
  32. // time a function is invoked. The hook then switches the return address on the
  33. // stack for the address of an exit hook function, and pushes the original
  34. // return address to a shadow stack of some type. When in due course the CPU
  35. // executes a return to the exit hook, the exit hook will do whatever work it
  36. // does on function exit, then arrange to return to the original return address.
  37. // This class of profiler does not play well with programs that look at the
  38. // return address, as does e.g. V8. V8 uses the return address to certain
  39. // runtime functions to find the JIT code that called it, and from there finds
  40. // the V8 data structures associated to the JS function involved.
  41. // A return address resolution function is used to fix this. It allows such
  42. // programs to resolve a location on stack where a return address originally
  43. // resided, to the shadow stack location where the profiler stashed it.
  44. typedef uintptr_t (*ReturnAddressLocationResolver)(
  45. uintptr_t return_addr_location);
  46. typedef void (*AddDynamicSymbol)(const void* address,
  47. size_t length,
  48. const char* name,
  49. size_t name_len);
  50. typedef void (*MoveDynamicSymbol)(const void* address, const void* new_address);
  51. // If this binary is instrumented and the instrumentation supplies a function
  52. // for each of those purposes, find and return the function in question.
  53. // Otherwise returns NULL.
  54. BASE_EXPORT ReturnAddressLocationResolver GetProfilerReturnAddrResolutionFunc();
  55. BASE_EXPORT AddDynamicSymbol GetProfilerAddDynamicSymbolFunc();
  56. BASE_EXPORT MoveDynamicSymbol GetProfilerMoveDynamicSymbolFunc();
  57. } // namespace debug
  58. } // namespace base
  59. #endif // BASE_DEBUG_PROFILER_H_