auto import from //depot/cupcake/@136745
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 2ede783..9d720d1 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -11823,6 +11823,56 @@
         }
     }
 
+    public boolean profileControl(String process, boolean start,
+            String path) throws RemoteException {
+
+        synchronized (this) {
+            // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
+            // its own permission.
+            if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
+                    != PackageManager.PERMISSION_GRANTED) {
+                throw new SecurityException("Requires permission "
+                        + android.Manifest.permission.SET_ACTIVITY_WATCHER);
+            }
+            
+            ProcessRecord proc = null;
+            try {
+                int pid = Integer.parseInt(process);
+                synchronized (mPidsSelfLocked) {
+                    proc = mPidsSelfLocked.get(pid);
+                }
+            } catch (NumberFormatException e) {
+            }
+            
+            if (proc == null) {
+                HashMap<String, SparseArray<ProcessRecord>> all
+                        = mProcessNames.getMap();
+                SparseArray<ProcessRecord> procs = all.get(process);
+                if (procs != null && procs.size() > 0) {
+                    proc = procs.valueAt(0);
+                }
+            }
+            
+            if (proc == null || proc.thread == null) {
+                throw new IllegalArgumentException("Unknown process: " + process);
+            }
+            
+            boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0"));
+            if (isSecure) {
+                if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
+                    throw new SecurityException("Process not debuggable: " + proc);
+                }
+            }
+            
+            try {
+                proc.thread.profilerControl(start, path);
+                return true;
+            } catch (RemoteException e) {
+                throw new IllegalStateException("Process disappeared");
+            }
+        }
+    }
+    
     /** In this method we try to acquire our lock to make sure that we have not deadlocked */
     public void monitor() {
         synchronized (this) { }