1 /* 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 23 extern "C" { 24 25 static sighandler_t chained_signal_handler = SIG_ERR; 26 27 #ifndef __CONTINUOUS_COVERAGE_MODE__ 28 int __llvm_profile_write_file(void); 29 #endif // __CONTINUOUS_COVERAGE_MODE__ 30 llvm_signal_handler(__unused int signum)31static void llvm_signal_handler(__unused int signum) { 32 // 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__ 35 __llvm_profile_write_file(); 36 #endif // __CONTINUOUS_COVERAGE_MODE__ 37 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. init_profile_extras(void)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 } 65