blob: 6a7766ef1e26fbf1b0c2d256067dea9b94f99c82 [file] [log] [blame]
Pirama Arumuga Nainar13bc99f2022-01-18 13:05:21 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <signal.h>
19#include <stdlib.h>
20
21#include "profile-extras.h"
22
23extern "C" {
24
25static sighandler_t chained_signal_handler = SIG_ERR;
26
Pirama Arumuga Nainar1d088272022-01-27 10:32:09 -080027#ifndef __CONTINUOUS_COVERAGE_MODE__
Pirama Arumuga Nainar13bc99f2022-01-18 13:05:21 -080028int __llvm_profile_write_file(void);
Pirama Arumuga Nainar1d088272022-01-27 10:32:09 -080029#endif // __CONTINUOUS_COVERAGE_MODE__
Pirama Arumuga Nainar13bc99f2022-01-18 13:05:21 -080030
31static void llvm_signal_handler(__unused int signum) {
Pirama Arumuga Nainar1d088272022-01-27 10:32:09 -080032 // TODO(pirama) Only disable __llvm_profile_write_file call to begin with.
33 // After continuous mode is stable, stop registering the signal handler.
34#ifndef __CONTINUOUS_COVERAGE_MODE__
Pirama Arumuga Nainar13bc99f2022-01-18 13:05:21 -080035 __llvm_profile_write_file();
Pirama Arumuga Nainar1d088272022-01-27 10:32:09 -080036#endif // __CONTINUOUS_COVERAGE_MODE__
Pirama Arumuga Nainar13bc99f2022-01-18 13:05:21 -080037
38 if (chained_signal_handler != SIG_ERR && chained_signal_handler != SIG_IGN &&
39 chained_signal_handler != SIG_DFL) {
40 (chained_signal_handler)(signum);
41 }
42}
43
44// Initialize libprofile-extras:
45//
46// - Install a signal handler that triggers __llvm_profile_write_file on
47// <COVERAGE_FLUSH_SIGNAL>.
48//
49// We want this initializer to run during load time. In addition to marking
50// this function as a constructor, we link this library with `--whole-archive`
51// to force this function to be included in the output.
52static __attribute__((constructor)) int init_profile_extras(void) {
53 if (chained_signal_handler != SIG_ERR) {
54 return -1;
55 }
56 sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, llvm_signal_handler);
57 if (ret1 == SIG_ERR) {
58 return -1;
59 }
60 chained_signal_handler = ret1;
61
62 return 0;
63}
64}