19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 28 import java.lang.ref.Reference; 29 import java.lang.ref.ReferenceQueue; 30 import java.lang.ref.WeakReference; 31 import java.security.AccessController; 32 import java.security.AccessControlContext; 33 import java.security.PrivilegedAction; 34 import java.util.Map; 35 import java.util.HashMap; 36 import java.util.concurrent.ConcurrentHashMap; 37 import java.util.concurrent.ConcurrentMap; 38 import java.util.concurrent.locks.LockSupport; 39 import sun.nio.ch.Interruptible; 40 import sun.reflect.CallerSensitive; 41 import sun.reflect.Reflection; 42 import sun.security.util.SecurityConstants; 43 44 45 /** 46 * A <i>thread</i> is a thread of execution in a program. The Java 47 * Virtual Machine allows an application to have multiple threads of 48 * execution running concurrently. 49 * <p> 50 * Every thread has a priority. Threads with higher priority are 51 * executed in preference to threads with lower priority. Each thread 52 * may or may not also be marked as a daemon. When code running in 53 * some thread creates a new <code>Thread</code> object, the new 54 * thread has its priority initially set equal to the priority of the 55 * creating thread, and is a daemon thread if and only if the 56 * creating thread is a daemon. 57 * <p> 58 * When a Java Virtual Machine starts up, there is usually a single 1574 security.checkPermission( 1575 SecurityConstants.GET_STACK_TRACE_PERMISSION); 1576 security.checkPermission( 1577 SecurityConstants.MODIFY_THREADGROUP_PERMISSION); 1578 } 1579 1580 // Get a snapshot of the list of all threads 1581 Thread[] threads = getThreads(); 1582 StackTraceElement[][] traces = dumpThreads(threads); 1583 Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length); 1584 for (int i = 0; i < threads.length; i++) { 1585 StackTraceElement[] stackTrace = traces[i]; 1586 if (stackTrace != null) { 1587 m.put(threads[i], stackTrace); 1588 } 1589 // else terminated so we don't put it in the map 1590 } 1591 return m; 1592 } 1593 1594 1595 private static final RuntimePermission SUBCLASS_IMPLEMENTATION_PERMISSION = 1596 new RuntimePermission("enableContextClassLoaderOverride"); 1597 1598 /** cache of subclass security audit results */ 1599 /* Replace with ConcurrentReferenceHashMap when/if it appears in a future 1600 * release */ 1601 private static class Caches { 1602 /** cache of subclass security audit results */ 1603 static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits = 1604 new ConcurrentHashMap<>(); 1605 1606 /** queue for WeakReferences to audited subclasses */ 1607 static final ReferenceQueue<Class<?>> subclassAuditsQueue = 1608 new ReferenceQueue<>(); 1609 } 1610 1611 /** 1612 * Verifies that this (possibly subclass) instance can be constructed 1613 * without violating security constraints: the subclass must not override | 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 28 import java.lang.ref.Reference; 29 import java.lang.ref.ReferenceQueue; 30 import java.lang.ref.WeakReference; 31 import java.security.AccessController; 32 import java.security.AccessControlContext; 33 import java.security.PrivilegedAction; 34 import java.util.Map; 35 import java.util.HashMap; 36 import java.util.concurrent.ConcurrentHashMap; 37 import java.util.concurrent.ConcurrentMap; 38 import java.util.concurrent.locks.LockSupport; 39 import java.util.function.Consumer; 40 import java.util.function.Function; 41 import java.util.function.Predicate; 42 import sun.nio.ch.Interruptible; 43 import sun.reflect.CallerSensitive; 44 import sun.reflect.Reflection; 45 import sun.security.util.SecurityConstants; 46 47 48 /** 49 * A <i>thread</i> is a thread of execution in a program. The Java 50 * Virtual Machine allows an application to have multiple threads of 51 * execution running concurrently. 52 * <p> 53 * Every thread has a priority. Threads with higher priority are 54 * executed in preference to threads with lower priority. Each thread 55 * may or may not also be marked as a daemon. When code running in 56 * some thread creates a new <code>Thread</code> object, the new 57 * thread has its priority initially set equal to the priority of the 58 * creating thread, and is a daemon thread if and only if the 59 * creating thread is a daemon. 60 * <p> 61 * When a Java Virtual Machine starts up, there is usually a single 1577 security.checkPermission( 1578 SecurityConstants.GET_STACK_TRACE_PERMISSION); 1579 security.checkPermission( 1580 SecurityConstants.MODIFY_THREADGROUP_PERMISSION); 1581 } 1582 1583 // Get a snapshot of the list of all threads 1584 Thread[] threads = getThreads(); 1585 StackTraceElement[][] traces = dumpThreads(threads); 1586 Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length); 1587 for (int i = 0; i < threads.length; i++) { 1588 StackTraceElement[] stackTrace = traces[i]; 1589 if (stackTrace != null) { 1590 m.put(threads[i], stackTrace); 1591 } 1592 // else terminated so we don't put it in the map 1593 } 1594 return m; 1595 } 1596 1597 // package-private 1598 static void checkStackWalkPermission() { 1599 // ## should it define a new "walkStack" permission? 1600 SecurityManager security = System.getSecurityManager(); 1601 if (security != null) { 1602 security.checkPermission( 1603 SecurityConstants.GET_STACK_TRACE_PERMISSION); 1604 } 1605 } 1606 1607 private static int MAX_STACK_DEPTH = 1024; 1608 /** 1609 * Performs the given action for each {@linkplain StackFrameInfo stack frame} 1610 * of the current thread. 1611 * <p> 1612 * The first stack frame is the top of the stack, which is the 1613 * last method invocation in the sequence. Some virtual machines may, 1614 * under some circumstances, omit one or more stack frames 1615 * from the stack trace. 1616 * 1617 * @param action a consumer traversing a stream of StackFrameInfo 1618 * @param depthLimit maximum number of frames traversed 1619 * @throws SecurityException if a security manager exists and its 1620 * {@code checkPermission} method denies access to traverse stack. 1621 * 1622 * @since 1.8 1623 */ 1624 public static void walkStack(Consumer<StackFrameInfo> action) { 1625 checkStackWalkPermission(); 1626 StackStream.build(MAX_STACK_DEPTH).walk(action); 1627 } 1628 1629 /** 1630 * Performs the given action for each {@linkplain StackFrameInfo stack frame} 1631 * of the current thread. This method traverses a maximum number of 1632 * {@code depthLimit} frames. 1633 * <p> 1634 * The first stack frame is the top of the stack, which is the 1635 * last method invocation in the sequence. Some virtual machines may, 1636 * under some circumstances, omit one or more stack frames from the stack trace. 1637 * 1638 * @param action a consumer traversing a stream of StackFrameInfo 1639 * @param depthLimit maximum number of frames traversed 1640 * @throws SecurityException if a security manager exists and its 1641 * {@code checkPermission} method denies access to traverse stack. 1642 * 1643 * @since 1.8 1644 */ 1645 public static void walkStack(Consumer<StackFrameInfo> action, int depthLimit) { 1646 checkStackWalkPermission(); 1647 StackStream.build(depthLimit).walk(action); 1648 } 1649 1650 /** 1651 * Performs the given action for the {@linkplain StackFrameInfo stack frames} 1652 * of the current thread that match the given predicate. This method 1653 * traverses a maximum number of {@code depthLimit} frames. 1654 * 1655 * @param predicate a predicate to apply to the stack walk 1656 * @param action a consumer traversing a stream of StackFrameInfo 1657 * @param depthLimit maximum number of frames traversed 1658 * 1659 * @throws SecurityException if a security manager exists and its 1660 * {@code checkPermission} method denies access to traverse stack. 1661 * 1662 * @since 1.8 1663 */ 1664 public static void walkStack(Predicate<StackFrameInfo> predicate, 1665 Consumer<StackFrameInfo> action, 1666 int depthLimit) { 1667 checkStackWalkPermission(); 1668 StackStream.build(depthLimit).walk(predicate, action); 1669 } 1670 1671 /** 1672 * Returns the result of applying the given function to 1673 * the caller frame that invokes the method calling this method. 1674 * The stack frames associated with {@code java.lang.reflect.Method#invoke 1675 * reflection} and its implementation are ignored. 1676 * 1677 * @param function a function to apply on the caller frame 1678 * 1679 * @return the result of applying the given function to 1680 * the caller frame that invokes the method calling this method. 1681 * 1682 * @throws SecurityException if a security manager exists and its 1683 * {@code checkPermission} method denies access to traverse stack. 1684 */ 1685 public static <T> T getCaller(Function<StackFrameInfo,T> function) { 1686 checkStackWalkPermission(); 1687 return StackStream.build(MAX_STACK_DEPTH).findCaller(function); 1688 } 1689 1690 /** 1691 * Returns the result of applying the given function to the first 1692 * caller matching the given predicate and skipping 1693 * the stack frames associated with {@linkplain java.lang.reflect.Method#invoke 1694 * reflection} and its implementation. 1695 * If there is no matching stack frame, {@code null} will be returned. 1696 * 1697 * @param predicate a predicate to apply to the stack walk 1698 * @param function a function to apply on the first stack frame 1699 * matching the given predicate 1700 * 1701 * @return the result of {@code function.apply(caller)} where {@code caller} 1702 * is the first stack frame, skipping the reflection implementation, 1703 * that matches the given predicate 1704 * 1705 * @throws SecurityException if a security manager exists and its 1706 * {@code checkPermission} method denies access to traverse stack. 1707 */ 1708 public static <T> T firstCaller(Predicate<StackFrameInfo> predicate, 1709 Function<StackFrameInfo,T> function) { 1710 checkStackWalkPermission(); 1711 return StackStream.build(MAX_STACK_DEPTH).findCaller(predicate, function); 1712 } 1713 1714 private static final RuntimePermission SUBCLASS_IMPLEMENTATION_PERMISSION = 1715 new RuntimePermission("enableContextClassLoaderOverride"); 1716 1717 /** cache of subclass security audit results */ 1718 /* Replace with ConcurrentReferenceHashMap when/if it appears in a future 1719 * release */ 1720 private static class Caches { 1721 /** cache of subclass security audit results */ 1722 static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits = 1723 new ConcurrentHashMap<>(); 1724 1725 /** queue for WeakReferences to audited subclasses */ 1726 static final ReferenceQueue<Class<?>> subclassAuditsQueue = 1727 new ReferenceQueue<>(); 1728 } 1729 1730 /** 1731 * Verifies that this (possibly subclass) instance can be constructed 1732 * without violating security constraints: the subclass must not override |