jdk/test/com/sun/tools/attach/AttachSelf.java
author alanb
Thu, 04 May 2017 07:26:55 +0000
changeset 45004 ea3137042a61
permissions -rw-r--r--
8178380: Module system implementation refresh (5/2017) Reviewed-by: mchung, alanb, sspitsyn Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, chris.hegarty@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     1
/*
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     4
 *
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     8
 *
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    13
 * accompanied this code).
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    14
 *
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    18
 *
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    21
 * questions.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    22
 */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    23
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    24
import com.sun.tools.attach.VirtualMachine;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    25
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    26
import java.io.IOException;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    27
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    28
/**
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    29
 * @test
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    30
 * @modules jdk.attach
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    31
 * @run main AttachSelf
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    32
 * @run main/othervm -Djdk.attach.allowAttachSelf AttachSelf
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    33
 * @run main/othervm -Djdk.attach.allowAttachSelf=true AttachSelf
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    34
 * @run main/othervm -Djdk.attach.allowAttachSelf=false AttachSelf
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    35
 */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    36
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    37
public class AttachSelf {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    38
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    40
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    41
        String value = System.getProperty("jdk.attach.allowAttachSelf");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    42
        boolean canAttachSelf = (value != null) && !value.equals("false");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    43
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    44
        String vmid = "" + ProcessHandle.current().pid();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    45
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    46
        VirtualMachine vm = null;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    47
        try {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    48
            vm = VirtualMachine.attach(vmid);
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    49
            if (!canAttachSelf)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    50
                throw new RuntimeException("Attached to self not expected");
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    51
        } catch (IOException ioe) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    52
            if (canAttachSelf)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    53
                throw ioe;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    54
        } finally {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    55
            if (vm != null) vm.detach();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    56
        }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    57
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    58
    }
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    59
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    60
}
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents:
diff changeset
    61