0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A#include <sys/types.h>
0N/A#include <sys/stat.h>
0N/A#include <fcntl.h>
0N/A
4632N/A#if !defined(LINUX) && !defined(_ALLBSD_SOURCE)
0N/A#include <procfs.h>
0N/A#endif
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <sys/socket.h>
0N/A#include <sys/errno.h>
0N/A#include <unistd.h>
0N/A#include <errno.h>
0N/A#include <dlfcn.h>
0N/A#include <sys/time.h>
0N/A
0N/A#include <netdb.h>
0N/A#include <netinet/in.h>
0N/A#include <sys/param.h>
0N/A#include <time.h>
0N/A
0N/A#include "jni.h"
4632N/A#include "jvm_md.h"
0N/A#include "hprof.h"
0N/A
0N/Aint
0N/Amd_getpid(void)
0N/A{
0N/A static int pid = -1;
0N/A
0N/A if ( pid >= 0 ) {
0N/A return pid;
0N/A }
0N/A pid = getpid();
0N/A return pid;
0N/A}
0N/A
0N/Avoid
0N/Amd_sleep(unsigned seconds)
0N/A{
0N/A sleep(seconds);
0N/A}
0N/A
0N/Avoid
0N/Amd_init(void)
0N/A{
4632N/A#if defined(LINUX) || defined(_ALLBSD_SOURCE)
0N/A /* No Hi-Res timer option? */
0N/A#else
0N/A if ( gdata->micro_state_accounting ) {
0N/A char proc_ctl_fn[48];
0N/A int procfd;
0N/A
0N/A /* Turn on micro state accounting, once per process */
0N/A (void)md_snprintf(proc_ctl_fn, sizeof(proc_ctl_fn),
0N/A "/proc/%d/ctl", md_getpid());
0N/A
0N/A procfd = open(proc_ctl_fn, O_WRONLY);
0N/A if (procfd >= 0) {
0N/A long ctl_op[2];
0N/A
0N/A ctl_op[0] = PCSET;
0N/A ctl_op[1] = PR_MSACCT;
0N/A (void)write(procfd, ctl_op, sizeof(ctl_op));
0N/A (void)close(procfd);
0N/A }
0N/A }
0N/A#endif
0N/A}
0N/A
0N/Aint
0N/Amd_connect(char *hostname, unsigned short port)
0N/A{
0N/A struct hostent *hentry;
0N/A struct sockaddr_in s;
0N/A int fd;
0N/A
0N/A /* create a socket */
0N/A fd = socket(AF_INET, SOCK_STREAM, 0);
0N/A
0N/A /* find remote host's addr from name */
0N/A if ((hentry = gethostbyname(hostname)) == NULL) {
0N/A return -1;
0N/A }
0N/A (void)memset((char *)&s, 0, sizeof(s));
0N/A /* set remote host's addr; its already in network byte order */
0N/A (void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
0N/A (int)sizeof(s.sin_addr.s_addr));
0N/A /* set remote host's port */
0N/A s.sin_port = htons(port);
0N/A s.sin_family = AF_INET;
0N/A
0N/A /* now try connecting */
0N/A if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
0N/A return 0;
0N/A }
0N/A return fd;
0N/A}
0N/A
0N/Aint
0N/Amd_recv(int f, char *buf, int len, int option)
0N/A{
0N/A return recv(f, buf, len, option);
0N/A}
0N/A
0N/Aint
0N/Amd_shutdown(int filedes, int option)
0N/A{
0N/A return shutdown(filedes, option);
0N/A}
0N/A
0N/Aint
0N/Amd_open(const char *filename)
0N/A{
0N/A return open(filename, O_RDONLY);
0N/A}
0N/A
0N/Aint
0N/Amd_open_binary(const char *filename)
0N/A{
0N/A return md_open(filename);
0N/A}
0N/A
0N/Aint
0N/Amd_creat(const char *filename)
0N/A{
0N/A return open(filename, O_WRONLY | O_CREAT | O_TRUNC,
0N/A S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
0N/A}
0N/A
0N/Aint
0N/Amd_creat_binary(const char *filename)
0N/A{
0N/A return md_creat(filename);
0N/A}
0N/A
0N/Ajlong
0N/Amd_seek(int filedes, jlong cur)
0N/A{
0N/A jlong new_pos;
0N/A
0N/A if ( cur == (jlong)-1 ) {
0N/A new_pos = lseek(filedes, 0, SEEK_END);
0N/A } else {
0N/A new_pos = lseek(filedes, cur, SEEK_SET);
0N/A }
0N/A return new_pos;
0N/A}
0N/A
0N/Avoid
0N/Amd_close(int filedes)
0N/A{
0N/A (void)close(filedes);
0N/A}
0N/A
0N/Aint
0N/Amd_send(int s, const char *msg, int len, int flags)
0N/A{
0N/A int res;
0N/A
0N/A do {
0N/A res = send(s, msg, len, flags);
0N/A } while ((res < 0) && (errno == EINTR));
0N/A
0N/A return res;
0N/A}
0N/A
0N/Aint
0N/Amd_write(int filedes, const void *buf, int nbyte)
0N/A{
0N/A int res;
0N/A
0N/A do {
0N/A res = write(filedes, buf, nbyte);
0N/A } while ((res < 0) && (errno == EINTR));
0N/A
0N/A return res;
0N/A}
0N/A
0N/Aint
0N/Amd_read(int filedes, void *buf, int nbyte)
0N/A{
0N/A int res;
0N/A
0N/A do {
0N/A res = read(filedes, buf, nbyte);
0N/A } while ((res < 0) && (errno == EINTR));
0N/A
0N/A return res;
0N/A}
0N/A
0N/A/* Time of day in milli-seconds */
0N/Astatic jlong
0N/Amd_timeofday(void)
0N/A{
0N/A struct timeval tv;
0N/A
0N/A if ( gettimeofday(&tv, (void *)0) != 0 ) {
0N/A return (jlong)0; /* EOVERFLOW ? */
0N/A }
0N/A /*LINTED*/
0N/A return ((jlong)tv.tv_sec * (jlong)1000) + (jlong)(tv.tv_usec / 1000);
0N/A}
0N/A
0N/A/* Hi-res timer in micro-seconds */
0N/Ajlong
0N/Amd_get_microsecs(void)
0N/A{
4632N/A#if defined(LINUX) || defined(_ALLBSD_SOURCE)
0N/A return (jlong)(md_timeofday() * (jlong)1000); /* Milli to micro */
0N/A#else
0N/A return (jlong)(gethrtime()/(hrtime_t)1000); /* Nano seconds to micro seconds */
0N/A#endif
0N/A}
0N/A
0N/A/* Time of day in milli-seconds */
0N/Ajlong
0N/Amd_get_timemillis(void)
0N/A{
0N/A return md_timeofday();
0N/A}
0N/A
0N/A/* Current CPU hi-res CPU time used */
0N/Ajlong
0N/Amd_get_thread_cpu_timemillis(void)
0N/A{
4632N/A#if defined(LINUX) || defined(_ALLBSD_SOURCE)
0N/A return md_timeofday();
0N/A#else
0N/A return (jlong)(gethrvtime()/1000); /* Nano seconds to milli seconds */
0N/A#endif
0N/A}
0N/A
0N/Avoid
0N/Amd_get_prelude_path(char *path, int path_len, char *filename)
0N/A{
0N/A void *addr;
0N/A char libdir[FILENAME_MAX+1];
0N/A Dl_info dlinfo;
0N/A
0N/A libdir[0] = 0;
4632N/A#if defined(LINUX) || defined(_ALLBSD_SOURCE)
0N/A addr = (void*)&Agent_OnLoad;
0N/A#else
0N/A /* Just using &Agent_OnLoad will get the first external symbol with
0N/A * this name in the first .so, which may not be libhprof.so.
0N/A * On Solaris we can actually ask for the address of our Agent_OnLoad.
0N/A */
0N/A addr = dlsym(RTLD_SELF, "Agent_OnLoad");
0N/A /* Just in case the above didn't work (missing linker patch?). */
0N/A if ( addr == NULL ) {
0N/A addr = (void*)&Agent_OnLoad;
0N/A }
0N/A#endif
0N/A
0N/A /* Use dladdr() to get the full path to libhprof.so, which we use to find
0N/A * the prelude file.
0N/A */
0N/A dlinfo.dli_fname = NULL;
0N/A (void)dladdr(addr, &dlinfo);
0N/A if ( dlinfo.dli_fname != NULL ) {
0N/A char * lastSlash;
0N/A
0N/A /* Full path to library name, need to move up one directory to 'lib' */
0N/A (void)strcpy(libdir, (char *)dlinfo.dli_fname);
0N/A lastSlash = strrchr(libdir, '/');
0N/A if ( lastSlash != NULL ) {
0N/A *lastSlash = '\0';
0N/A }
4632N/A#ifndef __APPLE__
4632N/A // not sure why other platforms have to go up two levels, but on macos we only need up one
0N/A lastSlash = strrchr(libdir, '/');
0N/A if ( lastSlash != NULL ) {
0N/A *lastSlash = '\0';
0N/A }
4632N/A#endif /* __APPLE__ */
0N/A }
0N/A (void)snprintf(path, path_len, "%s/%s", libdir, filename);
0N/A}
0N/A
0N/A
0N/Aint
0N/Amd_vsnprintf(char *s, int n, const char *format, va_list ap)
0N/A{
0N/A return vsnprintf(s, n, format, ap);
0N/A}
0N/A
0N/Aint
0N/Amd_snprintf(char *s, int n, const char *format, ...)
0N/A{
0N/A int ret;
0N/A va_list ap;
0N/A
0N/A va_start(ap, format);
0N/A ret = md_vsnprintf(s, n, format, ap);
0N/A va_end(ap);
0N/A return ret;
0N/A}
0N/A
0N/Avoid
0N/Amd_system_error(char *buf, int len)
0N/A{
0N/A char *p;
0N/A
0N/A buf[0] = 0;
0N/A p = strerror(errno);
0N/A if ( p != NULL ) {
0N/A (void)strcpy(buf, p);
0N/A }
0N/A}
0N/A
0N/Aunsigned
0N/Amd_htons(unsigned short s)
0N/A{
0N/A return htons(s);
0N/A}
0N/A
0N/Aunsigned
0N/Amd_htonl(unsigned l)
0N/A{
0N/A return htonl(l);
0N/A}
0N/A
0N/Aunsigned
0N/Amd_ntohs(unsigned short s)
0N/A{
0N/A return ntohs(s);
0N/A}
0N/A
0N/Aunsigned
0N/Amd_ntohl(unsigned l)
0N/A{
0N/A return ntohl(l);
0N/A}
0N/A
5539N/Astatic void dll_build_name(char* buffer, size_t buflen,
5539N/A const char* pname, const char* fname) {
5539N/A // Loosely based on os_solaris.cpp
5539N/A
5539N/A char *pathname = (char *)pname;
5539N/A while (strlen(pathname) > 0) {
5539N/A char *p = strchr(pathname, ':');
5539N/A if (p == NULL) {
5539N/A p = pathname + strlen(pathname);
5539N/A }
5539N/A /* check for NULL path */
5539N/A if (p == pathname) {
5539N/A continue;
5539N/A }
5539N/A (void)snprintf(buffer, buflen, "%.*s/lib%s" JNI_LIB_SUFFIX,
5539N/A (p - pathname), pathname, fname);
5539N/A
5539N/A if (access(buffer, F_OK) == 0) {
5539N/A break;
5539N/A }
5539N/A pathname = p + 1;
5539N/A *buffer = '\0';
5539N/A }
5539N/A}
5539N/A
0N/A/* Create the actual fill filename for a dynamic library. */
0N/Avoid
0N/Amd_build_library_name(char *holder, int holderlen, char *pname, char *fname)
0N/A{
0N/A int pnamelen;
0N/A
0N/A /* Length of options directory location. */
0N/A pnamelen = pname ? strlen(pname) : 0;
0N/A
5539N/A *holder = '\0';
0N/A /* Quietly truncate on buffer overflow. Should be an error. */
0N/A if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
0N/A return;
0N/A }
0N/A
0N/A /* Construct path to library */
0N/A if (pnamelen == 0) {
4632N/A (void)snprintf(holder, holderlen, "lib%s" JNI_LIB_SUFFIX, fname);
0N/A } else {
5539N/A dll_build_name(holder, holderlen, pname, fname);
0N/A }
0N/A}
0N/A
0N/A/* Load this library (return NULL on error, and error message in err_buf) */
0N/Avoid *
0N/Amd_load_library(const char *name, char *err_buf, int err_buflen)
0N/A{
0N/A void * result;
0N/A
0N/A result = dlopen(name, RTLD_LAZY);
0N/A if (result == NULL) {
0N/A (void)strncpy(err_buf, dlerror(), err_buflen-2);
0N/A err_buf[err_buflen-1] = '\0';
0N/A }
0N/A return result;
0N/A}
0N/A
0N/A/* Unload this library */
0N/Avoid
0N/Amd_unload_library(void *handle)
0N/A{
0N/A (void)dlclose(handle);
0N/A}
0N/A
0N/A/* Find an entry point inside this library (return NULL if not found) */
0N/Avoid *
0N/Amd_find_library_entry(void *handle, const char *name)
0N/A{
0N/A void * sym;
0N/A
0N/A sym = dlsym(handle, name);
0N/A return sym;
0N/A}