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