2

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)?

Eugen
  • 2,292
  • 3
  • 29
  • 43

3 Answers3

1

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).

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
0

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.

J Lundberg
  • 2,313
  • 2
  • 20
  • 23
  • I want to view the call sequence for a given period of time in the whole application, not just a call stack. – Eugen May 24 '11 at 12:48
0

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.

aav
  • 2,514
  • 1
  • 19
  • 27
  • How about the methods that were called and returned from? They won't be in the dump, right? – Eugen May 24 '11 at 12:48
  • thread execution is a very dynamic thing, and of course, you will see a snapshot, if you follow my suggestion. you may try making several such snapshots, but still it will not provide you with 100% of coverage. I'm afraid, that you will not be able to avoid byte code manipulation, if you want exact details. you can tale a look on some open source profiling and/or code coverage analysis tools to get an idea. – aav May 24 '11 at 13:02