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

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

Issue 12320103: Fix for bug 6767 - Limit stack trace collection for stack overflow exceptions. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 10 months 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 | « no previous file | runtime/vm/object.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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "vm/exceptions.h" 5 #include "vm/exceptions.h"
6 6
7 #include "vm/dart_api_impl.h" 7 #include "vm/dart_api_impl.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/debugger.h" 9 #include "vm/debugger.h"
10 #include "vm/flags.h" 10 #include "vm/flags.h"
11 #include "vm/object.h" 11 #include "vm/object.h"
12 #include "vm/object_store.h" 12 #include "vm/object_store.h"
13 #include "vm/stack_frame.h" 13 #include "vm/stack_frame.h"
14 #include "vm/stub_code.h" 14 #include "vm/stub_code.h"
15 #include "vm/symbols.h" 15 #include "vm/symbols.h"
16 16
17 namespace dart { 17 namespace dart {
18 18
19 DEFINE_FLAG(bool, print_stacktrace_at_throw, false, 19 DEFINE_FLAG(bool, print_stacktrace_at_throw, false,
20 "Prints a stack trace everytime a throw occurs."); 20 "Prints a stack trace everytime a throw occurs.");
21 DEFINE_FLAG(bool, heap_profile_out_of_memory, false, 21 DEFINE_FLAG(bool, heap_profile_out_of_memory, false,
22 "Writes a heap profile on unhandled out-of-memory exceptions."); 22 "Writes a heap profile on unhandled out-of-memory exceptions.");
23 DEFINE_FLAG(bool, verbose_stacktrace, false, 23 DEFINE_FLAG(bool, verbose_stacktrace, false,
24 "Stack traces will include methods marked invisible."); 24 "Stack traces will include methods marked invisible.");
25 25
26 const char* Exceptions::kCastErrorDstName = "type cast"; 26 const char* Exceptions::kCastErrorDstName = "type cast";
27 27
28 28
29 class StacktraceBuilder : public ValueObject {
30 public:
31 StacktraceBuilder() { }
32 virtual ~StacktraceBuilder() { }
33
34 virtual void AddFrame(const Function& func,
35 const Code& code,
36 const Smi& offset) = 0;
37 };
38
39
40 class RegularStacktraceBuilder : public StacktraceBuilder {
41 public:
42 RegularStacktraceBuilder(const GrowableObjectArray& func_list,
43 const GrowableObjectArray& code_list,
44 const GrowableObjectArray& pc_offset_list)
45 : func_list_(func_list),
46 code_list_(code_list),
47 pc_offset_list_(pc_offset_list) { }
48 ~RegularStacktraceBuilder() { }
49
50 const GrowableObjectArray& func_list() const { return func_list_; }
51 const GrowableObjectArray& code_list() const { return code_list_; }
52 const GrowableObjectArray& pc_offset_list() const { return pc_offset_list_; }
53
54 void AddFrame(const Function& func, const Code& code, const Smi& offset) {
55 func_list_.Add(func);
56 code_list_.Add(code);
57 pc_offset_list_.Add(offset);
58 }
59
60 private:
61 const GrowableObjectArray& func_list_;
62 const GrowableObjectArray& code_list_;
63 const GrowableObjectArray& pc_offset_list_;
64
65 DISALLOW_COPY_AND_ASSIGN(RegularStacktraceBuilder);
66 };
67
68
69 class PreallocatedStacktraceBuilder : public StacktraceBuilder {
70 public:
71 explicit PreallocatedStacktraceBuilder(const Stacktrace& stacktrace)
72 : stacktrace_(stacktrace),
73 cur_index_(0) {
74 ASSERT(stacktrace_.raw() ==
75 Isolate::Current()->object_store()->preallocated_stack_trace());
76 }
77 ~PreallocatedStacktraceBuilder() { }
78
79 void AddFrame(const Function& func, const Code& code, const Smi& offset);
80
81 private:
82 static const int kNumTopframes = 3;
83
84 const Stacktrace& stacktrace_;
85 intptr_t cur_index_;
86
87 DISALLOW_COPY_AND_ASSIGN(PreallocatedStacktraceBuilder);
88 };
89
90
91 void PreallocatedStacktraceBuilder::AddFrame(const Function& func,
92 const Code& code,
93 const Smi& offset) {
94 if (cur_index_ >= Stacktrace::kPreallocatedStackdepth) {
95 // The number of frames is overflowing the preallocated stack trace object.
96 Function& frame_func = Function::Handle();
97 Code& frame_code = Code::Handle();
98 Smi& frame_offset = Smi::Handle();
99 intptr_t start = Stacktrace::kPreallocatedStackdepth - (kNumTopframes - 1);
100 intptr_t null_slot = start - 2;
101 // Add an empty slot to indicate the overflow so that the toString
102 // method can account for the overflow.
103 if (stacktrace_.FunctionAtFrame(null_slot) != Function::null()) {
104 stacktrace_.SetFunctionAtFrame(null_slot, frame_func);
105 stacktrace_.SetCodeAtFrame(null_slot, frame_code);
106 }
107 // Move frames one slot down so that we can accomadate the new frame.
108 for (intptr_t i = start; i < Stacktrace::kPreallocatedStackdepth; i++) {
109 intptr_t prev = (i - 1);
110 frame_func = stacktrace_.FunctionAtFrame(i);
111 frame_code = stacktrace_.CodeAtFrame(i);
112 frame_offset = stacktrace_.PcOffsetAtFrame(i);
113 stacktrace_.SetFunctionAtFrame(prev, frame_func);
114 stacktrace_.SetCodeAtFrame(prev, frame_code);
115 stacktrace_.SetPcOffsetAtFrame(prev, frame_offset);
116 }
117 cur_index_ = (Stacktrace::kPreallocatedStackdepth - 1);
118 }
119 stacktrace_.SetFunctionAtFrame(cur_index_, func);
120 stacktrace_.SetCodeAtFrame(cur_index_, code);
121 stacktrace_.SetPcOffsetAtFrame(cur_index_, offset);
122 cur_index_ += 1;
123 }
124
125
29 static bool ShouldShowFunction(const Function& function) { 126 static bool ShouldShowFunction(const Function& function) {
30 if (FLAG_verbose_stacktrace) { 127 if (FLAG_verbose_stacktrace) {
31 return true; 128 return true;
32 } 129 }
33 return function.is_visible(); 130 return function.is_visible();
34 } 131 }
35 132
36 133
37 // Iterate through the stack frames and try to find a frame with an 134 // Iterate through the stack frames and try to find a frame with an
38 // exception handler. Once found, set the pc, sp and fp so that execution 135 // exception handler. Once found, set the pc, sp and fp so that execution
39 // can continue in that frame. 136 // can continue in that frame.
40 static bool FindExceptionHandler(uword* handler_pc, 137 static bool FindExceptionHandler(uword* handler_pc,
41 uword* handler_sp, 138 uword* handler_sp,
42 uword* handler_fp, 139 uword* handler_fp,
43 const GrowableObjectArray& func_list, 140 StacktraceBuilder* builder) {
44 const GrowableObjectArray& code_list,
45 const GrowableObjectArray& pc_offset_list) {
46 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 141 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
47 StackFrame* frame = frames.NextFrame(); 142 StackFrame* frame = frames.NextFrame();
48 ASSERT(frame != NULL); // We expect to find a dart invocation frame. 143 ASSERT(frame != NULL); // We expect to find a dart invocation frame.
49 Function& func = Function::Handle(); 144 Function& func = Function::Handle();
50 Code& code = Code::Handle(); 145 Code& code = Code::Handle();
51 Smi& offset = Smi::Handle(); 146 Smi& offset = Smi::Handle();
52 while (!frame->IsEntryFrame()) { 147 while (!frame->IsEntryFrame()) {
53 if (frame->IsDartFrame()) { 148 if (frame->IsDartFrame()) {
54 code = frame->LookupDartCode(); 149 code = frame->LookupDartCode();
55 if (code.is_optimized()) { 150 if (code.is_optimized()) {
56 // For optimized frames, extract all the inlined functions if any 151 // For optimized frames, extract all the inlined functions if any
57 // into the stack trace. 152 // into the stack trace.
58 for (InlinedFunctionsIterator it(frame); !it.Done(); it.Advance()) { 153 for (InlinedFunctionsIterator it(frame); !it.Done(); it.Advance()) {
59 func = it.function(); 154 func = it.function();
60 code = it.code(); 155 code = it.code();
61 uword pc = it.pc(); 156 uword pc = it.pc();
62 ASSERT(pc != 0); 157 ASSERT(pc != 0);
63 ASSERT(code.EntryPoint() <= pc); 158 ASSERT(code.EntryPoint() <= pc);
64 ASSERT(pc < (code.EntryPoint() + code.Size())); 159 ASSERT(pc < (code.EntryPoint() + code.Size()));
65 if (ShouldShowFunction(func)) { 160 if (ShouldShowFunction(func)) {
66 offset = Smi::New(pc - code.EntryPoint()); 161 offset = Smi::New(pc - code.EntryPoint());
67 func_list.Add(func); 162 builder->AddFrame(func, code, offset);
68 code_list.Add(code);
69 pc_offset_list.Add(offset);
70 } 163 }
71 } 164 }
72 } else { 165 } else {
73 offset = Smi::New(frame->pc() - code.EntryPoint()); 166 offset = Smi::New(frame->pc() - code.EntryPoint());
74 func = code.function(); 167 func = code.function();
75 if (ShouldShowFunction(func)) { 168 if (ShouldShowFunction(func)) {
76 func_list.Add(func); 169 builder->AddFrame(func, code, offset);
77 code_list.Add(code);
78 pc_offset_list.Add(offset);
79 } 170 }
80 } 171 }
81 if (frame->FindExceptionHandler(handler_pc)) { 172 if (frame->FindExceptionHandler(handler_pc)) {
82 *handler_sp = frame->sp(); 173 *handler_sp = frame->sp();
83 *handler_fp = frame->fp(); 174 *handler_fp = frame->fp();
84 return true; 175 return true;
85 } 176 }
86 } 177 }
87 frame = frames.NextFrame(); 178 frame = frames.NextFrame();
88 ASSERT(frame != NULL); 179 ASSERT(frame != NULL);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 typedef void (*ErrorHandler)(uword, uword, uword, RawError*); 255 typedef void (*ErrorHandler)(uword, uword, uword, RawError*);
165 ErrorHandler func = reinterpret_cast<ErrorHandler>( 256 ErrorHandler func = reinterpret_cast<ErrorHandler>(
166 StubCode::JumpToErrorHandlerEntryPoint()); 257 StubCode::JumpToErrorHandlerEntryPoint());
167 func(program_counter, stack_pointer, frame_pointer, raw_error); 258 func(program_counter, stack_pointer, frame_pointer, raw_error);
168 UNREACHABLE(); 259 UNREACHABLE();
169 } 260 }
170 261
171 262
172 static void ThrowExceptionHelper(const Instance& incoming_exception, 263 static void ThrowExceptionHelper(const Instance& incoming_exception,
173 const Instance& existing_stacktrace) { 264 const Instance& existing_stacktrace) {
174 Instance& exception = Instance::Handle(incoming_exception.raw()); 265 bool use_preallocated_stacktrace = false;
266 Isolate* isolate = Isolate::Current();
267 Instance& exception = Instance::Handle(isolate, incoming_exception.raw());
175 if (exception.IsNull()) { 268 if (exception.IsNull()) {
176 exception ^= Exceptions::Create(Exceptions::kNullThrown, 269 exception ^= Exceptions::Create(Exceptions::kNullThrown,
177 Object::empty_array()); 270 Object::empty_array());
271 } else if (exception.raw() == isolate->object_store()->out_of_memory() ||
272 exception.raw() == isolate->object_store()->stack_overflow()) {
273 use_preallocated_stacktrace = true;
178 } 274 }
179 uword handler_pc = 0; 275 uword handler_pc = 0;
180 uword handler_sp = 0; 276 uword handler_sp = 0;
181 uword handler_fp = 0; 277 uword handler_fp = 0;
182 const GrowableObjectArray& func_list = 278 Stacktrace& stacktrace = Stacktrace::Handle(isolate);
183 GrowableObjectArray::Handle(GrowableObjectArray::New()); 279 bool handler_exists = false;
184 const GrowableObjectArray& code_list = 280 if (use_preallocated_stacktrace) {
185 GrowableObjectArray::Handle(GrowableObjectArray::New()); 281 stacktrace ^= isolate->object_store()->preallocated_stack_trace();
186 const GrowableObjectArray& pc_offset_list = 282 PreallocatedStacktraceBuilder frame_builder(stacktrace);
187 GrowableObjectArray::Handle(GrowableObjectArray::New()); 283 handler_exists = FindExceptionHandler(&handler_pc,
188 bool handler_exists = FindExceptionHandler(&handler_pc, 284 &handler_sp,
189 &handler_sp, 285 &handler_fp,
190 &handler_fp, 286 &frame_builder);
191 func_list, 287 } else {
192 code_list, 288 RegularStacktraceBuilder frame_builder(
193 pc_offset_list); 289 GrowableObjectArray::Handle(isolate, GrowableObjectArray::New()),
290 GrowableObjectArray::Handle(isolate, GrowableObjectArray::New()),
291 GrowableObjectArray::Handle(isolate, GrowableObjectArray::New()));
292 handler_exists = FindExceptionHandler(&handler_pc,
293 &handler_sp,
294 &handler_fp,
295 &frame_builder);
296 // TODO(5411263): At some point we can optimize by figuring out if a
297 // stack trace is needed based on whether the catch code specifies a
298 // stack trace object or there is a rethrow in the catch clause.
299 if (frame_builder.pc_offset_list().Length() != 0) {
300 // Create arrays for function, code and pc_offset triplet for each frame.
301 const Array& func_array =
302 Array::Handle(isolate, Array::MakeArray(frame_builder.func_list()));
303 const Array& code_array =
304 Array::Handle(isolate, Array::MakeArray(frame_builder.code_list()));
305 const Array& pc_offset_array =
306 Array::Handle(isolate,
307 Array::MakeArray(frame_builder.pc_offset_list()));
308 if (existing_stacktrace.IsNull()) {
309 stacktrace = Stacktrace::New(func_array, code_array, pc_offset_array);
310 } else {
311 stacktrace ^= existing_stacktrace.raw();
312 stacktrace.Append(func_array, code_array, pc_offset_array);
313 }
314 } else {
315 stacktrace ^= existing_stacktrace.raw();
316 }
317 }
194 // We expect to find a handler_pc, if the exception is unhandled 318 // We expect to find a handler_pc, if the exception is unhandled
195 // then we expect to at least have the dart entry frame on the 319 // then we expect to at least have the dart entry frame on the
196 // stack as Exceptions::Throw should happen only after a dart 320 // stack as Exceptions::Throw should happen only after a dart
197 // invocation has been done. 321 // invocation has been done.
198 ASSERT(handler_pc != 0); 322 ASSERT(handler_pc != 0);
199 323
200 // TODO(5411263): At some point we can optimize by figuring out if a
201 // stack trace is needed based on whether the catch code specifies a
202 // stack trace object or there is a rethrow in the catch clause.
203 Stacktrace& stacktrace = Stacktrace::Handle();
204 if (pc_offset_list.Length() != 0) {
205 if (existing_stacktrace.IsNull()) {
206 stacktrace = Stacktrace::New(func_list, code_list, pc_offset_list);
207 } else {
208 stacktrace ^= existing_stacktrace.raw();
209 stacktrace.Append(func_list, code_list, pc_offset_list);
210 }
211 } else {
212 stacktrace ^= existing_stacktrace.raw();
213 }
214 if (FLAG_print_stacktrace_at_throw) { 324 if (FLAG_print_stacktrace_at_throw) {
215 OS::Print("Exception '%s' thrown:\n", exception.ToCString()); 325 OS::Print("Exception '%s' thrown:\n", exception.ToCString());
216 OS::Print("%s\n", stacktrace.ToCString()); 326 OS::Print("%s\n", stacktrace.ToCString());
217 } 327 }
218 if (handler_exists) { 328 if (handler_exists) {
219 // Found a dart handler for the exception, jump to it. 329 // Found a dart handler for the exception, jump to it.
220 JumpToExceptionHandler(handler_pc, 330 JumpToExceptionHandler(handler_pc,
221 handler_sp, 331 handler_sp,
222 handler_fp, 332 handler_fp,
223 exception, 333 exception,
224 stacktrace); 334 stacktrace);
225 } else { 335 } else {
226 if (FLAG_heap_profile_out_of_memory) { 336 if (FLAG_heap_profile_out_of_memory) {
227 Isolate* isolate = Isolate::Current();
228 if (exception.raw() == isolate->object_store()->out_of_memory()) { 337 if (exception.raw() == isolate->object_store()->out_of_memory()) {
229 isolate->heap()->ProfileToFile("out-of-memory"); 338 isolate->heap()->ProfileToFile("out-of-memory");
230 } 339 }
231 } 340 }
232 // No dart exception handler found in this invocation sequence, 341 // No dart exception handler found in this invocation sequence,
233 // so we create an unhandled exception object and return to the 342 // so we create an unhandled exception object and return to the
234 // invocation stub so that it returns this unhandled exception 343 // invocation stub so that it returns this unhandled exception
235 // object. The C++ code which invoked this dart sequence can check 344 // object. The C++ code which invoked this dart sequence can check
236 // and do the appropriate thing (rethrow the exception to the 345 // and do the appropriate thing (rethrow the exception to the
237 // dart invocation sequence above it, print diagnostics and terminate 346 // dart invocation sequence above it, print diagnostics and terminate
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 break; 589 break;
481 } 590 }
482 591
483 return DartLibraryCalls::ExceptionCreate(library, 592 return DartLibraryCalls::ExceptionCreate(library,
484 *class_name, 593 *class_name,
485 *constructor_name, 594 *constructor_name,
486 arguments); 595 arguments);
487 } 596 }
488 597
489 } // namespace dart 598 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698