jdk/test/com/sun/jdi/MonitorFrameInfo.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 836 d81b1f62fb82
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 6230699
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Test ThreadReference.ownedMonitorsAndFrames()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author Swamy Venkataramanappa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g MonitorFrameInfo.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main MonitorFrameInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class MonitorTestTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void foo3() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        System.out.println("executing foo3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void foo2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Object l1 = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        synchronized(l1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            foo3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void foo1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        foo2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Object l1 = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        synchronized(l1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            foo1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class MonitorFrameInfo extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    List monitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static int expectedCount = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static int[] expectedDepth = { 1, 3 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static String[] expectedNames = {"foo3", "foo2", "foo1", "main"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    MonitorFrameInfo (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        new MonitorFrameInfo(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        BreakpointEvent bpe = startToMain("MonitorTestTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int initialSize = mainThread.frames().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        resumeTo("MonitorTestTarg", "foo3", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!mainThread.frame(0).location().method().name()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        .equals("foo3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            failure("frame failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (mainThread.frames().size() != (initialSize + 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            failure("frames size failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (mainThread.frames().size() != mainThread.frameCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            failure("frames size not equal to frameCount");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /* Test monitor frame info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (vm().canGetMonitorFrameInfo()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println("Get monitors");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            monitors = mainThread.ownedMonitorsAndFrames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (monitors.size() != expectedCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                failure("monitors count is not equal to expected count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            for (int j=0; j < monitors.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                MonitorInfo mon  = (MonitorInfo)monitors.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                System.out.println("Monitor obj " + mon.monitor() + "depth =" +mon.stackDepth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (mon.stackDepth() != expectedDepth[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    failure("monitor stack depth is not equal to expected depth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("can not get monitors frame info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * resume until end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            println("MonitorFrameInfo: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new Exception("MonitorFrameInfo: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}