2

I am trying to get the stack trace of the underlying C code that is called from the android Java app code and tried using dumpStack() and printStackTrace() without any success. Just was curious is there a difference between the two if any?

Also is there a way to print a stack trace of the underlying C code the uses JNI to call the java functions on the android side?

Ducktales
  • 312
  • 4
  • 11
  • 1
    One difference is that you can call dumpStack() anytime, while printStackTrace() is from Throwable, which means you can call it only when you have a reference to a Throwable. – NESPowerGlove Jul 02 '14 at 21:48
  • Well I have been able to use Thread.currentThread().printStackTrace() without referencing Throwable and basically call it from anywhere. – Ducktales Jul 02 '14 at 23:02
  • 1
    No you haven't. There is no such method. There are Thread.dumpStack() and Throwable.printStackTrace(). – user207421 Jul 03 '14 at 01:38
  • http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java Instead of using get I was able to use print successfully and able to print out the stack trace on my Logcat – Ducktales Jul 03 '14 at 02:08
  • That was Thread.getStackTrace(). Not Thread.printStackTrace() as you stated above. – user207421 Jul 03 '14 at 02:23
  • Not via a Thread. The call Thread.currentThread().printStacktrace() does not compile. There is no such method. Period. Experience has nothing to do with it. Just see the Javadoc. And make up your mind about whether it was printStackTrace() or getStackTrace(). You seem to be claiming both. – user207421 Jul 03 '14 at 02:47
  • Oh well, my bad . I am printing the trace by converting it to a string. you are right – Ducktales Jul 03 '14 at 21:15

2 Answers2

7

Thread.dumpStack() dumps the current stack trace of the thread it is invoked on.

Throwable.printStackTrace() prints the stack trace as it was when the throwable was thrown.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Main difference between using dumpStack() and printStackTrace() is first entry in Stack, In case of dumpStack() first entry is always java.lang.Thread.dumpStack(), while in later case it's the method from where you printed stack trace. If you don't want to print stack trace and rather wants it in Java program, you can use getStackTrace() method from Thread class. This method returns an array of StackTraceElement. In following Java program we will see examples of all three approaches to get stack trace of current thread in Java.

josedlujan
  • 5,357
  • 2
  • 27
  • 49
  • 1
    No. The first line in Throwable.printStackTrace() is the line where the exception was thrown. It isn't necessarily a line in the method you called printStackTrace() from at all. – user207421 Jul 03 '14 at 03:46