From ada3d372193dff7efd6bcd05b7e0130a689b9006 Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Wed, 13 May 2009 08:54:50 +0000 Subject: [PATCH] Added support to backtrace from botton of stack to debugger protocol. Fixed backtrace in D8 debugger and added gdb like syntax 'bt n' and 'bt -n' in addition to the already existing 'bt from to'. Review URL: http://codereview.chromium.org/99342 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1929 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/d8.js | 27 ++++++++++++++++++++++++--- src/debug-delay.js | 16 +++++++++++----- test/mjsunit/debug-backtrace.js | 24 +++++++++++++++++++++--- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/d8.js b/src/d8.js index ea2fb44..4c2f152 100644 --- a/src/d8.js +++ b/src/d8.js @@ -498,9 +498,26 @@ DebugRequest.prototype.stepCommandToJSONRequest_ = function(args) { DebugRequest.prototype.backtraceCommandToJSONRequest_ = function(args) { // Build a backtrace request from the text command. var request = this.createRequest('backtrace'); + + // Default is to show top 10 frames. + request.arguments = {}; + request.arguments.fromFrame = 0; + request.arguments.toFrame = 10; + args = args.split(/\s*[ ]+\s*/g); - if (args.length == 2) { - request.arguments = {}; + if (args.length == 1 && args[0].length > 0) { + var frameCount = parseInt(args[0]); + if (frameCount > 0) { + // Show top frames. + request.arguments.fromFrame = 0; + request.arguments.toFrame = frameCount; + } else { + // Show bottom frames. + request.arguments.fromFrame = 0; + request.arguments.toFrame = -frameCount; + request.arguments.bottom = true; + } + } else if (args.length == 2) { var fromFrame = parseInt(args[0]); var toFrame = parseInt(args[1]); if (isNaN(fromFrame) || fromFrame < 0) { @@ -513,9 +530,13 @@ DebugRequest.prototype.backtraceCommandToJSONRequest_ = function(args) { throw new Error('Invalid arguments start frame cannot be larger ' + 'than end frame.'); } + // Show frame range. request.arguments.fromFrame = fromFrame; request.arguments.toFrame = toFrame + 1; + } else if (args.length > 2) { + throw new Error('Invalid backtrace arguments.'); } + return request.toJSONProtocol(); }; @@ -755,7 +776,7 @@ DebugRequest.prototype.helpCommand_ = function(args) { print(' break on function: location is ##'); print(' break on script position: location is name:line[:column]'); print('clear '); - print('backtrace [from frame #] [to frame #]]'); + print('backtrace [n] | [-n] | [from to]'); print('frame '); print('step [in | next | out| min [step count]]'); print('print '); diff --git a/src/debug-delay.js b/src/debug-delay.js index 865b2ba..598921a 100644 --- a/src/debug-delay.js +++ b/src/debug-delay.js @@ -1452,12 +1452,18 @@ DebugCommandProcessor.prototype.backtraceRequest_ = function(request, response) // Get the range from the arguments. if (request.arguments) { - from_index = request.arguments.fromFrame; - if (from_index < 0) { - return response.failed('Invalid frame number'); + if (request.arguments.fromFrame) { + from_index = request.arguments.fromFrame; + } + if (request.arguments.toFrame) { + to_index = request.arguments.toFrame; + } + if (request.arguments.bottom) { + var tmp_index = total_frames - from_index; + from_index = total_frames - to_index + to_index = tmp_index; } - to_index = request.arguments.toFrame; - if (to_index < 0) { + if (from_index < 0 || to_index < 0) { return response.failed('Invalid frame number'); } } diff --git a/test/mjsunit/debug-backtrace.js b/test/mjsunit/debug-backtrace.js index f08f639..1d2bb9a 100644 --- a/test/mjsunit/debug-backtrace.js +++ b/test/mjsunit/debug-backtrace.js @@ -37,7 +37,7 @@ var m = function() { }; function g() { - m(); + m(); }; @@ -80,8 +80,9 @@ function listener(event, exec_state, event_data, data) { { // The expected backtrace is // 0: f - // 1: g - // 2: [anonymous] + // 1: m + // 2: g + // 3: [anonymous] var response; var backtrace; @@ -133,6 +134,23 @@ function listener(event, exec_state, event_data, data) { assertEquals(2, frames[1].index); assertEquals("g", response.lookup(frames[1].func.ref).name); + // Get backtrace with bottom two frames. + json = '{"seq":0,"type":"request","command":"backtrace","arguments":{"fromFrame":0,"toFrame":2, "bottom":true}}' + response = new ParsedResponse(dcp.processDebugJSONRequest(json)); + backtrace = response.body(); + assertEquals(2, backtrace.fromFrame); + assertEquals(4, backtrace.toFrame); + assertEquals(4, backtrace.totalFrames); + var frames = backtrace.frames; + assertEquals(2, frames.length); + for (var i = 0; i < frames.length; i++) { + assertEquals('frame', frames[i].type); + } + assertEquals(2, frames[0].index); + assertEquals("g", response.lookup(frames[0].func.ref).name); + assertEquals(3, frames[1].index); + assertEquals("", response.lookup(frames[1].func.ref).name); + // Get the individual frames. json = '{"seq":0,"type":"request","command":"frame"}' response = new ParsedResponse(dcp.processDebugJSONRequest(json)); -- 2.7.4