FileDocCategorySizeDatePackage
BackEndTest.javaAPI DocphoneME MR2 API (J2ME)10962Wed May 02 17:59:48 BST 2007com.sun.cldchi.tools.memoryprofiler.jdwp

BackEndTest

public class BackEndTest extends jdwp implements VMConnection
The BackEndTest class implemtens common debugger features such as:
  • Connects to DebugAgent (either KdpProxy or Java), sends Handshake command and waits for VM_INIT event.
  • Contains a number of helper functions to work with JDWP
This class encapsulates a huge amount of direct work with JDWP.

It's a good idea to place all common JDWP operations here. Notice that all BackEndTest methods are declared as static so you must never create instances of this class

see
jdwp.jdwp
see
jdwp.Reply
see
jdwp.Tools
see
jdwp.Packet
see
jdwp.Command
see
jdwp.Transport
see
jdwp.ByteBuffer
see
jdwp.BoundException
see
jdwp.BreakpointInfo
see
jdwp.SocketTransport
see
jdwp.MethodDescriptor

Fields Summary
public static boolean
verbose
Debug time variable. To see a bit more verbose output turn it to true. In non-debug mode must be false otherwise identity of J2SE and KVM outputs is not guarantered.
public boolean
is_connected
public SocketTransport
debug
This object executes all low-level work with JDWP (connection establishing/terminating, data exchange etc).
Constructors Summary
Methods Summary
public ReplycheckReply(Command c, int ErrorCode, java.lang.String Description)
Sends JDWP command and waits for reply. The method expects that JDWP reply packet will contain expected error code otherwise DebugeeException is raised. Currently this method is used internally by othe methods of BackEndTest.

param
c JDWP command to be sent
param
ErrorCode expected error code in reply packet
param
Description if unexpected error code (in particular, NONE)is received this string will be included in the error message
return
received reply packet
see
#checkReplyF(Command, int, String)

        Reply r = checkReply(c);
        if (r.getErrorCode() != ErrorCode) {
            print("\nCommand:\n" + c + "\nReply:\n" + r);
            String m = "Unexpected error code";
            if (! Description.equals(""))
                Description = "Unexpected error code (" + Description + ").";
            else
                Description = "Unexpected error code.";
            throw new DebugeeException (Description);
        }
        return r;
    
public ReplycheckReply(Command c)
Sends JDWP command and waits for reply. If reply is not received in 100 seconds DebugeeException is raised. So large timeout is choosen because of possibility of using extremely slow or busy systems where VM being debugged is run. This method is used internally by other BackEndTest methods. Also it's a good idea to explicity invoke this method when non-zero error code in considered as correct or allowable answer.

param
c JDWP command to be sent
return
received reply packet
throws
DebugeeException the command can't be sent or reply packet is not received in 100 seconds
throws
IOException a generic I/O error occured

        sendCommand(c);
        Reply r = debug.receiveReply(c.getID(), 100000);
        if (r.getErrorCode() == Reply.errNotAvailable) {
            print("\nCommand:\n" + c);
            printReplies();
            throw new DebugeeException("Reply packet is not received.");
        }
        return r;
    
public voidcloseConnections()
Closes connection. This function is executed prior to KJDB finishing.

        if (debug != null) {
            try {
                debug.done();
            } catch (Exception e) {};
            debug = null;
        }
        is_connected = false;
    
public voidconnect(java.lang.String hostName, int port)

        
            
        try{
            openConnections(hostName, port);
            is_connected = true;
        }catch(DebugeeException e){
            throw new ConnectException(e.getMessage());
        }catch(IOException e){
            throw new ConnectException(e.getMessage());
        }
    
public voidgetIDSizes()
Determines the sizes (in bytes ) of field IDs, method IDs, object reference IDs and reference type IDs. These values are VM specific and are obtained via VirtualMachine/IDSizes JDWP command. This method is invoked by openConnections(String, int) method immediately after establishing JDWP connection.

see
#openConnections(String, int)

        try{
            Command c;
            Reply r;
            c = new Command(0x0107);
            r = checkReply(c);
            r.resetDataParser();
            fieldIDSize = r.getInt();
            methodIDSize = r.getInt();
            objectIDSize = r.getInt();
            referenceTypeIDSize = r.getInt();
            frameIDSize = r.getInt();
        }catch(Exception e){
            throw new DebugeeException("Exception occured in IDSizes function");
        } 
    
public booleanisConnected()

      return is_connected;
    
private voidopenConnections(java.lang.String debugee_server, int debugee_port)
Connects to VM being debugged.

param
debugee_server host name where VM being debugged is started
param
debugee_port TCP/IP port for connection with VM being debugged
throws
ConnectException connection establishing failed
throws
DebugeeException VM being debugged sent JDWP package with incorrect content


        try {
            Tools.wait(1000);
            debug = new SocketTransport();

            debug.attachToServer(debugee_server, debugee_port);
            debug.Handshake();
        }
        catch (Exception e) {
            System.err.println("* Exception.\n");
            throw new ConnectException("Can't create JDWP connection.");
        }

        System.out.println("* JDWP connection is installed.");
        getIDSizes();
    
public static voidprint(java.lang.String s)
Debug time method. Rewrite it if you'd like to see debug output not in the standard output

param
s String to be printed

        if (s.equals(""))
            return;
        System.out.println(s);
    
public static voidprint(java.util.Vector v)
Debug time method. Prints the content of the vector to standard output.

param
v java.util.Vector object to be printed

        System.out.println(Tools.listVector(v));
    
public static voidprint(Packet p, java.lang.String how)
Debug time method. Prints the content of JDWP package in specified manner.

param
p JDWP package to be printed
param
how mask for proper representing of package content. For example, the mask i(o) will protected parsed as follows: first is int that determines the number of object references followed. All the follwed elements are object references

        print(p.parse(how));
    
public voidprintReplies()
Prints all JDWP replies that were not demanded yet. This method is invoked when no expected reply is received that is considered as fatal error.


        debug.receive();

        if (debug.Replies.size() == 0)
            return;

           print("\nReplies:\n");
        for (int i = 0; i < debug.Replies.size(); i++)
            print("\n" + (Reply) debug.Replies.elementAt(i) + "\n");
    
public voidsendCommand(int command)

      sendCommand(new Command(command));
    
public CommandsendCommand(Command c)
This method sends JDWP command and does not wait for answer. It's used internally by other BackEndTest methods. Also it's a good idea to use this method in some cases when answer is not guaranteered (for example, if you send VirtualMachine/Resume JDWP command you may receive no answer because of debugging session finishing).

param
c command to be sent
return
command with ID field filled

        try{
            debug.sendCommand(c);
        }catch (IOException e){
            throw new DebugeeException(e.getMessage());
        }
        return c;
    
public VMReplysendReplyCommand(int command, int[] params)

      try {
        Command cmd = new Command(command);
        for (int i = 0; i < params.length; i++) {
          cmd.addInt(params[i]);
        }
        Reply reply = checkReply(cmd);
        reply.resetDataParser(); 
        return reply;
      } catch (IOException e) {
        throw new DebugeeException(e.getMessage());
      }
    
public VMReplysendReplyCommand(int command)

      try {
        Reply reply = checkReply(new Command(command));
        reply.resetDataParser(); 
        return reply;
      } catch (IOException e) {
        throw new DebugeeException(e.getMessage());
      }