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  * @author Robert Field
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g FramesTest.java
  32  * @run driver FramesTest
  33  */
  34 import com.sun.jdi.*;
  35 import com.sun.jdi.event.*;
  36 import com.sun.jdi.request.*;
  37 
  38 import java.util.*;
  39 
  40     /********** target program **********/
  41 
  42 class FramesTarg {
  43     static void foo3() {
  44         System.out.println("executing foo3");
  45     }
  46     static void foo2() {
  47         foo3();
  48     }
  49     static void foo1() {
  50         foo2();
  51     }
  52     public static void main(String[] args){
  53         System.out.println("Howdy!");
  54         foo1();
  55     }
  56 }
  57 
  58     /********** test program **********/
  59 
  60 public class FramesTest extends TestScaffold {
  61     ReferenceType targetClass;
  62     ThreadReference mainThread;
  63 
  64     static String[] expectedNames = {"foo3", "foo2", "foo1", "main"};
  65 
  66     FramesTest (String args[]) {
  67         super(args);
  68     }
  69 
  70     public static void main(String[] args)      throws Exception {
  71         new FramesTest(args).startTests();
  72     }
  73 
  74     /********** test assist **********/
  75 
  76     void exceptionTest(int start, int length) {
  77         boolean gotException = false;
  78         try {
  79             mainThread.frames(start, length);
  80         } catch (IndexOutOfBoundsException exc) {
  81             gotException = true;
  82         } catch (Exception exc) {
  83             failure("unexpected exception thrown for: " +
  84                     "start = " + start + ", length = " + length +
  85                     " - " + exc);
  86             gotException = true;
  87         }
  88         if (!gotException) {
  89             failure("expected IndexOutOfBoundsException " +
  90                     "not thrown for: " +
  91                     "start = " + start + ", length = " + length);
  92         }
  93     }
  94 
  95     void nameTest(int start, int length) {
  96         try {
  97             List fs = mainThread.frames(start, length);
  98             if (fs.size() != length) {
  99                 failure("wrong length for: " +
 100                         "start = " + start + ", length = " + length);
 101             }
 102             for (int i = 0; i < length; ++i) {
 103                 StackFrame sf = (StackFrame)(fs.get(i));
 104                 String name = sf.location().method().name();
 105                 String expected = expectedNames[start+i];
 106                 if (!name.equals(expected)) {
 107                     failure("bad frame entry (" + start + "," + length +
 108                             ") - expected " + expected +
 109                             ", got " + name);
 110                 }
 111             }
 112         } catch (Exception exc) {
 113             failure("unexpected exception thrown for: " +
 114                     "start = " + start + ", length = " + length +
 115                     " - " + exc);
 116         }
 117     }
 118 
 119     /********** test core **********/
 120 
 121     protected void runTests() throws Exception {
 122         /*
 123          * Get to the top of main()
 124          * to determine targetClass and mainThread
 125          */
 126         BreakpointEvent bpe = startToMain("FramesTarg");
 127         targetClass = bpe.location().declaringType();
 128         mainThread = bpe.thread();
 129 
 130         int initialSize = mainThread.frames().size();
 131 
 132         resumeTo("FramesTarg", "foo3", "()V");
 133 
 134         if (!mainThread.frame(0).location().method().name()
 135                         .equals("foo3")) {
 136             failure("frame failed");
 137         }
 138 
 139         if (mainThread.frames().size() != (initialSize + 3)) {
 140             failure("frames size failed");
 141         }
 142 
 143         if (mainThread.frames().size() != mainThread.frameCount()) {
 144             failure("frames size not equal to frameCount");
 145         }
 146 
 147         exceptionTest(-1, 1);
 148         exceptionTest(mainThread.frameCount(), 1);
 149         exceptionTest(0, -1);
 150         exceptionTest(0, -2);
 151         exceptionTest(0, mainThread.frameCount()+1);
 152 
 153         nameTest(0, 0);
 154         nameTest(0, 1);
 155         nameTest(0, 4);
 156         nameTest(2, 2);
 157         nameTest(1, 1);
 158 
 159         /*
 160          * resume until end
 161          */
 162         listenUntilVMDisconnect();
 163 
 164         /*
 165          * deal with results of test
 166          * if anything has called failure("foo") testFailed will be true
 167          */
 168         if (!testFailed) {
 169             println("FramesTest: passed");
 170         } else {
 171             throw new Exception("FramesTest: failed");
 172         }
 173     }
 174 }