The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / mjsunit / debug-backtrace.js @ 40c0f755

History | View | Annotate | Download (8.52 KB)

1
// Copyright 2008 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27

    
28
// Flags: --expose-debug-as debug
29
// The functions used for testing backtraces. They are at the top to make the
30
// testing of source line/column easier.
31
function f(x, y) {
32
  a=1;
33
};
34

    
35
function g() {
36
  new f(1);
37
};
38

    
39

    
40
// Get the Debug object exposed from the debug context global object.
41
Debug = debug.Debug
42

    
43
listenerCalled = false;
44
exception = false;
45

    
46

    
47
function ParsedResponse(json) {
48
  this.response_ = eval('(' + json + ')');
49
  this.refs_ = [];
50
  if (this.response_.refs) {
51
    for (var i = 0; i < this.response_.refs.length; i++) {
52
      this.refs_[this.response_.refs[i].handle] = this.response_.refs[i];
53
    }
54
  }
55
}
56

    
57

    
58
ParsedResponse.prototype.response = function() {
59
  return this.response_;
60
}
61

    
62

    
63
ParsedResponse.prototype.body = function() {
64
  return this.response_.body;
65
}
66

    
67

    
68
ParsedResponse.prototype.lookup = function(handle) {
69
  return this.refs_[handle];
70
}
71

    
72

    
73
function listener(event, exec_state, event_data, data) {
74
  try {
75
  if (event == Debug.DebugEvent.Break)
76
  {
77
    // The expected backtrace is
78
    // 0: f
79
    // 1: g
80
    // 2: [anonymous]
81
    
82
    var response;
83
    var backtrace;
84
    var frame;
85
    var source;
86
    
87
    // Get the debug command processor.
88
    var dcp = exec_state.debugCommandProcessor();
89

    
90
    // Get the backtrace.
91
    var json;
92
    json = '{"seq":0,"type":"request","command":"backtrace"}'
93
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
94
    backtrace = response.body();
95
    assertEquals(0, backtrace.fromFrame);
96
    assertEquals(3, backtrace.toFrame);
97
    assertEquals(3, backtrace.totalFrames);
98
    var frames = backtrace.frames;
99
    assertEquals(3, frames.length);
100
    for (var i = 0; i < frames.length; i++) {
101
      assertEquals('frame', frames[i].type);
102
    }
103
    assertEquals(0, frames[0].index);
104
    assertEquals("f", response.lookup(frames[0].func.ref).name);
105
    assertEquals(1, frames[1].index);
106
    assertEquals("g", response.lookup(frames[1].func.ref).name);
107
    assertEquals(2, frames[2].index);
108
    assertEquals("", response.lookup(frames[2].func.ref).name);
109

    
110
    // Get backtrace with two frames.
111
    json = '{"seq":0,"type":"request","command":"backtrace","arguments":{"fromFrame":1,"toFrame":3}}'
112
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
113
    backtrace = response.body();
114
    assertEquals(1, backtrace.fromFrame);
115
    assertEquals(3, backtrace.toFrame);
116
    assertEquals(3, backtrace.totalFrames);
117
    var frames = backtrace.frames;
118
    assertEquals(2, frames.length);
119
    for (var i = 0; i < frames.length; i++) {
120
      assertEquals('frame', frames[i].type);
121
    }
122
    assertEquals(1, frames[0].index);
123
    assertEquals("g", response.lookup(frames[0].func.ref).name);
124
    assertEquals(2, frames[1].index);
125
    assertEquals("", response.lookup(frames[1].func.ref).name);
126

    
127
    // Get the individual frames.
128
    json = '{"seq":0,"type":"request","command":"frame"}'
129
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
130
    frame = response.body();
131
    assertEquals(0, frame.index);
132
    assertEquals("f", response.lookup(frame.func.ref).name);
133
    assertTrue(frame.constructCall);
134
    assertEquals(31, frame.line);
135
    assertEquals(3, frame.column);
136
    assertEquals(2, frame.arguments.length);
137
    assertEquals('x', frame.arguments[0].name);
138
    assertEquals('number', response.lookup(frame.arguments[0].value.ref).type);
139
    assertEquals(1, response.lookup(frame.arguments[0].value.ref).value);
140
    assertEquals('y', frame.arguments[1].name);
141
    assertEquals('undefined', response.lookup(frame.arguments[1].value.ref).type);
142

    
143
    json = '{"seq":0,"type":"request","command":"frame","arguments":{"number":0}}'
144
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
145
    frame = response.body();
146
    assertEquals(0, frame.index);
147
    assertEquals("f", response.lookup(frame.func.ref).name);
148
    assertEquals(31, frame.line);
149
    assertEquals(3, frame.column);
150
    assertEquals(2, frame.arguments.length);
151
    assertEquals('x', frame.arguments[0].name);
152
    assertEquals('number', response.lookup(frame.arguments[0].value.ref).type);
153
    assertEquals(1, response.lookup(frame.arguments[0].value.ref).value);
154
    assertEquals('y', frame.arguments[1].name);
155
    assertEquals('undefined', response.lookup(frame.arguments[1].value.ref).type);
156

    
157
    json = '{"seq":0,"type":"request","command":"frame","arguments":{"number":1}}'
158
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
159
    frame = response.body();
160
    assertEquals(1, frame.index);
161
    assertEquals("g", response.lookup(frame.func.ref).name);
162
    assertFalse(frame.constructCall);
163
    assertEquals(35, frame.line);
164
    assertEquals(2, frame.column);
165
    assertEquals(0, frame.arguments.length);
166

    
167
    json = '{"seq":0,"type":"request","command":"frame","arguments":{"number":2}}'
168
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
169
    frame = response.body();
170
    assertEquals(2, frame.index);
171
    assertEquals("", response.lookup(frame.func.ref).name);
172

    
173
    // Source slices for the individual frames (they all refer to this script).
174
    json = '{"seq":0,"type":"request","command":"source",' +
175
            '"arguments":{"frame":0,"fromLine":30,"toLine":32}}'
176
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
177
    source = response.body();
178
    assertEquals("function f(x, y) {", source.source.substring(0, 18));
179
    assertEquals(30, source.fromLine);
180
    assertEquals(32, source.toLine);
181
    
182
    json = '{"seq":0,"type":"request","command":"source",' +
183
            '"arguments":{"frame":1,"fromLine":31,"toLine":32}}'
184
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
185
    source = response.body();
186
    assertEquals("  a=1;", source.source.substring(0, 6));
187
    assertEquals(31, source.fromLine);
188
    assertEquals(32, source.toLine);
189
    
190
    json = '{"seq":0,"type":"request","command":"source",' +
191
            '"arguments":{"frame":2,"fromLine":35,"toLine":36}}'
192
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
193
    source = response.body();
194
    assertEquals("  new f(1);", source.source.substring(0, 11));
195
    assertEquals(35, source.fromLine);
196
    assertEquals(36, source.toLine);
197
    
198
    // Test line interval way beyond this script will result in an error.
199
    json = '{"seq":0,"type":"request","command":"source",' +
200
            '"arguments":{"frame":0,"fromLine":10000,"toLine":20000}}'
201
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
202
    assertFalse(response.response().success);
203
    
204
    // Test without arguments.
205
    json = '{"seq":0,"type":"request","command":"source"}'
206
    response = new ParsedResponse(dcp.processDebugJSONRequest(json));
207
    source = response.body();
208
    assertEquals(Debug.findScript(f).source, source.source);
209
    
210
    listenerCalled = true;
211
  }
212
  } catch (e) {
213
    exception = e
214
  };
215
};
216

    
217
// Add the debug event listener.
218
Debug.setListener(listener);
219

    
220
// Set a break point and call to invoke the debug event listener.
221
Debug.setBreakPoint(f, 0, 0);
222
g();
223

    
224
// Make sure that the debug event listener vas invoked.
225
assertFalse(exception, "exception in listener");
226
assertTrue(listenerCalled);
227