annotate src/share/vm/classfile/javaAssertions.cpp @ 973:ad6585fd4087

6830542: Performance: JVM_DefineClass already verified. Reviewed-by: kamg, phh
author acorn
date Fri, 04 Sep 2009 12:53:02 -0400
parents a61af66fc99e
children 4ce7240d622c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include "incls/_javaAssertions.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 bool JavaAssertions::_userDefault = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 bool JavaAssertions::_sysDefault = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 JavaAssertions::OptionList* JavaAssertions::_classes = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 JavaAssertions::OptionList* JavaAssertions::_packages = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 JavaAssertions::OptionList::OptionList(const char* name, bool enabled,
a61af66fc99e Initial load
duke
parents:
diff changeset
34 OptionList* next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 assert(name != 0, "need a name");
a61af66fc99e Initial load
duke
parents:
diff changeset
36 _name = name;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 _enabled = enabled;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 _next = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 }
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 int JavaAssertions::OptionList::count(OptionList* p) {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 int rc;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 for (rc = 0; p != 0; p = p->next(), ++rc) /* empty */;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 return rc;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 void JavaAssertions::addOption(const char* name, bool enable) {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 assert(name != 0, "must have a name");
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // Copy the name. The storage needs to exist for the the lifetime of the vm;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // it is never freed, so will be leaked (along with other option strings -
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // e.g., bootclasspath) if a process creates/destroys multiple VMs.
a61af66fc99e Initial load
duke
parents:
diff changeset
53 int len = (int)strlen(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
54 char *name_copy = NEW_C_HEAP_ARRAY(char, len + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
55 strcpy(name_copy, name);
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // Figure out which list the new item should go on. Names that end in "..."
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // go on the package tree list.
a61af66fc99e Initial load
duke
parents:
diff changeset
59 OptionList** head = &_classes;
a61af66fc99e Initial load
duke
parents:
diff changeset
60 if (len >= 3 && strcmp(name_copy + len - 3, "...") == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // Delete the "...".
a61af66fc99e Initial load
duke
parents:
diff changeset
62 len -= 3;
a61af66fc99e Initial load
duke
parents:
diff changeset
63 name_copy[len] = '\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
64 head = &_packages;
a61af66fc99e Initial load
duke
parents:
diff changeset
65 }
a61af66fc99e Initial load
duke
parents:
diff changeset
66
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // Convert class/package names to internal format. Will have to convert back
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // when copying to java in createJavaAssertionStatusDirectives, but that
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // should happen only once. Alternative would require that
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // JVM_DesiredAssertionStatus pass the external_name() to
a61af66fc99e Initial load
duke
parents:
diff changeset
71 // JavaAssertion::enabled(), but that is done once per loaded class.
a61af66fc99e Initial load
duke
parents:
diff changeset
72 for (int i = 0; i < len; ++i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 if (name_copy[i] == '.') name_copy[i] = '/';
a61af66fc99e Initial load
duke
parents:
diff changeset
74 }
a61af66fc99e Initial load
duke
parents:
diff changeset
75
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if (TraceJavaAssertions) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 tty->print_cr("JavaAssertions: adding %s %s=%d",
a61af66fc99e Initial load
duke
parents:
diff changeset
78 head == &_classes ? "class" : "package",
a61af66fc99e Initial load
duke
parents:
diff changeset
79 name_copy[0] != '\0' ? name_copy : "'default'",
a61af66fc99e Initial load
duke
parents:
diff changeset
80 enable);
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // Prepend a new item to the list. Items added later take precedence, so
a61af66fc99e Initial load
duke
parents:
diff changeset
84 // prepending allows us to stop searching the list after the first match.
a61af66fc99e Initial load
duke
parents:
diff changeset
85 *head = new OptionList(name_copy, enable, *head);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 oop JavaAssertions::createAssertionStatusDirectives(TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 symbolHandle asd_sym = vmSymbolHandles::java_lang_AssertionStatusDirectives();
a61af66fc99e Initial load
duke
parents:
diff changeset
90 klassOop k = SystemDictionary::resolve_or_fail(asd_sym, true, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 instanceKlassHandle asd_klass (THREAD, k);
a61af66fc99e Initial load
duke
parents:
diff changeset
92 asd_klass->initialize(CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
93 Handle h = asd_klass->allocate_instance_handle(CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 int len;
a61af66fc99e Initial load
duke
parents:
diff changeset
96 typeArrayOop t;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 len = OptionList::count(_packages);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 objArrayOop pn = oopFactory::new_objArray(SystemDictionary::string_klass(), len, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
99 objArrayHandle pkgNames (THREAD, pn);
a61af66fc99e Initial load
duke
parents:
diff changeset
100 t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
101 typeArrayHandle pkgEnabled(THREAD, t);
a61af66fc99e Initial load
duke
parents:
diff changeset
102 fillJavaArrays(_packages, len, pkgNames, pkgEnabled, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 len = OptionList::count(_classes);
a61af66fc99e Initial load
duke
parents:
diff changeset
105 objArrayOop cn = oopFactory::new_objArray(SystemDictionary::string_klass(), len, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
106 objArrayHandle classNames (THREAD, cn);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
108 typeArrayHandle classEnabled(THREAD, t);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 fillJavaArrays(_classes, len, classNames, classEnabled, CHECK_NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 java_lang_AssertionStatusDirectives::set_packages(h(), pkgNames());
a61af66fc99e Initial load
duke
parents:
diff changeset
112 java_lang_AssertionStatusDirectives::set_packageEnabled(h(), pkgEnabled());
a61af66fc99e Initial load
duke
parents:
diff changeset
113 java_lang_AssertionStatusDirectives::set_classes(h(), classNames());
a61af66fc99e Initial load
duke
parents:
diff changeset
114 java_lang_AssertionStatusDirectives::set_classEnabled(h(), classEnabled());
a61af66fc99e Initial load
duke
parents:
diff changeset
115 java_lang_AssertionStatusDirectives::set_deflt(h(), userClassDefault());
a61af66fc99e Initial load
duke
parents:
diff changeset
116 return h();
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 void JavaAssertions::fillJavaArrays(const OptionList* p, int len,
a61af66fc99e Initial load
duke
parents:
diff changeset
120 objArrayHandle names, typeArrayHandle enabled, TRAPS) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 // Fill in the parallel names and enabled (boolean) arrays. Start at the end
a61af66fc99e Initial load
duke
parents:
diff changeset
122 // of the array and work backwards, so the order of items in the arrays
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // matches the order on the command line (the list is in reverse order, since
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // it was created by prepending successive items from the command line).
a61af66fc99e Initial load
duke
parents:
diff changeset
125 int index;
a61af66fc99e Initial load
duke
parents:
diff changeset
126 for (index = len - 1; p != 0; p = p->next(), --index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 assert(index >= 0, "length does not match list");
a61af66fc99e Initial load
duke
parents:
diff changeset
128 Handle s = java_lang_String::create_from_str(p->name(), CHECK);
a61af66fc99e Initial load
duke
parents:
diff changeset
129 s = java_lang_String::char_converter(s, '/', '.', CHECK);
a61af66fc99e Initial load
duke
parents:
diff changeset
130 names->obj_at_put(index, s());
a61af66fc99e Initial load
duke
parents:
diff changeset
131 enabled->bool_at_put(index, p->enabled());
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133 assert(index == -1, "length does not match list");
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 inline JavaAssertions::OptionList*
a61af66fc99e Initial load
duke
parents:
diff changeset
137 JavaAssertions::match_class(const char* classname) {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 for (OptionList* p = _classes; p != 0; p = p->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 if (strcmp(p->name(), classname) == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
140 return p;
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142 }
a61af66fc99e Initial load
duke
parents:
diff changeset
143 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
144 }
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 JavaAssertions::OptionList*
a61af66fc99e Initial load
duke
parents:
diff changeset
147 JavaAssertions::match_package(const char* classname) {
a61af66fc99e Initial load
duke
parents:
diff changeset
148 // Search the package list for any items that apply to classname. Each
a61af66fc99e Initial load
duke
parents:
diff changeset
149 // sub-package in classname is checked, from most-specific to least, until one
a61af66fc99e Initial load
duke
parents:
diff changeset
150 // is found.
a61af66fc99e Initial load
duke
parents:
diff changeset
151 if (_packages == 0) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 // Find the length of the "most-specific" package in classname. If classname
a61af66fc99e Initial load
duke
parents:
diff changeset
154 // does not include a package, length will be 0 which will match items for the
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // default package (from options "-ea:..." or "-da:...").
a61af66fc99e Initial load
duke
parents:
diff changeset
156 size_t len = strlen(classname);
a61af66fc99e Initial load
duke
parents:
diff changeset
157 for (/* empty */; len > 0 && classname[len] != '/'; --len) /* empty */;
a61af66fc99e Initial load
duke
parents:
diff changeset
158
a61af66fc99e Initial load
duke
parents:
diff changeset
159 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
160 assert(len == 0 || classname[len] == '/', "not a package name");
a61af66fc99e Initial load
duke
parents:
diff changeset
161 for (OptionList* p = _packages; p != 0; p = p->next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 if (strncmp(p->name(), classname, len) == 0 && p->name()[len] == '\0') {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 return p;
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165 }
a61af66fc99e Initial load
duke
parents:
diff changeset
166
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // Find the length of the next package, taking care to avoid decrementing
a61af66fc99e Initial load
duke
parents:
diff changeset
168 // past 0 (len is unsigned).
a61af66fc99e Initial load
duke
parents:
diff changeset
169 while (len > 0 && classname[--len] != '/') /* empty */;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 } while (len > 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 inline void JavaAssertions::trace(const char* name,
a61af66fc99e Initial load
duke
parents:
diff changeset
176 const char* typefound, const char* namefound, bool enabled) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 if (TraceJavaAssertions) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 tty->print_cr("JavaAssertions: search for %s found %s %s=%d",
a61af66fc99e Initial load
duke
parents:
diff changeset
179 name, typefound, namefound[0] != '\0' ? namefound : "'default'", enabled);
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182
a61af66fc99e Initial load
duke
parents:
diff changeset
183 bool JavaAssertions::enabled(const char* classname, bool systemClass) {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 assert(classname != 0, "must have a classname");
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 // This will be slow if the number of assertion options on the command line is
a61af66fc99e Initial load
duke
parents:
diff changeset
187 // large--it traverses two lists, one of them multiple times. Could use a
a61af66fc99e Initial load
duke
parents:
diff changeset
188 // single n-ary tree instead of lists if someone ever notices.
a61af66fc99e Initial load
duke
parents:
diff changeset
189
a61af66fc99e Initial load
duke
parents:
diff changeset
190 // First check options that apply to classes. If we find a match we're done.
a61af66fc99e Initial load
duke
parents:
diff changeset
191 OptionList* p;
a61af66fc99e Initial load
duke
parents:
diff changeset
192 if (p = match_class(classname)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
193 trace(classname, "class", p->name(), p->enabled());
a61af66fc99e Initial load
duke
parents:
diff changeset
194 return p->enabled();
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // Now check packages, from most specific to least.
a61af66fc99e Initial load
duke
parents:
diff changeset
198 if (p = match_package(classname)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 trace(classname, "package", p->name(), p->enabled());
a61af66fc99e Initial load
duke
parents:
diff changeset
200 return p->enabled();
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // No match. Return the default status.
a61af66fc99e Initial load
duke
parents:
diff changeset
204 bool result = systemClass ? systemClassDefault() : userClassDefault();
a61af66fc99e Initial load
duke
parents:
diff changeset
205 trace(classname, systemClass ? "system" : "user", "default", result);
a61af66fc99e Initial load
duke
parents:
diff changeset
206 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 }