Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/crash/CrashFileManager.java

Issue 2307713002: [Android] Provide an API for manually triggering a crash report upload. (Closed)
Patch Set: Use a separate suffix for forced uploads Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.crash; 5 package org.chromium.chrome.browser.crash;
6 6
7 import org.chromium.base.Log; 7 import org.chromium.base.Log;
8 import org.chromium.base.VisibleForTesting; 8 import org.chromium.base.VisibleForTesting;
9 9
10 import java.io.File; 10 import java.io.File;
(...skipping 27 matching lines...) Expand all
38 Pattern.compile("\\.dmp([0-9]*)$\\z"); 38 Pattern.compile("\\.dmp([0-9]*)$\\z");
39 39
40 private static final Pattern MINIDUMP_MIME_FIRST_TRY_PATTERN = 40 private static final Pattern MINIDUMP_MIME_FIRST_TRY_PATTERN =
41 Pattern.compile("\\.dmp([0-9]+)$\\z"); 41 Pattern.compile("\\.dmp([0-9]+)$\\z");
42 42
43 private static final Pattern MINIDUMP_PATTERN = 43 private static final Pattern MINIDUMP_PATTERN =
44 Pattern.compile("\\.dmp([0-9]*)(\\.try[0-9])?\\z"); 44 Pattern.compile("\\.dmp([0-9]*)(\\.try[0-9])?\\z");
45 45
46 private static final Pattern UPLOADED_MINIDUMP_PATTERN = Pattern.compile("\\ .up([0-9]*)\\z"); 46 private static final Pattern UPLOADED_MINIDUMP_PATTERN = Pattern.compile("\\ .up([0-9]*)\\z");
47 47
48 private static final String NOT_YET_UPLOADED_MINIDUMP_SUFFIX = ".dmp";
49
48 private static final String UPLOADED_MINIDUMP_SUFFIX = ".up"; 50 private static final String UPLOADED_MINIDUMP_SUFFIX = ".up";
49 51
50 private static final String UPLOAD_SKIPPED_MINIDUMP_SUFFIX = ".skipped"; 52 private static final String UPLOAD_SKIPPED_MINIDUMP_SUFFIX = ".skipped";
51 53
54 private static final String UPLOAD_FORCED_MINIDUMP_SUFFIX = ".forced";
55
52 private static final String UPLOAD_ATTEMPT_DELIMITER = ".try"; 56 private static final String UPLOAD_ATTEMPT_DELIMITER = ".try";
53 57
54 @VisibleForTesting 58 @VisibleForTesting
55 protected static final String TMP_SUFFIX = ".tmp"; 59 protected static final String TMP_SUFFIX = ".tmp";
56 60
57 private static final Pattern TMP_PATTERN = Pattern.compile("\\.tmp\\z"); 61 private static final Pattern TMP_PATTERN = Pattern.compile("\\.tmp\\z");
58 62
59 // The maximum number of non-uploaded crashes that may be kept in the crash reports directory. 63 // The maximum number of non-uploaded crashes that may be kept in the crash reports directory.
60 // Chosen to attempt to balance between keeping a generous number of crashes , and not using up 64 // Chosen to attempt to balance between keeping a generous number of crashes , and not using up
61 // too much filesystem storage space for obsolete crash reports. 65 // too much filesystem storage space for obsolete crash reports.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 } 113 }
110 114
111 /** 115 /**
112 * @return The file name to rename to after an addition attempt to upload 116 * @return The file name to rename to after an addition attempt to upload
113 */ 117 */
114 @VisibleForTesting 118 @VisibleForTesting
115 public static String filenameWithIncrementedAttemptNumber(String filename) { 119 public static String filenameWithIncrementedAttemptNumber(String filename) {
116 int numTried = readAttemptNumber(filename); 120 int numTried = readAttemptNumber(filename);
117 if (numTried > 0) { 121 if (numTried > 0) {
118 int newCount = numTried + 1; 122 int newCount = numTried + 1;
119 return filename.replaceAll(UPLOAD_ATTEMPT_DELIMITER + numTried, 123 return filename.replace(
120 UPLOAD_ATTEMPT_DELIMITER + newCount); 124 UPLOAD_ATTEMPT_DELIMITER + numTried, UPLOAD_ATTEMPT_DELIMITE R + newCount);
121 } else { 125 } else {
122 return filename + UPLOAD_ATTEMPT_DELIMITER + "1"; 126 return filename + UPLOAD_ATTEMPT_DELIMITER + "1";
123 } 127 }
124 } 128 }
125 129
130 /**
131 * Attempts to rename the given file to clear any state from previous upload attempts,
gayane -on leave until 09-2017 2016/09/06 17:00:00 The comment needs an update
Ilya Sherman 2016/09/07 00:54:01 Done.
132 * essentially treating as a fresh minidump file. This is useful for manuall y initiating
133 * previously skipped uploads.
134 *
135 * @return The renamed file, or null if renaming failed.
136 */
137 public static File trySetForcedUpload(File fileToUpload) {
138 if (fileToUpload.getName().contains(UPLOADED_MINIDUMP_SUFFIX)) {
139 Log.w(TAG, "Refusing to reset upload attempt state for a file that h as already been "
140 + "successfully uploaded: " + fileToUpload.getName() );
141 return null;
142 }
143 File renamedFile = new File(filenameWithForcedUploadState(fileToUpload.g etPath()));
144 return fileToUpload.renameTo(renamedFile) ? renamedFile : null;
145 }
146
147 /**
148 * @return True iff the provided File was manually forced (by the user) to b e uploaded.
149 */
150 public static boolean isForcedUpload(File fileToUpload) {
151 return fileToUpload.getName().contains(UPLOAD_FORCED_MINIDUMP_SUFFIX);
152 }
153
154 /**
155 * @return The filename to rename to so as to clear any upload attempt histo ry.
156 */
157 @VisibleForTesting
158 protected static String filenameWithForcedUploadState(String filename) {
159 int numTried = readAttemptNumber(filename);
160 if (numTried > 0) {
161 filename = filename.replace(
162 UPLOAD_ATTEMPT_DELIMITER + numTried, UPLOAD_ATTEMPT_DELIMITE R + 0);
163 }
164 filename = filename.replace(UPLOAD_SKIPPED_MINIDUMP_SUFFIX, UPLOAD_FORCE D_MINIDUMP_SUFFIX);
165 return filename.replace(NOT_YET_UPLOADED_MINIDUMP_SUFFIX, UPLOAD_FORCED_ MINIDUMP_SUFFIX);
166 }
167
126 @VisibleForTesting 168 @VisibleForTesting
127 public static int readAttemptNumber(String filename) { 169 public static int readAttemptNumber(String filename) {
128 int tryIndex = filename.lastIndexOf(UPLOAD_ATTEMPT_DELIMITER); 170 int tryIndex = filename.lastIndexOf(UPLOAD_ATTEMPT_DELIMITER);
129 if (tryIndex >= 0) { 171 if (tryIndex >= 0) {
130 tryIndex += UPLOAD_ATTEMPT_DELIMITER.length(); 172 tryIndex += UPLOAD_ATTEMPT_DELIMITER.length();
131 // To avoid out of bound exceptions 173 // To avoid out of bound exceptions
132 if (tryIndex < filename.length()) { 174 if (tryIndex < filename.length()) {
133 // We don't try more than 3 times. 175 // We don't try more than 3 times.
134 String numTriesString = filename.substring( 176 String numTriesString = filename.substring(
135 tryIndex, tryIndex + 1); 177 tryIndex, tryIndex + 1);
(...skipping 28 matching lines...) Expand all
164 public static void markUploadSkipped(File crashDumpFile) { 206 public static void markUploadSkipped(File crashDumpFile) {
165 CrashFileManager.renameCrashDumpFollowingUpload( 207 CrashFileManager.renameCrashDumpFollowingUpload(
166 crashDumpFile, UPLOAD_SKIPPED_MINIDUMP_SUFFIX); 208 crashDumpFile, UPLOAD_SKIPPED_MINIDUMP_SUFFIX);
167 } 209 }
168 210
169 /** 211 /**
170 * Renames a crash dump file. However, if renaming fails, attempts to delete the file 212 * Renames a crash dump file. However, if renaming fails, attempts to delete the file
171 * immediately. 213 * immediately.
172 */ 214 */
173 private static void renameCrashDumpFollowingUpload(File crashDumpFile, Strin g suffix) { 215 private static void renameCrashDumpFollowingUpload(File crashDumpFile, Strin g suffix) {
174 boolean renamed = crashDumpFile.renameTo( 216 // The pre-upload filename might have been either "foo.dmpN.tryM" or "fo o.forcedN.tryM".
175 new File(crashDumpFile.getPath().replaceAll("\\.dmp", suffix))); 217 String newName = crashDumpFile.getPath()
218 .replace(NOT_YET_UPLOADED_MINIDUMP_SUFFIX, suff ix)
219 .replace(UPLOAD_FORCED_MINIDUMP_SUFFIX, suffix) ;
220 boolean renamed = crashDumpFile.renameTo(new File(newName));
176 if (!renamed) { 221 if (!renamed) {
177 Log.w(TAG, "Failed to rename " + crashDumpFile); 222 Log.w(TAG, "Failed to rename " + crashDumpFile);
178 if (!crashDumpFile.delete()) { 223 if (!crashDumpFile.delete()) {
179 Log.w(TAG, "Failed to delete " + crashDumpFile); 224 Log.w(TAG, "Failed to delete " + crashDumpFile);
180 } 225 }
181 } 226 }
182 } 227 }
183 228
184 private final File mCacheDir; 229 private final File mCacheDir;
185 230
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 + f.getAbsolutePath()); 350 + f.getAbsolutePath());
306 } 351 }
307 } 352 }
308 return f; 353 return f;
309 } 354 }
310 355
311 File getCrashFile(String filename) { 356 File getCrashFile(String filename) {
312 return new File(getCrashDirectory(), filename); 357 return new File(getCrashDirectory(), filename);
313 } 358 }
314 359
360 /**
361 * Returns the minidump file with the given local ID, or null if no previous ly minidump file has
362 * the given ID.
363 * NOTE: Crash files that have already been successfully uploaded are not in cluded.
364 *
365 * @param localId The local ID of the crash report.
366 * @return The matching File, or null if no matching file is found.
367 */
368 File getCrashFileWithLocalId(String localId) {
369 for (File f : getAllFilesSorted()) {
370 // Only match non-uploaded or previously skipped files.
371 if (!f.getName().contains(NOT_YET_UPLOADED_MINIDUMP_SUFFIX)
372 && !f.getName().contains(UPLOAD_SKIPPED_MINIDUMP_SUFFIX)
373 && !f.getName().contains(UPLOAD_FORCED_MINIDUMP_SUFFIX)) {
374 continue;
375 }
376
377 String filenameSansExtension = f.getName().split("\\.")[0];
378 if (filenameSansExtension.endsWith(localId)) {
379 return f;
380 }
381 }
382 return null;
383 }
384
315 File getCrashUploadLogFile() { 385 File getCrashUploadLogFile() {
316 return new File(getCrashDirectory(), CRASH_DUMP_LOGFILE); 386 return new File(getCrashDirectory(), CRASH_DUMP_LOGFILE);
317 } 387 }
318 388
319 private File[] getAllTempFiles() { 389 private File[] getAllTempFiles() {
320 return getMatchingFiles(TMP_PATTERN); 390 return getMatchingFiles(TMP_PATTERN);
321 } 391 }
322 } 392 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698