• Post Reply Bookmark Topic Watch Topic
  • New Topic

Call Stack Logging  RSS feed

 
Justin Lewis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible for me to write something that will log the call stack during execution of a java class ? I would like to be able to collect information about what methods are called, what classes are used, etc. during the run of an application. I would like to be able to do this without having to do it in each method. I was hoping there was something I could hook into that would get called each time a new method is called or whatever. Please let me know if you have any suggestions.

Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a good application of Aspect-Oriented Programming - e.g. see this article on logging with AspectJ. Or a simillar article here. Hope that helps...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is the advanced forum, so I'll toss out something that is frankly beyond me, but might be great fun for the right person. Any number of profiling and debugging tools use a custom class loader to insert bytecode into some or all methods (configurable) that logs entry and maybe exit. It's more or less AOP in hard-coded (virtual) machine code. I read some in-depth details on this in an article by the debugger team for one of the IDEs that isn't Eclipse, but darned if I can remember which one. I can't even think of a good Google search for this right now.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stan]: It's more or less AOP in hard-coded (virtual) machine code.

My understanding is that AspectJ can be brought in at compile time or at run time, depending how you set it up. Inserting the aspects directly into the bytecode is probably a good way to speed up execution compared to interpreted mode. So I think we're talking about some of the same things here, coming in from a few different directions. I wonder which current tools are most suitable for this sort of thing. Would be intested to hear from others who have done this, if any are lurking about.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cool thing about Wily Introscope (and Mercury Deep-Dive and a couple others) is they can dynamically do this aspect-like thing on any class as it is loaded or when it is just about to be executed. If AspectJ has runtime abilities, it might be just as capable. Introscope is dizzyingly expensive. I wish I could remember which IDE was going to have this dynamic bytecode modification built in as a debugging feature.

My bias is not to favor AOP right now, but I imagine that's largely from ignorance. If them that really use it recommend it, give it a good look.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AOP won't do it. It's already been done a long long time ago: http://www.lambdacs.com/debugger/debugger.html
It sucked enough (but not a lot relative to all else) such that I started rewriting it, but then I quit my job, and its relevance became negigible. Still, I maintain that if it were well-written, it would be a killer application for the big corporates solving their clients problems.
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time I read an article about AOP, this is trotted out as an example of what it could be used for. But I never could understand why somebody would want to log every method called in an application. Justin, would you mind saying why you want to do that?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Paul C]: But I never could understand why somebody would want to log every method called in an application.

I would note that AspectJ offers a bit more control than that, allowing you specify by package, class, method name, access level, and other modifiers (or combinations thereof) which methods to log. One could declare a pointcut to logg all public method calls in a given package, for example, to get a quick idea of how that package is getting used in code. If that view turns out to be less useful than imagined, then you can specify some other set of methods to be logged. The log entries could also be nested to show at a glance who called who:

One could also log initial parameters and return values. If the log files have too much info, or too little, or just not the right info, changing the pointcut definitions allows you to make significant changes to the logging being done, across many different packages, without having to touch those files. Presumably when performance is a concern it would be also quite easy to slow things down substantially with unnecessary calls, if not careful. But a tool like this would have some applications in debugging, I'd think.
[ July 26, 2006: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!