1 Reply Latest reply: May 1, 2009 10:24 AM by 843810 RSS

    probelm with ReferenceType in setting up a breakpoint

    843810
      Im a newbie to java & im trying to develop a simple debugger,This debugger setsup a breakpoint a specific line no.But when i trace the code among the list of classes loaded the Debugee class itself is absent.Only library classes are present.Ive pasted only the necessary function bcos only 7500 characters allowed.

      "void setbreakpoint(int lno,String s)" is the function which sets up the break point.please someone reply as early as possible .


      package jds;

      public class JDS {

      static VirtualMachine vm;


      public static void main(String[] args) {
      PrintWriter writer = new PrintWriter(System.out);
      // TODO code application logic here
      if(args.length == 0 || args[0].equalsIgnoreCase("-help"))
      {
      help();
      System.exit(0);
      }
      StringBuffer sb =new StringBuffer();
      sb.append(args[0]);
      for(int i=1;i<args.length;i++)
      {
      sb.append(' ');
      sb.append(args);
      }
      LaunchVM lvm = new LaunchVM();
      vm = lvm.launchTarget(sb.toString());
      System.out.println(sb.toString());
      vm.setDebugTraceMode(0);
      String[] s1={"java.*", "javax.*", "sun.*","com.sun.*"};
      DbgEngine expm =new DbgEngine(vm,s1,writer);
      expm.setEventRequests(false);
      String str= new String();
      str = readstr();
      while (str.startsWith("b:"))
      {
      String s = str.substring(2);
      int lno = Integer.parseInt(s);
      System.out.println("line no " + lno +" "+ args[0]);
      expm.setbreakpoint(lno,args[0]);
      str = readstr();
      }
      if (str.contains("run"))
      {  
      System.out.println("run cmd");
      expm.run1();
      redirectOutput(vm);
      vm.resume();
      try {
           expm.join();
           errThread.join(); // Make sure output is forwarded
           outThread.join(); // before we exit
      writer.close();
           } catch (InterruptedException exc) {
           // we don't interrupt
           }

      }

      }

      }


      another class files code

      package jds;

      import com.sun.jdi.*;
      import com.sun.jdi.request.*;
      import com.sun.jdi.event.*;

      import java.util.*;
      import java.io.PrintWriter;


      public class DbgEngine extends Thread {

      private final VirtualMachine vm; // Running VM
      private final String[] excludes; // Packages to exclude
      private final PrintWriter writer; // Where output goes
      private boolean connected = true; // Connected to VM
      private boolean vmDied = true; // VMDeath occurred

      // Maps ThreadReference to ThreadTrace instances
      private Map traceMap = new HashMap();

      DbgEngine(VirtualMachine vm, String[] excludes, PrintWriter writer) {
      super("event-handler");
      this.vm = vm;
      this.excludes = excludes;
      this.writer = writer;
      }


      public void run1() {
      EventQueue queue = vm.eventQueue();
      while (connected) {
      try {
      EventSet eventSet = queue.remove();
      EventIterator it = eventSet.eventIterator();
      while (it.hasNext()) {
      handleEvent(it.nextEvent());
      }
      eventSet.resume();
      } catch (InterruptedException exc) {
      // Ignore
      } catch (VMDisconnectedException discExc) {
      handleDisconnectedException();
      break;
      }
      }
      }


      void setbreakpoint(int lno,String s)
      {
      List reftyp = vm.allClasses();
      Iterator iter = reftyp.iterator();
      while(iter.hasNext())
      {
      ReferenceType rt = (ReferenceType)iter.next();
      try
      {
      if(rt.sourceName().contains(s))
      {
      try
      {
      Location l = findloc(rt,lno);
      EventRequestManager erm = vm.eventRequestManager();
      BreakpointRequest bpr = erm.createBreakpointRequest(l);
      bpr.enable();
      }
      catch (AbsentInformationException aie)
      {
      //ignore
      }

      }
      }
      catch(AbsentInformationException aie)
      {
      //ignore
      }
      }
      }
      Location findloc(ReferenceType rt,int loc) throws AbsentInformationException
      {

      List loclist = rt.locationsOfLine(loc);
      Iterator iter = loclist.iterator();
      return (Location)iter.next();

      }


      void setEventRequests(boolean watchFields) {
      EventRequestManager mgr = vm.eventRequestManager();

      // want all exceptions
      ExceptionRequest excReq = mgr.createExceptionRequest(null,
      true, true);
      // suspend so we can step
      excReq.setSuspendPolicy(EventRequest.SUSPEND_ALL);
      excReq.enable();

      MethodEntryRequest menr = mgr.createMethodEntryRequest();
      for (int i=0; i<excludes.length; ++i) {
      menr.addClassExclusionFilter(excludes[i]);
      }
      menr.setSuspendPolicy(EventRequest.SUSPEND_NONE);
      menr.enable();

      MethodExitRequest mexr = mgr.createMethodExitRequest();
      for (int i=0; i<excludes.length; ++i) {
      mexr.addClassExclusionFilter(excludes[i]);
      }
      mexr.setSuspendPolicy(EventRequest.SUSPEND_NONE);
      mexr.enable();

      ThreadDeathRequest tdr = mgr.createThreadDeathRequest();
           // Make sure we sync on thread death
      tdr.setSuspendPolicy(EventRequest.SUSPEND_ALL);
      tdr.enable();

           if (watchFields) {
           ClassPrepareRequest cpr = mgr.createClassPrepareRequest();

           for (int i=0; i<excludes.length; ++i) {
                cpr.addClassExclusionFilter(excludes[i]);
           }
           cpr.setSuspendPolicy(EventRequest.SUSPEND_ALL);
           cpr.enable();
           }
      }


      private void handleEvent(Event event) {
      if (event instanceof ExceptionEvent) {
      exceptionEvent((ExceptionEvent)event);
      } else if (event instanceof ModificationWatchpointEvent) {
      fieldWatchEvent((ModificationWatchpointEvent)event);
      } else if (event instanceof MethodEntryEvent) {
      methodEntryEvent((MethodEntryEvent)event);
      } else if (event instanceof MethodExitEvent) {
      methodExitEvent((MethodExitEvent)event);
      }
      else if (event instanceof BreakpointEvent)
      System.out.println(event);
      else if (event instanceof StepEvent) {
      stepEvent((StepEvent)event);
      } else if (event instanceof ThreadDeathEvent) {
      threadDeathEvent((ThreadDeathEvent)event);
      } else if (event instanceof ClassPrepareEvent) {
      classPrepareEvent((ClassPrepareEvent)event);
      } else if (event instanceof VMStartEvent) {
      vmStartEvent((VMStartEvent)event);
      } else if (event instanceof VMDeathEvent) {
      vmDeathEvent((VMDeathEvent)event);
      } else if (event instanceof VMDisconnectEvent) {
      vmDisconnectEvent((VMDisconnectEvent)event);
      } else {
      throw new Error("Unexpected event type");
      }
      }

      }