Skip to content

Instantly share code, notes, and snippets.

@kares
Last active November 24, 2016 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kares/7bc6af522faff44173c9391bc629b5df to your computer and use it in GitHub Desktop.
Save kares/7bc6af522faff44173c9391bc629b5df to your computer and use it in GitHub Desktop.
Extract relevant (partial backtrace) information from heap-dump using OQL
map( heap.objects('org.jruby.runtime.ThreadContext'), function(ctx) {
var str = toHtml(ctx) + " oid: " + objectid(ctx);
str += " ";
var toHtmlObj = function(obj) { return "<br> " + toHtml(obj); };
try {
//return str + doObjPath(ctx, toHtmlObj, 'thread', 'threadImpl', 'nativeThread', 'referent');
str += resolveObjPath(ctx, 'thread', 'threadImpl', 'nativeThread', 'referent', 'name').toString();
str += '<br> backtraceIndex = ' + ctx.backtraceIndex;
var i = 0;
map( resolve(ctx, 'backtrace'), function(e) {
map( [ heap.findObject( objectid(e) ) ], function(elem) {
if (elem) str += "<br> " + (i++) + ' ' + showBacktraceElement(elem);
} );
} );
return str;
return str;
} catch(e) { return str + " ERROR: " + e; }
});
function doObjPath(obj, fn) { // some, fn, 'foo', 'bar'
if (typeof(fn) === 'undefined') throw 'no function given';
var out = ''; var args = Array.prototype.slice.call(arguments);
if ( args.length > 2 ) {
var prop = args[2]; var args0 = args[0];
for ( var i=2; i<args.length - 1; i++ ) { args[i] = args[i+1]; }
args = args.slice(0, args.length - 1);
map( [ heap.findObject( objectid( obj[prop] ) ) ], function(elem) {
args[0] = elem; out += fn(elem) + ' (' + prop + ')';
} );
//out += ' - ' + args.toString();
if ( args0 === args[0] ) out += "'" + prop + "' not set!";
else out += doObjPath.apply(this, args);
}
return out;
}
function resolveObjPath(obj) { // this, 'foo', 'bar' -> this['foo']['bar']
var last;
var args = Array.prototype.slice.call(arguments);
args.unshift(obj); // args[0]
args[1] = function(val) { last = val; };
doObjPath.apply(this, args);
return last;
}
function resolve(obj, prop) {
var val;
map( [ heap.findObject( objectid( obj[prop] ) ) ], function(elem) {
val = elem;
} );
return val;
}
function formatBacktraceElement(elem) {
if ( ! elem.method ) return null; var str = '';
str += heap.findObject( objectid(elem.method) ).toString();
str += ' at ';
str += elem.filename ? heap.findObject( objectid(elem.filename) ).toString() : null;
str += ':' + elem.line;
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment