private void commandMethods(StringTokenizer t) throws NoSessionException {
   if (!t.hasMoreTokens()) {
     env.error("No class specified.");
     return;
   }
   String idClass = t.nextToken();
   ReferenceType cls = findClass(idClass);
   if (cls != null) {
     List<Method> methods = cls.allMethods();
     OutputSink out = env.getOutputSink();
     for (int i = 0; i < methods.size(); i++) {
       Method method = methods.get(i);
       out.print(method.declaringType().name() + " " + method.name() + "(");
       Iterator<String> it = method.argumentTypeNames().iterator();
       if (it.hasNext()) {
         while (true) {
           out.print(it.next());
           if (!it.hasNext()) {
             break;
           }
           out.print(", ");
         }
       }
       out.println(")");
     }
     out.show();
   } else {
     // ### Should validate class name syntax.
     env.failure("\"" + idClass + "\" is not a valid id or class name.");
   }
 }
 private void commandThreads(StringTokenizer t) throws NoSessionException {
   if (!t.hasMoreTokens()) {
     OutputSink out = env.getOutputSink();
     printThreadGroup(out, getDefaultThreadGroup(), 0);
     out.show();
     return;
   }
   String name = t.nextToken();
   ThreadGroupReference tg = findThreadGroup(name);
   if (tg == null) {
     env.failure(name + " is not a valid threadgroup name.");
   } else {
     OutputSink out = env.getOutputSink();
     printThreadGroup(out, tg, 0);
     out.show();
   }
 }
 private void commandClasses() throws NoSessionException {
   OutputSink out = env.getOutputSink();
   // out.println("** classes list **");
   for (ReferenceType refType : runtime.allClasses()) {
     out.println(refType.name());
   }
   out.show();
 }
 private void dumpStack(ThreadReference thread, boolean showPC) {
   // ### Check for these.
   // env.failure("Thread no longer exists.");
   // env.failure("Target VM must be in interrupted state.");
   // env.failure("Current thread isn't suspended.");
   // ### Should handle extremely long stack traces sensibly for user.
   List<StackFrame> stack = null;
   try {
     stack = thread.frames();
   } catch (IncompatibleThreadStateException e) {
     env.failure("Thread is not suspended.");
   }
   // ### Fix this!
   // ### Previously mishandled cases where thread was not current.
   // ### Now, prints all of the stack regardless of current frame.
   int frameIndex = 0;
   // int frameIndex = context.getCurrentFrameIndex();
   if (stack == null) {
     env.failure("Thread is not running (no stack).");
   } else {
     OutputSink out = env.getOutputSink();
     int nFrames = stack.size();
     for (int i = frameIndex; i < nFrames; i++) {
       StackFrame frame = stack.get(i);
       Location loc = frame.location();
       Method meth = loc.method();
       out.print("  [" + (i + 1) + "] ");
       out.print(meth.declaringType().name());
       out.print('.');
       out.print(meth.name());
       out.print(" (");
       if (meth.isNative()) {
         out.print("native method");
       } else if (loc.lineNumber() != -1) {
         try {
           out.print(loc.sourceName());
         } catch (AbsentInformationException e) {
           out.print("<unknown>");
         }
         out.print(':');
         out.print(loc.lineNumber());
       }
       out.print(')');
       if (showPC) {
         long pc = loc.codeIndex();
         if (pc != -1) {
           out.print(", pc = " + pc);
         }
       }
       out.println();
     }
     out.show();
   }
 }
 private void commandThreadGroups() throws NoSessionException {
   ThreadGroupIterator it = allThreadGroups();
   int cnt = 0;
   OutputSink out = env.getOutputSink();
   while (it.hasNext()) {
     ThreadGroupReference tg = it.nextThreadGroup();
     ++cnt;
     out.println("" + cnt + ". " + Utils.description(tg) + " " + tg.name());
   }
   out.show();
 }
 private void listEventRequests() throws NoSessionException {
   // Print set breakpoints
   List<EventRequestSpec> specs = runtime.eventRequestSpecs();
   if (specs.isEmpty()) {
     env.notice("No breakpoints/watchpoints/exceptions set.");
   } else {
     OutputSink out = env.getOutputSink();
     out.println("Current breakpoints/watchpoints/exceptions set:");
     for (EventRequestSpec bp : specs) {
       out.println("\t" + bp);
     }
     out.show();
   }
 }
  private void commandLocals() throws NoSessionException {
    ThreadReference current = context.getCurrentThread();
    if (current == null) {
      env.failure("No default thread specified: " + "use the \"thread\" command first.");
      return;
    }
    StackFrame frame;
    try {
      frame = context.getCurrentFrame(current);
      if (frame == null) {
        env.failure("Thread has not yet created any stack frames.");
        return;
      }
    } catch (VMNotInterruptedException e) {
      env.failure("Target VM must be in interrupted state.");
      return;
    }

    List<LocalVariable> vars;
    try {
      vars = frame.visibleVariables();
      if (vars == null || vars.size() == 0) {
        env.failure("No local variables");
        return;
      }
    } catch (AbsentInformationException e) {
      env.failure(
          "Local variable information not available."
              + " Compile with -g to generate variable information");
      return;
    }

    OutputSink out = env.getOutputSink();
    out.println("Method arguments:");
    for (LocalVariable var : vars) {
      if (var.isArgument()) {
        printVar(out, var, frame);
      }
    }
    out.println("Local variables:");
    for (LocalVariable var : vars) {
      if (!var.isArgument()) {
        printVar(out, var, frame);
      }
    }
    out.show();
    return;
  }
 private void commandPrint(StringTokenizer t, boolean dumpObject) throws NoSessionException {
   if (!t.hasMoreTokens()) {
     // ### Probably confused if expresion contains whitespace.
     env.error("No expression specified.");
     return;
   }
   ThreadReference current = context.getCurrentThread();
   if (current == null) {
     env.failure("No default thread specified: " + "use the \"thread\" command first.");
     return;
   }
   StackFrame frame;
   try {
     frame = context.getCurrentFrame(current);
     if (frame == null) {
       env.failure("Thread has not yet created any stack frames.");
       return;
     }
   } catch (VMNotInterruptedException e) {
     env.failure("Target VM must be in interrupted state.");
     return;
   }
   while (t.hasMoreTokens()) {
     String expr = t.nextToken("");
     Value val = null;
     try {
       val = runtime.evaluate(frame, expr);
     } catch (Exception e) {
       env.error("Exception: " + e);
       // ### Fix this!
     }
     if (val == null) {
       return; // Error message already printed
     }
     OutputSink out = env.getOutputSink();
     if (dumpObject && (val instanceof ObjectReference) && !(val instanceof StringReference)) {
       ObjectReference obj = (ObjectReference) val;
       ReferenceType refType = obj.referenceType();
       out.println(expr + " = " + val.toString() + " {");
       dump(out, obj, refType, refType);
       out.println("}");
     } else {
       out.println(expr + " = " + val.toString());
     }
     out.show();
   }
 }
 private void commandList(StringTokenizer t) throws NoSessionException {
   ThreadReference current = context.getCurrentThread();
   if (current == null) {
     env.error("No thread specified.");
     return;
   }
   Location loc;
   try {
     StackFrame frame = context.getCurrentFrame(current);
     if (frame == null) {
       env.failure("Thread has not yet begun execution.");
       return;
     }
     loc = frame.location();
   } catch (VMNotInterruptedException e) {
     env.failure("Target VM must be in interrupted state.");
     return;
   }
   SourceModel source = sourceManager.sourceForLocation(loc);
   if (source == null) {
     if (loc.method().isNative()) {
       env.failure("Current method is native.");
       return;
     }
     env.failure("No source available for " + Utils.locationString(loc) + ".");
     return;
   }
   ReferenceType refType = loc.declaringType();
   int lineno = loc.lineNumber();
   if (t.hasMoreTokens()) {
     String id = t.nextToken();
     // See if token is a line number.
     try {
       lineno = Integer.valueOf(id).intValue();
     } catch (NumberFormatException nfe) {
       // It isn't -- see if it's a method name.
       List<Method> meths = refType.methodsByName(id);
       if (meths == null || meths.size() == 0) {
         env.failure(
             id + " is not a valid line number or " + "method name for class " + refType.name());
         return;
       } else if (meths.size() > 1) {
         env.failure(id + " is an ambiguous method name in" + refType.name());
         return;
       }
       loc = meths.get(0).location();
       lineno = loc.lineNumber();
     }
   }
   int startLine = (lineno > 4) ? lineno - 4 : 1;
   int endLine = startLine + 9;
   String sourceLine = source.sourceLine(lineno);
   if (sourceLine == null) {
     env.failure("" + lineno + " is an invalid line number for " + refType.name());
   } else {
     OutputSink out = env.getOutputSink();
     for (int i = startLine; i <= endLine; i++) {
       sourceLine = source.sourceLine(i);
       if (sourceLine == null) {
         break;
       }
       out.print(i);
       out.print("\t");
       if (i == lineno) {
         out.print("=> ");
       } else {
         out.print("   ");
       }
       out.println(sourceLine);
     }
     out.show();
   }
 }
 public void executeCommand(String command) {
   // ### Treatment of 'out' here is dirty...
   out = env.getOutputSink();
   if (echo) {
     out.println(">>> " + command);
   }
   StringTokenizer t = new StringTokenizer(command);
   try {
     String cmd;
     if (t.hasMoreTokens()) {
       cmd = t.nextToken().toLowerCase();
       lastCommand = cmd;
     } else {
       cmd = lastCommand;
     }
     if (cmd.equals("print")) {
       commandPrint(t, false);
     } else if (cmd.equals("eval")) {
       commandPrint(t, false);
     } else if (cmd.equals("dump")) {
       commandPrint(t, true);
     } else if (cmd.equals("locals")) {
       commandLocals();
     } else if (cmd.equals("classes")) {
       commandClasses();
     } else if (cmd.equals("methods")) {
       commandMethods(t);
     } else if (cmd.equals("threads")) {
       commandThreads(t);
     } else if (cmd.equals("thread")) {
       commandThread(t);
     } else if (cmd.equals("suspend")) {
       commandSuspend(t);
     } else if (cmd.equals("resume")) {
       commandResume(t);
     } else if (cmd.equals("cont")) {
       commandCont();
     } else if (cmd.equals("threadgroups")) {
       commandThreadGroups();
     } else if (cmd.equals("threadgroup")) {
       commandThreadGroup(t);
     } else if (cmd.equals("run")) {
       commandRun(t);
     } else if (cmd.equals("load")) {
       commandLoad(t);
     } else if (cmd.equals("connect")) {
       commandConnect(t);
     } else if (cmd.equals("attach")) {
       commandAttach(t);
     } else if (cmd.equals("detach")) {
       commandDetach(t);
     } else if (cmd.equals("interrupt")) {
       commandInterrupt(t);
       // ### Not implemented.
       //          } else if (cmd.equals("catch")) {
       //              commandCatchException(t);
       // ### Not implemented.
       //          } else if (cmd.equals("ignore")) {
       //              commandIgnoreException(t);
     } else if (cmd.equals("step")) {
       commandStep(t);
     } else if (cmd.equals("stepi")) {
       commandStepi();
     } else if (cmd.equals("next")) {
       commandNext();
     } else if (cmd.equals("nexti")) {
       commandNexti();
     } else if (cmd.equals("kill")) {
       commandKill(t);
     } else if (cmd.equals("where")) {
       commandWhere(t, false);
     } else if (cmd.equals("wherei")) {
       commandWhere(t, true);
     } else if (cmd.equals("up")) {
       commandUp(t);
     } else if (cmd.equals("down")) {
       commandDown(t);
     } else if (cmd.equals("frame")) {
       commandFrame(t);
     } else if (cmd.equals("stop")) {
       commandStop(t);
     } else if (cmd.equals("clear")) {
       commandClear(t);
     } else if (cmd.equals("list")) {
       commandList(t);
     } else if (cmd.equals("use")) {
       commandUse(t);
     } else if (cmd.equals("sourcepath")) {
       commandSourcepath(t);
     } else if (cmd.equals("classpath")) {
       commandClasspath(t);
     } else if (cmd.equals("monitor")) {
       commandMonitor(t);
     } else if (cmd.equals("unmonitor")) {
       commandUnmonitor(t);
     } else if (cmd.equals("view")) {
       commandView(t);
       //          } else if (cmd.equals("read")) {
       //              readCommand(t);
     } else if (cmd.equals("help") || cmd.equals("?")) {
       help();
     } else if (cmd.equals("quit") || cmd.equals("exit")) {
       try {
         runtime.detach();
       } catch (NoSessionException e) {
         // ignore
       }
       env.terminate();
     } else {
       // ### Dubious repeat-count feature inherited from 'jdb'
       if (t.hasMoreTokens()) {
         try {
           int repeat = Integer.parseInt(cmd);
           String subcom = t.nextToken("");
           while (repeat-- > 0) {
             executeCommand(subcom);
           }
           return;
         } catch (NumberFormatException exc) {
         }
       }
       out.println("huh? Try help...");
       out.flush();
     }
   } catch (NoSessionException e) {
     out.println("There is no currently attached VM session.");
     out.flush();
   } catch (Exception e) {
     out.println("Internal exception: " + e.toString());
     out.flush();
     System.out.println("JDB internal exception: " + e.toString());
     e.printStackTrace();
   }
   out.show();
 }