TIP: Use NetBeans debugger to debug class loading

  1. In this blog entry I talked about how to use NetBeans debugger breakpoints to trace your application functionality.
  2. In the newer trunk (6.0) builds of NetBeans debugger it is possible to view the values of method parameters even if the debug information is missing. What this means is that you can also debug into the JDK libraries and look at the parameter values. NOTE: AFAIK this only works if you are running the debugee with JDK 6.0.

Using the above two features it is possible to explore and/or debug class loading problems in a multi classloader applications such as NetBeans IDE, App server. So what is the trick?

As we know - in Java - the classes are loaded by classloaders. Ultimately any class is defined using the following method of the java.lang.ClassLoader :

 protected final Class<?> defineClass(String name,
                                      byte[] b,
                                      int off,
                                      int len,
                                      ProtectionDomain protectionDomain)
        throws ClassFormatError {...};

By setting the breakpoint in this method and customizing the Print Text field of the breakpoint it is possible to generate the trace of class loading. Also note that the Suspend setting is set to No thread (continue). That way the debugee does not stop every time the breakpoint is hit. The below screenshot shows the required breakpoint customization:

Breakpoint

I used the above breakpoint on one of my modules. The same technic can be used for App Server. Here is the output I got in the debugger console:

Classloading output

As you can see it shows exactly which one of the NetBeans classloaders loaded which classes. Formatting this information as a comma separated list and loading it into a spreadsheet one can analyse the class loading further.  For example - same class laoded by two different class loaders or a class loaded by unexpected classloader and so on. Customizing the Breakpoint condition you can go after a particular class (e.g. name.equals("packagename.classname") or classes from a cetrain package (e.g. name.startsWith("packagename.") .

I think there are many more cool features coming up in the NetBeans debugger. You will be able to learn about these new features at NetBeans Day (May 7th) and JavaOne 2007 (May 8th - 11th) in San Francisco. I know I will be there :)

Comments:

My friend and colleague Winston Prakash asked me if it will be possible to figure out the location of folder/jar file from which the class was loaded from. I think it should be possible by adding the following expression to the above breakpoint:

{=(protectionDomain==null || protectionDomain.getCodeSource()== null?"":protectionDomain.getCodeSource().getLocation().toString())}
This may slow down the debugger though. (Not tested yet - but should work).

Posted by Sandip on April 17, 2007 at 01:52 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

sandipchitale

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today