Allow debugging only for apps forked from zygote

When starting the runtime from app_process, we only pass JDWP options
if starting zygote. It prevents from opening a JDWP connection in
non-zygote programs while Android apps (forked from zygote) remain
debuggable.

Bug: 23050463

(cherry picked from commit 7a09b8322cab26d6e3da1362d3c74964ae66b5d4)

Change-Id: Ib5b6d3bc4d45389993c3c54226df5a7b72479d19
diff --git a/cmds/app_process/app_main.cpp b/cmds/app_process/app_main.cpp
index 1bb28c3..91cec19 100644
--- a/cmds/app_process/app_main.cpp
+++ b/cmds/app_process/app_main.cpp
@@ -304,9 +304,9 @@
     }
 
     if (zygote) {
-        runtime.start("com.android.internal.os.ZygoteInit", args);
+        runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
     } else if (className) {
-        runtime.start("com.android.internal.os.RuntimeInit", args);
+        runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
     } else {
         fprintf(stderr, "Error: no class name or --zygote supplied.\n");
         app_usage();
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index 1f4105f..0c84279 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -519,7 +519,7 @@
  *
  * Returns 0 on success.
  */
-int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
+int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote)
 {
     int result = -1;
     JavaVMInitArgs initArgs;
@@ -724,9 +724,13 @@
         }
     }
 
-    /* enable debugging; set suspend=y to pause during VM init */
-    /* use android ADB transport */
-    addOption("-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y");
+    /*
+     * Enable debugging only for apps forked from zygote.
+     * Set suspend=y to pause during VM init and use android ADB transport.
+     */
+    if (zygote) {
+      addOption("-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y");
+    }
 
     parseRuntimeOption("dalvik.vm.lockprof.threshold",
                        lockProfThresholdBuf,
@@ -927,7 +931,7 @@
  * Passes the main function two arguments, the class name and the specified
  * options string.
  */
-void AndroidRuntime::start(const char* className, const Vector<String8>& options)
+void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
 {
     ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
             className != NULL ? className : "(unknown)");
@@ -963,7 +967,7 @@
     JniInvocation jni_invocation;
     jni_invocation.Init(NULL);
     JNIEnv* env;
-    if (startVm(&mJavaVM, &env) != 0) {
+    if (startVm(&mJavaVM, &env, zygote) != 0) {
         return;
     }
     onVmCreated(env);
diff --git a/include/android_runtime/AndroidRuntime.h b/include/android_runtime/AndroidRuntime.h
index fc33b7e..9a3b990 100644
--- a/include/android_runtime/AndroidRuntime.h
+++ b/include/android_runtime/AndroidRuntime.h
@@ -64,7 +64,7 @@
      */
     static jclass findClass(JNIEnv* env, const char* className);
 
-    void start(const char *classname, const Vector<String8>& options);
+    void start(const char *classname, const Vector<String8>& options, bool zygote);
 
     void exit(int code);
 
@@ -131,7 +131,7 @@
                                     const char* runtimeArg,
                                     const char* quotingArg);
     void parseExtraOpts(char* extraOptsBuf, const char* quotingArg);
-    int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
+    int startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote);
 
     Vector<JavaVMOption> mOptions;
     bool mExitWithoutCleanup;