1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test
  26  *  @bug 4434232
  27  *  @summary Test ThreadReference.frames(int,int)
  28  *
  29  *  @author Robert Field
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g FramesTest.java
  34  *  @run driver FramesTest
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 
  42     /********** target program **********/
  43 
  44 class FramesTarg {
  45     static void foo3() {
  46         System.out.println("executing foo3");
  47     }
  48     static void foo2() {
  49         foo3();
  50     }
  51     static void foo1() {
  52         foo2();
  53     }
  54     public static void main(String[] args){
  55         System.out.println("Howdy!");
  56         foo1();
  57     }
  58 }
  59 
  60     /********** test program **********/
  61 
  62 public class FramesTest extends TestScaffold {
  63     ReferenceType targetClass;
  64     ThreadReference mainThread;
  65 
  66     static String[] expectedNames = {"foo3", "foo2", "foo1", "main"};
  67 
  68     FramesTest (String args[]) {
  69         super(args);
  70     }
  71 
  72     public static void main(String[] args)      throws Exception {
  73         new FramesTest(args).startTests();
  74     }
  75 
  76     /********** test assist **********/
  77 
  78     void exceptionTest(int start, int length) {
  79         boolean gotException = false;
  80         try {
  81             mainThread.frames(start, length);
  82         } catch (IndexOutOfBoundsException exc) {
  83             gotException = true;
  84         } catch (Exception exc) {
  85             failure("unexpected exception thrown for: " +
  86                     "start = " + start + ", length = " + length +
  87                     " - " + exc);
  88             gotException = true;
  89         }
  90         if (!gotException) {
  91             failure("expected IndexOutOfBoundsException " +
  92                     "not thrown for: " +
  93                     "start = " + start + ", length = " + length);
  94         }
  95     }
  96 
  97     void nameTest(int start, int length) {
  98         try {
  99             List fs = mainThread.frames(start, length);
 100             if (fs.size() != length) {
 101                 failure("wrong length for: " +
 102                         "start = " + start + ", length = " + length);
 103             }
 104             for (int i = 0; i < length; ++i) {
 105                 StackFrame sf = (StackFrame)(fs.get(i));
 106                 String name = sf.location().method().name();
 107                 String expected = expectedNames[start+i];
 108                 if (!name.equals(expected)) {
 109                     failure("bad frame entry (" + start + "," + length +
 110                             ") - expected " + expected +
 111                             ", got " + name);
 112                 }
 113             }
 114         } catch (Exception exc) {
 115             failure("unexpected exception thrown for: " +
 116                     "start = " + start + ", length = " + length +
 117                     " - " + exc);
 118         }
 119     }
 120 
 121     /********** test core **********/
 122 
 123     protected void runTests() throws Exception {
 124         /*
 125          * Get to the top of main()
 126          * to determine targetClass and mainThread
 127          */
 128         BreakpointEvent bpe = startToMain("FramesTarg");
 129         targetClass = bpe.location().declaringType();
 130         mainThread = bpe.thread();
 131 
 132         int initialSize = mainThread.frames().size();
 133 
 134         resumeTo("FramesTarg", "foo3", "()V");
 135 
 136         if (!mainThread.frame(0).location().method().name()
 137                         .equals("foo3")) {
 138             failure("frame failed");
 139         }
 140 
 141         if (mainThread.frames().size() != (initialSize + 3)) {
 142             failure("frames size failed");
 143         }
 144 
 145         if (mainThread.frames().size() != mainThread.frameCount()) {
 146             failure("frames size not equal to frameCount");
 147         }
 148 
 149         exceptionTest(-1, 1);
 150         exceptionTest(mainThread.frameCount(), 1);
 151         exceptionTest(0, -1);
 152         exceptionTest(0, -2);
 153         exceptionTest(0, mainThread.frameCount()+1);
 154 
 155         nameTest(0, 0);
 156         nameTest(0, 1);
 157         nameTest(0, 4);
 158         nameTest(2, 2);
 159         nameTest(1, 1);
 160 
 161         /*
 162          * resume until end
 163          */
 164         listenUntilVMDisconnect();
 165 
 166         /*
 167          * deal with results of test
 168          * if anything has called failure("foo") testFailed will be true
 169          */
 170         if (!testFailed) {
 171             println("FramesTest: passed");
 172         } else {
 173             throw new Exception("FramesTest: failed");
 174         }
 175     }
 176 }