1 /*
 2  * Copyright (c) 2020, 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 package jdk.jfr.jvm;
25 
26 import jdk.jfr.FlightRecorder;
27 import jdk.jfr.internal.JVM;
28 import jdk.test.lib.Asserts;
29 
30 /**
31  * @test TestGetStackTraceId
32  * @key jfr
33  * @requires vm.hasJFR
34  * @library /test/lib
35  * @modules jdk.jfr/jdk.jfr.internal
36  * @run main/othervm jdk.jfr.jvm.TestGetStackTraceId
37  */
38 public class TestGetStackTraceId {
39 
40     public static void main(String... args) {
41         FlightRecorder.getFlightRecorder();
42         JVM jvm = JVM.getJVM();
43 
44         long id10 = getStackIdOfDepth(10);
45         assertValid(id10);
46 
47         long id5 = getStackIdOfDepth(5);
48         assertValid(id5);
49 
50         Asserts.assertNotEquals(id5, id10, "Different stack depth must return different stack trace ids");
51 
52         assertMaxSkip(jvm);
53     }
54 
55     private static void assertMaxSkip(JVM jvm) {
56         Asserts.assertEquals(jvm.getStackTraceId(Integer.MAX_VALUE), 0L, "Insane skip level " + Integer.MAX_VALUE + " should not return a valid stack trace id");
57     }
58 
59     private static void assertValid(long value) {
60         Asserts.assertGreaterThan(value, 0L, "Stack trace id must be greater than 0, was " + value);
61     }
62 
63     public static long getStackIdOfDepth(int depth) {
64         if (depth > 0) {
65             return getStackIdOfDepth(depth - 1);
66         }
67         return JVM.getJVM().getStackTraceId(0);
68     }
69 }