hotspot/agent/src/os/win32/serverLists.hpp
author never
Mon, 04 May 2009 22:06:47 -0700
changeset 2744 57f0579fbe09
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6837224: libsaproc.so on linux needs version of 6799141 Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#ifndef _SERVER_LISTS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#define _SERVER_LISTS_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <vector>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <winsock2.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include "IOBuf.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// NOTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// All of these lists are guarded by the global lock managed by the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// Lists class. Lists::init() must be called at the start of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// program.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
class Lists {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  friend class ListsLocker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  static void init();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  static void lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  static void unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  static CRITICAL_SECTION crit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// Should be allocated on stack. Ensures proper locking/unlocking
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// pairing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
class ListsLocker {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  ListsLocker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  ~ListsLocker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// We must keep track of all of the child processes we have forked to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// handle attaching to a target process. This is necessary because we
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// allow clients to detach from processes, but the child processes we
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// fork must necessarily stay alive for the duration of the target
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// application. A subsequent attach operation to the target process
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// results in the same child process being reused. For this reason,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// child processes are known to be in one of two states: attached and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
// detached.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
class ClientInfo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
class ChildInfo {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  /** The pid of the ChildInfo indicates the process ID of the target
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
      process which the subprocess was created to debug, not the pid
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      of the subprocess itself. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  ChildInfo(DWORD pid, HANDLE childProcessHandle,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
            HANDLE writeToStdinHandle, HANDLE readFromStdoutHandle,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
            HANDLE auxHandle1, HANDLE auxHandle2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  DWORD getPid();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  HANDLE getChildProcessHandle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  HANDLE getWriteToStdinHandle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  HANDLE getReadFromStdoutHandle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  /** Set the client which is currently attached to the target process
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      via this child process. Set this to NULL to indicate that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
      child process is ready to accept another attachment. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  void setClient(ClientInfo* clientInfo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  ClientInfo* getClient();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  /** This is NOT automatically called in the destructor */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  void closeAll();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  DWORD pid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  HANDLE childProcessHandle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  HANDLE writeToStdinHandle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  HANDLE readFromStdoutHandle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  HANDLE auxHandle1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  HANDLE auxHandle2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  ClientInfo* client;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
// We keep track of a list of child debugger processes, each of which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
// is responsible for debugging a certain target process. These
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
// debugger processes can serve multiple clients during their
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
// lifetime. When a client detaches from a given process or tells the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
// debugger to "exit", the debug server is notified that the child
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
// process is once again available to accept connections from clients.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
class ChildList {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  typedef std::vector<ChildInfo*> ChildInfoList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  ChildList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  ~ChildList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  void addChild(ChildInfo*);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  /** Removes and returns the ChildInfo* associated with the given
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      child process handle. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  ChildInfo* removeChild(HANDLE childProcessHandle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  /** Removes the given ChildInfo. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  void removeChild(ChildInfo* info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  /** Return the ChildInfo* associated with a given process ID without
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      removing it from the list. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  ChildInfo* getChildByPid(DWORD pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  /** Iteration support */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  int size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  /** Iteration support */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  ChildInfo* getChildByIndex(int index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  ChildInfoList childList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
// We also keep a list of clients whose requests we are responsible
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
// for serving. Clients can attach and detach from child processes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
class ClientInfo {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  ClientInfo(SOCKET dataSocket);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  ~ClientInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  SOCKET getDataSocket();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  /** Gets an IOBuf configured for the data socket, which should be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      used for all communication with the client. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  IOBuf* getIOBuf();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  /** Set the information for the process to which this client is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      attached. Set this to NULL to indicate that the client is not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      currently attached to any target process. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  void setTarget(ChildInfo* childInfo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  /** Get the information for the process to which this client is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      currently attached, or NULL if none. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  ChildInfo* getTarget();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  /** Close down the socket connection to this client. This is NOT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      automatically called by the destructor. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  void closeAll();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  SOCKET dataSocket;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  IOBuf* buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  ChildInfo* target;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
class ClientList {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  typedef std::vector<ClientInfo*> ClientInfoList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  ClientList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  ~ClientList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  /** Adds a client to the list. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  void addClient(ClientInfo* info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  /** Check to see whether the parent socket of any of the ClientInfo
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      objects is readable in the given fd_set. If so, returns TRUE and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      sets the given ClientInfo* (a non-NULL pointer to which must be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      given) appropriately. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  bool isAnyDataSocketSet(fd_set* fds, ClientInfo** info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  /** Removes a client from the list. User is responsible for deleting
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      the ClientInfo* using operator delete. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  void removeClient(ClientInfo* client);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  /** Iteration support. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  int size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  /** Iteration support. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  ClientInfo* get(int num);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  ClientInfoList clientList;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
#endif  // #defined _SERVER_LISTS_