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