xref: /aosp_15_r20/external/cronet/base/profiler/stack_sampling_profiler_java_test_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 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 #include "base/profiler/stack_sampling_profiler_java_test_util.h"
6 
7 #include "base/base_profiler_test_support_jni/TestSupport_jni.h"
8 #include "base/location.h"
9 
10 namespace base {
11 
12 namespace {
13 
14 struct UnwinderJavaTestSupportParams {
15   OnceClosure closure;
16   FunctionAddressRange range;
17 };
18 
19 }  // namespace
20 
JNI_TestSupport_InvokeCallbackFunction(JNIEnv * env,jlong context)21 void JNI_TestSupport_InvokeCallbackFunction(JNIEnv* env, jlong context) {
22   const void* start_program_counter = GetProgramCounter();
23 
24   UnwinderJavaTestSupportParams* params =
25       reinterpret_cast<UnwinderJavaTestSupportParams*>(context);
26   if (!params->closure.is_null()) {
27     std::move(params->closure).Run();
28   }
29 
30   // Volatile to prevent a tail call to GetProgramCounter().
31   const void* volatile end_program_counter = GetProgramCounter();
32 
33   params->range = {start_program_counter, end_program_counter};
34 }
35 
callWithJavaFunction(OnceClosure closure)36 FunctionAddressRange callWithJavaFunction(OnceClosure closure) {
37   JNIEnv* env = jni_zero::AttachCurrentThread();
38   UnwinderJavaTestSupportParams params{std::move(closure), {}};
39   base::Java_TestSupport_callWithJavaFunction(
40       env, reinterpret_cast<uintptr_t>(&params));
41   return params.range;
42 }
43 
44 }  // namespace base
45