1 /*
  2  * Copyright (c) 1998, 2021, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package com.sun.tools.jdi;
 27 
 28 import java.io.File;
 29 import java.io.IOException;
 30 import java.lang.reflect.InvocationTargetException;
 31 import java.util.Map;
 32 import java.util.Random;
 33 
 34 import com.sun.jdi.VirtualMachine;
 35 import com.sun.jdi.connect.Connector;
 36 import com.sun.jdi.connect.IllegalConnectorArgumentsException;
 37 import com.sun.jdi.connect.Transport;
 38 import com.sun.jdi.connect.VMStartException;
 39 import com.sun.jdi.connect.spi.TransportService;
 40 
 41 public class SunCommandLineLauncher extends AbstractLauncher {
 42 
 43     static private final String ARG_HOME = "home";
 44     static private final String ARG_OPTIONS = "options";
 45     static private final String ARG_MAIN = "main";
 46     static private final String ARG_INIT_SUSPEND = "suspend";
 47     static private final String ARG_QUOTE = "quote";
 48     static private final String ARG_VM_EXEC = "vmexec";
 49 
 50     static private final String ARG_VM_ENUMERATE_VTHREADS = "enumeratevthreads";
 51     static private final String ARG_VM_NOTIFY_VTHREADS    = "notifyvthreads";
 52 
 53     TransportService transportService;
 54     Transport transport;
 55     boolean usingSharedMemory = false;
 56 
 57     TransportService transportService() {
 58         return transportService;
 59     }
 60 
 61     public Transport transport() {
 62         return transport;
 63     }
 64 
 65     public SunCommandLineLauncher() {
 66         super();
 67 
 68         /**
 69          * By default this connector uses either the shared memory
 70          * transport or the socket transport
 71          */
 72         try {
 73             transportService = (TransportService)Class.
 74                 forName("com.sun.tools.jdi.SharedMemoryTransportService").
 75                 getDeclaredConstructor().newInstance();
 76             transport = new Transport() {
 77                 public String name() {
 78                     return "dt_shmem";
 79                 }
 80             };
 81             usingSharedMemory = true;
 82         } catch (ClassNotFoundException |
 83                  UnsatisfiedLinkError |
 84                  InstantiationException |
 85                  InvocationTargetException |
 86                  IllegalAccessException |
 87                  NoSuchMethodException x) {
 88         };
 89         if (transportService == null) {
 90             transportService = new SocketTransportService();
 91             transport = new Transport() {
 92                 public String name() {
 93                     return "dt_socket";
 94                 }
 95             };
 96         }
 97 
 98         addStringArgument(
 99                 ARG_HOME,
100                 getString("sun.home.label"),
101                 getString("sun.home"),
102                 System.getProperty("java.home"),
103                 false);
104         addStringArgument(
105                 ARG_OPTIONS,
106                 getString("sun.options.label"),
107                 getString("sun.options"),
108                 "",
109                 false);
110         addStringArgument(
111                 ARG_MAIN,
112                 getString("sun.main.label"),
113                 getString("sun.main"),
114                 "",
115                 true);
116 
117         addBooleanArgument(
118                 ARG_INIT_SUSPEND,
119                 getString("sun.init_suspend.label"),
120                 getString("sun.init_suspend"),
121                 true,
122                 false);
123 
124         addStringArgument(
125                 ARG_QUOTE,
126                 getString("sun.quote.label"),
127                 getString("sun.quote"),
128                 "\"",
129                 true);
130         addStringArgument(
131                 ARG_VM_EXEC,
132                 getString("sun.vm_exec.label"),
133                 getString("sun.vm_exec"),
134                 "java",
135                 true);
136         addStringArgument(
137                 ARG_VM_ENUMERATE_VTHREADS,
138                 getString("sun.vm_enumerate_vthreads.label"),
139                 getString("sun.vm_enumerate_vthreads"),
140                 "n",
141                 false);
142         addStringArgument(
143                 ARG_VM_NOTIFY_VTHREADS,
144                 getString("sun.vm_notify_vthreads.label"),
145                 getString("sun.vm_notify_vthreads"),
146                 "y",
147                 false);
148     }
149 
150     static boolean hasWhitespace(String string) {
151         int length = string.length();
152         for (int i = 0; i < length; i++) {
153             if (Character.isWhitespace(string.charAt(i))) {
154                 return true;
155             }
156         }
157         return false;
158     }
159 
160     public VirtualMachine
161         launch(Map<String, ? extends Connector.Argument> arguments)
162         throws IOException, IllegalConnectorArgumentsException,
163                VMStartException
164     {
165         VirtualMachine vm;
166 
167         String home = argument(ARG_HOME, arguments).value();
168         String options = argument(ARG_OPTIONS, arguments).value();
169         String mainClassAndArgs = argument(ARG_MAIN, arguments).value();
170         boolean wait = ((BooleanArgumentImpl)argument(ARG_INIT_SUSPEND,
171                                                   arguments)).booleanValue();
172         String quote = argument(ARG_QUOTE, arguments).value();
173         String exe = argument(ARG_VM_EXEC, arguments).value();
174         String enumerateVThreads = argument(ARG_VM_ENUMERATE_VTHREADS, arguments).value();
175         String notifyVThreads = argument(ARG_VM_NOTIFY_VTHREADS, arguments).value();
176         String exePath = null;
177 
178         if (quote.length() > 1) {
179             throw new IllegalConnectorArgumentsException("Invalid length",
180                                                          ARG_QUOTE);
181         }
182 
183         if ((options.indexOf("-Djava.compiler=") != -1) &&
184             (options.toLowerCase().indexOf("-djava.compiler=none") == -1)) {
185             throw new IllegalConnectorArgumentsException("Cannot debug with a JIT compiler",
186                                                          ARG_OPTIONS);
187         }
188 
189         /*
190          * Start listening.
191          * If we're using the shared memory transport then we pick a
192          * random address rather than using the (fixed) default.
193          * Random() uses System.currentTimeMillis() as the seed
194          * which can be a problem on windows (many calls to
195          * currentTimeMillis can return the same value), so
196          * we do a few retries if we get an IOException (we
197          * assume the IOException is the filename is already in use.)
198          */
199         TransportService.ListenKey listenKey;
200         if (usingSharedMemory) {
201             Random rr = new Random();
202             int failCount = 0;
203             while(true) {
204                 try {
205                     String address = "javadebug" +
206                         String.valueOf(rr.nextInt(100000));
207                     listenKey = transportService().startListening(address);
208                     break;
209                 } catch (IOException ioe) {
210                     if (++failCount > 5) {
211                         throw ioe;
212                     }
213                 }
214             }
215         } else {
216             listenKey = transportService().startListening();
217         }
218         String address = listenKey.address();
219 
220         try {
221             if (home.length() > 0) {
222                 exePath = home + File.separator + "bin" + File.separator + exe;
223             } else {
224                 exePath = exe;
225             }
226             // Quote only if necessary in case the quote arg value is bogus
227             if (hasWhitespace(exePath)) {
228                 exePath = quote + exePath + quote;
229             }
230 
231             String xrun = "transport=" + transport().name() +
232                           ",address=" + address +
233                           ",suspend=" + (wait? 'y' : 'n') +
234                           ",enumeratevthreads=" + enumerateVThreads +
235                           ",notifyvthreads=" + notifyVThreads;
236             // Quote only if necessary in case the quote arg value is bogus
237             if (hasWhitespace(xrun)) {
238                 xrun = quote + xrun + quote;
239             }
240 
241             String command = exePath + ' ' +
242                              options + ' ' +
243                              "-Xdebug " +
244                              "-Xrunjdwp:" + xrun + ' ' +
245                              mainClassAndArgs;
246 
247             // System.err.println("Command: \"" + command + '"');
248             vm = launch(tokenizeCommand(command, quote.charAt(0)), address, listenKey,
249                         transportService());
250         } finally {
251             transportService().stopListening(listenKey);
252         }
253 
254         return vm;
255     }
256 
257     public String name() {
258         return "com.sun.jdi.CommandLineLaunch";
259     }
260 
261     public String description() {
262         return getString("sun.description");
263     }
264 }