0N/A/*
1879N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A#include <door.h>
0N/A#include <errno.h>
0N/A#include <fcntl.h>
0N/A#include <limits.h>
0N/A#include <poll.h>
0N/A#include <signal.h>
0N/A#include <stdarg.h>
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <sys/types.h>
0N/A#include <sys/stat.h>
0N/A#include <thread.h>
0N/A#include <unistd.h>
0N/A#include "jvm_dtrace.h"
0N/A
0N/A// NOTE: These constants are used in JVM code as well.
0N/A// KEEP JVM CODE IN SYNC if you are going to change these...
0N/A
0N/A#define DTRACE_ALLOC_PROBES 0x1
0N/A#define DTRACE_METHOD_PROBES 0x2
0N/A#define DTRACE_MONITOR_PROBES 0x4
0N/A#define DTRACE_ALL_PROBES -1
0N/A
0N/A// generic error messages
0N/A#define JVM_ERR_OUT_OF_MEMORY "out of memory (native heap)"
0N/A#define JVM_ERR_INVALID_PARAM "invalid input parameter(s)"
0N/A#define JVM_ERR_NULL_PARAM "input paramater is NULL"
0N/A
0N/A// error messages for attach
0N/A#define JVM_ERR_CANT_OPEN_DOOR "cannot open door file"
0N/A#define JVM_ERR_CANT_CREATE_ATTACH_FILE "cannot create attach file"
0N/A#define JVM_ERR_DOOR_FILE_PERMISSION "door file is not secure"
0N/A#define JVM_ERR_CANT_SIGNAL "cannot send SIGQUIT to target"
0N/A
0N/A// error messages for enable probe
0N/A#define JVM_ERR_DOOR_CMD_SEND "door command send failed"
0N/A#define JVM_ERR_DOOR_CANT_READ_STATUS "cannot read door command status"
0N/A#define JVM_ERR_DOOR_CMD_STATUS "door command error status"
0N/A
0N/A// error message for detach
0N/A#define JVM_ERR_CANT_CLOSE_DOOR "cannot close door file"
0N/A
0N/A#define RESTARTABLE(_cmd, _result) do { \
0N/A do { \
0N/A _result = _cmd; \
0N/A } while((_result == -1) && (errno == EINTR)); \
0N/A} while(0)
0N/A
0N/Astruct _jvm_t {
0N/A pid_t pid;
0N/A int door_fd;
0N/A};
0N/A
0N/Astatic int libjvm_dtrace_debug;
0N/Astatic void print_debug(const char* fmt,...) {
0N/A if (libjvm_dtrace_debug) {
0N/A va_list alist;
0N/A va_start(alist, fmt);
0N/A fputs("libjvm_dtrace DEBUG: ", stderr);
0N/A vfprintf(stderr, fmt, alist);
0N/A va_end(alist);
0N/A }
0N/A}
0N/A
0N/A/* Key for thread local error message */
0N/Astatic thread_key_t jvm_error_key;
0N/A
0N/A/* init function for this library */
0N/Astatic void init_jvm_dtrace() {
0N/A /* check for env. var for debug mode */
0N/A libjvm_dtrace_debug = getenv("LIBJVM_DTRACE_DEBUG") != NULL;
0N/A /* create key for thread local error message */
0N/A if (thr_keycreate(&jvm_error_key, NULL) != 0) {
0N/A print_debug("can't create thread_key_t for jvm error key\n");
0N/A // exit(1); ?
0N/A }
0N/A}
0N/A
0N/A#pragma init(init_jvm_dtrace)
0N/A
0N/A/* set thread local error message */
0N/Astatic void set_jvm_error(const char* msg) {
0N/A thr_setspecific(jvm_error_key, (void*)msg);
0N/A}
0N/A
0N/A/* clear thread local error message */
0N/Astatic void clear_jvm_error() {
0N/A thr_setspecific(jvm_error_key, NULL);
0N/A}
0N/A
0N/A/* file handling functions that can handle interrupt */
0N/A
0N/Astatic int file_open(const char* path, int flag) {
0N/A int ret;
0N/A RESTARTABLE(open(path, flag), ret);
0N/A return ret;
0N/A}
0N/A
0N/Astatic int file_close(int fd) {
0N/A int ret;
0N/A RESTARTABLE(close(fd), ret);
0N/A return ret;
0N/A}
0N/A
0N/Astatic int file_read(int fd, char* buf, int len) {
0N/A int ret;
0N/A RESTARTABLE(read(fd, buf, len), ret);
0N/A return ret;
0N/A}
0N/A
0N/A/* send SIGQUIT signal to given process */
0N/Astatic int send_sigquit(pid_t pid) {
0N/A int ret;
0N/A RESTARTABLE(kill(pid, SIGQUIT), ret);
0N/A return ret;
0N/A}
0N/A
0N/A/* called to check permissions on attach file */
0N/Astatic int check_permission(const char* path) {
0N/A struct stat64 sb;
0N/A uid_t uid, gid;
0N/A int res;
0N/A
0N/A /*
0N/A * Check that the path is owned by the effective uid/gid of this
0N/A * process. Also check that group/other access is not allowed.
0N/A */
0N/A uid = geteuid();
0N/A gid = getegid();
0N/A
0N/A res = stat64(path, &sb);
0N/A if (res != 0) {
0N/A print_debug("stat failed for %s\n", path);
0N/A return -1;
0N/A }
0N/A
0N/A if ((sb.st_uid != uid) || (sb.st_gid != gid) ||
0N/A ((sb.st_mode & (S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) != 0)) {
0N/A print_debug("well-known file %s is not secure\n", path);
0N/A return -1;
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/A#define ATTACH_FILE_PATTERN "/tmp/.attach_pid%d"
0N/A
0N/A/* fill-in the name of attach file name in given buffer */
0N/Astatic void fill_attach_file_name(char* path, int len, pid_t pid) {
0N/A memset(path, 0, len);
0N/A sprintf(path, ATTACH_FILE_PATTERN, pid);
0N/A}
0N/A
0N/A#define DOOR_FILE_PATTERN "/tmp/.java_pid%d"
0N/A
0N/A/* open door file for the given JVM */
0N/Astatic int open_door(pid_t pid) {
0N/A char path[PATH_MAX + 1];
0N/A int fd;
0N/A
0N/A sprintf(path, DOOR_FILE_PATTERN, pid);
0N/A fd = file_open(path, O_RDONLY);
0N/A if (fd < 0) {
0N/A set_jvm_error(JVM_ERR_CANT_OPEN_DOOR);
0N/A print_debug("cannot open door file %s\n", path);
0N/A return -1;
0N/A }
0N/A print_debug("opened door file %s\n", path);
0N/A if (check_permission(path) != 0) {
0N/A set_jvm_error(JVM_ERR_DOOR_FILE_PERMISSION);
0N/A print_debug("check permission failed for %s\n", path);
0N/A file_close(fd);
0N/A fd = -1;
0N/A }
0N/A return fd;
0N/A}
0N/A
0N/A/* create attach file for given process */
0N/Astatic int create_attach_file(pid_t pid) {
0N/A char path[PATH_MAX + 1];
0N/A int fd;
0N/A fill_attach_file_name(path, sizeof(path), pid);
0N/A fd = file_open(path, O_CREAT | O_RDWR);
0N/A if (fd < 0) {
0N/A set_jvm_error(JVM_ERR_CANT_CREATE_ATTACH_FILE);
0N/A print_debug("cannot create file %s\n", path);
0N/A } else {
0N/A print_debug("created attach file %s\n", path);
0N/A }
0N/A return fd;
0N/A}
0N/A
0N/A/* delete attach file for given process */
0N/Astatic void delete_attach_file(pid_t pid) {
0N/A char path[PATH_MAX + 1];
0N/A fill_attach_file_name(path, sizeof(path), pid);
0N/A int res = unlink(path);
0N/A if (res) {
0N/A print_debug("cannot delete attach file %s\n", path);
0N/A } else {
0N/A print_debug("deleted attach file %s\n", path);
0N/A }
0N/A}
0N/A
0N/A/* attach to given JVM */
0N/Ajvm_t* jvm_attach(pid_t pid) {
0N/A jvm_t* jvm;
0N/A int door_fd, attach_fd, i;
0N/A
0N/A jvm = (jvm_t*) calloc(1, sizeof(jvm_t));
0N/A if (jvm == NULL) {
0N/A set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
0N/A print_debug("calloc failed in %s at %d\n", __FILE__, __LINE__);
0N/A return NULL;
0N/A }
0N/A jvm->pid = pid;
0N/A attach_fd = -1;
0N/A
0N/A door_fd = open_door(pid);
0N/A if (door_fd < 0) {
0N/A print_debug("trying to create attach file\n");
0N/A if ((attach_fd = create_attach_file(pid)) < 0) {
0N/A goto quit;
0N/A }
0N/A
0N/A /* send QUIT signal to the target so that it will
0N/A * check for the attach file.
0N/A */
0N/A if (send_sigquit(pid) != 0) {
0N/A set_jvm_error(JVM_ERR_CANT_SIGNAL);
0N/A print_debug("sending SIGQUIT failed\n");
0N/A goto quit;
0N/A }
0N/A
0N/A /* give the target VM time to start the attach mechanism */
0N/A do {
0N/A int res;
0N/A RESTARTABLE(poll(0, 0, 200), res);
0N/A door_fd = open_door(pid);
0N/A i++;
0N/A } while (i <= 50 && door_fd == -1);
0N/A if (door_fd < 0) {
0N/A print_debug("Unable to open door to process %d\n", pid);
0N/A goto quit;
0N/A }
0N/A }
0N/A
0N/Aquit:
0N/A if (attach_fd >= 0) {
0N/A file_close(attach_fd);
0N/A delete_attach_file(jvm->pid);
0N/A }
0N/A if (door_fd >= 0) {
0N/A jvm->door_fd = door_fd;
0N/A clear_jvm_error();
0N/A } else {
0N/A free(jvm);
0N/A jvm = NULL;
0N/A }
0N/A return jvm;
0N/A}
0N/A
0N/A/* return the last thread local error message */
0N/Aconst char* jvm_get_last_error() {
0N/A const char* res = NULL;
0N/A thr_getspecific(jvm_error_key, (void**)&res);
0N/A return res;
0N/A}
0N/A
0N/A/* detach the givenb JVM */
0N/Aint jvm_detach(jvm_t* jvm) {
0N/A if (jvm) {
0N/A int res;
0N/A if (jvm->door_fd != -1) {
0N/A if (file_close(jvm->door_fd) != 0) {
0N/A set_jvm_error(JVM_ERR_CANT_CLOSE_DOOR);
0N/A res = -1;
0N/A } else {
0N/A clear_jvm_error();
0N/A res = 0;
0N/A }
0N/A }
0N/A free(jvm);
0N/A return res;
0N/A } else {
0N/A set_jvm_error(JVM_ERR_NULL_PARAM);
0N/A print_debug("jvm_t* is NULL\n");
0N/A return -1;
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * A simple table to translate some known errors into reasonable
0N/A * error messages
0N/A */
0N/Astatic struct {
0N/A int err;
0N/A const char* msg;
0N/A} const error_messages[] = {
0N/A { 100, "Bad request" },
0N/A { 101, "Protocol mismatch" },
0N/A { 102, "Resource failure" },
0N/A { 103, "Internal error" },
0N/A { 104, "Permission denied" },
0N/A};
0N/A
0N/A/*
0N/A * Lookup the given error code and return the appropriate
0N/A * message. If not found return NULL.
0N/A */
0N/Astatic const char* translate_error(int err) {
0N/A int table_size = sizeof(error_messages) / sizeof(error_messages[0]);
0N/A int i;
0N/A
0N/A for (i=0; i<table_size; i++) {
0N/A if (err == error_messages[i].err) {
0N/A return error_messages[i].msg;
0N/A }
0N/A }
0N/A return NULL;
0N/A}
0N/A
0N/A/*
0N/A * Current protocol version
0N/A */
0N/Astatic const char* PROTOCOL_VERSION = "1";
0N/A
0N/A#define RES_BUF_SIZE 128
0N/A
0N/A/*
0N/A * Enqueue attach-on-demand command to the given JVM
0N/A */
0N/Astatic
0N/Aint enqueue_command(jvm_t* jvm, const char* cstr, int arg_count, const char** args) {
0N/A size_t size;
0N/A door_arg_t door_args;
0N/A char res_buffer[RES_BUF_SIZE];
0N/A int rc, i;
0N/A char* buf = NULL;
0N/A int result = -1;
0N/A
0N/A /*
0N/A * First we get the command string and create the start of the
0N/A * argument string to send to the target VM:
0N/A * <ver>\0<cmd>\0
0N/A */
0N/A if (cstr == NULL) {
0N/A print_debug("command name is NULL\n");
0N/A goto quit;
0N/A }
0N/A size = strlen(PROTOCOL_VERSION) + strlen(cstr) + 2;
0N/A buf = (char*)malloc(size);
0N/A if (buf != NULL) {
0N/A char* pos = buf;
0N/A strcpy(buf, PROTOCOL_VERSION);
0N/A pos += strlen(PROTOCOL_VERSION)+1;
0N/A strcpy(pos, cstr);
0N/A } else {
0N/A set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
0N/A print_debug("malloc failed at %d in %s\n", __LINE__, __FILE__);
0N/A goto quit;
0N/A }
0N/A
0N/A /*
0N/A * Next we iterate over the arguments and extend the buffer
0N/A * to include them.
0N/A */
0N/A for (i=0; i<arg_count; i++) {
0N/A cstr = args[i];
0N/A if (cstr != NULL) {
0N/A size_t len = strlen(cstr);
0N/A char* newbuf = (char*)realloc(buf, size+len+1);
0N/A if (newbuf == NULL) {
0N/A set_jvm_error(JVM_ERR_OUT_OF_MEMORY);
0N/A print_debug("realloc failed in %s at %d\n", __FILE__, __LINE__);
0N/A goto quit;
0N/A }
0N/A buf = newbuf;
0N/A strcpy(buf+size, cstr);
0N/A size += len+1;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The arguments to the door function are in 'buf' so we now
0N/A * do the door call
0N/A */
0N/A door_args.data_ptr = buf;
0N/A door_args.data_size = size;
0N/A door_args.desc_ptr = NULL;
0N/A door_args.desc_num = 0;
0N/A door_args.rbuf = (char*)&res_buffer;
0N/A door_args.rsize = sizeof(res_buffer);
0N/A
0N/A RESTARTABLE(door_call(jvm->door_fd, &door_args), rc);
0N/A
0N/A /*
0N/A * door_call failed
0N/A */
0N/A if (rc == -1) {
0N/A print_debug("door_call failed\n");
0N/A } else {
0N/A /*
0N/A * door_call succeeded but the call didn't return the the expected jint.
0N/A */
0N/A if (door_args.data_size < sizeof(int)) {
0N/A print_debug("Enqueue error - reason unknown as result is truncated!");
0N/A } else {
0N/A int* res = (int*)(door_args.data_ptr);
0N/A if (*res != 0) {
0N/A const char* msg = translate_error(*res);
0N/A if (msg == NULL) {
0N/A print_debug("Unable to enqueue command to target VM: %d\n", *res);
0N/A } else {
0N/A print_debug("Unable to enqueue command to target VM: %s\n", msg);
0N/A }
0N/A } else {
0N/A /*
0N/A * The door call should return a file descriptor to one end of
0N/A * a socket pair
0N/A */
0N/A if ((door_args.desc_ptr != NULL) &&
0N/A (door_args.desc_num == 1) &&
0N/A (door_args.desc_ptr->d_attributes & DOOR_DESCRIPTOR)) {
0N/A result = door_args.desc_ptr->d_data.d_desc.d_descriptor;
0N/A } else {
0N/A print_debug("Reply from enqueue missing descriptor!\n");
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/Aquit:
0N/A if (buf) free(buf);
0N/A return result;
0N/A}
0N/A
0N/A/* read status code for a door command */
0N/Astatic int read_status(int fd) {
0N/A char ch, buf[16];
0N/A int index = 0;
0N/A
0N/A while (1) {
0N/A if (file_read(fd, &ch, sizeof(ch)) != sizeof(ch)) {
0N/A set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS);
0N/A print_debug("door cmd status: read status failed\n");
0N/A return -1;
0N/A }
0N/A buf[index++] = ch;
0N/A if (ch == '\n') {
0N/A buf[index - 1] = '\0';
0N/A return atoi(buf);
0N/A }
0N/A if (index == sizeof(buf)) {
0N/A set_jvm_error(JVM_ERR_DOOR_CANT_READ_STATUS);
0N/A print_debug("door cmd status: read status overflow\n");
0N/A return -1;
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic const char* ENABLE_DPROBES_CMD = "enabledprobes";
0N/A
0N/A/* enable one or more DTrace probes for a given JVM */
0N/Aint jvm_enable_dtprobes(jvm_t* jvm, int num_probe_types, const char** probe_types) {
0N/A int fd, status = 0;
0N/A char ch;
0N/A const char* args[1];
0N/A char buf[16];
0N/A int probe_type = 0, index;
0N/A int count = 0;
0N/A
0N/A if (jvm == NULL) {
0N/A set_jvm_error(JVM_ERR_NULL_PARAM);
0N/A print_debug("jvm_t* is NULL\n");
0N/A return -1;
0N/A }
0N/A
0N/A if (num_probe_types == 0 || probe_types == NULL ||
0N/A probe_types[0] == NULL) {
0N/A set_jvm_error(JVM_ERR_INVALID_PARAM);
0N/A print_debug("invalid probe type argument(s)\n");
0N/A return -1;
0N/A }
0N/A
0N/A for (index = 0; index < num_probe_types; index++) {
0N/A const char* p = probe_types[index];
0N/A if (strcmp(p, JVM_DTPROBE_OBJECT_ALLOC) == 0) {
0N/A probe_type |= DTRACE_ALLOC_PROBES;
0N/A count++;
0N/A } else if (strcmp(p, JVM_DTPROBE_METHOD_ENTRY) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_METHOD_RETURN) == 0) {
0N/A probe_type |= DTRACE_METHOD_PROBES;
0N/A count++;
0N/A } else if (strcmp(p, JVM_DTPROBE_MONITOR_ENTER) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_MONITOR_ENTERED) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_MONITOR_EXIT) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_MONITOR_WAIT) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_MONITOR_WAITED) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_MONITOR_NOTIFY) == 0 ||
0N/A strcmp(p, JVM_DTPROBE_MONITOR_NOTIFYALL) == 0) {
0N/A probe_type |= DTRACE_MONITOR_PROBES;
0N/A count++;
0N/A } else if (strcmp(p, JVM_DTPROBE_ALL) == 0) {
0N/A probe_type |= DTRACE_ALL_PROBES;
0N/A count++;
0N/A }
0N/A }
0N/A
0N/A if (count == 0) {
0N/A return count;
0N/A }
0N/A sprintf(buf, "%d", probe_type);
0N/A args[0] = buf;
0N/A
0N/A fd = enqueue_command(jvm, ENABLE_DPROBES_CMD, 1, args);
0N/A if (fd < 0) {
0N/A set_jvm_error(JVM_ERR_DOOR_CMD_SEND);
0N/A return -1;
0N/A }
0N/A
0N/A status = read_status(fd);
0N/A // non-zero status is error
0N/A if (status) {
0N/A set_jvm_error(JVM_ERR_DOOR_CMD_STATUS);
0N/A print_debug("%s command failed (status: %d) in target JVM\n",
0N/A ENABLE_DPROBES_CMD, status);
0N/A file_close(fd);
0N/A return -1;
0N/A }
0N/A // read from stream until EOF
0N/A while (file_read(fd, &ch, sizeof(ch)) == sizeof(ch)) {
0N/A if (libjvm_dtrace_debug) {
0N/A printf("%c", ch);
0N/A }
0N/A }
0N/A
0N/A file_close(fd);
0N/A clear_jvm_error();
0N/A return count;
0N/A}