Welcome to Dream.In.Code
Become a Java Expert!

Join 150,120 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,994 people online right now. Registration is fast and FREE... Join Now!




Generating a stack trace

 
Reply to this topicStart new topic

Generating a stack trace, Who called me?

NickDMax
26 Jun, 2008 - 07:59 PM
Post #1

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Does anyone know how to get the caller of a function?

For example log4j's logger will tell you on what line of the code a logging call was made. I am trying to add this functionality to our exception handling framework so we don't have to pass in out class name and method name.

I am off to see if I can spot how log4j does it.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Generating A Stack Trace
26 Jun, 2008 - 08:08 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Thread.dumpStack();

Too easy ... what more do you want NickdMax ? I guess I don't understand your question.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Generating A Stack Trace
26 Jun, 2008 - 08:12 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
I think what I was looking for was more:

(new Throwable()).getStackTrace();

I need to get the data into a useble format. I don't want it printed. I don't want to have to capture the System.out and see if I can find it.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Generating A Stack Trace
26 Jun, 2008 - 08:15 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
StackTraceElement[] ste = Thread.getStackTrace();

probably called by dumpStack()
that System.out.println() the elements in the array

StackTraceElement has methods:
- getMethodName()
- getLineNumber();
- ...

This post has been edited by pbl: 26 Jun, 2008 - 08:20 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Generating A Stack Trace
26 Jun, 2008 - 08:19 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
? odd I don't see a getStackTrace() function on thread... (that was what I thought one did originally -- if I had kept my mouth shut about that I would not be doing this) Is that in the later versions of Java?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Generating A Stack Trace
26 Jun, 2008 - 08:26 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(NickDMax @ 26 Jun, 2008 - 09:19 PM) *

? odd I don't see a getStackTrace() function on thread... (that was what I thought one did originally -- if I had kept my mouth shut about that I would not be doing this) Is that in the later versions of Java?

It is on 1.5 for sure... that is what I use


QUOTE(pbl @ 26 Jun, 2008 - 09:22 PM) *

QUOTE(NickDMax @ 26 Jun, 2008 - 09:19 PM) *

? odd I don't see a getStackTrace() function on thread... (that was what I thought one did originally -- if I had kept my mouth shut about that I would not be doing this) Is that in the later versions of Java?

It is on 1.5 for sure... that is what I use

Just checked... it is not in 1.4.2

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Generating A Stack Trace
26 Jun, 2008 - 08:32 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Yea, much to my dismay, "Enterprise Java == Java 1.4".

Oh they talk about how the new EJB 3.0 version of the product(s) will be awesome... but I have yet to work on any thing other than 1.4.

This project actually started out on 1.5, but there were integration problems with WAS 6.1 and so we had to roll back to WAS 6.0.2 and thus Java 1.4

sad.gif I think I found my answer. I knew there had to be a way to do it, but my architect thought you had to use a native method.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Generating A Stack Trace
26 Jun, 2008 - 08:35 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(NickDMax @ 26 Jun, 2008 - 09:32 PM) *

This project actually started out on 1.5, but there were integration problems with WAS 6.1 and so we had to roll back to WAS 6.0.2 and thus Java 1.4


I would have a look at the source code of 1.4.2 dumpStack() method

which calls Throwable.dumpStack()
which has a class StackTraceElement (as far as the comment says since 1.4)
which has a PRIVATE method getStrackTraceElement()

This post has been edited by pbl: 26 Jun, 2008 - 08:49 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Generating A Stack Trace
26 Jun, 2008 - 08:45 PM
Post #9

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Here is what I came up with:
CODE
public class StackTraceUtil {
    public static String whoCalledMe() {
        String retVal = "";
        java.lang.StackTraceElement[] trace = (new Throwable()).getStackTrace();
        if (trace.length >= 2) {
            retVal = trace[1].getClassName() + "." +
                trace[1].getMethodName()+"()";
        }
        
        return retVal;
    }
    
    public static void main(String[] args) {
        System.out.println(StackTraceUtil.whoCalledMe());
    }

}


Why did we need this: Exception handling in concurrent environments. We needed a centralized place to send exceptions so that they did not bubble up and kill vital threads. So there comes a point where you can't just "throw" the exception -- it has to be caught and logged or whatever. If a thread has to be killed it should be able to shutdown gracefully and not just die. etc.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:21AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month