1 /*
   2  * Copyright (c) 2002, 2017, 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 
  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
  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.
  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.
  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.
 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 // ------------------------------------------------------------------
 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 // ------------------------------------------------------------------
 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 // ------------------------------------------------------------------
 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 .../>".
 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 // ------------------------------------------------------------------
 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 "/>".
 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 // ------------------------------------------------------------------
 239 void xmlStream::va_begin_elem(const char* format, va_list ap) {
 240   va_tag(false, format, ap);
 241 }
 242 
 243 // ------------------------------------------------------------------
 244 // Outputs "/>".
 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 "/>".
 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 ...>".
 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 // ------------------------------------------------------------------
 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 ">".
 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 // ------------------------------------------------------------------
 289 void xmlStream::va_begin_head(const char* format, va_list ap) {
 290   va_tag(true, format, ap);
 291 }
 292 
 293 // ------------------------------------------------------------------
 294 // Outputs ">".
 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 ">".
 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>".
 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>".
 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.
 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 // ------------------------------------------------------------------
 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 = (kind_end != NULL) ? (kind_end - kind) : format_len;
 357   strncpy(buffer, kind, kind_len);
 358   strcpy(buffer + kind_len, "_done");
 359   if (kind_end != NULL) {
 360     strncat(buffer, format + kind_len, sizeof(buffer) - (kind_len + 5 /* _done */) - 1);
 361   }
 362   // Output the trailing event with the timestamp.
 363   va_begin_elem(buffer, ap);
 364   stamp();
 365   end_elem();
 366   // Output the tail-tag of the enclosing element.
 367   buffer[kind_len] = 0;
 368   tail(buffer);
 369 }
 370 PRAGMA_DIAG_POP
 371 
 372 // Output a timestamp attribute.
 373 void xmlStream::stamp() {
 374   assert_if_no_error(inside_attrs(), "stamp must be an attribute");
 375   print_raw(" stamp='");
 376   out()->stamp();
 377   print_raw("'");
 378 }
 379 
 380 
 381 // ------------------------------------------------------------------
 382 // Output a method attribute, in the form " method='pkg/cls name sig'".
 383 // This is used only when there is no ciMethod available.
 384 void xmlStream::method(const methodHandle& method) {
 385   assert_if_no_error(inside_attrs(), "printing attributes");
 386   if (method.is_null())  return;
 387   print_raw(" method='");
 388   method_text(method);
 389   print("' bytes='%d'", method->code_size());
 390   print(" count='%d'", method->invocation_count());
 391   int bec = method->backedge_count();
 392   if (bec != 0)  print(" backedge_count='%d'", bec);
 393   print(" iicount='%d'", method->interpreter_invocation_count());
 394   int throwouts = method->interpreter_throwout_count();
 395   if (throwouts != 0)  print(" throwouts='%d'", throwouts);
 396   MethodData* mdo = method->method_data();
 397   if (mdo != NULL) {
 398     uint cnt;
 399     cnt = mdo->decompile_count();
 400     if (cnt != 0)  print(" decompiles='%d'", cnt);
 401     for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) {
 402       cnt = mdo->trap_count(reason);
 403       if (cnt != 0)  print(" %s_traps='%d'", Deoptimization::trap_reason_name(reason), cnt);
 404     }
 405     cnt = mdo->overflow_trap_count();
 406     if (cnt != 0)  print(" overflow_traps='%d'", cnt);
 407     cnt = mdo->overflow_recompile_count();
 408     if (cnt != 0)  print(" overflow_recompiles='%d'", cnt);
 409   }
 410 }
 411 
 412 void xmlStream::method_text(const methodHandle& method) {
 413   ResourceMark rm;
 414   assert_if_no_error(inside_attrs(), "printing attributes");
 415   if (method.is_null())  return;
 416   text()->print("%s", method->method_holder()->external_name());
 417   print_raw(" ");  // " " is easier for tools to parse than "::"
 418   method->name()->print_symbol_on(text());
 419   print_raw(" ");  // separator
 420   method->signature()->print_symbol_on(text());
 421 }
 422 
 423 
 424 // ------------------------------------------------------------------
 425 // Output a klass attribute, in the form " klass='pkg/cls'".
 426 // This is used only when there is no ciKlass available.
 427 void xmlStream::klass(Klass* klass) {
 428   assert_if_no_error(inside_attrs(), "printing attributes");
 429   if (klass == NULL) return;
 430   print_raw(" klass='");
 431   klass_text(klass);
 432   print_raw("'");
 433 }
 434 
 435 void xmlStream::klass_text(Klass* klass) {
 436   assert_if_no_error(inside_attrs(), "printing attributes");
 437   if (klass == NULL) return;
 438   //klass->print_short_name(log->out());
 439   klass->name()->print_symbol_on(out());
 440 }
 441 
 442 void xmlStream::name(const Symbol* name) {
 443   assert_if_no_error(inside_attrs(), "printing attributes");
 444   if (name == NULL)  return;
 445   print_raw(" name='");
 446   name_text(name);
 447   print_raw("'");
 448 }
 449 
 450 void xmlStream::name_text(const Symbol* name) {
 451   assert_if_no_error(inside_attrs(), "printing attributes");
 452   if (name == NULL)  return;
 453   //name->print_short_name(text());
 454   name->print_symbol_on(text());
 455 }
 456 
 457 void xmlStream::object(const char* attr, Handle x) {
 458   assert_if_no_error(inside_attrs(), "printing attributes");
 459   if (x == NULL)  return;
 460   print_raw(" ");
 461   print_raw(attr);
 462   print_raw("='");
 463   object_text(x);
 464   print_raw("'");
 465 }
 466 
 467 void xmlStream::object_text(Handle x) {
 468   assert_if_no_error(inside_attrs(), "printing attributes");
 469   if (x == NULL)  return;
 470   x->print_value_on(text());
 471 }
 472 
 473 
 474 void xmlStream::object(const char* attr, Metadata* x) {
 475   assert_if_no_error(inside_attrs(), "printing attributes");
 476   if (x == NULL)  return;
 477   print_raw(" ");
 478   print_raw(attr);
 479   print_raw("='");
 480   object_text(x);
 481   print_raw("'");
 482 }
 483 
 484 void xmlStream::object_text(Metadata* x) {
 485   assert_if_no_error(inside_attrs(), "printing attributes");
 486   if (x == NULL)  return;
 487   //x->print_value_on(text());
 488   if (x->is_method())
 489     method_text((Method*)x);
 490   else if (x->is_klass())
 491     klass_text((Klass*)x);
 492   else
 493     ShouldNotReachHere(); // Add impl if this is reached.
 494 }
 495 
 496 
 497 void xmlStream::flush() {
 498   out()->flush();
 499   _last_flush = count();
 500 }
 501 
 502 void xmlTextStream::flush() {
 503   if (_outer_xmlStream == NULL)  return;
 504   _outer_xmlStream->flush();
 505 }
 506 
 507 void xmlTextStream::write(const char* str, size_t len) {
 508   if (_outer_xmlStream == NULL)  return;
 509   _outer_xmlStream->write_text(str, len);
 510   update_position(str, len);
 511 }