Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/bridge/AccessBridgeDebug.cpp
32287 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* A class to manage AccessBridge debugging27*/2829#include "AccessBridgeDebug.h"30#include <stdarg.h>31#include <stdio.h>32#include <windows.h>33#include <cstdlib>34#include <cstring>3536#ifdef __cplusplus37extern "C" {38#endif3940static FILE* logFP = nullptr;4142void initializeFileLogger(char * fileName) {43auto var = "JAVA_ACCESSBRIDGE_LOGDIR";44const auto envfilePath = getenv(var);45if (envfilePath != nullptr && fileName != nullptr) {46auto envFilePathLength = strlen(envfilePath);47auto fileNameLength = strlen(fileName);48auto filePathSize = envFilePathLength + 1 + fileNameLength + 5; //1 for "/", 5 for ".log" and 0;49auto filePath = new char[filePathSize];50memset(filePath, 0, filePathSize*sizeof(char));51memcpy(filePath, envfilePath, envFilePathLength*sizeof(char));52filePath[envFilePathLength] = '/';53memcpy(filePath + envFilePathLength + 1, fileName, fileNameLength*sizeof(char));54memcpy(filePath + envFilePathLength + 1 + fileNameLength, ".log", 4*sizeof(char));5556logFP = fopen(filePath, "w");57if (logFP == nullptr) {58printf("\n%s\n", filePath);59PrintDebugString("Could not open file %s", filePath);60}6162delete [] filePath;63}64}6566void finalizeFileLogger() {67if (logFP) {68fclose(logFP);69logFP = nullptr;70}71}7273auto getTimeStamp() -> long long {74LARGE_INTEGER freqLarge;75::QueryPerformanceFrequency(&freqLarge);76long long freq = freqLarge.QuadPart;77LARGE_INTEGER counterLarge;78::QueryPerformanceCounter(&counterLarge);79long long counter = counterLarge.QuadPart;80long long milliDen = 1000;81// prevent possible overflow82long long whole = (counter / freq) * milliDen;83long long part = (counter % freq) * milliDen / freq;84return whole + part;85}8687/**88* Send debugging info to the appropriate place89*/90void PrintDebugString(char *msg, ...) {91#ifdef DEBUGGING_ON92char buf[1024] = {0};93va_list argprt;9495va_start(argprt, msg); // set up argptr96vsprintf(buf, msg, argprt);97#ifdef SEND_TO_OUTPUT_DEBUG_STRING98OutputDebugString(buf);99#endif100#ifdef SEND_TO_CONSOLE101printf(buf);102printf("\r\n");103#endif104#endif105if (logFP) {106fprintf(logFP, "[%llu] ", getTimeStamp());107va_list args;108va_start(args, msg);109vfprintf(logFP, msg, args);110va_end(args);111fprintf(logFP, "\r\n");112}113}114115/**116* Send Java debugging info to the appropriate place117*/118void PrintJavaDebugString2(char *msg, ...) {119#ifdef JAVA_DEBUGGING_ON120char buf[1024] = {0};121va_list argprt;122123va_start(argprt, msg); // set up argptr124vsprintf(buf, msg, argprt);125#ifdef SEND_TO_OUTPUT_DEBUG_STRING126OutputDebugString(buf);127#endif128#ifdef SEND_TO_CONSOLE129printf(buf);130printf("\r\n");131#endif132#endif133if (logFP) {134fprintf(logFP, "[%llu] ", getTimeStamp());135va_list args;136va_start(args, msg);137vfprintf(logFP, msg, args);138va_end(args);139fprintf(logFP, "\r\n");140}141}142/**143* Wide version of the method to send debugging info to the appropriate place144*/145void wPrintDebugString(wchar_t *msg, ...) {146#ifdef DEBUGGING_ON147char buf[1024] = {0};148char charmsg[256];149va_list argprt;150151va_start(argprt, msg); // set up argptr152sprintf(charmsg, "%ls", msg); // convert format string to multi-byte153vsprintf(buf, charmsg, argprt);154#ifdef SEND_TO_OUTPUT_DEBUG_STRING155OutputDebugString(buf);156#endif157#ifdef SEND_TO_CONSOLE158printf(buf);159printf("\r\n");160#endif161#endif162if (logFP) {163fprintf(logFP, "[%llu] ", getTimeStamp());164va_list args;165va_start(args, msg);166vfwprintf(logFP, msg, args);167va_end(args);168fprintf(logFP, "\r\n");169}170}171172/**173* Wide version of the method to send Java debugging info to the appropriate place174*/175void wPrintJavaDebugString(wchar_t *msg, ...) {176#ifdef JAVA_DEBUGGING_ON177char buf[1024] = {0};178char charmsg[256] = {0};179va_list argprt;180181va_start(argprt, msg); // set up argptr182sprintf(charmsg, "%ls", msg); // convert format string to multi-byte183vsprintf(buf, charmsg, argprt);184#ifdef SEND_TO_OUTPUT_DEBUG_STRING185OutputDebugString(buf);186#endif187#ifdef SEND_TO_CONSOLE188printf(buf);189printf("\r\n");190#endif191#endif192if (logFP) {193fprintf(logFP, "[%llu] ", getTimeStamp());194va_list args;195va_start(args, msg);196vfwprintf(logFP, msg, args);197va_end(args);198fprintf(logFP, "\r\n");199}200}201#ifdef __cplusplus202}203#endif204205206