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