00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "hprof.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <signal.h>
00048
00049 static int p = 1;
00050
00051
00052
00053 static void
00054 error_message(const char * format, ...)
00055 {
00056 va_list ap;
00057
00058 va_start(ap, format);
00059 (void)vfprintf(stderr, format, ap);
00060 va_end(ap);
00061 }
00062
00063 static void
00064 error_abort(void)
00065 {
00066
00067 (void)signal(SIGABRT, NULL);
00068 error_message("HPROF DUMPING CORE\n");
00069 abort();
00070 }
00071
00072 static void
00073 signal_handler(int sig)
00074 {
00075
00076 error_message("HPROF SIGNAL %d TERMINATED PROCESS\n", sig);
00077 error_abort();
00078 }
00079
00080 static void
00081 setup_signal_handler(int sig)
00082 {
00083
00084 if ( gdata->debug ) {
00085 (void)signal(sig, (void(*)(int))(void*)&signal_handler);
00086 }
00087 }
00088
00089 static void
00090 terminate_everything(jint exit_code)
00091 {
00092 if ( exit_code > 0 ) {
00093
00094 error_message("HPROF TERMINATED PROCESS\n");
00095 if ( gdata->coredump || gdata->debug ) {
00096
00097 error_abort();
00098 }
00099 }
00100
00101 error_exit_process(exit_code);
00102 }
00103
00104
00105
00106 void
00107 error_setup(void)
00108 {
00109 setup_signal_handler(SIGABRT);
00110 }
00111
00112 void
00113 error_do_pause(void)
00114 {
00115 int pid = md_getpid();
00116 int timeleft = 600;
00117 int interval = 10;
00118
00119
00120 error_message("\nHPROF pause for PID %d\n", (int)pid);
00121 while ( p && timeleft > 0 ) {
00122 md_sleep(interval);
00123 timeleft -= interval;
00124 }
00125 if ( timeleft <= 0 ) {
00126 error_message("\n HPROF pause got tired of waiting and gave up.\n");
00127 }
00128 }
00129
00130 void
00131 error_exit_process(int exit_code)
00132 {
00133 exit(exit_code);
00134 }
00135
00136 static const char *
00137 source_basename(const char *file)
00138 {
00139 const char *p;
00140
00141 if ( file == NULL ) {
00142 return "UnknownSourceFile";
00143 }
00144 p = strrchr(file, '/');
00145 if ( p == NULL ) {
00146 p = strrchr(file, '\\');
00147 }
00148 if ( p == NULL ) {
00149 p = file;
00150 } else {
00151 p++;
00152 }
00153 return p;
00154 }
00155
00156 void
00157 error_assert(const char *condition, const char *file, int line)
00158 {
00159 error_message("ASSERTION FAILURE: %s [%s:%d]\n", condition,
00160 source_basename(file), line);
00161 error_abort();
00162 }
00163
00164 void
00165 error_handler(jboolean fatal, jvmtiError error,
00166 const char *message, const char *file, int line)
00167 {
00168 char *error_name;
00169
00170 if ( message==NULL ) {
00171 message = "";
00172 }
00173 if ( error != JVMTI_ERROR_NONE ) {
00174 error_name = getErrorName(error);
00175 if ( error_name == NULL ) {
00176 error_name = "?";
00177 }
00178 error_message("HPROF ERROR: %s (JVMTI Error %s(%d)) [%s:%d]\n",
00179 message, error_name, error,
00180 source_basename(file), line);
00181 } else {
00182 error_message("HPROF ERROR: %s [%s:%d]\n", message,
00183 source_basename(file), line);
00184 }
00185 if ( fatal || gdata->errorexit ) {
00186
00187 terminate_everything(9);
00188 }
00189 }
00190
00191 void
00192 debug_message(const char * format, ...)
00193 {
00194 va_list ap;
00195
00196 va_start(ap, format);
00197 (void)vfprintf(stderr, format, ap);
00198 va_end(ap);
00199 }
00200
00201 void
00202 verbose_message(const char * format, ...)
00203 {
00204 if ( gdata->verbose ) {
00205 va_list ap;
00206
00207 va_start(ap, format);
00208 (void)vfprintf(stderr, format, ap);
00209 va_end(ap);
00210 }
00211 }
00212