Repository navigation
feat(inspector): serve multiple DevTools clients at once #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,351
−797
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b08c9e
feat(inspector): serve multiple DevTools clients at once
edusperoni 109460d
fix(inspector): pause workers and busy JS, drain on the runtime's thread
edusperoni f86d605
fix(inspector): pause debug-brk apps before they run, for every client
edusperoni 0ff85ab
fix(inspector): address review findings
edusperoni c7f18bd
fix(inspector): debug-brk wait and stall check races
edusperoni a589d62
fix(inspector): check and release a debug-brk hold under one lock
edusperoni 2ff5a17
fix(inspector): count partial writes, keep the debug-brk timeout
edusperoni 95d5f45
fix(inspector): start a client's pending output and its timestamp tog…
edusperoni 3c35f19
fix(inspector): private deferred JS replies, and review findings
edusperoni fa584c4
fix(inspector): drop late replies to departed frontends, copy handler…
edusperoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #ifndef InspectorQueue_h | ||
| #define InspectorQueue_h | ||
|
|
||
| #include <dispatch/dispatch.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <deque> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace v8_inspector { | ||
|
|
||
| // Frontend messages waiting for an isolate, in arrival order. Push from any | ||
| // thread; take only while holding the isolate's Locker, so that a pause loop | ||
| // on another thread (which holds it throughout) and the isolate's own drain | ||
| // never split a frontend's messages between them. | ||
| class InspectorQueue { | ||
| public: | ||
| struct Item { | ||
| enum class Kind { kConnect, kDisconnect, kMessage }; | ||
| uint64_t connectionId; | ||
| Kind kind; | ||
| std::string method; | ||
| std::string message; | ||
| bool interrupting = false; // see Interrupts | ||
| }; | ||
|
|
||
| InspectorQueue(); | ||
| ~InspectorQueue(); | ||
| InspectorQueue(const InspectorQueue&) = delete; | ||
| InspectorQueue& operator=(const InspectorQueue&) = delete; | ||
|
|
||
| // Whether a command runs no JS, so it may be dispatched from an interrupt | ||
| // in the middle of the isolate's JS and ahead of the frontend's earlier | ||
| // commands. Chromium's list (ShouldInterruptForMethod), without the domains | ||
| // V8 does not implement and without Runtime.terminateExecution, which would | ||
| // unwind the app's own frames. | ||
| static bool Interrupts(const std::string& method); | ||
|
|
||
| static Item Message(uint64_t connectionId, const std::string& method, | ||
| std::string message); | ||
|
|
||
| void Push(Item item); | ||
|
|
||
| // Takes the oldest item. Returns false when there is none. | ||
| bool Take(Item& item); | ||
|
|
||
| // Takes the interrupting commands, and with them what they need of their | ||
| // frontend's earlier items: its connect, Debugger.enable and | ||
| // Debugger.setSkipAllPauses, so that a frontend can pause an isolate that | ||
| // was busy before it attached. Those run no JS either, but overtake the | ||
| // frontend's other commands only for that. | ||
| // Nothing is taken past a disconnect or Debugger.disable. | ||
| std::vector<Item> TakeInterrupting(); | ||
|
|
||
| // Waits briefly for a Push. | ||
| void Wait(); | ||
|
|
||
| // Ends a Wait in progress. | ||
| void Wake(); | ||
|
|
||
| private: | ||
| std::mutex mutex_; | ||
| std::deque<Item> items_; | ||
| dispatch_semaphore_t pushed_; | ||
| }; | ||
|
|
||
| } // namespace v8_inspector | ||
|
|
||
| #endif /* InspectorQueue_h */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #include "InspectorQueue.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <iterator> | ||
| #include <map> | ||
| #include <set> | ||
|
|
||
| namespace v8_inspector { | ||
|
|
||
| namespace { | ||
| bool IsPrerequisite(const InspectorQueue::Item& item) { | ||
| return item.kind == InspectorQueue::Item::Kind::kConnect || item.method == "Debugger.enable" || | ||
| item.method == "Debugger.setSkipAllPauses"; | ||
| } | ||
|
|
||
| bool IsBarrier(const InspectorQueue::Item& item) { | ||
| return item.kind == InspectorQueue::Item::Kind::kDisconnect || item.method == "Debugger.disable"; | ||
| } | ||
| } // namespace | ||
|
|
||
| InspectorQueue::InspectorQueue() : pushed_(dispatch_semaphore_create(0)) {} | ||
|
|
||
| InspectorQueue::~InspectorQueue() { dispatch_release(pushed_); } | ||
|
|
||
| bool InspectorQueue::Interrupts(const std::string& method) { | ||
| static constexpr const char* kMethods[] = { | ||
| "Debugger.getPossibleBreakpoints", "Debugger.getScriptSource", | ||
| "Debugger.getStackTrace", "Debugger.pause", | ||
| "Debugger.removeBreakpoint", "Debugger.resume", | ||
| "Debugger.setBreakpoint", "Debugger.setBreakpointByUrl", | ||
| "Debugger.setBreakpointsActive", | ||
| }; | ||
| return std::any_of(std::begin(kMethods), std::end(kMethods), | ||
| [&](const char* name) { return method == name; }); | ||
| } | ||
|
|
||
| InspectorQueue::Item InspectorQueue::Message(uint64_t connectionId, const std::string& method, | ||
| std::string message) { | ||
| return {connectionId, Item::Kind::kMessage, method, std::move(message), Interrupts(method)}; | ||
| } | ||
|
|
||
| void InspectorQueue::Push(Item item) { | ||
| { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| items_.push_back(std::move(item)); | ||
| } | ||
| dispatch_semaphore_signal(pushed_); | ||
| } | ||
|
|
||
| bool InspectorQueue::Take(Item& item) { | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
| if (items_.empty()) { | ||
| return false; | ||
| } | ||
| item = std::move(items_.front()); | ||
| items_.pop_front(); | ||
| return true; | ||
| } | ||
|
|
||
| std::vector<InspectorQueue::Item> InspectorQueue::TakeInterrupting() { | ||
| std::vector<Item> taken; | ||
| std::lock_guard<std::mutex> lock(mutex_); | ||
|
|
||
| // The index after each frontend's last command to take. | ||
| std::map<uint64_t, size_t> ends; | ||
| std::set<uint64_t> blocked; | ||
| for (size_t i = 0; i < items_.size(); i++) { | ||
| const Item& item = items_[i]; | ||
| if (IsBarrier(item)) { | ||
| blocked.insert(item.connectionId); | ||
| } else if (item.interrupting && blocked.count(item.connectionId) == 0) { | ||
| ends[item.connectionId] = i + 1; | ||
| } | ||
| } | ||
|
|
||
| size_t index = 0; | ||
| for (auto it = items_.begin(); it != items_.end(); index++) { | ||
| auto end = ends.find(it->connectionId); | ||
| if (end != ends.end() && index < end->second && (it->interrupting || IsPrerequisite(*it))) { | ||
| taken.push_back(std::move(*it)); | ||
| it = items_.erase(it); | ||
| } else { | ||
| ++it; | ||
| } | ||
| } | ||
| return taken; | ||
| } | ||
|
|
||
| void InspectorQueue::Wait() { | ||
| dispatch_semaphore_wait(pushed_, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_MSEC)); | ||
| } | ||
|
|
||
| void InspectorQueue::Wake() { dispatch_semaphore_signal(pushed_); } | ||
|
|
||
| } // namespace v8_inspector | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.