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 int __llvm_profile_write_file(void); 28 llvm_signal_handler(__unused int signum)29static void llvm_signal_handler(__unused int signum) { 30 __llvm_profile_write_file(); 31 32 if (chained_signal_handler != SIG_ERR && 33 chained_signal_handler != SIG_IGN && 34 chained_signal_handler != SIG_DFL) { 35 (chained_signal_handler)(signum); 36 } 37 } 38 39 // Initialize libprofile-extras: 40 // 41 // - Install a signal handler that triggers __llvm_profile_write_file on 42 // <COVERAGE_FLUSH_SIGNAL>. 43 // 44 // We want this initializer to run during load time. In addition to marking 45 // this function as a constructor, we link this library with `--whole-archive` 46 // to force this function to be included in the output. init_profile_extras(void)47static __attribute__((constructor)) int init_profile_extras(void) { 48 if (chained_signal_handler != SIG_ERR) { 49 return -1; 50 } 51 sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, llvm_signal_handler); 52 if (ret1 == SIG_ERR) { 53 return -1; 54 } 55 chained_signal_handler = ret1; 56 57 return 0; 58 } 59 } 60