1 /* 2 * Copyright (c) 1997, 2016, 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_UTILITIES_OSTREAM_HPP 26 #define SHARE_VM_UTILITIES_OSTREAM_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/timer.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 32 class GCId; 33 DEBUG_ONLY(class ResourceMark;) 34 35 // Output streams for printing 36 // 37 // Printing guidelines: 38 // Where possible, please use tty->print() and tty->print_cr(). 39 // For product mode VM warnings use warning() which internally uses tty. 40 // In places where tty is not initialized yet or too much overhead, 41 // we may use jio_printf: 42 // jio_fprintf(defaultStream::output_stream(), "Message"); 43 // This allows for redirection via -XX:+DisplayVMOutputToStdout and 44 // -XX:+DisplayVMOutputToStderr 45 class outputStream : public ResourceObj { 46 protected: 47 int _indentation; // current indentation 48 int _width; // width of the page 49 int _position; // position on the current line 50 int _newlines; // number of '\n' output so far 51 julong _precount; // number of chars output, less _position 52 TimeStamp _stamp; // for time stamps 53 54 void update_position(const char* s, size_t len); 55 static const char* do_vsnprintf(char* buffer, size_t buflen, 56 const char* format, va_list ap, 57 bool add_cr, 58 size_t& result_len) ATTRIBUTE_PRINTF(3, 0); 59 60 public: 61 // creation 62 outputStream(int width = 80); 63 outputStream(int width, bool has_time_stamps); 64 65 // indentation 66 outputStream& indent(); 67 void inc() { _indentation++; }; 68 void dec() { _indentation--; }; 69 void inc(int n) { _indentation += n; }; 70 void dec(int n) { _indentation -= n; }; 71 int indentation() const { return _indentation; } 72 void set_indentation(int i) { _indentation = i; } 73 void fill_to(int col); 74 void move_to(int col, int slop = 6, int min_space = 2); 75 76 // sizing 77 int width() const { return _width; } 78 int position() const { return _position; } 79 int newlines() const { return _newlines; } 80 julong count() const { return _precount + _position; } 81 void set_count(julong count) { _precount = count - _position; } 82 void set_position(int pos) { _position = pos; } 83 84 // printing 85 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 86 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 87 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 88 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 89 void print_raw(const char* str) { write(str, strlen(str)); } 90 void print_raw(const char* str, int len) { write(str, len); } 91 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } 92 void print_raw_cr(const char* str, int len){ write(str, len); cr(); } 93 void print_data(void* data, size_t len, bool with_ascii); 94 void put(char ch); 95 void sp(int count = 1); 96 void cr(); 97 void bol() { if (_position > 0) cr(); } 98 99 // Time stamp 100 TimeStamp& time_stamp() { return _stamp; } 101 void stamp(); 102 void stamp(bool guard, const char* prefix, const char* suffix); 103 void stamp(bool guard) { 104 stamp(guard, "", ": "); 105 } 106 // Date stamp 107 void date_stamp(bool guard, const char* prefix, const char* suffix); 108 // A simplified call that includes a suffix of ": " 109 void date_stamp(bool guard) { 110 date_stamp(guard, "", ": "); 111 } 112 113 // portable printing of 64 bit integers 114 void print_jlong(jlong value); 115 void print_julong(julong value); 116 117 // flushing 118 virtual void flush() {} 119 virtual void write(const char* str, size_t len) = 0; 120 virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation 121 virtual ~outputStream() {} // close properly on deletion 122 123 void dec_cr() { dec(); cr(); } 124 void inc_cr() { inc(); cr(); } 125 }; 126 127 // standard output 128 // ANSI C++ name collision 129 extern outputStream* tty; // tty output 130 131 class streamIndentor : public StackObj { 132 private: 133 outputStream* _str; 134 int _amount; 135 136 public: 137 streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) { 138 _str->inc(_amount); 139 } 140 ~streamIndentor() { _str->dec(_amount); } 141 }; 142 143 144 // advisory locking for the shared tty stream: 145 class ttyLocker: StackObj { 146 friend class ttyUnlocker; 147 private: 148 intx _holder; 149 150 public: 151 static intx hold_tty(); // returns a "holder" token 152 static void release_tty(intx holder); // must witness same token 153 static bool release_tty_if_locked(); // returns true if lock was released 154 static void break_tty_lock_for_safepoint(intx holder); 155 156 ttyLocker() { _holder = hold_tty(); } 157 ~ttyLocker() { release_tty(_holder); } 158 }; 159 160 // Release the tty lock if it's held and reacquire it if it was 161 // locked. Used to avoid lock ordering problems. 162 class ttyUnlocker: StackObj { 163 private: 164 bool _was_locked; 165 public: 166 ttyUnlocker() { 167 _was_locked = ttyLocker::release_tty_if_locked(); 168 } 169 ~ttyUnlocker() { 170 if (_was_locked) { 171 ttyLocker::hold_tty(); 172 } 173 } 174 }; 175 176 // for writing to strings; buffer will expand automatically 177 class stringStream : public outputStream { 178 protected: 179 char* buffer; 180 size_t buffer_pos; 181 size_t buffer_length; 182 bool buffer_fixed; 183 DEBUG_ONLY(ResourceMark* rm;) 184 public: 185 stringStream(size_t initial_bufsize = 256); 186 stringStream(char* fixed_buffer, size_t fixed_buffer_size); 187 ~stringStream(); 188 virtual void write(const char* c, size_t len); 189 size_t size() { return buffer_pos; } 190 const char* base() { return buffer; } 191 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 192 char* as_string(); 193 }; 194 195 class fileStream : public outputStream { 196 protected: 197 FILE* _file; 198 bool _need_close; 199 public: 200 fileStream() { _file = NULL; _need_close = false; } 201 fileStream(const char* file_name); 202 fileStream(const char* file_name, const char* opentype); 203 fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; } 204 ~fileStream(); 205 bool is_open() const { return _file != NULL; } 206 void set_need_close(bool b) { _need_close = b;} 207 virtual void write(const char* c, size_t len); 208 size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); } 209 char* readln(char *data, int count); 210 int eof() { return feof(_file); } 211 long fileSize(); 212 void rewind() { ::rewind(_file); } 213 void flush(); 214 }; 215 216 CDS_ONLY(extern fileStream* classlist_file;) 217 218 // unlike fileStream, fdStream does unbuffered I/O by calling 219 // open() and write() directly. It is async-safe, but output 220 // from multiple thread may be mixed together. Used by fatal 221 // error handler. 222 class fdStream : public outputStream { 223 protected: 224 int _fd; 225 bool _need_close; 226 public: 227 fdStream(const char* file_name); 228 fdStream(int fd = -1) { _fd = fd; _need_close = false; } 229 ~fdStream(); 230 bool is_open() const { return _fd != -1; } 231 void set_fd(int fd) { _fd = fd; _need_close = false; } 232 int fd() const { return _fd; } 233 virtual void write(const char* c, size_t len); 234 void flush() {}; 235 }; 236 237 class logStream : public outputStream { 238 private: 239 stringStream _current_line; 240 void (*_log_func)(const char* fmt, ...) ATTRIBUTE_PRINTF(1, 2); 241 public: 242 void write(const char* s, size_t len); 243 logStream(void (*log_func)(const char* fmt, ...)) : _log_func(log_func) {} 244 ~logStream() { 245 guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?"); 246 } 247 }; 248 249 void ostream_init(); 250 void ostream_init_log(); 251 void ostream_exit(); 252 void ostream_abort(); 253 254 // staticBufferStream uses a user-supplied buffer for all formatting. 255 // Used for safe formatting during fatal error handling. Not MT safe. 256 // Do not share the stream between multiple threads. 257 class staticBufferStream : public outputStream { 258 private: 259 char* _buffer; 260 size_t _buflen; 261 outputStream* _outer_stream; 262 public: 263 staticBufferStream(char* buffer, size_t buflen, 264 outputStream *outer_stream); 265 ~staticBufferStream() {}; 266 virtual void write(const char* c, size_t len); 267 void flush(); 268 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 269 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 270 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 271 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 272 }; 273 274 // In the non-fixed buffer case an underlying buffer will be created and 275 // managed in C heap. Not MT-safe. 276 class bufferedStream : public outputStream { 277 protected: 278 char* buffer; 279 size_t buffer_pos; 280 size_t buffer_max; 281 size_t buffer_length; 282 bool buffer_fixed; 283 public: 284 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); 285 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); 286 ~bufferedStream(); 287 virtual void write(const char* c, size_t len); 288 size_t size() { return buffer_pos; } 289 const char* base() { return buffer; } 290 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 291 char* as_string(); 292 }; 293 294 #define O_BUFLEN 2000 // max size of output of individual print() methods 295 296 #ifndef PRODUCT 297 298 class networkStream : public bufferedStream { 299 300 private: 301 int _socket; 302 303 public: 304 networkStream(); 305 ~networkStream(); 306 307 bool connect(const char *host, short port); 308 bool is_open() const { return _socket != -1; } 309 int read(char *buf, size_t len); 310 void close(); 311 virtual void flush(); 312 }; 313 314 #endif 315 316 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP