bytecodeTracer.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_bytecodeTracer.cpp.incl"
27
28
29#ifndef PRODUCT
30
31// Standard closure for BytecodeTracer: prints the current bytecode
32// and its attributes using bytecode-specific information.
33
34class BytecodePrinter: public BytecodeClosure {
35 private:
36  // %%% This field is not GC-ed, and so can contain garbage
37  // between critical sections.  Use only pointer-comparison
38  // operations on the pointer, except within a critical section.
39  // (Also, ensure that occasional false positives are benign.)
40  methodOop _current_method;
41  bool      _is_wide;
42  address   _next_pc;                // current decoding position
43
44  void      align()                  { _next_pc = (address)round_to((intptr_t)_next_pc, sizeof(jint)); }
45  int       get_byte()               { return *(jbyte*) _next_pc++; }  // signed
46  short     get_short()              { short i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }
47  int       get_int()                { int i=Bytes::get_Java_u4(_next_pc); _next_pc+=4; return i; }
48
49  int       get_index()              { return *(address)_next_pc++; }
50  int       get_big_index()          { int i=Bytes::get_Java_u2(_next_pc); _next_pc+=2; return i; }
51  int       get_index_special()      { return (is_wide()) ? get_big_index() : get_index(); }
52  methodOop method()                 { return _current_method; }
53  bool      is_wide()                { return _is_wide; }
54
55
56  void      print_constant(int i, outputStream* st = tty);
57  void      print_attributes(Bytecodes::Code code, int bci, outputStream* st = tty);
58  void      bytecode_epilog(int bci, outputStream* st = tty);
59
60 public:
61  BytecodePrinter() {
62    _is_wide = false;
63  }
64
65  // This method is called while executing the raw bytecodes, so none of
66  // the adjustments that BytecodeStream performs applies.
67  void trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {
68    ResourceMark rm;
69    if (_current_method != method()) {
70      // Note 1: This code will not work as expected with true MT/MP.
71      //         Need an explicit lock or a different solution.
72      // It is possible for this block to be skipped, if a garbage
73      // _current_method pointer happens to have the same bits as
74      // the incoming method.  We could lose a line of trace output.
75      // This is acceptable in a debug-only feature.
76      st->cr();
77      st->print("[%d] ", (int) Thread::current()->osthread()->thread_id());
78      method->print_name(st);
79      st->cr();
80      _current_method = method();
81    }
82    Bytecodes::Code code;
83    if (is_wide()) {
84      // bcp wasn't advanced if previous bytecode was _wide.
85      code = Bytecodes::code_at(bcp+1);
86    } else {
87      code = Bytecodes::code_at(bcp);
88    }
89    int bci = bcp - method->code_base();
90    st->print("[%d] ", (int) Thread::current()->osthread()->thread_id());
91    if (Verbose) {
92      st->print("%8d  %4d  " INTPTR_FORMAT " " INTPTR_FORMAT " %s",
93           BytecodeCounter::counter_value(), bci, tos, tos2, Bytecodes::name(code));
94    } else {
95      st->print("%8d  %4d  %s",
96           BytecodeCounter::counter_value(), bci, Bytecodes::name(code));
97    }
98    _next_pc = is_wide() ? bcp+2 : bcp+1;
99    print_attributes(code, bci);
100    // Set is_wide for the next one, since the caller of this doesn't skip
101    // the next bytecode.
102    _is_wide = (code == Bytecodes::_wide);
103  }
104
105  // Used for methodOop::print_codes().  The input bcp comes from
106  // BytecodeStream, which will skip wide bytecodes.
107  void trace(methodHandle method, address bcp, outputStream* st) {
108    _current_method = method();
109    ResourceMark rm;
110    Bytecodes::Code code = Bytecodes::code_at(bcp);
111    // Set is_wide
112    _is_wide = (code == Bytecodes::_wide);
113    if (is_wide()) {
114      code = Bytecodes::code_at(bcp+1);
115    }
116    int bci = bcp - method->code_base();
117    // Print bytecode index and name
118    if (is_wide()) {
119      st->print("%d %s_w", bci, Bytecodes::name(code));
120    } else {
121      st->print("%d %s", bci, Bytecodes::name(code));
122    }
123    _next_pc = is_wide() ? bcp+2 : bcp+1;
124    print_attributes(code, bci, st);
125    bytecode_epilog(bci, st);
126  }
127};
128
129
130// Implementation of BytecodeTracer
131
132// %%% This set_closure thing seems overly general, given that
133// nobody uses it.  Also, if BytecodePrinter weren't hidden
134// then methodOop could use instances of it directly and it
135// would be easier to remove races on _current_method and bcp.
136// Since this is not product functionality, we can defer cleanup.
137
138BytecodeClosure* BytecodeTracer::_closure = NULL;
139
140static BytecodePrinter std_closure;
141BytecodeClosure* BytecodeTracer::std_closure() {
142  return &::std_closure;
143}
144
145
146void BytecodeTracer::trace(methodHandle method, address bcp, uintptr_t tos, uintptr_t tos2, outputStream* st) {
147  if (TraceBytecodes && BytecodeCounter::counter_value() >= TraceBytecodesAt) {
148    ttyLocker ttyl;  // 5065316: keep the following output coherent
149    // The ttyLocker also prevents races between two threads
150    // trying to use the single instance of BytecodePrinter.
151    // Using the ttyLocker prevents the system from coming to
152    // a safepoint within this code, which is sensitive to methodOop
153    // movement.
154    //
155    // There used to be a leaf mutex here, but the ttyLocker will
156    // work just as well, as long as the printing operations never block.
157    //
158    // We put the locker on the static trace method, not the
159    // virtual one, because the clients of this module go through
160    // the static method.
161    _closure->trace(method, bcp, tos, tos2, st);
162  }
163}
164
165void BytecodeTracer::trace(methodHandle method, address bcp, outputStream* st) {
166  ttyLocker ttyl;  // 5065316: keep the following output coherent
167  _closure->trace(method, bcp, st);
168}
169
170void print_oop(oop value, outputStream* st) {
171  if (value == NULL) {
172    st->print_cr(" NULL");
173  } else {
174    EXCEPTION_MARK;
175    Handle h_value (THREAD, value);
176    symbolHandle sym = java_lang_String::as_symbol(h_value, CATCH);
177    if (sym->utf8_length() > 32) {
178      st->print_cr(" ....");
179    } else {
180      sym->print_on(st); st->cr();
181    }
182  }
183}
184
185void BytecodePrinter::print_constant(int i, outputStream* st) {
186  constantPoolOop constants = method()->constants();
187  constantTag tag = constants->tag_at(i);
188
189  if (tag.is_int()) {
190    st->print_cr(" " INT32_FORMAT, constants->int_at(i));
191  } else if (tag.is_long()) {
192    st->print_cr(" " INT64_FORMAT, constants->long_at(i));
193  } else if (tag.is_float()) {
194    st->print_cr(" %f", constants->float_at(i));
195  } else if (tag.is_double()) {
196    st->print_cr(" %f", constants->double_at(i));
197  } else if (tag.is_string()) {
198    oop string = constants->resolved_string_at(i);
199    print_oop(string, st);
200  } else if (tag.is_unresolved_string()) {
201    st->print_cr(" <unresolved string at %d>", i);
202  } else if (tag.is_klass()) {
203    st->print_cr(" %s", constants->resolved_klass_at(i)->klass_part()->external_name());
204  } else if (tag.is_unresolved_klass()) {
205    st->print_cr(" <unresolved klass at %d>", i);
206  } else ShouldNotReachHere();
207}
208
209
210void BytecodePrinter::print_attributes(Bytecodes::Code code, int bci, outputStream* st) {
211  // Show attributes of pre-rewritten codes
212  code = Bytecodes::java_code(code);
213  // If the code doesn't have any fields there's nothing to print.
214  // note this is ==1 because the tableswitch and lookupswitch are
215  // zero size (for some reason) and we want to print stuff out for them.
216  if (Bytecodes::length_for(code) == 1) {
217    st->cr();
218    return;
219  }
220
221  switch(code) {
222    // Java specific bytecodes only matter.
223    case Bytecodes::_bipush:
224      st->print_cr(" " INT32_FORMAT, get_byte());
225      break;
226    case Bytecodes::_sipush:
227      st->print_cr(" " INT32_FORMAT, get_short());
228      break;
229    case Bytecodes::_ldc:
230      print_constant(get_index(), st);
231      break;
232
233    case Bytecodes::_ldc_w:
234    case Bytecodes::_ldc2_w:
235      print_constant(get_big_index(), st);
236      break;
237
238    case Bytecodes::_iload:
239    case Bytecodes::_lload:
240    case Bytecodes::_fload:
241    case Bytecodes::_dload:
242    case Bytecodes::_aload:
243    case Bytecodes::_istore:
244    case Bytecodes::_lstore:
245    case Bytecodes::_fstore:
246    case Bytecodes::_dstore:
247    case Bytecodes::_astore:
248      st->print_cr(" #%d", get_index_special());
249      break;
250
251    case Bytecodes::_iinc:
252      { int index = get_index_special();
253        jint offset = is_wide() ? get_short(): get_byte();
254        st->print_cr(" #%d " INT32_FORMAT, index, offset);
255      }
256      break;
257
258    case Bytecodes::_newarray: {
259        BasicType atype = (BasicType)get_index();
260        const char* str = type2name(atype);
261        if (str == NULL || atype == T_OBJECT || atype == T_ARRAY) {
262          assert(false, "Unidentified basic type");
263        }
264        st->print_cr(" %s", str);
265      }
266      break;
267    case Bytecodes::_anewarray: {
268        int klass_index = get_big_index();
269        constantPoolOop constants = method()->constants();
270        symbolOop name = constants->klass_name_at(klass_index);
271        st->print_cr(" %s ", name->as_C_string());
272      }
273      break;
274    case Bytecodes::_multianewarray: {
275        int klass_index = get_big_index();
276        int nof_dims = get_index();
277        constantPoolOop constants = method()->constants();
278        symbolOop name = constants->klass_name_at(klass_index);
279        st->print_cr(" %s %d", name->as_C_string(), nof_dims);
280      }
281      break;
282
283    case Bytecodes::_ifeq:
284    case Bytecodes::_ifnull:
285    case Bytecodes::_iflt:
286    case Bytecodes::_ifle:
287    case Bytecodes::_ifne:
288    case Bytecodes::_ifnonnull:
289    case Bytecodes::_ifgt:
290    case Bytecodes::_ifge:
291    case Bytecodes::_if_icmpeq:
292    case Bytecodes::_if_icmpne:
293    case Bytecodes::_if_icmplt:
294    case Bytecodes::_if_icmpgt:
295    case Bytecodes::_if_icmple:
296    case Bytecodes::_if_icmpge:
297    case Bytecodes::_if_acmpeq:
298    case Bytecodes::_if_acmpne:
299    case Bytecodes::_goto:
300    case Bytecodes::_jsr:
301      st->print_cr(" %d", bci + get_short());
302      break;
303
304    case Bytecodes::_goto_w:
305    case Bytecodes::_jsr_w:
306      st->print_cr(" %d", bci + get_int());
307      break;
308
309    case Bytecodes::_ret: st->print_cr(" %d", get_index_special()); break;
310
311    case Bytecodes::_tableswitch:
312      { align();
313        int  default_dest = bci + get_int();
314        int  lo           = get_int();
315        int  hi           = get_int();
316        int  len          = hi - lo + 1;
317        jint* dest        = NEW_RESOURCE_ARRAY(jint, len);
318        for (int i = 0; i < len; i++) {
319          dest[i] = bci + get_int();
320        }
321        st->print(" %d " INT32_FORMAT " " INT32_FORMAT " ",
322                      default_dest, lo, hi);
323        int first = true;
324        for (int ll = lo; ll <= hi; ll++, first = false)  {
325          int idx = ll - lo;
326          const char *format = first ? " %d:" INT32_FORMAT " (delta: %d)" :
327                                       ", %d:" INT32_FORMAT " (delta: %d)";
328          st->print(format, ll, dest[idx], dest[idx]-bci);
329        }
330        st->cr();
331      }
332      break;
333    case Bytecodes::_lookupswitch:
334      { align();
335        int  default_dest = bci + get_int();
336        int  len          = get_int();
337        jint* key         = NEW_RESOURCE_ARRAY(jint, len);
338        jint* dest        = NEW_RESOURCE_ARRAY(jint, len);
339        for (int i = 0; i < len; i++) {
340          key [i] = get_int();
341          dest[i] = bci + get_int();
342        };
343        st->print(" %d %d ", default_dest, len);
344        bool first = true;
345        for (int ll = 0; ll < len; ll++, first = false)  {
346          const char *format = first ? " " INT32_FORMAT ":" INT32_FORMAT :
347                                       ", " INT32_FORMAT ":" INT32_FORMAT ;
348          st->print(format, key[ll], dest[ll]);
349        }
350        st->cr();
351      }
352      break;
353
354    case Bytecodes::_putstatic:
355    case Bytecodes::_getstatic:
356    case Bytecodes::_putfield:
357    case Bytecodes::_getfield: {
358        int i = get_big_index();
359        constantPoolOop constants = method()->constants();
360        symbolOop field = constants->name_ref_at(i);
361        st->print_cr(" %d <%s>", i, field->as_C_string());
362      }
363      break;
364
365    case Bytecodes::_invokevirtual:
366    case Bytecodes::_invokespecial:
367    case Bytecodes::_invokestatic:
368      { int i = get_big_index();
369        constantPoolOop constants = method()->constants();
370        symbolOop name = constants->name_ref_at(i);
371        symbolOop signature = constants->signature_ref_at(i);
372        st->print_cr(" %d <%s> <%s> ", i, name->as_C_string(), signature->as_C_string());
373      }
374      break;
375
376    case Bytecodes::_invokeinterface:
377      { int i = get_big_index();
378        int n = get_index();
379        get_index();
380        constantPoolOop constants = method()->constants();
381        symbolOop name = constants->name_ref_at(i);
382        symbolOop signature = constants->signature_ref_at(i);
383        st->print_cr(" %d <%s> <%s> %d", i, name->as_C_string(), signature->as_C_string(), n);
384      }
385      break;
386
387    case Bytecodes::_new:
388    case Bytecodes::_checkcast:
389    case Bytecodes::_instanceof:
390      { int i = get_big_index();
391        constantPoolOop constants = method()->constants();
392        symbolOop name = constants->klass_name_at(i);
393        st->print_cr(" %d <%s>", i, name->as_C_string());
394      }
395      break;
396
397    case Bytecodes::_wide:
398      // length is zero not one, but printed with no more info.
399      break;
400
401    default:
402      ShouldNotReachHere();
403      break;
404  }
405}
406
407
408void BytecodePrinter::bytecode_epilog(int bci, outputStream* st) {
409  methodDataOop mdo = method()->method_data();
410  if (mdo != NULL) {
411    ProfileData* data = mdo->bci_to_data(bci);
412    if (data != NULL) {
413      st->print("  %d", mdo->dp_to_di(data->dp()));
414      st->fill_to(6);
415      data->print_data_on(st);
416    }
417  }
418}
419#endif // PRODUCT
420