1 /*
   2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package lib.jdb;
  25 
  26 import java.util.Arrays;
  27 import java.util.stream.Collectors;
  28 
  29 /**
  30  * Represents list of commands of <code>jdb</code> from JDK1.4:
  31  *
  32  *   run [class [args]]        -- start execution of application's main class
  33  *
  34  *   threads [threadgroup]     -- list threads
  35  *   thread <thread id>        -- set default thread
  36  *   suspend [thread id(s)]    -- suspend threads (default: all)
  37  *   resume [thread id(s)]     -- resume threads (default: all)
  38  *   where [thread id] | all   -- dump a thread's stack
  39  *   wherei [thread id] | all  -- dump a thread's stack, with pc info
  40  *   up [n frames]             -- move up a thread's stack
  41  *   down [n frames]           -- move down a thread's stack
  42  *   kill <thread> <expr>      -- kill a thread with the given exception object
  43  *   interrupt <thread>        -- interrupt a thread
  44  *
  45  *   print <expr>              -- print value of expression
  46  *   dump <expr>               -- print all object information
  47  *   eval <expr>               -- evaluate expression (same as print)
  48  *   set <lvalue> = <expr>     -- assign new value to field/variable/array element
  49  *   locals                    -- print all local variables in current stack frame
  50  *
  51  *   classes                   -- list currently known classes
  52  *   class <class id>          -- show details of named class
  53  *   methods <class id>        -- list a class's methods
  54  *   fields <class id>         -- list a class's fields
  55  *
  56  *   threadgroups              -- list threadgroups
  57  *   threadgroup <name>        -- set current threadgroup
  58  *
  59  *   stop in <class id>.<method>[(argument_type,...)]
  60  *                             -- set a breakpoint in a method
  61  *   stop at <class id>:<line> -- set a breakpoint at a line
  62  *   clear <class id>.<method>[(argument_type,...)]
  63  *                             -- clear a breakpoint in a method
  64  *   clear <class id>:<line>   -- clear a breakpoint at a line
  65  *   clear                     -- list breakpoints
  66  *   catch <class id>          -- break when specified exception thrown
  67  *   ignore <class id>         -- cancel 'catch'  for the specified exception
  68  *   watch [access|all] <class id>.<field name>
  69  *                             -- watch access/modifications to a field
  70  *   unwatch [access|all] <class id>.<field name>
  71  *                             -- discontinue watching access/modifications to a field
  72  *   trace methods [thread]    -- trace method entry and exit
  73  *   untrace methods [thread]  -- stop tracing method entry and exit
  74  *   step                      -- execute current line
  75  *   step up                   -- execute until the current method returns to its caller
  76  *   stepi                     -- execute current instruction
  77  *   next                      -- step one line (step OVER calls)
  78  *   cont                      -- continue execution from breakpoint
  79  *
  80  *   list [line number|method] -- print source code
  81  *   use (or sourcepath) [source file path]
  82  *                             -- display or change the source path
  83  *   exclude [class id ... | "none"]
  84  *                             -- do not report step or method events for specified classes
  85  *   classpath                 -- print classpath info from target VM
  86  *
  87  *   monitor <command>         -- execute command each time the program stops
  88  *   monitor                   -- list monitors
  89  *   unmonitor <monitor#>      -- delete a monitor
  90  *   read <filename>           -- read and execute a command file
  91  *
  92  *   lock <expr>               -- print lock info for an object
  93  *   threadlocks [thread id]   -- print lock info for a thread
  94  *
  95  *   pop                       -- pop the stack through and including the current frame
  96  *   reenter                   -- same as pop, but current frame is reentered
  97  *   redefine <class id> <class file name>
  98  *                             -- redefine the code for a class
  99  *
 100  *   disablegc <expr>          -- prevent garbage collection of an object
 101  *   enablegc <expr>           -- permit garbage collection of an object
 102  *
 103  *   !!                        -- repeat last command
 104  *   <n> <command>             -- repeat command n times
 105  *   help (or ?)               -- list commands
 106  *   version                   -- print version information
 107  *   exit (or quit)            -- exit debugger
 108  *
 109  *   <class id>: full class name with package qualifiers or a
 110  *   pattern with a leading or trailing wildcard ('*').
 111  *   <thread id>: thread number as reported in the 'threads' command
 112  *   <expr>: a Java(tm) Programming Language expression.
 113  *   Most common syntax is supported.
 114  *
 115  *   Startup commands can be placed in either "jdb.ini" or ".jdbrc"
 116  *   in user.home or user.dir
 117  */
 118 public class JdbCommand {
 119     final String cmd;
 120     boolean allowExit = false;
 121     boolean replyIsSimplePrompt = false;
 122 
 123     public JdbCommand(String cmd) {
 124         this.cmd = cmd;
 125     }
 126 
 127     public JdbCommand allowExit() {
 128         allowExit = true;
 129         return this;
 130     }
 131 
 132     public JdbCommand replyIsSimplePrompt() {
 133         replyIsSimplePrompt = true;
 134         return this;
 135     }
 136 
 137 
 138     public static JdbCommand run(String ... params) {
 139         return new JdbCommand("run " + Arrays.stream(params).collect(Collectors.joining(" ")));
 140     }
 141     public static JdbCommand cont() {
 142         return new JdbCommand("cont");
 143     }
 144     public static JdbCommand dump(String what) {
 145         return new JdbCommand("dump " + what);
 146     }
 147     public static JdbCommand quit() {
 148         // the command suppose jdb terminates
 149         return new JdbCommand("quit").allowExit();
 150     }
 151     public static JdbCommand stopAt(String targetClass, int lineNum) {
 152         return new JdbCommand("stop at " + targetClass + ":" + lineNum);
 153     }
 154     public static JdbCommand stopThreadAt(String targetClass, int lineNum) {
 155         return new JdbCommand("stop thread at " + targetClass + ":" + lineNum);
 156     }
 157     public static JdbCommand stopGoAt(String targetClass, int lineNum) {
 158         return new JdbCommand("stop go at " + targetClass + ":" + lineNum);
 159     }
 160     public static JdbCommand stopIn(String targetClass, String methodName) {
 161         return new JdbCommand("stop in " + targetClass + "." + methodName);
 162     }
 163     public static JdbCommand thread(int threadNumber) {
 164         return new JdbCommand("thread " + threadNumber);
 165     }
 166     // clear <class id>:<line>   -- clear a breakpoint at a line
 167     public static JdbCommand clear(String targetClass, int lineNum) {
 168         return new JdbCommand("clear " + targetClass + ":" + lineNum);
 169     }
 170 
 171     // exception type used by catch/ignore
 172     public enum ExType{
 173         uncaught,
 174         caught,
 175         all
 176     }
 177     public static JdbCommand catch_(String classId) {
 178         return new JdbCommand("catch " + classId);
 179     }
 180     public static JdbCommand catch_(ExType eType, String classId) {
 181         return catch_(eType.toString() + " " + classId);
 182     }
 183     public static JdbCommand ignore(String classId) {
 184         return new JdbCommand("ignore " + classId);
 185     }
 186     public static JdbCommand ignore(ExType eType, String classId) {
 187         return ignore(eType.toString() + " " + classId);
 188     }
 189 
 190     public static JdbCommand step() {
 191         return new JdbCommand("step");
 192     }
 193     public static JdbCommand stepUp() {
 194         return new JdbCommand("step up");
 195     }
 196     public static JdbCommand next() {
 197         return new JdbCommand("next");
 198     }
 199 
 200     // where [thread id] | all
 201     public static JdbCommand where(String threadId) {
 202         return new JdbCommand("where " + threadId);
 203     }
 204     public static JdbCommand eval(String expr) {
 205         return new JdbCommand("eval " + expr);
 206     }
 207     public static JdbCommand print(String expr) {
 208         return new JdbCommand("print " + expr);
 209     }
 210 
 211     public static JdbCommand locals() {
 212         return new JdbCommand("locals");
 213     }
 214 
 215     public static JdbCommand set(String lvalue, String expr) {
 216         return new JdbCommand("set " + lvalue + " = " + expr);
 217     }
 218 
 219     public static JdbCommand lock(String expr) {
 220         return new JdbCommand("lock " + expr);
 221     }
 222 
 223     public static JdbCommand methods(String classId) {
 224         return new JdbCommand("methods " + classId);
 225     }
 226 
 227     // trace [go] methods [thread]
 228     //                           -- trace method entries and exits.
 229     //                           -- All threads are suspended unless 'go' is specified
 230     // trace [go] method exit | exits [thread]
 231     //                           -- trace the current method's exit, or all methods' exits
 232     //                           -- All threads are suspended unless 'go' is specified
 233     // untrace [methods]         -- stop tracing method entrys and/or exits
 234     public static JdbCommand trace(boolean go, String  mode, Integer threadId) {
 235         return new JdbCommand(" trace"
 236                 + (go ? " go" : "")
 237                 + (mode != null ? " " + mode : "")
 238                 + (threadId != null ? " " + threadId.toString() : ""));
 239     }
 240     // prints trace status
 241     public static JdbCommand trace() {
 242         return trace(false, null, null);
 243     }
 244     public static JdbCommand traceMethods(boolean go, Integer threadId) {
 245         return trace(go, "methods", threadId);
 246     }
 247     public static JdbCommand traceMethodExit(boolean go, Integer threadId) {
 248         return trace(go, "method exit", threadId);
 249     }
 250     public static JdbCommand traceMethodExits(boolean go, Integer threadId) {
 251         return trace(go, "method exits", threadId);
 252     }
 253     public static JdbCommand untrace() {
 254         return new JdbCommand("untrace");
 255     }
 256 
 257     // watch [access|all] <class id>.<field name>
 258     public static JdbCommand watch(String classId, String fieldName) {
 259         return new JdbCommand("watch " + classId + "." + fieldName);
 260     }
 261 
 262     public static JdbCommand pop() {
 263         return new JdbCommand("pop");
 264     }
 265 
 266     public static JdbCommand redefine(String classId, String classFileName) {
 267         return new JdbCommand("redefine " + classId + " " + classFileName);
 268     }
 269 }