Skip KernelAddressSymbolization on non-debuggable builds

The previous attempt at fixing this using build-time detection
was wrong. CTS package can be built as part of a "user" lunch
combo, but can be run on a userdebug image (and viceversa).
This moves the logic to run-time detection.

Bug: 172272291
Change-Id: Ia46ca9c2f8b7d918bf576adad60c72a0d6d8a782
diff --git a/Android.bp b/Android.bp
index 87b5561..332bbc4 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1037,7 +1037,6 @@
     "test/cts/heapprofd_java_test_cts.cc",
     "test/cts/heapprofd_test_cts.cc",
     "test/cts/traced_perf_test_cts.cc",
-    "test/cts/utils.cc",
   ],
   static_libs: [
     "libgmock",
@@ -8074,6 +8073,7 @@
 filegroup {
   name: "perfetto_test_test_helper",
   srcs: [
+    "test/android_test_utils.cc",
     "test/fake_producer.cc",
     "test/test_helper.cc",
   ],
diff --git a/test/BUILD.gn b/test/BUILD.gn
index ca594f9..8a1b15a 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -122,6 +122,12 @@
       "test_helper.cc",
       "test_helper.h",
     ]
+    if (is_android) {
+      sources += [
+        "android_test_utils.cc",
+        "android_test_utils.h",
+      ]
+    }
     if (start_daemons_for_testing) {
       cflags = [ "-DPERFETTO_START_DAEMONS_FOR_TESTING" ]
     }
diff --git a/test/cts/utils.cc b/test/android_test_utils.cc
similarity index 94%
rename from test/cts/utils.cc
rename to test/android_test_utils.cc
index 1dd9b1e..8c6081a 100644
--- a/test/cts/utils.cc
+++ b/test/android_test_utils.cc
@@ -14,14 +14,13 @@
  * limitations under the License.
  */
 
-#include "test/cts/utils.h"
+#include "test/android_test_utils.h"
 
 #include <stdlib.h>
 #include <sys/system_properties.h>
 
 #include "perfetto/base/logging.h"
 #include "perfetto/ext/base/file_utils.h"
-#include "test/gtest_and_gmock.h"
 
 namespace perfetto {
 namespace {
@@ -110,7 +109,7 @@
                       uint32_t delay_ms) {
   std::string start_cmd = "am start " + app_name + "/." + activity_name;
   int status = system(start_cmd.c_str());
-  ASSERT_TRUE(status >= 0 && WEXITSTATUS(status) == 0) << "status: " << status;
+  PERFETTO_CHECK(status >= 0 && WEXITSTATUS(status) == 0);
   WaitForProcess(app_name, checkpoint_name, task_runner, delay_ms);
 }
 
@@ -119,7 +118,7 @@
              base::TestTaskRunner* task_runner) {
   std::string stop_cmd = "am force-stop " + app_name;
   int status = system(stop_cmd.c_str());
-  ASSERT_TRUE(status >= 0 && WEXITSTATUS(status) == 0) << "status: " << status;
+  PERFETTO_CHECK(status >= 0 && WEXITSTATUS(status) == 0);
 
   bool desired_run_state = false;
   auto checkpoint = task_runner->CreateCheckpoint(checkpoint_name);
diff --git a/test/cts/utils.h b/test/android_test_utils.h
similarity index 92%
rename from test/cts/utils.h
rename to test/android_test_utils.h
index 724c367..30ee947 100644
--- a/test/cts/utils.h
+++ b/test/android_test_utils.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef TEST_CTS_UTILS_H_
-#define TEST_CTS_UTILS_H_
+#ifndef TEST_ANDROID_TEST_UTILS_H_
+#define TEST_ANDROID_TEST_UTILS_H_
 
 #include <string>
 
@@ -50,4 +50,4 @@
 
 }  // namespace perfetto
 
-#endif  // TEST_CTS_UTILS_H_
+#endif  // TEST_ANDROID_TEST_UTILS_H_
diff --git a/test/cts/Android.bp b/test/cts/Android.bp
index 1692c56..e8dd71d 100644
--- a/test/cts/Android.bp
+++ b/test/cts/Android.bp
@@ -6,7 +6,6 @@
     "heapprofd_java_test_cts.cc",
     "heapprofd_test_cts.cc",
     "traced_perf_test_cts.cc",
-    "utils.cc",
     ":perfetto_protos_perfetto_config_cpp_gen",
   ],
   generated_headers: [
diff --git a/test/cts/BUILD.gn b/test/cts/BUILD.gn
index 84a7860..0404ceb 100644
--- a/test/cts/BUILD.gn
+++ b/test/cts/BUILD.gn
@@ -38,7 +38,6 @@
     "heapprofd_java_test_cts.cc",
     "heapprofd_test_cts.cc",
     "traced_perf_test_cts.cc",
-    "utils.cc",
   ]
 }
 
