hotspot/src/share/vm/compiler/compileLog.cpp
author ysr
Fri, 30 Jan 2009 14:17:52 -0800
changeset 2005 42075507972b
parent 1 489c9b5090e2
child 5402 c51fd0c1d005
permissions -rw-r--r--
6787254: Work queue capacity can be increased substantially on some platforms Summary: Increased the default and maximum size of the CMS marking stack and the size of the parallel workers' work queues in 64-bit mode. The latter was accomplished by an increase in the width of the Taskqueue's Age struct and its Tag field in 64-bit mode. Reviewed-by: jmasa, tonyp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include "incls/_compileLog.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
CompileLog* CompileLog::_first = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// ------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// CompileLog::CompileLog
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
CompileLog::CompileLog(const char* file, FILE* fp, intx thread_id)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  : _context(_context_buffer, sizeof(_context_buffer))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  initialize(new(ResourceObj::C_HEAP) fileStream(fp));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  _file = file;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  _file_end = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  _thread_id = thread_id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  _identities_limit = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  _identities_capacity = 400;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  // link into the global list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  { MutexLocker locker(CompileTaskAlloc_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    _next = _first;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    _first = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
CompileLog::~CompileLog() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  delete _out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _out = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  FREE_C_HEAP_ARRAY(char, _identities);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// Advance kind up to a null or space, return this tail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// Make sure kind is null-terminated, not space-terminated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// Use the buffer if necessary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
static const char* split_attrs(const char* &kind, char* buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  const char* attrs = strchr(kind, ' ');
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  // Tease apart the first word from the rest:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  if (attrs == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    return "";  // no attrs, no split
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  } else if (kind == buffer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    ((char*) attrs)[-1] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    return attrs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    // park it in the buffer, so we can put a null on the end
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    assert(!(kind >= buffer && kind < buffer+100), "not obviously in buffer")
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    int klen = attrs - kind;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    strncpy(buffer, kind, klen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    buffer[klen] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    kind = buffer;  // return by reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    return attrs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// see_tag, pop_tag:  Override the default do-nothing methods on xmlStream.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
// These methods provide a hook for managing the the extra context markup.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
void CompileLog::see_tag(const char* tag, bool push) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  if (_context.size() > 0 && _out != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    _out->write(_context.base(), _context.size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    _context.reset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  xmlStream::see_tag(tag, push);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
void CompileLog::pop_tag(const char* tag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  _context.reset();  // toss any context info.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  xmlStream::pop_tag(tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
// ------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
// CompileLog::identify
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
int CompileLog::identify(ciObject* obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  if (obj == NULL)  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  int id = obj->ident();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  if (id < 0)  return id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // If it has already been identified, just return the id.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  if (id < _identities_limit && _identities[id] != 0)  return id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // Lengthen the array, if necessary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  if (id >= _identities_capacity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    int new_cap = _identities_capacity * 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    if (new_cap <= id)  new_cap = id + 100;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    _identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    _identities_capacity = new_cap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  while (id >= _identities_limit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    _identities[_identities_limit++] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  assert(id < _identities_limit, "oob");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  // Mark this id as processed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  // (Be sure to do this before any recursive calls to identify.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  _identities[id] = 1;  // mark
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // Now, print the object's identity once, in detail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  if (obj->is_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    ciKlass* klass = obj->as_klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    begin_elem("klass id='%d'", id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    name(klass->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    if (!klass->is_loaded()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      print(" unloaded='1'");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      print(" flags='%d'", klass->modifier_flags());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    end_elem();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  } else if (obj->is_method()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    ciMethod* method = obj->as_method();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    ciSignature* sig = method->signature();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    // Pre-identify items that we will need!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    identify(sig->return_type());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    for (int i = 0; i < sig->count(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      identify(sig->type_at(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    begin_elem("method id='%d' holder='%d'",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
               id, identify(method->holder()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    name(method->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    print(" return='%d'", identify(sig->return_type()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    if (sig->count() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      print(" arguments='");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      for (int i = 0; i < sig->count(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        print((i == 0) ? "%d" : " %d", identify(sig->type_at(i)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      print("'");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    if (!method->is_loaded()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      print(" unloaded='1'");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      print(" flags='%d'", (jchar) method->flags().as_int());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      // output a few metrics
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      print(" bytes='%d'", method->code_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      method->log_nmethod_identity(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      //print(" count='%d'", method->invocation_count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      //int bec = method->backedge_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      //if (bec != 0)  print(" backedge_count='%d'", bec);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      print(" iicount='%d'", method->interpreter_invocation_count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    end_elem();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  } else if (obj->is_symbol()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    begin_elem("symbol id='%d'", id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    name(obj->as_symbol());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    end_elem();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  } else if (obj->is_null_object()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    elem("null_object id='%d'", id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  } else if (obj->is_type()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    BasicType type = obj->as_type()->basic_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    elem("type id='%d' name='%s'", id, type2name(type));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    // Should not happen.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    elem("unknown id='%d'", id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  return id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
void CompileLog::name(ciSymbol* name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  if (name == NULL)  return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  print(" name='");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  name->print_symbol_on(text());  // handles quoting conventions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  print("'");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
// ------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
// CompileLog::clear_identities
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
// Forget which identities have been printed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
void CompileLog::clear_identities() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  _identities_limit = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
// ------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
// CompileLog::finish_log_on_error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
// Note: This function is called after fatal error, avoid unnecessary memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
// or stack allocation, use only async-safe functions. It's possible JVM is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
// only partially initialized.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  static bool called_exit = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  if (called_exit)  return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  called_exit = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  for (CompileLog* log = _first; log != NULL; log = log->_next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    log->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    const char* partial_file = log->file();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    int partial_fd = open(partial_file, O_RDONLY);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    if (partial_fd != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      // print/print_cr may need to allocate large stack buffer to format
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
      // strings, here we use snprintf() and print_raw() instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
      file->print_raw("<compilation_log thread='");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      jio_snprintf(buf, buflen, UINTX_FORMAT, log->thread_id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      file->print_raw(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
      file->print_raw_cr("'>");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      size_t nr; // number read into buf from partial log
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      // Copy data up to the end of the last <event> element:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      julong to_read = log->_file_end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
      while (to_read > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
        if (to_read < (julong)buflen)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
              nr = (size_t)to_read;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
        else  nr = buflen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
        nr = read(partial_fd, buf, (int)nr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
        if (nr <= 0)  break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        to_read -= (julong)nr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
        file->write(buf, nr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      // Copy any remaining data inside a quote:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      bool saw_slop = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      int end_cdata = 0;  // state machine [0..2] watching for too many "]]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      while ((nr = read(partial_fd, buf, buflen)) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
        if (!saw_slop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
          file->print_raw_cr("<fragment>");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
          file->print_raw_cr("<![CDATA[");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
          saw_slop = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
        // The rest of this loop amounts to a simple copy operation:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
        // { file->write(buf, nr); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
        // However, it must sometimes output the buffer in parts,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
        // in case there is a CDATA quote embedded in the fragment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        const char* bufp;  // pointer into buf
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
        size_t nw; // number written in each pass of the following loop:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
        for (bufp = buf; nr > 0; nr -= nw, bufp += nw) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
          // Write up to any problematic CDATA delimiter (usually all of nr).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
          for (nw = 0; nw < nr; nw++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
            // First, scan ahead into the buf, checking the state machine.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
            switch (bufp[nw]) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
            case ']':
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
              if (end_cdata < 2)   end_cdata += 1;  // saturating counter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
              continue;  // keep scanning
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
            case '>':
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
              if (end_cdata == 2)  break;  // found CDATA delimiter!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
              // else fall through:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
            default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
              end_cdata = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
              continue;  // keep scanning
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
            // If we get here, nw is pointing at a bad '>'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
            // It is very rare for this to happen.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
            // However, this code has been tested by introducing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
            // CDATA sequences into the compilation log.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
          // Now nw is the number of characters to write, usually == nr.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
          file->write(bufp, nw);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
          if (nw < nr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
            // We are about to go around the loop again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
            // But first, disrupt the ]]> by closing and reopening the quote.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
            file->print_raw("]]><![CDATA[");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
            end_cdata = 0;  // reset state machine
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
      if (saw_slop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
        file->print_raw_cr("]]>");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
        file->print_raw_cr("</fragment>");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      file->print_raw_cr("</compilation_log>");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      close(partial_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      unlink(partial_file);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
// ------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
// CompileLog::finish_log
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
// Called during normal shutdown. For now, any clean-up needed in normal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
// shutdown is also needed in VM abort, so is covered by finish_log_on_error().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
// Just allocate a buffer and call finish_log_on_error().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
void CompileLog::finish_log(outputStream* file) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  char buf[4 * K];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  finish_log_on_error(file, buf, sizeof(buf));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
}