< prev index next >

src/jdk.jdi/share/classes/com/sun/tools/jdi/SunCommandLineLauncher.java

Print this page

  1 /*
  2  * Copyright (c) 1998, 2017, 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

 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     TransportService transportService;
 51     Transport transport;
 52     boolean usingSharedMemory = false;
 53 
 54     TransportService transportService() {
 55         return transportService;
 56     }
 57 
 58     public Transport transport() {
 59         return transport;
 60     }
 61 
 62     public SunCommandLineLauncher() {
 63         super();
 64 
 65         /**
 66          * By default this connector uses either the shared memory
 67          * transport or the socket transport
 68          */
 69         try {

113 
114         addBooleanArgument(
115                 ARG_INIT_SUSPEND,
116                 getString("sun.init_suspend.label"),
117                 getString("sun.init_suspend"),
118                 true,
119                 false);
120 
121         addStringArgument(
122                 ARG_QUOTE,
123                 getString("sun.quote.label"),
124                 getString("sun.quote"),
125                 "\"",
126                 true);
127         addStringArgument(
128                 ARG_VM_EXEC,
129                 getString("sun.vm_exec.label"),
130                 getString("sun.vm_exec"),
131                 "java",
132                 true);












133     }
134 
135     static boolean hasWhitespace(String string) {
136         int length = string.length();
137         for (int i = 0; i < length; i++) {
138             if (Character.isWhitespace(string.charAt(i))) {
139                 return true;
140             }
141         }
142         return false;
143     }
144 
145     public VirtualMachine
146         launch(Map<String, ? extends Connector.Argument> arguments)
147         throws IOException, IllegalConnectorArgumentsException,
148                VMStartException
149     {
150         VirtualMachine vm;
151 
152         String home = argument(ARG_HOME, arguments).value();
153         String options = argument(ARG_OPTIONS, arguments).value();
154         String mainClassAndArgs = argument(ARG_MAIN, arguments).value();
155         boolean wait = ((BooleanArgumentImpl)argument(ARG_INIT_SUSPEND,
156                                                   arguments)).booleanValue();
157         String quote = argument(ARG_QUOTE, arguments).value();
158         String exe = argument(ARG_VM_EXEC, arguments).value();


159         String exePath = null;
160 
161         if (quote.length() > 1) {
162             throw new IllegalConnectorArgumentsException("Invalid length",
163                                                          ARG_QUOTE);
164         }
165 
166         if ((options.indexOf("-Djava.compiler=") != -1) &&
167             (options.toLowerCase().indexOf("-djava.compiler=none") == -1)) {
168             throw new IllegalConnectorArgumentsException("Cannot debug with a JIT compiler",
169                                                          ARG_OPTIONS);
170         }
171 
172         /*
173          * Start listening.
174          * If we're using the shared memory transport then we pick a
175          * random address rather than using the (fixed) default.
176          * Random() uses System.currentTimeMillis() as the seed
177          * which can be a problem on windows (many calls to
178          * currentTimeMillis can return the same value), so

196                 }
197             }
198         } else {
199             listenKey = transportService().startListening();
200         }
201         String address = listenKey.address();
202 
203         try {
204             if (home.length() > 0) {
205                 exePath = home + File.separator + "bin" + File.separator + exe;
206             } else {
207                 exePath = exe;
208             }
209             // Quote only if necessary in case the quote arg value is bogus
210             if (hasWhitespace(exePath)) {
211                 exePath = quote + exePath + quote;
212             }
213 
214             String xrun = "transport=" + transport().name() +
215                           ",address=" + address +
216                           ",suspend=" + (wait? 'y' : 'n');


217             // Quote only if necessary in case the quote arg value is bogus
218             if (hasWhitespace(xrun)) {
219                 xrun = quote + xrun + quote;
220             }
221 
222             String command = exePath + ' ' +
223                              options + ' ' +
224                              "-Xdebug " +
225                              "-Xrunjdwp:" + xrun + ' ' +
226                              mainClassAndArgs;
227 
228             // System.err.println("Command: \"" + command + '"');
229             vm = launch(tokenizeCommand(command, quote.charAt(0)), address, listenKey,
230                         transportService());
231         } finally {
232             transportService().stopListening(listenKey);
233         }
234 
235         return vm;
236     }

  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

 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 {

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

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     }
< prev index next >