src/share/vm/prims/jvmtiTrace.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/prims/jvmtiTrace.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,298 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "jvmtifiles/jvmtiEnv.hpp"
    1.30 +#include "prims/jvmtiTrace.hpp"
    1.31 +
    1.32 +//
    1.33 +// class JvmtiTrace
    1.34 +//
    1.35 +// Support for JVMTI tracing code
    1.36 +//
    1.37 +// ------------
    1.38 +// Usage:
    1.39 +//    -XX:TraceJVMTI=DESC,DESC,DESC
    1.40 +//
    1.41 +//    DESC is   DOMAIN ACTION KIND
    1.42 +//
    1.43 +//    DOMAIN is function name
    1.44 +//              event name
    1.45 +//              "all" (all functions and events)
    1.46 +//              "func" (all functions except boring)
    1.47 +//              "allfunc" (all functions)
    1.48 +//              "event" (all events)
    1.49 +//              "ec" (event controller)
    1.50 +//
    1.51 +//    ACTION is "+" (add)
    1.52 +//              "-" (remove)
    1.53 +//
    1.54 +//    KIND is
    1.55 +//     for func
    1.56 +//              "i" (input params)
    1.57 +//              "e" (error returns)
    1.58 +//              "o" (output)
    1.59 +//     for event
    1.60 +//              "t" (event triggered aka posted)
    1.61 +//              "s" (event sent)
    1.62 +//
    1.63 +// Example:
    1.64 +//            -XX:TraceJVMTI=ec+,GetCallerFrame+ie,Breakpoint+s
    1.65 +
    1.66 +#ifdef JVMTI_TRACE
    1.67 +
    1.68 +bool JvmtiTrace::_initialized = false;
    1.69 +bool JvmtiTrace::_on = false;
    1.70 +bool JvmtiTrace::_trace_event_controller = false;
    1.71 +
    1.72 +void JvmtiTrace::initialize() {
    1.73 +  if (_initialized) {
    1.74 +    return;
    1.75 +  }
    1.76 +  SafeResourceMark rm;
    1.77 +
    1.78 +  const char *very_end;
    1.79 +  const char *curr;
    1.80 +  if (TraceJVMTI != NULL) {
    1.81 +    curr = TraceJVMTI;
    1.82 +  } else {
    1.83 +    curr = "";  // hack in fixed tracing here
    1.84 +  }
    1.85 +  very_end = curr + strlen(curr);
    1.86 +  while (curr < very_end) {
    1.87 +    const char *curr_end = strchr(curr, ',');
    1.88 +    if (curr_end == NULL) {
    1.89 +      curr_end = very_end;
    1.90 +    }
    1.91 +    const char *op_pos = strchr(curr, '+');
    1.92 +    const char *minus_pos = strchr(curr, '-');
    1.93 +    if (minus_pos != NULL && (minus_pos < op_pos || op_pos == NULL)) {
    1.94 +      op_pos = minus_pos;
    1.95 +    }
    1.96 +    char op;
    1.97 +    const char *flags = op_pos + 1;
    1.98 +    const char *flags_end = curr_end;
    1.99 +    if (op_pos == NULL || op_pos > curr_end) {
   1.100 +      flags = "ies";
   1.101 +      flags_end = flags + strlen(flags);
   1.102 +      op_pos = curr_end;
   1.103 +      op = '+';
   1.104 +    } else {
   1.105 +      op = *op_pos;
   1.106 +    }
   1.107 +    jbyte bits = 0;
   1.108 +    for (; flags < flags_end; ++flags) {
   1.109 +      switch (*flags) {
   1.110 +      case 'i':
   1.111 +        bits |= SHOW_IN;
   1.112 +        break;
   1.113 +      case 'I':
   1.114 +        bits |= SHOW_IN_DETAIL;
   1.115 +        break;
   1.116 +      case 'e':
   1.117 +        bits |= SHOW_ERROR;
   1.118 +        break;
   1.119 +      case 'o':
   1.120 +        bits |= SHOW_OUT;
   1.121 +        break;
   1.122 +      case 'O':
   1.123 +        bits |= SHOW_OUT_DETAIL;
   1.124 +        break;
   1.125 +      case 't':
   1.126 +        bits |= SHOW_EVENT_TRIGGER;
   1.127 +        break;
   1.128 +      case 's':
   1.129 +        bits |= SHOW_EVENT_SENT;
   1.130 +        break;
   1.131 +      default:
   1.132 +        tty->print_cr("Invalid trace flag '%c'", *flags);
   1.133 +        break;
   1.134 +      }
   1.135 +    }
   1.136 +    const int FUNC = 1;
   1.137 +    const int EXCLUDE  = 2;
   1.138 +    const int ALL_FUNC = 4;
   1.139 +    const int EVENT = 8;
   1.140 +    const int ALL_EVENT = 16;
   1.141 +    int domain = 0;
   1.142 +    size_t len = op_pos - curr;
   1.143 +    if (op_pos == curr) {
   1.144 +      domain = ALL_FUNC | FUNC | ALL_EVENT | EVENT | EXCLUDE;
   1.145 +    } else if (len==3 && strncmp(curr, "all", 3)==0) {
   1.146 +      domain = ALL_FUNC | FUNC | ALL_EVENT | EVENT;
   1.147 +    } else if (len==7 && strncmp(curr, "allfunc", 7)==0) {
   1.148 +      domain = ALL_FUNC | FUNC;
   1.149 +    } else if (len==4 && strncmp(curr, "func", 4)==0) {
   1.150 +      domain = ALL_FUNC | FUNC | EXCLUDE;
   1.151 +    } else if (len==8 && strncmp(curr, "allevent", 8)==0) {
   1.152 +      domain = ALL_EVENT | EVENT;
   1.153 +    } else if (len==5 && strncmp(curr, "event", 5)==0) {
   1.154 +      domain = ALL_EVENT | EVENT;
   1.155 +    } else if (len==2 && strncmp(curr, "ec", 2)==0) {
   1.156 +      _trace_event_controller = true;
   1.157 +      tty->print_cr("JVMTI Tracing the event controller");
   1.158 +    } else {
   1.159 +      domain = FUNC | EVENT;  // go searching
   1.160 +    }
   1.161 +
   1.162 +    int exclude_index = 0;
   1.163 +    if (domain & FUNC) {
   1.164 +      if (domain & ALL_FUNC) {
   1.165 +        if (domain & EXCLUDE) {
   1.166 +          tty->print("JVMTI Tracing all significant functions");
   1.167 +        } else {
   1.168 +          tty->print_cr("JVMTI Tracing all functions");
   1.169 +        }
   1.170 +      }
   1.171 +      for (int i = 0; i <= _max_function_index; ++i) {
   1.172 +        if (domain & EXCLUDE && i == _exclude_functions[exclude_index]) {
   1.173 +          ++exclude_index;
   1.174 +        } else {
   1.175 +          bool do_op = false;
   1.176 +          if (domain & ALL_FUNC) {
   1.177 +            do_op = true;
   1.178 +          } else {
   1.179 +            const char *fname = function_name(i);
   1.180 +            if (fname != NULL) {
   1.181 +              size_t fnlen = strlen(fname);
   1.182 +              if (len==fnlen && strncmp(curr, fname, fnlen)==0) {
   1.183 +                tty->print_cr("JVMTI Tracing the function: %s", fname);
   1.184 +                do_op = true;
   1.185 +              }
   1.186 +            }
   1.187 +          }
   1.188 +          if (do_op) {
   1.189 +            if (op == '+') {
   1.190 +              _trace_flags[i] |= bits;
   1.191 +            } else {
   1.192 +              _trace_flags[i] &= ~bits;
   1.193 +            }
   1.194 +            _on = true;
   1.195 +          }
   1.196 +        }
   1.197 +      }
   1.198 +    }
   1.199 +    if (domain & EVENT) {
   1.200 +      if (domain & ALL_EVENT) {
   1.201 +        tty->print_cr("JVMTI Tracing all events");
   1.202 +      }
   1.203 +      for (int i = 0; i <= _max_event_index; ++i) {
   1.204 +        bool do_op = false;
   1.205 +        if (domain & ALL_EVENT) {
   1.206 +          do_op = true;
   1.207 +        } else {
   1.208 +          const char *ename = event_name(i);
   1.209 +          if (ename != NULL) {
   1.210 +            size_t evtlen = strlen(ename);
   1.211 +            if (len==evtlen && strncmp(curr, ename, evtlen)==0) {
   1.212 +              tty->print_cr("JVMTI Tracing the event: %s", ename);
   1.213 +              do_op = true;
   1.214 +            }
   1.215 +          }
   1.216 +        }
   1.217 +        if (do_op) {
   1.218 +          if (op == '+') {
   1.219 +            _event_trace_flags[i] |= bits;
   1.220 +          } else {
   1.221 +            _event_trace_flags[i] &= ~bits;
   1.222 +          }
   1.223 +          _on = true;
   1.224 +        }
   1.225 +      }
   1.226 +    }
   1.227 +    if (!_on && (domain & (FUNC|EVENT))) {
   1.228 +      tty->print_cr("JVMTI Trace domain not found");
   1.229 +    }
   1.230 +    curr = curr_end + 1;
   1.231 +  }
   1.232 +  _initialized = true;
   1.233 +}
   1.234 +
   1.235 +
   1.236 +void JvmtiTrace::shutdown() {
   1.237 +  int i;
   1.238 +  _on = false;
   1.239 +  _trace_event_controller = false;
   1.240 +  for (i = 0; i <= _max_function_index; ++i) {
   1.241 +    _trace_flags[i] = 0;
   1.242 +  }
   1.243 +  for (i = 0; i <= _max_event_index; ++i) {
   1.244 +    _event_trace_flags[i] = 0;
   1.245 +  }
   1.246 +}
   1.247 +
   1.248 +
   1.249 +const char* JvmtiTrace::enum_name(const char** names, const jint* values, jint value) {
   1.250 +  for (int index = 0; names[index] != 0; ++index) {
   1.251 +    if (values[index] == value) {
   1.252 +      return names[index];
   1.253 +    }
   1.254 +  }
   1.255 +  return "*INVALID-ENUM-VALUE*";
   1.256 +}
   1.257 +
   1.258 +
   1.259 +// return a valid string no matter what state the thread is in
   1.260 +const char *JvmtiTrace::safe_get_thread_name(Thread *thread) {
   1.261 +  if (thread == NULL) {
   1.262 +    return "NULL";
   1.263 +  }
   1.264 +  if (!thread->is_Java_thread()) {
   1.265 +    return thread->name();
   1.266 +  }
   1.267 +  JavaThread *java_thread = (JavaThread *)thread;
   1.268 +  oop threadObj = java_thread->threadObj();
   1.269 +  if (threadObj == NULL) {
   1.270 +    return "NULL";
   1.271 +  }
   1.272 +  typeArrayOop name = java_lang_Thread::name(threadObj);
   1.273 +  if (name == NULL) {
   1.274 +    return "<NOT FILLED IN>";
   1.275 +  }
   1.276 +  return UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
   1.277 +}
   1.278 +
   1.279 +
   1.280 +// return the name of the current thread
   1.281 +const char *JvmtiTrace::safe_get_current_thread_name() {
   1.282 +  if (JvmtiEnv::is_vm_live()) {
   1.283 +    return JvmtiTrace::safe_get_thread_name(Thread::current());
   1.284 +  } else {
   1.285 +    return "VM not live";
   1.286 +  }
   1.287 +}
   1.288 +
   1.289 +// return a valid string no matter what the state of k_mirror
   1.290 +const char * JvmtiTrace::get_class_name(oop k_mirror) {
   1.291 +  if (java_lang_Class::is_primitive(k_mirror)) {
   1.292 +    return "primitive";
   1.293 +  }
   1.294 +  Klass* k_oop = java_lang_Class::as_Klass(k_mirror);
   1.295 +  if (k_oop == NULL) {
   1.296 +    return "INVALID";
   1.297 +  }
   1.298 +  return k_oop->external_name();
   1.299 +}
   1.300 +
   1.301 +#endif /*JVMTI_TRACE */

mercurial