Merge "Fix trace naming due to an overwrite permission issue with root permissions"
diff --git a/ui/src/controller/adb_record_controller.ts b/ui/src/controller/adb_record_controller.ts
index f52714d..a1643b6 100644
--- a/ui/src/controller/adb_record_controller.ts
+++ b/ui/src/controller/adb_record_controller.ts
@@ -29,11 +29,10 @@
   RECORDING,
   FETCHING
 }
-const DEFAULT_DESTINATION_FILE = '/data/misc/perfetto-traces/trace';
+const DEFAULT_DESTINATION_FILE = '/data/misc/perfetto-traces/trace-by-ui';
 const textDecoder = new _TextDecoder();
 
 export class AdbConsumerPort extends RpcConsumerPort {
-  // public for testing
   traceDestFile = DEFAULT_DESTINATION_FILE;
   private state = AdbState.READY;
   private adb: Adb;
@@ -171,7 +170,12 @@
   }
 
   generateReadTraceCommand(): string {
-    return `gzip -c ${this.traceDestFile}`;
+    // We attempt to delete the trace file after tracing. On a non-root shell,
+    // this will fail (due to selinux denial), but perfetto cmd will be able to
+    // override the file later. However, on a root shell, we need to clean up
+    // the file since perfetto cmd might otherwise fail to override it in a
+    // future session.
+    return `gzip -c ${this.traceDestFile} && rm -f ${this.traceDestFile}`;
   }
 
   generateStartTracingCommand(tracingConfig: Uint8Array) {
diff --git a/ui/src/controller/record_controller.ts b/ui/src/controller/record_controller.ts
index b20ce92..d14d695 100644
--- a/ui/src/controller/record_controller.ts
+++ b/ui/src/controller/record_controller.ts
@@ -571,6 +571,7 @@
   }
 
   onError(message: string) {
+    console.error('Error in record controller: ', message);
     globals.dispatch(
         Actions.setLastRecordingError({error: message.substr(0, 150)}));
     globals.dispatch(Actions.stopRecording({}));