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

3
  • This is similar to this question: stackoverflow.com/questions/1025681/call-trace-in-java Commented May 24, 2011 at 15:24
  • I thought Java logging tools did essentially this. Commented Jun 2, 2011 at 6:17
  • Yes but you have to insert manually all the log calls. Not a very viable option for a large project, is it? Commented Jun 30, 2011 at 14:50

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

I want to view the call sequence for a given period of time in the whole application, not just a call stack.
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.

2 Comments

How about the methods that were called and returned from? They won't be in the dump, right?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.