.class file includes debug info??

D

donalmurtagh

Hi,

I'm looking for a command-like utility which can read a .class file and
tell you whether it includes debug information, i.e. whether it was
built with the -g option of javac.

Thanks in Advance,

Dónal
 
C

Chris Uppal

[newgoups trimmed a little]

I'm looking for a command-like utility which can read a .class file and
tell you whether it includes debug information, i.e. whether it was
built with the -g option of javac.

You could find out by parsing the classfile (use whatever bytecode library
comes to hand). But that might be overkill for your application. If so, then
an approximate approach like checking whether the .class file contains the
strings "LocalVariableTable" and "LineNumberTable" (with that exact case)
should be a good approximation, provided that you you don't mind a small risk
of false positives.

That last test "works" -- if it does -- because the debugging info is stored as
so-called "attributes" with those names; attributes are named sub-sections of
the classfile.

-- chris
 
D

donalmurtagh

Thanks for your suggestion. I understand your "approximate approach" to
mean that if I write a shell script which "greps" a .class file for the
words "LocalVariableTable" and "LineNumberTable", it will tell me
whether the .class file contains debug info.

I'm not too sure what you mean by "parsing the classfile (use whatever
bytecode library comes to hand)." Do you mean that I should write a
Java program, that will tell me whether the file contains debug info?
If so, any suggestions about how I might achieve this in Java (or some
other language) would be very welcome.

Cheers,
- Don
 
C

Chris Uppal

Thanks for your suggestion. I understand your "approximate approach" to
mean that if I write a shell script which "greps" a .class file for the
words "LocalVariableTable" and "LineNumberTable", it will tell me
whether the .class file contains debug info.

Yes, or any other file scanning tool that you are comfortable with.

One thing I've just now discovered is that the JDK 1.5.0 javac seems to include
a "LineNumberTable" whatever the value of the compile-time flags, so that part
of the test may be of no use to you (depending on which part of the debugging
information you want to know about).

I'm not too sure what you mean by "parsing the classfile (use whatever
bytecode library comes to hand)." Do you mean that I should write a
Java program, that will tell me whether the file contains debug info?
If so, any suggestions about how I might achieve this in Java (or some
other language) would be very welcome.

Well, I mean that you /could/ "write a Java program [etc]". I'm not
recommending you /do/ it unless you need to be able to tell /for sure/ what
debugging information is present. My guess is that you /don't/ need to be so
certain.

Actually it's not difficult to do using something like BCEL, the bytecode
library from the Apache Jakarta project. I was going to add a few pointers to
how to do it (should you decide you need it) but it turns out to be easer just
to post code, so I've appended a /very quick/ hack to the end of this. All it
does is loads each classfile named on the command line. Loops over each
class's methods. For each method it looks to see if there is
LocalVariableTable (or LineNumberTable, but that's commented out), and prints
the names of the classes that have at least one method with the required data.

-- chris

======== QUICK HACK =========

import java.io.*;
import org.apache.bcel.classfile.*;

public class IsDebug
{
public static void
main(String[] args)
throws IOException
{
for (int i = 0; i < args.length; i++)
if (hasDebugInfo(args))
System.out.println(args);
}

public static boolean
hasDebugInfo(String filename)
throws IOException
{
ClassParser parser = new ClassParser(filename);
JavaClass clazz = parser.parse();
Method[] methods = clazz.getMethods();
for (int i = 0; i < methods.length; i++)
if (hasDebugInfo(methods))
return true;

return false;
}

public static boolean
hasDebugInfo(Method method)
throws IOException
{
if (method.getLocalVariableTable() != null)
return true;

/*
* interestingly, JDK 1.5.0 (and possibly earlier) seems to include
* a line number table whatever the compile-time flags so this test
* may not be much use.
if (method.getLineNumberTable() != null)
return true;
*/

return false;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top