In Java, how to register a message every time a given monitor of an object is entered or exited?

I am trying to debug some C / Java bindings that use some custom refcounting / locking. I would like the JVM to print a message every time this object turned on or exited from its monitor. Is there any way to do this? Basically, I want this:

synchronized(lock) { ... System.out.println("hi"); ... } 

to print this:

 *** "lock" monitorenter hi *** "lock" monitorexit 

I looked at options XX and found nothing. This is OpenJDK 6.

+4
source share
6 answers
Good question. The only solution I could come up with is basically this:

Use a custom loader class and preprocess files using a bytecode manipulation library such as ASM . (ASM has a good example of how to work with rewriting bytecode in classloaders.)

Then just add a call to System.out.println before each monitorenter and monitorexit .

Thanks to a good visitor template in the ASM library, this should not be more than a screen or two codes.

+2
source

Trying to debug concurreny when using ads in print ads is a losing battle, as your print statements may have their own concurrency error and not print in the expected order. Trying to debug or print your way out of a concurreny error may seem good, but I don't think that will give you the result you want. You need to use careful thinking and logic to reason that your code is correct (more computer science than software engineering).

Concurrency problems are very complex. If you have not read concurrency in Practice, be sure to read it. Then look at all the possible ways that your synchronized block can be achieved, everything that it can change, going beyond blocking, etc.

0
source

This will be the ideal situation to use dTrace.

Unfortunately, this requires Solaris or OS X.

Fortunately, OpenSolaris can be downloaded and run in a virtual machine. It works best in VirtualBox.

0
source

I do not believe that there is a way to bind to a "lock" event in java. But you can take a peek at java.lang.management for various lock information. For example, there is ThreadMXBean.findDeadlockedThreads ()

0
source

If you do not write your own lock class (or change the existing one), I assume that it would be rather difficult to do what you want, especially if you use a synchronized block over the monitor object, and not the Lock class. However, you can use the jstack command that comes with the JDK to analyze your process at runtime, check here for the manual page, and there is also a JVM parameter -XX: -PrintConcurrentLocks to print your locks if you stop your JVM process with Ctrl -Break (more options here ).

0
source

I suggest you implement an existing implementation of the Lock class or implement one of you ( http://download.oracle.com/javase/tutorial/essential/concurrency/newlocks.html ). Now you can override the lock and unlock method. Thus, instead of using synchronized methods / operators, use this object, and write down your log in the lock / unlock methods :)

0
source

All Articles