Mercurial > hg > graal-compiler
annotate src/share/vm/utilities/exceptions.cpp @ 1412:9195b99c841b
Added first VM entry method.
TODO: Check why initialization method of compiler is not called. Register native methods only once.
author | Thomas Wuerthinger <thomas.wuerthinger@gmail.com> |
---|---|
date | Mon, 17 May 2010 16:37:23 +0200 |
parents | b30a2cd5e3a2 |
children | 2d26b0046e0d |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright 1998-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/_exceptions.cpp.incl" | |
27 | |
28 | |
29 // Implementation of ThreadShadow | |
30 void check_ThreadShadow() { | |
31 const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception); | |
32 const ByteSize offset2 = Thread::pending_exception_offset(); | |
33 if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly"); | |
34 } | |
35 | |
36 | |
37 void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) { | |
38 assert(exception != NULL && exception->is_oop(), "invalid exception oop"); | |
39 _pending_exception = exception; | |
40 _exception_file = file; | |
41 _exception_line = line; | |
42 } | |
43 | |
44 void ThreadShadow::clear_pending_exception() { | |
45 if (TraceClearedExceptions) { | |
46 if (_pending_exception != NULL) { | |
47 tty->print_cr("Thread::clear_pending_exception: cleared exception:"); | |
48 _pending_exception->print(); | |
49 } | |
50 } | |
51 _pending_exception = NULL; | |
52 _exception_file = NULL; | |
53 _exception_line = 0; | |
54 } | |
55 // Implementation of Exceptions | |
56 | |
57 bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) { | |
58 // bootstrapping check | |
59 if (!Universe::is_fully_initialized()) { | |
60 vm_exit_during_initialization(h_exception); | |
61 ShouldNotReachHere(); | |
62 } | |
63 | |
64 if (thread->is_VM_thread() | |
1410
b30a2cd5e3a2
Added methods to c1x_VMExits.cpp
Thomas Wuerthinger <thomas.wuerthinger@gmail.com>
parents:
1142
diff
changeset
|
65 // (tw) May we do this? |
b30a2cd5e3a2
Added methods to c1x_VMExits.cpp
Thomas Wuerthinger <thomas.wuerthinger@gmail.com>
parents:
1142
diff
changeset
|
66 /*|| thread->is_Compiler_thread()*/ ) { |
0 | 67 // We do not care what kind of exception we get for the vm-thread or a thread which |
68 // is compiling. We just install a dummy exception object | |
69 thread->set_pending_exception(Universe::vm_exception(), file, line); | |
70 return true; | |
71 } | |
72 | |
73 return false; | |
74 } | |
75 | |
76 bool Exceptions::special_exception(Thread* thread, const char* file, int line, symbolHandle h_name, const char* message) { | |
77 // bootstrapping check | |
78 if (!Universe::is_fully_initialized()) { | |
79 if (h_name.is_null()) { | |
80 // atleast an informative message. | |
81 vm_exit_during_initialization("Exception", message); | |
82 } else { | |
83 vm_exit_during_initialization(h_name, message); | |
84 } | |
85 ShouldNotReachHere(); | |
86 } | |
87 | |
88 if (thread->is_VM_thread() | |
1410
b30a2cd5e3a2
Added methods to c1x_VMExits.cpp
Thomas Wuerthinger <thomas.wuerthinger@gmail.com>
parents:
1142
diff
changeset
|
89 // (tw) May we do this? |
b30a2cd5e3a2
Added methods to c1x_VMExits.cpp
Thomas Wuerthinger <thomas.wuerthinger@gmail.com>
parents:
1142
diff
changeset
|
90 /* || thread->is_Compiler_thread()*/ ) { |
0 | 91 // We do not care what kind of exception we get for the vm-thread or a thread which |
92 // is compiling. We just install a dummy exception object | |
93 thread->set_pending_exception(Universe::vm_exception(), file, line); | |
94 return true; | |
95 } | |
96 | |
97 return false; | |
98 } | |
99 | |
100 // This method should only be called from generated code, | |
101 // therefore the exception oop should be in the oopmap. | |
102 void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) { | |
103 assert(exception != NULL, "exception should not be NULL"); | |
104 Handle h_exception = Handle(thread, exception); | |
105 _throw(thread, file, line, h_exception); | |
106 } | |
107 | |
1011
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
108 void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) { |
0 | 109 assert(h_exception() != NULL, "exception should not be NULL"); |
110 | |
111 // tracing (do this up front - so it works during boot strapping) | |
112 if (TraceExceptions) { | |
113 ttyLocker ttyl; | |
114 ResourceMark rm; | |
1011
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
115 tty->print_cr("Exception <%s>%s%s (" INTPTR_FORMAT " ) \n" |
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
116 "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT, |
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
117 h_exception->print_value_string(), |
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
118 message ? ": " : "", message ? message : "", |
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
119 (address)h_exception(), file, line, thread); |
0 | 120 } |
121 // for AbortVMOnException flag | |
122 NOT_PRODUCT(Exceptions::debug_check_abort(h_exception)); | |
123 | |
124 // Check for special boot-strapping/vm-thread handling | |
125 if (special_exception(thread, file, line, h_exception)) return; | |
126 | |
1142 | 127 assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable"); |
0 | 128 |
129 // set the pending exception | |
130 thread->set_pending_exception(h_exception(), file, line); | |
131 | |
132 // vm log | |
133 Events::log("throw_exception " INTPTR_FORMAT, (address)h_exception()); | |
134 } | |
135 | |
136 | |
137 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, symbolHandle h_name, const char* message, Handle h_loader, Handle h_protection_domain) { | |
138 // Check for special boot-strapping/vm-thread handling | |
139 if (special_exception(thread, file, line, h_name, message)) return; | |
140 // Create and throw exception | |
141 Handle h_cause(thread, NULL); | |
142 Handle h_exception = new_exception(thread, h_name, message, h_cause, h_loader, h_protection_domain); | |
1011
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
143 _throw(thread, file, line, h_exception, message); |
0 | 144 } |
145 | |
146 // Throw an exception with a message and a cause | |
147 void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, symbolHandle h_name, const char* message, Handle h_cause, Handle h_loader, Handle h_protection_domain) { | |
148 // Check for special boot-strapping/vm-thread handling | |
149 if (special_exception(thread, file, line, h_name, message)) return; | |
150 // Create and throw exception and init cause | |
151 Handle h_exception = new_exception(thread, h_name, message, h_cause, h_loader, h_protection_domain); | |
1011
fcb148c6b605
6889302: TraceExceptions output should include detail message
never
parents:
0
diff
changeset
|
152 _throw(thread, file, line, h_exception, message); |
0 | 153 } |
154 | |
155 // This version creates handles and calls the other version | |
156 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, | |
157 symbolOop name, const char* message) { | |
158 symbolHandle h_name(thread, name); | |
159 Handle h_loader(thread, NULL); | |
160 Handle h_protection_domain(thread, NULL); | |
161 Exceptions::_throw_msg(thread, file, line, h_name, message, h_loader, h_protection_domain); | |
162 } | |
163 | |
164 // This version already has a handle for name | |
165 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, | |
166 symbolHandle name, const char* message) { | |
167 Handle h_loader(thread, NULL); | |
168 Handle h_protection_domain(thread, NULL); | |
169 Exceptions::_throw_msg(thread, file, line, name, message, h_loader, h_protection_domain); | |
170 } | |
171 | |
172 // This version already has a handle for name | |
173 void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, | |
174 symbolHandle name, const char* message, Handle cause) { | |
175 Handle h_loader(thread, NULL); | |
176 Handle h_protection_domain(thread, NULL); | |
177 Exceptions::_throw_msg_cause(thread, file, line, name, message, cause, h_loader, h_protection_domain); | |
178 } | |
179 | |
180 void Exceptions::_throw_args(Thread* thread, const char* file, int line, symbolHandle h_name, symbolHandle h_signature, JavaCallArguments *args) { | |
181 // Check for special boot-strapping/vm-thread handling | |
182 if (special_exception(thread, file, line, h_name, NULL)) return; | |
183 // Create and throw exception | |
184 Handle h_loader(thread, NULL); | |
185 Handle h_prot(thread, NULL); | |
186 Handle h_cause(thread, NULL); | |
187 Handle exception = new_exception(thread, h_name, h_signature, args, h_cause, h_loader, h_prot); | |
188 _throw(thread, file, line, exception); | |
189 } | |
190 | |
191 | |
192 void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line) { | |
193 Handle exception; | |
194 if (!THREAD->has_pending_exception()) { | |
195 klassOop k = SystemDictionary::StackOverflowError_klass(); | |
196 oop e = instanceKlass::cast(k)->allocate_instance(CHECK); | |
197 exception = Handle(THREAD, e); // fill_in_stack trace does gc | |
198 if (StackTraceInThrowable) { | |
199 java_lang_Throwable::fill_in_stack_trace(exception); | |
200 } | |
201 } else { | |
202 // if prior exception, throw that one instead | |
203 exception = Handle(THREAD, THREAD->pending_exception()); | |
204 } | |
205 _throw_oop(THREAD, file, line, exception()); | |
206 } | |
207 | |
208 void Exceptions::fthrow(Thread* thread, const char* file, int line, symbolHandle h_name, const char* format, ...) { | |
209 const int max_msg_size = 1024; | |
210 va_list ap; | |
211 va_start(ap, format); | |
212 char msg[max_msg_size]; | |
213 vsnprintf(msg, max_msg_size, format, ap); | |
214 msg[max_msg_size-1] = '\0'; | |
215 va_end(ap); | |
216 _throw_msg(thread, file, line, h_name, msg); | |
217 } | |
218 | |
219 // Creates an exception oop, calls the <init> method with the given signature. | |
220 // and returns a Handle | |
221 // Initializes the cause if cause non-null | |
222 Handle Exceptions::new_exception(Thread *thread, symbolHandle h_name, | |
223 symbolHandle signature, | |
224 JavaCallArguments *args, | |
225 Handle h_cause, Handle h_loader, | |
226 Handle h_protection_domain) { | |
227 assert(Universe::is_fully_initialized(), | |
228 "cannot be called during initialization"); | |
229 assert(thread->is_Java_thread(), "can only be called by a Java thread"); | |
230 assert(!thread->has_pending_exception(), "already has exception"); | |
231 | |
232 Handle h_exception; | |
233 | |
234 // Resolve exception klass | |
235 klassOop ik = SystemDictionary::resolve_or_fail(h_name, h_loader, h_protection_domain, true, thread); | |
236 instanceKlassHandle klass (thread, ik); | |
237 | |
238 if (!thread->has_pending_exception()) { | |
239 assert(klass.not_null(), "klass must exist"); | |
240 // We are about to create an instance - so make sure that klass is initialized | |
241 klass->initialize(thread); | |
242 if (!thread->has_pending_exception()) { | |
243 // Allocate new exception | |
244 h_exception = klass->allocate_instance_handle(thread); | |
245 if (!thread->has_pending_exception()) { | |
246 JavaValue result(T_VOID); | |
247 args->set_receiver(h_exception); | |
248 // Call constructor | |
249 JavaCalls::call_special(&result, klass, | |
250 vmSymbolHandles::object_initializer_name(), | |
251 signature, | |
252 args, | |
253 thread); | |
254 | |
255 } | |
256 } | |
257 | |
258 // Future: object initializer should take a cause argument | |
259 if (h_cause() != NULL) { | |
1142 | 260 assert(h_cause->is_a(SystemDictionary::Throwable_klass()), |
0 | 261 "exception cause is not a subclass of java/lang/Throwable"); |
262 JavaValue result1(T_OBJECT); | |
263 JavaCallArguments args1; | |
264 args1.set_receiver(h_exception); | |
265 args1.push_oop(h_cause); | |
266 JavaCalls::call_virtual(&result1, klass, | |
267 vmSymbolHandles::initCause_name(), | |
268 vmSymbolHandles::throwable_throwable_signature(), | |
269 &args1, | |
270 thread); | |
271 } | |
272 } | |
273 | |
274 // Check if another exception was thrown in the process, if so rethrow that one | |
275 if (thread->has_pending_exception()) { | |
276 h_exception = Handle(thread, thread->pending_exception()); | |
277 thread->clear_pending_exception(); | |
278 } | |
279 return h_exception; | |
280 } | |
281 | |
282 // Convenience method. Calls either the <init>() or <init>(String) method when | |
283 // creating a new exception | |
284 Handle Exceptions::new_exception(Thread* thread, symbolHandle h_name, | |
285 const char* message, Handle h_cause, | |
286 Handle h_loader, | |
287 Handle h_protection_domain, | |
288 ExceptionMsgToUtf8Mode to_utf8_safe) { | |
289 JavaCallArguments args; | |
290 symbolHandle signature; | |
291 if (message == NULL) { | |
292 signature = vmSymbolHandles::void_method_signature(); | |
293 } else { | |
294 // We want to allocate storage, but we can't do that if there's | |
295 // a pending exception, so we preserve any pending exception | |
296 // around the allocation. | |
297 // If we get an exception from the allocation, prefer that to | |
298 // the exception we are trying to build, or the pending exception. | |
299 // This is sort of like what PRESERVE_EXCEPTION_MARK does, except | |
300 // for the preferencing and the early returns. | |
301 Handle incoming_exception (thread, NULL); | |
302 if (thread->has_pending_exception()) { | |
303 incoming_exception = Handle(thread, thread->pending_exception()); | |
304 thread->clear_pending_exception(); | |
305 } | |
306 Handle msg; | |
307 if (to_utf8_safe == safe_to_utf8) { | |
308 // Make a java UTF8 string. | |
309 msg = java_lang_String::create_from_str(message, thread); | |
310 } else { | |
311 // Make a java string keeping the encoding scheme of the original string. | |
312 msg = java_lang_String::create_from_platform_dependent_str(message, thread); | |
313 } | |
314 if (thread->has_pending_exception()) { | |
315 Handle exception(thread, thread->pending_exception()); | |
316 thread->clear_pending_exception(); | |
317 return exception; | |
318 } | |
319 if (incoming_exception.not_null()) { | |
320 return incoming_exception; | |
321 } | |
322 args.push_oop(msg); | |
323 signature = vmSymbolHandles::string_void_signature(); | |
324 } | |
325 return new_exception(thread, h_name, signature, &args, h_cause, h_loader, h_protection_domain); | |
326 } | |
327 | |
328 // Another convenience method that creates handles for null class loaders and | |
329 // protection domains and null causes. | |
330 // If the last parameter 'to_utf8_mode' is safe_to_utf8, | |
331 // it means we can safely ignore the encoding scheme of the message string and | |
332 // convert it directly to a java UTF8 string. Otherwise, we need to take the | |
333 // encoding scheme of the string into account. One thing we should do at some | |
334 // point is to push this flag down to class java_lang_String since other | |
335 // classes may need similar functionalities. | |
336 Handle Exceptions::new_exception(Thread* thread, | |
337 symbolOop name, | |
338 const char* message, | |
339 ExceptionMsgToUtf8Mode to_utf8_safe) { | |
340 | |
341 symbolHandle h_name(thread, name); | |
342 Handle h_loader(thread, NULL); | |
343 Handle h_prot(thread, NULL); | |
344 Handle h_cause(thread, NULL); | |
345 return Exceptions::new_exception(thread, h_name, message, h_cause, h_loader, | |
346 h_prot, to_utf8_safe); | |
347 } | |
348 | |
349 // Implementation of ExceptionMark | |
350 | |
351 ExceptionMark::ExceptionMark(Thread*& thread) { | |
352 thread = Thread::current(); | |
353 _thread = thread; | |
354 if (_thread->has_pending_exception()) { | |
355 oop exception = _thread->pending_exception(); | |
356 _thread->clear_pending_exception(); // Needed to avoid infinite recursion | |
357 exception->print(); | |
358 fatal("ExceptionMark constructor expects no pending exceptions"); | |
359 } | |
360 } | |
361 | |
362 | |
363 ExceptionMark::~ExceptionMark() { | |
364 if (_thread->has_pending_exception()) { | |
365 Handle exception(_thread, _thread->pending_exception()); | |
366 _thread->clear_pending_exception(); // Needed to avoid infinite recursion | |
367 if (is_init_completed()) { | |
368 exception->print(); | |
369 fatal("ExceptionMark destructor expects no pending exceptions"); | |
370 } else { | |
371 vm_exit_during_initialization(exception); | |
372 } | |
373 } | |
374 } | |
375 | |
376 // ---------------------------------------------------------------------------------------- | |
377 | |
378 #ifndef PRODUCT | |
379 // caller frees value_string if necessary | |
380 void Exceptions::debug_check_abort(const char *value_string) { | |
381 if (AbortVMOnException != NULL && value_string != NULL && | |
382 strstr(value_string, AbortVMOnException)) { | |
383 fatal1("Saw %s, aborting", value_string); | |
384 } | |
385 } | |
386 | |
387 void Exceptions::debug_check_abort(Handle exception) { | |
388 if (AbortVMOnException != NULL) { | |
389 ResourceMark rm; | |
390 debug_check_abort(instanceKlass::cast(exception()->klass())->external_name()); | |
391 } | |
392 } | |
393 #endif |