Yell if NDC callers are holding bad locks.

The current MountService design heavily depends on down-callers not
holding any locks, since the vast majority of events are unsolicited
and bubble up to the framework.

This simple API gives us an easy way to track down people calling
while holding a lock they shouldn't be.

Bug: 25443096
Change-Id: Ifcbda95f00d5be8c1b88d58ca67927e76c713c3e
diff --git a/services/core/java/com/android/server/NativeDaemonConnector.java b/services/core/java/com/android/server/NativeDaemonConnector.java
index 519a2a3..3a6fa05 100644
--- a/services/core/java/com/android/server/NativeDaemonConnector.java
+++ b/services/core/java/com/android/server/NativeDaemonConnector.java
@@ -25,6 +25,7 @@
 import android.os.PowerManager;
 import android.os.SystemClock;
 import android.util.LocalLog;
+import android.util.Log;
 import android.util.Slog;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -57,6 +58,7 @@
     private LocalLog mLocalLog;
 
     private volatile boolean mDebug = false;
+    private volatile Object mLockWarning;
 
     private final ResponseQueue mResponseQueue;
 
@@ -107,6 +109,14 @@
         mDebug = debug;
     }
 
+    /**
+     * Yell loudly if someone tries making future {@link #execute(Command)} calls while holding a
+     * lock on the given object.
+     */
+    public void setLockWarning(Object lockWarning) {
+        mLockWarning = lockWarning;
+    }
+
     @Override
     public void run() {
         mCallbackHandler = new Handler(mLooper, this);
@@ -394,6 +404,10 @@
      */
     public NativeDaemonEvent[] executeForList(long timeoutMs, String cmd, Object... args)
             throws NativeDaemonConnectorException {
+        if (mLockWarning != null && Thread.holdsLock(mLockWarning)) {
+            Log.wtf(TAG, "Calling thread is holding lock " + mLockWarning, new Throwable());
+        }
+
         final long startTime = SystemClock.elapsedRealtime();
 
         final ArrayList<NativeDaemonEvent> events = Lists.newArrayList();