Generating stack traces

C

Chrisie

I am currently experimenting with policies and permissions in Java and
I am trying to generate a stack trace that will give me information
not only about the names of the methods that were called but also
about the CodeSource each method comes from and the certificates and
permissions it has. Is there a way to do that? I have tried using
Thread.dumpStack()
or
Throwable t=new Throwable();
t.printStackTrace();
but I haven't been able to find the information I want.
Furthermore, is there a way to get a stack trace from inside a library
method that I am using since I can't add a printStackTrace in there?
 
R

Roland Pibinger

I am currently experimenting with policies and permissions in Java and
I am trying to generate a stack trace that will give me information
not only about the names of the methods that were called but also
about the CodeSource each method comes from and the certificates and
permissions it has. Is there a way to do that? I have tried using
Thread.dumpStack()
or
Throwable t=new Throwable();
t.printStackTrace();
but I haven't been able to find the information I want.
Furthermore, is there a way to get a stack trace from inside a library
method that I am using since I can't add a printStackTrace in there?

The example here might be helpful:
http://forum.java.sun.com/thread.jspa?threadID=569768&messageID=2817422

Best wishes,
Roland Pibinger
 
B

Brandon McCombs

Chrisie said:
I am currently experimenting with policies and permissions in Java and
I am trying to generate a stack trace that will give me information
not only about the names of the methods that were called but also
about the CodeSource each method comes from and the certificates and
permissions it has. Is there a way to do that? I have tried using
Thread.dumpStack()
or
Throwable t=new Throwable();
t.printStackTrace();
but I haven't been able to find the information I want.
Furthermore, is there a way to get a stack trace from inside a library
method that I am using since I can't add a printStackTrace in there?

Maybe you should be running a debugger instead? I don't know if that
will give you permissions (never knew there were any) but it will give
you the other things you are looking for while they are happening. You
will need to set some breakpoints though to pause the flow.
 
J

Joshua Cranmer

Chrisie said:
I am currently experimenting with policies and permissions in Java and
I am trying to generate a stack trace that will give me information
not only about the names of the methods that were called but also
about the CodeSource each method comes from and the certificates and
permissions it has. Is there a way to do that? I have tried using
Thread.dumpStack()
or
Throwable t=new Throwable();
t.printStackTrace();
but I haven't been able to find the information I want.
Furthermore, is there a way to get a stack trace from inside a library
method that I am using since I can't add a printStackTrace in there?
If you want to get the CodeSource each method comes from, you should
parse the stackTrace and, for each Class,
Class.getProtectionDomain().getCodeSource(), e.g.:

StackTraceElement[] stack = Thread.currentThread().getStackTrace();
for (StackTraceElement e : stack) {
try {
System.err.println(e+" "+Class.forName(e.getClassName()).
getProtectionDomain().getCodeSource());
} catch (ClassNotFoundException e) {}
}

That code will print out each method (and its location in the file, to
boot) and then print out the code source. Ugly but it works.
 
C

Chrisie

Thanks a lot for your answers. I finally managed to get information on
the CodeSource and permissions of each class in the stack trace
through Class.getProtectionDomain() as Joshua suggested. Does anyone
have any ideas about the second thing I asked? Is it possible to get a
stack trace from inside a class to which I have no access? For example
if in my code I am calling the constructor of the FileReader class is
there a way to get a stack trace from inside this constructor? I hope
I'm not asking too much...
Chrisie
 
C

Chris Uppal

Chrisie said:
Is it possible to get a
stack trace from inside a class to which I have no access? For example
if in my code I am calling the constructor of the FileReader class is
there a way to get a stack trace from inside this constructor?

Not without using debugging level facilities.

-- chris
 
C

Chrisie

Chris said:
Not without using debugging level facilities.

-- chris

But even if I use a debugger I cannot step into the FileReader
constructor to generate the stack trace since it is a library class
and I'm not supposed to have any access to it. Or am I missing
something here?
Chrisie
 
D

Daniel Pitts

But even if I use a debugger I cannot step into the FileReader
constructor to generate the stack trace since it is a library class
and I'm not supposed to have any access to it. Or am I missing
something here?
Chrisie

Actually, I think the jar files provided in the JDK have enough debug
information that you can trace into them. The JDK does typically come
with a src.jar file, which contains the source code for the standard
API classes.

For example, I can trace into any java.util.* class with IntelliJ
IDEA... Haven't tried it with jdbg, but I suspect that its the same
case, as long as you set up your environment/classpath/sourcepath
correctly.
 
C

Chris Uppal

Chrisie said:
But even if I use a debugger I cannot step into the FileReader
constructor to generate the stack trace since it is a library class
and I'm not supposed to have any access to it.

It depnds on the debugger, but if you use the JRE which comes installed under
your JDK (which has debugging info included), and tell your debugger about the
source in src.zip (also in your JDK directory -- though you /may/ have to
unpack it first), then you should be able to step into system code.

I admit that I have never got this to work with Eclipse, but that is almost
certainly only because I dislike Eclipse so much that I nearly always give up
after only a few seconds rather than keep wrestling with its bizarely conceived
interface.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top