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

Side by Side Diff: runtime/lib/stacktrace.cc

Issue 2603383004: Sane asynchronous debugging and stack traces (Closed)
Patch Set: rebase Created 3 years, 11 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
« no previous file with comments | « runtime/lib/async_patch.dart ('k') | runtime/observatory/lib/src/elements/debugger.dart » ('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) 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 DEFINE_FLAG(int,
16 int skip_frames) { 19 max_async_stack_trace_frames,
20 96,
21 "Maximum number of asynchronous stack traces frames remembered in "
22 "an asynchronous function activation");
23
24 static RawStackTrace* CurrentSyncStackTrace(Thread* thread, Isolate* isolate) {
25 const Function& null_function = Function::Handle();
26 const intptr_t skip_frames = 1;
27
28 // Determine how big the stack trace is.
29 const intptr_t stack_trace_length =
30 StackTraceUtils::CountFrames(skip_frames, null_function);
31
32 // Allocate once.
33 const Array& code_array = Array::Handle(Array::New(stack_trace_length));
34 const Array& pc_offset_array = Array::Handle(Array::New(stack_trace_length));
35
36 // Collect the frames.
37 const intptr_t collected_frames_count = StackTraceUtils::CollectFrames(
38 code_array, pc_offset_array, 0, stack_trace_length, skip_frames);
39
40 ASSERT(collected_frames_count == stack_trace_length);
41
42 return StackTrace::New(code_array, pc_offset_array);
43 }
44
45
46 static RawStackTrace* CurrentStackTrace(
47 Thread* thread,
48 Isolate* isolate,
49 bool for_async_function,
50 bool sane_async_stacks = FLAG_sane_async_stacks) {
51 if (!sane_async_stacks) {
52 // Return the synchronous stack trace.
53 return CurrentSyncStackTrace(thread, isolate);
54 }
55
56 Code& code = Code::Handle();
57 Smi& offset = Smi::Handle();
58 Function& async_function = Function::Handle();
59 Array& async_code_array = Array::Handle();
60 Array& async_pc_offset_array = Array::Handle();
61
62 const intptr_t async_stack_trace_length =
63 StackTraceUtils::ExtractAsyncStackTraceInfo(
64 thread, &async_function, &async_code_array, &async_pc_offset_array);
65
66 // Determine the size of the stack trace.
67 const intptr_t extra_frames = for_async_function ? 1 : 0;
68 const intptr_t skip_frames = 1;
69 const intptr_t synchronous_stack_trace_length =
70 StackTraceUtils::CountFrames(skip_frames, async_function);
71
72 const intptr_t complete_length = async_stack_trace_length +
73 synchronous_stack_trace_length +
74 extra_frames; // For the asynchronous gap.
75
76 // We only truncate the stack trace when collecting for asynchronous
77 // stack traces.
78 const bool truncate_stack_trace =
79 for_async_function || (async_stack_trace_length > 0);
80
81 const intptr_t capacity =
82 (!truncate_stack_trace ||
83 (FLAG_max_async_stack_trace_frames > complete_length))
84 ? complete_length
85 : FLAG_max_async_stack_trace_frames;
86
87 // Allocate memory for the stack trace.
88 const Array& code_array = Array::Handle(Array::New(capacity));
89 const Array& pc_offset_array = Array::Handle(Array::New(capacity));
90
91 intptr_t write_cursor = 0;
92 if (for_async_function) {
93 // Place the asynchronous gap marker at the top of the stack trace.
94 code = StubCode::AsynchronousGapMarker_entry()->code();
95 ASSERT(!code.IsNull());
96 offset = Smi::New(0);
97 code_array.SetAt(write_cursor, code);
98 pc_offset_array.SetAt(write_cursor, offset);
99 write_cursor++;
100 }
101
102 const intptr_t sync_frames_to_collect =
103 (synchronous_stack_trace_length < (capacity - write_cursor))
104 ? synchronous_stack_trace_length
105 : capacity - write_cursor;
106
107 // Append the synchronous stack trace.
108 const intptr_t collected_frames_count =
109 StackTraceUtils::CollectFrames(code_array, pc_offset_array, write_cursor,
110 sync_frames_to_collect, skip_frames);
111
112 write_cursor += collected_frames_count;
113
114 // Append the asynchronous stack trace.
115 if (async_stack_trace_length > 0) {
116 ASSERT(!async_code_array.IsNull());
117 ASSERT(!async_pc_offset_array.IsNull());
118 for (intptr_t i = 0; i < async_stack_trace_length; i++) {
119 if (write_cursor == capacity) {
120 break;
121 }
122 if (async_code_array.At(i) == Code::null()) {
123 break;
124 }
125 code = Code::RawCast(async_code_array.At(i));
126 offset = Smi::RawCast(async_pc_offset_array.At(i));
127 code_array.SetAt(write_cursor, code);
128 pc_offset_array.SetAt(write_cursor, offset);
129 write_cursor++;
130 }
131 }
132
133 return StackTrace::New(code_array, pc_offset_array);
134 }
135
136
137 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) {
138 return CurrentStackTrace(thread, isolate, false);
139 }
140
141
142 DEFINE_NATIVE_ENTRY(StackTrace_forAsyncMethod, 1) {
143 GET_NON_NULL_NATIVE_ARGUMENT(Closure, async_op, arguments->NativeArgAt(0));
144 Debugger* debugger = isolate->debugger();
145 if ((debugger != NULL) && debugger->IsSingleStepping()) {
146 // This native entry is only ever called from the async/async* entry
147 // function. If we are single stepping at this point, treat it as a request
148 // to single step into the async body.
149 debugger->AsyncStepInto(async_op);
150 }
151 return CurrentStackTrace(thread, isolate, true);
152 }
153
154
155 DEFINE_NATIVE_ENTRY(AsyncStarMoveNext_debuggerStepCheck, 1) {
156 GET_NON_NULL_NATIVE_ARGUMENT(Closure, async_op, arguments->NativeArgAt(0));
157 Debugger* debugger = isolate->debugger();
158 OS::PrintErr("*************************** 1\n");
159 if ((debugger != NULL) && debugger->IsSingleStepping()) {
160 OS::PrintErr("*************************** 2\n");
161 // This native entry is only ever called from the async/async* entry
162 // function. If we are single stepping at this point, treat it as a request
163 // to single step into the async body.
164 debugger->AsyncStepInto(async_op);
165 }
166 OS::PrintErr("*************************** 3\n");
167 return Object::null();
168 }
169
170
171 DEFINE_NATIVE_ENTRY(StackTrace_clearAsyncThreadStackTrace, 0) {
172 thread->clear_async_stack_trace();
173 return Object::null();
174 }
175
176
177 DEFINE_NATIVE_ENTRY(StackTrace_setAsyncThreadStackTrace, 1) {
178 GET_NON_NULL_NATIVE_ARGUMENT(StackTrace, stack_trace,
179 arguments->NativeArgAt(0));
180 thread->set_async_stack_trace(stack_trace);
181 return Object::null();
182 }
183
184
185 static void AppendFrames(const GrowableObjectArray& code_list,
186 const GrowableObjectArray& pc_offset_list,
187 int skip_frames) {
17 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 188 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
18 StackFrame* frame = frames.NextFrame(); 189 StackFrame* frame = frames.NextFrame();
19 ASSERT(frame != NULL); // We expect to find a dart invocation frame. 190 ASSERT(frame != NULL); // We expect to find a dart invocation frame.
20 Code& code = Code::Handle(); 191 Code& code = Code::Handle();
21 Smi& offset = Smi::Handle(); 192 Smi& offset = Smi::Handle();
22 while (frame != NULL) { 193 while (frame != NULL) {
23 if (frame->IsDartFrame()) { 194 if (frame->IsDartFrame()) {
24 if (skip_frames > 0) { 195 if (skip_frames > 0) {
25 skip_frames--; 196 skip_frames--;
26 } else { 197 } else {
27 code = frame->LookupDartCode(); 198 code = frame->LookupDartCode();
28 offset = Smi::New(frame->pc() - code.PayloadStart()); 199 offset = Smi::New(frame->pc() - code.PayloadStart());
29 code_list.Add(code); 200 code_list.Add(code);
30 pc_offset_list.Add(offset); 201 pc_offset_list.Add(offset);
31 } 202 }
32 } 203 }
33 frame = frames.NextFrame(); 204 frame = frames.NextFrame();
34 } 205 }
35 } 206 }
36 207
208
37 // Creates a StackTrace object from the current stack. 209 // Creates a StackTrace object from the current stack.
38 // 210 //
39 // Skips the first skip_frames Dart frames. 211 // Skips the first skip_frames Dart frames.
40 const StackTrace& GetCurrentStackTrace(int skip_frames) { 212 const StackTrace& GetCurrentStackTrace(int skip_frames) {
41 const GrowableObjectArray& code_list = 213 const GrowableObjectArray& code_list =
42 GrowableObjectArray::Handle(GrowableObjectArray::New()); 214 GrowableObjectArray::Handle(GrowableObjectArray::New());
43 const GrowableObjectArray& pc_offset_list = 215 const GrowableObjectArray& pc_offset_list =
44 GrowableObjectArray::Handle(GrowableObjectArray::New()); 216 GrowableObjectArray::Handle(GrowableObjectArray::New());
45 IterateFrames(code_list, pc_offset_list, skip_frames); 217 AppendFrames(code_list, pc_offset_list, skip_frames);
46 const Array& code_array = Array::Handle(Array::MakeArray(code_list)); 218 const Array& code_array = Array::Handle(Array::MakeArray(code_list));
47 const Array& pc_offset_array = 219 const Array& pc_offset_array =
48 Array::Handle(Array::MakeArray(pc_offset_list)); 220 Array::Handle(Array::MakeArray(pc_offset_list));
49 const StackTrace& stacktrace = 221 const StackTrace& stacktrace =
50 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array)); 222 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array));
51 return stacktrace; 223 return stacktrace;
52 } 224 }
53 225
226
54 // An utility method for convenient printing of dart stack traces when 227 // An utility method for convenient printing of dart stack traces when
55 // inside 'gdb'. Note: This function will only work when there is a 228 // 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 229 // 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 230 // set in dart code and control is got inside 'gdb' without going through
58 // the runtime or native transition stub. 231 // the runtime or native transition stub.
59 void _printCurrentStackTrace() { 232 void _printCurrentStackTrace() {
60 const StackTrace& stacktrace = GetCurrentStackTrace(0); 233 const StackTrace& stacktrace = GetCurrentStackTrace(0);
61 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString()); 234 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString());
62 } 235 }
63 236
237
64 // Like _printCurrentStackTrace, but works in a NoSafepointScope. 238 // Like _printCurrentStackTrace, but works in a NoSafepointScope.
65 void _printCurrentStackTraceNoSafepoint() { 239 void _printCurrentStackTraceNoSafepoint() {
66 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 240 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
67 StackFrame* frame = frames.NextFrame(); 241 StackFrame* frame = frames.NextFrame();
68 while (frame != NULL) { 242 while (frame != NULL) {
69 OS::Print("%s\n", frame->ToCString()); 243 OS::PrintErr("%s\n", frame->ToCString());
70 frame = frames.NextFrame(); 244 frame = frames.NextFrame();
71 } 245 }
72 } 246 }
73 247
74 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) {
75 const StackTrace& stacktrace = GetCurrentStackTrace(1);
76 return stacktrace.raw();
77 }
78
79 } // namespace dart 248 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/async_patch.dart ('k') | runtime/observatory/lib/src/elements/debugger.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698