annotate src/os/windows/vm/attachListener_windows.cpp @ 1972:f95d63e2154a

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