BacktraceFieldTest.java revision 11884:b45c81ca8671
1277323Sdim/*
2277323Sdim * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
3353358Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4353358Sdim *
5353358Sdim * This code is free software; you can redistribute it and/or modify it
6277323Sdim * under the terms of the GNU General Public License version 2 only, as
7277323Sdim * published by the Free Software Foundation.
8277323Sdim *
9277323Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10277323Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11277323Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12309124Sdim * version 2 for more details (a copy is included in the LICENSE file that
13321369Sdim * accompanied this code).
14327952Sdim *
15277323Sdim * You should have received a copy of the GNU General Public License version
16309124Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17277323Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18296417Sdim *
19277323Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20277323Sdim * or visit www.oracle.com if you need additional information or have any
21327952Sdim * questions.
22296417Sdim */
23341825Sdim
24277323Sdim/**
25341825Sdim *  @test
26341825Sdim *  @bug 4446677
27341825Sdim *  @summary debuggee crashes when debugging under jbuilder
28277323Sdim *
29277323Sdim *  @author jjh
30296417Sdim *
31296417Sdim *  @modules jdk.jdi
32296417Sdim *  @run build TestScaffold VMConnection TargetListener TargetAdapter
33277323Sdim *  @run compile -g BacktraceFieldTest.java
34309124Sdim *  @run driver BacktraceFieldTest
35277323Sdim */
36277323Sdim
37/*
38 * The fix for this bug filters out the backtrace field from the list
39 * of fields for java.lang.Throwable.
40 * This test verifies that this really happens, and also verifies that the fix
41 * doesn't incorrectly discard other fields.
42 */
43
44import com.sun.jdi.*;
45import com.sun.jdi.event.*;
46import com.sun.jdi.request.*;
47
48import java.util.*;
49
50/********** target program **********/
51
52class Testy {
53    /*
54     * This is used to verify that the fix doesn't filter out fields that it
55     * shouldn't.  7 is an abitrary number, and this isn't a definitive
56     * test; the fix could conceivably filter out the 89th field of a class
57     * named Foo.
58     * To verify that this part of this test works, first uncomment the field8
59     * line and verify that the test fails, and then rename a field to xxx and
60     * verify that the test fails.
61     */
62    int field1;
63    int field2;
64    int field3;
65    int field4;
66    int field5;
67    int field6;
68    final static int field7 = 7;  // Value is the number of fields.
69    //int field8;
70
71    Testy() {
72    }
73}
74
75
76class BacktraceFieldTarg {
77    public static void gus() {
78    }
79
80    public static void main(String[] args) {
81        Testy myTesty = new Testy();
82        try {
83            throw new RuntimeException("jjException");
84        } catch (Exception ee) {
85            gus();
86            System.out.println("debuggee: Exception: " + ee);
87        }
88    }
89}
90
91/********** test program **********/
92
93public class BacktraceFieldTest extends TestScaffold {
94    ThreadReference mainThread;
95
96    BacktraceFieldTest (String args[]) {
97        super(args);
98    }
99
100    public static void main(String[] args)      throws Exception {
101        new BacktraceFieldTest(args).startTests();
102    }
103
104    /********** test core **********/
105
106    protected void runTests() throws Exception {
107        /*
108         * Get to the top of gus()
109         * to determine mainThread
110         */
111        BreakpointEvent bpe = startTo("BacktraceFieldTarg", "gus", "()V");
112        mainThread = bpe.thread();
113
114        /*
115         * We are now one frame below the exception frame that contains
116         * our ee var.
117         */
118        StackFrame myFrame = mainThread.frame(1);
119
120        LocalVariable lv = myFrame.visibleVariableByName("ee");
121        println("BT: lv = " + lv);
122        println("BT: lvType = " + lv.typeName());
123
124        List allFields = ((ReferenceType)(lv.type())).allFields();
125        println("BT: allFields = " + allFields);
126
127        /*
128         * Search through the fields of ee to verify that
129         * java.lang.Throwable.backtrace isn't there.
130         */
131        Iterator iter = allFields.iterator();
132        while(iter.hasNext()) {
133            Field ff = (Field)iter.next();
134            if (ff.toString().equals("java.lang.Throwable.backtrace")) {
135                failure("ERROR: java.lang.Throwable.backtrace field not filtered out.");
136
137                /*
138                 * If you want to experience the segv this bug causes, change
139                 * this test to 1 == 1 and run it with jdk 1.4, build 74 or earlier
140                 */
141                if (1 == 0) {
142                    // The following code will show the segv that this can cause.
143                    ObjectReference myVal = (ObjectReference)myFrame.getValue(lv);
144                    println("BT: myVal = " + myVal);
145
146                    ArrayReference backTraceVal = null;
147                    backTraceVal = (ArrayReference)myVal.getValue(ff);
148                    println("BT: backTraceVal = " + backTraceVal);
149
150                    ArrayReference secondVal = (ArrayReference)backTraceVal.getValue(1);
151                    println("BT: secondVal = " + secondVal);
152
153                    Object x2Val = (Object)secondVal.getValue(0);
154                    println("BT: x2Val = " + x2Val);
155
156                    ArrayReference firstVal = (ArrayReference)backTraceVal.getValue(0);
157                    println("BT: firstVal = " + firstVal);
158
159                    // The segv happens here.
160                    Object xVal = (Object)firstVal.getValue(0);
161                    println("BT: xVal = " + xVal);
162                }
163                break;
164            }
165        }
166
167        // Next, verify that we don't accidently discard a field that we shouldn't
168
169        if (!testFailed) {
170            lv = myFrame.visibleVariableByName("myTesty");
171
172            allFields = ((ReferenceType)(lv.type())).allFields();
173            println("BT: allFields = " + allFields);
174
175            if (allFields.size() != Testy.field7) {
176                failure("ERROR: wrong number of fields; expected " + Testy.field7 + ", Got " + allFields.size());
177            } else {
178                iter = allFields.iterator();
179                while(iter.hasNext()) {
180                    String fieldName = ((Field)iter.next()).toString();
181                    if (!fieldName.startsWith("Testy.field", 0)) {
182                        failure("ERROR: Found bogus field: " + fieldName.toString());
183                    }
184                }
185            }
186        }
187
188        listenUntilVMDisconnect();
189
190        /*
191         * deal with results of test
192         * if anything has called failure("foo") testFailed will be true
193         */
194        if (!testFailed) {
195            println("BacktraceFieldTest: passed");
196        } else {
197            throw new Exception("BacktraceFieldTest: failed");
198        }
199    }
200}
201