How to disable stack trace generation in a Java program?

I want to disable the stack trace generated when an exception is thrown. I used

Runtime.getRuntime().traceInstructions(false); Runtime.getRuntime().traceMethodCalls(false); 

but still, I could see how the trace is generated. How can you do this? I also need to determine if anyone is debugging my class.

I want to disable all traces of exceptions. I can not use obfuscation, since my product is an SDK that will be used in development. I also suggest Runtime, which is used when people want to deploy their applications created using my SDK. My requirement is that anyone using my Runtime banners should not debug the code that is written ... or at least I will make it difficult to debug it by avoiding generating a stack trace from my runtime banners.

One of the ways I found is that all the exceptions that arise from my java files, I just caught them and set an empty StackTraceElement array for the exception object and threw it again ...

Why such a requirement? Suppose you have developed an application using my SDK. (SDK banks cannot be combined with your application .. I limited it, and this is final :)!) Now, to run your application on your client machine, you (or the client) need to set the Runtime on the client machine and launch the application. Now, what if your client starts developing their own applications using my Runtime jars !! This is a threat to my business .... That's why a terrible demand.

Why disable stack tracing?
By disabling stack trace generation or method invocation trace generation, I wanted to make the development code with my Runtime banks difficult and that is why I started my question this way ... I propose another solution to achieve this requirement ...

+6
java debugging stack-trace
source share
5 answers

There are several difficult parts of the JVM (at least the Sun JVM implementation) that do not work if stack trace generation is disabled (I saw this when implementing some support methods for reflection). Therefore, I do not think that stack trace generation can be disabled at all. Runtime.trace*() methods relate to something else (a debugging tool is much more thorough than stack traces).

In general, any Java code can be transparently analyzed, if only with the help of bytecode tools (the bytecode is changed with additional instructions on loading). The only known defense against such analysis (I assume that you are trying to maintain the internal confidentiality of your code) is confusion. See for example ProGuard . Obfuscation will make stack traces useless for any overly curious user (and, unfortunately, this also makes debugging very difficult, for the same reasons).

+2
source share

I am also wondering why you want to do this, but if you really have your own reasons, you have at least two options:

If you want to disable stack trace generation for your own Exception implementations, you can simply override the fillInStackTrace method:

 public static class MyException extends Exception { @Override public Throwable fillInStackTrace() { return null; } } 

If you want to disable it for all exceptions, you can use the byte code toolkit agent to replace the fillInStackTrace method in the Throwable class. This, however, will only work for Java 6, since in Java 5 you are not allowed to replace your own method (fillInStackTrace) with a Java method using tools.

+8
source share
  • I don’t think that the code can know that it is being debugged, with the exception of indirect (and unreliable) means, for example, how to measure how much time it takes to execute code sequences.

  • Unable to disable all stacks. You can disable stracktraces for exception classes that you define yourself by overriding Throwable.fillInStackTrace() to do nothing. But this will not work for classes that you cannot change.

But if you are thinking of doing these things to prevent reverse engineering, you would have wasted your time, even if you could have done it. It would be easy for a hacker to identify your application code for reverse conversion and edit the corresponding bytecode files to disable it.

EDIT . I reconsidered what you are trying to do. Given what you are doing, it is distributing the SDK that you expect from your customers to embed in your own applications, disabling stacktraces for the entire Java application, is considered unfriendly client behavior, IMO. As a side effect of protecting your β€œprecious” IP address, you make it difficult for him / her to debug your own code. Even code that does not have your valuable methods in the call stack!

If I were a client, I would prefer that you send the running code than it did. But, most likely, I would try VERY HARD to find an alternative software provider who would not consider their payment customers as thieves.

+5
source share

Do you want to disable it for all exceptions?

Not knowing what you are trying to achieve, I would say that this is the wrong way. If you expect an exception to be thrown that you are happy to ignore, you must explicitly catch it and handle it (where processing can simply mean ignoring it or logging a short message without a full stack trace).

+1
source share

Throwable.setStackTrace (StackTraceElement [] stackTrace) blocks any additions to stackTrace after it is called:

 Throwable t = new Throwable(); StackTraceElement[] myStackTrace = new StackTraceElement[] { new StackTraceElement("MySDKClass","MySDKMethod","MySDKFile",0) }; t.setStackTrace(trace); 
0
source share

All Articles