OLD | NEW |
| (Empty) |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/inspector/V8ConsoleMessage.h" | |
6 | |
7 #include "src/inspector/InspectedContext.h" | |
8 #include "src/inspector/StringUtil.h" | |
9 #include "src/inspector/V8ConsoleAgentImpl.h" | |
10 #include "src/inspector/V8InspectorImpl.h" | |
11 #include "src/inspector/V8InspectorSessionImpl.h" | |
12 #include "src/inspector/V8RuntimeAgentImpl.h" | |
13 #include "src/inspector/V8StackTraceImpl.h" | |
14 #include "src/inspector/protocol/Protocol.h" | |
15 | |
16 #include "include/v8-inspector.h" | |
17 | |
18 namespace v8_inspector { | |
19 | |
20 namespace { | |
21 | |
22 String16 consoleAPITypeValue(ConsoleAPIType type) { | |
23 switch (type) { | |
24 case ConsoleAPIType::kLog: | |
25 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log; | |
26 case ConsoleAPIType::kDebug: | |
27 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug; | |
28 case ConsoleAPIType::kInfo: | |
29 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Info; | |
30 case ConsoleAPIType::kError: | |
31 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Error; | |
32 case ConsoleAPIType::kWarning: | |
33 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Warning; | |
34 case ConsoleAPIType::kClear: | |
35 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Clear; | |
36 case ConsoleAPIType::kDir: | |
37 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dir; | |
38 case ConsoleAPIType::kDirXML: | |
39 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dirxml; | |
40 case ConsoleAPIType::kTable: | |
41 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Table; | |
42 case ConsoleAPIType::kTrace: | |
43 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Trace; | |
44 case ConsoleAPIType::kStartGroup: | |
45 return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroup; | |
46 case ConsoleAPIType::kStartGroupCollapsed: | |
47 return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroupCollapsed; | |
48 case ConsoleAPIType::kEndGroup: | |
49 return protocol::Runtime::ConsoleAPICalled::TypeEnum::EndGroup; | |
50 case ConsoleAPIType::kAssert: | |
51 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Assert; | |
52 case ConsoleAPIType::kTimeEnd: | |
53 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug; | |
54 case ConsoleAPIType::kCount: | |
55 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug; | |
56 } | |
57 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log; | |
58 } | |
59 | |
60 const unsigned maxConsoleMessageCount = 1000; | |
61 const unsigned maxArrayItemsLimit = 10000; | |
62 const unsigned maxStackDepthLimit = 32; | |
63 | |
64 class V8ValueStringBuilder { | |
65 public: | |
66 static String16 toString(v8::Local<v8::Value> value, | |
67 v8::Local<v8::Context> context) { | |
68 V8ValueStringBuilder builder(context); | |
69 if (!builder.append(value)) return String16(); | |
70 return builder.toString(); | |
71 } | |
72 | |
73 private: | |
74 enum { | |
75 IgnoreNull = 1 << 0, | |
76 IgnoreUndefined = 1 << 1, | |
77 }; | |
78 | |
79 V8ValueStringBuilder(v8::Local<v8::Context> context) | |
80 : m_arrayLimit(maxArrayItemsLimit), | |
81 m_isolate(context->GetIsolate()), | |
82 m_tryCatch(context->GetIsolate()), | |
83 m_context(context) {} | |
84 | |
85 bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) { | |
86 if (value.IsEmpty()) return true; | |
87 if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true; | |
88 if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true; | |
89 if (value->IsString()) return append(v8::Local<v8::String>::Cast(value)); | |
90 if (value->IsStringObject()) | |
91 return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf()); | |
92 if (value->IsSymbol()) return append(v8::Local<v8::Symbol>::Cast(value)); | |
93 if (value->IsSymbolObject()) | |
94 return append(v8::Local<v8::SymbolObject>::Cast(value)->ValueOf()); | |
95 if (value->IsNumberObject()) { | |
96 m_builder.append(String16::fromDoublePrecision6( | |
97 v8::Local<v8::NumberObject>::Cast(value)->ValueOf())); | |
98 return true; | |
99 } | |
100 if (value->IsBooleanObject()) { | |
101 m_builder.append(v8::Local<v8::BooleanObject>::Cast(value)->ValueOf() | |
102 ? "true" | |
103 : "false"); | |
104 return true; | |
105 } | |
106 if (value->IsArray()) return append(v8::Local<v8::Array>::Cast(value)); | |
107 if (value->IsProxy()) { | |
108 m_builder.append("[object Proxy]"); | |
109 return true; | |
110 } | |
111 if (value->IsObject() && !value->IsDate() && !value->IsFunction() && | |
112 !value->IsNativeError() && !value->IsRegExp()) { | |
113 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | |
114 v8::Local<v8::String> stringValue; | |
115 if (object->ObjectProtoToString(m_isolate->GetCurrentContext()) | |
116 .ToLocal(&stringValue)) | |
117 return append(stringValue); | |
118 } | |
119 v8::Local<v8::String> stringValue; | |
120 if (!value->ToString(m_isolate->GetCurrentContext()).ToLocal(&stringValue)) | |
121 return false; | |
122 return append(stringValue); | |
123 } | |
124 | |
125 bool append(v8::Local<v8::Array> array) { | |
126 for (const auto& it : m_visitedArrays) { | |
127 if (it == array) return true; | |
128 } | |
129 uint32_t length = array->Length(); | |
130 if (length > m_arrayLimit) return false; | |
131 if (m_visitedArrays.size() > maxStackDepthLimit) return false; | |
132 | |
133 bool result = true; | |
134 m_arrayLimit -= length; | |
135 m_visitedArrays.push_back(array); | |
136 for (uint32_t i = 0; i < length; ++i) { | |
137 if (i) m_builder.append(','); | |
138 v8::Local<v8::Value> value; | |
139 if (!array->Get(m_context, i).ToLocal(&value)) continue; | |
140 if (!append(value, IgnoreNull | IgnoreUndefined)) { | |
141 result = false; | |
142 break; | |
143 } | |
144 } | |
145 m_visitedArrays.pop_back(); | |
146 return result; | |
147 } | |
148 | |
149 bool append(v8::Local<v8::Symbol> symbol) { | |
150 m_builder.append("Symbol("); | |
151 bool result = append(symbol->Name(), IgnoreUndefined); | |
152 m_builder.append(')'); | |
153 return result; | |
154 } | |
155 | |
156 bool append(v8::Local<v8::String> string) { | |
157 if (m_tryCatch.HasCaught()) return false; | |
158 if (!string.IsEmpty()) m_builder.append(toProtocolString(string)); | |
159 return true; | |
160 } | |
161 | |
162 String16 toString() { | |
163 if (m_tryCatch.HasCaught()) return String16(); | |
164 return m_builder.toString(); | |
165 } | |
166 | |
167 uint32_t m_arrayLimit; | |
168 v8::Isolate* m_isolate; | |
169 String16Builder m_builder; | |
170 std::vector<v8::Local<v8::Array>> m_visitedArrays; | |
171 v8::TryCatch m_tryCatch; | |
172 v8::Local<v8::Context> m_context; | |
173 }; | |
174 | |
175 } // namespace | |
176 | |
177 V8ConsoleMessage::V8ConsoleMessage(V8MessageOrigin origin, double timestamp, | |
178 const String16& message) | |
179 : m_origin(origin), | |
180 m_timestamp(timestamp), | |
181 m_message(message), | |
182 m_lineNumber(0), | |
183 m_columnNumber(0), | |
184 m_scriptId(0), | |
185 m_contextId(0), | |
186 m_type(ConsoleAPIType::kLog), | |
187 m_exceptionId(0), | |
188 m_revokedExceptionId(0) {} | |
189 | |
190 V8ConsoleMessage::~V8ConsoleMessage() {} | |
191 | |
192 void V8ConsoleMessage::setLocation(const String16& url, unsigned lineNumber, | |
193 unsigned columnNumber, | |
194 std::unique_ptr<V8StackTraceImpl> stackTrace, | |
195 int scriptId) { | |
196 m_url = url; | |
197 m_lineNumber = lineNumber; | |
198 m_columnNumber = columnNumber; | |
199 m_stackTrace = std::move(stackTrace); | |
200 m_scriptId = scriptId; | |
201 } | |
202 | |
203 void V8ConsoleMessage::reportToFrontend( | |
204 protocol::Console::Frontend* frontend) const { | |
205 DCHECK(m_origin == V8MessageOrigin::kConsole); | |
206 String16 level = protocol::Console::ConsoleMessage::LevelEnum::Log; | |
207 if (m_type == ConsoleAPIType::kDebug || m_type == ConsoleAPIType::kCount || | |
208 m_type == ConsoleAPIType::kTimeEnd) | |
209 level = protocol::Console::ConsoleMessage::LevelEnum::Debug; | |
210 else if (m_type == ConsoleAPIType::kError || | |
211 m_type == ConsoleAPIType::kAssert) | |
212 level = protocol::Console::ConsoleMessage::LevelEnum::Error; | |
213 else if (m_type == ConsoleAPIType::kWarning) | |
214 level = protocol::Console::ConsoleMessage::LevelEnum::Warning; | |
215 else if (m_type == ConsoleAPIType::kInfo) | |
216 level = protocol::Console::ConsoleMessage::LevelEnum::Info; | |
217 std::unique_ptr<protocol::Console::ConsoleMessage> result = | |
218 protocol::Console::ConsoleMessage::create() | |
219 .setSource(protocol::Console::ConsoleMessage::SourceEnum::ConsoleApi) | |
220 .setLevel(level) | |
221 .setText(m_message) | |
222 .build(); | |
223 result->setLine(static_cast<int>(m_lineNumber)); | |
224 result->setColumn(static_cast<int>(m_columnNumber)); | |
225 result->setUrl(m_url); | |
226 frontend->messageAdded(std::move(result)); | |
227 } | |
228 | |
229 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> | |
230 V8ConsoleMessage::wrapArguments(V8InspectorSessionImpl* session, | |
231 bool generatePreview) const { | |
232 if (!m_arguments.size() || !m_contextId) return nullptr; | |
233 InspectedContext* inspectedContext = | |
234 session->inspector()->getContext(session->contextGroupId(), m_contextId); | |
235 if (!inspectedContext) return nullptr; | |
236 | |
237 v8::Isolate* isolate = inspectedContext->isolate(); | |
238 v8::HandleScope handles(isolate); | |
239 v8::Local<v8::Context> context = inspectedContext->context(); | |
240 | |
241 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> args = | |
242 protocol::Array<protocol::Runtime::RemoteObject>::create(); | |
243 if (m_type == ConsoleAPIType::kTable && generatePreview) { | |
244 v8::Local<v8::Value> table = m_arguments[0]->Get(isolate); | |
245 v8::Local<v8::Value> columns = m_arguments.size() > 1 | |
246 ? m_arguments[1]->Get(isolate) | |
247 : v8::Local<v8::Value>(); | |
248 std::unique_ptr<protocol::Runtime::RemoteObject> wrapped = | |
249 session->wrapTable(context, table, columns); | |
250 if (wrapped) | |
251 args->addItem(std::move(wrapped)); | |
252 else | |
253 args = nullptr; | |
254 } else { | |
255 for (size_t i = 0; i < m_arguments.size(); ++i) { | |
256 std::unique_ptr<protocol::Runtime::RemoteObject> wrapped = | |
257 session->wrapObject(context, m_arguments[i]->Get(isolate), "console", | |
258 generatePreview); | |
259 if (!wrapped) { | |
260 args = nullptr; | |
261 break; | |
262 } | |
263 args->addItem(std::move(wrapped)); | |
264 } | |
265 } | |
266 return args; | |
267 } | |
268 | |
269 void V8ConsoleMessage::reportToFrontend(protocol::Runtime::Frontend* frontend, | |
270 V8InspectorSessionImpl* session, | |
271 bool generatePreview) const { | |
272 if (m_origin == V8MessageOrigin::kException) { | |
273 std::unique_ptr<protocol::Runtime::RemoteObject> exception = | |
274 wrapException(session, generatePreview); | |
275 std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails = | |
276 protocol::Runtime::ExceptionDetails::create() | |
277 .setExceptionId(m_exceptionId) | |
278 .setText(exception ? m_message : m_detailedMessage) | |
279 .setLineNumber(m_lineNumber ? m_lineNumber - 1 : 0) | |
280 .setColumnNumber(m_columnNumber ? m_columnNumber - 1 : 0) | |
281 .build(); | |
282 if (m_scriptId) | |
283 exceptionDetails->setScriptId(String16::fromInteger(m_scriptId)); | |
284 if (!m_url.isEmpty()) exceptionDetails->setUrl(m_url); | |
285 if (m_stackTrace) | |
286 exceptionDetails->setStackTrace(m_stackTrace->buildInspectorObjectImpl()); | |
287 if (m_contextId) exceptionDetails->setExecutionContextId(m_contextId); | |
288 if (exception) exceptionDetails->setException(std::move(exception)); | |
289 frontend->exceptionThrown(m_timestamp, std::move(exceptionDetails)); | |
290 return; | |
291 } | |
292 if (m_origin == V8MessageOrigin::kRevokedException) { | |
293 frontend->exceptionRevoked(m_message, m_revokedExceptionId); | |
294 return; | |
295 } | |
296 if (m_origin == V8MessageOrigin::kConsole) { | |
297 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> | |
298 arguments = wrapArguments(session, generatePreview); | |
299 if (!arguments) { | |
300 arguments = protocol::Array<protocol::Runtime::RemoteObject>::create(); | |
301 if (!m_message.isEmpty()) { | |
302 std::unique_ptr<protocol::Runtime::RemoteObject> messageArg = | |
303 protocol::Runtime::RemoteObject::create() | |
304 .setType(protocol::Runtime::RemoteObject::TypeEnum::String) | |
305 .build(); | |
306 messageArg->setValue(protocol::StringValue::create(m_message)); | |
307 arguments->addItem(std::move(messageArg)); | |
308 } | |
309 } | |
310 frontend->consoleAPICalled( | |
311 consoleAPITypeValue(m_type), std::move(arguments), m_contextId, | |
312 m_timestamp, | |
313 m_stackTrace ? m_stackTrace->buildInspectorObjectImpl() : nullptr); | |
314 return; | |
315 } | |
316 UNREACHABLE(); | |
317 } | |
318 | |
319 std::unique_ptr<protocol::Runtime::RemoteObject> | |
320 V8ConsoleMessage::wrapException(V8InspectorSessionImpl* session, | |
321 bool generatePreview) const { | |
322 if (!m_arguments.size() || !m_contextId) return nullptr; | |
323 DCHECK_EQ(1u, m_arguments.size()); | |
324 InspectedContext* inspectedContext = | |
325 session->inspector()->getContext(session->contextGroupId(), m_contextId); | |
326 if (!inspectedContext) return nullptr; | |
327 | |
328 v8::Isolate* isolate = inspectedContext->isolate(); | |
329 v8::HandleScope handles(isolate); | |
330 // TODO(dgozman): should we use different object group? | |
331 return session->wrapObject(inspectedContext->context(), | |
332 m_arguments[0]->Get(isolate), "console", | |
333 generatePreview); | |
334 } | |
335 | |
336 V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; } | |
337 | |
338 ConsoleAPIType V8ConsoleMessage::type() const { return m_type; } | |
339 | |
340 // static | |
341 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI( | |
342 double timestamp, ConsoleAPIType type, | |
343 const std::vector<v8::Local<v8::Value>>& arguments, | |
344 std::unique_ptr<V8StackTraceImpl> stackTrace, | |
345 InspectedContext* inspectedContext) { | |
346 v8::Isolate* isolate = inspectedContext->isolate(); | |
347 int contextId = inspectedContext->contextId(); | |
348 int contextGroupId = inspectedContext->contextGroupId(); | |
349 V8InspectorImpl* inspector = inspectedContext->inspector(); | |
350 v8::Local<v8::Context> context = inspectedContext->context(); | |
351 | |
352 std::unique_ptr<V8ConsoleMessage> message = wrapUnique( | |
353 new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16())); | |
354 if (stackTrace && !stackTrace->isEmpty()) { | |
355 message->m_url = toString16(stackTrace->topSourceURL()); | |
356 message->m_lineNumber = stackTrace->topLineNumber(); | |
357 message->m_columnNumber = stackTrace->topColumnNumber(); | |
358 } | |
359 message->m_stackTrace = std::move(stackTrace); | |
360 message->m_type = type; | |
361 message->m_contextId = contextId; | |
362 for (size_t i = 0; i < arguments.size(); ++i) | |
363 message->m_arguments.push_back( | |
364 wrapUnique(new v8::Global<v8::Value>(isolate, arguments.at(i)))); | |
365 if (arguments.size()) | |
366 message->m_message = V8ValueStringBuilder::toString(arguments[0], context); | |
367 | |
368 V8ConsoleAPIType clientType = V8ConsoleAPIType::kLog; | |
369 if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount || | |
370 type == ConsoleAPIType::kTimeEnd) | |
371 clientType = V8ConsoleAPIType::kDebug; | |
372 else if (type == ConsoleAPIType::kError || type == ConsoleAPIType::kAssert) | |
373 clientType = V8ConsoleAPIType::kError; | |
374 else if (type == ConsoleAPIType::kWarning) | |
375 clientType = V8ConsoleAPIType::kWarning; | |
376 else if (type == ConsoleAPIType::kInfo) | |
377 clientType = V8ConsoleAPIType::kInfo; | |
378 else if (type == ConsoleAPIType::kClear) | |
379 clientType = V8ConsoleAPIType::kClear; | |
380 inspector->client()->consoleAPIMessage( | |
381 contextGroupId, clientType, toStringView(message->m_message), | |
382 toStringView(message->m_url), message->m_lineNumber, | |
383 message->m_columnNumber, message->m_stackTrace.get()); | |
384 | |
385 return message; | |
386 } | |
387 | |
388 // static | |
389 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForException( | |
390 double timestamp, const String16& detailedMessage, const String16& url, | |
391 unsigned lineNumber, unsigned columnNumber, | |
392 std::unique_ptr<V8StackTraceImpl> stackTrace, int scriptId, | |
393 v8::Isolate* isolate, const String16& message, int contextId, | |
394 v8::Local<v8::Value> exception, unsigned exceptionId) { | |
395 std::unique_ptr<V8ConsoleMessage> consoleMessage = wrapUnique( | |
396 new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message)); | |
397 consoleMessage->setLocation(url, lineNumber, columnNumber, | |
398 std::move(stackTrace), scriptId); | |
399 consoleMessage->m_exceptionId = exceptionId; | |
400 consoleMessage->m_detailedMessage = detailedMessage; | |
401 if (contextId && !exception.IsEmpty()) { | |
402 consoleMessage->m_contextId = contextId; | |
403 consoleMessage->m_arguments.push_back( | |
404 wrapUnique(new v8::Global<v8::Value>(isolate, exception))); | |
405 } | |
406 return consoleMessage; | |
407 } | |
408 | |
409 // static | |
410 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException( | |
411 double timestamp, const String16& messageText, | |
412 unsigned revokedExceptionId) { | |
413 std::unique_ptr<V8ConsoleMessage> message = wrapUnique(new V8ConsoleMessage( | |
414 V8MessageOrigin::kRevokedException, timestamp, messageText)); | |
415 message->m_revokedExceptionId = revokedExceptionId; | |
416 return message; | |
417 } | |
418 | |
419 void V8ConsoleMessage::contextDestroyed(int contextId) { | |
420 if (contextId != m_contextId) return; | |
421 m_contextId = 0; | |
422 if (m_message.isEmpty()) m_message = "<message collected>"; | |
423 Arguments empty; | |
424 m_arguments.swap(empty); | |
425 } | |
426 | |
427 // ------------------------ V8ConsoleMessageStorage ---------------------------- | |
428 | |
429 V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector, | |
430 int contextGroupId) | |
431 : m_inspector(inspector), | |
432 m_contextGroupId(contextGroupId), | |
433 m_expiredCount(0) {} | |
434 | |
435 V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); } | |
436 | |
437 void V8ConsoleMessageStorage::addMessage( | |
438 std::unique_ptr<V8ConsoleMessage> message) { | |
439 int contextGroupId = m_contextGroupId; | |
440 V8InspectorImpl* inspector = m_inspector; | |
441 if (message->type() == ConsoleAPIType::kClear) clear(); | |
442 | |
443 V8InspectorSessionImpl* session = | |
444 inspector->sessionForContextGroup(contextGroupId); | |
445 if (session) { | |
446 if (message->origin() == V8MessageOrigin::kConsole) | |
447 session->consoleAgent()->messageAdded(message.get()); | |
448 session->runtimeAgent()->messageAdded(message.get()); | |
449 } | |
450 if (!inspector->hasConsoleMessageStorage(contextGroupId)) return; | |
451 | |
452 DCHECK(m_messages.size() <= maxConsoleMessageCount); | |
453 if (m_messages.size() == maxConsoleMessageCount) { | |
454 ++m_expiredCount; | |
455 m_messages.pop_front(); | |
456 } | |
457 m_messages.push_back(std::move(message)); | |
458 } | |
459 | |
460 void V8ConsoleMessageStorage::clear() { | |
461 m_messages.clear(); | |
462 m_expiredCount = 0; | |
463 if (V8InspectorSessionImpl* session = | |
464 m_inspector->sessionForContextGroup(m_contextGroupId)) | |
465 session->releaseObjectGroup("console"); | |
466 } | |
467 | |
468 void V8ConsoleMessageStorage::contextDestroyed(int contextId) { | |
469 for (size_t i = 0; i < m_messages.size(); ++i) | |
470 m_messages[i]->contextDestroyed(contextId); | |
471 } | |
472 | |
473 } // namespace v8_inspector | |
OLD | NEW |