aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_OSTREAM_HPP aoqi@0: #define SHARE_VM_UTILITIES_OSTREAM_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "runtime/timer.hpp" aoqi@0: brutisso@6904: class GCId; aoqi@0: DEBUG_ONLY(class ResourceMark;) aoqi@0: aoqi@0: // Output streams for printing aoqi@0: // aoqi@0: // Printing guidelines: aoqi@0: // Where possible, please use tty->print() and tty->print_cr(). aoqi@0: // For product mode VM warnings use warning() which internally uses tty. aoqi@0: // In places where tty is not initialized yet or too much overhead, aoqi@0: // we may use jio_printf: aoqi@0: // jio_fprintf(defaultStream::output_stream(), "Message"); aoqi@0: // This allows for redirection via -XX:+DisplayVMOutputToStdout and aoqi@0: // -XX:+DisplayVMOutputToStderr aoqi@0: class outputStream : public ResourceObj { aoqi@0: protected: aoqi@0: int _indentation; // current indentation aoqi@0: int _width; // width of the page aoqi@0: int _position; // position on the current line aoqi@0: int _newlines; // number of '\n' output so far aoqi@0: julong _precount; // number of chars output, less _position aoqi@0: TimeStamp _stamp; // for time stamps aoqi@0: aoqi@0: void update_position(const char* s, size_t len); aoqi@0: static const char* do_vsnprintf(char* buffer, size_t buflen, aoqi@0: const char* format, va_list ap, aoqi@0: bool add_cr, aoqi@0: size_t& result_len) ATTRIBUTE_PRINTF(3, 0); aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: outputStream(int width = 80); aoqi@0: outputStream(int width, bool has_time_stamps); aoqi@0: aoqi@0: // indentation aoqi@0: outputStream& indent(); aoqi@0: void inc() { _indentation++; }; aoqi@0: void dec() { _indentation--; }; aoqi@0: void inc(int n) { _indentation += n; }; aoqi@0: void dec(int n) { _indentation -= n; }; aoqi@0: int indentation() const { return _indentation; } aoqi@0: void set_indentation(int i) { _indentation = i; } aoqi@0: void fill_to(int col); aoqi@0: void move_to(int col, int slop = 6, int min_space = 2); aoqi@0: aoqi@0: // sizing aoqi@0: int width() const { return _width; } aoqi@0: int position() const { return _position; } aoqi@0: int newlines() const { return _newlines; } aoqi@0: julong count() const { return _precount + _position; } aoqi@0: void set_count(julong count) { _precount = count - _position; } aoqi@0: void set_position(int pos) { _position = pos; } aoqi@0: aoqi@0: // printing aoqi@0: void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void print_raw(const char* str) { write(str, strlen(str)); } aoqi@0: void print_raw(const char* str, int len) { write(str, len); } aoqi@0: void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } aoqi@0: void print_raw_cr(const char* str, int len){ write(str, len); cr(); } aoqi@0: void print_data(void* data, size_t len, bool with_ascii); aoqi@0: void put(char ch); aoqi@0: void sp(int count = 1); aoqi@0: void cr(); aoqi@0: void bol() { if (_position > 0) cr(); } aoqi@0: aoqi@0: // Time stamp aoqi@0: TimeStamp& time_stamp() { return _stamp; } aoqi@0: void stamp(); aoqi@0: void stamp(bool guard, const char* prefix, const char* suffix); aoqi@0: void stamp(bool guard) { aoqi@0: stamp(guard, "", ": "); aoqi@0: } aoqi@0: // Date stamp aoqi@0: void date_stamp(bool guard, const char* prefix, const char* suffix); aoqi@0: // A simplified call that includes a suffix of ": " aoqi@0: void date_stamp(bool guard) { aoqi@0: date_stamp(guard, "", ": "); aoqi@0: } brutisso@6904: void gclog_stamp(const GCId& gc_id); aoqi@0: aoqi@0: // portable printing of 64 bit integers aoqi@0: void print_jlong(jlong value); aoqi@0: void print_julong(julong value); aoqi@0: aoqi@0: // flushing aoqi@0: virtual void flush() {} aoqi@0: virtual void write(const char* str, size_t len) = 0; aoqi@0: virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation aoqi@0: virtual ~outputStream() {} // close properly on deletion aoqi@0: aoqi@0: void dec_cr() { dec(); cr(); } aoqi@0: void inc_cr() { inc(); cr(); } aoqi@0: }; aoqi@0: aoqi@0: // standard output aoqi@0: // ANSI C++ name collision aoqi@0: extern outputStream* tty; // tty output aoqi@0: extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:, or tty aoqi@0: aoqi@0: class streamIndentor : public StackObj { aoqi@0: private: aoqi@0: outputStream* _str; aoqi@0: int _amount; aoqi@0: aoqi@0: public: aoqi@0: streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) { aoqi@0: _str->inc(_amount); aoqi@0: } aoqi@0: ~streamIndentor() { _str->dec(_amount); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // advisory locking for the shared tty stream: aoqi@0: class ttyLocker: StackObj { aoqi@0: friend class ttyUnlocker; aoqi@0: private: aoqi@0: intx _holder; aoqi@0: aoqi@0: public: aoqi@0: static intx hold_tty(); // returns a "holder" token aoqi@0: static void release_tty(intx holder); // must witness same token aoqi@0: static bool release_tty_if_locked(); // returns true if lock was released aoqi@0: static void break_tty_lock_for_safepoint(intx holder); aoqi@0: aoqi@0: ttyLocker() { _holder = hold_tty(); } aoqi@0: ~ttyLocker() { release_tty(_holder); } aoqi@0: }; aoqi@0: aoqi@0: // Release the tty lock if it's held and reacquire it if it was aoqi@0: // locked. Used to avoid lock ordering problems. aoqi@0: class ttyUnlocker: StackObj { aoqi@0: private: aoqi@0: bool _was_locked; aoqi@0: public: aoqi@0: ttyUnlocker() { aoqi@0: _was_locked = ttyLocker::release_tty_if_locked(); aoqi@0: } aoqi@0: ~ttyUnlocker() { aoqi@0: if (_was_locked) { aoqi@0: ttyLocker::hold_tty(); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // for writing to strings; buffer will expand automatically aoqi@0: class stringStream : public outputStream { aoqi@0: protected: aoqi@0: char* buffer; aoqi@0: size_t buffer_pos; aoqi@0: size_t buffer_length; aoqi@0: bool buffer_fixed; aoqi@0: DEBUG_ONLY(ResourceMark* rm;) aoqi@0: public: aoqi@0: stringStream(size_t initial_bufsize = 256); aoqi@0: stringStream(char* fixed_buffer, size_t fixed_buffer_size); aoqi@0: ~stringStream(); aoqi@0: virtual void write(const char* c, size_t len); aoqi@0: size_t size() { return buffer_pos; } aoqi@0: const char* base() { return buffer; } aoqi@0: void reset() { buffer_pos = 0; _precount = 0; _position = 0; } aoqi@0: char* as_string(); aoqi@0: }; aoqi@0: aoqi@0: class fileStream : public outputStream { aoqi@0: protected: aoqi@0: FILE* _file; aoqi@0: bool _need_close; aoqi@0: public: aoqi@0: fileStream() { _file = NULL; _need_close = false; } aoqi@0: fileStream(const char* file_name); aoqi@0: fileStream(const char* file_name, const char* opentype); aoqi@0: fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; } aoqi@0: ~fileStream(); aoqi@0: bool is_open() const { return _file != NULL; } aoqi@0: void set_need_close(bool b) { _need_close = b;} aoqi@0: virtual void write(const char* c, size_t len); aoqi@0: size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); } aoqi@0: char* readln(char *data, int count); aoqi@0: int eof() { return feof(_file); } aoqi@0: long fileSize(); aoqi@0: void rewind() { ::rewind(_file); } aoqi@0: void flush(); aoqi@0: }; aoqi@0: iklam@7089: CDS_ONLY(extern fileStream* classlist_file;) iklam@7089: aoqi@0: // unlike fileStream, fdStream does unbuffered I/O by calling aoqi@0: // open() and write() directly. It is async-safe, but output aoqi@0: // from multiple thread may be mixed together. Used by fatal aoqi@0: // error handler. aoqi@0: class fdStream : public outputStream { aoqi@0: protected: aoqi@0: int _fd; aoqi@0: bool _need_close; aoqi@0: public: aoqi@0: fdStream(const char* file_name); aoqi@0: fdStream(int fd = -1) { _fd = fd; _need_close = false; } aoqi@0: ~fdStream(); aoqi@0: bool is_open() const { return _fd != -1; } aoqi@0: void set_fd(int fd) { _fd = fd; _need_close = false; } aoqi@0: int fd() const { return _fd; } aoqi@0: virtual void write(const char* c, size_t len); aoqi@0: void flush() {}; aoqi@0: }; aoqi@0: aoqi@0: class gcLogFileStream : public fileStream { aoqi@0: protected: aoqi@0: const char* _file_name; aoqi@0: jlong _bytes_written; aoqi@0: uintx _cur_file_num; // current logfile rotation number, from 0 to NumberOfGCLogFiles-1 aoqi@0: public: aoqi@0: gcLogFileStream(const char* file_name); aoqi@0: ~gcLogFileStream(); aoqi@0: virtual void write(const char* c, size_t len); aoqi@0: virtual void rotate_log(bool force, outputStream* out = NULL); aoqi@0: void dump_loggc_header(); aoqi@0: aoqi@0: /* If "force" sets true, force log file rotation from outside JVM */ aoqi@0: bool should_rotate(bool force) { aoqi@0: return force || aoqi@0: ((GCLogFileSize != 0) && ((uintx)_bytes_written >= GCLogFileSize)); aoqi@0: } aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // unit test for checking -Xloggc: parsing result aoqi@0: void test_loggc_filename(); aoqi@0: #endif aoqi@0: aoqi@0: void ostream_init(); aoqi@0: void ostream_init_log(); aoqi@0: void ostream_exit(); aoqi@0: void ostream_abort(); aoqi@0: aoqi@0: // staticBufferStream uses a user-supplied buffer for all formatting. aoqi@0: // Used for safe formatting during fatal error handling. Not MT safe. aoqi@0: // Do not share the stream between multiple threads. aoqi@0: class staticBufferStream : public outputStream { aoqi@0: private: aoqi@0: char* _buffer; aoqi@0: size_t _buflen; aoqi@0: outputStream* _outer_stream; aoqi@0: public: aoqi@0: staticBufferStream(char* buffer, size_t buflen, aoqi@0: outputStream *outer_stream); aoqi@0: ~staticBufferStream() {}; aoqi@0: virtual void write(const char* c, size_t len); aoqi@0: void flush(); aoqi@0: void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); aoqi@0: void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); aoqi@0: void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); aoqi@0: }; aoqi@0: aoqi@0: // In the non-fixed buffer case an underlying buffer will be created and aoqi@0: // managed in C heap. Not MT-safe. aoqi@0: class bufferedStream : public outputStream { aoqi@0: protected: aoqi@0: char* buffer; aoqi@0: size_t buffer_pos; aoqi@0: size_t buffer_max; aoqi@0: size_t buffer_length; aoqi@0: bool buffer_fixed; aoqi@0: public: aoqi@0: bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); aoqi@0: bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); aoqi@0: ~bufferedStream(); aoqi@0: virtual void write(const char* c, size_t len); aoqi@0: size_t size() { return buffer_pos; } aoqi@0: const char* base() { return buffer; } aoqi@0: void reset() { buffer_pos = 0; _precount = 0; _position = 0; } aoqi@0: char* as_string(); aoqi@0: }; aoqi@0: aoqi@0: #define O_BUFLEN 2000 // max size of output of individual print() methods aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: class networkStream : public bufferedStream { aoqi@0: aoqi@0: private: aoqi@0: int _socket; aoqi@0: aoqi@0: public: aoqi@0: networkStream(); aoqi@0: ~networkStream(); aoqi@0: aoqi@0: bool connect(const char *host, short port); aoqi@0: bool is_open() const { return _socket != -1; } aoqi@0: int read(char *buf, size_t len); aoqi@0: void close(); aoqi@0: virtual void flush(); aoqi@0: }; aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_OSTREAM_HPP