summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPirama Arumuga Nainar <pirama@google.com>2022-01-18 13:05:21 -0800
committerPirama Arumuga Nainar <pirama@google.com>2022-01-18 13:14:53 -0800
commit13bc99f54504900ddd87653990009f867cd4f21c (patch)
tree7a0fae05ffb7b89fda9bc591fcab53234acc6673
parent7c1ac352574621daf232d01d6f5960585f2829f9 (diff)
downloadextras-13bc99f54504900ddd87653990009f867cd4f21c.tar.gz
Revert "Stop writing coverage data in coverage-flush signal handler"
Bug: http://b/194128476 Bug: http://b/210012154 This reverts commit c99fe0b3c3a4cabc080f5dbd5bdf022b02b04133. Coverage metrics dropped for ~10 of the 40 modules. There are also regressions in mainline when running tests on older platform builds. Test: presubmit Change-Id: Ifc36ca87363890347cbb83e0e1e58d443e078018
-rw-r--r--toolchain-extras/Android.bp1
-rw-r--r--toolchain-extras/profile-clang-extras-test.cpp18
-rw-r--r--toolchain-extras/profile-clang-extras.cpp58
3 files changed, 77 insertions, 0 deletions
diff --git a/toolchain-extras/Android.bp b/toolchain-extras/Android.bp
index 61fcaebc..220f3e34 100644
--- a/toolchain-extras/Android.bp
+++ b/toolchain-extras/Android.bp
@@ -46,6 +46,7 @@ cc_library_static {
cc_defaults {
name: "libprofile-clang-defaults",
srcs: [
+ "profile-clang-extras.cpp",
"profile-clang-openat.cpp",
],
native_coverage: false,
diff --git a/toolchain-extras/profile-clang-extras-test.cpp b/toolchain-extras/profile-clang-extras-test.cpp
index dba450d0..0c746478 100644
--- a/toolchain-extras/profile-clang-extras-test.cpp
+++ b/toolchain-extras/profile-clang-extras-test.cpp
@@ -20,6 +20,24 @@
#include "profile-extras.h"
+static int flush_count = 0;
+
+extern "C" {
+int __llvm_profile_write_file() {
+ flush_count++;
+ return 0;
+}
+}
+
+TEST(profile_extras, smoke) {
+ flush_count = 0;
+
+ ASSERT_EQ(0, flush_count);
+ kill(getpid(), COVERAGE_FLUSH_SIGNAL);
+ sleep(2);
+ ASSERT_EQ(1, flush_count);
+}
+
static const char* OPEN_AT_TEST_FNAME = "/data/misc/trace/test.profraw";
TEST(profile_extras, openat) {
mode_t old_umask = umask(0077);
diff --git a/toolchain-extras/profile-clang-extras.cpp b/toolchain-extras/profile-clang-extras.cpp
new file mode 100644
index 00000000..c45f9b39
--- /dev/null
+++ b/toolchain-extras/profile-clang-extras.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#include "profile-extras.h"
+
+extern "C" {
+
+static sighandler_t chained_signal_handler = SIG_ERR;
+
+int __llvm_profile_write_file(void);
+
+static void llvm_signal_handler(__unused int signum) {
+ __llvm_profile_write_file();
+
+ if (chained_signal_handler != SIG_ERR && chained_signal_handler != SIG_IGN &&
+ chained_signal_handler != SIG_DFL) {
+ (chained_signal_handler)(signum);
+ }
+}
+
+// Initialize libprofile-extras:
+//
+// - Install a signal handler that triggers __llvm_profile_write_file on
+// <COVERAGE_FLUSH_SIGNAL>.
+//
+// We want this initializer to run during load time. In addition to marking
+// this function as a constructor, we link this library with `--whole-archive`
+// to force this function to be included in the output.
+static __attribute__((constructor)) int init_profile_extras(void) {
+ if (chained_signal_handler != SIG_ERR) {
+ return -1;
+ }
+ sighandler_t ret1 = signal(COVERAGE_FLUSH_SIGNAL, llvm_signal_handler);
+ if (ret1 == SIG_ERR) {
+ return -1;
+ }
+ chained_signal_handler = ret1;
+
+ return 0;
+}
+}