2

Suppose I have a Java server application, which I don't understand. I would like to know the whole execution sequence of the request processing (see example below)

received HttpRerquest 12345 from client (...) with headers (...) and body (...)
enter WebUserController with HttpRequest 12345
enter UserProcessor.process1 with HttpRequest 12345
enter UserProcessor.process2 with createUserRequest(userName=..., userEmail=..., ...)
enter userDAO.createUser with User(userName=..., userEmail=..., ...)
invoke SQL statement (...) against database (...) in <host>::<port>
exit userDAO.createUser with result (...)
exit UserProcessor.process2 with (...)
exit UserProcessor.process1 with (...)
exit WebUserController with ...
sent HttpResponse 7890 to client (...) with headers (...) and body (...)

I am aware about only one way to get it: add trace calls to the application code manually. Now I wonder if there is an automatic way to add those trace calls to the code. I am thinking about byte instrumentation, which would add the trace calls to all public methods of all public classes in given jars. Does it make sense ?

Could you suggest any other way to understand how the application works?

Michael
  • 39,175
  • 70
  • 183
  • 319
  • 1
    You definitely should check out aspect-oriented programming (http://en.wikipedia.org/wiki/Aspect-oriented_programming). – Smutje Feb 18 '14 at 07:02
  • @Smutje - should you add the aspects by hand also? – Olimpiu POP Feb 18 '14 at 07:07
  • I don't know about such tool you are asking, but can't you use loggers ? May be there are already loggers at the key points in the app, and you just have to increase the logging level, to see their messages? – Svetlin Zarev Feb 18 '14 at 07:09
  • 1
    As far as I am concerned, if you can identify the AOP pointcuts automatically (i.e., every method entry and exit point), you don't have to do this by hand and rather automate it (additional benefit: you don't have to pollute your business code with pointcut definitions). I quickly found an article about AOP and Spring: http://marxsoftware.blogspot.de/2008/10/logging-method-entryexit-with.html – Smutje Feb 18 '14 at 07:10
  • @SvetlinZarev Suppose I do not have enough logging in the application. – Michael Feb 18 '14 at 07:10
  • 1
    @Michael what you're saying makes sense, what tool would you use to accomplish the instrumentation of all public methods automatically? I know this provides some overview at runtime: https://kenai.com/projects/btrace/pages/Home – Olimpiu POP Feb 18 '14 at 07:11
  • @user503413 Unfortunately I do not know any tool, which would do that. – Michael Feb 18 '14 at 07:21
  • 1
    @Michael As stated by Smutje, AOP can be the solution and I think this might be what you are looking for: http://stackoverflow.com/questions/8839077/how-to-use-aop-with-aspectj-for-logging (the name of the user that had the question was Michael also :P). Please share if the solution works – Olimpiu POP Feb 18 '14 at 07:29

2 Answers2

1

If it is a development server, you can also try attaching your IDE to the debug port of the server. That way you can follow the code line by line from where you set the breakpoint.

This is definitely a no-go if you're talking about acceptance or production servers, since you're blocking all other threads from executing the code in question.

If you need to log every statement, then AOP would be your best friend as already stated by Smutje.

Kurt Du Bois
  • 7,310
  • 4
  • 23
  • 33
1

You can try Jackplay, which is designed to enable tracing without coding or redeployment.

It presents web UI as your control panel to trace or undo tracing on your methods.

It also allows you to redefine a method body Live in JVM.

Alfred Xiao
  • 1,698
  • 14
  • 16