Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1387)

Side by Side Diff: runtime/vm/dart_api_impl_test.cc

Issue 51793002: Add an API function to get a debugger stack trace from an error handle. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: . Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/include/dart_debugger_api.h ('k') | runtime/vm/debugger.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/builtin.h" 5 #include "bin/builtin.h"
6 #include "include/dart_api.h" 6 #include "include/dart_api.h"
7 #include "include/dart_debugger_api.h" 7 #include "include/dart_debugger_api.h"
8 #include "include/dart_mirrors_api.h" 8 #include "include/dart_mirrors_api.h"
9 #include "include/dart_native_api.h" 9 #include "include/dart_native_api.h"
10 #include "platform/assert.h" 10 #include "platform/assert.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 EXPECT(Dart_IsError(Dart_ErrorGetException(instance))); 55 EXPECT(Dart_IsError(Dart_ErrorGetException(instance)));
56 EXPECT(Dart_IsError(Dart_ErrorGetException(error))); 56 EXPECT(Dart_IsError(Dart_ErrorGetException(error)));
57 EXPECT_VALID(Dart_ErrorGetException(exception)); 57 EXPECT_VALID(Dart_ErrorGetException(exception));
58 58
59 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(instance))); 59 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(instance)));
60 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(error))); 60 EXPECT(Dart_IsError(Dart_ErrorGetStacktrace(error)));
61 EXPECT_VALID(Dart_ErrorGetStacktrace(exception)); 61 EXPECT_VALID(Dart_ErrorGetStacktrace(exception));
62 } 62 }
63 63
64 64
65 TEST_CASE(StacktraceInfo) {
66 const char* kScriptChars =
67 "bar() => throw new Error();\n"
68 "foo() => bar();\n"
69 "testMain() => foo();\n";
70
71 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
72 Dart_Handle error = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
73
74 EXPECT(Dart_IsError(error));
75
76 Dart_StackTrace stacktrace;
77 Dart_Handle result = Dart_GetStackTraceFromError(error, &stacktrace);
78 EXPECT_VALID(result);
79
80 intptr_t frame_count = 0;
81 result = Dart_StackTraceLength(stacktrace, &frame_count);
82 EXPECT_VALID(result);
83 EXPECT_EQ(3, frame_count);
84
85 Dart_Handle function_name;
86 Dart_Handle script_url;
87 intptr_t line_number = 0;
88 intptr_t column_number = 0;
89 const char* cstr = "";
90
91 Dart_ActivationFrame frame;
92 result = Dart_GetActivationFrame(stacktrace, 0, &frame);
93 EXPECT_VALID(result);
94 result = Dart_ActivationFrameInfo(
95 frame, &function_name, &script_url, &line_number, &column_number);
96 EXPECT_VALID(result);
97 Dart_StringToCString(function_name, &cstr);
98 EXPECT_STREQ("bar", cstr);
99 Dart_StringToCString(script_url, &cstr);
100 EXPECT_STREQ("dart:test-lib", cstr);
101 EXPECT_EQ(1, line_number);
102 EXPECT_EQ(10, column_number);
103
104 result = Dart_GetActivationFrame(stacktrace, 1, &frame);
105 EXPECT_VALID(result);
106 result = Dart_ActivationFrameInfo(
107 frame, &function_name, &script_url, &line_number, &column_number);
108 EXPECT_VALID(result);
109 Dart_StringToCString(function_name, &cstr);
110 EXPECT_STREQ("foo", cstr);
111 Dart_StringToCString(script_url, &cstr);
112 EXPECT_STREQ("dart:test-lib", cstr);
113 EXPECT_EQ(2, line_number);
114 EXPECT_EQ(13, column_number);
115
116 result = Dart_GetActivationFrame(stacktrace, 2, &frame);
117 EXPECT_VALID(result);
118 result = Dart_ActivationFrameInfo(
119 frame, &function_name, &script_url, &line_number, &column_number);
120 EXPECT_VALID(result);
121 Dart_StringToCString(function_name, &cstr);
122 EXPECT_STREQ("testMain", cstr);
123 Dart_StringToCString(script_url, &cstr);
124 EXPECT_STREQ("dart:test-lib", cstr);
125 EXPECT_EQ(3, line_number);
126 EXPECT_EQ(18, column_number);
127
128 // Out-of-bounds frames.
129 result = Dart_GetActivationFrame(stacktrace, frame_count, &frame);
130 EXPECT(Dart_IsError(result));
131 result = Dart_GetActivationFrame(stacktrace, -1, &frame);
132 EXPECT(Dart_IsError(result));
133 }
134
135
136 TEST_CASE(DeepStacktraceInfo) {
137 const char* kScriptChars =
138 "foo(n) => n == 1 ? throw new Error() : foo(n-1);\n"
139 "testMain() => foo(50);\n";
140
141 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
142 Dart_Handle error = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
143
144 EXPECT(Dart_IsError(error));
145
146 Dart_StackTrace stacktrace;
147 Dart_Handle result = Dart_GetStackTraceFromError(error, &stacktrace);
148 EXPECT_VALID(result);
149
150 intptr_t frame_count = 0;
151 result = Dart_StackTraceLength(stacktrace, &frame_count);
152 EXPECT_VALID(result);
153 EXPECT_EQ(51, frame_count);
154 // Test something bigger than the preallocated size to verify nothing was
155 // truncated.
156 EXPECT(51 > Stacktrace::kPreallocatedStackdepth);
157
158 Dart_Handle function_name;
159 Dart_Handle script_url;
160 intptr_t line_number = 0;
161 intptr_t column_number = 0;
162 const char* cstr = "";
163
164 // Top frame at positioned at throw.
165 Dart_ActivationFrame frame;
166 result = Dart_GetActivationFrame(stacktrace, 0, &frame);
167 EXPECT_VALID(result);
168 result = Dart_ActivationFrameInfo(
169 frame, &function_name, &script_url, &line_number, &column_number);
170 EXPECT_VALID(result);
171 Dart_StringToCString(function_name, &cstr);
172 EXPECT_STREQ("foo", cstr);
173 Dart_StringToCString(script_url, &cstr);
174 EXPECT_STREQ("dart:test-lib", cstr);
175 EXPECT_EQ(1, line_number);
176 EXPECT_EQ(20, column_number);
177
178 // Middle frames positioned at the recursive call.
179 for (intptr_t frame_index = 1;
180 frame_index < (frame_count - 1);
181 frame_index++) {
182 result = Dart_GetActivationFrame(stacktrace, frame_index, &frame);
183 EXPECT_VALID(result);
184 result = Dart_ActivationFrameInfo(
185 frame, &function_name, &script_url, &line_number, &column_number);
186 EXPECT_VALID(result);
187 Dart_StringToCString(function_name, &cstr);
188 EXPECT_STREQ("foo", cstr);
189 Dart_StringToCString(script_url, &cstr);
190 EXPECT_STREQ("dart:test-lib", cstr);
191 EXPECT_EQ(1, line_number);
192 EXPECT_EQ(43, column_number);
193 }
194
195 // Bottom frame positioned at testMain().
196 result = Dart_GetActivationFrame(stacktrace, frame_count - 1, &frame);
197 EXPECT_VALID(result);
198 result = Dart_ActivationFrameInfo(
199 frame, &function_name, &script_url, &line_number, &column_number);
200 EXPECT_VALID(result);
201 Dart_StringToCString(function_name, &cstr);
202 EXPECT_STREQ("testMain", cstr);
203 Dart_StringToCString(script_url, &cstr);
204 EXPECT_STREQ("dart:test-lib", cstr);
205 EXPECT_EQ(2, line_number);
206 EXPECT_EQ(18, column_number);
207
208 // Out-of-bounds frames.
209 result = Dart_GetActivationFrame(stacktrace, frame_count, &frame);
210 EXPECT(Dart_IsError(result));
211 result = Dart_GetActivationFrame(stacktrace, -1, &frame);
212 EXPECT(Dart_IsError(result));
213 }
214
215
216 TEST_CASE(StackOverflowStacktraceInfo) {
217 const char* kScriptChars =
218 "class C {\n"
219 " static foo() => foo();\n"
220 "}\n"
221 "testMain() => C.foo();\n";
222
223 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
224 Dart_Handle error = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
225
226 EXPECT(Dart_IsError(error));
227
228 Dart_StackTrace stacktrace;
229 Dart_Handle result = Dart_GetStackTraceFromError(error, &stacktrace);
230 EXPECT_VALID(result);
231
232 intptr_t frame_count = 0;
233 result = Dart_StackTraceLength(stacktrace, &frame_count);
234 EXPECT_VALID(result);
235 EXPECT_EQ(Stacktrace::kPreallocatedStackdepth - 1, frame_count);
236
237 Dart_Handle function_name;
238 Dart_Handle script_url;
239 intptr_t line_number = 0;
240 intptr_t column_number = 0;
241 const char* cstr = "";
242
243 // Top frame at recursive call.
244 Dart_ActivationFrame frame;
245 result = Dart_GetActivationFrame(stacktrace, 0, &frame);
246 EXPECT_VALID(result);
247 result = Dart_ActivationFrameInfo(
248 frame, &function_name, &script_url, &line_number, &column_number);
249 EXPECT_VALID(result);
250 Dart_StringToCString(function_name, &cstr);
251 EXPECT_STREQ("C.foo", cstr);
252 Dart_StringToCString(script_url, &cstr);
253 EXPECT_STREQ("dart:test-lib", cstr);
254 EXPECT_EQ(2, line_number);
255 EXPECT_EQ(3, column_number);
256
257 // Out-of-bounds frames.
258 result = Dart_GetActivationFrame(stacktrace, frame_count, &frame);
259 EXPECT(Dart_IsError(result));
260 result = Dart_GetActivationFrame(stacktrace, -1, &frame);
261 EXPECT(Dart_IsError(result));
262 }
263
264
265 TEST_CASE(OutOfMemoryStacktraceInfo) {
266 const char* kScriptChars =
267 "var number_of_ints = 134000000;\n"
268 "testMain() {\n"
269 " new List<int>(number_of_ints)\n"
270 "}\n";
271
272 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
273 Dart_Handle error = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
274
275 EXPECT(Dart_IsError(error));
276
277 Dart_StackTrace stacktrace;
278 Dart_Handle result = Dart_GetStackTraceFromError(error, &stacktrace);
279 EXPECT(Dart_IsError(result)); // No Stacktrace for OutOfMemory.
280 }
281
282
283 void CurrentStackTraceNative(Dart_NativeArguments args) {
284 Dart_EnterScope();
285
286 Dart_StackTrace stacktrace;
287 Dart_Handle result = Dart_GetStackTrace(&stacktrace);
288 EXPECT_VALID(result);
289
290 intptr_t frame_count = 0;
291 result = Dart_StackTraceLength(stacktrace, &frame_count);
292 EXPECT_VALID(result);
293 EXPECT_EQ(52, frame_count);
294 // Test something bigger than the preallocated size to verify nothing was
295 // truncated.
296 EXPECT(52 > Stacktrace::kPreallocatedStackdepth);
297
298 Dart_Handle function_name;
299 Dart_Handle script_url;
300 intptr_t line_number = 0;
301 intptr_t column_number = 0;
302 const char* cstr = "";
303
304 // Top frame is inspectStack().
305 Dart_ActivationFrame frame;
306 result = Dart_GetActivationFrame(stacktrace, 0, &frame);
307 EXPECT_VALID(result);
308 result = Dart_ActivationFrameInfo(
309 frame, &function_name, &script_url, &line_number, &column_number);
310 EXPECT_VALID(result);
311 Dart_StringToCString(function_name, &cstr);
312 EXPECT_STREQ("inspectStack", cstr);
313 Dart_StringToCString(script_url, &cstr);
314 EXPECT_STREQ("dart:test-lib", cstr);
315 EXPECT_EQ(1, line_number);
316 EXPECT_EQ(47, column_number);
317
318 // Second frame is foo() positioned at call to inspectStack().
319 result = Dart_GetActivationFrame(stacktrace, 1, &frame);
320 EXPECT_VALID(result);
321 result = Dart_ActivationFrameInfo(
322 frame, &function_name, &script_url, &line_number, &column_number);
323 EXPECT_VALID(result);
324 Dart_StringToCString(function_name, &cstr);
325 EXPECT_STREQ("foo", cstr);
326 Dart_StringToCString(script_url, &cstr);
327 EXPECT_STREQ("dart:test-lib", cstr);
328 EXPECT_EQ(2, line_number);
329 EXPECT_EQ(32, column_number);
330
331 // Middle frames positioned at the recursive call.
332 for (intptr_t frame_index = 2;
333 frame_index < (frame_count - 1);
334 frame_index++) {
335 result = Dart_GetActivationFrame(stacktrace, frame_index, &frame);
336 EXPECT_VALID(result);
337 result = Dart_ActivationFrameInfo(
338 frame, &function_name, &script_url, &line_number, &column_number);
339 EXPECT_VALID(result);
340 Dart_StringToCString(function_name, &cstr);
341 EXPECT_STREQ("foo", cstr);
342 Dart_StringToCString(script_url, &cstr);
343 EXPECT_STREQ("dart:test-lib", cstr);
344 EXPECT_EQ(2, line_number);
345 EXPECT_EQ(40, column_number);
346 }
347
348 // Bottom frame positioned at testMain().
349 result = Dart_GetActivationFrame(stacktrace, frame_count - 1, &frame);
350 EXPECT_VALID(result);
351 result = Dart_ActivationFrameInfo(
352 frame, &function_name, &script_url, &line_number, &column_number);
353 EXPECT_VALID(result);
354 Dart_StringToCString(function_name, &cstr);
355 EXPECT_STREQ("testMain", cstr);
356 Dart_StringToCString(script_url, &cstr);
357 EXPECT_STREQ("dart:test-lib", cstr);
358 EXPECT_EQ(3, line_number);
359 EXPECT_EQ(18, column_number);
360
361 // Out-of-bounds frames.
362 result = Dart_GetActivationFrame(stacktrace, frame_count, &frame);
363 EXPECT(Dart_IsError(result));
364 result = Dart_GetActivationFrame(stacktrace, -1, &frame);
365 EXPECT(Dart_IsError(result));
366
367 Dart_SetReturnValue(args, Dart_NewInteger(42));
368 Dart_ExitScope();
369 }
370
371
372 static Dart_NativeFunction CurrentStackTraceNativeLookup(
373 Dart_Handle name, int argument_count) {
374 return reinterpret_cast<Dart_NativeFunction>(&CurrentStackTraceNative);
375 }
376
377
378 TEST_CASE(CurrentStacktraceInfo) {
379 const char* kScriptChars =
380 "inspectStack() native 'CurrentStackTraceNatve';\n"
381 "foo(n) => n == 1 ? inspectStack() : foo(n-1);\n"
382 "testMain() => foo(50);\n";
383
384 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
385 &CurrentStackTraceNativeLookup);
386 Dart_Handle result = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
387 EXPECT_VALID(result);
388 EXPECT(Dart_IsInteger(result));
389 int64_t value = 0;
390 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
391 EXPECT_EQ(42, value);
392 }
393
394
65 TEST_CASE(ErrorHandleTypes) { 395 TEST_CASE(ErrorHandleTypes) {
66 Isolate* isolate = Isolate::Current(); 396 Isolate* isolate = Isolate::Current();
67 const String& compile_message = String::Handle(String::New("CompileError")); 397 const String& compile_message = String::Handle(String::New("CompileError"));
68 const String& fatal_message = String::Handle(String::New("FatalError")); 398 const String& fatal_message = String::Handle(String::New("FatalError"));
69 399
70 Dart_Handle not_error = NewString("NotError"); 400 Dart_Handle not_error = NewString("NotError");
71 Dart_Handle api_error = Api::NewError("Api%s", "Error"); 401 Dart_Handle api_error = Api::NewError("Api%s", "Error");
72 Dart_Handle exception_error = 402 Dart_Handle exception_error =
73 Dart_NewUnhandledExceptionError(NewString("ExceptionError")); 403 Dart_NewUnhandledExceptionError(NewString("ExceptionError"));
74 Dart_Handle compile_error = 404 Dart_Handle compile_error =
(...skipping 7354 matching lines...) Expand 10 before | Expand all | Expand 10 after
7429 NewString("main"), 7759 NewString("main"),
7430 1, 7760 1,
7431 dart_args); 7761 dart_args);
7432 int64_t value = 0; 7762 int64_t value = 0;
7433 result = Dart_IntegerToInt64(result, &value); 7763 result = Dart_IntegerToInt64(result, &value);
7434 EXPECT_VALID(result); 7764 EXPECT_VALID(result);
7435 EXPECT_EQ(6, value); 7765 EXPECT_EQ(6, value);
7436 } 7766 }
7437 7767
7438 } // namespace dart 7768 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/include/dart_debugger_api.h ('k') | runtime/vm/debugger.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698