xref: /aosp_15_r20/system/extras/toolchain-extras/profile-clang-extras.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2020 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 <errno.h>
18*288bf522SAndroid Build Coastguard Worker #include <signal.h>
19*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include "profile-extras.h"
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker extern "C" {
24*288bf522SAndroid Build Coastguard Worker 
25*288bf522SAndroid Build Coastguard Worker static sighandler_t chained_signal_handler = SIG_ERR;
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker #ifndef __CONTINUOUS_COVERAGE_MODE__
28*288bf522SAndroid Build Coastguard Worker int __llvm_profile_write_file(void);
29*288bf522SAndroid Build Coastguard Worker #endif  // __CONTINUOUS_COVERAGE_MODE__
30*288bf522SAndroid Build Coastguard Worker 
llvm_signal_handler(__unused int signum)31*288bf522SAndroid Build Coastguard Worker static void llvm_signal_handler(__unused int signum) {
32*288bf522SAndroid Build Coastguard Worker   // TODO(pirama) Only disable __llvm_profile_write_file call to begin with.
33*288bf522SAndroid Build Coastguard Worker   // After continuous mode is stable, stop registering the signal handler.
34*288bf522SAndroid Build Coastguard Worker #ifndef __CONTINUOUS_COVERAGE_MODE__
35*288bf522SAndroid Build Coastguard Worker   __llvm_profile_write_file();
36*288bf522SAndroid Build Coastguard Worker #endif  // __CONTINUOUS_COVERAGE_MODE__
37*288bf522SAndroid Build Coastguard Worker 
38*288bf522SAndroid Build Coastguard Worker   if (chained_signal_handler != SIG_ERR && chained_signal_handler != SIG_IGN &&
39*288bf522SAndroid Build Coastguard Worker       chained_signal_handler != SIG_DFL) {
40*288bf522SAndroid Build Coastguard Worker     (chained_signal_handler)(signum);
41*288bf522SAndroid Build Coastguard Worker   }
42*288bf522SAndroid Build Coastguard Worker }
43*288bf522SAndroid Build Coastguard Worker 
44*288bf522SAndroid Build Coastguard Worker // Initialize libprofile-extras:
45*288bf522SAndroid Build Coastguard Worker //
46*288bf522SAndroid Build Coastguard Worker // - Install a signal handler that triggers __llvm_profile_write_file on
47*288bf522SAndroid Build Coastguard Worker // <COVERAGE_FLUSH_SIGNAL>.
48*288bf522SAndroid Build Coastguard Worker //
49*288bf522SAndroid Build Coastguard Worker // We want this initializer to run during load time.  In addition to marking
50*288bf522SAndroid Build Coastguard Worker // this function as a constructor, we link this library with `--whole-archive`
51*288bf522SAndroid Build Coastguard Worker // to force this function to be included in the output.
init_profile_extras(void)52*288bf522SAndroid Build Coastguard Worker static __attribute__((constructor)) int init_profile_extras(void) {
53*288bf522SAndroid Build Coastguard Worker   if (chained_signal_handler != SIG_ERR) {
54*288bf522SAndroid Build Coastguard Worker     return -1;
55*288bf522SAndroid Build Coastguard Worker   }
56*288bf522SAndroid Build Coastguard Worker   sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, llvm_signal_handler);
57*288bf522SAndroid Build Coastguard Worker   if (ret1 == SIG_ERR) {
58*288bf522SAndroid Build Coastguard Worker     return -1;
59*288bf522SAndroid Build Coastguard Worker   }
60*288bf522SAndroid Build Coastguard Worker   chained_signal_handler = ret1;
61*288bf522SAndroid Build Coastguard Worker 
62*288bf522SAndroid Build Coastguard Worker   return 0;
63*288bf522SAndroid Build Coastguard Worker }
64*288bf522SAndroid Build Coastguard Worker }
65