src/share/vm/utilities/xmlstream.cpp

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

mercurial