ndk-gdb.py: work around permissions issue with minSdkVersion >= 24.

Applications with a minSdkVersion of 24 or greater have their data
directory permissions set to rwx------ (compared to rwxr-x--- for
previous versions). This prevents adbd from forwarding connections to
gdbserver. Resolve this by running `run-as <pkg> chmod a+x` on devices
running 24 or greater.

Bug: http://b/32292522
Bug: https://github.com/android-ndk/ndk/issues/224
Change-Id: I697a040e8b069dfda9169441c100520ba5e6c686
Test: chmod 700 /data/data/<pkgname>, ndk-gdb.py starts working
diff --git a/ndk-gdb.py b/ndk-gdb.py
index 00b4461..1ae478d 100755
--- a/ndk-gdb.py
+++ b/ndk-gdb.py
@@ -374,6 +374,18 @@
         error("Could not find application's data directory. Are you sure that "
               "the application is installed and debuggable?")
     data_dir = stdout.strip()
+
+    # Applications with minSdkVersion >= 24 will have their data directories
+    # created with rwx------ permissions, preventing adbd from forwarding to
+    # the gdbserver socket. To be safe, if we're on a device >= 24, always
+    # chmod the directory.
+    if get_api_level(args.props) >= 24:
+        chmod_cmd = ["/system/bin/chmod", "a+x", data_dir]
+        chmod_cmd = gdbrunner.get_run_as_cmd(package_name, chmod_cmd)
+        (rc, _, _) = args.device.shell_nocheck(chmod_cmd)
+        if rc != 0:
+            error("Failed to make application data directory world executable")
+
     log("Found application data directory: {}".format(data_dir))
     return data_dir