Search code examples
javascriptgoogle-chrome-extensiongoogle-chrome-devtoolsheap-memory

Chrome Debugging Protocol: HeapProfiler.getHeapSnapshot Ignores Callback


I'm working on a testing suite (implemented as a Chrome extension) that programmatically takes and analyzes heap snapshots with Chrome/Chromium's remote debugging protocol. Because Profiler.* doesn't seem to be part of the public protocol, I'm using this page for reference.

Right now, I'm able to take a heap snapshot by calling HeapProfiler.takeHeapSnapshot like in the snippet below. However, when I try calling HeapProfiler.getHeapSnapshot, my callback is completely ignored.

var debuggerId = {tabId: sender.tab.id};
chrome.debugger.attach(debuggerId, "1.0", function() {
    var listener = function(source, method, params) {
        if(source.tabId === debuggerId.tabId) {
            if(method === "HeapProfiler.addProfileHeader") {
                var uid = params.header.uid;
                chrome.debugger.sendCommand(debuggerId, "HeapProfiler.getHeapSnapshot", { uid: uid }, function() {
                    console.log("I never get called!");
                });
                chrome.debugger.onEvent.removeListener(listener);
            }
        }
    };
    chrome.debugger.onEvent.addListener(listener);
    chrome.debugger.sendCommand(debuggerId, "HeapProfiler.takeHeapSnapshot", { reportProgress: false }, function() {
        if(chrome.runtime.lastError) { console.error(chrome.runtime.lastError); }
    });
});

Solution

  • It is a bug in DevTools. I've filled an issue in chrome bug tracker.

    Fortunately there is a workaround. You have to enable Debugger agent before getting the snapshot.

    chrome.debugger.sendCommand(debuggeeId,"Debugger.enable", { }, function() { });
    

    Three other commands were also affected by this bug.

    Profiler.start
    Profiler.stop
    Profiler.getCPUProfile