1 /* 2 * Copyright (C) 2019 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 <pthread.h> 19 #include <signal.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include <libgen.h> // For POSIX basename(). 23 24 // Use _system_properties.h to use __system_property_wait_any() 25 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ 26 #include <sys/_system_properties.h> 27 28 #include "profile-extras.h" 29 30 extern "C" { 31 32 void __gcov_flush(void); 33 34 // storing SIG_ERR helps us detect (unlikely) looping. 35 static sighandler_t chained_gcov_signal_handler = SIG_ERR; 36 gcov_signal_handler(int signum)37static void gcov_signal_handler(int signum) { 38 __gcov_flush(); 39 if (chained_gcov_signal_handler != SIG_ERR && 40 chained_gcov_signal_handler != SIG_IGN && 41 chained_gcov_signal_handler != SIG_DFL) { 42 (chained_gcov_signal_handler)(signum); 43 } 44 } 45 46 __attribute__((weak)) int init_profile_extras_once = 0; 47 48 // Initialize libprofile-extras: 49 // - Install a signal handler that triggers __gcov_flush on <COVERAGE_FLUSH_SIGNAL>. 50 // 51 // We want this initiazlier to run during load time. 52 // 53 // Just marking init_profile_extras() with __attribute__((constructor)) isn't 54 // enough since the linker drops it from its output since no other symbol from 55 // this static library is referenced. 56 // 57 // We force the linker to include init_profile_extras() by passing 58 // '-uinit_profile_extras' to the linker (in build/soong). init_profile_extras(void)59__attribute__((constructor)) int init_profile_extras(void) { 60 if (init_profile_extras_once) 61 return 0; 62 init_profile_extras_once = 1; 63 64 // is this instance already registered? 65 if (chained_gcov_signal_handler != SIG_ERR) { 66 return -1; 67 } 68 sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, gcov_signal_handler); 69 if (ret1 == SIG_ERR) { 70 return -1; 71 } 72 chained_gcov_signal_handler = ret1; 73 74 return 0; 75 } 76 } 77