OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 return result; | 73 return result; |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 // Global object holding debugger related constants and state. | 77 // Global object holding debugger related constants and state. |
78 const Debug = {}; | 78 const Debug = {}; |
79 | 79 |
80 | 80 |
81 // Debug events which can occour in the V8 JavaScript engine. These originate | 81 // Debug events which can occour in the V8 JavaScript engine. These originate |
82 // from the API include file debug.h. | 82 // from the API include file v8-debug.h. |
83 Debug.DebugEvent = { Break: 1, | 83 Debug.DebugEvent = { Break: 1, |
84 Exception: 2, | 84 Exception: 2, |
85 NewFunction: 3, | 85 NewFunction: 3, |
86 BeforeCompile: 4, | 86 BeforeCompile: 4, |
87 AfterCompile: 5 }; | 87 AfterCompile: 5 }; |
88 | 88 |
89 | 89 |
90 // The different types of scripts matching enum ScriptType in objects.h. | 90 // The different types of scripts matching enum ScriptType in objects.h. |
91 Debug.ScriptType = { Native: 0, | 91 Debug.ScriptType = { Native: 0, |
92 Extension: 1, | 92 Extension: 1, |
93 Normal: 2 }; | 93 Normal: 2 }; |
94 | 94 |
95 | 95 |
96 // Current debug state. | 96 // Current debug state. |
97 const kNoFrame = -1; | 97 const kNoFrame = -1; |
98 Debug.State = { | 98 Debug.State = { |
99 currentFrame: kNoFrame, | 99 currentFrame: kNoFrame, |
100 currentSourceLine: -1 | 100 currentSourceLine: -1 |
101 } | 101 } |
| 102 var trace_compile = false; // Tracing all compile events? |
102 | 103 |
103 | 104 |
104 function DebugEventToText(event) { | 105 function DebugEventToText(event) { |
105 if (event.eventType() == 1) { | 106 switch (event.eventType()) { |
106 // Build the break details. | 107 case Debug.DebugEvent.Break: |
107 var details = ''; | 108 // Build the break details. |
108 if (event.breakPointsHit()) { | 109 var details = ''; |
109 details += 'breakpoint'; | 110 if (event.breakPointsHit()) { |
110 if (event.breakPointsHit().length > 1) { | 111 details += 'breakpoint'; |
111 details += 's'; | 112 if (event.breakPointsHit().length > 1) { |
| 113 details += 's'; |
| 114 } |
| 115 details += ' #'; |
| 116 for (var i = 0; i < event.breakPointsHit().length; i++) { |
| 117 if (i > 0) { |
| 118 details += ', #'; |
| 119 } |
| 120 // Find the break point number. For break points originating from a |
| 121 // script break point display the script break point number. |
| 122 var break_point = event.breakPointsHit()[i]; |
| 123 var script_break_point = break_point.script_break_point(); |
| 124 if (script_break_point) { |
| 125 details += script_break_point.number(); |
| 126 } else { |
| 127 details += break_point.number(); |
| 128 } |
| 129 } |
| 130 } else { |
| 131 details += 'break'; |
112 } | 132 } |
113 details += ' #'; | 133 details += ' in '; |
114 for (var i = 0; i < event.breakPointsHit().length; i++) { | 134 details += event.executionState().frame(0).invocationText(); |
115 if (i > 0) { | |
116 details += ', #'; | |
117 } | |
118 // Find the break point number. For break points originating from a | |
119 // script break point display the script break point number. | |
120 var break_point = event.breakPointsHit()[i]; | |
121 var script_break_point = break_point.script_break_point(); | |
122 if (script_break_point) { | |
123 details += script_break_point.number(); | |
124 } else { | |
125 details += break_point.number(); | |
126 } | |
127 } | |
128 } else { | |
129 details += 'break'; | |
130 } | |
131 details += ' in '; | |
132 details += event.executionState().frame(0).invocationText(); | |
133 details += ' at '; | |
134 details += event.executionState().frame(0).sourceAndPositionText(); | |
135 details += '\n' | |
136 if (event.func().script()) { | |
137 details += FrameSourceUnderline(event.executionState().frame(0)); | |
138 } | |
139 Debug.State.currentSourceLine = | |
140 event.executionState().frame(0).sourceLine(); | |
141 Debug.State.currentFrame = 0; | |
142 return details; | |
143 } else if (event.eventType() == 2) { | |
144 var details = ''; | |
145 if (event.uncaught_) { | |
146 details += 'Uncaught: '; | |
147 } else { | |
148 details += 'Exception: '; | |
149 } | |
150 | |
151 details += '"'; | |
152 details += event.exception(); | |
153 details += '"'; | |
154 if (event.executionState().frameCount() > 0) { | |
155 details += '"'; | |
156 details += event.exception(); | |
157 details += ' at '; | 135 details += ' at '; |
158 details += event.executionState().frame(0).sourceAndPositionText(); | 136 details += event.executionState().frame(0).sourceAndPositionText(); |
159 details += '\n'; | 137 details += '\n' |
160 details += FrameSourceUnderline(event.executionState().frame(0)); | 138 if (event.func().script()) { |
| 139 details += FrameSourceUnderline(event.executionState().frame(0)); |
| 140 } |
161 Debug.State.currentSourceLine = | 141 Debug.State.currentSourceLine = |
162 event.executionState().frame(0).sourceLine(); | 142 event.executionState().frame(0).sourceLine(); |
163 Debug.State.currentFrame = 0; | 143 Debug.State.currentFrame = 0; |
164 } else { | 144 return details; |
165 details += ' (empty stack)'; | |
166 Debug.State.currentSourceLine = -1; | |
167 Debug.State.currentFrame = kNoFrame; | |
168 } | |
169 | 145 |
170 return details; | 146 case Debug.DebugEvent.Exception: |
| 147 var details = ''; |
| 148 if (event.uncaught_) { |
| 149 details += 'Uncaught: '; |
| 150 } else { |
| 151 details += 'Exception: '; |
| 152 } |
| 153 |
| 154 details += '"'; |
| 155 details += event.exception(); |
| 156 details += '"'; |
| 157 if (event.executionState().frameCount() > 0) { |
| 158 details += '"'; |
| 159 details += event.exception(); |
| 160 details += ' at '; |
| 161 details += event.executionState().frame(0).sourceAndPositionText(); |
| 162 details += '\n'; |
| 163 details += FrameSourceUnderline(event.executionState().frame(0)); |
| 164 Debug.State.currentSourceLine = |
| 165 event.executionState().frame(0).sourceLine(); |
| 166 Debug.State.currentFrame = 0; |
| 167 } else { |
| 168 details += ' (empty stack)'; |
| 169 Debug.State.currentSourceLine = -1; |
| 170 Debug.State.currentFrame = kNoFrame; |
| 171 } |
| 172 return details; |
| 173 |
| 174 case Debug.DebugEvent.AfterCompile: |
| 175 if (trace_compile) { |
| 176 details = 'Source ' + event.script().name() + ' compiled:\n' |
| 177 var source = event.script().source(); |
| 178 if (!(source[source.length - 1] == '\n')) { |
| 179 details += source; |
| 180 } else { |
| 181 details += source.substring(0, source.length - 1); |
| 182 } |
| 183 return details; |
| 184 } else { |
| 185 return ''; |
| 186 } |
171 } | 187 } |
172 | 188 |
173 return 'Unknown debug event ' + event.eventType(); | 189 return 'Unknown debug event ' + event.eventType(); |
174 }; | 190 }; |
175 | 191 |
176 | 192 |
177 function SourceUnderline(source_text, position) { | 193 function SourceUnderline(source_text, position) { |
178 if (!source_text) { | 194 if (!source_text) { |
179 return; | 195 return; |
180 } | 196 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 | 292 |
277 case 'break': | 293 case 'break': |
278 case 'b': | 294 case 'b': |
279 this.request_ = this.breakCommandToJSONRequest_(args); | 295 this.request_ = this.breakCommandToJSONRequest_(args); |
280 break; | 296 break; |
281 | 297 |
282 case 'clear': | 298 case 'clear': |
283 this.request_ = this.clearCommandToJSONRequest_(args); | 299 this.request_ = this.clearCommandToJSONRequest_(args); |
284 break; | 300 break; |
285 | 301 |
| 302 case 'trace': |
| 303 // Return undefined to indicate command handled internally (no JSON). |
| 304 this.request_ = void 0; |
| 305 this.traceCommand_(args); |
| 306 break; |
| 307 |
286 case 'help': | 308 case 'help': |
287 case '?': | 309 case '?': |
288 this.helpCommand_(args); | 310 this.helpCommand_(args); |
289 // Return null to indicate no JSON to send (command handled internally). | 311 // Return undefined to indicate command handled internally (no JSON). |
290 this.request_ = void 0; | 312 this.request_ = void 0; |
291 break; | 313 break; |
292 | 314 |
293 default: | 315 default: |
294 throw new Error('Unknown command "' + cmd + '"'); | 316 throw new Error('Unknown command "' + cmd + '"'); |
295 } | 317 } |
296 | 318 |
297 last_cmd = cmd; | 319 last_cmd = cmd; |
298 } | 320 } |
299 | 321 |
300 DebugRequest.prototype.JSONRequest = function() { | 322 DebugRequest.prototype.JSONRequest = function() { |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 request.arguments = {}; | 612 request.arguments = {}; |
591 request.arguments.breakpoint = parseInt(args); | 613 request.arguments.breakpoint = parseInt(args); |
592 } else { | 614 } else { |
593 throw new Error('Invalid break arguments.'); | 615 throw new Error('Invalid break arguments.'); |
594 } | 616 } |
595 | 617 |
596 return request.toJSONProtocol(); | 618 return request.toJSONProtocol(); |
597 }; | 619 }; |
598 | 620 |
599 | 621 |
600 // Create a JSON request for the break command. | 622 // Handle the trace command. |
| 623 DebugRequest.prototype.traceCommand_ = function(args) { |
| 624 // Process arguments. |
| 625 if (args && args.length > 0) { |
| 626 if (args == 'compile') { |
| 627 trace_compile = !trace_compile; |
| 628 print('Tracing of compiled scripts ' + (trace_compile ? 'on' : 'off')); |
| 629 } else { |
| 630 throw new Error('Invalid trace arguments.'); |
| 631 } |
| 632 } else { |
| 633 throw new Error('Invalid trace arguments.'); |
| 634 } |
| 635 } |
| 636 |
| 637 // Handle the help command. |
601 DebugRequest.prototype.helpCommand_ = function(args) { | 638 DebugRequest.prototype.helpCommand_ = function(args) { |
602 // Help os quite simple. | 639 // Help os quite simple. |
603 if (args && args.length > 0) { | 640 if (args && args.length > 0) { |
604 print('warning: arguments to \'help\' are ignored'); | 641 print('warning: arguments to \'help\' are ignored'); |
605 } | 642 } |
606 | 643 |
607 print('break location [condition]'); | 644 print('break location [condition]'); |
608 print('clear <breakpoint #>'); | 645 print('clear <breakpoint #>'); |
609 print('backtrace [from frame #] [to frame #]]'); | 646 print('backtrace [from frame #] [to frame #]]'); |
610 print('frame <frame #>'); | 647 print('frame <frame #>'); |
611 print('step [in | next | out| min [step count]]'); | 648 print('step [in | next | out| min [step count]]'); |
612 print('print <expression>'); | 649 print('print <expression>'); |
613 print('source [from line [num lines]]'); | 650 print('source [from line [num lines]]'); |
614 print('scripts'); | 651 print('scripts'); |
615 print('continue'); | 652 print('continue'); |
| 653 print('trace compile'); |
616 print('help'); | 654 print('help'); |
617 } | 655 } |
618 | 656 |
619 | 657 |
620 function formatHandleReference_(value) { | 658 function formatHandleReference_(value) { |
621 return '#' + value.handle() + '#'; | 659 return '#' + value.handle() + '#'; |
622 } | 660 } |
623 | 661 |
624 | 662 |
625 // Convert a JSON response to text for display in a text based debugger. | 663 // Convert a JSON response to text for display in a text based debugger. |
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1266 json += NumberToJSON_(elem); | 1304 json += NumberToJSON_(elem); |
1267 } else if (typeof(elem) === 'string') { | 1305 } else if (typeof(elem) === 'string') { |
1268 json += StringToJSON_(elem); | 1306 json += StringToJSON_(elem); |
1269 } else { | 1307 } else { |
1270 json += elem; | 1308 json += elem; |
1271 } | 1309 } |
1272 } | 1310 } |
1273 json += ']'; | 1311 json += ']'; |
1274 return json; | 1312 return json; |
1275 } | 1313 } |
OLD | NEW |