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

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

Issue 2646443005: Track async causal stack traces (Closed)
Patch Set: Chain stack traces together 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
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");
siva 2017/02/08 18:46:28 These flags don't seem to be used anymore, can the
Cutch 2017/02/09 22:45:50 Removed.
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);
siva 2017/02/08 18:46:28 Pass thread into this function too?
Cutch 2017/02/09 22:45:50 Done.
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 StackTrace& async_stack_trace = StackTrace::ZoneHandle(zone);
65 Array& async_code_array = Array::ZoneHandle(zone);
66 Array& async_pc_offset_array = Array::ZoneHandle(zone);
67
68 StackTraceUtils::ExtractAsyncStackTraceInfo(
69 thread, &async_function, &async_stack_trace, &async_code_array,
70 &async_pc_offset_array);
71
72 // Determine the size of the stack trace.
73 const intptr_t extra_frames = for_async_function ? 1 : 0;
74 const intptr_t synchronous_stack_trace_length =
75 StackTraceUtils::CountFrames(skip_frames, async_function);
76
77 const intptr_t capacity = synchronous_stack_trace_length +
78 extra_frames; // For the asynchronous gap.
79
80 // Allocate memory for the stack trace.
81 const Array& code_array = Array::ZoneHandle(zone, Array::New(capacity));
82 const Array& pc_offset_array = Array::ZoneHandle(zone, Array::New(capacity));
83
84 intptr_t write_cursor = 0;
85 if (for_async_function) {
86 // Place the asynchronous gap marker at the top of the stack trace.
87 code = StubCode::AsynchronousGapMarker_entry()->code();
88 ASSERT(!code.IsNull());
89 offset = Smi::New(0);
90 code_array.SetAt(write_cursor, code);
91 pc_offset_array.SetAt(write_cursor, offset);
92 write_cursor++;
93 }
94
95 // Append the synchronous stack trace.
96 const intptr_t collected_frames_count = StackTraceUtils::CollectFrames(
97 thread, code_array, pc_offset_array, write_cursor,
98 synchronous_stack_trace_length, skip_frames);
99
100 write_cursor += collected_frames_count;
101
102 ASSERT(write_cursor == capacity);
103
104 return StackTrace::New(code_array, pc_offset_array, async_stack_trace);
105 }
106
107
108 RawStackTrace* GetStackTraceForException() {
109 Thread* thread = Thread::Current();
110 return CurrentStackTrace(thread, false, 0);
111 }
112
113
114 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) {
115 return CurrentStackTrace(thread, false);
116 }
117
118
119 DEFINE_NATIVE_ENTRY(StackTrace_asyncStackTraceHelper, 0) {
120 return CurrentStackTrace(thread, true);
121 }
122
123
124 DEFINE_NATIVE_ENTRY(StackTrace_clearAsyncThreadStackTrace, 0) {
125 thread->clear_async_stack_trace();
126 return Object::null();
127 }
128
129
130 DEFINE_NATIVE_ENTRY(StackTrace_setAsyncThreadStackTrace, 1) {
131 GET_NON_NULL_NATIVE_ARGUMENT(StackTrace, stack_trace,
132 arguments->NativeArgAt(0));
133 thread->set_async_stack_trace(stack_trace);
134 return Object::null();
135 }
136
137
138 static void AppendFrames(const GrowableObjectArray& code_list,
139 const GrowableObjectArray& pc_offset_list,
140 int skip_frames) {
17 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 141 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
18 StackFrame* frame = frames.NextFrame(); 142 StackFrame* frame = frames.NextFrame();
19 ASSERT(frame != NULL); // We expect to find a dart invocation frame. 143 ASSERT(frame != NULL); // We expect to find a dart invocation frame.
20 Code& code = Code::Handle(); 144 Code& code = Code::Handle();
21 Smi& offset = Smi::Handle(); 145 Smi& offset = Smi::Handle();
22 while (frame != NULL) { 146 while (frame != NULL) {
23 if (frame->IsDartFrame()) { 147 if (frame->IsDartFrame()) {
24 if (skip_frames > 0) { 148 if (skip_frames > 0) {
25 skip_frames--; 149 skip_frames--;
26 } else { 150 } else {
27 code = frame->LookupDartCode(); 151 code = frame->LookupDartCode();
28 offset = Smi::New(frame->pc() - code.PayloadStart()); 152 offset = Smi::New(frame->pc() - code.PayloadStart());
29 code_list.Add(code); 153 code_list.Add(code);
30 pc_offset_list.Add(offset); 154 pc_offset_list.Add(offset);
31 } 155 }
32 } 156 }
33 frame = frames.NextFrame(); 157 frame = frames.NextFrame();
34 } 158 }
35 } 159 }
36 160
161
37 // Creates a StackTrace object from the current stack. 162 // Creates a StackTrace object from the current stack.
38 // 163 //
39 // Skips the first skip_frames Dart frames. 164 // Skips the first skip_frames Dart frames.
40 const StackTrace& GetCurrentStackTrace(int skip_frames) { 165 const StackTrace& GetCurrentStackTrace(int skip_frames) {
41 const GrowableObjectArray& code_list = 166 const GrowableObjectArray& code_list =
42 GrowableObjectArray::Handle(GrowableObjectArray::New()); 167 GrowableObjectArray::Handle(GrowableObjectArray::New());
43 const GrowableObjectArray& pc_offset_list = 168 const GrowableObjectArray& pc_offset_list =
44 GrowableObjectArray::Handle(GrowableObjectArray::New()); 169 GrowableObjectArray::Handle(GrowableObjectArray::New());
45 IterateFrames(code_list, pc_offset_list, skip_frames); 170 AppendFrames(code_list, pc_offset_list, skip_frames);
46 const Array& code_array = Array::Handle(Array::MakeArray(code_list)); 171 const Array& code_array = Array::Handle(Array::MakeArray(code_list));
47 const Array& pc_offset_array = 172 const Array& pc_offset_array =
48 Array::Handle(Array::MakeArray(pc_offset_list)); 173 Array::Handle(Array::MakeArray(pc_offset_list));
49 const StackTrace& stacktrace = 174 const StackTrace& stacktrace =
50 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array)); 175 StackTrace::Handle(StackTrace::New(code_array, pc_offset_array));
51 return stacktrace; 176 return stacktrace;
52 } 177 }
53 178
179
54 // An utility method for convenient printing of dart stack traces when 180 // An utility method for convenient printing of dart stack traces when
55 // inside 'gdb'. Note: This function will only work when there is a 181 // 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 182 // 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 183 // set in dart code and control is got inside 'gdb' without going through
58 // the runtime or native transition stub. 184 // the runtime or native transition stub.
59 void _printCurrentStackTrace() { 185 void _printCurrentStackTrace() {
60 const StackTrace& stacktrace = GetCurrentStackTrace(0); 186 const StackTrace& stacktrace = GetCurrentStackTrace(0);
61 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString()); 187 OS::PrintErr("=== Current Trace:\n%s===\n", stacktrace.ToCString());
62 } 188 }
63 189
190
64 // Like _printCurrentStackTrace, but works in a NoSafepointScope. 191 // Like _printCurrentStackTrace, but works in a NoSafepointScope.
65 void _printCurrentStackTraceNoSafepoint() { 192 void _printCurrentStackTraceNoSafepoint() {
66 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames); 193 StackFrameIterator frames(StackFrameIterator::kDontValidateFrames);
67 StackFrame* frame = frames.NextFrame(); 194 StackFrame* frame = frames.NextFrame();
68 while (frame != NULL) { 195 while (frame != NULL) {
69 OS::Print("%s\n", frame->ToCString()); 196 OS::PrintErr("%s\n", frame->ToCString());
70 frame = frames.NextFrame(); 197 frame = frames.NextFrame();
71 } 198 }
72 } 199 }
73 200
74 DEFINE_NATIVE_ENTRY(StackTrace_current, 0) {
75 const StackTrace& stacktrace = GetCurrentStackTrace(1);
76 return stacktrace.raw();
77 }
78
79 } // namespace dart 201 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698