Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/demo/jvmti/hprof/hprof_md.c
32287 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940#include <sys/types.h>41#include <sys/stat.h>42#include <fcntl.h>4344#if !defined(LINUX) && !defined(_ALLBSD_SOURCE) && !defined(AIX)45#include <procfs.h>46#endif4748#include <stdio.h>49#include <stdlib.h>50#include <string.h>51#include <sys/socket.h>52#include <sys/errno.h>53#include <unistd.h>54#include <errno.h>55#include <dlfcn.h>56#include <sys/time.h>5758#include <netdb.h>59#include <netinet/in.h>60#include <sys/param.h>61#include <time.h>6263#include "jni.h"64#include "jvm_md.h"65#include "hprof.h"6667#ifdef AIX68#include "porting_aix.h" /* For the 'dladdr' function. */69#endif7071int72md_getpid(void)73{74static int pid = -1;7576if ( pid >= 0 ) {77return pid;78}79pid = getpid();80return pid;81}8283void84md_sleep(unsigned seconds)85{86sleep(seconds);87}8889void90md_init(void)91{92#if defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(AIX)93/* No Hi-Res timer option? */94#else95if ( gdata->micro_state_accounting ) {96char proc_ctl_fn[48];97int procfd;9899/* Turn on micro state accounting, once per process */100(void)md_snprintf(proc_ctl_fn, sizeof(proc_ctl_fn),101"/proc/%d/ctl", md_getpid());102103procfd = open(proc_ctl_fn, O_WRONLY);104if (procfd >= 0) {105long ctl_op[2];106107ctl_op[0] = PCSET;108ctl_op[1] = PR_MSACCT;109(void)write(procfd, ctl_op, sizeof(ctl_op));110(void)close(procfd);111}112}113#endif114}115116int117md_connect(char *hostname, unsigned short port)118{119struct hostent *hentry;120struct sockaddr_in s;121int fd;122123/* create a socket */124fd = socket(AF_INET, SOCK_STREAM, 0);125if ( fd < 0 ) {126return -1;127}128129/* find remote host's addr from name */130if ((hentry = gethostbyname(hostname)) == NULL) {131(void)close(fd);132return -1;133}134(void)memset((char *)&s, 0, sizeof(s));135/* set remote host's addr; its already in network byte order */136(void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),137(int)sizeof(s.sin_addr.s_addr));138/* set remote host's port */139s.sin_port = htons(port);140s.sin_family = AF_INET;141142/* now try connecting */143if (-1 == connect(fd, (struct sockaddr*)&s, sizeof(s))) {144(void)close(fd);145return 0;146}147return fd;148}149150int151md_recv(int f, char *buf, int len, int option)152{153return recv(f, buf, len, option);154}155156int157md_shutdown(int filedes, int option)158{159return shutdown(filedes, option);160}161162int163md_open(const char *filename)164{165return open(filename, O_RDONLY);166}167168int169md_open_binary(const char *filename)170{171return md_open(filename);172}173174int175md_creat(const char *filename)176{177return open(filename, O_WRONLY | O_CREAT | O_TRUNC,178S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);179}180181int182md_creat_binary(const char *filename)183{184return md_creat(filename);185}186187jlong188md_seek(int filedes, jlong cur)189{190jlong new_pos;191192if ( cur == (jlong)-1 ) {193new_pos = lseek(filedes, 0, SEEK_END);194} else {195new_pos = lseek(filedes, cur, SEEK_SET);196}197return new_pos;198}199200void201md_close(int filedes)202{203(void)close(filedes);204}205206int207md_send(int s, const char *msg, int len, int flags)208{209int res;210211do {212res = send(s, msg, len, flags);213} while ((res < 0) && (errno == EINTR));214215return res;216}217218int219md_write(int filedes, const void *buf, int nbyte)220{221int res;222223do {224res = write(filedes, buf, nbyte);225} while ((res < 0) && (errno == EINTR));226227return res;228}229230int231md_read(int filedes, void *buf, int nbyte)232{233int res;234235do {236res = read(filedes, buf, nbyte);237} while ((res < 0) && (errno == EINTR));238239return res;240}241242/* Time of day in milli-seconds */243static jlong244md_timeofday(void)245{246struct timeval tv;247248if ( gettimeofday(&tv, (void *)0) != 0 ) {249return (jlong)0; /* EOVERFLOW ? */250}251/*LINTED*/252return ((jlong)tv.tv_sec * (jlong)1000) + (jlong)(tv.tv_usec / 1000);253}254255/* Hi-res timer in micro-seconds */256jlong257md_get_microsecs(void)258{259#if defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(AIX)260return (jlong)(md_timeofday() * (jlong)1000); /* Milli to micro */261#else262return (jlong)(gethrtime()/(hrtime_t)1000); /* Nano seconds to micro seconds */263#endif264}265266/* Time of day in milli-seconds */267jlong268md_get_timemillis(void)269{270return md_timeofday();271}272273/* Current CPU hi-res CPU time used */274jlong275md_get_thread_cpu_timemillis(void)276{277#if defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(AIX)278return md_timeofday();279#else280return (jlong)(gethrvtime()/1000); /* Nano seconds to milli seconds */281#endif282}283284void285md_get_prelude_path(char *path, int path_len, char *filename)286{287void *addr;288char libdir[FILENAME_MAX+1];289Dl_info dlinfo;290291libdir[0] = 0;292#if defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(AIX)293addr = (void*)&Agent_OnLoad;294#else295/* Just using &Agent_OnLoad will get the first external symbol with296* this name in the first .so, which may not be libhprof.so.297* On Solaris we can actually ask for the address of our Agent_OnLoad.298*/299addr = dlsym(RTLD_SELF, "Agent_OnLoad");300/* Just in case the above didn't work (missing linker patch?). */301if ( addr == NULL ) {302addr = (void*)&Agent_OnLoad;303}304#endif305306/* Use dladdr() to get the full path to libhprof.so, which we use to find307* the prelude file.308*/309dlinfo.dli_fname = NULL;310(void)dladdr(addr, &dlinfo);311if ( dlinfo.dli_fname != NULL ) {312char * lastSlash;313314/* Full path to library name, need to move up one directory to 'lib' */315(void)strcpy(libdir, (char *)dlinfo.dli_fname);316lastSlash = strrchr(libdir, '/');317if ( lastSlash != NULL ) {318*lastSlash = '\0';319}320#ifndef __APPLE__321// not sure why other platforms have to go up two levels, but on macos we only need up one322lastSlash = strrchr(libdir, '/');323if ( lastSlash != NULL ) {324*lastSlash = '\0';325}326#endif /* __APPLE__ */327}328(void)snprintf(path, path_len, "%s/%s", libdir, filename);329}330331332int333md_vsnprintf(char *s, int n, const char *format, va_list ap)334{335return vsnprintf(s, n, format, ap);336}337338int339md_snprintf(char *s, int n, const char *format, ...)340{341int ret;342va_list ap;343344va_start(ap, format);345ret = md_vsnprintf(s, n, format, ap);346va_end(ap);347return ret;348}349350void351md_system_error(char *buf, int len)352{353char *p;354355buf[0] = 0;356p = strerror(errno);357if ( p != NULL ) {358(void)strcpy(buf, p);359}360}361362unsigned363md_htons(unsigned short s)364{365return htons(s);366}367368unsigned369md_htonl(unsigned l)370{371return htonl(l);372}373374unsigned375md_ntohs(unsigned short s)376{377return ntohs(s);378}379380unsigned381md_ntohl(unsigned l)382{383return ntohl(l);384}385386static void dll_build_name(char* buffer, size_t buflen,387const char* paths, const char* fname) {388char *path, *paths_copy, *next_token;389390paths_copy = strdup(paths);391if (paths_copy == NULL) {392return;393}394395next_token = NULL;396path = strtok_r(paths_copy, ":", &next_token);397398while (path != NULL) {399snprintf(buffer, buflen, "%s/lib%s" JNI_LIB_SUFFIX, path, fname);400if (access(buffer, F_OK) == 0) {401break;402}403*buffer = '\0';404path = strtok_r(NULL, ":", &next_token);405}406407free(paths_copy);408}409410/* Create the actual fill filename for a dynamic library. */411void412md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)413{414int pnamelen;415416/* Length of options directory location. */417pnamelen = pname ? strlen(pname) : 0;418419*holder = '\0';420/* Quietly truncate on buffer overflow. Should be an error. */421if (pnamelen + (int)strlen(fname) + 10 > holderlen) {422return;423}424425/* Construct path to library */426if (pnamelen == 0) {427(void)snprintf(holder, holderlen, "lib%s" JNI_LIB_SUFFIX, fname);428} else {429dll_build_name(holder, holderlen, pname, fname);430}431}432433/* Load this library (return NULL on error, and error message in err_buf) */434void *435md_load_library(const char *name, char *err_buf, int err_buflen)436{437void * result;438439result = dlopen(name, RTLD_LAZY);440if (result == NULL) {441(void)strncpy(err_buf, dlerror(), err_buflen-2);442err_buf[err_buflen-1] = '\0';443}444return result;445}446447/* Unload this library */448void449md_unload_library(void *handle)450{451(void)dlclose(handle);452}453454/* Find an entry point inside this library (return NULL if not found) */455void *456md_find_library_entry(void *handle, const char *name)457{458void * sym;459460sym = dlsym(handle, name);461return sym;462}463464465466467