1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiTrace.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,297 @@ 1.4 +/* 1.5 + * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_jvmtiTrace.cpp.incl" 1.30 + 1.31 +// 1.32 +// class JvmtiTrace 1.33 +// 1.34 +// Support for JVMTI tracing code 1.35 +// 1.36 +// ------------ 1.37 +// Usage: 1.38 +// -XX:TraceJVMTI=DESC,DESC,DESC 1.39 +// 1.40 +// DESC is DOMAIN ACTION KIND 1.41 +// 1.42 +// DOMAIN is function name 1.43 +// event name 1.44 +// "all" (all functions and events) 1.45 +// "func" (all functions except boring) 1.46 +// "allfunc" (all functions) 1.47 +// "event" (all events) 1.48 +// "ec" (event controller) 1.49 +// 1.50 +// ACTION is "+" (add) 1.51 +// "-" (remove) 1.52 +// 1.53 +// KIND is 1.54 +// for func 1.55 +// "i" (input params) 1.56 +// "e" (error returns) 1.57 +// "o" (output) 1.58 +// for event 1.59 +// "t" (event triggered aka posted) 1.60 +// "s" (event sent) 1.61 +// 1.62 +// Example: 1.63 +// -XX:TraceJVMTI=ec+,GetCallerFrame+ie,Breakpoint+s 1.64 + 1.65 +#ifdef JVMTI_TRACE 1.66 + 1.67 +bool JvmtiTrace::_initialized = false; 1.68 +bool JvmtiTrace::_on = false; 1.69 +bool JvmtiTrace::_trace_event_controller = false; 1.70 + 1.71 +void JvmtiTrace::initialize() { 1.72 + if (_initialized) { 1.73 + return; 1.74 + } 1.75 + SafeResourceMark rm; 1.76 + 1.77 + const char *very_end; 1.78 + const char *curr; 1.79 + if (strlen(TraceJVMTI)) { 1.80 + curr = TraceJVMTI; 1.81 + } else { 1.82 + curr = ""; // hack in fixed tracing here 1.83 + } 1.84 + very_end = curr + strlen(curr); 1.85 + while (curr < very_end) { 1.86 + const char *curr_end = strchr(curr, ','); 1.87 + if (curr_end == NULL) { 1.88 + curr_end = very_end; 1.89 + } 1.90 + const char *op_pos = strchr(curr, '+'); 1.91 + const char *minus_pos = strchr(curr, '-'); 1.92 + if (minus_pos != NULL && (minus_pos < op_pos || op_pos == NULL)) { 1.93 + op_pos = minus_pos; 1.94 + } 1.95 + char op; 1.96 + const char *flags = op_pos + 1; 1.97 + const char *flags_end = curr_end; 1.98 + if (op_pos == NULL || op_pos > curr_end) { 1.99 + flags = "ies"; 1.100 + flags_end = flags + strlen(flags); 1.101 + op_pos = curr_end; 1.102 + op = '+'; 1.103 + } else { 1.104 + op = *op_pos; 1.105 + } 1.106 + jbyte bits = 0; 1.107 + for (; flags < flags_end; ++flags) { 1.108 + switch (*flags) { 1.109 + case 'i': 1.110 + bits |= SHOW_IN; 1.111 + break; 1.112 + case 'I': 1.113 + bits |= SHOW_IN_DETAIL; 1.114 + break; 1.115 + case 'e': 1.116 + bits |= SHOW_ERROR; 1.117 + break; 1.118 + case 'o': 1.119 + bits |= SHOW_OUT; 1.120 + break; 1.121 + case 'O': 1.122 + bits |= SHOW_OUT_DETAIL; 1.123 + break; 1.124 + case 't': 1.125 + bits |= SHOW_EVENT_TRIGGER; 1.126 + break; 1.127 + case 's': 1.128 + bits |= SHOW_EVENT_SENT; 1.129 + break; 1.130 + default: 1.131 + tty->print_cr("Invalid trace flag '%c'", *flags); 1.132 + break; 1.133 + } 1.134 + } 1.135 + const int FUNC = 1; 1.136 + const int EXCLUDE = 2; 1.137 + const int ALL_FUNC = 4; 1.138 + const int EVENT = 8; 1.139 + const int ALL_EVENT = 16; 1.140 + int domain = 0; 1.141 + size_t len = op_pos - curr; 1.142 + if (op_pos == curr) { 1.143 + domain = ALL_FUNC | FUNC | ALL_EVENT | EVENT | EXCLUDE; 1.144 + } else if (len==3 && strncmp(curr, "all", 3)==0) { 1.145 + domain = ALL_FUNC | FUNC | ALL_EVENT | EVENT; 1.146 + } else if (len==7 && strncmp(curr, "allfunc", 7)==0) { 1.147 + domain = ALL_FUNC | FUNC; 1.148 + } else if (len==4 && strncmp(curr, "func", 4)==0) { 1.149 + domain = ALL_FUNC | FUNC | EXCLUDE; 1.150 + } else if (len==8 && strncmp(curr, "allevent", 8)==0) { 1.151 + domain = ALL_EVENT | EVENT; 1.152 + } else if (len==5 && strncmp(curr, "event", 5)==0) { 1.153 + domain = ALL_EVENT | EVENT; 1.154 + } else if (len==2 && strncmp(curr, "ec", 2)==0) { 1.155 + _trace_event_controller = true; 1.156 + tty->print_cr("JVMTI Tracing the event controller"); 1.157 + } else { 1.158 + domain = FUNC | EVENT; // go searching 1.159 + } 1.160 + 1.161 + int exclude_index = 0; 1.162 + if (domain & FUNC) { 1.163 + if (domain & ALL_FUNC) { 1.164 + if (domain & EXCLUDE) { 1.165 + tty->print("JVMTI Tracing all significant functions"); 1.166 + } else { 1.167 + tty->print_cr("JVMTI Tracing all functions"); 1.168 + } 1.169 + } 1.170 + for (int i = 0; i <= _max_function_index; ++i) { 1.171 + if (domain & EXCLUDE && i == _exclude_functions[exclude_index]) { 1.172 + ++exclude_index; 1.173 + } else { 1.174 + bool do_op = false; 1.175 + if (domain & ALL_FUNC) { 1.176 + do_op = true; 1.177 + } else { 1.178 + const char *fname = function_name(i); 1.179 + if (fname != NULL) { 1.180 + size_t fnlen = strlen(fname); 1.181 + if (len==fnlen && strncmp(curr, fname, fnlen)==0) { 1.182 + tty->print_cr("JVMTI Tracing the function: %s", fname); 1.183 + do_op = true; 1.184 + } 1.185 + } 1.186 + } 1.187 + if (do_op) { 1.188 + if (op == '+') { 1.189 + _trace_flags[i] |= bits; 1.190 + } else { 1.191 + _trace_flags[i] &= ~bits; 1.192 + } 1.193 + _on = true; 1.194 + } 1.195 + } 1.196 + } 1.197 + } 1.198 + if (domain & EVENT) { 1.199 + if (domain & ALL_EVENT) { 1.200 + tty->print_cr("JVMTI Tracing all events"); 1.201 + } 1.202 + for (int i = 0; i <= _max_event_index; ++i) { 1.203 + bool do_op = false; 1.204 + if (domain & ALL_EVENT) { 1.205 + do_op = true; 1.206 + } else { 1.207 + const char *ename = event_name(i); 1.208 + if (ename != NULL) { 1.209 + size_t evtlen = strlen(ename); 1.210 + if (len==evtlen && strncmp(curr, ename, evtlen)==0) { 1.211 + tty->print_cr("JVMTI Tracing the event: %s", ename); 1.212 + do_op = true; 1.213 + } 1.214 + } 1.215 + } 1.216 + if (do_op) { 1.217 + if (op == '+') { 1.218 + _event_trace_flags[i] |= bits; 1.219 + } else { 1.220 + _event_trace_flags[i] &= ~bits; 1.221 + } 1.222 + _on = true; 1.223 + } 1.224 + } 1.225 + } 1.226 + if (!_on && (domain & (FUNC|EVENT))) { 1.227 + tty->print_cr("JVMTI Trace domain not found"); 1.228 + } 1.229 + curr = curr_end + 1; 1.230 + } 1.231 + _initialized = true; 1.232 +} 1.233 + 1.234 + 1.235 +void JvmtiTrace::shutdown() { 1.236 + int i; 1.237 + _on = false; 1.238 + _trace_event_controller = false; 1.239 + for (i = 0; i <= _max_function_index; ++i) { 1.240 + _trace_flags[i] = 0; 1.241 + } 1.242 + for (i = 0; i <= _max_event_index; ++i) { 1.243 + _event_trace_flags[i] = 0; 1.244 + } 1.245 +} 1.246 + 1.247 + 1.248 +const char* JvmtiTrace::enum_name(const char** names, const jint* values, jint value) { 1.249 + for (int index = 0; names[index] != 0; ++index) { 1.250 + if (values[index] == value) { 1.251 + return names[index]; 1.252 + } 1.253 + } 1.254 + return "*INVALID-ENUM-VALUE*"; 1.255 +} 1.256 + 1.257 + 1.258 +// return a valid string no matter what state the thread is in 1.259 +const char *JvmtiTrace::safe_get_thread_name(Thread *thread) { 1.260 + if (thread == NULL) { 1.261 + return "NULL"; 1.262 + } 1.263 + if (!thread->is_Java_thread()) { 1.264 + return thread->name(); 1.265 + } 1.266 + JavaThread *java_thread = (JavaThread *)thread; 1.267 + oop threadObj = java_thread->threadObj(); 1.268 + if (threadObj == NULL) { 1.269 + return "NULL"; 1.270 + } 1.271 + typeArrayOop name = java_lang_Thread::name(threadObj); 1.272 + if (name == NULL) { 1.273 + return "<NOT FILLED IN>"; 1.274 + } 1.275 + return UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length()); 1.276 +} 1.277 + 1.278 + 1.279 +// return the name of the current thread 1.280 +const char *JvmtiTrace::safe_get_current_thread_name() { 1.281 + if (JvmtiEnv::is_vm_live()) { 1.282 + return JvmtiTrace::safe_get_thread_name(Thread::current()); 1.283 + } else { 1.284 + return "VM not live"; 1.285 + } 1.286 +} 1.287 + 1.288 +// return a valid string no matter what the state of k_mirror 1.289 +const char * JvmtiTrace::get_class_name(oop k_mirror) { 1.290 + if (java_lang_Class::is_primitive(k_mirror)) { 1.291 + return "primitive"; 1.292 + } 1.293 + klassOop k_oop = java_lang_Class::as_klassOop(k_mirror); 1.294 + if (k_oop == NULL) { 1.295 + return "INVALID"; 1.296 + } 1.297 + return Klass::cast(k_oop)->external_name(); 1.298 +} 1.299 + 1.300 +#endif /*JVMTI_TRACE */