extension: improve debug attach reliability

(by forcing detach first for tabs that already have an attached
debugger -- this crops up a lot if I reload the extension, the old
extension ver stays hanging -- and by attaching before we open/getattr
files too, not just when we open the dir)

(it seems pretty solid now)
This commit is contained in:
Omar Rizwan 2020-12-04 17:40:03 -08:00
parent e4c92ac4e9
commit 34c16fec0d

View file

@ -85,9 +85,13 @@ async function debugTab(tabId) {
debugging[tabId] += 1; debugging[tabId] += 1;
} else { } else {
await new Promise((resolve, reject) => chrome.debugger.attach({tabId}, "1.3", () => { await new Promise((resolve, reject) => chrome.debugger.attach({tabId}, "1.3", function callback() {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError); if (chrome.runtime.lastError.message.indexOf('Another debugger is already attached') !== -1) {
chrome.debugger.detach({tabId}, callback);
} else {
reject(chrome.runtime.lastError);
}
} else { } else {
debugging[tabId] = 1; debugging[tabId] = 1;
resolve(); resolve();
@ -99,11 +103,7 @@ function sendDebuggerCommand(tabId, method, commandParams) {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
chrome.debugger.sendCommand({tabId}, method, commandParams, result => { chrome.debugger.sendCommand({tabId}, method, commandParams, result => {
console.log(method, result); console.log(method, result);
if (result) { if (result) { resolve(result); } else { reject(chrome.runtime.lastError); }
resolve(result);
} else {
reject(chrome.runtime.lastError);
}
}) })
); );
} }
@ -194,7 +194,7 @@ router["/tabs/by-id/*/resources/*"] = {
const tabId = parseInt(pathComponent(path, -3)); const tabId = parseInt(pathComponent(path, -3));
const suffix = pathComponent(path, -1); const suffix = pathComponent(path, -1);
if (!debugging[tabId]) throw new UnixError(unix.EIO); await debugTab(tabId);
await sendDebuggerCommand(tabId, "Page.enable", {}); await sendDebuggerCommand(tabId, "Page.enable", {});
@ -219,6 +219,8 @@ router["/tabs/by-id/*/resources/*"] = {
}, },
async open({path}) { async open({path}) {
// FIXME: cache the file // FIXME: cache the file
const tabId = parseInt(pathComponent(path, -3));
await debugTab(tabId);
return {fh: 3}; return {fh: 3};
}, },
async read({path, fh, size, offset}) { async read({path, fh, size, offset}) {
@ -238,7 +240,7 @@ router["/tabs/by-id/*/resources/*"] = {
url: resource.url url: resource.url
}); });
if (base64Encoded) { if (base64Encoded) {
const arr = Uint8Array.from(atob(data), c => c.charCodeAt(0)); const arr = Uint8Array.from(atob(content), c => c.charCodeAt(0));
const slice = arr.slice(offset, offset + size); const slice = arr.slice(offset, offset + size);
return { buf: String.fromCharCode(...slice) }; return { buf: String.fromCharCode(...slice) };
} else { } else {
@ -249,6 +251,7 @@ router["/tabs/by-id/*/resources/*"] = {
throw new UnixError(unix.ENOENT); throw new UnixError(unix.ENOENT);
}, },
async release({path, fh}) { async release({path, fh}) {
// FIXME: free the debug?
return {}; return {};
} }
}; };
@ -400,7 +403,7 @@ function findRoute(path) {
let port; let port;
async function onMessage(req) { async function onMessage(req) {
if (req.buf) req.buf = atob(req.buf); if (req.buf) req.buf = atob(req.buf);
console.log('req', req); /* console.log('req', req);*/
let response = { op: req.op, error: unix.EIO }; let response = { op: req.op, error: unix.EIO };
/* console.time(req.op + ':' + req.path);*/ /* console.time(req.op + ':' + req.path);*/
@ -418,7 +421,7 @@ async function onMessage(req) {
} }
/* console.timeEnd(req.op + ':' + req.path);*/ /* console.timeEnd(req.op + ':' + req.path);*/
console.log('resp', response); /* console.log('resp', response);*/
port.postMessage(response); port.postMessage(response);
}; };