Wed, 08 Jan 2014 10:25:50 -0800
8028468: Add inlining information into ciReplay
Summary: Allow dump and replay inlining for specified method during a program execution.
Reviewed-by: roland, twisti
1 /* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 *
22 */
24 #include "precompiled.hpp"
25 #include "ci/ciMethodData.hpp"
26 #include "ci/ciReplay.hpp"
27 #include "ci/ciSymbol.hpp"
28 #include "ci/ciKlass.hpp"
29 #include "ci/ciUtilities.hpp"
30 #include "compiler/compileBroker.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/oopFactory.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "utilities/copy.hpp"
35 #include "utilities/macros.hpp"
37 #ifndef PRODUCT
39 // ciReplay
41 typedef struct _ciMethodDataRecord {
42 const char* _klass_name;
43 const char* _method_name;
44 const char* _signature;
46 int _state;
47 int _current_mileage;
49 intptr_t* _data;
50 char* _orig_data;
51 jobject* _oops_handles;
52 int* _oops_offsets;
53 int _data_length;
54 int _orig_data_length;
55 int _oops_length;
56 } ciMethodDataRecord;
58 typedef struct _ciMethodRecord {
59 const char* _klass_name;
60 const char* _method_name;
61 const char* _signature;
63 int _instructions_size;
64 int _interpreter_invocation_count;
65 int _interpreter_throwout_count;
66 int _invocation_counter;
67 int _backedge_counter;
68 } ciMethodRecord;
70 typedef struct _ciInlineRecord {
71 const char* _klass_name;
72 const char* _method_name;
73 const char* _signature;
75 int _inline_depth;
76 int _inline_bci;
77 } ciInlineRecord;
79 class CompileReplay;
80 static CompileReplay* replay_state;
82 class CompileReplay : public StackObj {
83 private:
84 FILE* _stream;
85 Thread* _thread;
86 Handle _protection_domain;
87 Handle _loader;
89 GrowableArray<ciMethodRecord*> _ci_method_records;
90 GrowableArray<ciMethodDataRecord*> _ci_method_data_records;
92 // Use pointer because we may need to return inline records
93 // without destroying them.
94 GrowableArray<ciInlineRecord*>* _ci_inline_records;
96 const char* _error_message;
98 char* _bufptr;
99 char* _buffer;
100 int _buffer_length;
101 int _buffer_pos;
103 // "compile" data
104 ciKlass* _iklass;
105 Method* _imethod;
106 int _entry_bci;
107 int _comp_level;
109 public:
110 CompileReplay(const char* filename, TRAPS) {
111 _thread = THREAD;
112 _loader = Handle(_thread, SystemDictionary::java_system_loader());
113 _protection_domain = Handle();
115 _stream = fopen(filename, "rt");
116 if (_stream == NULL) {
117 fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
118 }
120 _ci_inline_records = NULL;
121 _error_message = NULL;
123 _buffer_length = 32;
124 _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
125 _bufptr = _buffer;
126 _buffer_pos = 0;
128 _imethod = NULL;
129 _iklass = NULL;
130 _entry_bci = 0;
131 _comp_level = 0;
133 test();
134 }
136 ~CompileReplay() {
137 if (_stream != NULL) fclose(_stream);
138 }
140 void test() {
141 strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
142 _bufptr = _buffer;
143 assert(parse_int("test") == 1, "what");
144 assert(parse_int("test") == 2, "what");
145 assert(strcmp(parse_string(), "foo") == 0, "what");
146 assert(parse_int("test") == 4, "what");
147 assert(strcmp(parse_string(), "bar") == 0, "what");
148 assert(parse_intptr_t("test") == 9, "what");
149 assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
150 }
152 bool had_error() {
153 return _error_message != NULL || _thread->has_pending_exception();
154 }
156 bool can_replay() {
157 return !(_stream == NULL || had_error());
158 }
160 void report_error(const char* msg) {
161 _error_message = msg;
162 // Restore the _buffer contents for error reporting
163 for (int i = 0; i < _buffer_pos; i++) {
164 if (_buffer[i] == '\0') _buffer[i] = ' ';
165 }
166 }
168 int parse_int(const char* label) {
169 if (had_error()) {
170 return 0;
171 }
173 int v = 0;
174 int read;
175 if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
176 report_error(label);
177 } else {
178 _bufptr += read;
179 }
180 return v;
181 }
183 intptr_t parse_intptr_t(const char* label) {
184 if (had_error()) {
185 return 0;
186 }
188 intptr_t v = 0;
189 int read;
190 if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
191 report_error(label);
192 } else {
193 _bufptr += read;
194 }
195 return v;
196 }
198 void skip_ws() {
199 // Skip any leading whitespace
200 while (*_bufptr == ' ' || *_bufptr == '\t') {
201 _bufptr++;
202 }
203 }
206 char* scan_and_terminate(char delim) {
207 char* str = _bufptr;
208 while (*_bufptr != delim && *_bufptr != '\0') {
209 _bufptr++;
210 }
211 if (*_bufptr != '\0') {
212 *_bufptr++ = '\0';
213 }
214 if (_bufptr == str) {
215 // nothing here
216 return NULL;
217 }
218 return str;
219 }
221 char* parse_string() {
222 if (had_error()) return NULL;
224 skip_ws();
225 return scan_and_terminate(' ');
226 }
228 char* parse_quoted_string() {
229 if (had_error()) return NULL;
231 skip_ws();
233 if (*_bufptr == '"') {
234 _bufptr++;
235 return scan_and_terminate('"');
236 } else {
237 return scan_and_terminate(' ');
238 }
239 }
241 const char* parse_escaped_string() {
242 char* result = parse_quoted_string();
243 if (result != NULL) {
244 unescape_string(result);
245 }
246 return result;
247 }
249 // Look for the tag 'tag' followed by an
250 bool parse_tag_and_count(const char* tag, int& length) {
251 const char* t = parse_string();
252 if (t == NULL) {
253 return false;
254 }
256 if (strcmp(tag, t) != 0) {
257 report_error(tag);
258 return false;
259 }
260 length = parse_int("parse_tag_and_count");
261 return !had_error();
262 }
264 // Parse a sequence of raw data encoded as bytes and return the
265 // resulting data.
266 char* parse_data(const char* tag, int& length) {
267 if (!parse_tag_and_count(tag, length)) {
268 return NULL;
269 }
271 char * result = NEW_RESOURCE_ARRAY(char, length);
272 for (int i = 0; i < length; i++) {
273 int val = parse_int("data");
274 result[i] = val;
275 }
276 return result;
277 }
279 // Parse a standard chunk of data emitted as:
280 // 'tag' <length> # # ...
281 // Where each # is an intptr_t item
282 intptr_t* parse_intptr_data(const char* tag, int& length) {
283 if (!parse_tag_and_count(tag, length)) {
284 return NULL;
285 }
287 intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
288 for (int i = 0; i < length; i++) {
289 skip_ws();
290 intptr_t val = parse_intptr_t("data");
291 result[i] = val;
292 }
293 return result;
294 }
296 // Parse a possibly quoted version of a symbol into a symbolOop
297 Symbol* parse_symbol(TRAPS) {
298 const char* str = parse_escaped_string();
299 if (str != NULL) {
300 Symbol* sym = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
301 return sym;
302 }
303 return NULL;
304 }
306 // Parse a valid klass name and look it up
307 Klass* parse_klass(TRAPS) {
308 const char* str = parse_escaped_string();
309 Symbol* klass_name = SymbolTable::lookup(str, (int)strlen(str), CHECK_NULL);
310 if (klass_name != NULL) {
311 Klass* k = NULL;
312 if (_iklass != NULL) {
313 k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
314 } else {
315 k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
316 }
317 if (HAS_PENDING_EXCEPTION) {
318 oop throwable = PENDING_EXCEPTION;
319 java_lang_Throwable::print(throwable, tty);
320 tty->cr();
321 report_error(str);
322 return NULL;
323 }
324 return k;
325 }
326 return NULL;
327 }
329 // Lookup a klass
330 Klass* resolve_klass(const char* klass, TRAPS) {
331 Symbol* klass_name = SymbolTable::lookup(klass, (int)strlen(klass), CHECK_NULL);
332 return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, CHECK_NULL);
333 }
335 // Parse the standard tuple of <klass> <name> <signature>
336 Method* parse_method(TRAPS) {
337 InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
338 Symbol* method_name = parse_symbol(CHECK_NULL);
339 Symbol* method_signature = parse_symbol(CHECK_NULL);
340 Method* m = k->find_method(method_name, method_signature);
341 if (m == NULL) {
342 report_error("Can't find method");
343 }
344 return m;
345 }
347 int get_line(int c) {
348 while(c != EOF) {
349 if (_buffer_pos + 1 >= _buffer_length) {
350 int new_length = _buffer_length * 2;
351 // Next call will throw error in case of OOM.
352 _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
353 _buffer_length = new_length;
354 }
355 if (c == '\n') {
356 c = getc(_stream); // get next char
357 break;
358 } else if (c == '\r') {
359 // skip LF
360 } else {
361 _buffer[_buffer_pos++] = c;
362 }
363 c = getc(_stream);
364 }
365 // null terminate it, reset the pointer
366 _buffer[_buffer_pos] = '\0'; // NL or EOF
367 _buffer_pos = 0;
368 _bufptr = _buffer;
369 return c;
370 }
372 // Process each line of the replay file executing each command until
373 // the file ends.
374 void process(TRAPS) {
375 int line_no = 1;
376 int c = getc(_stream);
377 while(c != EOF) {
378 c = get_line(c);
379 process_command(CHECK);
380 if (had_error()) {
381 tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
382 tty->print_cr("%s", _buffer);
383 return;
384 }
385 line_no++;
386 }
387 }
389 void process_command(TRAPS) {
390 char* cmd = parse_string();
391 if (cmd == NULL) {
392 return;
393 }
394 if (strcmp("#", cmd) == 0) {
395 // ignore
396 } else if (strcmp("compile", cmd) == 0) {
397 process_compile(CHECK);
398 } else if (strcmp("ciMethod", cmd) == 0) {
399 process_ciMethod(CHECK);
400 } else if (strcmp("ciMethodData", cmd) == 0) {
401 process_ciMethodData(CHECK);
402 } else if (strcmp("staticfield", cmd) == 0) {
403 process_staticfield(CHECK);
404 } else if (strcmp("ciInstanceKlass", cmd) == 0) {
405 process_ciInstanceKlass(CHECK);
406 } else if (strcmp("instanceKlass", cmd) == 0) {
407 process_instanceKlass(CHECK);
408 #if INCLUDE_JVMTI
409 } else if (strcmp("JvmtiExport", cmd) == 0) {
410 process_JvmtiExport(CHECK);
411 #endif // INCLUDE_JVMTI
412 } else {
413 report_error("unknown command");
414 }
415 }
417 // validation of comp_level
418 bool is_valid_comp_level(int comp_level) {
419 const int msg_len = 256;
420 char* msg = NULL;
421 if (!is_compile(comp_level)) {
422 msg = NEW_RESOURCE_ARRAY(char, msg_len);
423 jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
424 } else if (!TieredCompilation && (comp_level != CompLevel_highest_tier)) {
425 msg = NEW_RESOURCE_ARRAY(char, msg_len);
426 switch (comp_level) {
427 case CompLevel_simple:
428 jio_snprintf(msg, msg_len, "compilation level %d requires Client VM or TieredCompilation", comp_level);
429 break;
430 case CompLevel_full_optimization:
431 jio_snprintf(msg, msg_len, "compilation level %d requires Server VM", comp_level);
432 break;
433 default:
434 jio_snprintf(msg, msg_len, "compilation level %d requires TieredCompilation", comp_level);
435 }
436 }
437 if (msg != NULL) {
438 report_error(msg);
439 return false;
440 }
441 return true;
442 }
444 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> <depth> <bci> <klass> <name> <signature> ...
445 void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
446 _imethod = m;
447 _iklass = imethod->holder();
448 _entry_bci = entry_bci;
449 _comp_level = comp_level;
450 int line_no = 1;
451 int c = getc(_stream);
452 while(c != EOF) {
453 c = get_line(c);
454 // Expecting only lines with "compile" command in inline replay file.
455 char* cmd = parse_string();
456 if (cmd == NULL || strcmp("compile", cmd) != 0) {
457 return NULL;
458 }
459 process_compile(CHECK_NULL);
460 if (had_error()) {
461 tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
462 tty->print_cr("%s", _buffer);
463 return NULL;
464 }
465 if (_ci_inline_records != NULL && _ci_inline_records->length() > 0) {
466 // Found inlining record for the requested method.
467 return _ci_inline_records;
468 }
469 line_no++;
470 }
471 return NULL;
472 }
474 // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> <depth> <bci> <klass> <name> <signature> ...
475 void process_compile(TRAPS) {
476 Method* method = parse_method(CHECK);
477 if (had_error()) return;
478 int entry_bci = parse_int("entry_bci");
479 const char* comp_level_label = "comp_level";
480 int comp_level = parse_int(comp_level_label);
481 // old version w/o comp_level
482 if (had_error() && (error_message() == comp_level_label)) {
483 comp_level = CompLevel_full_optimization;
484 }
485 if (!is_valid_comp_level(comp_level)) {
486 return;
487 }
488 if (_imethod != NULL) {
489 // Replay Inlining
490 if (entry_bci != _entry_bci || comp_level != _comp_level) {
491 return;
492 }
493 const char* iklass_name = _imethod->method_holder()->name()->as_utf8();
494 const char* imethod_name = _imethod->name()->as_utf8();
495 const char* isignature = _imethod->signature()->as_utf8();
496 const char* klass_name = method->method_holder()->name()->as_utf8();
497 const char* method_name = method->name()->as_utf8();
498 const char* signature = method->signature()->as_utf8();
499 if (strcmp(iklass_name, klass_name) != 0 ||
500 strcmp(imethod_name, method_name) != 0 ||
501 strcmp(isignature, signature) != 0) {
502 return;
503 }
504 }
505 int inline_count = 0;
506 if (parse_tag_and_count("inline", inline_count)) {
507 // Record inlining data
508 _ci_inline_records = new GrowableArray<ciInlineRecord*>();
509 for (int i = 0; i < inline_count; i++) {
510 int depth = parse_int("inline_depth");
511 int bci = parse_int("inline_bci");
512 if (had_error()) {
513 break;
514 }
515 Method* inl_method = parse_method(CHECK);
516 if (had_error()) {
517 break;
518 }
519 new_ciInlineRecord(inl_method, bci, depth);
520 }
521 }
522 if (_imethod != NULL) {
523 return; // Replay Inlining
524 }
525 Klass* k = method->method_holder();
526 ((InstanceKlass*)k)->initialize(THREAD);
527 if (HAS_PENDING_EXCEPTION) {
528 oop throwable = PENDING_EXCEPTION;
529 java_lang_Throwable::print(throwable, tty);
530 tty->cr();
531 if (ReplayIgnoreInitErrors) {
532 CLEAR_PENDING_EXCEPTION;
533 ((InstanceKlass*)k)->set_init_state(InstanceKlass::fully_initialized);
534 } else {
535 return;
536 }
537 }
538 // Make sure the existence of a prior compile doesn't stop this one
539 nmethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
540 if (nm != NULL) {
541 nm->make_not_entrant();
542 }
543 replay_state = this;
544 CompileBroker::compile_method(method, entry_bci, comp_level,
545 methodHandle(), 0, "replay", THREAD);
546 replay_state = NULL;
547 reset();
548 }
550 // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
551 //
552 //
553 void process_ciMethod(TRAPS) {
554 Method* method = parse_method(CHECK);
555 if (had_error()) return;
556 ciMethodRecord* rec = new_ciMethod(method);
557 rec->_invocation_counter = parse_int("invocation_counter");
558 rec->_backedge_counter = parse_int("backedge_counter");
559 rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
560 rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
561 rec->_instructions_size = parse_int("instructions_size");
562 }
564 // ciMethodData <klass> <name> <signature> <state> <current mileage> orig <length> # # ... data <length> # # ... oops <length>
565 void process_ciMethodData(TRAPS) {
566 Method* method = parse_method(CHECK);
567 if (had_error()) return;
568 /* jsut copied from Method, to build interpret data*/
569 if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) {
570 return;
571 }
572 // methodOopDesc::build_interpreter_method_data(method, CHECK);
573 {
574 // Grab a lock here to prevent multiple
575 // MethodData*s from being created.
576 MutexLocker ml(MethodData_lock, THREAD);
577 if (method->method_data() == NULL) {
578 ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
579 MethodData* method_data = MethodData::allocate(loader_data, method, CHECK);
580 method->set_method_data(method_data);
581 }
582 }
584 // collect and record all the needed information for later
585 ciMethodDataRecord* rec = new_ciMethodData(method);
586 rec->_state = parse_int("state");
587 rec->_current_mileage = parse_int("current_mileage");
589 rec->_orig_data = parse_data("orig", rec->_orig_data_length);
590 if (rec->_orig_data == NULL) {
591 return;
592 }
593 rec->_data = parse_intptr_data("data", rec->_data_length);
594 if (rec->_data == NULL) {
595 return;
596 }
597 if (!parse_tag_and_count("oops", rec->_oops_length)) {
598 return;
599 }
600 rec->_oops_handles = NEW_RESOURCE_ARRAY(jobject, rec->_oops_length);
601 rec->_oops_offsets = NEW_RESOURCE_ARRAY(int, rec->_oops_length);
602 for (int i = 0; i < rec->_oops_length; i++) {
603 int offset = parse_int("offset");
604 if (had_error()) {
605 return;
606 }
607 Klass* k = parse_klass(CHECK);
608 rec->_oops_offsets[i] = offset;
609 KlassHandle *kh = NEW_C_HEAP_OBJ(KlassHandle, mtCompiler);
610 ::new ((void*)kh) KlassHandle(THREAD, k);
611 rec->_oops_handles[i] = (jobject)kh;
612 }
613 }
615 // instanceKlass <name>
616 //
617 // Loads and initializes the klass 'name'. This can be used to
618 // create particular class loading environments
619 void process_instanceKlass(TRAPS) {
620 // just load the referenced class
621 Klass* k = parse_klass(CHECK);
622 }
624 // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag # # # ...
625 //
626 // Load the klass 'name' and link or initialize it. Verify that the
627 // constant pool is the same length as 'length' and make sure the
628 // constant pool tags are in the same state.
629 void process_ciInstanceKlass(TRAPS) {
630 InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
631 int is_linked = parse_int("is_linked");
632 int is_initialized = parse_int("is_initialized");
633 int length = parse_int("length");
634 if (is_initialized) {
635 k->initialize(THREAD);
636 if (HAS_PENDING_EXCEPTION) {
637 oop throwable = PENDING_EXCEPTION;
638 java_lang_Throwable::print(throwable, tty);
639 tty->cr();
640 if (ReplayIgnoreInitErrors) {
641 CLEAR_PENDING_EXCEPTION;
642 k->set_init_state(InstanceKlass::fully_initialized);
643 } else {
644 return;
645 }
646 }
647 } else if (is_linked) {
648 k->link_class(CHECK);
649 }
650 ConstantPool* cp = k->constants();
651 if (length != cp->length()) {
652 report_error("constant pool length mismatch: wrong class files?");
653 return;
654 }
656 int parsed_two_word = 0;
657 for (int i = 1; i < length; i++) {
658 int tag = parse_int("tag");
659 if (had_error()) {
660 return;
661 }
662 switch (cp->tag_at(i).value()) {
663 case JVM_CONSTANT_UnresolvedClass: {
664 if (tag == JVM_CONSTANT_Class) {
665 tty->print_cr("Resolving klass %s at %d", cp->unresolved_klass_at(i)->as_utf8(), i);
666 Klass* k = cp->klass_at(i, CHECK);
667 }
668 break;
669 }
670 case JVM_CONSTANT_Long:
671 case JVM_CONSTANT_Double:
672 parsed_two_word = i + 1;
674 case JVM_CONSTANT_ClassIndex:
675 case JVM_CONSTANT_StringIndex:
676 case JVM_CONSTANT_String:
677 case JVM_CONSTANT_UnresolvedClassInError:
678 case JVM_CONSTANT_Fieldref:
679 case JVM_CONSTANT_Methodref:
680 case JVM_CONSTANT_InterfaceMethodref:
681 case JVM_CONSTANT_NameAndType:
682 case JVM_CONSTANT_Utf8:
683 case JVM_CONSTANT_Integer:
684 case JVM_CONSTANT_Float:
685 case JVM_CONSTANT_MethodHandle:
686 case JVM_CONSTANT_MethodType:
687 case JVM_CONSTANT_InvokeDynamic:
688 if (tag != cp->tag_at(i).value()) {
689 report_error("tag mismatch: wrong class files?");
690 return;
691 }
692 break;
694 case JVM_CONSTANT_Class:
695 if (tag == JVM_CONSTANT_Class) {
696 } else if (tag == JVM_CONSTANT_UnresolvedClass) {
697 tty->print_cr("Warning: entry was unresolved in the replay data");
698 } else {
699 report_error("Unexpected tag");
700 return;
701 }
702 break;
704 case 0:
705 if (parsed_two_word == i) continue;
707 default:
708 fatal(err_msg_res("Unexpected tag: %d", cp->tag_at(i).value()));
709 break;
710 }
712 }
713 }
715 // Initialize a class and fill in the value for a static field.
716 // This is useful when the compile was dependent on the value of
717 // static fields but it's impossible to properly rerun the static
718 // initiailizer.
719 void process_staticfield(TRAPS) {
720 InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
722 if (ReplaySuppressInitializers == 0 ||
723 ReplaySuppressInitializers == 2 && k->class_loader() == NULL) {
724 return;
725 }
727 assert(k->is_initialized(), "must be");
729 const char* field_name = parse_escaped_string();;
730 const char* field_signature = parse_string();
731 fieldDescriptor fd;
732 Symbol* name = SymbolTable::lookup(field_name, (int)strlen(field_name), CHECK);
733 Symbol* sig = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
734 if (!k->find_local_field(name, sig, &fd) ||
735 !fd.is_static() ||
736 fd.has_initial_value()) {
737 report_error(field_name);
738 return;
739 }
741 oop java_mirror = k->java_mirror();
742 if (field_signature[0] == '[') {
743 int length = parse_int("array length");
744 oop value = NULL;
746 if (field_signature[1] == '[') {
747 // multi dimensional array
748 ArrayKlass* kelem = (ArrayKlass *)parse_klass(CHECK);
749 int rank = 0;
750 while (field_signature[rank] == '[') {
751 rank++;
752 }
753 int* dims = NEW_RESOURCE_ARRAY(int, rank);
754 dims[0] = length;
755 for (int i = 1; i < rank; i++) {
756 dims[i] = 1; // These aren't relevant to the compiler
757 }
758 value = kelem->multi_allocate(rank, dims, CHECK);
759 } else {
760 if (strcmp(field_signature, "[B") == 0) {
761 value = oopFactory::new_byteArray(length, CHECK);
762 } else if (strcmp(field_signature, "[Z") == 0) {
763 value = oopFactory::new_boolArray(length, CHECK);
764 } else if (strcmp(field_signature, "[C") == 0) {
765 value = oopFactory::new_charArray(length, CHECK);
766 } else if (strcmp(field_signature, "[S") == 0) {
767 value = oopFactory::new_shortArray(length, CHECK);
768 } else if (strcmp(field_signature, "[F") == 0) {
769 value = oopFactory::new_singleArray(length, CHECK);
770 } else if (strcmp(field_signature, "[D") == 0) {
771 value = oopFactory::new_doubleArray(length, CHECK);
772 } else if (strcmp(field_signature, "[I") == 0) {
773 value = oopFactory::new_intArray(length, CHECK);
774 } else if (strcmp(field_signature, "[J") == 0) {
775 value = oopFactory::new_longArray(length, CHECK);
776 } else if (field_signature[0] == '[' && field_signature[1] == 'L') {
777 KlassHandle kelem = resolve_klass(field_signature + 1, CHECK);
778 value = oopFactory::new_objArray(kelem(), length, CHECK);
779 } else {
780 report_error("unhandled array staticfield");
781 }
782 }
783 java_mirror->obj_field_put(fd.offset(), value);
784 } else {
785 const char* string_value = parse_escaped_string();
786 if (strcmp(field_signature, "I") == 0) {
787 int value = atoi(string_value);
788 java_mirror->int_field_put(fd.offset(), value);
789 } else if (strcmp(field_signature, "B") == 0) {
790 int value = atoi(string_value);
791 java_mirror->byte_field_put(fd.offset(), value);
792 } else if (strcmp(field_signature, "C") == 0) {
793 int value = atoi(string_value);
794 java_mirror->char_field_put(fd.offset(), value);
795 } else if (strcmp(field_signature, "S") == 0) {
796 int value = atoi(string_value);
797 java_mirror->short_field_put(fd.offset(), value);
798 } else if (strcmp(field_signature, "Z") == 0) {
799 int value = atol(string_value);
800 java_mirror->bool_field_put(fd.offset(), value);
801 } else if (strcmp(field_signature, "J") == 0) {
802 jlong value;
803 if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
804 fprintf(stderr, "Error parsing long: %s\n", string_value);
805 return;
806 }
807 java_mirror->long_field_put(fd.offset(), value);
808 } else if (strcmp(field_signature, "F") == 0) {
809 float value = atof(string_value);
810 java_mirror->float_field_put(fd.offset(), value);
811 } else if (strcmp(field_signature, "D") == 0) {
812 double value = atof(string_value);
813 java_mirror->double_field_put(fd.offset(), value);
814 } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
815 Handle value = java_lang_String::create_from_str(string_value, CHECK);
816 java_mirror->obj_field_put(fd.offset(), value());
817 } else if (field_signature[0] == 'L') {
818 Symbol* klass_name = SymbolTable::lookup(field_signature, (int)strlen(field_signature), CHECK);
819 KlassHandle kelem = resolve_klass(field_signature, CHECK);
820 oop value = ((InstanceKlass*)kelem())->allocate_instance(CHECK);
821 java_mirror->obj_field_put(fd.offset(), value);
822 } else {
823 report_error("unhandled staticfield");
824 }
825 }
826 }
828 #if INCLUDE_JVMTI
829 void process_JvmtiExport(TRAPS) {
830 const char* field = parse_string();
831 bool value = parse_int("JvmtiExport flag") != 0;
832 if (strcmp(field, "can_access_local_variables") == 0) {
833 JvmtiExport::set_can_access_local_variables(value);
834 } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
835 JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
836 } else if (strcmp(field, "can_post_on_exceptions") == 0) {
837 JvmtiExport::set_can_post_on_exceptions(value);
838 } else {
839 report_error("Unrecognized JvmtiExport directive");
840 }
841 }
842 #endif // INCLUDE_JVMTI
844 // Create and initialize a record for a ciMethod
845 ciMethodRecord* new_ciMethod(Method* method) {
846 ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
847 rec->_klass_name = method->method_holder()->name()->as_utf8();
848 rec->_method_name = method->name()->as_utf8();
849 rec->_signature = method->signature()->as_utf8();
850 _ci_method_records.append(rec);
851 return rec;
852 }
854 // Lookup data for a ciMethod
855 ciMethodRecord* find_ciMethodRecord(Method* method) {
856 const char* klass_name = method->method_holder()->name()->as_utf8();
857 const char* method_name = method->name()->as_utf8();
858 const char* signature = method->signature()->as_utf8();
859 for (int i = 0; i < _ci_method_records.length(); i++) {
860 ciMethodRecord* rec = _ci_method_records.at(i);
861 if (strcmp(rec->_klass_name, klass_name) == 0 &&
862 strcmp(rec->_method_name, method_name) == 0 &&
863 strcmp(rec->_signature, signature) == 0) {
864 return rec;
865 }
866 }
867 return NULL;
868 }
870 // Create and initialize a record for a ciMethodData
871 ciMethodDataRecord* new_ciMethodData(Method* method) {
872 ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
873 rec->_klass_name = method->method_holder()->name()->as_utf8();
874 rec->_method_name = method->name()->as_utf8();
875 rec->_signature = method->signature()->as_utf8();
876 _ci_method_data_records.append(rec);
877 return rec;
878 }
880 // Lookup data for a ciMethodData
881 ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
882 const char* klass_name = method->method_holder()->name()->as_utf8();
883 const char* method_name = method->name()->as_utf8();
884 const char* signature = method->signature()->as_utf8();
885 for (int i = 0; i < _ci_method_data_records.length(); i++) {
886 ciMethodDataRecord* rec = _ci_method_data_records.at(i);
887 if (strcmp(rec->_klass_name, klass_name) == 0 &&
888 strcmp(rec->_method_name, method_name) == 0 &&
889 strcmp(rec->_signature, signature) == 0) {
890 return rec;
891 }
892 }
893 return NULL;
894 }
896 // Create and initialize a record for a ciInlineRecord
897 ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth) {
898 ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
899 rec->_klass_name = method->method_holder()->name()->as_utf8();
900 rec->_method_name = method->name()->as_utf8();
901 rec->_signature = method->signature()->as_utf8();
902 rec->_inline_bci = bci;
903 rec->_inline_depth = depth;
904 _ci_inline_records->append(rec);
905 return rec;
906 }
908 // Lookup inlining data for a ciMethod
909 ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
910 if (_ci_inline_records != NULL) {
911 return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
912 }
913 return NULL;
914 }
916 static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>* records,
917 Method* method, int bci, int depth) {
918 if (records != NULL) {
919 const char* klass_name = method->method_holder()->name()->as_utf8();
920 const char* method_name = method->name()->as_utf8();
921 const char* signature = method->signature()->as_utf8();
922 for (int i = 0; i < records->length(); i++) {
923 ciInlineRecord* rec = records->at(i);
924 if ((rec->_inline_bci == bci) &&
925 (rec->_inline_depth == depth) &&
926 (strcmp(rec->_klass_name, klass_name) == 0) &&
927 (strcmp(rec->_method_name, method_name) == 0) &&
928 (strcmp(rec->_signature, signature) == 0)) {
929 return rec;
930 }
931 }
932 }
933 return NULL;
934 }
936 const char* error_message() {
937 return _error_message;
938 }
940 void reset() {
941 _error_message = NULL;
942 _ci_method_records.clear();
943 _ci_method_data_records.clear();
944 }
946 // Take an ascii string contain \u#### escapes and convert it to utf8
947 // in place.
948 static void unescape_string(char* value) {
949 char* from = value;
950 char* to = value;
951 while (*from != '\0') {
952 if (*from != '\\') {
953 *from++ = *to++;
954 } else {
955 switch (from[1]) {
956 case 'u': {
957 from += 2;
958 jchar value=0;
959 for (int i=0; i<4; i++) {
960 char c = *from++;
961 switch (c) {
962 case '0': case '1': case '2': case '3': case '4':
963 case '5': case '6': case '7': case '8': case '9':
964 value = (value << 4) + c - '0';
965 break;
966 case 'a': case 'b': case 'c':
967 case 'd': case 'e': case 'f':
968 value = (value << 4) + 10 + c - 'a';
969 break;
970 case 'A': case 'B': case 'C':
971 case 'D': case 'E': case 'F':
972 value = (value << 4) + 10 + c - 'A';
973 break;
974 default:
975 ShouldNotReachHere();
976 }
977 }
978 UNICODE::convert_to_utf8(&value, 1, to);
979 to++;
980 break;
981 }
982 case 't': *to++ = '\t'; from += 2; break;
983 case 'n': *to++ = '\n'; from += 2; break;
984 case 'r': *to++ = '\r'; from += 2; break;
985 case 'f': *to++ = '\f'; from += 2; break;
986 default:
987 ShouldNotReachHere();
988 }
989 }
990 }
991 *from = *to;
992 }
993 };
995 void ciReplay::replay(TRAPS) {
996 int exit_code = replay_impl(THREAD);
998 Threads::destroy_vm();
1000 vm_exit(exit_code);
1001 }
1003 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1004 if (FLAG_IS_DEFAULT(InlineDataFile)) {
1005 tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1006 return NULL;
1007 }
1009 VM_ENTRY_MARK;
1010 // Load and parse the replay data
1011 CompileReplay rp(InlineDataFile, THREAD);
1012 if (!rp.can_replay()) {
1013 tty->print_cr("ciReplay: !rp.can_replay()");
1014 return NULL;
1015 }
1016 void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1017 if (HAS_PENDING_EXCEPTION) {
1018 oop throwable = PENDING_EXCEPTION;
1019 CLEAR_PENDING_EXCEPTION;
1020 java_lang_Throwable::print(throwable, tty);
1021 tty->cr();
1022 java_lang_Throwable::print_stack_trace(throwable, tty);
1023 tty->cr();
1024 return NULL;
1025 }
1027 if (rp.had_error()) {
1028 tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1029 return NULL;
1030 }
1031 return data;
1032 }
1034 int ciReplay::replay_impl(TRAPS) {
1035 HandleMark hm;
1036 ResourceMark rm;
1037 // Make sure we don't run with background compilation
1038 BackgroundCompilation = false;
1040 if (ReplaySuppressInitializers > 2) {
1041 // ReplaySuppressInitializers > 2 means that we want to allow
1042 // normal VM bootstrap but once we get into the replay itself
1043 // don't allow any intializers to be run.
1044 ReplaySuppressInitializers = 1;
1045 }
1047 if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1048 tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1049 return 1;
1050 }
1052 // Load and parse the replay data
1053 CompileReplay rp(ReplayDataFile, THREAD);
1054 int exit_code = 0;
1055 if (rp.can_replay()) {
1056 rp.process(THREAD);
1057 } else {
1058 exit_code = 1;
1059 return exit_code;
1060 }
1062 if (HAS_PENDING_EXCEPTION) {
1063 oop throwable = PENDING_EXCEPTION;
1064 CLEAR_PENDING_EXCEPTION;
1065 java_lang_Throwable::print(throwable, tty);
1066 tty->cr();
1067 java_lang_Throwable::print_stack_trace(throwable, tty);
1068 tty->cr();
1069 exit_code = 2;
1070 }
1072 if (rp.had_error()) {
1073 tty->print_cr("Failed on %s", rp.error_message());
1074 exit_code = 1;
1075 }
1076 return exit_code;
1077 }
1079 void ciReplay::initialize(ciMethodData* m) {
1080 if (replay_state == NULL) {
1081 return;
1082 }
1084 ASSERT_IN_VM;
1085 ResourceMark rm;
1087 Method* method = m->get_MethodData()->method();
1088 ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1089 if (rec == NULL) {
1090 // This indicates some mismatch with the original environment and
1091 // the replay environment though it's not always enough to
1092 // interfere with reproducing a bug
1093 tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1094 method->print_name(tty);
1095 tty->cr();
1096 } else {
1097 m->_state = rec->_state;
1098 m->_current_mileage = rec->_current_mileage;
1099 if (rec->_data_length != 0) {
1100 assert(m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1102 // Write the correct ciObjects back into the profile data
1103 ciEnv* env = ciEnv::current();
1104 for (int i = 0; i < rec->_oops_length; i++) {
1105 KlassHandle *h = (KlassHandle *)rec->_oops_handles[i];
1106 *(ciMetadata**)(rec->_data + rec->_oops_offsets[i]) =
1107 env->get_metadata((*h)());
1108 }
1109 // Copy the updated profile data into place as intptr_ts
1110 #ifdef _LP64
1111 Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1112 #else
1113 Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1114 #endif
1115 }
1117 // copy in the original header
1118 Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1119 }
1120 }
1123 bool ciReplay::should_not_inline(ciMethod* method) {
1124 if (replay_state == NULL) {
1125 return false;
1126 }
1127 VM_ENTRY_MARK;
1128 // ciMethod without a record shouldn't be inlined.
1129 return replay_state->find_ciMethodRecord(method->get_Method()) == NULL;
1130 }
1132 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1133 if (data != NULL) {
1134 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1135 VM_ENTRY_MARK;
1136 // Inline record are ordered by bci and depth.
1137 return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) != NULL;
1138 } else if (replay_state != NULL) {
1139 VM_ENTRY_MARK;
1140 // Inline record are ordered by bci and depth.
1141 return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) != NULL;
1142 }
1143 return false;
1144 }
1146 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1147 if (data != NULL) {
1148 GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1149 VM_ENTRY_MARK;
1150 // Inline record are ordered by bci and depth.
1151 return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == NULL;
1152 } else if (replay_state != NULL) {
1153 VM_ENTRY_MARK;
1154 // Inline record are ordered by bci and depth.
1155 return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == NULL;
1156 }
1157 return false;
1158 }
1160 void ciReplay::initialize(ciMethod* m) {
1161 if (replay_state == NULL) {
1162 return;
1163 }
1165 ASSERT_IN_VM;
1166 ResourceMark rm;
1168 Method* method = m->get_Method();
1169 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1170 if (rec == NULL) {
1171 // This indicates some mismatch with the original environment and
1172 // the replay environment though it's not always enough to
1173 // interfere with reproducing a bug
1174 tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1175 method->print_name(tty);
1176 tty->cr();
1177 } else {
1178 EXCEPTION_CONTEXT;
1179 // m->_instructions_size = rec->_instructions_size;
1180 m->_instructions_size = -1;
1181 m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1182 m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1183 MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1184 guarantee(mcs != NULL, "method counters allocation failed");
1185 mcs->invocation_counter()->_counter = rec->_invocation_counter;
1186 mcs->backedge_counter()->_counter = rec->_backedge_counter;
1187 }
1188 }
1190 bool ciReplay::is_loaded(Method* method) {
1191 if (replay_state == NULL) {
1192 return true;
1193 }
1195 ASSERT_IN_VM;
1196 ResourceMark rm;
1198 ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1199 return rec != NULL;
1200 }
1201 #endif // PRODUCT