-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdebugger_api_impl_test.cc
263 lines (236 loc) · 10.2 KB
/
debugger_api_impl_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <include/dart_api.h>
#include "include/dart_tools_api.h"
#include "vm/class_finalizer.h"
#include "vm/compiler/jit/compiler.h"
#include "vm/dart_api_impl.h"
#include "vm/dart_api_state.h"
#include "vm/debugger.h"
#include "vm/debugger_api_impl_test.h"
#include "vm/isolate.h"
#include "vm/object_store.h"
#include "vm/symbols.h"
namespace dart {
// Facilitate quick access to the current zone once we have the current thread.
#define Z (T->zone())
#ifndef PRODUCT
#define UNWRAP_AND_CHECK_PARAM(type, var, param) \
type& var = type::Handle(); \
do { \
const Object& tmp = Object::Handle(Api::UnwrapHandle(param)); \
if (tmp.IsNull()) { \
return Api::NewError("%s expects argument '%s' to be non-null.", \
CURRENT_FUNC, #param); \
} else if (tmp.IsApiError()) { \
return param; \
} else if (!tmp.Is##type()) { \
return Api::NewError("%s expects argument '%s' to be of type %s.", \
CURRENT_FUNC, #param, #type); \
} \
var ^= tmp.ptr(); \
} while (0)
#define CHECK_AND_CAST(type, var, param) \
type* var = nullptr; \
do { \
if (param == nullptr) { \
return Api::NewError("%s expects argument '%s' to be non-null.", \
CURRENT_FUNC, #param); \
} \
var = reinterpret_cast<type*>(param); \
} while (0)
#define CHECK_NOT_NULL(param) \
if (param == nullptr) { \
return Api::NewError("%s expects argument '%s' to be non-null.", \
CURRENT_FUNC, #param); \
}
#define CHECK_DEBUGGER(isolate) \
if (isolate->debugger() == nullptr) { \
return Api::NewError("%s requires debugger support.", CURRENT_FUNC); \
}
Dart_Handle Dart_StackTraceLength(Dart_StackTrace trace, intptr_t* length) {
DARTSCOPE(Thread::Current());
CHECK_NOT_NULL(length);
CHECK_AND_CAST(DebuggerStackTrace, stack_trace, trace);
*length = stack_trace->Length();
return Api::Success();
}
Dart_Handle Dart_GetActivationFrame(Dart_StackTrace trace,
int frame_index,
Dart_ActivationFrame* frame) {
DARTSCOPE(Thread::Current());
CHECK_NOT_NULL(frame);
CHECK_AND_CAST(DebuggerStackTrace, stack_trace, trace);
if ((frame_index < 0) || (frame_index >= stack_trace->Length())) {
return Api::NewError("argument 'frame_index' is out of range for %s",
CURRENT_FUNC);
}
*frame =
reinterpret_cast<Dart_ActivationFrame>(stack_trace->FrameAt(frame_index));
return Api::Success();
}
Dart_Handle Dart_GetStackTrace(Dart_StackTrace* trace) {
DARTSCOPE(Thread::Current());
Isolate* I = T->isolate();
CHECK_DEBUGGER(I);
CHECK_NOT_NULL(trace);
*trace = reinterpret_cast<Dart_StackTrace>(DebuggerStackTrace::Collect());
return Api::Success();
}
Dart_Handle Dart_GetStackTraceFromError(Dart_Handle handle,
Dart_StackTrace* trace) {
DARTSCOPE(Thread::Current());
CHECK_DEBUGGER(T->isolate());
CHECK_NOT_NULL(trace);
const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
if (obj.IsUnhandledException()) {
const UnhandledException& error = UnhandledException::Cast(obj);
StackTrace& dart_stacktrace = StackTrace::Handle(Z);
dart_stacktrace ^= error.stacktrace();
if (dart_stacktrace.IsNull()) {
*trace = nullptr;
} else {
*trace = reinterpret_cast<Dart_StackTrace>(
DebuggerStackTrace::From(dart_stacktrace));
}
return Api::Success();
} else {
return Api::NewError(
"Can only get stacktraces from error handles or "
"instances of Error.");
}
}
Dart_Handle Dart_ActivationFrameInfo(Dart_ActivationFrame activation_frame,
Dart_Handle* function_name,
Dart_Handle* script_url,
intptr_t* line_number,
intptr_t* column_number) {
DARTSCOPE(Thread::Current());
CHECK_AND_CAST(ActivationFrame, frame, activation_frame);
if (function_name != nullptr) {
*function_name = Api::NewHandle(T, frame->QualifiedFunctionName());
}
if (script_url != nullptr) {
*script_url = Api::NewHandle(T, frame->SourceUrl());
}
if (line_number != nullptr) {
*line_number = frame->LineNumber();
}
if (column_number != nullptr) {
*column_number = frame->ColumnNumber();
}
return Api::Success();
}
Dart_Handle Dart_SetBreakpoint(Dart_Handle script_url_in,
intptr_t line_number) {
Breakpoint* bpt;
{
DARTSCOPE(Thread::Current());
Isolate* I = T->isolate();
CHECK_DEBUGGER(I);
UNWRAP_AND_CHECK_PARAM(String, script_url, script_url_in);
Debugger* debugger = I->debugger();
const Error& error = Error::Handle(
debugger->SetBreakpointAtLineCol(script_url, line_number, -1, &bpt));
if (!error.IsNull() || bpt == nullptr) {
return Api::NewError("%s: could not set breakpoint at line %" Pd
" in '%s'",
CURRENT_FUNC, line_number, script_url.ToCString());
}
}
return Dart_NewInteger(bpt->id());
}
Dart_Handle Dart_RemoveBreakpoint(Dart_Handle breakpoint_id_in) {
DARTSCOPE(Thread::Current());
Isolate* I = T->isolate();
CHECK_DEBUGGER(I);
UNWRAP_AND_CHECK_PARAM(Integer, breakpoint_id, breakpoint_id_in);
I->debugger()->RemoveBreakpoint(breakpoint_id.Value());
return Api::Success();
}
Dart_Handle Dart_EvaluateStaticExpr(Dart_Handle lib_handle,
Dart_Handle expr_in) {
DARTSCOPE(Thread::Current());
CHECK_DEBUGGER(T->isolate());
const Object& target = Object::Handle(Z, Api::UnwrapHandle(lib_handle));
if (target.IsError()) return lib_handle;
if (target.IsNull()) {
return Api::NewError("%s expects argument 'target' to be non-null",
CURRENT_FUNC);
}
const Library& lib = Library::Cast(target);
UNWRAP_AND_CHECK_PARAM(String, expr, expr_in);
if (!KernelIsolate::IsRunning()) {
UNREACHABLE();
} else {
Dart_KernelCompilationResult compilation_result =
KernelIsolate::CompileExpressionToKernel(
/* platform_kernel= */ nullptr, /* platform_kernel_size= */ 0,
expr.ToCString(),
/* definitions= */ Array::empty_array(),
/* definition_types= */ Array::empty_array(),
/* type_definitions= */ Array::empty_array(),
/* type_bounds= */ Array::empty_array(),
/* type_defaults= */ Array::empty_array(),
String::Handle(lib.url()).ToCString(),
/* klass= */ nullptr,
/* method= */ nullptr,
/* token_pos= */ TokenPosition::kNoSource,
/* script_uri= */ String::Handle(lib.url()).ToCString(),
/* is_static= */ true);
if (compilation_result.status != Dart_KernelCompilationStatus_Ok) {
return Api::NewError("Failed to compile expression.");
}
const ExternalTypedData& kernel_buffer =
ExternalTypedData::Handle(ExternalTypedData::NewFinalizeWithFree(
const_cast<uint8_t*>(compilation_result.kernel),
compilation_result.kernel_size));
Dart_Handle result = Api::NewHandle(
T,
lib.EvaluateCompiledExpression(kernel_buffer,
/* type_definitions= */
Array::empty_array(),
/* param_values= */
Array::empty_array(),
/* type_param_values= */
TypeArguments::null_type_arguments()));
return result;
}
}
Dart_Handle Dart_LibraryId(Dart_Handle library, intptr_t* library_id) {
DARTSCOPE(Thread::Current());
const Library& lib = Api::UnwrapLibraryHandle(Z, library);
if (lib.IsNull()) {
RETURN_TYPE_ERROR(Z, library, Library);
}
if (library_id == nullptr) {
RETURN_NULL_ERROR(library_id);
}
*library_id = lib.index();
return Api::Success();
}
Dart_Handle Dart_GetLibraryDebuggable(intptr_t library_id,
bool* is_debuggable) {
DARTSCOPE(Thread::Current());
CHECK_NOT_NULL(is_debuggable);
const Library& lib = Library::Handle(Library::GetLibrary(library_id));
if (lib.IsNull()) {
return Api::NewError("%s: %" Pd " is not a valid library id", CURRENT_FUNC,
library_id);
}
*is_debuggable = lib.IsDebuggable();
return Api::Success();
}
Dart_Handle Dart_SetLibraryDebuggable(intptr_t library_id, bool is_debuggable) {
DARTSCOPE(Thread::Current());
const Library& lib = Library::Handle(Z, Library::GetLibrary(library_id));
if (lib.IsNull()) {
return Api::NewError("%s: %" Pd " is not a valid library id", CURRENT_FUNC,
library_id);
}
lib.set_debuggable(is_debuggable);
return Api::Success();
}
#endif // !PRODUCT
} // namespace dart