d8-debug.h 3.21 KB
Newer Older
1
// Copyright 2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7 8 9 10

#ifndef V8_D8_DEBUG_H_
#define V8_D8_DEBUG_H_


#include "d8.h"
#include "debug.h"
11
#include "platform/socket.h"
12 13 14 15 16


namespace v8 {


17
void HandleDebugEvent(const Debug::EventDetails& event_details);
18

19 20
// Start the remove debugger connecting to a V8 debugger agent on the specified
// port.
21
void RunRemoteDebugger(Isolate* isolate, int port);
22 23 24 25 26 27 28 29 30

// Forward declerations.
class RemoteDebuggerEvent;
class ReceiverThread;


// Remote debugging class.
class RemoteDebugger {
 public:
31 32 33
  explicit RemoteDebugger(Isolate* isolate, int port)
      : isolate_(isolate),
        port_(port),
34
        event_available_(0),
35
        head_(NULL), tail_(NULL) {}
36 37 38
  void Run();

  // Handle events from the subordinate threads.
39 40
  void MessageReceived(i::SmartArrayPointer<char> message);
  void KeyboardCommand(i::SmartArrayPointer<char> command);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  void ConnectionClosed();

 private:
  // Add new debugger event to the list.
  void AddEvent(RemoteDebuggerEvent* event);
  // Read next debugger event from the list.
  RemoteDebuggerEvent* GetEvent();

  // Handle a message from the debugged V8.
  void HandleMessageReceived(char* message);
  // Handle a keyboard command.
  void HandleKeyboardCommand(char* command);

  // Get connection to agent in debugged V8.
  i::Socket* conn() { return conn_; }

57
  Isolate* isolate_;
58 59 60 61 62 63
  int port_;  // Port used to connect to debugger V8.
  i::Socket* conn_;  // Connection to debugger agent in debugged V8.

  // Linked list of events from debugged V8 and from keyboard input. Access to
  // the list is guarded by a mutex and a semaphore signals new items in the
  // list.
64
  i::Mutex event_access_;
65
  i::Semaphore event_available_;
66 67 68 69 70 71 72 73 74 75
  RemoteDebuggerEvent* head_;
  RemoteDebuggerEvent* tail_;

  friend class ReceiverThread;
};


// Thread reading from debugged V8 instance.
class ReceiverThread: public i::Thread {
 public:
76
  explicit ReceiverThread(RemoteDebugger* remote_debugger)
77
      : Thread("d8:ReceiverThrd"),
78
        remote_debugger_(remote_debugger) {}
79 80 81 82 83 84 85 86 87 88 89 90
  ~ReceiverThread() {}

  void Run();

 private:
  RemoteDebugger* remote_debugger_;
};


// Thread reading keyboard input.
class KeyboardThread: public i::Thread {
 public:
91
  explicit KeyboardThread(RemoteDebugger* remote_debugger)
92
      : Thread("d8:KeyboardThrd"),
93
        remote_debugger_(remote_debugger) {}
94 95 96 97 98 99 100 101 102 103 104 105
  ~KeyboardThread() {}

  void Run();

 private:
  RemoteDebugger* remote_debugger_;
};


// Events processed by the main deubgger thread.
class RemoteDebuggerEvent {
 public:
106
  RemoteDebuggerEvent(int type, i::SmartArrayPointer<char> data)
107 108 109 110 111 112 113 114 115
      : type_(type), data_(data), next_(NULL) {
    ASSERT(type == kMessage || type == kKeyboard || type == kDisconnect);
  }

  static const int kMessage = 1;
  static const int kKeyboard = 2;
  static const int kDisconnect = 3;

  int type() { return type_; }
116
  char* data() { return data_.get(); }
117 118 119 120 121 122

 private:
  void set_next(RemoteDebuggerEvent* event) { next_ = event; }
  RemoteDebuggerEvent* next() { return next_; }

  int type_;
123
  i::SmartArrayPointer<char> data_;
124 125 126 127 128
  RemoteDebuggerEvent* next_;

  friend class RemoteDebugger;
};

129 130 131 132 133

}  // namespace v8


#endif  // V8_D8_DEBUG_H_