jdk/src/share/demo/jvmti/hprof/hprof_error.c
author goetz
Thu, 21 Nov 2013 18:29:34 -0800
changeset 22852 1063026e8cee
parent 14342 8435a30053c1
permissions -rw-r--r--
8028471: PPC64 (part 215): opto: Extend ImplicitNullCheck optimization. Summary: Fixed Implicit NULL check optimization for AIX, where the page at address '0' is only write-protected. Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/* The error handling logic. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Most hprof error processing and error functions are kept here, along with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   termination functions and signal handling (used in debug version only).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include <signal.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static int p = 1; /* Used with pause=y|n option */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/* Private functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
error_message(const char * format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    (void)vfprintf(stderr, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
error_abort(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* Important to remove existing signal handler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    (void)signal(SIGABRT, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    error_message("HPROF DUMPING CORE\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    abort();        /* Sends SIGABRT signal, usually also caught by libjvm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
signal_handler(int sig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* Caught a signal, most likely a SIGABRT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    error_message("HPROF SIGNAL %d TERMINATED PROCESS\n", sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    error_abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
setup_signal_handler(int sig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* Only if debug version or debug=y */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    if ( gdata->debug ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        (void)signal(sig, (void(*)(int))(void*)&signal_handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
terminate_everything(jint exit_code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    if ( exit_code > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /* Could be a fatal error or assert error or a sanity error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        error_message("HPROF TERMINATED PROCESS\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if ( gdata->coredump || gdata->debug ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            /* Core dump here by request */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            error_abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* Terminate the process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    error_exit_process(exit_code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
/* External functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
error_setup(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    setup_signal_handler(SIGABRT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
error_do_pause(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    int pid = md_getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    int timeleft = 600; /* 10 minutes max */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int interval = 10;  /* 10 second message check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    error_message("\nHPROF pause for PID %d\n", (int)pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    while ( p && timeleft > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        md_sleep(interval); /* 'assign p=0' to stop pause loop */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        timeleft -= interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if ( timeleft <= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        error_message("\n HPROF pause got tired of waiting and gave up.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
error_exit_process(int exit_code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    exit(exit_code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
static const char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
source_basename(const char *file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    const char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    if ( file == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return "UnknownSourceFile";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    p = strrchr(file, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    if ( p == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        p = strrchr(file, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if ( p == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        p = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        p++; /* go past / */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
error_assert(const char *condition, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    error_message("ASSERTION FAILURE: %s [%s:%d]\n", condition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        source_basename(file), line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    error_abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
error_handler(jboolean fatal, jvmtiError error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                const char *message, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    char *error_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    if ( message==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        message = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if ( error != JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        error_name = getErrorName(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if ( error_name == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            error_name = "?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        error_message("HPROF ERROR: %s (JVMTI Error %s(%d)) [%s:%d]\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            message, error_name, error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            source_basename(file), line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        error_message("HPROF ERROR: %s [%s:%d]\n", message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            source_basename(file), line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if ( fatal || gdata->errorexit ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        /* If it's fatal, or the user wants termination on any error, die */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        terminate_everything(9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
debug_message(const char * format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    (void)vfprintf(stderr, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
verbose_message(const char * format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    if ( gdata->verbose ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        (void)vfprintf(stderr, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}