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

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

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

Powered by Google App Engine
This is Rietveld 408576698