From 23bdf4894bd2ba38546c6dd5198b7d5e0b1de78e Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Mon, 3 Jul 2017 16:58:54 +0100 Subject: [PATCH] ActivityManagerService: Improve file naming for ANR traces. Use the date and time of the dump in a similar format as bug-reports. Test: Manual Bug: 63258211 Bug: 32064548 Change-Id: I1d214e9c6c2e404733122c7ab0c61af17a43cfa1 --- .../server/am/ActivityManagerService.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 151a5105af6ac..606dc983148a6 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -414,6 +414,7 @@ import com.android.server.vr.VrManagerInternal; import com.android.server.wm.PinnedStackWindowController; import com.android.server.wm.WindowManagerService; +import java.text.SimpleDateFormat; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; @@ -5507,11 +5508,8 @@ public class ActivityManagerService extends IActivityManager.Stub // NOTE: We should consider creating the file in native code atomically once we've // gotten rid of the old scheme of dumping and lot of the code that deals with paths // can be removed. - try { - tracesFile = File.createTempFile("anr_", "", tracesDir); - FileUtils.setPermissions(tracesFile.getAbsolutePath(), 0600, -1, -1); // -rw------- - } catch (IOException ioe) { - Slog.w(TAG, "Unable to create ANR traces file: ", ioe); + tracesFile = createAnrDumpFile(tracesDir); + if (tracesFile == null) { return null; } @@ -5523,6 +5521,31 @@ public class ActivityManagerService extends IActivityManager.Stub return tracesFile; } + @GuardedBy("ActivityManagerService.class") + private static SimpleDateFormat sAnrFileDateFormat; + + private static synchronized File createAnrDumpFile(File tracesDir) { + if (sAnrFileDateFormat == null) { + sAnrFileDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS"); + } + + final String formattedDate = sAnrFileDateFormat.format(new Date()); + final File anrFile = new File(tracesDir, "anr_" + formattedDate); + + try { + if (anrFile.createNewFile()) { + FileUtils.setPermissions(anrFile.getAbsolutePath(), 0600, -1, -1); // -rw------- + return anrFile; + } else { + Slog.w(TAG, "Unable to create ANR dump file: createNewFile failed"); + } + } catch (IOException ioe) { + Slog.w(TAG, "Exception creating ANR dump file:", ioe); + } + + return null; + } + /** * Prune all trace files that are more than a day old. *