1 /* 2 * Copyright (c) 1997, 2015, 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 31 class GCId; 32 DEBUG_ONLY(class ResourceMark;) 33 34 // Output streams for printing 35 // 36 // Printing guidelines: 37 // Where possible, please use tty->print() and tty->print_cr(). 38 // For product mode VM warnings use warning() which internally uses tty. 39 // In places where tty is not initialized yet or too much overhead, 40 // we may use jio_printf: 41 // jio_fprintf(defaultStream::output_stream(), "Message"); 42 // This allows for redirection via -XX:+DisplayVMOutputToStdout and 43 // -XX:+DisplayVMOutputToStderr 44 class outputStream : public ResourceObj { 45 protected: 46 int _indentation; // current indentation 47 int _width; // width of the page 48 int _position; // position on the current line 49 int _newlines; // number of '\n' output so far 50 julong _precount; // number of chars output, less _position 51 TimeStamp _stamp; // for time stamps 52 53 void update_position(const char* s, size_t len); 54 static const char* do_vsnprintf(char* buffer, size_t buflen, 55 const char* format, va_list ap, 56 bool add_cr, 57 size_t& result_len) ATTRIBUTE_PRINTF(3, 0); 58 59 public: 60 // creation 61 outputStream(int width = 80); 62 outputStream(int width, bool has_time_stamps); 63 64 // indentation 65 outputStream& indent(); 66 void inc() { _indentation++; }; 67 void dec() { _indentation--; }; 68 void inc(int n) { _indentation += n; }; 69 void dec(int n) { _indentation -= n; }; 70 int indentation() const { return _indentation; } 71 void set_indentation(int i) { _indentation = i; } 72 void fill_to(int col); 73 void move_to(int col, int slop = 6, int min_space = 2); 74 75 // sizing 76 int width() const { return _width; } 77 int position() const { return _position; } 78 int newlines() const { return _newlines; } 79 julong count() const { return _precount + _position; } 80 void set_count(julong count) { _precount = count - _position; } 81 void set_position(int pos) { _position = pos; } 82 83 // printing 84 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 85 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 86 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 87 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 88 void print_raw(const char* str) { write(str, strlen(str)); } 89 void print_raw(const char* str, int len) { write(str, len); } 90 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } 91 void print_raw_cr(const char* str, int len){ write(str, len); cr(); } 92 void print_data(void* data, size_t len, bool with_ascii); 93 void put(char ch); 94 void sp(int count = 1); 95 void cr(); 96 void bol() { if (_position > 0) cr(); } 97 98 // Time stamp 99 TimeStamp& time_stamp() { return _stamp; } 100 void stamp(); 101 void stamp(bool guard, const char* prefix, const char* suffix); 102 void stamp(bool guard) { 103 stamp(guard, "", ": "); 104 } 105 // Date stamp 106 void date_stamp(bool guard, const char* prefix, const char* suffix); 107 // A simplified call that includes a suffix of ": " 108 void date_stamp(bool guard) { 109 date_stamp(guard, "", ": "); 110 } 111 112 // portable printing of 64 bit integers 113 void print_jlong(jlong value); 114 void print_julong(julong value); 115 116 // flushing 117 virtual void flush() {} 118 virtual void write(const char* str, size_t len) = 0; 119 virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation 120 virtual ~outputStream() {} // close properly on deletion 121 122 void dec_cr() { dec(); cr(); } 123 void inc_cr() { inc(); cr(); } 124 }; 125 126 // standard output 127 // ANSI C++ name collision 128 extern outputStream* tty; // tty output 129 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty 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, ...); 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 class gcLogFileStream : public fileStream { 250 protected: 251 const char* _file_name; 252 jlong _bytes_written; 253 uintx _cur_file_num; // current logfile rotation number, from 0 to NumberOfGCLogFiles-1 254 public: 255 gcLogFileStream(const char* file_name); 256 ~gcLogFileStream(); 257 virtual void write(const char* c, size_t len); 258 virtual void rotate_log(bool force, outputStream* out = NULL); 259 void dump_loggc_header(); 260 261 /* If "force" sets true, force log file rotation from outside JVM */ 262 bool should_rotate(bool force) { 263 return force || 264 ((GCLogFileSize != 0) && (_bytes_written >= (jlong)GCLogFileSize)); 265 } 266 }; 267 268 #ifndef PRODUCT 269 // unit test for checking -Xloggc:<filename> parsing result 270 void test_loggc_filename(); 271 #endif 272 273 void ostream_init(); 274 void ostream_init_log(); 275 void ostream_exit(); 276 void ostream_abort(); 277 278 // staticBufferStream uses a user-supplied buffer for all formatting. 279 // Used for safe formatting during fatal error handling. Not MT safe. 280 // Do not share the stream between multiple threads. 281 class staticBufferStream : public outputStream { 282 private: 283 char* _buffer; 284 size_t _buflen; 285 outputStream* _outer_stream; 286 public: 287 staticBufferStream(char* buffer, size_t buflen, 288 outputStream *outer_stream); 289 ~staticBufferStream() {}; 290 virtual void write(const char* c, size_t len); 291 void flush(); 292 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 293 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 294 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 295 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); 296 }; 297 298 // In the non-fixed buffer case an underlying buffer will be created and 299 // managed in C heap. Not MT-safe. 300 class bufferedStream : public outputStream { 301 protected: 302 char* buffer; 303 size_t buffer_pos; 304 size_t buffer_max; 305 size_t buffer_length; 306 bool buffer_fixed; 307 public: 308 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); 309 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); 310 ~bufferedStream(); 311 virtual void write(const char* c, size_t len); 312 size_t size() { return buffer_pos; } 313 const char* base() { return buffer; } 314 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } 315 char* as_string(); 316 }; 317 318 #define O_BUFLEN 2000 // max size of output of individual print() methods 319 320 #ifndef PRODUCT 321 322 class networkStream : public bufferedStream { 323 324 private: 325 int _socket; 326 327 public: 328 networkStream(); 329 ~networkStream(); 330 331 bool connect(const char *host, short port); 332 bool is_open() const { return _socket != -1; } 333 int read(char *buf, size_t len); 334 void close(); 335 virtual void flush(); 336 }; 337 338 #endif 339 340 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP