1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_TRACE_TRACESTREAM_HPP
  26 #define SHARE_VM_TRACE_TRACESTREAM_HPP
  27 
  28 #include "utilities/macros.hpp"
  29 #if INCLUDE_TRACE
  30 #include "classfile/classLoaderData.hpp"
  31 #include "classfile/javaClasses.inline.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "oops/klass.hpp"
  34 #include "oops/method.hpp"
  35 #include "oops/symbol.hpp"
  36 #include "utilities/ostream.hpp"
  37 
  38 class TraceStream : public StackObj {
  39  private:
  40   outputStream& _st;
  41 
  42  public:
  43   TraceStream(outputStream& stream): _st(stream) {}
  44 
  45   void print_val(const char* label, u1 val) {
  46     _st.print("%s = " UINT32_FORMAT, label, val);
  47   }
  48 
  49   void print_val(const char* label, u2 val) {
  50     _st.print("%s = " UINT32_FORMAT, label, val);
  51   }
  52 
  53   void print_val(const char* label, s2 val) {
  54     _st.print("%s = " INT32_FORMAT, label, val);
  55   }
  56 
  57   void print_val(const char* label, u4 val) {
  58     _st.print("%s = " UINT32_FORMAT, label, val);
  59   }
  60 
  61   void print_val(const char* label, s4 val) {
  62     _st.print("%s = " INT32_FORMAT, label, val);
  63   }
  64 
  65   void print_val(const char* label, u8 val) {
  66     _st.print("%s = " UINT64_FORMAT, label, val);
  67   }
  68 
  69   void print_val(const char* label, s8 val) {
  70     _st.print("%s = " INT64_FORMAT, label, (int64_t) val);
  71   }
  72 
  73   void print_val(const char* label, bool val) {
  74     _st.print("%s = %s", label, val ? "true" : "false");
  75   }
  76 
  77   void print_val(const char* label, float val) {
  78     _st.print("%s = %f", label, val);
  79   }
  80 
  81   void print_val(const char* label, double val) {
  82     _st.print("%s = %f", label, val);
  83   }
  84 
  85   void print_val(const char* label, const Klass* const val) {
  86     ResourceMark rm;
  87     const char* description = "NULL";
  88     if (val != NULL) {
  89       Symbol* name = val->name();
  90       if (name != NULL) {
  91         description = name->as_C_string();
  92       }
  93     }
  94     _st.print("%s = %s", label, description);
  95   }
  96 
  97   void print_val(const char* label, const Method* const val) {
  98     ResourceMark rm;
  99     const char* description = "NULL";
 100     if (val != NULL) {
 101       description = val->name_and_sig_as_C_string();
 102     }
 103     _st.print("%s = %s", label, description);
 104   }
 105 
 106   void print_val(const char* label, const ClassLoaderData* const cld) {
 107     ResourceMark rm;
 108     if (cld == NULL || cld->is_anonymous()) {
 109       _st.print("%s = NULL", label);
 110       return;
 111     }
 112     const oop class_loader_oop = cld->class_loader();
 113     if (class_loader_oop == NULL) {
 114       _st.print("%s = NULL", label);
 115       return;
 116     }
 117     const char* class_loader_name = "NULL";
 118     const char* klass_name = "NULL";
 119     const oop class_loader_name_oop =
 120       java_lang_ClassLoader::name(class_loader_oop);
 121     if (class_loader_name_oop != NULL) {
 122       class_loader_name =
 123         java_lang_String::as_utf8_string(class_loader_name_oop);
 124     }
 125     const Klass* const k = class_loader_oop->klass();
 126     const Symbol* klass_name_sym = k->name();
 127     if (klass_name_sym != NULL) {
 128       klass_name = klass_name_sym->as_C_string();
 129     }
 130     _st.print("%s = name=%s class=%s", label, class_loader_name, klass_name);
 131   }
 132 
 133   void print_val(const char* label, const char* val) {
 134     _st.print("%s = '%s'", label, val);
 135   }
 136 
 137   void print(const char* val) {
 138     _st.print("%s", val);
 139   }
 140 };
 141 
 142 #endif // INCLUDE_TRACE
 143 #endif // SHARE_VM_TRACE_TRACESTREAM_HPP