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

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

Issue 2646443005: Track async causal stack traces (Closed)
Patch Set: Observatory UI support 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
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) {
rmacnak 2017/01/26 18:05:57 isolate parameter not used
siva 2017/01/26 19:26:23 Thread is available here, you could extract the zo
Cutch 2017/01/31 23:45:30 Done.
Cutch 2017/01/31 23:45:30 Done.
25 const Function& null_function = Function::Handle();
26 const intptr_t skip_frames = 1;
siva 2017/01/26 19:26:22 maybe document which frame is skipped
Cutch 2017/01/31 23:45:30 Done.
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);
siva 2017/01/26 19:26:23 Do you think there stack traces are going to be li
Cutch 2017/01/31 23:45:30 Acknowledged.
43 }
44
45
46 static RawStackTrace* CurrentStackTrace(
47 Thread* thread,
48 Isolate* isolate,
rmacnak 2017/01/26 18:05:57 isolate parameter not used
Cutch 2017/01/31 23:45:30 Done.
49 bool for_async_function,
50 intptr_t skip_frames = 1,
51 bool sane_async_stacks = FLAG_sane_async_stacks) {
52 if (!sane_async_stacks) {
53 // Return the synchronous stack trace.
54 return CurrentSyncStackTrace(thread, isolate);
55 }
56
57 Code& code = Code::Handle();
rmacnak 2017/01/26 18:05:57 Handle(zone)
Cutch 2017/01/31 23:45:30 Done (here and elsewhere).
58 Smi& offset = Smi::Handle();
59 Function& async_function = Function::Handle();
60 Array& async_code_array = Array::Handle();
61 Array& async_pc_offset_array = Array::Handle();
62
63 const intptr_t async_stack_trace_length =
64 StackTraceUtils::ExtractAsyncStackTraceInfo(
65 thread, &async_function, &async_code_array, &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(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));
rmacnak 2017/01/26 18:05:57 code ^= async_code_array.At(i)
Cutch 2017/01/31 23:45:30 Done.
126 offset = Smi::RawCast(async_pc_offset_array.At(i));
rmacnak 2017/01/26 18:05:57 offset ^=
Cutch 2017/01/31 23:45:30 Done.
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 RawStackTrace* GetStackTraceForException() {
138 Thread* thread = Thread::Current();
139 Isolate* isolate = thread->isolate();
140 return CurrentStackTrace(thread, isolate, false, 0);
141 }
142
143
144 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) {
145 return CurrentStackTrace(thread, isolate, false);
146 }
147
148
149 DEFINE_NATIVE_ENTRY(StackTrace_forAsyncMethod, 0) {
150 return CurrentStackTrace(thread, isolate, true);
151 }
152
153
154 DEFINE_NATIVE_ENTRY(StackTrace_clearAsyncThreadStackTrace, 0) {
155 thread->clear_async_stack_trace();
156 return Object::null();
157 }
158
159
160 DEFINE_NATIVE_ENTRY(StackTrace_setAsyncThreadStackTrace, 1) {
161 GET_NON_NULL_NATIVE_ARGUMENT(StackTrace, stack_trace,
162 arguments->NativeArgAt(0));
163 thread->set_async_stack_trace(stack_trace);
164 return Object::null();
165 }
166
167
168 static void AppendFrames(const GrowableObjectArray& code_list,
169 const GrowableObjectArray& pc_offset_list,
170 int skip_frames) {
17 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 171 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
18 StackFrame* frame = frames.NextFrame(); 172 StackFrame* frame = frames.NextFrame();
19 ASSERT(frame != NULL); // We expect to find a dart invocation frame. 173 ASSERT(frame != NULL); // We expect to find a dart invocation frame.
20 Code& code = Code::Handle(); 174 Code& code = Code::Handle();
21 Smi& offset = Smi::Handle(); 175 Smi& offset = Smi::Handle();
22 while (frame != NULL) { 176 while (frame != NULL) {
23 if (frame->IsDartFrame()) { 177 if (frame->IsDartFrame()) {
24 if (skip_frames > 0) { 178 if (skip_frames > 0) {
25 skip_frames--; 179 skip_frames--;
26 } else { 180 } else {
27 code = frame->LookupDartCode(); 181 code = frame->LookupDartCode();
28 offset = Smi::New(frame->pc() - code.PayloadStart()); 182 offset = Smi::New(frame->pc() - code.PayloadStart());
29 code_list.Add(code); 183 code_list.Add(code);
30 pc_offset_list.Add(offset); 184 pc_offset_list.Add(offset);
31 } 185 }
32 } 186 }
33 frame = frames.NextFrame(); 187 frame = frames.NextFrame();
34 } 188 }
35 } 189 }
36 190
191
37 // Creates a StackTrace object from the current stack. 192 // Creates a StackTrace object from the current stack.
38 // 193 //
39 // Skips the first skip_frames Dart frames. 194 // Skips the first skip_frames Dart frames.
40 const StackTrace& GetCurrentStackTrace(int skip_frames) { 195 const StackTrace& GetCurrentStackTrace(int skip_frames) {
41 const GrowableObjectArray& code_list = 196 const GrowableObjectArray& code_list =
42 GrowableObjectArray::Handle(GrowableObjectArray::New()); 197 GrowableObjectArray::Handle(GrowableObjectArray::New());
43 const GrowableObjectArray& pc_offset_list = 198 const GrowableObjectArray& pc_offset_list =
44 GrowableObjectArray::Handle(GrowableObjectArray::New()); 199 GrowableObjectArray::Handle(GrowableObjectArray::New());
45 IterateFrames(code_list, pc_offset_list, skip_frames); 200 AppendFrames(code_list, pc_offset_list, skip_frames);
46 const Array& code_array = Array::Handle(Array::MakeArray(code_list)); 201 const Array& code_array = Array::Handle(Array::MakeArray(code_list));
47 const Array& pc_offset_array = 202 const Array& pc_offset_array =
48 Array::Handle(Array::MakeArray(pc_offset_list)); 203 Array::Handle(Array::MakeArray(pc_offset_list));
49 const StackTrace& stacktrace = 204 const StackTrace& stacktrace =
50 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array)); 205 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array));
51 return stacktrace; 206 return stacktrace;
52 } 207 }
53 208
209
54 // An utility method for convenient printing of dart stack traces when 210 // An utility method for convenient printing of dart stack traces when
55 // inside 'gdb'. Note: This function will only work when there is a 211 // 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 212 // 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 213 // set in dart code and control is got inside 'gdb' without going through
58 // the runtime or native transition stub. 214 // the runtime or native transition stub.
59 void _printCurrentStackTrace() { 215 void _printCurrentStackTrace() {
60 const StackTrace& stacktrace = GetCurrentStackTrace(0); 216 const StackTrace& stacktrace = GetCurrentStackTrace(0);
61 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString()); 217 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString());
62 } 218 }
63 219
220
64 // Like _printCurrentStackTrace, but works in a NoSafepointScope. 221 // Like _printCurrentStackTrace, but works in a NoSafepointScope.
65 void _printCurrentStackTraceNoSafepoint() { 222 void _printCurrentStackTraceNoSafepoint() {
66 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 223 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
67 StackFrame* frame = frames.NextFrame(); 224 StackFrame* frame = frames.NextFrame();
68 while (frame != NULL) { 225 while (frame != NULL) {
69 OS::Print("%s\n", frame->ToCString()); 226 OS::PrintErr("%s\n", frame->ToCString());
70 frame = frames.NextFrame(); 227 frame = frames.NextFrame();
71 } 228 }
72 } 229 }
73 230
74 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) {
75 const StackTrace& stacktrace = GetCurrentStackTrace(1);
76 return stacktrace.raw();
77 }
78
79 } // namespace dart 231 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698