Allow apps to be profileable by trusted system services.

Bug: 170284829
Change-Id: Ie5dc3669bc8e7ce545484b1ec55e29fe137cf7e4
diff --git a/runtime/native/dalvik_system_ZygoteHooks.cc b/runtime/native/dalvik_system_ZygoteHooks.cc
index bc19503..9f75a88 100644
--- a/runtime/native/dalvik_system_ZygoteHooks.cc
+++ b/runtime/native/dalvik_system_ZygoteHooks.cc
@@ -153,6 +153,7 @@
   USE_APP_IMAGE_STARTUP_CACHE         = 1 << 16,
   DEBUG_IGNORE_APP_SIGNAL_HANDLER     = 1 << 17,
   DISABLE_TEST_API_ENFORCEMENT_POLICY = 1 << 18,
+  PROFILEABLE                         = 1 << 24,
 
   // bits to shift (flags & HIDDEN_API_ENFORCEMENT_POLICY_MASK) by to get a value
   // corresponding to hiddenapi::EnforcementPolicy
@@ -245,6 +246,8 @@
 
   runtime->SetProfileableFromShell((runtime_flags & PROFILE_FROM_SHELL) != 0);
   runtime_flags &= ~PROFILE_FROM_SHELL;
+  runtime->SetProfileable((runtime_flags & PROFILEABLE) != 0);
+  runtime_flags &= ~PROFILEABLE;
 
   return runtime_flags;
 }
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index a4e5550..10999dc 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1093,7 +1093,7 @@
 
   ScopedObjectAccess soa(Thread::Current());
   if (IsPerfettoHprofEnabled() &&
-      (Dbg::IsJdwpAllowed() || IsProfileableFromShell() || IsJavaDebuggable() ||
+      (Dbg::IsJdwpAllowed() || IsProfileable() || IsProfileableFromShell() || IsJavaDebuggable() ||
        Runtime::Current()->IsSystemServer())) {
     std::string err;
     ScopedTrace tr("perfetto_hprof init.");
@@ -1103,7 +1103,7 @@
     }
   }
   if (IsPerfettoJavaHeapStackProfEnabled() &&
-      (Dbg::IsJdwpAllowed() || IsProfileableFromShell() || IsJavaDebuggable() ||
+      (Dbg::IsJdwpAllowed() || IsProfileable() || IsProfileableFromShell() || IsJavaDebuggable() ||
        Runtime::Current()->IsSystemServer())) {
     std::string err;
     ScopedTrace tr("perfetto_javaheapprof init.");
diff --git a/runtime/runtime.h b/runtime/runtime.h
index 43a8454..e8fde68 100644
--- a/runtime/runtime.h
+++ b/runtime/runtime.h
@@ -724,6 +724,14 @@
     return is_profileable_from_shell_;
   }
 
+  void SetProfileable(bool value) {
+    is_profileable_ = value;
+  }
+
+  bool IsProfileable() const {
+    return is_profileable_;
+  }
+
   void SetJavaDebuggable(bool value);
 
   // Deoptimize the boot image, called for Java debuggable apps.
@@ -1224,8 +1232,14 @@
   // Whether Java code needs to be debuggable.
   bool is_java_debuggable_;
 
+  // Whether or not this application can be profiled by the shell user,
+  // even when running on a device that is running in user mode.
   bool is_profileable_from_shell_ = false;
 
+  // Whether or not this application can be profiled by system services on a
+  // device running in user mode, but not necessarily by the shell user.
+  bool is_profileable_ = false;
+
   // The maximum number of failed boots we allow before pruning the dalvik cache
   // and trying again. This option is only inspected when we're running as a
   // zygote.