Path: blob/master/test/jdk/com/sun/jdi/BacktraceFieldTest.java
41149 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 444667726* @bug 815823727* @summary debuggee used to crash when debugging under jbuilder28* @author jjh29*30* @run build TestScaffold VMConnection TargetListener TargetAdapter31* @run compile -g BacktraceFieldTest.java32* @run driver BacktraceFieldTest33*/3435/*36* The fix for this bug filters out the backtrace field from the list37* of fields for java.lang.Throwable.38* This test verifies that this really happens, and also verifies that the fix39* doesn't incorrectly discard other fields.40*/4142import com.sun.jdi.*;43import com.sun.jdi.event.*;44import com.sun.jdi.request.*;4546import java.util.*;4748/********** target program **********/4950class Testy {51/*52* This is used to verify that the fix doesn't filter out fields that it53* shouldn't. 7 is an abitrary number, and this isn't a definitive54* test; the fix could conceivably filter out the 89th field of a class55* named Foo.56* To verify that this part of this test works, first uncomment the field857* line and verify that the test fails, and then rename a field to xxx and58* verify that the test fails.59*/60int field1;61int field2;62int field3;63int field4;64int field5;65int field6;66final static int field7 = 7; // Value is the number of fields.67//int field8;6869Testy() {70}71}727374class BacktraceFieldTarg {75public static void gus() {76}7778public static void main(String[] args) {79Testy myTesty = new Testy();80try {81throw new RuntimeException("jjException");82} catch (Exception ee) {83gus();84System.out.println("debuggee: Exception: " + ee);85}86}87}8889/********** test program **********/9091public class BacktraceFieldTest extends TestScaffold {92ThreadReference mainThread;9394BacktraceFieldTest (String args[]) {95super(args);96}9798public static void main(String[] args) throws Exception {99new BacktraceFieldTest(args).startTests();100}101102private void printval(ArrayReference backTraceVal, int index) throws Exception {103ArrayReference val = (ArrayReference)backTraceVal.getValue(index);104println("BT: val at " + index + " = " + val);105106// The segv used to happen here for index = 0107// Now all objects in the backtrace are objects.108Object xVal = (Object)val.getValue(0);109println("BT: xVal = " + xVal);110}111112/********** test core **********/113114protected void runTests() throws Exception {115/*116* Get to the top of gus()117* to determine mainThread118*/119BreakpointEvent bpe = startTo("BacktraceFieldTarg", "gus", "()V");120mainThread = bpe.thread();121122/*123* We are now one frame below the exception frame that contains124* our ee var.125*/126StackFrame myFrame = mainThread.frame(1);127128LocalVariable lv = myFrame.visibleVariableByName("ee");129println("BT: lv = " + lv);130println("BT: lvType = " + lv.typeName());131132List allFields = ((ReferenceType)(lv.type())).allFields();133println("BT: allFields = " + allFields);134135/*136* Search through the fields of ee to verify that137* java.lang.Throwable.backtrace isn't there.138*/139boolean backtrace_found = false;140Iterator iter = allFields.iterator();141while(iter.hasNext()) {142Field ff = (Field)iter.next();143if (ff.toString().equals("java.lang.Throwable.backtrace")) {144backtrace_found = true;145println("java.lang.Throwable.backtrace field not filtered out.");146147/*148* If you want to experience the segv this bug causes, change149* this test to 1 == 1 and run it with jdk 1.4, build 74 or earlier150*/151if (1 == 1) {152// The following code will show the segv that this can cause.153ObjectReference myVal = (ObjectReference)myFrame.getValue(lv);154println("BT: myVal = " + myVal);155156ArrayReference backTraceVal = (ArrayReference)myVal.getValue(ff);157println("BT: backTraceVal = " + backTraceVal);158159printval(backTraceVal, 0);160printval(backTraceVal, 1);161printval(backTraceVal, 2);162printval(backTraceVal, 3); // backtrace has 4 elements163164try {165printval(backTraceVal, 4);166} catch (Exception e) {167println("Exception " + e);168}169}170break;171}172}173174if (!backtrace_found) {175failure("ERROR: java.lang.Throwable.backtrace field filtered out.");176}177178// Next, verify that we don't accidently discard a field that we shouldn't179180if (!testFailed) {181lv = myFrame.visibleVariableByName("myTesty");182183allFields = ((ReferenceType)(lv.type())).allFields();184println("BT: allFields = " + allFields);185186if (allFields.size() != Testy.field7) {187failure("ERROR: wrong number of fields; expected " + Testy.field7 + ", Got " + allFields.size());188} else {189iter = allFields.iterator();190while(iter.hasNext()) {191String fieldName = ((Field)iter.next()).toString();192if (!fieldName.startsWith("Testy.field", 0)) {193failure("ERROR: Found bogus field: " + fieldName.toString());194}195}196}197}198199listenUntilVMDisconnect();200201/*202* deal with results of test203* if anything has called failure("foo") testFailed will be true204*/205if (!testFailed) {206println("BacktraceFieldTest: passed");207} else {208throw new Exception("BacktraceFieldTest: failed");209}210}211}212213214