1 // Copyright 2020 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 #ifndef V8_DEBUG_WASM_GDB_SERVER_GDB_SERVER_THREAD_H_ 6 #define V8_DEBUG_WASM_GDB_SERVER_GDB_SERVER_THREAD_H_ 7 8 #include "src/base/platform/platform.h" 9 #include "src/base/platform/semaphore.h" 10 #include "src/debug/wasm/gdb-server/target.h" 11 #include "src/debug/wasm/gdb-server/transport.h" 12 13 namespace v8 { 14 namespace internal { 15 namespace wasm { 16 namespace gdb_server { 17 18 class GdbServer; 19 20 // class GdbServerThread spawns a thread where all communication with a debugger 21 // happens. 22 class GdbServerThread : public v8::base::Thread { 23 public: 24 explicit GdbServerThread(GdbServer* gdb_server); 25 GdbServerThread(const GdbServerThread&) = delete; 26 GdbServerThread& operator=(const GdbServerThread&) = delete; 27 28 // base::Thread 29 void Run() override; 30 31 // Starts the GDB-server thread and waits Run() method is called on the new 32 // thread and the initialization completes. 33 bool StartAndInitialize(); 34 35 // Stops the GDB-server thread when the V8 process shuts down; gracefully 36 // closes any active debugging session. 37 void Stop(); 38 GetTarget()39 Target& GetTarget() { return *target_; } 40 41 private: 42 void CleanupThread(); 43 44 GdbServer* gdb_server_; 45 46 // Used to block the caller on StartAndInitialize() waiting for the new thread 47 // to have completed its initialization. 48 // (Note that Thread::StartSynchronously() wouldn't work in this case because 49 // it returns as soon as the new thread starts, but before Run() is called). 50 base::Semaphore start_semaphore_; 51 52 base::Mutex mutex_; 53 // Protected by {mutex_}: 54 std::unique_ptr<TransportBase> transport_; 55 std::unique_ptr<Target> target_; 56 }; 57 58 } // namespace gdb_server 59 } // namespace wasm 60 } // namespace internal 61 } // namespace v8 62 63 #endif // V8_DEBUG_WASM_GDB_SERVER_GDB_SERVER_THREAD_H_ 64