jdk/test/com/sun/jdi/BacktraceFieldTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 5506 202f599c92aa
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 2001-2002 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 4446677
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary debuggee crashes when debugging under jbuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author jjh
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 BacktraceFieldTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main BacktraceFieldTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The fix for this bug filters out the backtrace field from the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * of fields for java.lang.Throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This test verifies that this really happens, and also verifies that the fix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * doesn't incorrectly discard other fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class Testy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This is used to verify that the fix doesn't filter out fields that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * shouldn't.  7 is an abitrary number, and this isn't a definitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * test; the fix could conceivably filter out the 89th field of a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * named Foo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * To verify that this part of this test works, first uncomment the field8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * line and verify that the test fails, and then rename a field to xxx and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * verify that the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int field1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int field2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int field3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int field4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int field5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int field6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    final static int field7 = 7;  // Value is the number of fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    //int field8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Testy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
class BacktraceFieldTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static void gus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Testy myTesty = new Testy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new RuntimeException("jjException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            gus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println("debuggee: Exception: " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
/********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class BacktraceFieldTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    BacktraceFieldTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        new BacktraceFieldTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * Get to the top of gus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * to determine mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        BreakpointEvent bpe = startTo("BacktraceFieldTarg", "gus", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * We are now one frame below the exception frame that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * our ee var.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        StackFrame myFrame = mainThread.frame(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        LocalVariable lv = myFrame.visibleVariableByName("ee");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        println("BT: lv = " + lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        println("BT: lvType = " + lv.typeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        List allFields = ((ReferenceType)(lv.type())).allFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        println("BT: allFields = " + allFields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Search through the fields of ee to verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * java.lang.Throwable.backtrace isn't there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Iterator iter = allFields.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        while(iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            Field ff = (Field)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (ff.toString().equals("java.lang.Throwable.backtrace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                failure("ERROR: java.lang.Throwable.backtrace field not filtered out.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                 * If you want to experience the segv this bug causes, change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                 * this test to 1 == 1 and run it with jdk 1.4, build 74 or earlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (1 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    // The following code will show the segv that this can cause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    ObjectReference myVal = (ObjectReference)myFrame.getValue(lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    println("BT: myVal = " + myVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    ArrayReference backTraceVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    backTraceVal = (ArrayReference)myVal.getValue(ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    println("BT: backTraceVal = " + backTraceVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    ArrayReference secondVal = (ArrayReference)backTraceVal.getValue(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    println("BT: secondVal = " + secondVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    Object x2Val = (Object)secondVal.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    println("BT: x2Val = " + x2Val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    ArrayReference firstVal = (ArrayReference)backTraceVal.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    println("BT: firstVal = " + firstVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // The segv happens here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    Object xVal = (Object)firstVal.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    println("BT: xVal = " + xVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                break;
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
        // Next, verify that we don't accidently discard a field that we shouldn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            lv = myFrame.visibleVariableByName("myTesty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            allFields = ((ReferenceType)(lv.type())).allFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            println("BT: allFields = " + allFields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (allFields.size() != Testy.field7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                failure("ERROR: wrong number of fields; expected " + Testy.field7 + ", Got " + allFields.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                iter = allFields.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                while(iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    String fieldName = ((Field)iter.next()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    if (!fieldName.startsWith("Testy.field", 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        failure("ERROR: Found bogus field: " + fieldName.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            println("BacktraceFieldTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new Exception("BacktraceFieldTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}