Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: printing current backtrace [duplicate]

is there a way to add a command in Java to add the current backtrace ?

I'm writing a red5 application and the appDisconnect function is being called twice. whenever a user changes room. I want to add a function at the beginning of the appDisconnect function that shows the current backtrace and then I can see what called it.

thanks

like image 878
ufk Avatar asked Dec 17 '22 17:12

ufk


2 Answers

You can output the stack trace to the current line like this:

new Exception().printStackTrace();

Or if you need programmattic acces the the stacktrace elements you can use

Thread.currentThread().getStackTrace()
like image 118
Jörn Horstmann Avatar answered Dec 30 '22 18:12

Jörn Horstmann


The only way I know of is to look at:

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();

Although this sounds more like a logging issue than actually observing the call trace. You could also try to setup debug execution in your IDE and add a few well placed breakpoints.

like image 33
Johan Sjöberg Avatar answered Dec 30 '22 19:12

Johan Sjöberg