Home | History | Annotate | Download | only in src
      1 // Copyright 2008 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #ifndef V8_D8_DEBUG_H_
     29 #define V8_D8_DEBUG_H_
     30 
     31 
     32 #include "d8.h"
     33 #include "debug.h"
     34 
     35 
     36 namespace v8 {
     37 
     38 
     39 void HandleDebugEvent(const Debug::EventDetails& event_details);
     40 
     41 // Start the remove debugger connecting to a V8 debugger agent on the specified
     42 // port.
     43 void RunRemoteDebugger(Isolate* isolate, int port);
     44 
     45 // Forward declerations.
     46 class RemoteDebuggerEvent;
     47 class ReceiverThread;
     48 
     49 
     50 // Remote debugging class.
     51 class RemoteDebugger {
     52  public:
     53   explicit RemoteDebugger(Isolate* isolate, int port)
     54       : isolate_(isolate),
     55         port_(port),
     56         event_access_(i::OS::CreateMutex()),
     57         event_available_(i::OS::CreateSemaphore(0)),
     58         head_(NULL), tail_(NULL) {}
     59   void Run();
     60 
     61   // Handle events from the subordinate threads.
     62   void MessageReceived(i::SmartArrayPointer<char> message);
     63   void KeyboardCommand(i::SmartArrayPointer<char> command);
     64   void ConnectionClosed();
     65 
     66  private:
     67   // Add new debugger event to the list.
     68   void AddEvent(RemoteDebuggerEvent* event);
     69   // Read next debugger event from the list.
     70   RemoteDebuggerEvent* GetEvent();
     71 
     72   // Handle a message from the debugged V8.
     73   void HandleMessageReceived(char* message);
     74   // Handle a keyboard command.
     75   void HandleKeyboardCommand(char* command);
     76 
     77   // Get connection to agent in debugged V8.
     78   i::Socket* conn() { return conn_; }
     79 
     80   Isolate* isolate_;
     81   int port_;  // Port used to connect to debugger V8.
     82   i::Socket* conn_;  // Connection to debugger agent in debugged V8.
     83 
     84   // Linked list of events from debugged V8 and from keyboard input. Access to
     85   // the list is guarded by a mutex and a semaphore signals new items in the
     86   // list.
     87   i::Mutex* event_access_;
     88   i::Semaphore* event_available_;
     89   RemoteDebuggerEvent* head_;
     90   RemoteDebuggerEvent* tail_;
     91 
     92   friend class ReceiverThread;
     93 };
     94 
     95 
     96 // Thread reading from debugged V8 instance.
     97 class ReceiverThread: public i::Thread {
     98  public:
     99   explicit ReceiverThread(RemoteDebugger* remote_debugger)
    100       : Thread("d8:ReceiverThrd"),
    101         remote_debugger_(remote_debugger) {}
    102   ~ReceiverThread() {}
    103 
    104   void Run();
    105 
    106  private:
    107   RemoteDebugger* remote_debugger_;
    108 };
    109 
    110 
    111 // Thread reading keyboard input.
    112 class KeyboardThread: public i::Thread {
    113  public:
    114   explicit KeyboardThread(RemoteDebugger* remote_debugger)
    115       : Thread("d8:KeyboardThrd"),
    116         remote_debugger_(remote_debugger) {}
    117   ~KeyboardThread() {}
    118 
    119   void Run();
    120 
    121  private:
    122   RemoteDebugger* remote_debugger_;
    123 };
    124 
    125 
    126 // Events processed by the main deubgger thread.
    127 class RemoteDebuggerEvent {
    128  public:
    129   RemoteDebuggerEvent(int type, i::SmartArrayPointer<char> data)
    130       : type_(type), data_(data), next_(NULL) {
    131     ASSERT(type == kMessage || type == kKeyboard || type == kDisconnect);
    132   }
    133 
    134   static const int kMessage = 1;
    135   static const int kKeyboard = 2;
    136   static const int kDisconnect = 3;
    137 
    138   int type() { return type_; }
    139   char* data() { return *data_; }
    140 
    141  private:
    142   void set_next(RemoteDebuggerEvent* event) { next_ = event; }
    143   RemoteDebuggerEvent* next() { return next_; }
    144 
    145   int type_;
    146   i::SmartArrayPointer<char> data_;
    147   RemoteDebuggerEvent* next_;
    148 
    149   friend class RemoteDebugger;
    150 };
    151 
    152 
    153 }  // namespace v8
    154 
    155 
    156 #endif  // V8_D8_DEBUG_H_
    157