|
| 1 | +#include "base_object-inl.h" |
| 2 | +#include "debug_utils.h" |
| 3 | +#include "inspector_agent.h" |
| 4 | +#include "node_internals.h" |
| 5 | +#include "v8-inspector.h" |
| 6 | + |
| 7 | +namespace node { |
| 8 | +namespace profiler { |
| 9 | + |
| 10 | +using v8::Context; |
| 11 | +using v8::EscapableHandleScope; |
| 12 | +using v8::Function; |
| 13 | +using v8::FunctionCallbackInfo; |
| 14 | +using v8::HandleScope; |
| 15 | +using v8::Isolate; |
| 16 | +using v8::Local; |
| 17 | +using v8::MaybeLocal; |
| 18 | +using v8::NewStringType; |
| 19 | +using v8::Object; |
| 20 | +using v8::ObjectTemplate; |
| 21 | +using v8::String; |
| 22 | +using v8::Value; |
| 23 | + |
| 24 | +using v8_inspector::StringBuffer; |
| 25 | +using v8_inspector::StringView; |
| 26 | + |
| 27 | +std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate, |
| 28 | + Local<Value> value) { |
| 29 | + TwoByteValue buffer(isolate, value); |
| 30 | + return StringBuffer::create(StringView(*buffer, buffer.length())); |
| 31 | +} |
| 32 | + |
| 33 | +class V8ProfilerConnection : public BaseObject { |
| 34 | + public: |
| 35 | + class V8ProfilerSessionDelegate |
| 36 | + : public inspector::InspectorSessionDelegate { |
| 37 | + public: |
| 38 | + explicit V8ProfilerSessionDelegate(V8ProfilerConnection* connection) |
| 39 | + : connection_(connection) {} |
| 40 | + |
| 41 | + void SendMessageToFrontend( |
| 42 | + const v8_inspector::StringView& message) override { |
| 43 | + Environment* env = connection_->env(); |
| 44 | + |
| 45 | + Local<Function> fn = connection_->GetMessageCallback(); |
| 46 | + bool ending = !fn.IsEmpty(); |
| 47 | + Debug(env, |
| 48 | + DebugCategory::INSPECTOR_PROFILER, |
| 49 | + "Sending message to frontend, ending = %s\n", |
| 50 | + ending ? "true" : "false"); |
| 51 | + if (!ending) { |
| 52 | + return; |
| 53 | + } |
| 54 | + Isolate* isolate = env->isolate(); |
| 55 | + |
| 56 | + HandleScope handle_scope(isolate); |
| 57 | + Context::Scope context_scope(env->context()); |
| 58 | + MaybeLocal<String> v8string = |
| 59 | + String::NewFromTwoByte(isolate, |
| 60 | + message.characters16(), |
| 61 | + NewStringType::kNormal, |
| 62 | + message.length()); |
| 63 | + Local<Value> args[] = {v8string.ToLocalChecked().As<Value>()}; |
| 64 | + USE(fn->Call( |
| 65 | + env->context(), connection_->object(), arraysize(args), args)); |
| 66 | + } |
| 67 | + |
| 68 | + private: |
| 69 | + V8ProfilerConnection* connection_; |
| 70 | + }; |
| 71 | + |
| 72 | + SET_MEMORY_INFO_NAME(V8ProfilerConnection) |
| 73 | + SET_SELF_SIZE(V8ProfilerConnection) |
| 74 | + |
| 75 | + void MemoryInfo(MemoryTracker* tracker) const override { |
| 76 | + tracker->TrackFieldWithSize( |
| 77 | + "session", sizeof(*session_), "InspectorSession"); |
| 78 | + } |
| 79 | + |
| 80 | + explicit V8ProfilerConnection(Environment* env, Local<Object> obj) |
| 81 | + : BaseObject(env, obj), session_(nullptr) { |
| 82 | + inspector::Agent* inspector = env->inspector_agent(); |
| 83 | + std::unique_ptr<inspector::InspectorSession> session = inspector->Connect( |
| 84 | + std::make_unique<V8ProfilerSessionDelegate>(this), false); |
| 85 | + session_ = std::move(session); |
| 86 | + MakeWeak(); |
| 87 | + } |
| 88 | + |
| 89 | + void DispatchMessage(Isolate* isolate, Local<String> message) { |
| 90 | + session_->Dispatch(ToProtocolString(isolate, message)->string()); |
| 91 | + } |
| 92 | + |
| 93 | + static MaybeLocal<Object> CreateConnectionObject(Environment* env) { |
| 94 | + Isolate* isolate = env->isolate(); |
| 95 | + Local<Context> context = env->context(); |
| 96 | + EscapableHandleScope scope(isolate); |
| 97 | + |
| 98 | + Local<ObjectTemplate> t = ObjectTemplate::New(isolate); |
| 99 | + t->SetInternalFieldCount(1); |
| 100 | + Local<Object> obj; |
| 101 | + if (!t->NewInstance(context).ToLocal(&obj)) { |
| 102 | + return MaybeLocal<Object>(); |
| 103 | + } |
| 104 | + |
| 105 | + obj->SetAlignedPointerInInternalField(0, nullptr); |
| 106 | + return scope.Escape(obj); |
| 107 | + } |
| 108 | + |
| 109 | + void Start() { |
| 110 | + SetConnection(object()); |
| 111 | + StartProfiling(); |
| 112 | + } |
| 113 | + |
| 114 | + void End(Local<Function> callback) { |
| 115 | + SetMessageCallback(callback); |
| 116 | + EndProfiling(); |
| 117 | + } |
| 118 | + |
| 119 | + // Override this to return a JS function that gets called with the message |
| 120 | + // sent from the session. |
| 121 | + virtual Local<Function> GetMessageCallback() = 0; |
| 122 | + virtual void SetMessageCallback(Local<Function> callback) = 0; |
| 123 | + // Use DispatchMessage() to dispatch necessary inspector messages |
| 124 | + virtual void StartProfiling() = 0; |
| 125 | + virtual void EndProfiling() = 0; |
| 126 | + virtual void SetConnection(Local<Object> connection) = 0; |
| 127 | + |
| 128 | + private: |
| 129 | + std::unique_ptr<inspector::InspectorSession> session_; |
| 130 | +}; |
| 131 | + |
| 132 | +class V8CoverageConnection : public V8ProfilerConnection { |
| 133 | + public: |
| 134 | + explicit V8CoverageConnection(Environment* env) |
| 135 | + : V8ProfilerConnection(env, |
| 136 | + CreateConnectionObject(env).ToLocalChecked()) {} |
| 137 | + |
| 138 | + Local<Function> GetMessageCallback() override { |
| 139 | + return env()->on_coverage_message_function(); |
| 140 | + } |
| 141 | + |
| 142 | + void SetMessageCallback(Local<Function> callback) override { |
| 143 | + return env()->set_on_coverage_message_function(callback); |
| 144 | + } |
| 145 | + |
| 146 | + static V8ProfilerConnection* GetConnection(Environment* env) { |
| 147 | + return Unwrap<V8CoverageConnection>(env->coverage_connection()); |
| 148 | + } |
| 149 | + |
| 150 | + void SetConnection(Local<Object> obj) override { |
| 151 | + env()->set_coverage_connection(obj); |
| 152 | + } |
| 153 | + |
| 154 | + void StartProfiling() override { |
| 155 | + Debug(env(), |
| 156 | + DebugCategory::INSPECTOR_PROFILER, |
| 157 | + "Sending Profiler.startPreciseCoverage\n"); |
| 158 | + Isolate* isolate = env()->isolate(); |
| 159 | + Local<String> enable = FIXED_ONE_BYTE_STRING( |
| 160 | + isolate, "{\"id\": 1, \"method\": \"Profiler.enable\"}"); |
| 161 | + Local<String> start = FIXED_ONE_BYTE_STRING( |
| 162 | + isolate, |
| 163 | + "{" |
| 164 | + "\"id\": 2," |
| 165 | + "\"method\": \"Profiler.startPreciseCoverage\"," |
| 166 | + "\"params\": {\"callCount\": true, \"detailed\": true}" |
| 167 | + "}"); |
| 168 | + DispatchMessage(isolate, enable); |
| 169 | + DispatchMessage(isolate, start); |
| 170 | + } |
| 171 | + |
| 172 | + void EndProfiling() override { |
| 173 | + Debug(env(), |
| 174 | + DebugCategory::INSPECTOR_PROFILER, |
| 175 | + "Sending Profiler.takePreciseCoverage\n"); |
| 176 | + Isolate* isolate = env()->isolate(); |
| 177 | + Local<String> end = |
| 178 | + FIXED_ONE_BYTE_STRING(isolate, |
| 179 | + "{" |
| 180 | + "\"id\": 3," |
| 181 | + "\"method\": \"Profiler.takePreciseCoverage\"" |
| 182 | + "}"); |
| 183 | + DispatchMessage(isolate, end); |
| 184 | + } |
| 185 | + |
| 186 | + private: |
| 187 | + std::unique_ptr<inspector::InspectorSession> session_; |
| 188 | +}; |
| 189 | + |
| 190 | +void StartCoverageCollection(Environment* env) { |
| 191 | + V8CoverageConnection* connection = new V8CoverageConnection(env); |
| 192 | + connection->Start(); |
| 193 | +} |
| 194 | + |
| 195 | +static void EndCoverageCollection(const FunctionCallbackInfo<Value>& args) { |
| 196 | + Environment* env = Environment::GetCurrent(args); |
| 197 | + CHECK(args[0]->IsFunction()); |
| 198 | + Debug(env, DebugCategory::INSPECTOR_PROFILER, "Ending coverage collection\n"); |
| 199 | + V8ProfilerConnection* connection = V8CoverageConnection::GetConnection(env); |
| 200 | + CHECK_NOT_NULL(connection); |
| 201 | + connection->End(args[0].As<Function>()); |
| 202 | +} |
| 203 | + |
| 204 | +static void Initialize(Local<Object> target, |
| 205 | + Local<Value> unused, |
| 206 | + Local<Context> context, |
| 207 | + void* priv) { |
| 208 | + Environment* env = Environment::GetCurrent(context); |
| 209 | + env->SetMethod(target, "endCoverage", EndCoverageCollection); |
| 210 | +} |
| 211 | +} // namespace profiler |
| 212 | +} // namespace node |
| 213 | + |
| 214 | +NODE_MODULE_CONTEXT_AWARE_INTERNAL(profiler, node::profiler::Initialize) |
0 commit comments