What does this have to do with Java? I thought you'd never ask. The JVM is a multi-threaded process and so under the covers it's using signals to do OS level threading. But the JVM is also doing a metric ton of other really clever stuff; for example in a regular C/C++ program hitting a NULL (a Zero) when you're expecting a pointer to some structure would cause your application to crash. That crash is actually, as you can probably guess by now, the OS sending your process a signal - specifically SIGSEGV. If your app didn't register a signal handler for that signal (and 99.5% of c/c++ apps out there don't) then the signal comes back up to the OS which then terminates the app and (usually) saves the memory state into a core file. The JVM does register a signal handler for SIGSEGV and not just because it doesn't want to crash out when something goes wrong. The JVM registers a signal handler for SIGSEGV because it actually uses SIGSEGV and a bunch of other signals for its own purposes. What purpose?
Signal | Description |
---|---|
SIGSEGV, SIGBUS, SIGFPE, SIGPIPE, SIGILL | Used in the implementation for implicit null check, and so forth. |
SIGQUIT | Thread dump support: To dump Java stack traces at the standard error stream. (Optional.) |
SIGTERM, SIGINT, SIGHUP | Used to support the shutdown hook mechanism (java.lang.Runtime.addShutdownHook) when the VM is terminated abnormally. (Optional.) |
SIGUSR1 | Used in the implementation of the java.lang.Thread.interrupt method. (Configurable.) Not used starting with Solaris 10 OS. Reserved on Linux. |
SIGUSR2 | Used internally. (Configurable.) Not used starting with Solaris 10 OS. |
SIGABRT | The HotSpot VM does not handle this signal. Instead it calls the abort function after fatal error handling. If an application uses this signal then it should terminate the process to preserve the expected semantics. |
try { ... } catch ( Exception e ) { ... }And the catch block executes because of a NullPointerException what's actually happening is that a SIGSEGV shows up and the JVM is handling it. And that's perfectly normal and completely safe. Cribbing from that page on the JVM again:
In general there are two categories of situations where signal/traps arise.I don't recommend running strace against a Java process, but if you do do that it's almost certainly safe to ignore any signals you see, including SIGSEGV.
- Situations in which signals are expected and handled. Examples include the implicit null handling cited above. Another example is the safepoint polling mechanism, which protects a page in memory when a safepoint is required. Any thread that accesses that page causes a SIGSEGV, which results in the execution of a stub that brings the thread to a safepoint.
- Unexpected signals. This includes a SIGSEGV when executing in VM code, JNI code, or native code. In these cases the signal is unexpected, so fatal error handling is invoked to create the error log and terminate the process.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.