annotate src/os/solaris/dtrace/jvm_dtrace.c @ 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 e95fc50106cf
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) 2006, 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
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #include <door.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include <errno.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
27 #include <fcntl.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
28 #include <limits.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
29 #include <poll.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
30 #include <signal.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
31 #include <stdarg.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #include <stdio.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
33 #include <stdlib.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
34 #include <string.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
35 #include <sys/types.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
36 #include <sys/stat.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
37 #include <thread.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
38 #include <unistd.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #include "jvm_dtrace.h"
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 // NOTE: These constants are used in JVM code as well.
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // KEEP JVM CODE IN SYNC if you are going to change these...
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44 #define DTRACE_ALLOC_PROBES 0x1
a61af66fc99e Initial load
duke
parents:
diff changeset
45 #define DTRACE_METHOD_PROBES 0x2
a61af66fc99e Initial load
duke
parents:
diff changeset
46 #define DTRACE_MONITOR_PROBES 0x4
a61af66fc99e Initial load
duke
parents:
diff changeset
47 #define DTRACE_ALL_PROBES -1
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // generic error messages
a61af66fc99e Initial load
duke
parents:
diff changeset
50 #define JVM_ERR_OUT_OF_MEMORY "out of memory (native heap)"
a61af66fc99e Initial load
duke
parents:
diff changeset
51 #define JVM_ERR_INVALID_PARAM "invalid input parameter(s)"
a61af66fc99e Initial load
duke
parents:
diff changeset
52 #define JVM_ERR_NULL_PARAM "input paramater is NULL"
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // error messages for attach
a61af66fc99e Initial load
duke
parents:
diff changeset
55 #define JVM_ERR_CANT_OPEN_DOOR "cannot open door file"
a61af66fc99e Initial load
duke
parents:
diff changeset
56 #define JVM_ERR_CANT_CREATE_ATTACH_FILE "cannot create attach file"
a61af66fc99e Initial load
duke
parents:
diff changeset
57 #define JVM_ERR_DOOR_FILE_PERMISSION "door file is not secure"
a61af66fc99e Initial load
duke
parents:
diff changeset
58 #define JVM_ERR_CANT_SIGNAL "cannot send SIGQUIT to target"
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // error messages for enable probe
a61af66fc99e Initial load
duke
parents:
diff changeset
61 #define JVM_ERR_DOOR_CMD_SEND "door command send failed"
a61af66fc99e Initial load
duke
parents:
diff changeset
62 #define JVM_ERR_DOOR_CANT_READ_STATUS "cannot read door command status"
a61af66fc99e Initial load
duke
parents:
diff changeset
63 #define JVM_ERR_DOOR_CMD_STATUS "door command error status"
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 // error message for detach
a61af66fc99e Initial load
duke
parents:
diff changeset
66 #define JVM_ERR_CANT_CLOSE_DOOR "cannot close door file"
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 #define RESTARTABLE(_cmd, _result) do { \
a61af66fc99e Initial load
duke
parents:
diff changeset
69 do { \
a61af66fc99e Initial load
duke
parents:
diff changeset
70 _result = _cmd; \
a61af66fc99e Initial load
duke
parents:
diff changeset
71 } while((_result == -1) && (errno == EINTR)); \
a61af66fc99e Initial load
duke
parents:
diff changeset
72 } while(0)
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 struct _jvm_t {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 pid_t pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 int door_fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
77 };
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 static int libjvm_dtrace_debug;
a61af66fc99e Initial load
duke
parents:
diff changeset
80 static void print_debug(const char* fmt,...) {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 if (libjvm_dtrace_debug) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 va_list alist;
a61af66fc99e Initial load
duke
parents:
diff changeset
83 va_start(alist, fmt);
a61af66fc99e Initial load
duke
parents:
diff changeset
84 fputs("libjvm_dtrace DEBUG: ", stderr);
a61af66fc99e Initial load
duke
parents:
diff changeset
85 vfprintf(stderr, fmt, alist);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 va_end(alist);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 /* Key for thread local error message */
a61af66fc99e Initial load
duke
parents:
diff changeset
91 static thread_key_t jvm_error_key;
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 /* init function for this library */
a61af66fc99e Initial load
duke
parents:
diff changeset
94 static void init_jvm_dtrace() {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 /* check for env. var for debug mode */
a61af66fc99e Initial load
duke
parents:
diff changeset
96 libjvm_dtrace_debug = getenv("LIBJVM_DTRACE_DEBUG") != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 /* create key for thread local error message */
a61af66fc99e Initial load
duke
parents:
diff changeset
98 if (thr_keycreate(&jvm_error_key, NULL) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 print_debug("can't create thread_key_t for jvm error key\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
100 // exit(1); ?
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102 }
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 #pragma init(init_jvm_dtrace)
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 /* set thread local error message */
a61af66fc99e Initial load
duke
parents:
diff changeset
107 static void set_jvm_error(const char* msg) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 thr_setspecific(jvm_error_key, (void*)msg);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 /* clear thread local error message */
a61af66fc99e Initial load
duke
parents:
diff changeset
112 static void clear_jvm_error() {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 thr_setspecific(jvm_error_key, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 /* file handling functions that can handle interrupt */
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 static int file_open(const char* path, int flag) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 RESTARTABLE(open(path, flag), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
121 return ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 static int file_close(int fd) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
126 RESTARTABLE(close(fd), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
127 return ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 static int file_read(int fd, char* buf, int len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 RESTARTABLE(read(fd, buf, len), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 return ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 /* send SIGQUIT signal to given process */
a61af66fc99e Initial load
duke
parents:
diff changeset
137 static int send_sigquit(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
139 RESTARTABLE(kill(pid, SIGQUIT), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
140 return ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142
a61af66fc99e Initial load
duke
parents:
diff changeset
143 /* called to check permissions on attach file */
a61af66fc99e Initial load
duke
parents:
diff changeset
144 static int check_permission(const char* path) {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 struct stat64 sb;
a61af66fc99e Initial load
duke
parents:
diff changeset
146 uid_t uid, gid;
a61af66fc99e Initial load
duke
parents:
diff changeset
147 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
148
a61af66fc99e Initial load
duke
parents:
diff changeset
149 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
150 * Check that the path is owned by the effective uid/gid of this
a61af66fc99e Initial load
duke
parents:
diff changeset
151 * process. Also check that group/other access is not allowed.
a61af66fc99e Initial load
duke
parents:
diff changeset
152 */
a61af66fc99e Initial load
duke
parents:
diff changeset
153 uid = geteuid();
a61af66fc99e Initial load
duke
parents:
diff changeset
154 gid = getegid();
a61af66fc99e Initial load
duke
parents:
diff changeset
155
a61af66fc99e Initial load
duke
parents:
diff changeset
156 res = stat64(path, &sb);
a61af66fc99e Initial load
duke
parents:
diff changeset
157 if (res != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 print_debug("stat failed for %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
159 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161
a61af66fc99e Initial load
duke
parents:
diff changeset
162 if ((sb.st_uid != uid) || (sb.st_gid != gid) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
163 ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
164 print_debug("well-known file %s is not secure\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
165 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
168 }
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 #define ATTACH_FILE_PATTERN "/tmp/.attach_pid%d"
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 /* fill-in the name of attach file name in given buffer */
a61af66fc99e Initial load
duke
parents:
diff changeset
173 static void fill_attach_file_name(char* path, int len, pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
174 memset(path, 0, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
175 sprintf(path, ATTACH_FILE_PATTERN, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177
a61af66fc99e Initial load
duke
parents:
diff changeset
178 #define DOOR_FILE_PATTERN "/tmp/.java_pid%d"
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 /* open door file for the given JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
181 static int open_door(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 char path[PATH_MAX + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
183 int fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
184
a61af66fc99e Initial load
duke
parents:
diff changeset
185 sprintf(path, DOOR_FILE_PATTERN, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 fd = file_open(path, O_RDONLY);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 if (fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 set_jvm_error(JVM_ERR_CANT_OPEN_DOOR);
a61af66fc99e Initial load
duke
parents:
diff changeset
189 print_debug("cannot open door file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
190 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192 print_debug("opened door file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
193 if (check_permission(path) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 set_jvm_error(JVM_ERR_DOOR_FILE_PERMISSION);
a61af66fc99e Initial load
duke
parents:
diff changeset
195 print_debug("check permission failed for %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
196 file_close(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
197 fd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 return fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
200 }
a61af66fc99e Initial load
duke
parents:
diff changeset
201
a61af66fc99e Initial load
duke
parents:
diff changeset
202 /* create attach file for given process */
a61af66fc99e Initial load
duke
parents:
diff changeset
203 static int create_attach_file(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
204 char path[PATH_MAX + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
205 int fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
206 fill_attach_file_name(path, sizeof(path), pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
207 fd = file_open(path, O_CREAT | O_RDWR);
a61af66fc99e Initial load
duke
parents:
diff changeset
208 if (fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
209 set_jvm_error(JVM_ERR_CANT_CREATE_ATTACH_FILE);
a61af66fc99e Initial load
duke
parents:
diff changeset
210 print_debug("cannot create file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
211 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
212 print_debug("created attach file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214 return fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216
a61af66fc99e Initial load
duke
parents:
diff changeset
217 /* delete attach file for given process */
a61af66fc99e Initial load
duke
parents:
diff changeset
218 static void delete_attach_file(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 char path[PATH_MAX + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
220 fill_attach_file_name(path, sizeof(path), pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
221 int res = unlink(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
222 if (res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 print_debug("cannot delete attach file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
224 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 print_debug("deleted attach file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }
a61af66fc99e Initial load
duke
parents:
diff changeset
227 }
a61af66fc99e Initial load
duke
parents:
diff changeset
228
a61af66fc99e Initial load
duke
parents:
diff changeset
229 /* attach to given JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
230 jvm_t* jvm_attach(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
231 jvm_t* jvm;
a61af66fc99e Initial load
duke
parents:
diff changeset
232 int door_fd, attach_fd, i;
a61af66fc99e Initial load
duke
parents:
diff changeset
233
a61af66fc99e Initial load
duke
parents:
diff changeset
234 jvm = (jvm_t*) calloc(1, sizeof(jvm_t));
a61af66fc99e Initial load
duke
parents:
diff changeset
235 if (jvm == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
236 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
a61af66fc99e Initial load
duke
parents:
diff changeset
237 print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__);
a61af66fc99e Initial load
duke
parents:
diff changeset
238 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240 jvm->pid = pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
241 attach_fd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 door_fd = open_door(pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 if (door_fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 print_debug("trying to create attach file\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
246 if ((attach_fd = create_attach_file(pid)) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
247 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
248 }
a61af66fc99e Initial load
duke
parents:
diff changeset
249
a61af66fc99e Initial load
duke
parents:
diff changeset
250 /* send QUIT signal to the target so that it will
a61af66fc99e Initial load
duke
parents:
diff changeset
251 * check for the attach file.
a61af66fc99e Initial load
duke
parents:
diff changeset
252 */
a61af66fc99e Initial load
duke
parents:
diff changeset
253 if (send_sigquit(pid) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 set_jvm_error(JVM_ERR_CANT_SIGNAL);
a61af66fc99e Initial load
duke
parents:
diff changeset
255 print_debug("sending SIGQUIT failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
256 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259 /* give the target VM time to start the attach mechanism */
a61af66fc99e Initial load
duke
parents:
diff changeset
260 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
261 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
262 RESTARTABLE(poll(0, 0, 200), res);
a61af66fc99e Initial load
duke
parents:
diff changeset
263 door_fd = open_door(pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
264 i++;
a61af66fc99e Initial load
duke
parents:
diff changeset
265 } while (i <= 50 && door_fd == -1);
a61af66fc99e Initial load
duke
parents:
diff changeset
266 if (door_fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
267 print_debug("Unable to open door to process %d\n", pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
268 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
269 }
a61af66fc99e Initial load
duke
parents:
diff changeset
270 }
a61af66fc99e Initial load
duke
parents:
diff changeset
271
a61af66fc99e Initial load
duke
parents:
diff changeset
272 quit:
a61af66fc99e Initial load
duke
parents:
diff changeset
273 if (attach_fd >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
274 file_close(attach_fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
275 delete_attach_file(jvm->pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
276 }
a61af66fc99e Initial load
duke
parents:
diff changeset
277 if (door_fd >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 jvm->door_fd = door_fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
279 clear_jvm_error();
a61af66fc99e Initial load
duke
parents:
diff changeset
280 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
281 free(jvm);
a61af66fc99e Initial load
duke
parents:
diff changeset
282 jvm = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
283 }
a61af66fc99e Initial load
duke
parents:
diff changeset
284 return jvm;
a61af66fc99e Initial load
duke
parents:
diff changeset
285 }
a61af66fc99e Initial load
duke
parents:
diff changeset
286
a61af66fc99e Initial load
duke
parents:
diff changeset
287 /* return the last thread local error message */
a61af66fc99e Initial load
duke
parents:
diff changeset
288 const char* jvm_get_last_error() {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 const char* res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 thr_getspecific(jvm_error_key, (void**)&res);
a61af66fc99e Initial load
duke
parents:
diff changeset
291 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293
a61af66fc99e Initial load
duke
parents:
diff changeset
294 /* detach the givenb JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
295 int jvm_detach(jvm_t* jvm) {
a61af66fc99e Initial load
duke
parents:
diff changeset
296 if (jvm) {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 if (jvm->door_fd != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 if (file_close(jvm->door_fd) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
300 set_jvm_error(JVM_ERR_CANT_CLOSE_DOOR);
a61af66fc99e Initial load
duke
parents:
diff changeset
301 res = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
302 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 clear_jvm_error();
a61af66fc99e Initial load
duke
parents:
diff changeset
304 res = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
305 }
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307 free(jvm);
a61af66fc99e Initial load
duke
parents:
diff changeset
308 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
309 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
310 set_jvm_error(JVM_ERR_NULL_PARAM);
a61af66fc99e Initial load
duke
parents:
diff changeset
311 print_debug("jvm_t* is NULL\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
312 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
313 }
a61af66fc99e Initial load
duke
parents:
diff changeset
314 }
a61af66fc99e Initial load
duke
parents:
diff changeset
315
a61af66fc99e Initial load
duke
parents:
diff changeset
316 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
317 * A simple table to translate some known errors into reasonable
a61af66fc99e Initial load
duke
parents:
diff changeset
318 * error messages
a61af66fc99e Initial load
duke
parents:
diff changeset
319 */
a61af66fc99e Initial load
duke
parents:
diff changeset
320 static struct {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 int err;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 const char* msg;
a61af66fc99e Initial load
duke
parents:
diff changeset
323 } const error_messages[] = {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 { 100, "Bad request" },
a61af66fc99e Initial load
duke
parents:
diff changeset
325 { 101, "Protocol mismatch" },
a61af66fc99e Initial load
duke
parents:
diff changeset
326 { 102, "Resource failure" },
a61af66fc99e Initial load
duke
parents:
diff changeset
327 { 103, "Internal error" },
a61af66fc99e Initial load
duke
parents:
diff changeset
328 { 104, "Permission denied" },
a61af66fc99e Initial load
duke
parents:
diff changeset
329 };
a61af66fc99e Initial load
duke
parents:
diff changeset
330
a61af66fc99e Initial load
duke
parents:
diff changeset
331 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
332 * Lookup the given error code and return the appropriate
a61af66fc99e Initial load
duke
parents:
diff changeset
333 * message. If not found return NULL.
a61af66fc99e Initial load
duke
parents:
diff changeset
334 */
a61af66fc99e Initial load
duke
parents:
diff changeset
335 static const char* translate_error(int err) {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 int table_size = sizeof(error_messages) / sizeof(error_messages[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
337 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
338
a61af66fc99e Initial load
duke
parents:
diff changeset
339 for (i=0; i<table_size; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
340 if (err == error_messages[i].err) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 return error_messages[i].msg;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 }
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
345 }
a61af66fc99e Initial load
duke
parents:
diff changeset
346
a61af66fc99e Initial load
duke
parents:
diff changeset
347 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
348 * Current protocol version
a61af66fc99e Initial load
duke
parents:
diff changeset
349 */
a61af66fc99e Initial load
duke
parents:
diff changeset
350 static const char* PROTOCOL_VERSION = "1";
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 #define RES_BUF_SIZE 128
a61af66fc99e Initial load
duke
parents:
diff changeset
353
a61af66fc99e Initial load
duke
parents:
diff changeset
354 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
355 * Enqueue attach-on-demand command to the given JVM
a61af66fc99e Initial load
duke
parents:
diff changeset
356 */
a61af66fc99e Initial load
duke
parents:
diff changeset
357 static
a61af66fc99e Initial load
duke
parents:
diff changeset
358 int enqueue_command(jvm_t* jvm, const char* cstr, int arg_count, const char** args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
359 size_t size;
a61af66fc99e Initial load
duke
parents:
diff changeset
360 door_arg_t door_args;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 char res_buffer[RES_BUF_SIZE];
a61af66fc99e Initial load
duke
parents:
diff changeset
362 int rc, i;
a61af66fc99e Initial load
duke
parents:
diff changeset
363 char* buf = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
364 int result = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
365
a61af66fc99e Initial load
duke
parents:
diff changeset
366 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
367 * First we get the command string and create the start of the
a61af66fc99e Initial load
duke
parents:
diff changeset
368 * argument string to send to the target VM:
a61af66fc99e Initial load
duke
parents:
diff changeset
369 * <ver>\0<cmd>\0
a61af66fc99e Initial load
duke
parents:
diff changeset
370 */
a61af66fc99e Initial load
duke
parents:
diff changeset
371 if (cstr == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
372 print_debug("command name is NULL\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
373 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
374 }
a61af66fc99e Initial load
duke
parents:
diff changeset
375 size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
376 buf = (char*)malloc(size);
a61af66fc99e Initial load
duke
parents:
diff changeset
377 if (buf != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
378 char* pos = buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
379 strcpy(buf, PROTOCOL_VERSION);
a61af66fc99e Initial load
duke
parents:
diff changeset
380 pos += strlen(PROTOCOL_VERSION)+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
381 strcpy(pos, cstr);
a61af66fc99e Initial load
duke
parents:
diff changeset
382 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
383 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
a61af66fc99e Initial load
duke
parents:
diff changeset
384 print_debug("malloc failed at %d in %s\n", __LINE__, __FILE__);
a61af66fc99e Initial load
duke
parents:
diff changeset
385 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
386 }
a61af66fc99e Initial load
duke
parents:
diff changeset
387
a61af66fc99e Initial load
duke
parents:
diff changeset
388 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
389 * Next we iterate over the arguments and extend the buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
390 * to include them.
a61af66fc99e Initial load
duke
parents:
diff changeset
391 */
a61af66fc99e Initial load
duke
parents:
diff changeset
392 for (i=0; i<arg_count; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 cstr = args[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
394 if (cstr != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 size_t len = strlen(cstr);
a61af66fc99e Initial load
duke
parents:
diff changeset
396 char* newbuf = (char*)realloc(buf, size+len+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
397 if (newbuf == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
398 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
a61af66fc99e Initial load
duke
parents:
diff changeset
399 print_debug("realloc failed in %s at %d\n", __FILE__, __LINE__);
a61af66fc99e Initial load
duke
parents:
diff changeset
400 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
401 }
a61af66fc99e Initial load
duke
parents:
diff changeset
402 buf = newbuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
403 strcpy(buf+size, cstr);
a61af66fc99e Initial load
duke
parents:
diff changeset
404 size += len+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
405 }
a61af66fc99e Initial load
duke
parents:
diff changeset
406 }
a61af66fc99e Initial load
duke
parents:
diff changeset
407
a61af66fc99e Initial load
duke
parents:
diff changeset
408 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
409 * The arguments to the door function are in 'buf' so we now
a61af66fc99e Initial load
duke
parents:
diff changeset
410 * do the door call
a61af66fc99e Initial load
duke
parents:
diff changeset
411 */
a61af66fc99e Initial load
duke
parents:
diff changeset
412 door_args.data_ptr = buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
413 door_args.data_size = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
414 door_args.desc_ptr = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
415 door_args.desc_num = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
416 door_args.rbuf = (char*)&res_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
417 door_args.rsize = sizeof(res_buffer);
a61af66fc99e Initial load
duke
parents:
diff changeset
418
a61af66fc99e Initial load
duke
parents:
diff changeset
419 RESTARTABLE(door_call(jvm->door_fd, &door_args), rc);
a61af66fc99e Initial load
duke
parents:
diff changeset
420
a61af66fc99e Initial load
duke
parents:
diff changeset
421 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
422 * door_call failed
a61af66fc99e Initial load
duke
parents:
diff changeset
423 */
a61af66fc99e Initial load
duke
parents:
diff changeset
424 if (rc == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 print_debug("door_call failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
426 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
427 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
428 * door_call succeeded but the call didn't return the the expected jint.
a61af66fc99e Initial load
duke
parents:
diff changeset
429 */
a61af66fc99e Initial load
duke
parents:
diff changeset
430 if (door_args.data_size < sizeof(int)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
431 print_debug("Enqueue error - reason unknown as result is truncated!");
a61af66fc99e Initial load
duke
parents:
diff changeset
432 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
433 int* res = (int*)(door_args.data_ptr);
a61af66fc99e Initial load
duke
parents:
diff changeset
434 if (*res != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
435 const char* msg = translate_error(*res);
a61af66fc99e Initial load
duke
parents:
diff changeset
436 if (msg == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
437 print_debug("Unable to enqueue command to target VM: %d\n", *res);
a61af66fc99e Initial load
duke
parents:
diff changeset
438 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
439 print_debug("Unable to enqueue command to target VM: %s\n", msg);
a61af66fc99e Initial load
duke
parents:
diff changeset
440 }
a61af66fc99e Initial load
duke
parents:
diff changeset
441 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
442 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
443 * The door call should return a file descriptor to one end of
a61af66fc99e Initial load
duke
parents:
diff changeset
444 * a socket pair
a61af66fc99e Initial load
duke
parents:
diff changeset
445 */
a61af66fc99e Initial load
duke
parents:
diff changeset
446 if ((door_args.desc_ptr != NULL) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
447 (door_args.desc_num == 1) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
448 (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 result = door_args.desc_ptr->d_data.d_desc.d_descriptor;
a61af66fc99e Initial load
duke
parents:
diff changeset
450 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
451 print_debug("Reply from enqueue missing descriptor!\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
452 }
a61af66fc99e Initial load
duke
parents:
diff changeset
453 }
a61af66fc99e Initial load
duke
parents:
diff changeset
454 }
a61af66fc99e Initial load
duke
parents:
diff changeset
455 }
a61af66fc99e Initial load
duke
parents:
diff changeset
456
a61af66fc99e Initial load
duke
parents:
diff changeset
457 quit:
a61af66fc99e Initial load
duke
parents:
diff changeset
458 if (buf) free(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
459 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
460 }
a61af66fc99e Initial load
duke
parents:
diff changeset
461
a61af66fc99e Initial load
duke
parents:
diff changeset
462 /* read status code for a door command */
a61af66fc99e Initial load
duke
parents:
diff changeset
463 static int read_status(int fd) {
a61af66fc99e Initial load
duke
parents:
diff changeset
464 char ch, buf[16];
a61af66fc99e Initial load
duke
parents:
diff changeset
465 int index = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
466
a61af66fc99e Initial load
duke
parents:
diff changeset
467 while (1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
468 if (file_read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
469 set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS);
a61af66fc99e Initial load
duke
parents:
diff changeset
470 print_debug("door cmd status: read status failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
471 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473 buf[index++] = ch;
a61af66fc99e Initial load
duke
parents:
diff changeset
474 if (ch == '\n') {
a61af66fc99e Initial load
duke
parents:
diff changeset
475 buf[index - 1] = '\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
476 return atoi(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
477 }
a61af66fc99e Initial load
duke
parents:
diff changeset
478 if (index == sizeof(buf)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS);
a61af66fc99e Initial load
duke
parents:
diff changeset
480 print_debug("door cmd status: read status overflow\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
481 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }
a61af66fc99e Initial load
duke
parents:
diff changeset
483 }
a61af66fc99e Initial load
duke
parents:
diff changeset
484 }
a61af66fc99e Initial load
duke
parents:
diff changeset
485
a61af66fc99e Initial load
duke
parents:
diff changeset
486 static const char* ENABLE_DPROBES_CMD = "enabledprobes";
a61af66fc99e Initial load
duke
parents:
diff changeset
487
a61af66fc99e Initial load
duke
parents:
diff changeset
488 /* enable one or more DTrace probes for a given JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
489 int jvm_enable_dtprobes(jvm_t* jvm, int num_probe_types, const char** probe_types) {
a61af66fc99e Initial load
duke
parents:
diff changeset
490 int fd, status = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 char ch;
a61af66fc99e Initial load
duke
parents:
diff changeset
492 const char* args[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
493 char buf[16];
a61af66fc99e Initial load
duke
parents:
diff changeset
494 int probe_type = 0, index;
a61af66fc99e Initial load
duke
parents:
diff changeset
495 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
496
a61af66fc99e Initial load
duke
parents:
diff changeset
497 if (jvm == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
498 set_jvm_error(JVM_ERR_NULL_PARAM);
a61af66fc99e Initial load
duke
parents:
diff changeset
499 print_debug("jvm_t* is NULL\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
500 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
501 }
a61af66fc99e Initial load
duke
parents:
diff changeset
502
a61af66fc99e Initial load
duke
parents:
diff changeset
503 if (num_probe_types == 0 || probe_types == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
504 probe_types[0] == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
505 set_jvm_error(JVM_ERR_INVALID_PARAM);
a61af66fc99e Initial load
duke
parents:
diff changeset
506 print_debug("invalid probe type argument(s)\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
507 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
508 }
a61af66fc99e Initial load
duke
parents:
diff changeset
509
a61af66fc99e Initial load
duke
parents:
diff changeset
510 for (index = 0; index < num_probe_types; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
511 const char* p = probe_types[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
512 if (strcmp(p, JVM_DTPROBE_OBJECT_ALLOC) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
513 probe_type |= DTRACE_ALLOC_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
514 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
515 } else if (strcmp(p, JVM_DTPROBE_METHOD_ENTRY) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
516 strcmp(p, JVM_DTPROBE_METHOD_RETURN) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
517 probe_type |= DTRACE_METHOD_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
518 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
519 } else if (strcmp(p, JVM_DTPROBE_MONITOR_ENTER) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
520 strcmp(p, JVM_DTPROBE_MONITOR_ENTERED) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
521 strcmp(p, JVM_DTPROBE_MONITOR_EXIT) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
522 strcmp(p, JVM_DTPROBE_MONITOR_WAIT) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
523 strcmp(p, JVM_DTPROBE_MONITOR_WAITED) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
524 strcmp(p, JVM_DTPROBE_MONITOR_NOTIFY) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
525 strcmp(p, JVM_DTPROBE_MONITOR_NOTIFYALL) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
526 probe_type |= DTRACE_MONITOR_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
527 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
528 } else if (strcmp(p, JVM_DTPROBE_ALL) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
529 probe_type |= DTRACE_ALL_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
530 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
531 }
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533
a61af66fc99e Initial load
duke
parents:
diff changeset
534 if (count == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
535 return count;
a61af66fc99e Initial load
duke
parents:
diff changeset
536 }
a61af66fc99e Initial load
duke
parents:
diff changeset
537 sprintf(buf, "%d", probe_type);
a61af66fc99e Initial load
duke
parents:
diff changeset
538 args[0] = buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
539
a61af66fc99e Initial load
duke
parents:
diff changeset
540 fd = enqueue_command(jvm, ENABLE_DPROBES_CMD, 1, args);
a61af66fc99e Initial load
duke
parents:
diff changeset
541 if (fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
542 set_jvm_error(JVM_ERR_DOOR_CMD_SEND);
a61af66fc99e Initial load
duke
parents:
diff changeset
543 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
544 }
a61af66fc99e Initial load
duke
parents:
diff changeset
545
a61af66fc99e Initial load
duke
parents:
diff changeset
546 status = read_status(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
547 // non-zero status is error
a61af66fc99e Initial load
duke
parents:
diff changeset
548 if (status) {
a61af66fc99e Initial load
duke
parents:
diff changeset
549 set_jvm_error(JVM_ERR_DOOR_CMD_STATUS);
a61af66fc99e Initial load
duke
parents:
diff changeset
550 print_debug("%s command failed (status: %d) in target JVM\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
551 ENABLE_DPROBES_CMD, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
552 file_close(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
553 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
554 }
a61af66fc99e Initial load
duke
parents:
diff changeset
555 // read from stream until EOF
a61af66fc99e Initial load
duke
parents:
diff changeset
556 while (file_read(fd, &ch, sizeof(ch)) == sizeof(ch)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 if (libjvm_dtrace_debug) {
a61af66fc99e Initial load
duke
parents:
diff changeset
558 printf("%c", ch);
a61af66fc99e Initial load
duke
parents:
diff changeset
559 }
a61af66fc99e Initial load
duke
parents:
diff changeset
560 }
a61af66fc99e Initial load
duke
parents:
diff changeset
561
a61af66fc99e Initial load
duke
parents:
diff changeset
562 file_close(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
563 clear_jvm_error();
a61af66fc99e Initial load
duke
parents:
diff changeset
564 return count;
a61af66fc99e Initial load
duke
parents:
diff changeset
565 }