xref: /aosp_15_r20/system/extras/toolchain-extras/profile-extras.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <signal.h>
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include "profile-extras.h"
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker extern "C" {
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker void __gcov_dump(void);
24*288bf522SAndroid Build Coastguard Worker void __gcov_reset(void);
25*288bf522SAndroid Build Coastguard Worker 
26*288bf522SAndroid Build Coastguard Worker // storing SIG_ERR helps us detect (unlikely) looping.
27*288bf522SAndroid Build Coastguard Worker static sighandler_t chained_gcov_signal_handler = SIG_ERR;
28*288bf522SAndroid Build Coastguard Worker 
gcov_signal_handler(int signum)29*288bf522SAndroid Build Coastguard Worker static void gcov_signal_handler(int signum) {
30*288bf522SAndroid Build Coastguard Worker   __gcov_dump();
31*288bf522SAndroid Build Coastguard Worker   __gcov_reset();
32*288bf522SAndroid Build Coastguard Worker   if (chained_gcov_signal_handler != SIG_ERR &&
33*288bf522SAndroid Build Coastguard Worker       chained_gcov_signal_handler != SIG_IGN &&
34*288bf522SAndroid Build Coastguard Worker       chained_gcov_signal_handler != SIG_DFL) {
35*288bf522SAndroid Build Coastguard Worker     (chained_gcov_signal_handler)(signum);
36*288bf522SAndroid Build Coastguard Worker   }
37*288bf522SAndroid Build Coastguard Worker }
38*288bf522SAndroid Build Coastguard Worker 
39*288bf522SAndroid Build Coastguard Worker __attribute__((weak)) int init_profile_extras_once = 0;
40*288bf522SAndroid Build Coastguard Worker 
41*288bf522SAndroid Build Coastguard Worker // Initialize libprofile-extras:
42*288bf522SAndroid Build Coastguard Worker // - Install a signal handler that triggers __gcov_flush on <COVERAGE_FLUSH_SIGNAL>.
43*288bf522SAndroid Build Coastguard Worker //
44*288bf522SAndroid Build Coastguard Worker // We want this initiazlier to run during load time.
45*288bf522SAndroid Build Coastguard Worker //
46*288bf522SAndroid Build Coastguard Worker // Just marking init_profile_extras() with __attribute__((constructor)) isn't
47*288bf522SAndroid Build Coastguard Worker // enough since the linker drops it from its output since no other symbol from
48*288bf522SAndroid Build Coastguard Worker // this static library is referenced.
49*288bf522SAndroid Build Coastguard Worker //
50*288bf522SAndroid Build Coastguard Worker // We force the linker to include init_profile_extras() by passing
51*288bf522SAndroid Build Coastguard Worker // '-uinit_profile_extras' to the linker (in build/soong).
init_profile_extras(void)52*288bf522SAndroid Build Coastguard Worker __attribute__((constructor)) int init_profile_extras(void) {
53*288bf522SAndroid Build Coastguard Worker   if (init_profile_extras_once)
54*288bf522SAndroid Build Coastguard Worker     return 0;
55*288bf522SAndroid Build Coastguard Worker   init_profile_extras_once = 1;
56*288bf522SAndroid Build Coastguard Worker 
57*288bf522SAndroid Build Coastguard Worker   // is this instance already registered?
58*288bf522SAndroid Build Coastguard Worker   if (chained_gcov_signal_handler != SIG_ERR) {
59*288bf522SAndroid Build Coastguard Worker     return -1;
60*288bf522SAndroid Build Coastguard Worker   }
61*288bf522SAndroid Build Coastguard Worker   sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, gcov_signal_handler);
62*288bf522SAndroid Build Coastguard Worker   if (ret1 == SIG_ERR) {
63*288bf522SAndroid Build Coastguard Worker     return -1;
64*288bf522SAndroid Build Coastguard Worker   }
65*288bf522SAndroid Build Coastguard Worker   chained_gcov_signal_handler = ret1;
66*288bf522SAndroid Build Coastguard Worker 
67*288bf522SAndroid Build Coastguard Worker   return 0;
68*288bf522SAndroid Build Coastguard Worker }
69*288bf522SAndroid Build Coastguard Worker }
70