NullPointerException stack trace not available without debug agent

前端 未结 3 480
北荒
北荒 2020-11-29 20:46

I have recently found a bug that causes a NullPointerException. The exception is caught and logged using a standard slf4j statement. Abridged code below:

for         


        
相关标签:
3条回答
  • 2020-11-29 21:24

    With the JVM flag -XX:-OmitStackTraceInFastThrow you can disable the performance optimization of the JVM for this use case. IF this parameter is given, which disables the flag, the stacktrace will be available.

    For more information please have a look at the following release notes:

    "The compiler in the server VM now provides correct stack backtraces for all "cold" built-in exceptions. For performance purposes, when such an exception is thrown a few times, the method may be recompiled. After recompilation, the compiler may choose a faster tactic using preallocated exceptions that do not provide a stack trace. To disable completely the use of preallocated exceptions, use this new flag: -XX:-OmitStackTraceInFastThrow." http://java.sun.com/j2se/1.5.0/relnotes.html

    0 讨论(0)
  • 2020-11-29 21:30

    I can replicate this but it seems kind of weird that this would be happening in your Action somewhere.

    If you call setStackTrace with an empty array, that'll cause only the text to be shown.

     public class Fark {
       public static void main(String[] args) {
           try {
               Fark.throwMe(args.length != 0);
    
           }
           catch (Exception e) {
               e.printStackTrace();
           }
    
       }
    
         public static final void throwMe(boolean arg) throws Exception{
             Exception e = new NullPointerException();
             if (arg) {
               e.setStackTrace(new StackTraceElement[0]);
             }
             throw e;
         }
     }
    

    Running it....

    % java Fark
    java.lang.NullPointerException
            at Fark.throwMe(Fark.java:15)
            at Fark.main(Fark.java:5)
    
    % java Fark nothing
    java.lang.NullPointerException
    
    0 讨论(0)
  • 2020-11-29 21:39

    Is it possible that this code is in an inner loop? Then then JIT compiler might be compiling the call stack for this to native code, losing the stack information. Then when you attach the debugger it disables the JIT, making the information available again.

    The other manual exceptions keep displaying the information as the JIT is not optimising.

    It looks like this can sometimes happen for others from a comment in this class source code at line 102:

    http://logging.apache.org/log4j/1.2/xref/org/apache/log4j/spi/LocationInfo.html

    0 讨论(0)
提交回复
热议问题