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 "hprof.h"
0N/A
0N/A/* The error handling logic. */
0N/A
0N/A/*
0N/A * Most hprof error processing and error functions are kept here, along with
0N/A * termination functions and signal handling (used in debug version only).
0N/A *
0N/A */
0N/A
0N/A#include <signal.h>
0N/A
0N/Astatic int p = 1; /* Used with pause=y|n option */
0N/A
0N/A/* Private functions */
0N/A
0N/Astatic void
0N/Aerror_message(const char * format, ...)
0N/A{
0N/A va_list ap;
0N/A
0N/A va_start(ap, format);
0N/A (void)vfprintf(stderr, format, ap);
0N/A va_end(ap);
0N/A}
0N/A
0N/Astatic void
0N/Aerror_abort(void)
0N/A{
0N/A /* Important to remove existing signal handler */
0N/A (void)signal(SIGABRT, NULL);
0N/A error_message("HPROF DUMPING CORE\n");
0N/A abort(); /* Sends SIGABRT signal, usually also caught by libjvm */
0N/A}
0N/A
0N/Astatic void
0N/Asignal_handler(int sig)
0N/A{
0N/A /* Caught a signal, most likely a SIGABRT */
0N/A error_message("HPROF SIGNAL %d TERMINATED PROCESS\n", sig);
0N/A error_abort();
0N/A}
0N/A
0N/Astatic void
0N/Asetup_signal_handler(int sig)
0N/A{
0N/A /* Only if debug version or debug=y */
0N/A if ( gdata->debug ) {
0N/A (void)signal(sig, (void(*)(int))(void*)&signal_handler);
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/Aterminate_everything(jint exit_code)
0N/A{
0N/A if ( exit_code > 0 ) {
0N/A /* Could be a fatal error or assert error or a sanity error */
0N/A error_message("HPROF TERMINATED PROCESS\n");
0N/A if ( gdata->coredump || gdata->debug ) {
0N/A /* Core dump here by request */
0N/A error_abort();
0N/A }
0N/A }
0N/A /* Terminate the process */
0N/A error_exit_process(exit_code);
0N/A}
0N/A
0N/A/* External functions */
0N/A
0N/Avoid
0N/Aerror_setup(void)
0N/A{
0N/A setup_signal_handler(SIGABRT);
0N/A}
0N/A
0N/Avoid
0N/Aerror_do_pause(void)
0N/A{
0N/A int pid = md_getpid();
0N/A int timeleft = 600; /* 10 minutes max */
0N/A int interval = 10; /* 10 second message check */
0N/A
0N/A /*LINTED*/
0N/A error_message("\nHPROF pause for PID %d\n", (int)pid);
0N/A while ( p && timeleft > 0 ) {
0N/A md_sleep(interval); /* 'assign p=0' to stop pause loop */
0N/A timeleft -= interval;
0N/A }
0N/A if ( timeleft <= 0 ) {
0N/A error_message("\n HPROF pause got tired of waiting and gave up.\n");
0N/A }
0N/A}
0N/A
0N/Avoid
0N/Aerror_exit_process(int exit_code)
0N/A{
0N/A exit(exit_code);
0N/A}
0N/A
0N/Astatic const char *
0N/Asource_basename(const char *file)
0N/A{
0N/A const char *p;
0N/A
0N/A if ( file == NULL ) {
0N/A return "UnknownSourceFile";
0N/A }
0N/A p = strrchr(file, '/');
0N/A if ( p == NULL ) {
0N/A p = strrchr(file, '\\');
0N/A }
0N/A if ( p == NULL ) {
0N/A p = file;
0N/A } else {
0N/A p++; /* go past / */
0N/A }
0N/A return p;
0N/A}
0N/A
0N/Avoid
0N/Aerror_assert(const char *condition, const char *file, int line)
0N/A{
0N/A error_message("ASSERTION FAILURE: %s [%s:%d]\n", condition,
0N/A source_basename(file), line);
0N/A error_abort();
0N/A}
0N/A
0N/Avoid
0N/Aerror_handler(jboolean fatal, jvmtiError error,
0N/A const char *message, const char *file, int line)
0N/A{
0N/A char *error_name;
0N/A
0N/A if ( message==NULL ) {
0N/A message = "";
0N/A }
0N/A if ( error != JVMTI_ERROR_NONE ) {
0N/A error_name = getErrorName(error);
0N/A if ( error_name == NULL ) {
0N/A error_name = "?";
0N/A }
0N/A error_message("HPROF ERROR: %s (JVMTI Error %s(%d)) [%s:%d]\n",
0N/A message, error_name, error,
0N/A source_basename(file), line);
0N/A } else {
0N/A error_message("HPROF ERROR: %s [%s:%d]\n", message,
0N/A source_basename(file), line);
0N/A }
0N/A if ( fatal || gdata->errorexit ) {
0N/A /* If it's fatal, or the user wants termination on any error, die */
0N/A terminate_everything(9);
0N/A }
0N/A}
0N/A
0N/Avoid
0N/Adebug_message(const char * format, ...)
0N/A{
0N/A va_list ap;
0N/A
0N/A va_start(ap, format);
0N/A (void)vfprintf(stderr, format, ap);
0N/A va_end(ap);
0N/A}
0N/A
0N/Avoid
0N/Averbose_message(const char * format, ...)
0N/A{
0N/A if ( gdata->verbose ) {
0N/A va_list ap;
0N/A
0N/A va_start(ap, format);
0N/A (void)vfprintf(stderr, format, ap);
0N/A va_end(ap);
0N/A }
0N/A}