annotate src/os/solaris/dtrace/jvm_dtrace.c @ 17524:89152779163c

Merge with jdk8-b132
author Gilles Duboscq <duboscq@ssw.jku.at>
date Wed, 15 Oct 2014 11:59:32 +0200
parents 4ca6dc0799b6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17524
89152779163c Merge with jdk8-b132
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 14909
diff changeset
2 * Copyright (c) 2006, 2013, 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) {
10978
e95fc50106cf 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 1972
diff changeset
125 return close(fd);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127
a61af66fc99e Initial load
duke
parents:
diff changeset
128 static int file_read(int fd, char* buf, int len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
130 RESTARTABLE(read(fd, buf, len), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 return ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 /* send SIGQUIT signal to given process */
a61af66fc99e Initial load
duke
parents:
diff changeset
135 static int send_sigquit(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 int ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
137 RESTARTABLE(kill(pid, SIGQUIT), ret);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 return ret;
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 /* called to check permissions on attach file */
a61af66fc99e Initial load
duke
parents:
diff changeset
142 static int check_permission(const char* path) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 struct stat64 sb;
a61af66fc99e Initial load
duke
parents:
diff changeset
144 uid_t uid, gid;
a61af66fc99e Initial load
duke
parents:
diff changeset
145 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
146
a61af66fc99e Initial load
duke
parents:
diff changeset
147 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
148 * Check that the path is owned by the effective uid/gid of this
a61af66fc99e Initial load
duke
parents:
diff changeset
149 * process. Also check that group/other access is not allowed.
a61af66fc99e Initial load
duke
parents:
diff changeset
150 */
a61af66fc99e Initial load
duke
parents:
diff changeset
151 uid = geteuid();
a61af66fc99e Initial load
duke
parents:
diff changeset
152 gid = getegid();
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 res = stat64(path, &sb);
a61af66fc99e Initial load
duke
parents:
diff changeset
155 if (res != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 print_debug("stat failed for %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
157 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 if ((sb.st_uid != uid) || (sb.st_gid != gid) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
161 ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 print_debug("well-known file %s is not secure\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
163 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 #define ATTACH_FILE_PATTERN "/tmp/.attach_pid%d"
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 /* fill-in the name of attach file name in given buffer */
a61af66fc99e Initial load
duke
parents:
diff changeset
171 static void fill_attach_file_name(char* path, int len, pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 memset(path, 0, len);
a61af66fc99e Initial load
duke
parents:
diff changeset
173 sprintf(path, ATTACH_FILE_PATTERN, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 #define DOOR_FILE_PATTERN "/tmp/.java_pid%d"
a61af66fc99e Initial load
duke
parents:
diff changeset
177
a61af66fc99e Initial load
duke
parents:
diff changeset
178 /* open door file for the given JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
179 static int open_door(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 char path[PATH_MAX + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
181 int fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
182
a61af66fc99e Initial load
duke
parents:
diff changeset
183 sprintf(path, DOOR_FILE_PATTERN, pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
184 fd = file_open(path, O_RDONLY);
a61af66fc99e Initial load
duke
parents:
diff changeset
185 if (fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 set_jvm_error(JVM_ERR_CANT_OPEN_DOOR);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 print_debug("cannot open door file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
188 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 print_debug("opened door file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
191 if (check_permission(path) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
192 set_jvm_error(JVM_ERR_DOOR_FILE_PERMISSION);
a61af66fc99e Initial load
duke
parents:
diff changeset
193 print_debug("check permission failed for %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
194 file_close(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
195 fd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197 return fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
198 }
a61af66fc99e Initial load
duke
parents:
diff changeset
199
a61af66fc99e Initial load
duke
parents:
diff changeset
200 /* create attach file for given process */
a61af66fc99e Initial load
duke
parents:
diff changeset
201 static int create_attach_file(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 char path[PATH_MAX + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
203 int fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
204 fill_attach_file_name(path, sizeof(path), pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
205 fd = file_open(path, O_CREAT | O_RDWR);
a61af66fc99e Initial load
duke
parents:
diff changeset
206 if (fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
207 set_jvm_error(JVM_ERR_CANT_CREATE_ATTACH_FILE);
a61af66fc99e Initial load
duke
parents:
diff changeset
208 print_debug("cannot create file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
209 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
210 print_debug("created attach file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
211 }
a61af66fc99e Initial load
duke
parents:
diff changeset
212 return fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 /* delete attach file for given process */
a61af66fc99e Initial load
duke
parents:
diff changeset
216 static void delete_attach_file(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 char path[PATH_MAX + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
218 fill_attach_file_name(path, sizeof(path), pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
219 int res = unlink(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
220 if (res) {
a61af66fc99e Initial load
duke
parents:
diff changeset
221 print_debug("cannot delete attach file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
222 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 print_debug("deleted attach file %s\n", path);
a61af66fc99e Initial load
duke
parents:
diff changeset
224 }
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226
a61af66fc99e Initial load
duke
parents:
diff changeset
227 /* attach to given JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
228 jvm_t* jvm_attach(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
229 jvm_t* jvm;
a61af66fc99e Initial load
duke
parents:
diff changeset
230 int door_fd, attach_fd, i;
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 jvm = (jvm_t*) calloc(1, sizeof(jvm_t));
a61af66fc99e Initial load
duke
parents:
diff changeset
233 if (jvm == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
234 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
a61af66fc99e Initial load
duke
parents:
diff changeset
235 print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
237 }
a61af66fc99e Initial load
duke
parents:
diff changeset
238 jvm->pid = pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 attach_fd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
240
a61af66fc99e Initial load
duke
parents:
diff changeset
241 door_fd = open_door(pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
242 if (door_fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 print_debug("trying to create attach file\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
244 if ((attach_fd = create_attach_file(pid)) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 /* send QUIT signal to the target so that it will
a61af66fc99e Initial load
duke
parents:
diff changeset
249 * check for the attach file.
a61af66fc99e Initial load
duke
parents:
diff changeset
250 */
a61af66fc99e Initial load
duke
parents:
diff changeset
251 if (send_sigquit(pid) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 set_jvm_error(JVM_ERR_CANT_SIGNAL);
a61af66fc99e Initial load
duke
parents:
diff changeset
253 print_debug("sending SIGQUIT failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
254 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
255 }
a61af66fc99e Initial load
duke
parents:
diff changeset
256
a61af66fc99e Initial load
duke
parents:
diff changeset
257 /* give the target VM time to start the attach mechanism */
a61af66fc99e Initial load
duke
parents:
diff changeset
258 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
260 RESTARTABLE(poll(0, 0, 200), res);
a61af66fc99e Initial load
duke
parents:
diff changeset
261 door_fd = open_door(pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
262 i++;
a61af66fc99e Initial load
duke
parents:
diff changeset
263 } while (i <= 50 && door_fd == -1);
a61af66fc99e Initial load
duke
parents:
diff changeset
264 if (door_fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 print_debug("Unable to open door to process %d\n", pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
266 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 quit:
a61af66fc99e Initial load
duke
parents:
diff changeset
271 if (attach_fd >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 file_close(attach_fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
273 delete_attach_file(jvm->pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
274 }
a61af66fc99e Initial load
duke
parents:
diff changeset
275 if (door_fd >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 jvm->door_fd = door_fd;
a61af66fc99e Initial load
duke
parents:
diff changeset
277 clear_jvm_error();
a61af66fc99e Initial load
duke
parents:
diff changeset
278 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
279 free(jvm);
a61af66fc99e Initial load
duke
parents:
diff changeset
280 jvm = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
281 }
a61af66fc99e Initial load
duke
parents:
diff changeset
282 return jvm;
a61af66fc99e Initial load
duke
parents:
diff changeset
283 }
a61af66fc99e Initial load
duke
parents:
diff changeset
284
a61af66fc99e Initial load
duke
parents:
diff changeset
285 /* return the last thread local error message */
a61af66fc99e Initial load
duke
parents:
diff changeset
286 const char* jvm_get_last_error() {
a61af66fc99e Initial load
duke
parents:
diff changeset
287 const char* res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
288 thr_getspecific(jvm_error_key, (void**)&res);
a61af66fc99e Initial load
duke
parents:
diff changeset
289 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 }
a61af66fc99e Initial load
duke
parents:
diff changeset
291
a61af66fc99e Initial load
duke
parents:
diff changeset
292 /* detach the givenb JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
293 int jvm_detach(jvm_t* jvm) {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 if (jvm) {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 int res;
a61af66fc99e Initial load
duke
parents:
diff changeset
296 if (jvm->door_fd != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 if (file_close(jvm->door_fd) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 set_jvm_error(JVM_ERR_CANT_CLOSE_DOOR);
a61af66fc99e Initial load
duke
parents:
diff changeset
299 res = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
300 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
301 clear_jvm_error();
a61af66fc99e Initial load
duke
parents:
diff changeset
302 res = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
303 }
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305 free(jvm);
a61af66fc99e Initial load
duke
parents:
diff changeset
306 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
307 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
308 set_jvm_error(JVM_ERR_NULL_PARAM);
a61af66fc99e Initial load
duke
parents:
diff changeset
309 print_debug("jvm_t* is NULL\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
310 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
311 }
a61af66fc99e Initial load
duke
parents:
diff changeset
312 }
a61af66fc99e Initial load
duke
parents:
diff changeset
313
a61af66fc99e Initial load
duke
parents:
diff changeset
314 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
315 * A simple table to translate some known errors into reasonable
a61af66fc99e Initial load
duke
parents:
diff changeset
316 * error messages
a61af66fc99e Initial load
duke
parents:
diff changeset
317 */
a61af66fc99e Initial load
duke
parents:
diff changeset
318 static struct {
a61af66fc99e Initial load
duke
parents:
diff changeset
319 int err;
a61af66fc99e Initial load
duke
parents:
diff changeset
320 const char* msg;
a61af66fc99e Initial load
duke
parents:
diff changeset
321 } const error_messages[] = {
a61af66fc99e Initial load
duke
parents:
diff changeset
322 { 100, "Bad request" },
a61af66fc99e Initial load
duke
parents:
diff changeset
323 { 101, "Protocol mismatch" },
a61af66fc99e Initial load
duke
parents:
diff changeset
324 { 102, "Resource failure" },
a61af66fc99e Initial load
duke
parents:
diff changeset
325 { 103, "Internal error" },
a61af66fc99e Initial load
duke
parents:
diff changeset
326 { 104, "Permission denied" },
a61af66fc99e Initial load
duke
parents:
diff changeset
327 };
a61af66fc99e Initial load
duke
parents:
diff changeset
328
a61af66fc99e Initial load
duke
parents:
diff changeset
329 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
330 * Lookup the given error code and return the appropriate
a61af66fc99e Initial load
duke
parents:
diff changeset
331 * message. If not found return NULL.
a61af66fc99e Initial load
duke
parents:
diff changeset
332 */
a61af66fc99e Initial load
duke
parents:
diff changeset
333 static const char* translate_error(int err) {
a61af66fc99e Initial load
duke
parents:
diff changeset
334 int table_size = sizeof(error_messages) / sizeof(error_messages[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
335 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
336
a61af66fc99e Initial load
duke
parents:
diff changeset
337 for (i=0; i<table_size; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
338 if (err == error_messages[i].err) {
a61af66fc99e Initial load
duke
parents:
diff changeset
339 return error_messages[i].msg;
a61af66fc99e Initial load
duke
parents:
diff changeset
340 }
a61af66fc99e Initial load
duke
parents:
diff changeset
341 }
a61af66fc99e Initial load
duke
parents:
diff changeset
342 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344
a61af66fc99e Initial load
duke
parents:
diff changeset
345 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
346 * Current protocol version
a61af66fc99e Initial load
duke
parents:
diff changeset
347 */
a61af66fc99e Initial load
duke
parents:
diff changeset
348 static const char* PROTOCOL_VERSION = "1";
a61af66fc99e Initial load
duke
parents:
diff changeset
349
a61af66fc99e Initial load
duke
parents:
diff changeset
350 #define RES_BUF_SIZE 128
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
353 * Enqueue attach-on-demand command to the given JVM
a61af66fc99e Initial load
duke
parents:
diff changeset
354 */
a61af66fc99e Initial load
duke
parents:
diff changeset
355 static
a61af66fc99e Initial load
duke
parents:
diff changeset
356 int enqueue_command(jvm_t* jvm, const char* cstr, int arg_count, const char** args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
357 size_t size;
a61af66fc99e Initial load
duke
parents:
diff changeset
358 door_arg_t door_args;
a61af66fc99e Initial load
duke
parents:
diff changeset
359 char res_buffer[RES_BUF_SIZE];
a61af66fc99e Initial load
duke
parents:
diff changeset
360 int rc, i;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 char* buf = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
362 int result = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
363
a61af66fc99e Initial load
duke
parents:
diff changeset
364 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
365 * First we get the command string and create the start of the
a61af66fc99e Initial load
duke
parents:
diff changeset
366 * argument string to send to the target VM:
a61af66fc99e Initial load
duke
parents:
diff changeset
367 * <ver>\0<cmd>\0
a61af66fc99e Initial load
duke
parents:
diff changeset
368 */
a61af66fc99e Initial load
duke
parents:
diff changeset
369 if (cstr == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
370 print_debug("command name is NULL\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
371 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
372 }
a61af66fc99e Initial load
duke
parents:
diff changeset
373 size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
374 buf = (char*)malloc(size);
a61af66fc99e Initial load
duke
parents:
diff changeset
375 if (buf != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
376 char* pos = buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 strcpy(buf, PROTOCOL_VERSION);
a61af66fc99e Initial load
duke
parents:
diff changeset
378 pos += strlen(PROTOCOL_VERSION)+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
379 strcpy(pos, cstr);
a61af66fc99e Initial load
duke
parents:
diff changeset
380 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
381 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
a61af66fc99e Initial load
duke
parents:
diff changeset
382 print_debug("malloc failed at %d in %s\n", __LINE__, __FILE__);
a61af66fc99e Initial load
duke
parents:
diff changeset
383 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
384 }
a61af66fc99e Initial load
duke
parents:
diff changeset
385
a61af66fc99e Initial load
duke
parents:
diff changeset
386 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
387 * Next we iterate over the arguments and extend the buffer
a61af66fc99e Initial load
duke
parents:
diff changeset
388 * to include them.
a61af66fc99e Initial load
duke
parents:
diff changeset
389 */
a61af66fc99e Initial load
duke
parents:
diff changeset
390 for (i=0; i<arg_count; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
391 cstr = args[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
392 if (cstr != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 size_t len = strlen(cstr);
a61af66fc99e Initial load
duke
parents:
diff changeset
394 char* newbuf = (char*)realloc(buf, size+len+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
395 if (newbuf == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
396 set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
a61af66fc99e Initial load
duke
parents:
diff changeset
397 print_debug("realloc failed in %s at %d\n", __FILE__, __LINE__);
a61af66fc99e Initial load
duke
parents:
diff changeset
398 goto quit;
a61af66fc99e Initial load
duke
parents:
diff changeset
399 }
a61af66fc99e Initial load
duke
parents:
diff changeset
400 buf = newbuf;
a61af66fc99e Initial load
duke
parents:
diff changeset
401 strcpy(buf+size, cstr);
a61af66fc99e Initial load
duke
parents:
diff changeset
402 size += len+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
403 }
a61af66fc99e Initial load
duke
parents:
diff changeset
404 }
a61af66fc99e Initial load
duke
parents:
diff changeset
405
a61af66fc99e Initial load
duke
parents:
diff changeset
406 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
407 * The arguments to the door function are in 'buf' so we now
a61af66fc99e Initial load
duke
parents:
diff changeset
408 * do the door call
a61af66fc99e Initial load
duke
parents:
diff changeset
409 */
a61af66fc99e Initial load
duke
parents:
diff changeset
410 door_args.data_ptr = buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
411 door_args.data_size = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
412 door_args.desc_ptr = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
413 door_args.desc_num = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
414 door_args.rbuf = (char*)&res_buffer;
a61af66fc99e Initial load
duke
parents:
diff changeset
415 door_args.rsize = sizeof(res_buffer);
a61af66fc99e Initial load
duke
parents:
diff changeset
416
a61af66fc99e Initial load
duke
parents:
diff changeset
417 RESTARTABLE(door_call(jvm->door_fd, &door_args), rc);
a61af66fc99e Initial load
duke
parents:
diff changeset
418
a61af66fc99e Initial load
duke
parents:
diff changeset
419 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
420 * door_call failed
a61af66fc99e Initial load
duke
parents:
diff changeset
421 */
a61af66fc99e Initial load
duke
parents:
diff changeset
422 if (rc == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 print_debug("door_call failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
424 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
426 * door_call succeeded but the call didn't return the the expected jint.
a61af66fc99e Initial load
duke
parents:
diff changeset
427 */
a61af66fc99e Initial load
duke
parents:
diff changeset
428 if (door_args.data_size < sizeof(int)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
429 print_debug("Enqueue error - reason unknown as result is truncated!");
a61af66fc99e Initial load
duke
parents:
diff changeset
430 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
431 int* res = (int*)(door_args.data_ptr);
a61af66fc99e Initial load
duke
parents:
diff changeset
432 if (*res != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
433 const char* msg = translate_error(*res);
a61af66fc99e Initial load
duke
parents:
diff changeset
434 if (msg == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
435 print_debug("Unable to enqueue command to target VM: %d\n", *res);
a61af66fc99e Initial load
duke
parents:
diff changeset
436 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
437 print_debug("Unable to enqueue command to target VM: %s\n", msg);
a61af66fc99e Initial load
duke
parents:
diff changeset
438 }
a61af66fc99e Initial load
duke
parents:
diff changeset
439 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
440 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
441 * The door call should return a file descriptor to one end of
a61af66fc99e Initial load
duke
parents:
diff changeset
442 * a socket pair
a61af66fc99e Initial load
duke
parents:
diff changeset
443 */
a61af66fc99e Initial load
duke
parents:
diff changeset
444 if ((door_args.desc_ptr != NULL) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
445 (door_args.desc_num == 1) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
446 (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
447 result = door_args.desc_ptr->d_data.d_desc.d_descriptor;
a61af66fc99e Initial load
duke
parents:
diff changeset
448 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 print_debug("Reply from enqueue missing descriptor!\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
450 }
a61af66fc99e Initial load
duke
parents:
diff changeset
451 }
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 quit:
a61af66fc99e Initial load
duke
parents:
diff changeset
456 if (buf) free(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
457 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
458 }
a61af66fc99e Initial load
duke
parents:
diff changeset
459
a61af66fc99e Initial load
duke
parents:
diff changeset
460 /* read status code for a door command */
a61af66fc99e Initial load
duke
parents:
diff changeset
461 static int read_status(int fd) {
a61af66fc99e Initial load
duke
parents:
diff changeset
462 char ch, buf[16];
a61af66fc99e Initial load
duke
parents:
diff changeset
463 int index = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465 while (1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
466 if (file_read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
467 set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS);
a61af66fc99e Initial load
duke
parents:
diff changeset
468 print_debug("door cmd status: read status failed\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
469 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
470 }
a61af66fc99e Initial load
duke
parents:
diff changeset
471 buf[index++] = ch;
a61af66fc99e Initial load
duke
parents:
diff changeset
472 if (ch == '\n') {
a61af66fc99e Initial load
duke
parents:
diff changeset
473 buf[index - 1] = '\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
474 return atoi(buf);
a61af66fc99e Initial load
duke
parents:
diff changeset
475 }
a61af66fc99e Initial load
duke
parents:
diff changeset
476 if (index == sizeof(buf)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
477 set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS);
a61af66fc99e Initial load
duke
parents:
diff changeset
478 print_debug("door cmd status: read status overflow\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
479 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 }
a61af66fc99e Initial load
duke
parents:
diff changeset
481 }
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }
a61af66fc99e Initial load
duke
parents:
diff changeset
483
a61af66fc99e Initial load
duke
parents:
diff changeset
484 static const char* ENABLE_DPROBES_CMD = "enabledprobes";
a61af66fc99e Initial load
duke
parents:
diff changeset
485
a61af66fc99e Initial load
duke
parents:
diff changeset
486 /* enable one or more DTrace probes for a given JVM */
a61af66fc99e Initial load
duke
parents:
diff changeset
487 int jvm_enable_dtprobes(jvm_t* jvm, int num_probe_types, const char** probe_types) {
a61af66fc99e Initial load
duke
parents:
diff changeset
488 int fd, status = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
489 char ch;
a61af66fc99e Initial load
duke
parents:
diff changeset
490 const char* args[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
491 char buf[16];
a61af66fc99e Initial load
duke
parents:
diff changeset
492 int probe_type = 0, index;
a61af66fc99e Initial load
duke
parents:
diff changeset
493 int count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
494
a61af66fc99e Initial load
duke
parents:
diff changeset
495 if (jvm == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
496 set_jvm_error(JVM_ERR_NULL_PARAM);
a61af66fc99e Initial load
duke
parents:
diff changeset
497 print_debug("jvm_t* is NULL\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
498 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
499 }
a61af66fc99e Initial load
duke
parents:
diff changeset
500
a61af66fc99e Initial load
duke
parents:
diff changeset
501 if (num_probe_types == 0 || probe_types == NULL ||
a61af66fc99e Initial load
duke
parents:
diff changeset
502 probe_types[0] == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
503 set_jvm_error(JVM_ERR_INVALID_PARAM);
a61af66fc99e Initial load
duke
parents:
diff changeset
504 print_debug("invalid probe type argument(s)\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
505 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
506 }
a61af66fc99e Initial load
duke
parents:
diff changeset
507
a61af66fc99e Initial load
duke
parents:
diff changeset
508 for (index = 0; index < num_probe_types; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
509 const char* p = probe_types[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
510 if (strcmp(p, JVM_DTPROBE_OBJECT_ALLOC) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
511 probe_type |= DTRACE_ALLOC_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
512 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
513 } else if (strcmp(p, JVM_DTPROBE_METHOD_ENTRY) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
514 strcmp(p, JVM_DTPROBE_METHOD_RETURN) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
515 probe_type |= DTRACE_METHOD_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
516 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
517 } else if (strcmp(p, JVM_DTPROBE_MONITOR_ENTER) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
518 strcmp(p, JVM_DTPROBE_MONITOR_ENTERED) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
519 strcmp(p, JVM_DTPROBE_MONITOR_EXIT) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
520 strcmp(p, JVM_DTPROBE_MONITOR_WAIT) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
521 strcmp(p, JVM_DTPROBE_MONITOR_WAITED) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
522 strcmp(p, JVM_DTPROBE_MONITOR_NOTIFY) == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
523 strcmp(p, JVM_DTPROBE_MONITOR_NOTIFYALL) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
524 probe_type |= DTRACE_MONITOR_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
525 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
526 } else if (strcmp(p, JVM_DTPROBE_ALL) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
527 probe_type |= DTRACE_ALL_PROBES;
a61af66fc99e Initial load
duke
parents:
diff changeset
528 count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
529 }
a61af66fc99e Initial load
duke
parents:
diff changeset
530 }
a61af66fc99e Initial load
duke
parents:
diff changeset
531
a61af66fc99e Initial load
duke
parents:
diff changeset
532 if (count == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
533 return count;
a61af66fc99e Initial load
duke
parents:
diff changeset
534 }
a61af66fc99e Initial load
duke
parents:
diff changeset
535 sprintf(buf, "%d", probe_type);
a61af66fc99e Initial load
duke
parents:
diff changeset
536 args[0] = buf;
a61af66fc99e Initial load
duke
parents:
diff changeset
537
a61af66fc99e Initial load
duke
parents:
diff changeset
538 fd = enqueue_command(jvm, ENABLE_DPROBES_CMD, 1, args);
a61af66fc99e Initial load
duke
parents:
diff changeset
539 if (fd < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
540 set_jvm_error(JVM_ERR_DOOR_CMD_SEND);
a61af66fc99e Initial load
duke
parents:
diff changeset
541 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
542 }
a61af66fc99e Initial load
duke
parents:
diff changeset
543
a61af66fc99e Initial load
duke
parents:
diff changeset
544 status = read_status(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
545 // non-zero status is error
a61af66fc99e Initial load
duke
parents:
diff changeset
546 if (status) {
a61af66fc99e Initial load
duke
parents:
diff changeset
547 set_jvm_error(JVM_ERR_DOOR_CMD_STATUS);
a61af66fc99e Initial load
duke
parents:
diff changeset
548 print_debug("%s command failed (status: %d) in target JVM\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
549 ENABLE_DPROBES_CMD, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
550 file_close(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
551 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
552 }
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // read from stream until EOF
a61af66fc99e Initial load
duke
parents:
diff changeset
554 while (file_read(fd, &ch, sizeof(ch)) == sizeof(ch)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
555 if (libjvm_dtrace_debug) {
a61af66fc99e Initial load
duke
parents:
diff changeset
556 printf("%c", ch);
a61af66fc99e Initial load
duke
parents:
diff changeset
557 }
a61af66fc99e Initial load
duke
parents:
diff changeset
558 }
a61af66fc99e Initial load
duke
parents:
diff changeset
559
a61af66fc99e Initial load
duke
parents:
diff changeset
560 file_close(fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
561 clear_jvm_error();
a61af66fc99e Initial load
duke
parents:
diff changeset
562 return count;
a61af66fc99e Initial load
duke
parents:
diff changeset
563 }