Skip to content

Commit 2f5dc1b

Browse files
pirama-arumuga-nainarGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "Revert "Stop writing coverage data in coverage-flush signal handler""
2 parents b668720 + 13bc99f commit 2f5dc1b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

toolchain-extras/Android.bp

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ cc_library_static {
4646
cc_defaults {
4747
name: "libprofile-clang-defaults",
4848
srcs: [
49+
"profile-clang-extras.cpp",
4950
"profile-clang-openat.cpp",
5051
],
5152
native_coverage: false,

toolchain-extras/profile-clang-extras-test.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@
2020

2121
#include "profile-extras.h"
2222

23+
static int flush_count = 0;
24+
25+
extern "C" {
26+
int __llvm_profile_write_file() {
27+
flush_count++;
28+
return 0;
29+
}
30+
}
31+
32+
TEST(profile_extras, smoke) {
33+
flush_count = 0;
34+
35+
ASSERT_EQ(0, flush_count);
36+
kill(getpid(), COVERAGE_FLUSH_SIGNAL);
37+
sleep(2);
38+
ASSERT_EQ(1, flush_count);
39+
}
40+
2341
static const char* OPEN_AT_TEST_FNAME = "/data/misc/trace/test.profraw";
2442
TEST(profile_extras, openat) {
2543
mode_t old_umask = umask(0077);
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
29+
static void llvm_signal_handler(__unused int signum) {
30+
__llvm_profile_write_file();
31+
32+
if (chained_signal_handler != SIG_ERR && chained_signal_handler != SIG_IGN &&
33+
chained_signal_handler != SIG_DFL) {
34+
(chained_signal_handler)(signum);
35+
}
36+
}
37+
38+
// Initialize libprofile-extras:
39+
//
40+
// - Install a signal handler that triggers __llvm_profile_write_file on
41+
// <COVERAGE_FLUSH_SIGNAL>.
42+
//
43+
// We want this initializer to run during load time. In addition to marking
44+
// this function as a constructor, we link this library with `--whole-archive`
45+
// to force this function to be included in the output.
46+
static __attribute__((constructor)) int init_profile_extras(void) {
47+
if (chained_signal_handler != SIG_ERR) {
48+
return -1;
49+
}
50+
sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, llvm_signal_handler);
51+
if (ret1 == SIG_ERR) {
52+
return -1;
53+
}
54+
chained_signal_handler = ret1;
55+
56+
return 0;
57+
}
58+
}

0 commit comments

Comments
 (0)