OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/inspector/v8-console-message.h" | 5 #include "src/inspector/v8-console-message.h" |
6 | 6 |
| 7 #include "src/debug/debug-interface.h" |
7 #include "src/inspector/inspected-context.h" | 8 #include "src/inspector/inspected-context.h" |
8 #include "src/inspector/protocol/Protocol.h" | 9 #include "src/inspector/protocol/Protocol.h" |
9 #include "src/inspector/string-util.h" | 10 #include "src/inspector/string-util.h" |
10 #include "src/inspector/v8-console-agent-impl.h" | 11 #include "src/inspector/v8-console-agent-impl.h" |
11 #include "src/inspector/v8-inspector-impl.h" | 12 #include "src/inspector/v8-inspector-impl.h" |
12 #include "src/inspector/v8-inspector-session-impl.h" | 13 #include "src/inspector/v8-inspector-session-impl.h" |
13 #include "src/inspector/v8-runtime-agent-impl.h" | 14 #include "src/inspector/v8-runtime-agent-impl.h" |
14 #include "src/inspector/v8-stack-trace-impl.h" | 15 #include "src/inspector/v8-stack-trace-impl.h" |
15 | 16 |
16 #include "include/v8-inspector.h" | 17 #include "include/v8-inspector.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Assert; | 52 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Assert; |
52 case ConsoleAPIType::kTimeEnd: | 53 case ConsoleAPIType::kTimeEnd: |
53 return protocol::Runtime::ConsoleAPICalled::TypeEnum::TimeEnd; | 54 return protocol::Runtime::ConsoleAPICalled::TypeEnum::TimeEnd; |
54 case ConsoleAPIType::kCount: | 55 case ConsoleAPIType::kCount: |
55 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Count; | 56 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Count; |
56 } | 57 } |
57 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log; | 58 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log; |
58 } | 59 } |
59 | 60 |
60 const unsigned maxConsoleMessageCount = 1000; | 61 const unsigned maxConsoleMessageCount = 1000; |
| 62 const int maxConsoleMessageV8Size = 10 * 1024 * 1024; |
61 const unsigned maxArrayItemsLimit = 10000; | 63 const unsigned maxArrayItemsLimit = 10000; |
62 const unsigned maxStackDepthLimit = 32; | 64 const unsigned maxStackDepthLimit = 32; |
63 | 65 |
64 class V8ValueStringBuilder { | 66 class V8ValueStringBuilder { |
65 public: | 67 public: |
66 static String16 toString(v8::Local<v8::Value> value, | 68 static String16 toString(v8::Local<v8::Value> value, |
67 v8::Local<v8::Context> context) { | 69 v8::Local<v8::Context> context) { |
68 V8ValueStringBuilder builder(context); | 70 V8ValueStringBuilder builder(context); |
69 if (!builder.append(value)) return String16(); | 71 if (!builder.append(value)) return String16(); |
70 return builder.toString(); | 72 return builder.toString(); |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 std::unique_ptr<V8ConsoleMessage> message( | 366 std::unique_ptr<V8ConsoleMessage> message( |
365 new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16())); | 367 new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16())); |
366 if (stackTrace && !stackTrace->isEmpty()) { | 368 if (stackTrace && !stackTrace->isEmpty()) { |
367 message->m_url = toString16(stackTrace->topSourceURL()); | 369 message->m_url = toString16(stackTrace->topSourceURL()); |
368 message->m_lineNumber = stackTrace->topLineNumber(); | 370 message->m_lineNumber = stackTrace->topLineNumber(); |
369 message->m_columnNumber = stackTrace->topColumnNumber(); | 371 message->m_columnNumber = stackTrace->topColumnNumber(); |
370 } | 372 } |
371 message->m_stackTrace = std::move(stackTrace); | 373 message->m_stackTrace = std::move(stackTrace); |
372 message->m_type = type; | 374 message->m_type = type; |
373 message->m_contextId = contextId; | 375 message->m_contextId = contextId; |
374 for (size_t i = 0; i < arguments.size(); ++i) | 376 for (size_t i = 0; i < arguments.size(); ++i) { |
375 message->m_arguments.push_back(std::unique_ptr<v8::Global<v8::Value>>( | 377 message->m_arguments.push_back(std::unique_ptr<v8::Global<v8::Value>>( |
376 new v8::Global<v8::Value>(isolate, arguments.at(i)))); | 378 new v8::Global<v8::Value>(isolate, arguments.at(i)))); |
| 379 message->m_v8Size += |
| 380 v8::debug::EstimatedValueSize(isolate, arguments.at(i)); |
| 381 } |
377 if (arguments.size()) | 382 if (arguments.size()) |
378 message->m_message = V8ValueStringBuilder::toString(arguments[0], context); | 383 message->m_message = V8ValueStringBuilder::toString(arguments[0], context); |
379 | 384 |
380 v8::Isolate::MessageErrorLevel clientLevel = v8::Isolate::kMessageInfo; | 385 v8::Isolate::MessageErrorLevel clientLevel = v8::Isolate::kMessageInfo; |
381 if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount || | 386 if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount || |
382 type == ConsoleAPIType::kTimeEnd) { | 387 type == ConsoleAPIType::kTimeEnd) { |
383 clientLevel = v8::Isolate::kMessageDebug; | 388 clientLevel = v8::Isolate::kMessageDebug; |
384 } else if (type == ConsoleAPIType::kError || | 389 } else if (type == ConsoleAPIType::kError || |
385 type == ConsoleAPIType::kAssert) { | 390 type == ConsoleAPIType::kAssert) { |
386 clientLevel = v8::Isolate::kMessageError; | 391 clientLevel = v8::Isolate::kMessageError; |
(...skipping 24 matching lines...) Expand all Loading... |
411 new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message)); | 416 new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message)); |
412 consoleMessage->setLocation(url, lineNumber, columnNumber, | 417 consoleMessage->setLocation(url, lineNumber, columnNumber, |
413 std::move(stackTrace), scriptId); | 418 std::move(stackTrace), scriptId); |
414 consoleMessage->m_exceptionId = exceptionId; | 419 consoleMessage->m_exceptionId = exceptionId; |
415 consoleMessage->m_detailedMessage = detailedMessage; | 420 consoleMessage->m_detailedMessage = detailedMessage; |
416 if (contextId && !exception.IsEmpty()) { | 421 if (contextId && !exception.IsEmpty()) { |
417 consoleMessage->m_contextId = contextId; | 422 consoleMessage->m_contextId = contextId; |
418 consoleMessage->m_arguments.push_back( | 423 consoleMessage->m_arguments.push_back( |
419 std::unique_ptr<v8::Global<v8::Value>>( | 424 std::unique_ptr<v8::Global<v8::Value>>( |
420 new v8::Global<v8::Value>(isolate, exception))); | 425 new v8::Global<v8::Value>(isolate, exception))); |
| 426 consoleMessage->m_v8Size += |
| 427 v8::debug::EstimatedValueSize(isolate, exception); |
421 } | 428 } |
422 return consoleMessage; | 429 return consoleMessage; |
423 } | 430 } |
424 | 431 |
425 // static | 432 // static |
426 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException( | 433 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException( |
427 double timestamp, const String16& messageText, | 434 double timestamp, const String16& messageText, |
428 unsigned revokedExceptionId) { | 435 unsigned revokedExceptionId) { |
429 std::unique_ptr<V8ConsoleMessage> message(new V8ConsoleMessage( | 436 std::unique_ptr<V8ConsoleMessage> message(new V8ConsoleMessage( |
430 V8MessageOrigin::kRevokedException, timestamp, messageText)); | 437 V8MessageOrigin::kRevokedException, timestamp, messageText)); |
431 message->m_revokedExceptionId = revokedExceptionId; | 438 message->m_revokedExceptionId = revokedExceptionId; |
432 return message; | 439 return message; |
433 } | 440 } |
434 | 441 |
435 void V8ConsoleMessage::contextDestroyed(int contextId) { | 442 void V8ConsoleMessage::contextDestroyed(int contextId) { |
436 if (contextId != m_contextId) return; | 443 if (contextId != m_contextId) return; |
437 m_contextId = 0; | 444 m_contextId = 0; |
438 if (m_message.isEmpty()) m_message = "<message collected>"; | 445 if (m_message.isEmpty()) m_message = "<message collected>"; |
439 Arguments empty; | 446 Arguments empty; |
440 m_arguments.swap(empty); | 447 m_arguments.swap(empty); |
| 448 m_v8Size = 0; |
441 } | 449 } |
442 | 450 |
443 // ------------------------ V8ConsoleMessageStorage ---------------------------- | 451 // ------------------------ V8ConsoleMessageStorage ---------------------------- |
444 | 452 |
445 V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector, | 453 V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector, |
446 int contextGroupId) | 454 int contextGroupId) |
447 : m_inspector(inspector), | 455 : m_inspector(inspector), m_contextGroupId(contextGroupId) {} |
448 m_contextGroupId(contextGroupId), | |
449 m_expiredCount(0) {} | |
450 | 456 |
451 V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); } | 457 V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); } |
452 | 458 |
453 void V8ConsoleMessageStorage::addMessage( | 459 void V8ConsoleMessageStorage::addMessage( |
454 std::unique_ptr<V8ConsoleMessage> message) { | 460 std::unique_ptr<V8ConsoleMessage> message) { |
455 int contextGroupId = m_contextGroupId; | 461 int contextGroupId = m_contextGroupId; |
456 V8InspectorImpl* inspector = m_inspector; | 462 V8InspectorImpl* inspector = m_inspector; |
457 if (message->type() == ConsoleAPIType::kClear) clear(); | 463 if (message->type() == ConsoleAPIType::kClear) clear(); |
458 | 464 |
459 V8InspectorSessionImpl* session = | 465 V8InspectorSessionImpl* session = |
460 inspector->sessionForContextGroup(contextGroupId); | 466 inspector->sessionForContextGroup(contextGroupId); |
461 if (session) { | 467 if (session) { |
462 if (message->origin() == V8MessageOrigin::kConsole) | 468 if (message->origin() == V8MessageOrigin::kConsole) |
463 session->consoleAgent()->messageAdded(message.get()); | 469 session->consoleAgent()->messageAdded(message.get()); |
464 session->runtimeAgent()->messageAdded(message.get()); | 470 session->runtimeAgent()->messageAdded(message.get()); |
465 } | 471 } |
466 if (!inspector->hasConsoleMessageStorage(contextGroupId)) return; | 472 if (!inspector->hasConsoleMessageStorage(contextGroupId)) return; |
467 | 473 |
468 DCHECK(m_messages.size() <= maxConsoleMessageCount); | 474 DCHECK(m_messages.size() <= maxConsoleMessageCount); |
469 if (m_messages.size() == maxConsoleMessageCount) { | 475 if (m_messages.size() == maxConsoleMessageCount) { |
470 ++m_expiredCount; | 476 m_estimatedSize -= m_messages.front()->estimatedSize(); |
471 m_messages.pop_front(); | 477 m_messages.pop_front(); |
472 } | 478 } |
| 479 while (m_estimatedSize + message->estimatedSize() > maxConsoleMessageV8Size && |
| 480 !m_messages.empty()) { |
| 481 m_estimatedSize -= m_messages.front()->estimatedSize(); |
| 482 m_messages.pop_front(); |
| 483 } |
| 484 |
473 m_messages.push_back(std::move(message)); | 485 m_messages.push_back(std::move(message)); |
| 486 m_estimatedSize += m_messages.back()->estimatedSize(); |
474 } | 487 } |
475 | 488 |
476 void V8ConsoleMessageStorage::clear() { | 489 void V8ConsoleMessageStorage::clear() { |
477 m_messages.clear(); | 490 m_messages.clear(); |
478 m_expiredCount = 0; | 491 m_estimatedSize = 0; |
479 if (V8InspectorSessionImpl* session = | 492 if (V8InspectorSessionImpl* session = |
480 m_inspector->sessionForContextGroup(m_contextGroupId)) | 493 m_inspector->sessionForContextGroup(m_contextGroupId)) |
481 session->releaseObjectGroup("console"); | 494 session->releaseObjectGroup("console"); |
482 } | 495 } |
483 | 496 |
484 void V8ConsoleMessageStorage::contextDestroyed(int contextId) { | 497 void V8ConsoleMessageStorage::contextDestroyed(int contextId) { |
485 for (size_t i = 0; i < m_messages.size(); ++i) | 498 m_estimatedSize = 0; |
| 499 for (size_t i = 0; i < m_messages.size(); ++i) { |
486 m_messages[i]->contextDestroyed(contextId); | 500 m_messages[i]->contextDestroyed(contextId); |
| 501 m_estimatedSize += m_messages[i]->estimatedSize(); |
| 502 } |
487 } | 503 } |
488 | 504 |
489 } // namespace v8_inspector | 505 } // namespace v8_inspector |
OLD | NEW |