.class file includes debug info??

Discussion in 'Java' started by donalmurtagh@yahoo.co.uk, Dec 21, 2004.

  1. Guest

    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
     
    , Dec 21, 2004
    #1
    1. Advertising

  2. Chris Uppal Guest

    [newgoups trimmed a little]

    wrote:

    > 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
     
    Chris Uppal, Dec 21, 2004
    #2
    1. Advertising

  3. Guest

    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
     
    , Dec 22, 2004
    #3
  4. Chris Uppal Guest

    wrote:

    > 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;
    }
    }
     
    Chris Uppal, Dec 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vasanth
    Replies:
    0
    Views:
    587
    Vasanth
    Jun 28, 2004
  2. Andrea Williams

    Clear the Render info & change to different info

    Andrea Williams, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,446
    Ken Dopierala Jr.
    Oct 27, 2004
  3. André
    Replies:
    3
    Views:
    1,642
  4. Kiran
    Replies:
    0
    Views:
    1,247
    Kiran
    Feb 6, 2005
  5. Jeff
    Replies:
    4
    Views:
    2,927
    Jeff Schwab
    Feb 3, 2008
Loading...

Share This Page