aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/jdi/ConnectorImpl.java
blob: ea51292def452a51d08f349350faf50ff5f5bf55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package sun.jvm.hotspot.jdi;

import com.sun.jdi.connect.*;
import com.sun.jdi.InternalException;

import java.io.*;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.util.*;

abstract class ConnectorImpl implements Connector {
    Map defaultArguments = new LinkedHashMap();

    // Used by BooleanArgument
    static String trueString = null;
    static String falseString;


    /**  This is not public in VirtualMachineManagerImpl
    ThreadGroup mainGroupForJDI() {
        return ((VirtualMachineManagerImpl)manager).mainGroupForJDI();
    }
    ***/

    // multiple debuggee support for SA/JDI
    private static List freeVMClasses; // List<SoftReference<Class>>
    private static ClassLoader myLoader;
    // debug mode for SA/JDI connectors
    static final protected boolean DEBUG;
    static {
        myLoader = ConnectorImpl.class.getClassLoader();
        freeVMClasses = new ArrayList(0);
        DEBUG = System.getProperty("sun.jvm.hotspot.jdi.ConnectorImpl.DEBUG") != null;
    }

    // add a new free VirtualMachineImpl class
    private static synchronized void addFreeVMImplClass(Class clazz) {
        if (DEBUG) {
            System.out.println("adding free VirtualMachineImpl class");
        }
        freeVMClasses.add(new SoftReference(clazz));
    }

    // returns null if we don't have anything free
    private static synchronized Class getFreeVMImplClass() {
        while (!freeVMClasses.isEmpty()) {
              SoftReference ref = (SoftReference) freeVMClasses.remove(0);
              Object o = ref.get();
              if (o != null) {
                  if (DEBUG) {
                      System.out.println("re-using loaded VirtualMachineImpl");
                  }
                  return (Class) o;
              }
        }
        return null;
    }

    private static Class getVMImplClassFrom(ClassLoader cl)
                               throws ClassNotFoundException {
        return Class.forName("sun.jvm.hotspot.jdi.VirtualMachineImpl", true, cl);
    }

    /* SA has not been designed to support multiple debuggee VMs
     * at-a-time.  But, JDI supports multiple debuggee VMs.  We
     * support multiple debuggee VMs in SA/JDI, by creating a new
     * class loader instance (refer to comment in SAJDIClassLoader
     * for details). But, to avoid excessive class loading (and
     * thereby resulting in larger footprint), we re-use 'dispose'd
     * VirtualMachineImpl classes.
     */
    protected static Class loadVirtualMachineImplClass()
                               throws ClassNotFoundException {
        Class vmImplClass = getFreeVMImplClass();
        if (vmImplClass == null) {
            ClassLoader cl = new SAJDIClassLoader(myLoader);
            vmImplClass = getVMImplClassFrom(cl);
        }
        return vmImplClass;
    }

    /* We look for System property sun.jvm.hotspot.jdi.<vm version>.
     * This property should have the value of JDK HOME directory for
     * the given <vm version>.
     */
    private static String getSAClassPathForVM(String vmVersion) {
        final String prefix = "sun.jvm.hotspot.jdi.";
        // look for exact match of VM version
        String jvmHome = System.getProperty(prefix + vmVersion);
        if (DEBUG) {
            System.out.println("looking for System property " + prefix + vmVersion);
        }

        if (jvmHome == null) {
            // omit chars after first '-' in VM version and try
            // for example, in '1.5.0-b55' we take '1.5.0'
            int index = vmVersion.indexOf('-');
            if (index != -1) {
                vmVersion = vmVersion.substring(0, index);
                if (DEBUG) {
                    System.out.println("looking for System property " + prefix + vmVersion);
                }
                jvmHome = System.getProperty(prefix + vmVersion);
            }

            if (jvmHome == null) {
                // System property is not set
                if (DEBUG) {
                    System.out.println("can't locate JDK home for " + vmVersion);
                }
                return null;
            }
        }

        if (DEBUG) {
            System.out.println("JDK home for " + vmVersion + " is " + jvmHome);
        }

        // sa-jdi is in $JDK_HOME/lib directory
        StringBuffer buf = new StringBuffer();
        buf.append(jvmHome);
        buf.append(File.separatorChar);
        buf.append("lib");
        buf.append(File.separatorChar);
        buf.append("sa-jdi.jar");
        return buf.toString();
    }

