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 
 122     public JdbCommand(String cmd) {
 123         this.cmd = cmd;
 124     }
 125 
 126     public JdbCommand allowExit() {
 127         allowExit = true;
 128         return this;
 129     }
 130 
 131 
 132     public static JdbCommand run(String ... params) {
 133         return new JdbCommand("run " + Arrays.stream(params).collect(Collectors.joining(" ")));
 134     }
 135     public static JdbCommand cont() {
 136         return new JdbCommand("cont");
 137     }
 138     public static JdbCommand dump(String what) {
 139         return new JdbCommand("dump " + what);
 140     }
 141     public static JdbCommand quit() {
 142         // the command suppose jdb terminates
 143         return new JdbCommand("quit").allowExit();
 144     }
 145     public static JdbCommand stopAt(String targetClass, int lineNum) {
 146         return new JdbCommand("stop at " + targetClass + ":" + lineNum);
 147     }
 148     public static JdbCommand stopIn(String targetClass, String methodName) {
 149         return new JdbCommand("stop in " + targetClass + "." + methodName);
 150     }
 151 
 152     // clear <class id>:<line>   -- clear a breakpoint at a line
 153     public static JdbCommand clear(String targetClass, int lineNum) {
 154         return new JdbCommand("clear " + targetClass + ":" + lineNum);
 155     }
 156 
 157     // exception type used by catch/ignore
 158     public enum ExType{
 159         uncaught,
 160         caught,
 161         all
 162     }
 163     public static JdbCommand catch_(String classId) {
 164         return new JdbCommand("catch " + classId);
 165     }
 166     public static JdbCommand catch_(ExType eType, String classId) {
 167         return catch_(eType.toString() + " " + classId);
 168     }
 169     public static JdbCommand ignore(String classId) {
 170         return new JdbCommand("ignore " + classId);
 171     }
 172     public static JdbCommand ignore(ExType eType, String classId) {
 173         return ignore(eType.toString() + " " + classId);
 174     }
 175 
 176     public static JdbCommand step() {
 177         return new JdbCommand("step");
 178     }
 179     public static JdbCommand stepUp() {
 180         return new JdbCommand("step up");
 181     }
 182     public static JdbCommand next() {
 183         return new JdbCommand("next");
 184     }
 185 
 186     // where [thread id] | all
 187     public static JdbCommand where(String threadId) {
 188         return new JdbCommand("where " + threadId);
 189     }
 190     public static JdbCommand eval(String expr) {
 191         return new JdbCommand("eval " + expr);
 192     }
 193     public static JdbCommand print(String expr) {
 194         return new JdbCommand("print " + expr);
 195     }
 196 
 197     public static JdbCommand locals() {
 198         return new JdbCommand("locals");
 199     }
 200 
 201     public static JdbCommand set(String lvalue, String expr) {
 202         return new JdbCommand("set " + lvalue + " = " + expr);
 203     }
 204 
 205     public static JdbCommand lock(String expr) {
 206         return new JdbCommand("lock " + expr);
 207     }
 208 
 209     public static JdbCommand methods(String classId) {
 210         return new JdbCommand("methods " + classId);
 211     }
 212 
 213     // trace [go] methods [thread]
 214     //                           -- trace method entries and exits.
 215     //                           -- All threads are suspended unless 'go' is specified
 216     // trace [go] method exit | exits [thread]
 217     //                           -- trace the current method's exit, or all methods' exits
 218     //                           -- All threads are suspended unless 'go' is specified
 219     // untrace [methods]         -- stop tracing method entrys and/or exits
 220     public static JdbCommand trace(boolean go, String  mode, Integer threadId) {
 221         return new JdbCommand(" trace"
 222                 + (go ? " go" : "")
 223                 + (mode != null ? " " + mode : "")
 224                 + (threadId != null ? " " + threadId.toString() : ""));
 225     }
 226     // prints trace status
 227     public static JdbCommand trace() {
 228         return trace(false, null, null);
 229     }
 230     public static JdbCommand traceMethods(boolean go, Integer threadId) {
 231         return trace(go, "methods", threadId);
 232     }
 233     public static JdbCommand traceMethodExit(boolean go, Integer threadId) {
 234         return trace(go, "method exit", threadId);
 235     }
 236     public static JdbCommand traceMethodExits(boolean go, Integer threadId) {
 237         return trace(go, "method exits", threadId);
 238     }
 239     public static JdbCommand untrace() {
 240         return new JdbCommand("untrace");
 241     }
 242 
 243     // watch [access|all] <class id>.<field name>
 244     public static JdbCommand watch(String classId, String fieldName) {
 245         return new JdbCommand("watch " + classId + "." + fieldName);
 246     }
 247 
 248     public static JdbCommand pop() {
 249         return new JdbCommand("pop");
 250     }
 251 
 252     public static JdbCommand redefine(String classId, String classFileName) {
 253         return new JdbCommand("redefine " + classId + " " + classFileName);
 254     }
 255 }