@@ -6221,6 +6221,135 @@ TEST(DebugBreakOffThreadTerminate) { |
6221 | 6221 | } |
6222 | 6222 |
|
6223 | 6223 |
|
| 6224 | +class ArchiveRestoreThread : public v8::base::Thread, |
| 6225 | + public v8::debug::DebugDelegate { |
| 6226 | + public: |
| 6227 | + ArchiveRestoreThread(v8::Isolate* isolate, int spawn_count) |
| 6228 | + : Thread(Options("ArchiveRestoreThread")), |
| 6229 | + isolate_(isolate), |
| 6230 | + debug_(reinterpret_cast<i::Isolate*>(isolate_)->debug()), |
| 6231 | + spawn_count_(spawn_count), |
| 6232 | + break_count_(0) {} |
| 6233 | + |
| 6234 | + virtual void Run() { |
| 6235 | + v8::Locker locker(isolate_); |
| 6236 | + isolate_->Enter(); |
| 6237 | + |
| 6238 | + v8::HandleScope scope(isolate_); |
| 6239 | + v8::Local<v8::Context> context = v8::Context::New(isolate_); |
| 6240 | + v8::Context::Scope context_scope(context); |
| 6241 | + |
| 6242 | + v8::Local<v8::Function> test = CompileFunction(isolate_, |
| 6243 | + "function test(n) {\n" |
| 6244 | + " debugger;\n" |
| 6245 | + " return n + 1;\n" |
| 6246 | + "}\n", |
| 6247 | + "test"); |
| 6248 | + |
| 6249 | + debug_->SetDebugDelegate(this, false); |
| 6250 | + v8::internal::DisableBreak enable_break(debug_, false); |
| 6251 | + |
| 6252 | + v8::Local<v8::Value> args[1] = {v8::Integer::New(isolate_, spawn_count_)}; |
| 6253 | + |
| 6254 | + int result = test->Call(context, context->Global(), 1, args) |
| 6255 | + .ToLocalChecked() |
| 6256 | + ->Int32Value(context) |
| 6257 | + .FromJust(); |
| 6258 | + |
| 6259 | + // Verify that test(spawn_count_) returned spawn_count_ + 1. |
| 6260 | + CHECK_EQ(spawn_count_ + 1, result); |
| 6261 | + |
| 6262 | + isolate_->Exit(); |
| 6263 | + } |
| 6264 | + |
| 6265 | + void BreakProgramRequested(v8::Local<v8::Context> context, |
| 6266 | + v8::Local<v8::Object> exec_state, |
| 6267 | + const std::vector<v8::debug::BreakpointId>&) { |
| 6268 | + auto stack_traces = v8::debug::StackTraceIterator::Create(isolate_); |
| 6269 | + if (!stack_traces->Done()) { |
| 6270 | + v8::debug::Location location = stack_traces->GetSourceLocation(); |
| 6271 | + |
| 6272 | + i::PrintF("ArchiveRestoreThread #%d hit breakpoint at line %d\n", |
| 6273 | + spawn_count_, location.GetLineNumber()); |
| 6274 | + |
| 6275 | + switch (location.GetLineNumber()) { |
| 6276 | + case 1: // debugger; |
| 6277 | + CHECK_EQ(break_count_, 0); |
| 6278 | + |
| 6279 | + // Attempt to stop on the next line after the first debugger |
| 6280 | + // statement. If debug->{Archive,Restore}Debug() improperly reset |
| 6281 | + // thread-local debug information, the debugger will fail to stop |
| 6282 | + // before the test function returns. |
| 6283 | + debug_->PrepareStep(StepNext); |
| 6284 | + |
| 6285 | + // Spawning threads while handling the current breakpoint verifies |
| 6286 | + // that the parent thread correctly archived and restored the |
| 6287 | + // state necessary to stop on the next line. If not, then control |
| 6288 | + // will simply continue past the `return n + 1` statement. |
| 6289 | + MaybeSpawnChildThread(); |
| 6290 | + |
| 6291 | + break; |
| 6292 | + |
| 6293 | + case 2: // return n + 1; |
| 6294 | + CHECK_EQ(break_count_, 1); |
| 6295 | + break; |
| 6296 | + |
| 6297 | + default: |
| 6298 | + CHECK(false); |
| 6299 | + } |
| 6300 | + } |
| 6301 | + |
| 6302 | + ++break_count_; |
| 6303 | + } |
| 6304 | + |
| 6305 | + void MaybeSpawnChildThread() { |
| 6306 | + if (spawn_count_ > 1) { |
| 6307 | + v8::Unlocker unlocker(isolate_); |
| 6308 | + |
| 6309 | + // Spawn a thread that spawns a thread that spawns a thread (and so |
| 6310 | + // on) so that the ThreadManager is forced to archive and restore |
| 6311 | + // the current thread. |
| 6312 | + ArchiveRestoreThread child(isolate_, spawn_count_ - 1); |
| 6313 | + child.Start(); |
| 6314 | + child.Join(); |
| 6315 | + |
| 6316 | + // The child thread sets itself as the debug delegate, so we need to |
| 6317 | + // usurp it after the child finishes, or else future breakpoints |
| 6318 | + // will be delegated to a destroyed ArchiveRestoreThread object. |
| 6319 | + debug_->SetDebugDelegate(this, false); |
| 6320 | + |
| 6321 | + // This is the most important check in this test, since |
| 6322 | + // child.GetBreakCount() will return 1 if the debugger fails to stop |
| 6323 | + // on the `return n + 1` line after the grandchild thread returns. |
| 6324 | + CHECK_EQ(child.GetBreakCount(), 2); |
| 6325 | + } |
| 6326 | + } |
| 6327 | + |
| 6328 | + int GetBreakCount() { return break_count_; } |
| 6329 | + |
| 6330 | + private: |
| 6331 | + v8::Isolate* isolate_; |
| 6332 | + v8::internal::Debug* debug_; |
| 6333 | + const int spawn_count_; |
| 6334 | + int break_count_; |
| 6335 | +}; |
| 6336 | + |
| 6337 | +TEST(DebugArchiveRestore) { |
| 6338 | + v8::Isolate::CreateParams create_params; |
| 6339 | + create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
| 6340 | + v8::Isolate* isolate = v8::Isolate::New(create_params); |
| 6341 | + |
| 6342 | + ArchiveRestoreThread thread(isolate, 5); |
| 6343 | + // Instead of calling thread.Start() and thread.Join() here, we call |
| 6344 | + // thread.Run() directly, to make sure we exercise archive/restore |
| 6345 | + // logic on the *current* thread as well as other threads. |
| 6346 | + thread.Run(); |
| 6347 | + CHECK_EQ(thread.GetBreakCount(), 2); |
| 6348 | + |
| 6349 | + isolate->Dispose(); |
| 6350 | +} |
| 6351 | + |
| 6352 | + |
6224 | 6353 | static void DebugEventExpectNoException( |
6225 | 6354 | const v8::Debug::EventDetails& event_details) { |
6226 | 6355 | v8::DebugEvent event = event_details.GetEvent(); |
|