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