Java call sequence analysis
I would like to be able to visualize the call sequence for a given JVM process (what methods were called on which objects and what parameters where passed). For example a tool that would dump this information to a file. Is there an existing tool to do this? If not could you give some pointers 开发者_高级运维on how this can be done? What solutions could you advise (besides modifying the bytecodes of the methods)?
OP says, "Java logging" no good. "Yes but you have to insert manually all the log calls. Not a very viable option for a large project, is it?".
OK, what you need is a way to insert custom instrumentation automatically into your application.
Our DMS Software Reengineering Toolkit with its Java Front End could be used to do this. DMS provides generic machinery to parse source code, build ASTs and symbol tables, analyze the code (trees) for special cases, and carry out transformations on the code(trees), finally regenerating modified code (from the modified trees). The Java Front end enables DMS to do this for Java; DMS has many other front end language modules.
What you'd want to do is to write a DMS source-to-source transformation to instrument the function entry with logic to dump the parameter list values (e.g., serialize their "toString" equivalents):
tag RecordArguments(a:arguments):statements;
instrument_function_entry rule(r:type,m:identifier,a:arguments,s:stmts):method->method
"\r \m(\a) { \s } "
->
"\r \m(\a) { Call.FunctionEntry(\stringify\(\m\));\RecordArguments\(\a\); { \s } }"
empty_arguments rule () arguments -> statements
"\RecordArguments\(\) " -> ";"
more_arguments rule (args:arguments,a:argument) arguments -> statements
"\RecordArguments\(\args,\a)"
-> "\RecordArguments\(\args\);Call.Argument(\a.toString()))"
What the "instrument_function_entry" rule does is place a call to record the function entry, and to generate a series of calls to record the argument values. The "empty_arguments" rule handles the base case of no-more-arguments to process (including "no arguments at all"). The "more_arguments" rule handle a list of arguments by picking off the last, generating code to dump that argument, and producing a shorter list of remaining argumetns to be processed by the same rule or eventually the "empty_arguments" rule.
What this should produce for the method:
int X3(char J, array[] X)
{ <code> }
will be
int X3(char J, array[] X)
{ Call.FunctionEntry("X3");
Call.Argument(J.toString());
Call.Argument(X.toString());
{ <code> }
}
You get to define the "Call" object however you like, to record the results. If you want to place extra filters in there to eliminate data from the wrong thread, or not between some time window or not near some interesting event, you can. This is likely to slow down you application a lot, at least if you let the instrumenter do this to every function call across your application. (More complex transformations can control where they are applied).
The best way I have looked at the call stack for Java has been through the Eclipse debugger. If you just place break points in your code you will be able to step through the code and look at the call stack.
I think the easiest thing is to use JPDA (Java Platform Debugger Architecture). You will have to suspend all threads, analyze, dump the info, and resume threads. It will not be trivial, but from the first look should be possible.
精彩评论