|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +#ifndef SHARE_UTILITIES_NATIVESTACKPRINTER_HPP |
| 25 | +#define SHARE_UTILITIES_NATIVESTACKPRINTER_HPP |
| 26 | + |
| 27 | +#include "memory/allocation.hpp" |
| 28 | +#include "runtime/frame.hpp" |
| 29 | +#include "runtime/os.hpp" |
| 30 | +#include "utilities/globalDefinitions.hpp" |
| 31 | + |
| 32 | +// Forward declarations |
| 33 | +class outputStream; |
| 34 | +class Thread; |
| 35 | + |
| 36 | +// Helper class to do native stack printing from various contexts |
| 37 | +// including during crash reporting. |
| 38 | +// The NativeStackPrinter is created with the basic context information |
| 39 | +// available from the caller. Then the print_stack function is called |
| 40 | +// to do the actual printing. |
| 41 | +class NativeStackPrinter : public StackObj { |
| 42 | + Thread* _current; // Current thread if known |
| 43 | + const void* _context; // OS crash context if known |
| 44 | + const char* _filename; // Source file name if known |
| 45 | + int _lineno; // Source file line number if known |
| 46 | + |
| 47 | + public: |
| 48 | + // Creates a NativeStackPrinter using the given additional context |
| 49 | + // information: |
| 50 | + // - the current thread is used for frame-based stack walking |
| 51 | + // - context is the crash context from the OS and can be used to get a frame; |
| 52 | + // otherwise os::current_frame() will be used |
| 53 | + // - filename and lineno provide details from the fatal error handler so we |
| 54 | + // can skip use of the Decoder for the first line (optimization) |
| 55 | + NativeStackPrinter(Thread* current_or_null, |
| 56 | + const void* context, |
| 57 | + const char* filename, |
| 58 | + int lineno) : |
| 59 | + _current(current_or_null), |
| 60 | + _context(context), |
| 61 | + _filename(filename), |
| 62 | + _lineno(lineno) { |
| 63 | + assert((_lineno == 0 && _filename == nullptr) || |
| 64 | + (_lineno > 0 && _filename != nullptr), |
| 65 | + "file name and line number need to be provided together"); |
| 66 | + } |
| 67 | + |
| 68 | + NativeStackPrinter(Thread* current_or_null) |
| 69 | + : NativeStackPrinter(current_or_null, nullptr, nullptr, 0) {} |
| 70 | + |
| 71 | + // Prints the stack of the current thread to the given stream. |
| 72 | + // We first try to print via os::platform_print_native_stack. If that |
| 73 | + // succeeds then lastpc is set and we return true. Otherwise we do a |
| 74 | + // frame walk to print the stack, and return false. |
| 75 | + // - st: the stream to print to |
| 76 | + // - buf, buf_size: temporary buffer to use for formatting output |
| 77 | + // - print_source_info: see print_stack_from_frame |
| 78 | + // - max_frames: see print_stack_from_frame |
| 79 | + // |
| 80 | + bool print_stack(outputStream* st, char* buf, int buf_size, |
| 81 | + address& lastpc, bool print_source_info, |
| 82 | + int max_frames); |
| 83 | + |
| 84 | + // Prints the stack to st by walking the frames starting from |
| 85 | + // either the context frame, else the current frame. |
| 86 | + // - st: the stream to print to |
| 87 | + // - buf, buf_size: temporary buffer to use when printing frames |
| 88 | + // - print_source_info: if true obtains source information from the Decoder |
| 89 | + // if available. (Useful but may slow down, timeout or |
| 90 | + // misfunction in error situations) |
| 91 | + // - max_frames: the maximum number of frames to print. -1 means print all. |
| 92 | + // However, StackPrintLimit sets a hard limit on the maximum. |
| 93 | + void print_stack_from_frame(outputStream* st, frame fr, |
| 94 | + char* buf, int buf_size, |
| 95 | + bool print_source_info, int max_frames); |
| 96 | + |
| 97 | + // Prints the stack to st by walking the frames starting from |
| 98 | + // either the context frame, else the current frame. |
| 99 | + void print_stack_from_frame(outputStream* st, |
| 100 | + char* buf, int buf_size, |
| 101 | + bool print_source_info, int max_frames) { |
| 102 | + frame fr = _context != nullptr ? os::fetch_frame_from_context(_context) |
| 103 | + : os::current_frame(); |
| 104 | + print_stack_from_frame(st, fr, buf, buf_size, print_source_info, max_frames); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +#endif // SHARE_UTILITIES_NATIVESTACKPRINTER_HPP |
0 commit comments