2014-08-22 12:32:00 +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/. */
|
|
|
|
/* jshint moz: true */
|
|
|
|
/* global Uint8Array, Components, dump */
|
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
"use strict";
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
|
2015-12-09 23:52:38 +03:00
|
|
|
Cu.importGlobalProperties(['FileReader']);
|
2015-01-26 21:16:00 +03:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Promise", "resource://gre/modules/Promise.jsm");
|
2015-03-11 08:35:00 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Screenshot", "resource://gre/modules/Screenshot.jsm");
|
2015-01-26 21:16:00 +03:00
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["LogCapture"];
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
const SYSTEM_PROPERTY_KEY_MAX = 32;
|
|
|
|
const SYSTEM_PROPERTY_VALUE_MAX = 92;
|
|
|
|
|
|
|
|
function debug(msg) {
|
2015-01-26 21:16:00 +03:00
|
|
|
dump("LogCapture.jsm: " + msg + "\n");
|
2014-11-12 08:31:00 +03:00
|
|
|
}
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var LogCapture = {
|
2014-11-12 08:31:00 +03:00
|
|
|
ensureLoaded: function() {
|
|
|
|
if (!this.ctypes) {
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
load: function() {
|
2014-08-22 12:32:00 +04:00
|
|
|
// load in everything on first use
|
2015-01-26 21:16:00 +03:00
|
|
|
Cu.import("resource://gre/modules/ctypes.jsm", this);
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
this.libc = this.ctypes.open(this.ctypes.libraryName("c"));
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
this.read = this.libc.declare("read",
|
2014-08-22 12:32:00 +04:00
|
|
|
this.ctypes.default_abi,
|
|
|
|
this.ctypes.int, // bytes read (out)
|
|
|
|
this.ctypes.int, // file descriptor (in)
|
|
|
|
this.ctypes.voidptr_t, // buffer to read into (in)
|
|
|
|
this.ctypes.size_t // size_t size of buffer (in)
|
|
|
|
);
|
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
this.open = this.libc.declare("open",
|
2014-08-22 12:32:00 +04:00
|
|
|
this.ctypes.default_abi,
|
|
|
|
this.ctypes.int, // file descriptor (returned)
|
|
|
|
this.ctypes.char.ptr, // path
|
|
|
|
this.ctypes.int // flags
|
|
|
|
);
|
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
this.close = this.libc.declare("close",
|
2014-08-22 12:32:00 +04:00
|
|
|
this.ctypes.default_abi,
|
|
|
|
this.ctypes.int, // error code (returned)
|
|
|
|
this.ctypes.int // file descriptor
|
|
|
|
);
|
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
this.getpid = this.libc.declare("getpid",
|
|
|
|
this.ctypes.default_abi,
|
|
|
|
this.ctypes.int // PID
|
|
|
|
);
|
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
this.property_find_nth =
|
|
|
|
this.libc.declare("__system_property_find_nth",
|
|
|
|
this.ctypes.default_abi,
|
|
|
|
this.ctypes.voidptr_t, // return value: nullable prop_info*
|
|
|
|
this.ctypes.unsigned_int); // n: the index of the property to return
|
|
|
|
|
|
|
|
this.property_read =
|
|
|
|
this.libc.declare("__system_property_read",
|
|
|
|
this.ctypes.default_abi,
|
|
|
|
this.ctypes.void_t, // return: none
|
|
|
|
this.ctypes.voidptr_t, // non-null prop_info*
|
|
|
|
this.ctypes.char.ptr, // key
|
|
|
|
this.ctypes.char.ptr); // value
|
|
|
|
|
|
|
|
this.key_buf = this.ctypes.char.array(SYSTEM_PROPERTY_KEY_MAX)();
|
|
|
|
this.value_buf = this.ctypes.char.array(SYSTEM_PROPERTY_VALUE_MAX)();
|
|
|
|
},
|
|
|
|
|
|
|
|
cleanup: function() {
|
|
|
|
this.libc.close();
|
|
|
|
|
|
|
|
this.read = null;
|
|
|
|
this.open = null;
|
|
|
|
this.close = null;
|
|
|
|
this.property_find_nth = null;
|
|
|
|
this.property_read = null;
|
|
|
|
this.key_buf = null;
|
|
|
|
this.value_buf = null;
|
|
|
|
|
|
|
|
this.libc = null;
|
|
|
|
this.ctypes = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* readLogFile
|
|
|
|
* Read in /dev/log/{{log}} in nonblocking mode, which will return -1 if
|
|
|
|
* reading would block the thread.
|
|
|
|
*
|
|
|
|
* @param log {String} The log from which to read. Must be present in /dev/log
|
|
|
|
* @return {Uint8Array} Raw log data
|
|
|
|
*/
|
|
|
|
readLogFile: function(logLocation) {
|
|
|
|
this.ensureLoaded();
|
|
|
|
|
|
|
|
const O_READONLY = 0;
|
|
|
|
const O_NONBLOCK = 1 << 11;
|
|
|
|
|
|
|
|
const BUF_SIZE = 2048;
|
|
|
|
|
|
|
|
let BufType = this.ctypes.ArrayType(this.ctypes.char);
|
|
|
|
let buf = new BufType(BUF_SIZE);
|
|
|
|
let logArray = [];
|
|
|
|
|
|
|
|
let logFd = this.open(logLocation, O_READONLY | O_NONBLOCK);
|
|
|
|
if (logFd === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
let readStart = Date.now();
|
|
|
|
let readCount = 0;
|
|
|
|
while (true) {
|
|
|
|
let count = this.read(logFd, buf, BUF_SIZE);
|
|
|
|
readCount += 1;
|
|
|
|
|
|
|
|
if (count <= 0) {
|
|
|
|
// log has return due to being nonblocking or running out of things
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(let i = 0; i < count; i++) {
|
|
|
|
logArray.push(buf[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
let logTypedArray = new Uint8Array(logArray);
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
this.close(logFd);
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
return logTypedArray;
|
|
|
|
},
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
/**
|
|
|
|
* Get all system properties as a dict with keys mapping to values
|
|
|
|
*/
|
|
|
|
readProperties: function() {
|
|
|
|
this.ensureLoaded();
|
|
|
|
let n = 0;
|
|
|
|
let propertyDict = {};
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
while(true) {
|
|
|
|
let prop_info = this.property_find_nth(n);
|
|
|
|
if(prop_info.isNull()) {
|
|
|
|
break;
|
|
|
|
}
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
// read the prop_info into the key and value buffers
|
|
|
|
this.property_read(prop_info, this.key_buf, this.value_buf);
|
|
|
|
let key = this.key_buf.readString();;
|
|
|
|
let value = this.value_buf.readString()
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
propertyDict[key] = value;
|
|
|
|
n++;
|
|
|
|
}
|
2014-08-22 12:32:00 +04:00
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
return propertyDict;
|
2015-01-26 21:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumping about:memory to a file in /data/local/tmp/, returning a Promise.
|
|
|
|
* Will be resolved with the dumped file name.
|
|
|
|
*/
|
|
|
|
readAboutMemory: function() {
|
|
|
|
this.ensureLoaded();
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
// Perform the dump
|
|
|
|
let dumper = Cc["@mozilla.org/memory-info-dumper;1"]
|
|
|
|
.getService(Ci.nsIMemoryInfoDumper);
|
|
|
|
|
|
|
|
let file = "/data/local/tmp/logshake-about_memory-" + this.getpid() + ".json.gz";
|
|
|
|
dumper.dumpMemoryReportsToNamedFile(file, function() {
|
|
|
|
deferred.resolve(file);
|
|
|
|
}, null, false);
|
|
|
|
|
2015-03-11 08:35:00 +03:00
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumping screenshot, returning a Promise. Will be resolved with the content
|
|
|
|
* as an ArrayBuffer.
|
|
|
|
*/
|
|
|
|
getScreenshot: function() {
|
|
|
|
let deferred = Promise.defer();
|
2015-08-04 14:47:00 +03:00
|
|
|
try {
|
|
|
|
this.ensureLoaded();
|
|
|
|
|
2015-12-09 23:52:15 +03:00
|
|
|
let fr = new FileReader();
|
2015-08-04 14:47:00 +03:00
|
|
|
fr.onload = function(evt) {
|
|
|
|
deferred.resolve(new Uint8Array(evt.target.result));
|
|
|
|
};
|
2015-03-11 08:35:00 +03:00
|
|
|
|
2015-08-04 14:47:00 +03:00
|
|
|
fr.onerror = function(evt) {
|
|
|
|
deferred.reject(evt);
|
|
|
|
};
|
2015-03-11 08:35:00 +03:00
|
|
|
|
2015-08-04 14:47:00 +03:00
|
|
|
fr.readAsArrayBuffer(Screenshot.get());
|
|
|
|
} catch(e) {
|
|
|
|
// We pass any errors through to the deferred Promise
|
|
|
|
deferred.reject(e);
|
|
|
|
}
|
2015-03-11 08:35:00 +03:00
|
|
|
|
2015-01-26 21:16:00 +03:00
|
|
|
return deferred.promise;
|
2014-11-12 08:31:00 +03:00
|
|
|
}
|
2014-08-22 12:32:00 +04:00
|
|
|
};
|
|
|
|
|
2014-11-12 08:31:00 +03:00
|
|
|
this.LogCapture = LogCapture;
|