Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/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// To ensure winsock2.h is used, it has to be included ahead of41// windows.h, which includes winsock.h by default.42#include <winsock2.h>43#include <windows.h>44#include <io.h>45#include <sys/types.h>46#include <sys/stat.h>47#include <mmsystem.h>48#include <fcntl.h>49#include <process.h>5051#include "jni.h"52#include "hprof.h"5354int55md_getpid(void)56{57static int pid = -1;5859if ( pid >= 0 ) {60return pid;61}62pid = getpid();63return pid;64}6566void67md_sleep(unsigned seconds)68{69Sleep((DWORD)seconds*1000);70}7172void73md_init(void)74{75}7677int78md_connect(char *hostname, unsigned short port)79{80struct hostent *hentry;81struct sockaddr_in s;82int fd;8384/* find remote host's addr from name */85if ((hentry = gethostbyname(hostname)) == NULL) {86return -1;87}88(void)memset((char *)&s, 0, sizeof(s));89/* set remote host's addr; its already in network byte order */90(void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),91(int)sizeof(s.sin_addr.s_addr));92/* set remote host's port */93s.sin_port = htons(port);94s.sin_family = AF_INET;9596/* create a socket */97fd = (int)socket(AF_INET, SOCK_STREAM, 0);98if (INVALID_SOCKET == fd) {99return 0;100}101102/* now try connecting */103if (SOCKET_ERROR == connect(fd, (struct sockaddr*)&s, sizeof(s))) {104closesocket(fd);105return 0;106}107return fd;108}109110int111md_recv(int f, char *buf, int len, int option)112{113return recv(f, buf, len, option);114}115116int117md_shutdown(int filedes, int option)118{119return shutdown(filedes, option);120}121122int123md_open(const char *filename)124{125return open(filename, O_RDONLY);126}127128int129md_open_binary(const char *filename)130{131return open(filename, O_RDONLY|O_BINARY);132}133134int135md_creat(const char *filename)136{137return open(filename, O_CREAT | O_WRONLY | O_TRUNC,138_S_IREAD | _S_IWRITE);139}140141int142md_creat_binary(const char *filename)143{144return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,145_S_IREAD | _S_IWRITE);146}147148jlong149md_seek(int filedes, jlong pos)150{151jlong new_pos;152153if ( pos == (jlong)-1 ) {154new_pos = _lseeki64(filedes, 0L, SEEK_END);155} else {156new_pos = _lseeki64(filedes, pos, SEEK_SET);157}158return new_pos;159}160161void162md_close(int filedes)163{164(void)closesocket(filedes);165}166167int168md_send(int s, const char *msg, int len, int flags)169{170return send(s, msg, len, flags);171}172173int174md_read(int filedes, void *buf, int nbyte)175{176return read(filedes, buf, nbyte);177}178179int180md_write(int filedes, const void *buf, int nbyte)181{182return write(filedes, buf, nbyte);183}184185jlong186md_get_microsecs(void)187{188return (jlong)(timeGetTime())*(jlong)1000;189}190191#define FT2JLONG(ft) \192((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime)193194jlong195md_get_timemillis(void)196{197static jlong fileTime_1_1_70 = 0;198SYSTEMTIME st0;199FILETIME ft0;200201if (fileTime_1_1_70 == 0) {202/* Initialize fileTime_1_1_70 -- the Win32 file time of midnight203* 1/1/70.204*/205206memset(&st0, 0, sizeof(st0));207st0.wYear = 1970;208st0.wMonth = 1;209st0.wDay = 1;210SystemTimeToFileTime(&st0, &ft0);211fileTime_1_1_70 = FT2JLONG(ft0);212}213214GetSystemTime(&st0);215SystemTimeToFileTime(&st0, &ft0);216217return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000;218}219220jlong221md_get_thread_cpu_timemillis(void)222{223return md_get_timemillis();224}225226HINSTANCE hJavaInst;227static int nError = 0;228229BOOL WINAPI230DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)231{232WSADATA wsaData;233switch (reason) {234case DLL_PROCESS_ATTACH:235hJavaInst = hinst;236nError = WSAStartup(MAKEWORD(2,0), &wsaData);237break;238case DLL_PROCESS_DETACH:239WSACleanup();240hJavaInst = NULL;241default:242break;243}244return TRUE;245}246247void248md_get_prelude_path(char *path, int path_len, char *filename)249{250char libdir[FILENAME_MAX+1];251char *lastSlash;252253GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);254255/* This is actually in the bin directory, so move above bin for lib */256lastSlash = strrchr(libdir, '\\');257if ( lastSlash != NULL ) {258*lastSlash = '\0';259}260lastSlash = strrchr(libdir, '\\');261if ( lastSlash != NULL ) {262*lastSlash = '\0';263}264(void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);265}266267int268md_vsnprintf(char *s, int n, const char *format, va_list ap)269{270return _vsnprintf(s, n, format, ap);271}272273int274md_snprintf(char *s, int n, const char *format, ...)275{276int ret;277va_list ap;278279va_start(ap, format);280ret = md_vsnprintf(s, n, format, ap);281va_end(ap);282return ret;283}284285void286md_system_error(char *buf, int len)287{288long errval;289290errval = GetLastError();291buf[0] = '\0';292if (errval != 0) {293int n;294295n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,296NULL, errval,2970, buf, len, NULL);298if (n > 3) {299/* Drop final '.', CR, LF */300if (buf[n - 1] == '\n') n--;301if (buf[n - 1] == '\r') n--;302if (buf[n - 1] == '.') n--;303buf[n] = '\0';304}305}306}307308unsigned309md_htons(unsigned short s)310{311return htons(s);312}313314unsigned315md_htonl(unsigned l)316{317return htonl(l);318}319320unsigned321md_ntohs(unsigned short s)322{323return ntohs(s);324}325326unsigned327md_ntohl(unsigned l)328{329return ntohl(l);330}331332static int333get_last_error_string(char *buf, int len)334{335long errval;336337errval = GetLastError();338if (errval != 0) {339/* DOS error */340int n;341342n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,343NULL, errval,3440, buf, len, NULL);345if (n > 3) {346/* Drop final '.', CR, LF */347if (buf[n - 1] == '\n') n--;348if (buf[n - 1] == '\r') n--;349if (buf[n - 1] == '.') n--;350buf[n] = '\0';351}352return n;353}354355if (errno != 0) {356/* C runtime error that has no corresponding DOS error code */357const char *s;358int n;359360s = strerror(errno);361n = (int)strlen(s);362if (n >= len) {363n = len - 1;364}365(void)strncpy(buf, s, n);366buf[n] = '\0';367return n;368}369370return 0;371}372373static void dll_build_name(char* buffer, size_t buflen,374const char* paths, const char* fname) {375char *path, *paths_copy, *next_token;376377paths_copy = strdup(paths);378if (paths_copy == NULL) {379return;380}381382next_token = NULL;383path = strtok_s(paths_copy, ";", &next_token);384385while (path != NULL) {386_snprintf(buffer, buflen, "%s\\%s.dll", path, fname);387if (_access(buffer, 0) == 0) {388break;389}390*buffer = '\0';391path = strtok_s(NULL, ";", &next_token);392}393394free(paths_copy);395}396397/* Build a machine dependent library name out of a path and file name. */398void399md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)400{401int pnamelen;402403pnamelen = pname ? (int)strlen(pname) : 0;404405*holder = '\0';406/* Quietly truncates on buffer overflow. Should be an error. */407if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {408return;409}410411if (pnamelen == 0) {412sprintf(holder, "%s.dll", fname);413} else {414dll_build_name(holder, holderlen, pname, fname);415}416}417418void *419md_load_library(const char * name, char *err_buf, int err_buflen)420{421void *result;422423result = LoadLibrary(name);424if (result == NULL) {425/* Error message is pretty lame, try to make a better guess. */426long errcode;427428errcode = GetLastError();429if (errcode == ERROR_MOD_NOT_FOUND) {430strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);431err_buf[err_buflen-1] = '\0';432} else {433get_last_error_string(err_buf, err_buflen);434}435}436return result;437}438439void440md_unload_library(void *handle)441{442FreeLibrary(handle);443}444445void *446md_find_library_entry(void *handle, const char *name)447{448return GetProcAddress(handle, name);449}450451452