jvm_dtrace.c revision 5776:de6a9e811145
148391Speter/* 248391Speter * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. 348391Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 448391Speter * 548391Speter * This code is free software; you can redistribute it and/or modify it 648391Speter * under the terms of the GNU General Public License version 2 only, as 748391Speter * published by the Free Software Foundation. 848391Speter * 948391Speter * This code is distributed in the hope that it will be useful, but WITHOUT 1048391Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1148391Speter * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1248391Speter * version 2 for more details (a copy is included in the LICENSE file that 1348391Speter * accompanied this code). 1448391Speter * 1548391Speter * You should have received a copy of the GNU General Public License version 1648391Speter * 2 along with this work; if not, write to the Free Software Foundation, 1748391Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1848391Speter * 1948391Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2048391Speter * or visit www.oracle.com if you need additional information or have any 2148391Speter * questions. 2248391Speter * 2348391Speter */ 2448391Speter 2548391Speter#include <door.h> 2648391Speter#include <errno.h> 2748391Speter#include <fcntl.h> 2848391Speter#include <limits.h> 2948391Speter#include <poll.h> 3048391Speter#include <signal.h> 3148391Speter#include <stdarg.h> 3270317Sjake#include <stdio.h> 3374927Sjhb#include <stdlib.h> 3474927Sjhb#include <string.h> 3555722Simp#include <sys/types.h> 3655539Sluoqi#include <sys/stat.h> 3774927Sjhb#include <thread.h> 3848391Speter#include <unistd.h> 3948391Speter#include "jvm_dtrace.h" 4048391Speter 4148391Speter// NOTE: These constants are used in JVM code as well. 4248391Speter// KEEP JVM CODE IN SYNC if you are going to change these... 4348391Speter 4448391Speter#define DTRACE_ALLOC_PROBES 0x1 4548391Speter#define DTRACE_METHOD_PROBES 0x2 4648391Speter#define DTRACE_MONITOR_PROBES 0x4 4748391Speter#define DTRACE_ALL_PROBES -1 4848391Speter 4948391Speter// generic error messages 5048391Speter#define JVM_ERR_OUT_OF_MEMORY "out of memory (native heap)" 5148391Speter#define JVM_ERR_INVALID_PARAM "invalid input parameter(s)" 5248391Speter#define JVM_ERR_NULL_PARAM "input paramater is NULL" 5348391Speter 5448391Speter// error messages for attach 5548391Speter#define JVM_ERR_CANT_OPEN_DOOR "cannot open door file" 5648391Speter#define JVM_ERR_CANT_CREATE_ATTACH_FILE "cannot create attach file" 5748391Speter#define JVM_ERR_DOOR_FILE_PERMISSION "door file is not secure" 58104354Sscottl#define JVM_ERR_CANT_SIGNAL "cannot send SIGQUIT to target" 5948391Speter 6048391Speter// error messages for enable probe 6148391Speter#define JVM_ERR_DOOR_CMD_SEND "door command send failed" 6248391Speter#define JVM_ERR_DOOR_CANT_READ_STATUS "cannot read door command status" 6348391Speter#define JVM_ERR_DOOR_CMD_STATUS "door command error status" 6465557Sjasone 6548391Speter// error message for detach 6665557Sjasone#define JVM_ERR_CANT_CLOSE_DOOR "cannot close door file" 6765557Sjasone 6865557Sjasone#define RESTARTABLE(_cmd, _result) do { \ 6965557Sjasone do { \ 7065557Sjasone _result = _cmd; \ 7165557Sjasone } while((_result == -1) && (errno == EINTR)); \ 7248391Speter} while(0) 7348391Speter 7448391Speterstruct _jvm_t { 75104354Sscottl pid_t pid; 7648391Speter int door_fd; 7748391Speter}; 7848391Speter 79103216Sjulianstatic int libjvm_dtrace_debug; 8048391Speterstatic void print_debug(const char* fmt,...) { 8148391Speter if (libjvm_dtrace_debug) { 8265557Sjasone va_list alist; 8365557Sjasone va_start(alist, fmt); 8465557Sjasone fputs("libjvm_dtrace DEBUG: ", stderr); 8590375Speter vfprintf(stderr, fmt, alist); 86104354Sscottl va_end(alist); 8748391Speter } 8848391Speter} 8948391Speter 9048391Speter/* Key for thread local error message */ 9148391Speterstatic thread_key_t jvm_error_key; 9248391Speter 9348391Speter/* init function for this library */ 9448391Speterstatic void init_jvm_dtrace() { 9571559Sjhb /* check for env. var for debug mode */ 9671559Sjhb libjvm_dtrace_debug = getenv("LIBJVM_DTRACE_DEBUG") != NULL; 9752140Sluoqi /* create key for thread local error message */ 9873911Sjhb if (thr_keycreate(&jvm_error_key, NULL) != 0) { 9971559Sjhb print_debug("can't create thread_key_t for jvm error key\n"); 10048391Speter // exit(1); ? 10148391Speter } 10248391Speter} 10348391Speter 10448391Speter#pragma init(init_jvm_dtrace) 10548391Speter 10648391Speter/* set thread local error message */ 107103216Sjulianstatic void set_jvm_error(const char* msg) { 108103216Sjulian thr_setspecific(jvm_error_key, (void*)msg); 109103216Sjulian} 11048391Speter 11169657Sjhb/* clear thread local error message */ 11272200Sbmilekicstatic void clear_jvm_error() { 11371559Sjhb thr_setspecific(jvm_error_key, NULL); 11469657Sjhb} 115103216Sjulian 11669657Sjhb/* file handling functions that can handle interrupt */ 11772200Sbmilekic 11869657Sjhbstatic int file_open(const char* path, int flag) { 11948391Speter int ret; 12048391Speter RESTARTABLE(open(path, flag), ret); 12148391Speter return ret; 12248391Speter} 12348391Speter 12448391Speterstatic int file_close(int fd) { 12586293Speter return close(fd); 12686293Speter} 12770317Sjake 12886293Speterstatic int file_read(int fd, char* buf, int len) { 12986293Speter int ret; 13074927Sjhb RESTARTABLE(read(fd, buf, len), ret); 13186292Sdillon return ret; 13286292Sdillon} 13386292Sdillon 13474927Sjhb/* send SIGQUIT signal to given process */ 13586293Speterstatic int send_sigquit(pid_t pid) { 13648391Speter int ret; 13748391Speter RESTARTABLE(kill(pid, SIGQUIT), ret); 13855539Sluoqi return ret; 13955539Sluoqi} 14055539Sluoqi 14155539Sluoqi/* called to check permissions on attach file */ 14255539Sluoqistatic int check_permission(const char* path) { 14370063Sjhb struct stat64 sb; 14455539Sluoqi uid_t uid, gid; 14555539Sluoqi int res; 14655539Sluoqi 147104306Sjmallett /* 14855539Sluoqi * Check that the path is owned by the effective uid/gid of this 14973911Sjhb * process. Also check that group/other access is not allowed. 15073911Sjhb */ 15173911Sjhb uid = geteuid(); 15255539Sluoqi gid = getegid(); 15373911Sjhb 154104306Sjmallett res = stat64(path, &sb); 15588160Speter if (res != 0) { 156104306Sjmallett print_debug("stat failed for %s\n", path); 15755539Sluoqi return -1; 15855539Sluoqi } 15955539Sluoqi 16070063Sjhb if ((sb.st_uid != uid) || (sb.st_gid != gid) || 16155539Sluoqi ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0)) { 16255539Sluoqi print_debug("well-known file %s is not secure\n", path); 16355539Sluoqi return -1; 16455539Sluoqi } 16555539Sluoqi return 0; 16673911Sjhb} 16773911Sjhb 16873911Sjhb#define ATTACH_FILE_PATTERN "/tmp/.attach_pid%d" 16955539Sluoqi 17073911Sjhb/* fill-in the name of attach file name in given buffer */ 171104306Sjmallettstatic void fill_attach_file_name(char* path, int len, pid_t pid) { 17273911Sjhb memset(path, 0, len); 173104306Sjmallett sprintf(path, ATTACH_FILE_PATTERN, pid); 17455539Sluoqi} 17555539Sluoqi 17655539Sluoqi#define DOOR_FILE_PATTERN "/tmp/.java_pid%d" 17755539Sluoqi 17870063Sjhb/* open door file for the given JVM */ 17955539Sluoqistatic int open_door(pid_t pid) { 18073911Sjhb char path[PATH_MAX + 1]; 181104306Sjmallett int fd; 182104306Sjmallett 183104306Sjmallett sprintf(path, DOOR_FILE_PATTERN, pid); 18455539Sluoqi fd = file_open(path, O_RDONLY); 18573911Sjhb if (fd < 0) { 18655539Sluoqi set_jvm_error(JVM_ERR_CANT_OPEN_DOOR); 187 print_debug("cannot open door file %s\n", path); 188 return -1; 189 } 190 print_debug("opened door file %s\n", path); 191 if (check_permission(path) != 0) { 192 set_jvm_error(JVM_ERR_DOOR_FILE_PERMISSION); 193 print_debug("check permission failed for %s\n", path); 194 file_close(fd); 195 fd = -1; 196 } 197 return fd; 198} 199 200/* create attach file for given process */ 201static int create_attach_file(pid_t pid) { 202 char path[PATH_MAX + 1]; 203 int fd; 204 fill_attach_file_name(path, sizeof(path), pid); 205 fd = file_open(path, O_CREAT | O_RDWR); 206 if (fd < 0) { 207 set_jvm_error(JVM_ERR_CANT_CREATE_ATTACH_FILE); 208 print_debug("cannot create file %s\n", path); 209 } else { 210 print_debug("created attach file %s\n", path); 211 } 212 return fd; 213} 214 215/* delete attach file for given process */ 216static void delete_attach_file(pid_t pid) { 217 char path[PATH_MAX + 1]; 218 fill_attach_file_name(path, sizeof(path), pid); 219 int res = unlink(path); 220 if (res) { 221 print_debug("cannot delete attach file %s\n", path); 222 } else { 223 print_debug("deleted attach file %s\n", path); 224 } 225} 226 227/* attach to given JVM */ 228jvm_t* jvm_attach(pid_t pid) { 229 jvm_t* jvm; 230 int door_fd, attach_fd, i; 231 232 jvm = (jvm_t*) calloc(1, sizeof(jvm_t)); 233 if (jvm == NULL) { 234 set_jvm_error(JVM_ERR_OUT_OF_MEMORY); 235 print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__); 236 return NULL; 237 } 238 jvm->pid = pid; 239 attach_fd = -1; 240 241 door_fd = open_door(pid); 242 if (door_fd < 0) { 243 print_debug("trying to create attach file\n"); 244 if ((attach_fd = create_attach_file(pid)) < 0) { 245 goto quit; 246 } 247 248 /* send QUIT signal to the target so that it will 249 * check for the attach file. 250 */ 251 if (send_sigquit(pid) != 0) { 252 set_jvm_error(JVM_ERR_CANT_SIGNAL); 253 print_debug("sending SIGQUIT failed\n"); 254 goto quit; 255 } 256 257 /* give the target VM time to start the attach mechanism */ 258 do { 259 int res; 260 RESTARTABLE(poll(0, 0, 200), res); 261 door_fd = open_door(pid); 262 i++; 263 } while (i <= 50 && door_fd == -1); 264 if (door_fd < 0) { 265 print_debug("Unable to open door to process %d\n", pid); 266 goto quit; 267 } 268 } 269 270quit: 271 if (attach_fd >= 0) { 272 file_close(attach_fd); 273 delete_attach_file(jvm->pid); 274 } 275 if (door_fd >= 0) { 276 jvm->door_fd = door_fd; 277 clear_jvm_error(); 278 } else { 279 free(jvm); 280 jvm = NULL; 281 } 282 return jvm; 283} 284 285/* return the last thread local error message */ 286const char* jvm_get_last_error() { 287 const char* res = NULL; 288 thr_getspecific(jvm_error_key, (void**)&res); 289 return res; 290} 291 292/* detach the givenb JVM */ 293int jvm_detach(jvm_t* jvm) { 294 if (jvm) { 295 int res; 296 if (jvm->door_fd != -1) { 297 if (file_close(jvm->door_fd) != 0) { 298 set_jvm_error(JVM_ERR_CANT_CLOSE_DOOR); 299 res = -1; 300 } else { 301 clear_jvm_error(); 302 res = 0; 303 } 304 } 305 free(jvm); 306 return res; 307 } else { 308 set_jvm_error(JVM_ERR_NULL_PARAM); 309 print_debug("jvm_t* is NULL\n"); 310 return -1; 311 } 312} 313 314/* 315 * A simple table to translate some known errors into reasonable 316 * error messages 317 */ 318static struct { 319 int err; 320 const char* msg; 321} const error_messages[] = { 322 { 100, "Bad request" }, 323 { 101, "Protocol mismatch" }, 324 { 102, "Resource failure" }, 325 { 103, "Internal error" }, 326 { 104, "Permission denied" }, 327}; 328 329/* 330 * Lookup the given error code and return the appropriate 331 * message. If not found return NULL. 332 */ 333static const char* translate_error(int err) { 334 int table_size = sizeof(error_messages) / sizeof(error_messages[0]); 335 int i; 336 337 for (i=0; i<table_size; i++) { 338 if (err == error_messages[i].err) { 339 return error_messages[i].msg; 340 } 341 } 342 return NULL; 343} 344 345/* 346 * Current protocol version 347 */ 348static const char* PROTOCOL_VERSION = "1"; 349 350#define RES_BUF_SIZE 128 351 352/* 353 * Enqueue attach-on-demand command to the given JVM 354 */ 355static 356int enqueue_command(jvm_t* jvm, const char* cstr, int arg_count, const char** args) { 357 size_t size; 358 door_arg_t door_args; 359 char res_buffer[RES_BUF_SIZE]; 360 int rc, i; 361 char* buf = NULL; 362 int result = -1; 363 364 /* 365 * First we get the command string and create the start of the 366 * argument string to send to the target VM: 367 * <ver>\0<cmd>\0 368 */ 369 if (cstr == NULL) { 370 print_debug("command name is NULL\n"); 371 goto quit; 372 } 373 size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2; 374 buf = (char*)malloc(size); 375 if (buf != NULL) { 376 char* pos = buf; 377 strcpy(buf, PROTOCOL_VERSION); 378 pos += strlen(PROTOCOL_VERSION)+1; 379 strcpy(pos, cstr); 380 } else { 381 set_jvm_error(JVM_ERR_OUT_OF_MEMORY); 382 print_debug("malloc failed at %d in %s\n", __LINE__, __FILE__); 383 goto quit; 384 } 385 386 /* 387 * Next we iterate over the arguments and extend the buffer 388 * to include them. 389 */ 390 for (i=0; i<arg_count; i++) { 391 cstr = args[i]; 392 if (cstr != NULL) { 393 size_t len = strlen(cstr); 394 char* newbuf = (char*)realloc(buf, size+len+1); 395 if (newbuf == NULL) { 396 set_jvm_error(JVM_ERR_OUT_OF_MEMORY); 397 print_debug("realloc failed in %s at %d\n", __FILE__, __LINE__); 398 goto quit; 399 } 400 buf = newbuf; 401 strcpy(buf+size, cstr); 402 size += len+1; 403 } 404 } 405 406 /* 407 * The arguments to the door function are in 'buf' so we now 408 * do the door call 409 */ 410 door_args.data_ptr = buf; 411 door_args.data_size = size; 412 door_args.desc_ptr = NULL; 413 door_args.desc_num = 0; 414 door_args.rbuf = (char*)&res_buffer; 415 door_args.rsize = sizeof(res_buffer); 416 417 RESTARTABLE(door_call(jvm->door_fd, &door_args), rc); 418 419 /* 420 * door_call failed 421 */ 422 if (rc == -1) { 423 print_debug("door_call failed\n"); 424 } else { 425 /* 426 * door_call succeeded but the call didn't return the the expected jint. 427 */ 428 if (door_args.data_size < sizeof(int)) { 429 print_debug("Enqueue error - reason unknown as result is truncated!"); 430 } else { 431 int* res = (int*)(door_args.data_ptr); 432 if (*res != 0) { 433 const char* msg = translate_error(*res); 434 if (msg == NULL) { 435 print_debug("Unable to enqueue command to target VM: %d\n", *res); 436 } else { 437 print_debug("Unable to enqueue command to target VM: %s\n", msg); 438 } 439 } else { 440 /* 441 * The door call should return a file descriptor to one end of 442 * a socket pair 443 */ 444 if ((door_args.desc_ptr != NULL) && 445 (door_args.desc_num == 1) && 446 (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) { 447 result = door_args.desc_ptr->d_data.d_desc.d_descriptor; 448 } else { 449 print_debug("Reply from enqueue missing descriptor!\n"); 450 } 451 } 452 } 453 } 454 455quit: 456 if (buf) free(buf); 457 return result; 458} 459 460/* read status code for a door command */ 461static int read_status(int fd) { 462 char ch, buf[16]; 463 int index = 0; 464 465 while (1) { 466 if (file_read(fd, &ch, sizeof(ch)) != sizeof(ch)) { 467 set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS); 468 print_debug("door cmd status: read status failed\n"); 469 return -1; 470 } 471 buf[index++] = ch; 472 if (ch == '\n') { 473 buf[index - 1] = '\0'; 474 return atoi(buf); 475 } 476 if (index == sizeof(buf)) { 477 set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS); 478 print_debug("door cmd status: read status overflow\n"); 479 return -1; 480 } 481 } 482} 483 484static const char* ENABLE_DPROBES_CMD = "enabledprobes"; 485 486/* enable one or more DTrace probes for a given JVM */ 487int jvm_enable_dtprobes(jvm_t* jvm, int num_probe_types, const char** probe_types) { 488 int fd, status = 0; 489 char ch; 490 const char* args[1]; 491 char buf[16]; 492 int probe_type = 0, index; 493 int count = 0; 494 495 if (jvm == NULL) { 496 set_jvm_error(JVM_ERR_NULL_PARAM); 497 print_debug("jvm_t* is NULL\n"); 498 return -1; 499 } 500 501 if (num_probe_types == 0 || probe_types == NULL || 502 probe_types[0] == NULL) { 503 set_jvm_error(JVM_ERR_INVALID_PARAM); 504 print_debug("invalid probe type argument(s)\n"); 505 return -1; 506 } 507 508 for (index = 0; index < num_probe_types; index++) { 509 const char* p = probe_types[index]; 510 if (strcmp(p, JVM_DTPROBE_OBJECT_ALLOC) == 0) { 511 probe_type |= DTRACE_ALLOC_PROBES; 512 count++; 513 } else if (strcmp(p, JVM_DTPROBE_METHOD_ENTRY) == 0 || 514 strcmp(p, JVM_DTPROBE_METHOD_RETURN) == 0) { 515 probe_type |= DTRACE_METHOD_PROBES; 516 count++; 517 } else if (strcmp(p, JVM_DTPROBE_MONITOR_ENTER) == 0 || 518 strcmp(p, JVM_DTPROBE_MONITOR_ENTERED) == 0 || 519 strcmp(p, JVM_DTPROBE_MONITOR_EXIT) == 0 || 520 strcmp(p, JVM_DTPROBE_MONITOR_WAIT) == 0 || 521 strcmp(p, JVM_DTPROBE_MONITOR_WAITED) == 0 || 522 strcmp(p, JVM_DTPROBE_MONITOR_NOTIFY) == 0 || 523 strcmp(p, JVM_DTPROBE_MONITOR_NOTIFYALL) == 0) { 524 probe_type |= DTRACE_MONITOR_PROBES; 525 count++; 526 } else if (strcmp(p, JVM_DTPROBE_ALL) == 0) { 527 probe_type |= DTRACE_ALL_PROBES; 528 count++; 529 } 530 } 531 532 if (count == 0) { 533 return count; 534 } 535 sprintf(buf, "%d", probe_type); 536 args[0] = buf; 537 538 fd = enqueue_command(jvm, ENABLE_DPROBES_CMD, 1, args); 539 if (fd < 0) { 540 set_jvm_error(JVM_ERR_DOOR_CMD_SEND); 541 return -1; 542 } 543 544 status = read_status(fd); 545 // non-zero status is error 546 if (status) { 547 set_jvm_error(JVM_ERR_DOOR_CMD_STATUS); 548 print_debug("%s command failed (status: %d) in target JVM\n", 549 ENABLE_DPROBES_CMD, status); 550 file_close(fd); 551 return -1; 552 } 553 // read from stream until EOF 554 while (file_read(fd, &ch, sizeof(ch)) == sizeof(ch)) { 555 if (libjvm_dtrace_debug) { 556 printf("%c", ch); 557 } 558 } 559 560 file_close(fd); 561 clear_jvm_error(); 562 return count; 563} 564