src/os/windows/vm/attachListener_windows.cpp

Thu, 17 Jan 2013 10:25:16 -0500

author
hseigel
date
Thu, 17 Jan 2013 10:25:16 -0500
changeset 4465
203f64878aab
parent 2314
f95d63e2154a
child 5412
2e8f19c2feef
permissions
-rw-r--r--

7102489: RFE: cleanup jlong typedef on __APPLE__and _LLP64 systems.
Summary: Define jlong as long on all LP64 platforms and add JLONG_FORMAT macro.
Reviewed-by: dholmes, coleenp, mikael, kvn

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

mercurial