Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | # include "incls/_precompiled.incl" |
duke@435 | 26 | # include "incls/_attachListener_windows.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | #include <windows.h> |
duke@435 | 29 | #include <signal.h> // SIGBREAK |
duke@435 | 30 | |
duke@435 | 31 | // The AttachListener thread services a queue of operations. It blocks in the dequeue |
duke@435 | 32 | // function until an operation is enqueued. A client enqueues an operation by creating |
duke@435 | 33 | // a thread in this process using the Win32 CreateRemoteThread function. That thread |
duke@435 | 34 | // executes a small stub generated by the client. The stub invokes the |
duke@435 | 35 | // JVM_EnqueueOperation function which checks the operation parameters and enqueues |
duke@435 | 36 | // the operation to the queue serviced by the attach listener. The thread created by |
duke@435 | 37 | // the client is a native thread and is restricted to a single page of stack. To keep |
duke@435 | 38 | // it simple operations are pre-allocated at initialization time. An enqueue thus |
duke@435 | 39 | // takes a preallocated operation, populates the operation parameters, adds it to |
duke@435 | 40 | // queue and wakes up the attach listener. |
duke@435 | 41 | // |
duke@435 | 42 | // When an operation has completed the attach listener is required to send the |
duke@435 | 43 | // operation result and any result data to the client. In this implementation the |
duke@435 | 44 | // client is a pipe server. In the enqueue operation it provides the name of pipe |
duke@435 | 45 | // to this process. When the operation is completed this process opens the pipe and |
duke@435 | 46 | // sends the result and output back to the client. Note that writing to the pipe |
duke@435 | 47 | // (and flushing the output) is a blocking operation. This means that a non-responsive |
duke@435 | 48 | // client could potentially hang the attach listener thread indefinitely. In that |
duke@435 | 49 | // case no new operations would be executed but the VM would continue as normal. |
duke@435 | 50 | // As only suitably privileged processes can open this process we concluded that |
duke@435 | 51 | // this wasn't worth worrying about. |
duke@435 | 52 | |
duke@435 | 53 | |
duke@435 | 54 | // forward reference |
duke@435 | 55 | class Win32AttachOperation; |
duke@435 | 56 | |
duke@435 | 57 | |
duke@435 | 58 | class Win32AttachListener: AllStatic { |
duke@435 | 59 | private: |
duke@435 | 60 | enum { |
duke@435 | 61 | preallocate_count = 4 // number of preallocated operations |
duke@435 | 62 | }; |
duke@435 | 63 | |
duke@435 | 64 | // protects the preallocated list and the operation list |
duke@435 | 65 | static HANDLE _mutex; |
duke@435 | 66 | |
duke@435 | 67 | // head of preallocated operations list |
duke@435 | 68 | static Win32AttachOperation* _avail; |
duke@435 | 69 | |
duke@435 | 70 | // head and tail of enqueue operations list |
duke@435 | 71 | static Win32AttachOperation* _head; |
duke@435 | 72 | static Win32AttachOperation* _tail; |
duke@435 | 73 | |
duke@435 | 74 | |
duke@435 | 75 | static Win32AttachOperation* head() { return _head; } |
duke@435 | 76 | static void set_head(Win32AttachOperation* head) { _head = head; } |
duke@435 | 77 | |
duke@435 | 78 | static Win32AttachOperation* tail() { return _tail; } |
duke@435 | 79 | static void set_tail(Win32AttachOperation* tail) { _tail = tail; } |
duke@435 | 80 | |
duke@435 | 81 | |
duke@435 | 82 | // used to wakeup the listener |
duke@435 | 83 | static HANDLE _wakeup; |
duke@435 | 84 | static HANDLE wakeup() { return _wakeup; } |
duke@435 | 85 | |
duke@435 | 86 | public: |
duke@435 | 87 | enum { |
duke@435 | 88 | ATTACH_ERROR_DISABLED = 100, // error codes |
duke@435 | 89 | ATTACH_ERROR_RESOURCE = 101, |
duke@435 | 90 | ATTACH_ERROR_ILLEGALARG = 102, |
duke@435 | 91 | ATTACH_ERROR_INTERNAL = 103 |
duke@435 | 92 | }; |
duke@435 | 93 | |
duke@435 | 94 | static int init(); |
duke@435 | 95 | static HANDLE mutex() { return _mutex; } |
duke@435 | 96 | |
duke@435 | 97 | static Win32AttachOperation* available() { return _avail; } |
duke@435 | 98 | static void set_available(Win32AttachOperation* avail) { _avail = avail; } |
duke@435 | 99 | |
duke@435 | 100 | // enqueue an operation to the end of the list |
duke@435 | 101 | static int enqueue(char* cmd, char* arg1, char* arg2, char* arg3, char* pipename); |
duke@435 | 102 | |
duke@435 | 103 | // dequeue an operation from from head of the list |
duke@435 | 104 | static Win32AttachOperation* dequeue(); |
duke@435 | 105 | }; |
duke@435 | 106 | |
duke@435 | 107 | // statics |
duke@435 | 108 | HANDLE Win32AttachListener::_mutex; |
duke@435 | 109 | HANDLE Win32AttachListener::_wakeup; |
duke@435 | 110 | Win32AttachOperation* Win32AttachListener::_avail; |
duke@435 | 111 | Win32AttachOperation* Win32AttachListener::_head; |
duke@435 | 112 | Win32AttachOperation* Win32AttachListener::_tail; |
duke@435 | 113 | |
duke@435 | 114 | |
duke@435 | 115 | // Win32AttachOperation is an AttachOperation that additionally encapsulates the name |
duke@435 | 116 | // of a pipe which is used to send the operation reply/output to the client. |
duke@435 | 117 | // Win32AttachOperation can also be linked in a list. |
duke@435 | 118 | |
duke@435 | 119 | class Win32AttachOperation: public AttachOperation { |
duke@435 | 120 | private: |
duke@435 | 121 | friend class Win32AttachListener; |
duke@435 | 122 | |
duke@435 | 123 | enum { |
duke@435 | 124 | pipe_name_max = 256 // maximum pipe name |
duke@435 | 125 | }; |
duke@435 | 126 | |
duke@435 | 127 | char _pipe[pipe_name_max+1]; |
duke@435 | 128 | |
duke@435 | 129 | const char* pipe() const { return _pipe; } |
duke@435 | 130 | void set_pipe(const char* pipe) { |
duke@435 | 131 | assert(strlen(pipe) <= pipe_name_max, "execeds maximum length of pipe name"); |
duke@435 | 132 | strcpy(_pipe, pipe); |
duke@435 | 133 | } |
duke@435 | 134 | |
duke@435 | 135 | HANDLE open_pipe(); |
duke@435 | 136 | static BOOL write_pipe(HANDLE hPipe, char* buf, int len); |
duke@435 | 137 | |
duke@435 | 138 | Win32AttachOperation* _next; |
duke@435 | 139 | |
duke@435 | 140 | Win32AttachOperation* next() const { return _next; } |
duke@435 | 141 | void set_next(Win32AttachOperation* next) { _next = next; } |
duke@435 | 142 | |
duke@435 | 143 | // noarg constructor as operation is preallocated |
duke@435 | 144 | Win32AttachOperation() : AttachOperation("<noname>") { |
duke@435 | 145 | set_pipe("<nopipe>"); |
duke@435 | 146 | set_next(NULL); |
duke@435 | 147 | } |
duke@435 | 148 | |
duke@435 | 149 | public: |
duke@435 | 150 | void Win32AttachOperation::complete(jint result, bufferedStream* result_stream); |
duke@435 | 151 | }; |
duke@435 | 152 | |
duke@435 | 153 | |
duke@435 | 154 | // preallocate the required number of operations |
duke@435 | 155 | int Win32AttachListener::init() { |
duke@435 | 156 | _mutex = (void*)::CreateMutex(NULL, FALSE, NULL); |
duke@435 | 157 | guarantee(_mutex != (HANDLE)NULL, "mutex creation failed"); |
duke@435 | 158 | |
duke@435 | 159 | _wakeup = ::CreateSemaphore(NULL, 0, 1, NULL); |
duke@435 | 160 | guarantee(_wakeup != (HANDLE)NULL, "semaphore creation failed"); |
duke@435 | 161 | |
duke@435 | 162 | set_head(NULL); |
duke@435 | 163 | set_tail(NULL); |
duke@435 | 164 | |
duke@435 | 165 | // preallocate a few operations |
duke@435 | 166 | set_available(NULL); |
duke@435 | 167 | for (int i=0; i<preallocate_count; i++) { |
duke@435 | 168 | Win32AttachOperation* op = new Win32AttachOperation(); |
duke@435 | 169 | op->set_next(available()); |
duke@435 | 170 | set_available(op); |
duke@435 | 171 | } |
duke@435 | 172 | |
duke@435 | 173 | return 0; |
duke@435 | 174 | } |
duke@435 | 175 | |
duke@435 | 176 | // Enqueue an operation. This is called from a native thread that is not attached to VM. |
duke@435 | 177 | // Also we need to be careful not to execute anything that results in more than a 4k stack. |
duke@435 | 178 | // |
duke@435 | 179 | int Win32AttachListener::enqueue(char* cmd, char* arg0, char* arg1, char* arg2, char* pipename) { |
duke@435 | 180 | // listener not running |
duke@435 | 181 | if (!AttachListener::is_initialized()) { |
duke@435 | 182 | return ATTACH_ERROR_DISABLED; |
duke@435 | 183 | } |
duke@435 | 184 | |
duke@435 | 185 | // check that all paramteres to the operation |
duke@435 | 186 | if (strlen(cmd) > AttachOperation::name_length_max) return ATTACH_ERROR_ILLEGALARG; |
duke@435 | 187 | if (strlen(arg0) > AttachOperation::arg_length_max) return ATTACH_ERROR_ILLEGALARG; |
duke@435 | 188 | if (strlen(arg0) > AttachOperation::arg_length_max) return ATTACH_ERROR_ILLEGALARG; |
duke@435 | 189 | if (strlen(pipename) > Win32AttachOperation::pipe_name_max) return ATTACH_ERROR_ILLEGALARG; |
duke@435 | 190 | |
duke@435 | 191 | // check for a well-formed pipename |
duke@435 | 192 | if (strstr(pipename, "\\\\.\\pipe\\") != pipename) return ATTACH_ERROR_ILLEGALARG; |
duke@435 | 193 | |
duke@435 | 194 | // grab the lock for the list |
duke@435 | 195 | DWORD res = ::WaitForSingleObject(mutex(), INFINITE); |
duke@435 | 196 | if (res != WAIT_OBJECT_0) { |
duke@435 | 197 | return ATTACH_ERROR_INTERNAL; |
duke@435 | 198 | } |
duke@435 | 199 | |
duke@435 | 200 | // try to get an operation from the available list |
duke@435 | 201 | Win32AttachOperation* op = available(); |
duke@435 | 202 | if (op != NULL) { |
duke@435 | 203 | set_available(op->next()); |
duke@435 | 204 | |
duke@435 | 205 | // add to end (tail) of list |
duke@435 | 206 | op->set_next(NULL); |
duke@435 | 207 | if (tail() == NULL) { |
duke@435 | 208 | set_head(op); |
duke@435 | 209 | } else { |
duke@435 | 210 | tail()->set_next(op); |
duke@435 | 211 | } |
duke@435 | 212 | set_tail(op); |
duke@435 | 213 | |
duke@435 | 214 | op->set_name(cmd); |
duke@435 | 215 | op->set_arg(0, arg0); |
duke@435 | 216 | op->set_arg(1, arg1); |
duke@435 | 217 | op->set_arg(2, arg2); |
duke@435 | 218 | op->set_pipe(pipename); |
duke@435 | 219 | |
duke@435 | 220 | // wakeup the thread waiting for operations |
duke@435 | 221 | ::ReleaseSemaphore(wakeup(), 1, NULL); |
duke@435 | 222 | } |
duke@435 | 223 | ::ReleaseMutex(mutex()); |
duke@435 | 224 | |
duke@435 | 225 | return (op != NULL) ? 0 : ATTACH_ERROR_RESOURCE; |
duke@435 | 226 | } |
duke@435 | 227 | |
duke@435 | 228 | |
duke@435 | 229 | // dequeue the operation from the head of the operation list. If |
duke@435 | 230 | Win32AttachOperation* Win32AttachListener::dequeue() { |
duke@435 | 231 | for (;;) { |
duke@435 | 232 | DWORD res = ::WaitForSingleObject(wakeup(), INFINITE); |
duke@435 | 233 | guarantee(res == WAIT_OBJECT_0, "wait failed"); |
duke@435 | 234 | |
duke@435 | 235 | res = ::WaitForSingleObject(mutex(), INFINITE); |
duke@435 | 236 | guarantee(res == WAIT_OBJECT_0, "wait failed"); |
duke@435 | 237 | |
duke@435 | 238 | Win32AttachOperation* op = head(); |
duke@435 | 239 | if (op != NULL) { |
duke@435 | 240 | set_head(op->next()); |
duke@435 | 241 | if (head() == NULL) { // list is empty |
duke@435 | 242 | set_tail(NULL); |
duke@435 | 243 | } |
duke@435 | 244 | } |
duke@435 | 245 | ::ReleaseMutex(mutex()); |
duke@435 | 246 | |
duke@435 | 247 | if (op != NULL) { |
duke@435 | 248 | return op; |
duke@435 | 249 | } |
duke@435 | 250 | } |
duke@435 | 251 | } |
duke@435 | 252 | |
duke@435 | 253 | |
duke@435 | 254 | // open the pipe to the client |
duke@435 | 255 | HANDLE Win32AttachOperation::open_pipe() { |
duke@435 | 256 | HANDLE hPipe; |
duke@435 | 257 | |
duke@435 | 258 | hPipe = ::CreateFile( pipe(), // pipe name |
duke@435 | 259 | GENERIC_WRITE, // write only |
duke@435 | 260 | 0, // no sharing |
duke@435 | 261 | NULL, // default security attributes |
duke@435 | 262 | OPEN_EXISTING, // opens existing pipe |
duke@435 | 263 | 0, // default attributes |
duke@435 | 264 | NULL); // no template file |
duke@435 | 265 | |
duke@435 | 266 | if (hPipe != INVALID_HANDLE_VALUE) { |
duke@435 | 267 | // shouldn't happen as there is a pipe created per operation |
duke@435 | 268 | if (::GetLastError() == ERROR_PIPE_BUSY) { |
duke@435 | 269 | return INVALID_HANDLE_VALUE; |
duke@435 | 270 | } |
duke@435 | 271 | } |
duke@435 | 272 | return hPipe; |
duke@435 | 273 | } |
duke@435 | 274 | |
duke@435 | 275 | // write to the pipe |
duke@435 | 276 | BOOL Win32AttachOperation::write_pipe(HANDLE hPipe, char* buf, int len) { |
duke@435 | 277 | do { |
duke@435 | 278 | DWORD nwrote; |
duke@435 | 279 | |
duke@435 | 280 | BOOL fSuccess = WriteFile( hPipe, // pipe handle |
duke@435 | 281 | (LPCVOID)buf, // message |
duke@435 | 282 | (DWORD)len, // message length |
duke@435 | 283 | &nwrote, // bytes written |
duke@435 | 284 | NULL); // not overlapped |
duke@435 | 285 | if (!fSuccess) { |
duke@435 | 286 | return fSuccess; |
duke@435 | 287 | } |
duke@435 | 288 | buf += nwrote; |
duke@435 | 289 | len -= nwrote; |
duke@435 | 290 | } |
duke@435 | 291 | while (len > 0); |
duke@435 | 292 | return TRUE; |
duke@435 | 293 | } |
duke@435 | 294 | |
duke@435 | 295 | // Complete the operation: |
duke@435 | 296 | // - open the pipe to the client |
duke@435 | 297 | // - write the operation result (a jint) |
duke@435 | 298 | // - write the operation output (the result stream) |
duke@435 | 299 | // |
duke@435 | 300 | void Win32AttachOperation::complete(jint result, bufferedStream* result_stream) { |
duke@435 | 301 | JavaThread* thread = JavaThread::current(); |
duke@435 | 302 | ThreadBlockInVM tbivm(thread); |
duke@435 | 303 | |
duke@435 | 304 | thread->set_suspend_equivalent(); |
duke@435 | 305 | // cleared by handle_special_suspend_equivalent_condition() or |
duke@435 | 306 | // java_suspend_self() via check_and_wait_while_suspended() |
duke@435 | 307 | |
duke@435 | 308 | HANDLE hPipe = open_pipe(); |
duke@435 | 309 | if (hPipe != INVALID_HANDLE_VALUE) { |
duke@435 | 310 | BOOL fSuccess; |
duke@435 | 311 | |
duke@435 | 312 | char msg[32]; |
duke@435 | 313 | sprintf(msg, "%d\n", result); |
duke@435 | 314 | |
duke@435 | 315 | fSuccess = write_pipe(hPipe, msg, (int)strlen(msg)); |
duke@435 | 316 | if (fSuccess) { |
duke@435 | 317 | write_pipe(hPipe, (char*) result_stream->base(), (int)(result_stream->size())); |
duke@435 | 318 | } |
duke@435 | 319 | |
duke@435 | 320 | // Need to flush buffers |
duke@435 | 321 | FlushFileBuffers(hPipe); |
duke@435 | 322 | CloseHandle(hPipe); |
duke@435 | 323 | } |
duke@435 | 324 | |
duke@435 | 325 | DWORD res = ::WaitForSingleObject(Win32AttachListener::mutex(), INFINITE); |
duke@435 | 326 | if (res == WAIT_OBJECT_0) { |
duke@435 | 327 | |
duke@435 | 328 | // put the operation back on the available list |
duke@435 | 329 | set_next(Win32AttachListener::available()); |
duke@435 | 330 | Win32AttachListener::set_available(this); |
duke@435 | 331 | |
duke@435 | 332 | ::ReleaseMutex(Win32AttachListener::mutex()); |
duke@435 | 333 | } |
duke@435 | 334 | |
duke@435 | 335 | // were we externally suspended while we were waiting? |
duke@435 | 336 | thread->check_and_wait_while_suspended(); |
duke@435 | 337 | } |
duke@435 | 338 | |
duke@435 | 339 | |
duke@435 | 340 | // AttachOperation functions |
duke@435 | 341 | |
duke@435 | 342 | AttachOperation* AttachListener::dequeue() { |
duke@435 | 343 | JavaThread* thread = JavaThread::current(); |
duke@435 | 344 | ThreadBlockInVM tbivm(thread); |
duke@435 | 345 | |
duke@435 | 346 | thread->set_suspend_equivalent(); |
duke@435 | 347 | // cleared by handle_special_suspend_equivalent_condition() or |
duke@435 | 348 | // java_suspend_self() via check_and_wait_while_suspended() |
duke@435 | 349 | |
duke@435 | 350 | AttachOperation* op = Win32AttachListener::dequeue(); |
duke@435 | 351 | |
duke@435 | 352 | // were we externally suspended while we were waiting? |
duke@435 | 353 | thread->check_and_wait_while_suspended(); |
duke@435 | 354 | |
duke@435 | 355 | return op; |
duke@435 | 356 | } |
duke@435 | 357 | |
duke@435 | 358 | int AttachListener::pd_init() { |
duke@435 | 359 | return Win32AttachListener::init(); |
duke@435 | 360 | } |
duke@435 | 361 | |
duke@435 | 362 | // always startup on Windows NT/2000/XP |
duke@435 | 363 | bool AttachListener::init_at_startup() { |
duke@435 | 364 | return os::win32::is_nt(); |
duke@435 | 365 | } |
duke@435 | 366 | |
duke@435 | 367 | // no trigger mechanism on Windows to start Attach Listener lazily |
duke@435 | 368 | bool AttachListener::is_init_trigger() { |
duke@435 | 369 | return false; |
duke@435 | 370 | } |
duke@435 | 371 | |
duke@435 | 372 | void AttachListener::abort() { |
duke@435 | 373 | // nothing to do |
duke@435 | 374 | } |
duke@435 | 375 | |
duke@435 | 376 | void AttachListener::pd_data_dump() { |
duke@435 | 377 | os::signal_notify(SIGBREAK); |
duke@435 | 378 | } |
duke@435 | 379 | |
duke@435 | 380 | AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) { |
duke@435 | 381 | return NULL; |
duke@435 | 382 | } |
duke@435 | 383 | |
duke@435 | 384 | jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) { |
duke@435 | 385 | out->print_cr("flag '%s' cannot be changed", op->arg(0)); |
duke@435 | 386 | return JNI_ERR; |
duke@435 | 387 | } |
duke@435 | 388 | |
duke@435 | 389 | void AttachListener::pd_detachall() { |
duke@435 | 390 | // do nothing for now |
duke@435 | 391 | } |
duke@435 | 392 | |
duke@435 | 393 | // Native thread started by remote client executes this. |
duke@435 | 394 | extern "C" { |
duke@435 | 395 | JNIEXPORT jint JNICALL |
duke@435 | 396 | JVM_EnqueueOperation(char* cmd, char* arg0, char* arg1, char* arg2, char* pipename) { |
duke@435 | 397 | return (jint)Win32AttachListener::enqueue(cmd, arg0, arg1, arg2, pipename); |
duke@435 | 398 | } |
duke@435 | 399 | |
duke@435 | 400 | } // extern |