1 /*
2  * Copyright (c) 2002, 2019, 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 #include "precompiled.hpp"
26 #include "code/nmethod.hpp"
27 #include "memory/allocation.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/methodData.hpp"
31 #include "oops/method.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/deoptimization.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/vmThread.hpp"
36 #include "utilities/vmError.hpp"
37 #include "utilities/xmlstream.hpp"
38 
39 // Do not assert this condition if there's already another error reported.
40 #define assert_if_no_error(cond, msg) \
41   vmassert((cond) || VMError::is_error_reported(), msg)
42 
initialize(outputStream * out)43 void xmlStream::initialize(outputStream* out) {
44   _out = out;
45   _last_flush = 0;
46   _markup_state = BODY;
47   _text_init._outer_xmlStream = this;
48   _text = &_text_init;
49 
50 #ifdef ASSERT
51   _element_depth = 0;
52   int   init_len = 100;
53   char* init_buf = NEW_C_HEAP_ARRAY(char, init_len, mtInternal);
54   _element_close_stack_low  = init_buf;
55   _element_close_stack_high = init_buf + init_len;
56   _element_close_stack_ptr  = init_buf + init_len - 1;
57   _element_close_stack_ptr[0] = '\0';
58 #endif
59 
60   // Make sure each log uses the same base for time stamps.
61   if (is_open()) {
62     _out->time_stamp().update_to(1);
63   }
64 }
65 
66 #ifdef ASSERT
~xmlStream()67 xmlStream::~xmlStream() {
68   FREE_C_HEAP_ARRAY(char, _element_close_stack_low);
69 }
70 #endif
71 
72 // Pass the given chars directly to _out.
write(const char * s,size_t len)73 void xmlStream::write(const char* s, size_t len) {
74   if (!is_open())  return;
75 
76   out()->write(s, len);
77   update_position(s, len);
78 }
79 
80 
81 // Pass the given chars directly to _out, except that
82 // we watch for special "<&>" chars.
83 // This is suitable for either attribute text or for body text.
84 // We don't fool with "<![CDATA[" quotes, just single-character entities.
85 // This makes it easier for dumb tools to parse the output.
write_text(const char * s,size_t len)86 void xmlStream::write_text(const char* s, size_t len) {
87   if (!is_open())  return;
88 
89   size_t written = 0;
90   // All normally printed material goes inside XML quotes.
91   // This leaves the output free to include markup also.
92   // Scan the string looking for inadvertant "<&>" chars
93   for (size_t i = 0; i < len; i++) {
94     char ch = s[i];
95     // Escape special chars.
96     const char* esc = NULL;
97     switch (ch) {
98       // These are important only in attrs, but we do them always:
99     case '\'': esc = "&apos;"; break;
100     case '"':  esc = "&quot;"; break;
101     case '<':  esc = "&lt;";   break;
102     case '&':  esc = "&amp;";  break;
103       // This is a freebie.
104     case '>':  esc = "&gt;";   break;
105     }
106     if (esc != NULL) {
107       if (written < i) {
108         out()->write(&s[written], i - written);
109         written = i;
110       }
111       out()->print_raw(esc);
112       written++;
113     }
114   }
115 
116   // Print the clean remainder.  Usually, it is all of s.
117   if (written < len) {
118     out()->write(&s[written], len - written);
119   }
120 }
121 
122 // ------------------------------------------------------------------
123 // Outputs XML text, with special characters quoted.
text(const char * format,...)124 void xmlStream::text(const char* format, ...) {
125   va_list ap;
126   va_start(ap, format);
127   va_text(format, ap);
128   va_end(ap);
129 }
130 
131 #define BUFLEN 2*K   /* max size of output of individual print methods */
132 
133 // ------------------------------------------------------------------
va_tag(bool push,const char * format,va_list ap)134 void xmlStream::va_tag(bool push, const char* format, va_list ap) {
135   assert_if_no_error(!inside_attrs(), "cannot print tag inside attrs");
136   char buffer[BUFLEN];
137   size_t len;
138   const char* kind = do_vsnprintf(buffer, BUFLEN, format, ap, false, len);
139   see_tag(kind, push);
140   print_raw("<");
141   write(kind, len);
142   _markup_state = (push ? HEAD : ELEM);
143 }
144 
145 #ifdef ASSERT
146 /// Debugging goo to make sure element tags nest properly.
147 
148 // ------------------------------------------------------------------
see_tag(const char * tag,bool push)149 void xmlStream::see_tag(const char* tag, bool push) {
150   assert_if_no_error(!inside_attrs(), "cannot start new element inside attrs");
151   if (!push)  return;
152 
153   // tag goes up until either null or space:
154   const char* tag_end = strchr(tag, ' ');
155   size_t tag_len = (tag_end == NULL) ? strlen(tag) : tag_end - tag;
156   assert(tag_len > 0, "tag must not be empty");
157   // push the tag onto the stack, pulling down the pointer
158   char* old_ptr  = _element_close_stack_ptr;
159   char* old_low  = _element_close_stack_low;
160   char* push_ptr = old_ptr - (tag_len+1);
161   if (push_ptr < old_low) {
162     int old_len = _element_close_stack_high - old_ptr;
163     int new_len = old_len * 2;
164     if (new_len < 100)  new_len = 100;
165     char* new_low  = NEW_C_HEAP_ARRAY(char, new_len, mtInternal);
166     char* new_high = new_low + new_len;
167     char* new_ptr  = new_high - old_len;
168     memcpy(new_ptr, old_ptr, old_len);
169     _element_close_stack_high = new_high;
170     _element_close_stack_low  = new_low;
171     _element_close_stack_ptr  = new_ptr;
172     FREE_C_HEAP_ARRAY(char, old_low);
173     push_ptr = new_ptr - (tag_len+1);
174   }
175   assert(push_ptr >= _element_close_stack_low, "in range");
176   memcpy(push_ptr, tag, tag_len);
177   push_ptr[tag_len] = 0;
178   _element_close_stack_ptr = push_ptr;
179   _element_depth += 1;
180 }
181 
182 // ------------------------------------------------------------------
pop_tag(const char * tag)183 void xmlStream::pop_tag(const char* tag) {
184   assert_if_no_error(!inside_attrs(), "cannot close element inside attrs");
185   assert(_element_depth > 0, "must be in an element to close");
186   assert(*tag != 0, "tag must not be empty");
187   char* cur_tag = _element_close_stack_ptr;
188   bool  bad_tag = false;
189   while (*cur_tag != 0 && strcmp(cur_tag, tag) != 0) {
190     this->print_cr("</%s> <!-- missing closing tag -->", cur_tag);
191     _element_close_stack_ptr = (cur_tag += strlen(cur_tag) + 1);
192     _element_depth -= 1;
193     bad_tag = true;
194   }
195   if (*cur_tag == 0) {
196     bad_tag = true;
197   } else {
198     // Pop the stack, by skipping over the tag and its null.
199     _element_close_stack_ptr = cur_tag + strlen(cur_tag) + 1;
200     _element_depth -= 1;
201   }
202   if (bad_tag && !VMThread::should_terminate() && !VM_Exit::vm_exited() &&
203       !VMError::is_error_reported())
204   {
205     assert(false, "bad tag in log");
206   }
207 }
208 #endif
209 
210 
211 // ------------------------------------------------------------------
212 // First word in formatted string is element kind, and any subsequent
213 // words must be XML attributes.  Outputs "<kind .../>".
elem(const char * format,...)214 void xmlStream::elem(const char* format, ...) {
215   va_list ap;
216   va_start(ap, format);
217   va_elem(format, ap);
218   va_end(ap);
219 }
220 
221 // ------------------------------------------------------------------
va_elem(const char * format,va_list ap)222 void xmlStream::va_elem(const char* format, va_list ap) {
223   va_begin_elem(format, ap);
224   end_elem();
225 }
226 
227 
228 // ------------------------------------------------------------------
229 // First word in formatted string is element kind, and any subsequent
230 // words must be XML attributes.  Outputs "<kind ...", not including "/>".
begin_elem(const char * format,...)231 void xmlStream::begin_elem(const char* format, ...) {
232   va_list ap;
233   va_start(ap, format);
234   va_tag(false, format, ap);
235   va_end(ap);
236 }
237 
238 // ------------------------------------------------------------------
va_begin_elem(const char * format,va_list ap)239 void xmlStream::va_begin_elem(const char* format, va_list ap) {
240   va_tag(false, format, ap);
241 }
242 
243 // ------------------------------------------------------------------
244 // Outputs "/>".
end_elem()245 void xmlStream::end_elem() {
246   assert(_markup_state == ELEM, "misplaced end_elem");
247   print_raw("/>\n");
248   _markup_state = BODY;
249 }
250 
251 // ------------------------------------------------------------------
252 // Outputs formatted text, followed by "/>".
end_elem(const char * format,...)253 void xmlStream::end_elem(const char* format, ...) {
254   va_list ap;
255   va_start(ap, format);
256   out()->vprint(format, ap);
257   va_end(ap);
258   end_elem();
259 }
260 
261 
262 // ------------------------------------------------------------------
263 // First word in formatted string is element kind, and any subsequent
264 // words must be XML attributes.  Outputs "<kind ...>".
head(const char * format,...)265 void xmlStream::head(const char* format, ...) {
266   va_list ap;
267   va_start(ap, format);
268   va_head(format, ap);
269   va_end(ap);
270 }
271 
272 // ------------------------------------------------------------------
va_head(const char * format,va_list ap)273 void xmlStream::va_head(const char* format, va_list ap) {
274   va_begin_head(format, ap);
275   end_head();
276 }
277 
278 // ------------------------------------------------------------------
279 // First word in formatted string is element kind, and any subsequent
280 // words must be XML attributes.  Outputs "<kind ...", not including ">".
begin_head(const char * format,...)281 void xmlStream::begin_head(const char* format, ...) {
282   va_list ap;
283   va_start(ap, format);
284   va_tag(true, format, ap);
285   va_end(ap);
286 }
287 
288 // ------------------------------------------------------------------
va_begin_head(const char * format,va_list ap)289 void xmlStream::va_begin_head(const char* format, va_list ap) {
290   va_tag(true, format, ap);
291 }
292 
293 // ------------------------------------------------------------------
294 // Outputs ">".
end_head()295 void xmlStream::end_head() {
296   assert(_markup_state == HEAD, "misplaced end_head");
297   print_raw(">\n");
298   _markup_state = BODY;
299 }
300 
301 
302 // ------------------------------------------------------------------
303 // Outputs formatted text, followed by ">".
end_head(const char * format,...)304 void xmlStream::end_head(const char* format, ...) {
305   va_list ap;
306   va_start(ap, format);
307   out()->vprint(format, ap);
308   va_end(ap);
309   end_head();
310 }
311 
312 
313 // ------------------------------------------------------------------
314 // Outputs "</kind>".
tail(const char * kind)315 void xmlStream::tail(const char* kind) {
316   pop_tag(kind);
317   print_raw("</");
318   print_raw(kind);
319   print_raw(">\n");
320 }
321 
322 // ------------------------------------------------------------------
323 // Outputs "<kind_done ... stamp='D.DD'/> </kind>".
done(const char * format,...)324 void xmlStream::done(const char* format, ...) {
325   va_list ap;
326   va_start(ap, format);
327   va_done(format, ap);
328   va_end(ap);
329 }
330 
331 // ------------------------------------------------------------------
332 // Outputs "<kind_done stamp='D.DD'/> </kind>".
333 // Because done_raw() doesn't need to format strings, it's simpler than
334 // done(), and can be called safely by fatal error handler.
done_raw(const char * kind)335 void xmlStream::done_raw(const char* kind) {
336   print_raw("<");
337   print_raw(kind);
338   print_raw("_done stamp='");
339   out()->stamp();
340   print_raw_cr("'/>");
341   print_raw("</");
342   print_raw(kind);
343   print_raw_cr(">");
344 }
345 
346 // If you remove the PRAGMA, this fails to compile with clang-503.0.40.
347 PRAGMA_DIAG_PUSH
348 PRAGMA_FORMAT_NONLITERAL_IGNORED
349 // ------------------------------------------------------------------
va_done(const char * format,va_list ap)350 void xmlStream::va_done(const char* format, va_list ap) {
351   char buffer[200];
352   size_t format_len = strlen(format);
353   guarantee(format_len + 10 < sizeof(buffer), "bigger format buffer");
354   const char* kind = format;
355   const char* kind_end = strchr(kind, ' ');
356   size_t kind_len;
357   if (kind_end != NULL) {
358     kind_len = kind_end - kind;
359     int n = os::snprintf(buffer, sizeof(buffer), "%.*s_done", (int)kind_len, kind);
360     assert((size_t)n < sizeof(buffer), "Unexpected number of characters in string");
361   } else {
362     kind_len = format_len;
363     int n = os::snprintf(buffer, sizeof(buffer), "%s_done%s", kind, kind + kind_len);
364     assert((size_t)n < sizeof(buffer), "Unexpected number of characters in string");
365   }
366   // Output the trailing event with the timestamp.
367   va_begin_elem(buffer, ap);
368   stamp();
369   end_elem();
370   // Output the tail-tag of the enclosing element.
371   buffer[kind_len] = 0;
372   tail(buffer);
373 }
374 PRAGMA_DIAG_POP
375 
376 // Output a timestamp attribute.
stamp()377 void xmlStream::stamp() {
378   assert_if_no_error(inside_attrs(), "stamp must be an attribute");
379   print_raw(" stamp='");
380   out()->stamp();
381   print_raw("'");
382 }
383 
384 
385 // ------------------------------------------------------------------
386 // Output a method attribute, in the form " method='pkg/cls name sig'".
387 // This is used only when there is no ciMethod available.
method(const methodHandle & method)388 void xmlStream::method(const methodHandle& method) {
389   assert_if_no_error(inside_attrs(), "printing attributes");
390   if (method.is_null())  return;
391   print_raw(" method='");
392   method_text(method);
393   print("' bytes='%d'", method->code_size());
394   print(" count='%d'", method->invocation_count());
395   int bec = method->backedge_count();
396   if (bec != 0)  print(" backedge_count='%d'", bec);
397   print(" iicount='%d'", method->interpreter_invocation_count());
398   int throwouts = method->interpreter_throwout_count();
399   if (throwouts != 0)  print(" throwouts='%d'", throwouts);
400   MethodData* mdo = method->method_data();
401   if (mdo != NULL) {
402     uint cnt;
403     cnt = mdo->decompile_count();
404     if (cnt != 0)  print(" decompiles='%d'", cnt);
405     for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) {
406       cnt = mdo->trap_count(reason);
407       if (cnt != 0)  print(" %s_traps='%d'", Deoptimization::trap_reason_name(reason), cnt);
408     }
409     cnt = mdo->overflow_trap_count();
410     if (cnt != 0)  print(" overflow_traps='%d'", cnt);
411     cnt = mdo->overflow_recompile_count();
412     if (cnt != 0)  print(" overflow_recompiles='%d'", cnt);
413   }
414 }
415 
method_text(const methodHandle & method)416 void xmlStream::method_text(const methodHandle& method) {
417   ResourceMark rm;
418   assert_if_no_error(inside_attrs(), "printing attributes");
419   if (method.is_null())  return;
420   text()->print("%s", method->method_holder()->external_name());
421   print_raw(" ");  // " " is easier for tools to parse than "::"
422   method->name()->print_symbol_on(text());
423   print_raw(" ");  // separator
424   method->signature()->print_symbol_on(text());
425 }
426 
427 
428 // ------------------------------------------------------------------
429 // Output a klass attribute, in the form " klass='pkg/cls'".
430 // This is used only when there is no ciKlass available.
klass(Klass * klass)431 void xmlStream::klass(Klass* klass) {
432   assert_if_no_error(inside_attrs(), "printing attributes");
433   if (klass == NULL) return;
434   print_raw(" klass='");
435   klass_text(klass);
436   print_raw("'");
437 }
438 
klass_text(Klass * klass)439 void xmlStream::klass_text(Klass* klass) {
440   assert_if_no_error(inside_attrs(), "printing attributes");
441   if (klass == NULL) return;
442   //klass->print_short_name(log->out());
443   klass->name()->print_symbol_on(out());
444 }
445 
name(const Symbol * name)446 void xmlStream::name(const Symbol* name) {
447   assert_if_no_error(inside_attrs(), "printing attributes");
448   if (name == NULL)  return;
449   print_raw(" name='");
450   name_text(name);
451   print_raw("'");
452 }
453 
name_text(const Symbol * name)454 void xmlStream::name_text(const Symbol* name) {
455   assert_if_no_error(inside_attrs(), "printing attributes");
456   if (name == NULL)  return;
457   //name->print_short_name(text());
458   name->print_symbol_on(text());
459 }
460 
object(const char * attr,Handle x)461 void xmlStream::object(const char* attr, Handle x) {
462   assert_if_no_error(inside_attrs(), "printing attributes");
463   if (x == NULL)  return;
464   print_raw(" ");
465   print_raw(attr);
466   print_raw("='");
467   object_text(x);
468   print_raw("'");
469 }
470 
object_text(Handle x)471 void xmlStream::object_text(Handle x) {
472   assert_if_no_error(inside_attrs(), "printing attributes");
473   if (x == NULL)  return;
474   x->print_value_on(text());
475 }
476 
477 
object(const char * attr,Metadata * x)478 void xmlStream::object(const char* attr, Metadata* x) {
479   assert_if_no_error(inside_attrs(), "printing attributes");
480   if (x == NULL)  return;
481   print_raw(" ");
482   print_raw(attr);
483   print_raw("='");
484   object_text(x);
485   print_raw("'");
486 }
487 
object_text(Metadata * x)488 void xmlStream::object_text(Metadata* x) {
489   assert_if_no_error(inside_attrs(), "printing attributes");
490   if (x == NULL)  return;
491   //x->print_value_on(text());
492   if (x->is_method())
493     method_text((Method*)x);
494   else if (x->is_klass())
495     klass_text((Klass*)x);
496   else
497     ShouldNotReachHere(); // Add impl if this is reached.
498 }
499 
500 
flush()501 void xmlStream::flush() {
502   out()->flush();
503   _last_flush = count();
504 }
505 
flush()506 void xmlTextStream::flush() {
507   if (_outer_xmlStream == NULL)  return;
508   _outer_xmlStream->flush();
509 }
510 
write(const char * str,size_t len)511 void xmlTextStream::write(const char* str, size_t len) {
512   if (_outer_xmlStream == NULL)  return;
513   _outer_xmlStream->write_text(str, len);
514   update_position(str, len);
515 }
516