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