1 /* 2 * Copyright (c) 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 #include "precompiled.hpp" 26 #include "classfile/classLoaderData.hpp" 27 #include "memory/metaspaceTracer.hpp" 28 #include "runtime/frame.hpp" 29 #include "runtime/os.hpp" 30 #include "runtime/thread.inline.hpp" 31 #include "trace/tracing.hpp" 32 #include "trace/traceBackend.hpp" 33 #include "utilities/decoder.hpp" 34 #include "utilities/ostream.hpp" 35 36 void MetaspaceTracer::report_gc_threshold(size_t old_val, 37 size_t new_val, 38 MetaspaceGCThresholdUpdater::Type updater) const { 39 EventMetaspaceGCThreshold event; 40 if (event.should_commit()) { 41 event.set_oldValue(old_val); 42 event.set_newValue(new_val); 43 event.set_updater((u1)updater); 44 event.commit(); 45 } 46 } 47 48 static bool get_method_name(address pc, char *buf, size_t buflen) { 49 if (os::address_is_in_vm(pc) && Decoder::can_decode_C_frame_in_vm()) { 50 return os::dll_address_to_function_name(pc, buf, (int) buflen, 0); 51 } 52 return false; 53 } 54 55 static bool is_valid_frame_in_vm(const frame* f) { 56 return f->pc() && os::address_is_in_vm(f->pc()) && !f->is_java_frame(); 57 } 58 59 static void write_vm_stack_trace(outputStream *stream) { 60 const size_t max_num_frames = 64; 61 size_t num_frames = 0; 62 63 const size_t buflen = 256; 64 char *buf = NEW_RESOURCE_ARRAY(char, buflen); 65 66 Thread* current_thread = Thread::current(); 67 frame fr = os::current_frame(); 68 while (is_valid_frame_in_vm(&fr) && num_frames < max_num_frames) { 69 if (get_method_name(fr.pc(), buf, buflen)) { 70 stream->print_cr("%s", buf); 71 } else { 72 stream->print_cr(PTR_FORMAT, fr.pc()); 73 } 74 75 // Check if it safe to get the caller's stack frame. 76 if (fr.safe_for_sender(current_thread)) { 77 fr = os::get_sender_for_C_frame(&fr); 78 } else { 79 // Not safe to traverse the stack any further. 80 return; 81 } 82 83 num_frames++; 84 } 85 } 86 87 void MetaspaceTracer::report_metaspace_allocation_failure(ClassLoaderData *cld, 88 size_t word_size, 89 MetaspaceObj::Type objtype, 90 Metaspace::MetadataType mdtype) const { 91 EventMetaspaceAllocationFailure event; 92 if (event.should_commit()) { 93 if (cld->is_anonymous()) { 94 event.set_classLoader(NULL); 95 event.set_anonymousClassLoader(true); 96 } else { 97 if (cld->is_the_null_class_loader_data()) { 98 event.set_classLoader((Klass*) NULL); 99 } else { 100 event.set_classLoader(cld->class_loader()->klass()); 101 } 102 event.set_anonymousClassLoader(false); 103 } 104 105 #ifdef _WINDOWS 106 // Can not walk the stack on Windows using the frame pointer. 107 event.set_nativeStackTrace(NULL); 108 #else 109 ResourceMark rm; 110 stringStream stack_trace; 111 write_vm_stack_trace(&stack_trace); 112 event.set_nativeStackTrace(stack_trace.as_string()); 113 #endif 114 115 event.set_size(word_size * BytesPerWord); 116 event.set_metadataType((u1) mdtype); 117 event.set_metaspaceObjectType((u1) objtype); 118 event.commit(); 119 } 120 }