2014-06-25 09:12:07 +04:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2013-11-15 19:46:44 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crash Monitor
|
|
|
|
*
|
|
|
|
* Monitors execution of a program to detect possible crashes. After
|
|
|
|
* program termination, the monitor can be queried during the next run
|
|
|
|
* to determine whether the last run exited cleanly or not.
|
|
|
|
*
|
|
|
|
* The monitoring is done by registering and listening for special
|
|
|
|
* notifications, or checkpoints, known to be sent by the monitored
|
|
|
|
* program as different stages in the execution are reached. As they
|
|
|
|
* are observed, these notifications are written asynchronously to a
|
|
|
|
* checkpoint file.
|
|
|
|
*
|
|
|
|
* During next program startup the crash monitor reads the checkpoint
|
|
|
|
* file from the last session. If notifications are missing, a crash
|
|
|
|
* has likely happened. By inspecting the notifications present, it is
|
|
|
|
* possible to determine what stages were reached in the program
|
|
|
|
* before the crash.
|
|
|
|
*
|
|
|
|
* Note that since the file is written asynchronously it is possible
|
|
|
|
* that a received notification is lost if the program crashes right
|
|
|
|
* after a checkpoint, but before crash monitor has been able to write
|
|
|
|
* it to disk. Thus, while the presence of a notification in the
|
|
|
|
* checkpoint file tells us that the corresponding stage was reached
|
|
|
|
* during the last run, the absence of a notification after a crash
|
|
|
|
* does not necessarily tell us that the checkpoint wasn't reached.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["CrashMonitor"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
|
|
|
|
const { PromiseUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/PromiseUtils.jsm"
|
|
|
|
);
|
2013-11-15 19:46:44 +04:00
|
|
|
|
|
|
|
const NOTIFICATIONS = [
|
|
|
|
"final-ui-startup",
|
|
|
|
"sessionstore-windows-restored",
|
|
|
|
"quit-application-granted",
|
|
|
|
"quit-application",
|
|
|
|
"profile-change-net-teardown",
|
|
|
|
"profile-change-teardown",
|
|
|
|
"profile-before-change",
|
|
|
|
"sessionstore-final-state-write-complete",
|
|
|
|
];
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var CrashMonitorInternal = {
|
2013-11-15 19:46:44 +04:00
|
|
|
/**
|
|
|
|
* Notifications received during the current session.
|
|
|
|
*
|
|
|
|
* Object where a property with a value of |true| means that the
|
|
|
|
* notification of the same name has been received at least once by
|
|
|
|
* the CrashMonitor during this session. Notifications that have not
|
|
|
|
* yet been received are not present as properties. |NOTIFICATIONS|
|
|
|
|
* lists the notifications tracked by the CrashMonitor.
|
|
|
|
*/
|
|
|
|
checkpoints: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifications received during previous session.
|
|
|
|
*
|
|
|
|
* Available after |loadPreviousCheckpoints|. Promise which resolves
|
|
|
|
* to an object containing a set of properties, where a property
|
|
|
|
* with a value of |true| means that the notification with the same
|
|
|
|
* name as the property name was received at least once last
|
|
|
|
* session.
|
|
|
|
*/
|
|
|
|
previousCheckpoints: null,
|
|
|
|
|
|
|
|
/* Deferred for AsyncShutdown blocker */
|
2017-06-23 12:25:02 +03:00
|
|
|
profileBeforeChangeDeferred: PromiseUtils.defer(),
|
2013-11-15 19:46:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to checkpoint file.
|
|
|
|
*
|
|
|
|
* Each time a new notification is received, this file is written to
|
2015-04-23 22:10:30 +03:00
|
|
|
* disc to reflect the information in |checkpoints|.
|
2013-11-15 19:46:44 +04:00
|
|
|
*/
|
2015-04-23 22:10:30 +03:00
|
|
|
path: OS.Path.join(OS.Constants.Path.profileDir, "sessionCheckpoints.json"),
|
2013-11-15 19:46:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load checkpoints from previous session asynchronously.
|
|
|
|
*
|
|
|
|
* @return {Promise} A promise that resolves/rejects once loading is complete
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
loadPreviousCheckpoints() {
|
2014-03-25 16:58:00 +04:00
|
|
|
this.previousCheckpoints = (async function() {
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await OS.File.read(CrashMonitorInternal.path, {
|
|
|
|
encoding: "utf-8",
|
|
|
|
});
|
2015-12-03 21:01:18 +03:00
|
|
|
} catch (ex) {
|
|
|
|
if (!(ex instanceof OS.File.Error)) {
|
|
|
|
throw ex;
|
|
|
|
}
|
2014-03-25 16:58:00 +04:00
|
|
|
if (!ex.becauseNoSuchFile) {
|
|
|
|
Cu.reportError(
|
|
|
|
"Error while loading crash monitor data: " + ex.toString()
|
|
|
|
);
|
2014-02-03 18:14:16 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 16:58:00 +04:00
|
|
|
return null;
|
2014-02-03 18:14:16 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 16:58:00 +04:00
|
|
|
let notifications;
|
2014-02-03 18:14:16 +04:00
|
|
|
try {
|
|
|
|
notifications = JSON.parse(data);
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("Error while parsing crash monitor data: " + ex);
|
2014-03-25 16:58:00 +04:00
|
|
|
return null;
|
2014-02-03 18:14:16 +04:00
|
|
|
}
|
|
|
|
|
2014-10-03 10:42:00 +04:00
|
|
|
// If `notifications` isn't an object, then the monitor data isn't valid.
|
|
|
|
if (Object(notifications) !== notifications) {
|
|
|
|
Cu.reportError(
|
|
|
|
"Error while parsing crash monitor data: invalid monitor data"
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-03-25 16:58:00 +04:00
|
|
|
return Object.freeze(notifications);
|
2013-11-15 19:46:44 +04:00
|
|
|
})();
|
|
|
|
|
2014-03-25 16:58:00 +04:00
|
|
|
return this.previousCheckpoints;
|
2013-11-15 19:46:44 +04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
var CrashMonitor = {
|
|
|
|
/**
|
|
|
|
* Notifications received during previous session.
|
|
|
|
*
|
|
|
|
* Return object containing the set of notifications received last
|
|
|
|
* session as keys with values set to |true|.
|
|
|
|
*
|
|
|
|
* @return {Promise} A promise resolving to previous checkpoints
|
|
|
|
*/
|
|
|
|
get previousCheckpoints() {
|
|
|
|
if (!CrashMonitorInternal.initialized) {
|
|
|
|
throw new Error(
|
|
|
|
"CrashMonitor must be initialized before getting previous checkpoints"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-15 21:50:30 +03:00
|
|
|
return CrashMonitorInternal.previousCheckpoints;
|
2013-11-15 19:46:44 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize CrashMonitor.
|
|
|
|
*
|
|
|
|
* Should only be called from the CrashMonitor XPCOM component.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
init() {
|
2013-11-15 19:46:44 +04:00
|
|
|
if (CrashMonitorInternal.initialized) {
|
|
|
|
throw new Error("CrashMonitor.init() must only be called once!");
|
|
|
|
}
|
|
|
|
|
|
|
|
let promise = CrashMonitorInternal.loadPreviousCheckpoints();
|
|
|
|
// Add "profile-after-change" to checkpoint as this method is
|
|
|
|
// called after receiving it
|
|
|
|
CrashMonitorInternal.checkpoints["profile-after-change"] = true;
|
|
|
|
|
2016-11-12 02:22:34 +03:00
|
|
|
NOTIFICATIONS.forEach(function(aTopic) {
|
2017-04-15 00:39:22 +03:00
|
|
|
Services.obs.addObserver(this, aTopic);
|
2013-11-15 19:46:44 +04:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Add shutdown blocker for profile-before-change
|
2014-06-17 23:11:00 +04:00
|
|
|
OS.File.profileBeforeChange.addBlocker(
|
2013-11-15 19:46:44 +04:00
|
|
|
"CrashMonitor: Writing notifications to file after receiving profile-before-change",
|
2014-06-17 23:11:00 +04:00
|
|
|
CrashMonitorInternal.profileBeforeChangeDeferred.promise,
|
|
|
|
() => this.checkpoints
|
2013-11-15 19:46:44 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
CrashMonitorInternal.initialized = true;
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle registered notifications.
|
|
|
|
*
|
|
|
|
* Update checkpoint file for every new notification received.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
observe(aSubject, aTopic, aData) {
|
2013-11-15 19:46:44 +04:00
|
|
|
if (!(aTopic in CrashMonitorInternal.checkpoints)) {
|
|
|
|
// If this is the first time this notification is received,
|
|
|
|
// remember it and write it to file
|
|
|
|
CrashMonitorInternal.checkpoints[aTopic] = true;
|
2015-12-03 21:01:18 +03:00
|
|
|
(async function() {
|
2013-11-15 19:46:44 +04:00
|
|
|
try {
|
|
|
|
let data = JSON.stringify(CrashMonitorInternal.checkpoints);
|
|
|
|
|
|
|
|
/* Write to the checkpoint file asynchronously, off the main
|
|
|
|
* thread, for performance reasons. Note that this means
|
|
|
|
* that there's not a 100% guarantee that the file will be
|
|
|
|
* written by the time the notification completes. The
|
|
|
|
* exception is profile-before-change which has a shutdown
|
|
|
|
* blocker. */
|
|
|
|
await OS.File.writeAtomic(CrashMonitorInternal.path, data, {
|
|
|
|
tmpPath: CrashMonitorInternal.path + ".tmp",
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
// Resolve promise for blocker
|
|
|
|
if (aTopic == "profile-before-change") {
|
|
|
|
CrashMonitorInternal.profileBeforeChangeDeferred.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NOTIFICATIONS.every(elem => elem in CrashMonitorInternal.checkpoints)) {
|
|
|
|
// All notifications received, unregister observers
|
2016-11-12 02:22:34 +03:00
|
|
|
NOTIFICATIONS.forEach(function(aTopic) {
|
2013-11-15 19:46:44 +04:00
|
|
|
Services.obs.removeObserver(this, aTopic);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2020-01-30 00:50:04 +03:00
|
|
|
Object.freeze(CrashMonitor);
|