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