diff --git a/test/cts/end_to_end_integrationtest_cts.cc b/test/cts/end_to_end_integrationtest_cts.cc
index c1539c7..632db6d 100644
--- a/test/cts/end_to_end_integrationtest_cts.cc
+++ b/test/cts/end_to_end_integrationtest_cts.cc
@@ -20,7 +20,7 @@
 
 #include "perfetto/tracing/core/data_source_config.h"
 #include "src/base/test/test_task_runner.h"
-#include "test/cts/utils.h"
+#include "test/android_test_utils.h"
 #include "test/test_helper.h"
 
 #include "protos/perfetto/config/test_config.gen.h"
diff --git a/test/cts/heapprofd_java_test_cts.cc b/test/cts/heapprofd_java_test_cts.cc
index 0801abd..8888009 100644
--- a/test/cts/heapprofd_java_test_cts.cc
+++ b/test/cts/heapprofd_java_test_cts.cc
@@ -22,7 +22,7 @@
 #include "perfetto/base/logging.h"
 #include "perfetto/tracing/core/data_source_config.h"
 #include "src/base/test/test_task_runner.h"
-#include "test/cts/utils.h"
+#include "test/android_test_utils.h"
 #include "test/gtest_and_gmock.h"
 #include "test/test_helper.h"
 
diff --git a/test/cts/heapprofd_test_cts.cc b/test/cts/heapprofd_test_cts.cc
index da56274..fc414c2 100644
--- a/test/cts/heapprofd_test_cts.cc
+++ b/test/cts/heapprofd_test_cts.cc
@@ -24,7 +24,7 @@
 #include "perfetto/base/logging.h"
 #include "perfetto/tracing/core/data_source_config.h"
 #include "src/base/test/test_task_runner.h"
-#include "test/cts/utils.h"
+#include "test/android_test_utils.h"
 #include "test/gtest_and_gmock.h"
 #include "test/test_helper.h"
 
diff --git a/test/cts/traced_perf_test_cts.cc b/test/cts/traced_perf_test_cts.cc
index bbc0add..df6a6b3 100644
--- a/test/cts/traced_perf_test_cts.cc
+++ b/test/cts/traced_perf_test_cts.cc
@@ -21,7 +21,7 @@
 #include "perfetto/base/logging.h"
 #include "perfetto/tracing/core/data_source_config.h"
 #include "src/base/test/test_task_runner.h"
-#include "test/cts/utils.h"
+#include "test/android_test_utils.h"
 #include "test/gtest_and_gmock.h"
 #include "test/test_helper.h"
 
diff --git a/test/end_to_end_integrationtest.cc b/test/end_to_end_integrationtest.cc
index 6b99fb7..0f8ad9a 100644
--- a/test/end_to_end_integrationtest.cc
+++ b/test/end_to_end_integrationtest.cc
@@ -58,6 +58,10 @@
 #include "protos/perfetto/trace/trace_packet.pbzero.h"
 #include "protos/perfetto/trace/trigger.gen.h"
 
+#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
+#include "test/android_test_utils.h"
+#endif
+
 namespace perfetto {
 
 namespace {
@@ -374,19 +378,21 @@
 //    We cannot change the length of the production code in
 //    CanReadKernelSymbolAddresses() to deal with it.
 // 2. On user (i.e. non-userdebug) builds. As that doesn't work there by design.
-#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) && \
-    (defined(__i386__) ||                         \
-     !PERFETTO_BUILDFLAG(PERFETTO_ANDROID_USERDEBUG_BUILD))
+#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) && defined(__i386__)
 #define MAYBE_KernelAddressSymbolization DISABLED_KernelAddressSymbolization
 #else
 #define MAYBE_KernelAddressSymbolization KernelAddressSymbolization
 #endif
 TEST_F(PerfettoTest, MAYBE_KernelAddressSymbolization) {
   // On Android in-tree builds (TreeHugger): this test must always run to
-  // prevent selinux / property-related regressions.
+  // prevent selinux / property-related regressions. However it can run only on
+  // userdebug.
   // On standalone builds and Linux, this can be optionally skipped because
   // there it requires root to lower kptr_restrict.
-#if !PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
+#if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
+  if (!IsDebuggableBuild())
+    GTEST_SKIP();
+#else
   if (geteuid() != 0)
     GTEST_SKIP();
 #endif