< prev index next >

src/hotspot/os/windows/attachListener_windows.cpp

Print this page


   1 /*
   2  * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 255     guarantee(res == WAIT_OBJECT_0, "wait failed");
 256 
 257     Win32AttachOperation* op = head();
 258     if (op != NULL) {
 259       set_head(op->next());
 260       if (head() == NULL) {     // list is empty
 261         set_tail(NULL);
 262       }
 263     }
 264     ::ReleaseMutex(mutex());
 265 
 266     if (op != NULL) {
 267       return op;
 268     }
 269   }
 270 }
 271 
 272 
 273 // open the pipe to the client
 274 HANDLE Win32AttachOperation::open_pipe() {
 275   HANDLE hPipe = ::CreateFile( pipe(),  // pipe name


 276                         GENERIC_WRITE,   // write only
 277                         0,              // no sharing
 278                         NULL,           // default security attributes
 279                         OPEN_EXISTING,  // opens existing pipe
 280                         0,              // default attributes
 281                         NULL);          // no template file








 282   return hPipe;
 283 }
 284 
 285 // write to the pipe
 286 BOOL Win32AttachOperation::write_pipe(HANDLE hPipe, char* buf, int len) {
 287   do {
 288     DWORD nwrote;
 289 
 290     BOOL fSuccess = WriteFile(  hPipe,                  // pipe handle
 291                                 (LPCVOID)buf,           // message
 292                                 (DWORD)len,             // message length
 293                                 &nwrote,                // bytes written
 294                                 NULL);                  // not overlapped
 295     if (!fSuccess) {
 296       return fSuccess;
 297     }
 298     buf += nwrote;
 299     len -= nwrote;
 300   } while (len > 0);

 301   return TRUE;
 302 }
 303 
 304 // Complete the operation:
 305 //   - open the pipe to the client
 306 //   - write the operation result (a jint)
 307 //   - write the operation output (the result stream)
 308 //
 309 void Win32AttachOperation::complete(jint result, bufferedStream* result_stream) {
 310   JavaThread* thread = JavaThread::current();
 311   ThreadBlockInVM tbivm(thread);
 312 
 313   thread->set_suspend_equivalent();
 314   // cleared by handle_special_suspend_equivalent_condition() or
 315   // java_suspend_self() via check_and_wait_while_suspended()
 316 
 317   HANDLE hPipe = open_pipe();
 318   int lastError = (int)::GetLastError();
 319   if (hPipe != INVALID_HANDLE_VALUE) {
 320     BOOL fSuccess;
 321 
 322     char msg[32];
 323     _snprintf(msg, sizeof(msg), "%d\n", result);
 324     msg[sizeof(msg) - 1] = '\0';
 325 
 326     fSuccess = write_pipe(hPipe, msg, (int)strlen(msg));
 327     if (fSuccess) {
 328       fSuccess = write_pipe(hPipe, (char*)result_stream->base(), (int)(result_stream->size()));
 329     }
 330     lastError = (int)::GetLastError();
 331 
 332     // Need to flush buffers
 333     FlushFileBuffers(hPipe);
 334     CloseHandle(hPipe);
 335 
 336     if (fSuccess) {
 337       log_debug(attach)("wrote result of attach operation %s to pipe %s", name(), pipe());
 338     } else {
 339       log_error(attach)("failure (%d) writing result of operation %s to pipe %s", lastError, name(), pipe());
 340     }
 341   } else {
 342     log_error(attach)("could not open (%d) pipe %s to send result of operation %s", lastError, pipe(), name());
 343   }
 344 
 345   DWORD res = ::WaitForSingleObject(Win32AttachListener::mutex(), INFINITE);
 346   if (res == WAIT_OBJECT_0) {
 347 
 348     // put the operation back on the available list
 349     set_next(Win32AttachListener::available());
 350     Win32AttachListener::set_available(this);
 351 
 352     ::ReleaseMutex(Win32AttachListener::mutex());
 353   }
 354 
 355   // were we externally suspended while we were waiting?
 356   thread->check_and_wait_while_suspended();
 357 }
 358 
 359 
 360 // AttachOperation functions
 361 
 362 AttachOperation* AttachListener::dequeue() {


 364   ThreadBlockInVM tbivm(thread);
 365 
 366   thread->set_suspend_equivalent();
 367   // cleared by handle_special_suspend_equivalent_condition() or
 368   // java_suspend_self() via check_and_wait_while_suspended()
 369 
 370   AttachOperation* op = Win32AttachListener::dequeue();
 371 
 372   // were we externally suspended while we were waiting?
 373   thread->check_and_wait_while_suspended();
 374 
 375   return op;
 376 }
 377 
 378 void AttachListener::vm_start() {
 379   // nothing to do
 380 }
 381 
 382 int AttachListener::pd_init() {
 383   return Win32AttachListener::init();
 384 }
 385 
 386 // This function is used for Un*x OSes only.
 387 // We need not to implement it for Windows.
 388 bool AttachListener::check_socket_file() {
 389   return false;
 390 }
 391 
 392 bool AttachListener::init_at_startup() {
 393   return true;
 394 }
 395 
 396 // no trigger mechanism on Windows to start Attach Listener lazily
 397 bool AttachListener::is_init_trigger() {
 398   return false;
 399 }
 400 
 401 void AttachListener::abort() {
 402   // nothing to do
 403 }
 404 
 405 void AttachListener::pd_data_dump() {
 406   os::signal_notify(SIGBREAK);
 407 }
 408 
 409 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
   1 /*
   2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 255     guarantee(res == WAIT_OBJECT_0, "wait failed");
 256 
 257     Win32AttachOperation* op = head();
 258     if (op != NULL) {
 259       set_head(op->next());
 260       if (head() == NULL) {     // list is empty
 261         set_tail(NULL);
 262       }
 263     }
 264     ::ReleaseMutex(mutex());
 265 
 266     if (op != NULL) {
 267       return op;
 268     }
 269   }
 270 }
 271 
 272 
 273 // open the pipe to the client
 274 HANDLE Win32AttachOperation::open_pipe() {
 275   HANDLE hPipe;
 276 
 277   hPipe = ::CreateFile( pipe(),  // pipe name
 278                         GENERIC_WRITE,   // write only
 279                         0,              // no sharing
 280                         NULL,           // default security attributes
 281                         OPEN_EXISTING,  // opens existing pipe
 282                         0,              // default attributes
 283                         NULL);          // no template file
 284 
 285   if (hPipe != INVALID_HANDLE_VALUE) {
 286     // shouldn't happen as there is a pipe created per operation
 287     if (::GetLastError() == ERROR_PIPE_BUSY) {
 288       ::CloseHandle(hPipe);
 289       return INVALID_HANDLE_VALUE;
 290     }
 291   }
 292   return hPipe;
 293 }
 294 
 295 // write to the pipe
 296 BOOL Win32AttachOperation::write_pipe(HANDLE hPipe, char* buf, int len) {
 297   do {
 298     DWORD nwrote;
 299 
 300     BOOL fSuccess = WriteFile(  hPipe,                  // pipe handle
 301                                 (LPCVOID)buf,           // message
 302                                 (DWORD)len,             // message length
 303                                 &nwrote,                // bytes written
 304                                 NULL);                  // not overlapped
 305     if (!fSuccess) {
 306       return fSuccess;
 307     }
 308     buf += nwrote;
 309     len -= nwrote;
 310   }
 311   while (len > 0);
 312   return TRUE;
 313 }
 314 
 315 // Complete the operation:
 316 //   - open the pipe to the client
 317 //   - write the operation result (a jint)
 318 //   - write the operation output (the result stream)
 319 //
 320 void Win32AttachOperation::complete(jint result, bufferedStream* result_stream) {
 321   JavaThread* thread = JavaThread::current();
 322   ThreadBlockInVM tbivm(thread);
 323 
 324   thread->set_suspend_equivalent();
 325   // cleared by handle_special_suspend_equivalent_condition() or
 326   // java_suspend_self() via check_and_wait_while_suspended()
 327 
 328   HANDLE hPipe = open_pipe();

 329   if (hPipe != INVALID_HANDLE_VALUE) {
 330     BOOL fSuccess;
 331 
 332     char msg[32];
 333     _snprintf(msg, sizeof(msg), "%d\n", result);
 334     msg[sizeof(msg) - 1] = '\0';
 335 
 336     fSuccess = write_pipe(hPipe, msg, (int)strlen(msg));
 337     if (fSuccess) {
 338       fSuccess = write_pipe(hPipe, (char*)result_stream->base(), (int)(result_stream->size()));
 339     }

 340 
 341     // Need to flush buffers
 342     FlushFileBuffers(hPipe);
 343     CloseHandle(hPipe);
 344 
 345     if (fSuccess) {
 346       log_debug(attach)("wrote result of attach operation %s to pipe %s", name(), pipe());
 347     } else {
 348       log_error(attach)("failure writing result of operation %s to pipe %s", name(), pipe());
 349     }
 350   } else {
 351     log_error(attach)("could not open pipe %s to send result of operation %s", pipe(), name());
 352   }
 353 
 354   DWORD res = ::WaitForSingleObject(Win32AttachListener::mutex(), INFINITE);
 355   if (res == WAIT_OBJECT_0) {
 356 
 357     // put the operation back on the available list
 358     set_next(Win32AttachListener::available());
 359     Win32AttachListener::set_available(this);
 360 
 361     ::ReleaseMutex(Win32AttachListener::mutex());
 362   }
 363 
 364   // were we externally suspended while we were waiting?
 365   thread->check_and_wait_while_suspended();
 366 }
 367 
 368 
 369 // AttachOperation functions
 370 
 371 AttachOperation* AttachListener::dequeue() {


 373   ThreadBlockInVM tbivm(thread);
 374 
 375   thread->set_suspend_equivalent();
 376   // cleared by handle_special_suspend_equivalent_condition() or
 377   // java_suspend_self() via check_and_wait_while_suspended()
 378 
 379   AttachOperation* op = Win32AttachListener::dequeue();
 380 
 381   // were we externally suspended while we were waiting?
 382   thread->check_and_wait_while_suspended();
 383 
 384   return op;
 385 }
 386 
 387 void AttachListener::vm_start() {
 388   // nothing to do
 389 }
 390 
 391 int AttachListener::pd_init() {
 392   return Win32AttachListener::init();






 393 }
 394 
 395 bool AttachListener::init_at_startup() {
 396   return true;
 397 }
 398 
 399 // no trigger mechanism on Windows to start Attach Listener lazily
 400 bool AttachListener::is_init_trigger() {
 401   return false;
 402 }
 403 
 404 void AttachListener::abort() {
 405   // nothing to do
 406 }
 407 
 408 void AttachListener::pd_data_dump() {
 409   os::signal_notify(SIGBREAK);
 410 }
 411 
 412 AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
< prev index next >