    /* This method loads VirtualMachineImpl class by a ClassLoader
     * configured with sa-jdi.jar path of given 'vmVersion'. This is
     * used for cross VM version debugging. Refer to comments in
     * SAJDIClassLoader as well.
     */
    protected static Class loadVirtualMachineImplClass(String vmVersion)
            throws ClassNotFoundException {
        if (DEBUG) {
            System.out.println("attemping to load sa-jdi.jar for version " + vmVersion);
        }
        String classPath = getSAClassPathForVM(vmVersion);
        if (classPath != null) {
            ClassLoader cl = new SAJDIClassLoader(myLoader, classPath);
            return getVMImplClassFrom(cl);
        } else {
            return null;
        }
    }

    /* Is the given throwable an instanceof VMVersionMismatchException?
     * Note that we can't do instanceof check because the exception
     * class might have been loaded by a different class loader.
     */
    private static boolean isVMVersionMismatch(Throwable throwable) {
        String className = throwable.getClass().getName();
        return className.equals("sun.jvm.hotspot.runtime.VMVersionMismatchException");
    }

    /* gets target VM version from the given VMVersionMismatchException.
     * Note that we need to reflectively call the method because of we may
     * have got this from different classloader's namespace */
    private static String getVMVersion(Throwable throwable)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // assert isVMVersionMismatch(throwable), "not a VMVersionMismatch"
        Class expClass = throwable.getClass();
        Method targetVersionMethod = expClass.getMethod("getTargetVersion", new Class[0]);
        return (String) targetVersionMethod.invoke(throwable, null);
    }

    /** If the causal chain has a sun.jvm.hotspot.runtime.VMVersionMismatchException,
        attempt to load VirtualMachineImpl class for target VM version. */
    protected static Class handleVMVersionMismatch(InvocationTargetException ite) {
        Throwable cause = ite.getCause();
        if (DEBUG) {
            System.out.println("checking for version mismatch...");
        }
        while (cause != null) {
            try {
                if (isVMVersionMismatch(cause)) {
                    if (DEBUG) {
                        System.out.println("Triggering cross VM version support...");
                    }
                    return loadVirtualMachineImplClass(getVMVersion(cause));
                }
            } catch (Exception exp) {
                if (DEBUG) {
                    System.out.println("failed to load VirtualMachineImpl class");
                    exp.printStackTrace();
                }
                return null;
            }
            cause = cause.getCause();
        }
        return null;
    }

    protected void checkNativeLink(SecurityManager sm, String os) {
        if (os.equals("SunOS") || os.equals("Linux")) {
            // link "saproc" - SA native library on SunOS and Linux?
            sm.checkLink("saproc");
        } else if (os.startsWith("Windows")) {
            // link "sawindbg" - SA native library on Windows.
            sm.checkLink("sawindbg");
        } else {
           throw new RuntimeException(os + " is not yet supported");
        }
    }

    // we set an observer to detect VirtualMachineImpl.dispose call
    // and on dispose we add corresponding VirtualMachineImpl.class to
    // free VirtualMachimeImpl Class list.
    protected static void setVMDisposeObserver(final Object vm) {
        try {
            Method setDisposeObserverMethod = vm.getClass().getDeclaredMethod("setDisposeObserver",
                                                         new Class[] { java.util.Observer.class });
            setDisposeObserverMethod.setAccessible(true);
            setDisposeObserverMethod.invoke(vm,
                                         new Object[] {
                                             new Observer() {
                                                 public void update(Observable o, Object data) {
                                                     if (DEBUG) {
                                                         System.out.println("got VM.dispose notification");
                                                     }
                                                     addFreeVMImplClass(vm.getClass());
                                                 }
                                             }
                                         });
        } catch (Exception exp) {
            if (DEBUG) {
               System.out.println("setVMDisposeObserver() got an exception:");
               exp.printStackTrace();
            }
        }
    }

    public Map defaultArguments() {
        Map defaults = new LinkedHashMap();
        Collection values = defaultArguments.values();

        Iterator iter = values.iterator();
        while (iter.hasNext()) {
            ArgumentImpl argument = (ArgumentImpl)iter.next();
            defaults.put(argument.name(), argument.clone());
        }
        return defaults;
    }

    void addStringArgument(String name, String label, String description,
                           String defaultValue, boolean mustSpecify) {
        defaultArguments.put(name,
                             new StringArgumentImpl(name, label,
                                                    description,
                                                    defaultValue,
                                                    mustSpecify));
    }

    void addBooleanArgument(String name, String label, String description,
                            boolean defaultValue, boolean mustSpecify) {
        defaultArguments.put(name,
                             new BooleanArgumentImpl(name, label,
                                                     description,
                                                     defaultValue,
                                                     mustSpecify));
    }

    void addIntegerArgument(String name, String label, String description,
                            String defaultValue, boolean mustSpecify,
                            int min, int max) {
        defaultArguments.put(name,
                             new IntegerArgumentImpl(name, label,
                                                     description,
                                                     defaultValue,
                                                     mustSpecify,
                                                     min, max));
    }

    void addSelectedArgument(String name, String label, String description,
                             String defaultValue, boolean mustSpecify,
                             List list) {
        defaultArguments.put(name,
                             new SelectedArgumentImpl(name, label,
                                                      description,
                                                      defaultValue,
                                                      mustSpecify, list));
    }

    ArgumentImpl argument(String name, Map arguments)
                throws IllegalConnectorArgumentsException {

        ArgumentImpl argument = (ArgumentImpl)arguments.get(name);
        if (argument == null) {
            throw new IllegalConnectorArgumentsException(
                         "Argument missing", name);
        }
        String value = argument.value();
        if (value == null || value.length() == 0) {
            if (argument.mustSpecify()) {
            throw new IllegalConnectorArgumentsException(
                         "Argument unspecified", name);
            }
        } else if(!argument.isValid(value)) {
            throw new IllegalConnectorArgumentsException(
                         "Argument invalid", name);
        }

        return argument;
    }

    String getString(String key) {
        //fixme jjh; needs i18n
        // this is not public return ((VirtualMachineManagerImpl)manager).getString(key);
        return key;
    }

    public String toString() {
        String string = name() + " (defaults: ";
        Iterator iter = defaultArguments().values().iterator();
        boolean first = true;
        while (iter.hasNext()) {
            ArgumentImpl argument = (ArgumentImpl)iter.next();
            if (!first) {
                string += ", ";
            }
            string += argument.toString();
            first = false;
        }
        return string  + ")";
    }

    abstract class ArgumentImpl implements Connector.Argument, Cloneable, Serializable {
        private String name;
        private String label;
        private String description;
        private String value;
        private boolean mustSpecify;

        ArgumentImpl(String name, String label, String description,
                     String value,
                     boolean mustSpecify) {
            this.name = name;
            this.label = label;
            this.description = description;
            this.value = value;
            this.mustSpecify = mustSpecify;
        }

        public abstract boolean isValid(String value);

        public String name() {
            return name;
        }

        public String label() {
            return label;
        }

        public String description() {
            return description;
        }

        public String value() {
            return value;
        }

        public void setValue(String value) {
            if (value == null) {
                throw new NullPointerException("Can't set null value");
            }
            this.value = value;
        }

        public boolean mustSpecify() {
            return mustSpecify;
        }

        public boolean equals(Object obj) {
            if ((obj != null) && (obj instanceof Connector.Argument)) {
                Connector.Argument other = (Connector.Argument)obj;
                return (name().equals(other.name())) &&
                       (description().equals(other.description())) &&
                       (mustSpecify() == other.mustSpecify()) &&
                       (value().equals(other.value()));
            } else {
                return false;
            }
        }

        public int hashCode() {
            return description().hashCode();
        }

        public Object clone() {
            try {
                return super.clone();
            } catch (CloneNotSupportedException e) {
                // Object should always support clone
                throw (InternalException) new InternalException().initCause(e);
            }
        }

        public String toString() {
            return name() + "=" + value();
        }
    }

    class BooleanArgumentImpl extends ConnectorImpl.ArgumentImpl
                              implements Connector.BooleanArgument {

        BooleanArgumentImpl(String name, String label, String description,
                            boolean value,
                            boolean mustSpecify) {
            super(name, label, description, null, mustSpecify);
            if(trueString == null) {
                trueString = getString("true");
                falseString = getString("false");
            }
            setValue(value);
        }

        /**
         * Sets the value of the argument.
         */
        public void setValue(boolean value) {
            setValue(stringValueOf(value));
        }

        /**
         * Performs basic sanity check of argument.
         * @return <code>true</code> if value is a string
         * representation of a boolean value.
         * @see #stringValueOf(boolean)
         */
        public boolean isValid(String value) {
            return value.equals(trueString) || value.equals(falseString);
        }

        /**
         * Return the string representation of the <code>value</code>
         * parameter.
         * Does not set or examine the value or the argument.
         * @return the localized String representation of the
         * boolean value.
         */
        public String stringValueOf(boolean value) {
            return value? trueString : falseString;
        }

        /**
         * Return the value of the argument as a boolean.  Since
         * the argument may not have been set or may have an invalid
         * value {@link #isValid(String)} should be called on
         * {@link #value()} to check its validity.  If it is invalid
         * the boolean returned by this method is undefined.
         * @return the value of the argument as a boolean.
         */
        public boolean booleanValue() {
            return value().equals(trueString);
        }
    }

    class IntegerArgumentImpl extends ConnectorImpl.ArgumentImpl
                              implements Connector.IntegerArgument {

        private final int min;
        private final int max;

        IntegerArgumentImpl(String name, String label, String description,
                            String value,
                            boolean mustSpecify, int min, int max) {
            super(name, label, description, value, mustSpecify);
            this.min = min;
            this.max = max;
        }

        /**
         * Sets the value of the argument.
         * The value should be checked with {@link #isValid(int)}
         * before setting it; invalid values will throw an exception
         * when the connection is established - for example,
         * on {@link LaunchingConnector#launch}
         */
        public void setValue(int value) {
            setValue(stringValueOf(value));
        }

        /**
         * Performs basic sanity check of argument.
         * @return <code>true</code> if value represents an int that is
         * <code>{@link #min()} &lt;= value &lt;= {@link #max()}</code>
         */
        public boolean isValid(String value) {
            if (value == null) {
                return false;
            }
            try {
                return isValid(Integer.decode(value).intValue());
            } catch(NumberFormatException exc) {
                return false;
            }
        }

        /**
         * Performs basic sanity check of argument.
         * @return <code>true</code> if
         * <code>{@link #min()} &lt;= value  &lt;= {@link #max()}</code>
         */
        public boolean isValid(int value) {
            return min <= value && value <= max;
        }

        /**
         * Return the string representation of the <code>value</code>
         * parameter.
         * Does not set or examine the value or the argument.
         * @return the String representation of the
         * int value.
         */
        public String stringValueOf(int value) {
            // *** Should this be internationalized????
            // *** Even Brian Beck was unsure if an Arabic programmer
            // *** would expect port numbers in Arabic numerals,
            // *** so punt for now.
            return ""+value;
        }

        /**
         * Return the value of the argument as a int.  Since
         * the argument may not have been set or may have an invalid
         * value {@link #isValid(String)} should be called on
         * {@link #value()} to check its validity.  If it is invalid
         * the int returned by this method is undefined.
         * @return the value of the argument as a int.
         */
        public int intValue() {
            if (value() == null) {
                return 0;
            }
            try {
                return Integer.decode(value()).intValue();
            } catch(NumberFormatException exc) {
                return 0;
            }
        }

        /**
         * The upper bound for the value.
         * @return the maximum allowed value for this argument.
         */
        public int max() {
            return max;
        }

        /**
         * The lower bound for the value.
         * @return the minimum allowed value for this argument.
         */
        public int min() {
            return min;
        }
    }

    class StringArgumentImpl extends ConnectorImpl.ArgumentImpl
                              implements Connector.StringArgument {

        StringArgumentImpl(String name, String label, String description,
                           String value,
                           boolean mustSpecify) {
            super(name, label, description, value, mustSpecify);
        }

        /**
         * Performs basic sanity check of argument.
         * @return <code>true</code> always
         */
        public boolean isValid(String value) {
            return true;
        }
    }

    class SelectedArgumentImpl extends ConnectorImpl.ArgumentImpl
                              implements Connector.SelectedArgument {

        private final List choices;

        SelectedArgumentImpl(String name, String label, String description,
                             String value,
                             boolean mustSpecify, List choices) {
            super(name, label, description, value, mustSpecify);
            this.choices = Collections.unmodifiableList(
                                           new ArrayList(choices));
        }

        /**
         * Return the possible values for the argument
         * @return {@link List} of {@link String}
         */
        public List choices() {
            return choices;
        }

        /**
         * Performs basic sanity check of argument.
         * @return <code>true</code> if value is one of {@link #choices()}.
         */
        public boolean isValid(String value) {
            return choices.contains(value);
        }
    }
}