Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Generating stack traces

Thread view: 
Chrisie - 10 Mar 2007 15:19 GMT
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?
Roland Pibinger - 10 Mar 2007 17:55 GMT
>I am currently experimenting with policies and permissions in Java and
>I am trying to generate a stack trace that will give me information
[quoted text clipped - 8 lines]
>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
Brandon McCombs - 10 Mar 2007 18:00 GMT
> I am currently experimenting with policies and permissions in Java and
> I am trying to generate a stack trace that will give me information
[quoted text clipped - 8 lines]
> 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.
Joshua Cranmer - 10 Mar 2007 18:35 GMT
> I am currently experimenting with policies and permissions in Java and
> I am trying to generate a stack trace that will give me information
[quoted text clipped - 8 lines]
> 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.
Chrisie - 11 Mar 2007 13:22 GMT
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
Chris Uppal - 11 Mar 2007 17:03 GMT
> 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
Chrisie - 11 Mar 2007 18:42 GMT
> 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
Daniel Pitts - 12 Mar 2007 04:03 GMT
> > Not without using debugging level facilities.
>
[quoted text clipped - 5 lines]
> 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.
Chris Uppal - 12 Mar 2007 19:43 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.