OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "lib/stacktrace.h" | 5 #include "lib/stacktrace.h" |
6 #include "vm/bootstrap_natives.h" | 6 #include "vm/bootstrap_natives.h" |
| 7 #include "vm/debugger.h" |
7 #include "vm/exceptions.h" | 8 #include "vm/exceptions.h" |
| 9 #include "vm/native_entry.h" |
8 #include "vm/object_store.h" | 10 #include "vm/object_store.h" |
9 #include "vm/runtime_entry.h" | 11 #include "vm/runtime_entry.h" |
10 #include "vm/stack_frame.h" | 12 #include "vm/stack_frame.h" |
| 13 #include "vm/stack_trace.h" |
11 | 14 |
12 namespace dart { | 15 namespace dart { |
13 | 16 |
14 static void IterateFrames(const GrowableObjectArray& code_list, | 17 DECLARE_FLAG(bool, show_invisible_frames); |
15 const GrowableObjectArray& pc_offset_list, | 18 |
16 int skip_frames) { | 19 static RawStackTrace* CurrentSyncStackTrace(Thread* thread) { |
| 20 Zone* zone = thread->zone(); |
| 21 const Function& null_function = Function::ZoneHandle(zone); |
| 22 // Skip the Dart exit frame. |
| 23 const intptr_t skip_frames = 1; |
| 24 |
| 25 // Determine how big the stack trace is. |
| 26 const intptr_t stack_trace_length = |
| 27 StackTraceUtils::CountFrames(thread, skip_frames, null_function); |
| 28 |
| 29 // Allocate once. |
| 30 const Array& code_array = |
| 31 Array::ZoneHandle(zone, Array::New(stack_trace_length)); |
| 32 const Array& pc_offset_array = |
| 33 Array::ZoneHandle(zone, Array::New(stack_trace_length)); |
| 34 |
| 35 // Collect the frames. |
| 36 const intptr_t collected_frames_count = StackTraceUtils::CollectFrames( |
| 37 thread, code_array, pc_offset_array, 0, stack_trace_length, skip_frames); |
| 38 |
| 39 ASSERT(collected_frames_count == stack_trace_length); |
| 40 |
| 41 return StackTrace::New(code_array, pc_offset_array); |
| 42 } |
| 43 |
| 44 |
| 45 static RawStackTrace* CurrentStackTrace( |
| 46 Thread* thread, |
| 47 bool for_async_function, |
| 48 intptr_t skip_frames = 1, |
| 49 bool causal_async_stacks = FLAG_causal_async_stacks) { |
| 50 if (!causal_async_stacks) { |
| 51 // Return the synchronous stack trace. |
| 52 return CurrentSyncStackTrace(thread); |
| 53 } |
| 54 |
| 55 Zone* zone = thread->zone(); |
| 56 Code& code = Code::ZoneHandle(zone); |
| 57 Smi& offset = Smi::ZoneHandle(zone); |
| 58 Function& async_function = Function::ZoneHandle(zone); |
| 59 StackTrace& async_stack_trace = StackTrace::ZoneHandle(zone); |
| 60 Array& async_code_array = Array::ZoneHandle(zone); |
| 61 Array& async_pc_offset_array = Array::ZoneHandle(zone); |
| 62 |
| 63 StackTraceUtils::ExtractAsyncStackTraceInfo( |
| 64 thread, &async_function, &async_stack_trace, &async_code_array, |
| 65 &async_pc_offset_array); |
| 66 |
| 67 // Determine the size of the stack trace. |
| 68 const intptr_t extra_frames = for_async_function ? 1 : 0; |
| 69 const intptr_t synchronous_stack_trace_length = |
| 70 StackTraceUtils::CountFrames(thread, skip_frames, async_function); |
| 71 |
| 72 const intptr_t capacity = synchronous_stack_trace_length + |
| 73 extra_frames; // For the asynchronous gap. |
| 74 |
| 75 // Allocate memory for the stack trace. |
| 76 const Array& code_array = Array::ZoneHandle(zone, Array::New(capacity)); |
| 77 const Array& pc_offset_array = Array::ZoneHandle(zone, Array::New(capacity)); |
| 78 |
| 79 intptr_t write_cursor = 0; |
| 80 if (for_async_function) { |
| 81 // Place the asynchronous gap marker at the top of the stack trace. |
| 82 code = StubCode::AsynchronousGapMarker_entry()->code(); |
| 83 ASSERT(!code.IsNull()); |
| 84 offset = Smi::New(0); |
| 85 code_array.SetAt(write_cursor, code); |
| 86 pc_offset_array.SetAt(write_cursor, offset); |
| 87 write_cursor++; |
| 88 } |
| 89 |
| 90 // Append the synchronous stack trace. |
| 91 const intptr_t collected_frames_count = StackTraceUtils::CollectFrames( |
| 92 thread, code_array, pc_offset_array, write_cursor, |
| 93 synchronous_stack_trace_length, skip_frames); |
| 94 |
| 95 write_cursor += collected_frames_count; |
| 96 |
| 97 ASSERT(write_cursor == capacity); |
| 98 |
| 99 return StackTrace::New(code_array, pc_offset_array, async_stack_trace); |
| 100 } |
| 101 |
| 102 |
| 103 RawStackTrace* GetStackTraceForException() { |
| 104 Thread* thread = Thread::Current(); |
| 105 return CurrentStackTrace(thread, false, 0); |
| 106 } |
| 107 |
| 108 |
| 109 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) { |
| 110 return CurrentStackTrace(thread, false); |
| 111 } |
| 112 |
| 113 |
| 114 DEFINE_NATIVE_ENTRY(StackTrace_asyncStackTraceHelper, 0) { |
| 115 return CurrentStackTrace(thread, true); |
| 116 } |
| 117 |
| 118 |
| 119 DEFINE_NATIVE_ENTRY(StackTrace_clearAsyncThreadStackTrace, 0) { |
| 120 thread->clear_async_stack_trace(); |
| 121 return Object::null(); |
| 122 } |
| 123 |
| 124 |
| 125 DEFINE_NATIVE_ENTRY(StackTrace_setAsyncThreadStackTrace, 1) { |
| 126 GET_NON_NULL_NATIVE_ARGUMENT(StackTrace, stack_trace, |
| 127 arguments->NativeArgAt(0)); |
| 128 thread->set_async_stack_trace(stack_trace); |
| 129 return Object::null(); |
| 130 } |
| 131 |
| 132 |
| 133 static void AppendFrames(const GrowableObjectArray& code_list, |
| 134 const GrowableObjectArray& pc_offset_list, |
| 135 int skip_frames) { |
17 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); | 136 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); |
18 StackFrame* frame = frames.NextFrame(); | 137 StackFrame* frame = frames.NextFrame(); |
19 ASSERT(frame != NULL); // We expect to find a dart invocation frame. | 138 ASSERT(frame != NULL); // We expect to find a dart invocation frame. |
20 Code& code = Code::Handle(); | 139 Code& code = Code::Handle(); |
21 Smi& offset = Smi::Handle(); | 140 Smi& offset = Smi::Handle(); |
22 while (frame != NULL) { | 141 while (frame != NULL) { |
23 if (frame->IsDartFrame()) { | 142 if (frame->IsDartFrame()) { |
24 if (skip_frames > 0) { | 143 if (skip_frames > 0) { |
25 skip_frames--; | 144 skip_frames--; |
26 } else { | 145 } else { |
27 code = frame->LookupDartCode(); | 146 code = frame->LookupDartCode(); |
28 offset = Smi::New(frame->pc() - code.PayloadStart()); | 147 offset = Smi::New(frame->pc() - code.PayloadStart()); |
29 code_list.Add(code); | 148 code_list.Add(code); |
30 pc_offset_list.Add(offset); | 149 pc_offset_list.Add(offset); |
31 } | 150 } |
32 } | 151 } |
33 frame = frames.NextFrame(); | 152 frame = frames.NextFrame(); |
34 } | 153 } |
35 } | 154 } |
36 | 155 |
| 156 |
37 // Creates a StackTrace object from the current stack. | 157 // Creates a StackTrace object from the current stack. |
38 // | 158 // |
39 // Skips the first skip_frames Dart frames. | 159 // Skips the first skip_frames Dart frames. |
40 const StackTrace& GetCurrentStackTrace(int skip_frames) { | 160 const StackTrace& GetCurrentStackTrace(int skip_frames) { |
41 const GrowableObjectArray& code_list = | 161 const GrowableObjectArray& code_list = |
42 GrowableObjectArray::Handle(GrowableObjectArray::New()); | 162 GrowableObjectArray::Handle(GrowableObjectArray::New()); |
43 const GrowableObjectArray& pc_offset_list = | 163 const GrowableObjectArray& pc_offset_list = |
44 GrowableObjectArray::Handle(GrowableObjectArray::New()); | 164 GrowableObjectArray::Handle(GrowableObjectArray::New()); |
45 IterateFrames(code_list, pc_offset_list, skip_frames); | 165 AppendFrames(code_list, pc_offset_list, skip_frames); |
46 const Array& code_array = Array::Handle(Array::MakeArray(code_list)); | 166 const Array& code_array = Array::Handle(Array::MakeArray(code_list)); |
47 const Array& pc_offset_array = | 167 const Array& pc_offset_array = |
48 Array::Handle(Array::MakeArray(pc_offset_list)); | 168 Array::Handle(Array::MakeArray(pc_offset_list)); |
49 const StackTrace& stacktrace = | 169 const StackTrace& stacktrace = |
50 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array)); | 170 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array)); |
51 return stacktrace; | 171 return stacktrace; |
52 } | 172 } |
53 | 173 |
| 174 |
54 // An utility method for convenient printing of dart stack traces when | 175 // An utility method for convenient printing of dart stack traces when |
55 // inside 'gdb'. Note: This function will only work when there is a | 176 // inside 'gdb'. Note: This function will only work when there is a |
56 // valid exit frame information. It will not work when a breakpoint is | 177 // valid exit frame information. It will not work when a breakpoint is |
57 // set in dart code and control is got inside 'gdb' without going through | 178 // set in dart code and control is got inside 'gdb' without going through |
58 // the runtime or native transition stub. | 179 // the runtime or native transition stub. |
59 void _printCurrentStackTrace() { | 180 void _printCurrentStackTrace() { |
60 const StackTrace& stacktrace = GetCurrentStackTrace(0); | 181 const StackTrace& stacktrace = GetCurrentStackTrace(0); |
61 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString()); | 182 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString()); |
62 } | 183 } |
63 | 184 |
| 185 |
64 // Like _printCurrentStackTrace, but works in a NoSafepointScope. | 186 // Like _printCurrentStackTrace, but works in a NoSafepointScope. |
65 void _printCurrentStackTraceNoSafepoint() { | 187 void _printCurrentStackTraceNoSafepoint() { |
66 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); | 188 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); |
67 StackFrame* frame = frames.NextFrame(); | 189 StackFrame* frame = frames.NextFrame(); |
68 while (frame != NULL) { | 190 while (frame != NULL) { |
69 OS::Print("%s\n", frame->ToCString()); | 191 OS::PrintErr("%s\n", frame->ToCString()); |
70 frame = frames.NextFrame(); | 192 frame = frames.NextFrame(); |
71 } | 193 } |
72 } | 194 } |
73 | 195 |
74 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) { | |
75 const StackTrace& stacktrace = GetCurrentStackTrace(1); | |
76 return stacktrace.raw(); | |
77 } | |
78 | |
79 } // namespace dart | 196 } // namespace dart |
OLD | NEW |