jdk/test/com/sun/jdi/StepTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 3432 8acd97c69118
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 1999-2004 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 4270488 4787861
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @author Gordon Hirsch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @run build TestScaffold VMConnection TargetAdapter TargetListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @run compile -g -target 1.5 MethodCalls.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run compile -g MethodCallsReflection.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g ControlFlow.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run build StepTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @summary StepTest starts at a method named "go()" in the debuggee and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * repetitively steps. It will do a step into until the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * stack depth (specified on command line) is reached. At that point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * it begins to step over. Step granularity is determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * command line. Stepping is repeated the until the end of go() is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * or until the requested number of steps (specified on command line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is performed. An exception is thrown if the requested number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * steps does not result in the debuggee's location being at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * go().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This test is sensitive to the line number info generated by the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for the debuggee files- MethodCalls.java, ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * See LineNumberInfo.java for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *                     +--- maximum stack depth in debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *                     |  +--- step granularity: "line" or "min"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *                     |  |    +---Expected number of steps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *                     |  |    |  +--- Debuggee command Line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *                     V  V    V  V      Workaround-----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *                                                      V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  @run main StepTest 2 line  2 MethodCalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *  @run main StepTest 3 line 14 MethodCalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  @run main StepTest 2 line 18 MethodCallsReflection  12
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  @run main StepTest 2 min   4 MethodCalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  @run main StepTest 3 min  43 MethodCalls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *  @run main StepTest 2 line 65 ControlFlow            64
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
 * The workaround column contains the expected number of steps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * on non IA64 VMs.  These VMs get it wrong and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * fixed (4787861).  When they are fixed, this test should be fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * to remove this workaround.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The C interpreter in the IA64 VM handles catches differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * than the asm interpreter.  For the construct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * line    statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * 68      catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * 69          System.out.println(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * javac outputs the store into ee as being on line 68 and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * start of the println on line 69.  The handler starts with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * store into ee, ie, line 68.  When a step is done under the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * associated try and an exception is encountered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * the IA64 VM stops at line 68 while the other VM stops at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * line 69.  It seems that the IA64 VM is correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * But, it is too late to fix the other VM for Mantis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * so this test is being made so that it will pass both VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * For each catch that occurs, an extra step is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * on the IA64 VM.  This only occurs in MethodCallsReflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * which contains 6 of these catches (so we have to do 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * extra steps to make it pass) and in ControlFlow which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * does it once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
public class StepTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int maxDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    String granularity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    int expectedCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int workaroundCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    boolean lastStepNeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        new StepTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    StepTest(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        maxDepth = Integer.decode(args[0]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        granularity = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        expectedCount = Integer.decode(args[2]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (args.length == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            workaroundCount = Integer.decode(args[4]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Skip test args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String[] args2 = new String[args.length - 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.arraycopy(args, 3, args2, 0, args.length - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        connect(args2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ThreadReference thread = waitForVMStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        StepEvent stepEvent = stepIntoLine(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String className = thread.frame(0).location().declaringType().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.out.println("\n\n-------Running test for class: " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        BreakpointEvent bpEvent = resumeTo(className, "go", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        thread = bpEvent.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (int i = 0; i < expectedCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (thread.frameCount() < maxDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (granularity.equals("line")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    stepEvent = stepIntoLine(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    stepEvent = stepIntoInstruction(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (granularity.equals("line")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    stepEvent = stepOverLine(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    stepEvent = stepOverInstruction(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.out.println("Step #" + (i+1) + "complete at " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                               stepEvent.location().method().name() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                               stepEvent.location().lineNumber() + " (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                               stepEvent.location().codeIndex() + "), frameCount = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                               thread.frameCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (thread.frameCount() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                // We have stepped one step too far.  If we did exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // the 'workaround' number of steps, then this is in all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                // likelihood the non IA64 VM.  So, stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (i == workaroundCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    lastStepNeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // Gone too far, past return of go()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                throw new Exception("Stepped too far");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (thread.frameCount() > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // Not far enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new Exception("Didn't step far enough (" + thread.frame(0) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (lastStepNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // One last step takes us out of go()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            stepIntoLine(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (thread.frameCount() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // Gone too far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new Exception("Didn't step far enough (" + thread.frame(0) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Allow application to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        resumeToVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            println("StepTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new Exception("StepTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}