Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/jvmti/hprof/hprof_error.c
38829 views
/*1* Copyright (c) 2003, 2011, 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 "hprof.h"4142/* The error handling logic. */4344/*45* Most hprof error processing and error functions are kept here, along with46* termination functions and signal handling (used in debug version only).47*48*/4950#include <signal.h>5152static int p = 1; /* Used with pause=y|n option */5354/* Private functions */5556static void57error_message(const char * format, ...)58{59va_list ap;6061va_start(ap, format);62(void)vfprintf(stderr, format, ap);63va_end(ap);64}6566static void67error_abort(void)68{69/* Important to remove existing signal handler */70(void)signal(SIGABRT, NULL);71error_message("HPROF DUMPING CORE\n");72abort(); /* Sends SIGABRT signal, usually also caught by libjvm */73}7475static void76signal_handler(int sig)77{78/* Caught a signal, most likely a SIGABRT */79error_message("HPROF SIGNAL %d TERMINATED PROCESS\n", sig);80error_abort();81}8283static void84setup_signal_handler(int sig)85{86/* Only if debug version or debug=y */87if ( gdata->debug ) {88(void)signal(sig, (void(*)(int))(void*)&signal_handler);89}90}9192static void93terminate_everything(jint exit_code)94{95if ( exit_code > 0 ) {96/* Could be a fatal error or assert error or a sanity error */97error_message("HPROF TERMINATED PROCESS\n");98if ( gdata->coredump || gdata->debug ) {99/* Core dump here by request */100error_abort();101}102}103/* Terminate the process */104error_exit_process(exit_code);105}106107/* External functions */108109void110error_setup(void)111{112setup_signal_handler(SIGABRT);113}114115void116error_do_pause(void)117{118int pid = md_getpid();119int timeleft = 600; /* 10 minutes max */120int interval = 10; /* 10 second message check */121122/*LINTED*/123error_message("\nHPROF pause for PID %d\n", (int)pid);124while ( p && timeleft > 0 ) {125md_sleep(interval); /* 'assign p=0' to stop pause loop */126timeleft -= interval;127}128if ( timeleft <= 0 ) {129error_message("\n HPROF pause got tired of waiting and gave up.\n");130}131}132133void134error_exit_process(int exit_code)135{136exit(exit_code);137}138139static const char *140source_basename(const char *file)141{142const char *p;143144if ( file == NULL ) {145return "UnknownSourceFile";146}147p = strrchr(file, '/');148if ( p == NULL ) {149p = strrchr(file, '\\');150}151if ( p == NULL ) {152p = file;153} else {154p++; /* go past / */155}156return p;157}158159void160error_assert(const char *condition, const char *file, int line)161{162error_message("ASSERTION FAILURE: %s [%s:%d]\n", condition,163source_basename(file), line);164error_abort();165}166167void168error_handler(jboolean fatal, jvmtiError error,169const char *message, const char *file, int line)170{171char *error_name;172173if ( message==NULL ) {174message = "";175}176if ( error != JVMTI_ERROR_NONE ) {177error_name = getErrorName(error);178if ( error_name == NULL ) {179error_name = "?";180}181error_message("HPROF ERROR: %s (JVMTI Error %s(%d)) [%s:%d]\n",182message, error_name, error,183source_basename(file), line);184} else {185error_message("HPROF ERROR: %s [%s:%d]\n", message,186source_basename(file), line);187}188if ( fatal || gdata->errorexit ) {189/* If it's fatal, or the user wants termination on any error, die */190terminate_everything(9);191}192}193194void195debug_message(const char * format, ...)196{197va_list ap;198199va_start(ap, format);200(void)vfprintf(stderr, format, ap);201va_end(ap);202}203204void205verbose_message(const char * format, ...)206{207if ( gdata->verbose ) {208va_list ap;209210va_start(ap, format);211(void)vfprintf(stderr, format, ap);212va_end(ap);213}214}215216217