< prev index next >

src/hotspot/share/classfile/javaClasses.cpp

Print this page

 178                            const char* name_string, Symbol* signature_symbol,
 179                            bool is_static = false) {
 180   TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
 181   if (name == NULL) {
 182     ResourceMark rm;
 183     log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
 184     vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name());
 185   }
 186   compute_offset(dest_offset, ik, name, signature_symbol, is_static);
 187 }
 188 
 189 
 190 #if INCLUDE_CDS
 191 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
 192   f->do_u4((u4*)&offset)
 193 #endif
 194 
 195 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
 196   compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
 197 


















































































































































































































































 198 
 199 // java_lang_String
 200 
 201 int java_lang_String::_value_offset;
 202 int java_lang_String::_hash_offset;
 203 int java_lang_String::_hashIsZero_offset;
 204 int java_lang_String::_coder_offset;
 205 int java_lang_String::_flags_offset;
 206 
 207 bool java_lang_String::_initialized;
 208 
 209 
 210 bool java_lang_String::test_and_set_flag(oop java_string, uint8_t flag_mask) {
 211   uint8_t* addr = flags_addr(java_string);
 212   uint8_t value = Atomic::load(addr);
 213   while ((value & flag_mask) == 0) {
 214     uint8_t old_value = value;
 215     value |= flag_mask;
 216     value = Atomic::cmpxchg(addr, old_value, value);
 217     if (value == old_value) return false; // Flag bit changed from 0 to 1.

1653 
1654 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1655   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1656   return the_class_mirror->int_field(_classRedefinedCount_offset);
1657 }
1658 
1659 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1660   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1661   the_class_mirror->int_field_put(_classRedefinedCount_offset, value);
1662 }
1663 
1664 
1665 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1666 //       platform thread structure, and a eetop offset which was used for thread
1667 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1668 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1669 //       instead of the privateInfo_offset.
1670 //
1671 // Note: The stackSize field is only present starting in 1.4.
1672 























































































































1673 int java_lang_Thread::_name_offset;
1674 int java_lang_Thread::_group_offset;
1675 int java_lang_Thread::_contextClassLoader_offset;
1676 int java_lang_Thread::_inheritedAccessControlContext_offset;
1677 int java_lang_Thread::_priority_offset;
1678 int java_lang_Thread::_eetop_offset;

1679 int java_lang_Thread::_interrupted_offset;
1680 int java_lang_Thread::_daemon_offset;
1681 int java_lang_Thread::_stillborn_offset;
1682 int java_lang_Thread::_stackSize_offset;
1683 int java_lang_Thread::_tid_offset;
1684 int java_lang_Thread::_thread_status_offset;
1685 int java_lang_Thread::_park_blocker_offset;

1686 
1687 #define THREAD_FIELDS_DO(macro) \

1688   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1689   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1690   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1691   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1692   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1693   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1694   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1695   macro(_interrupted_offset,   k, "interrupted", bool_signature, false); \
1696   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1697   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1698   macro(_tid_offset,           k, "tid", long_signature, false); \
1699   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1700   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false)

1701 
1702 void java_lang_Thread::compute_offsets() {
1703   assert(_group_offset == 0, "offsets should be initialized only once");
1704 
1705   InstanceKlass* k = vmClasses::Thread_klass();
1706   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);

1707 }
1708 
1709 #if INCLUDE_CDS
1710 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
1711   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);

1712 }
1713 #endif
1714 
1715 JavaThread* java_lang_Thread::thread(oop java_thread) {
1716   return (JavaThread*)java_thread->address_field(_eetop_offset);
1717 }
1718 
1719 
1720 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
1721   java_thread->address_field_put(_eetop_offset, (address)thread);
1722 }
1723 
















1724 bool java_lang_Thread::interrupted(oop java_thread) {
1725   // Make sure the caller can safely access oops.
1726   assert(Thread::current()->is_VM_thread() ||
1727          (JavaThread::current()->thread_state() != _thread_blocked &&
1728           JavaThread::current()->thread_state() != _thread_in_native),
1729          "Unsafe access to oop");
1730   return java_thread->bool_field_volatile(_interrupted_offset);
1731 }
1732 
1733 void java_lang_Thread::set_interrupted(oop java_thread, bool val) {
1734   // Make sure the caller can safely access oops.
1735   assert(Thread::current()->is_VM_thread() ||
1736          (JavaThread::current()->thread_state() != _thread_blocked &&
1737           JavaThread::current()->thread_state() != _thread_in_native),
1738          "Unsafe access to oop");
1739   java_thread->bool_field_put_volatile(_interrupted_offset, val);
1740 }
1741 
1742 
1743 oop java_lang_Thread::name(oop java_thread) {
1744   return java_thread->obj_field(_name_offset);
1745 }
1746 
1747 
1748 void java_lang_Thread::set_name(oop java_thread, oop name) {
1749   java_thread->obj_field_put(_name_offset, name);
1750 }
1751 
1752 
1753 ThreadPriority java_lang_Thread::priority(oop java_thread) {
1754   return (ThreadPriority)java_thread->int_field(_priority_offset);


1755 }
1756 
1757 
1758 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
1759   java_thread->int_field_put(_priority_offset, priority);


1760 }
1761 
1762 
1763 oop java_lang_Thread::threadGroup(oop java_thread) {
1764   return java_thread->obj_field(_group_offset);


1765 }
1766 
1767 
1768 bool java_lang_Thread::is_stillborn(oop java_thread) {
1769   return java_thread->bool_field(_stillborn_offset) != 0;


1770 }
1771 
1772 
1773 // We never have reason to turn the stillborn bit off
1774 void java_lang_Thread::set_stillborn(oop java_thread) {
1775   java_thread->bool_field_put(_stillborn_offset, true);


1776 }
1777 
1778 
1779 bool java_lang_Thread::is_alive(oop java_thread) {
1780   JavaThread* thr = java_lang_Thread::thread(java_thread);
1781   return (thr != NULL);
1782 }
1783 
1784 
1785 bool java_lang_Thread::is_daemon(oop java_thread) {
1786   return java_thread->bool_field(_daemon_offset) != 0;


1787 }
1788 
1789 
1790 void java_lang_Thread::set_daemon(oop java_thread) {
1791   java_thread->bool_field_put(_daemon_offset, true);


1792 }
1793 
1794 oop java_lang_Thread::context_class_loader(oop java_thread) {
1795   return java_thread->obj_field(_contextClassLoader_offset);
1796 }
1797 
1798 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
1799   return java_thread->obj_field(_inheritedAccessControlContext_offset);
1800 }
1801 
1802 
1803 jlong java_lang_Thread::stackSize(oop java_thread) {
1804   return java_thread->long_field(_stackSize_offset);


1805 }
1806 
1807 // Write the thread status value to threadStatus field in java.lang.Thread java class.
1808 void java_lang_Thread::set_thread_status(oop java_thread,
1809                                          JavaThreadStatus status) {
1810   java_thread->int_field_put(_thread_status_offset, static_cast<int>(status));

1811 }
1812 
1813 // Read thread status value from threadStatus field in java.lang.Thread java class.
1814 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1815   // Make sure the caller is operating on behalf of the VM or is
1816   // running VM code (state == _thread_in_vm).
1817   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1818          JavaThread::current()->thread_state() == _thread_in_vm,
1819          "Java Thread is not running in vm");
1820   return static_cast<JavaThreadStatus>(java_thread->int_field(_thread_status_offset));





1821 }
1822 
1823 
1824 jlong java_lang_Thread::thread_id(oop java_thread) {
1825   return java_thread->long_field(_tid_offset);







1826 }
1827 
1828 oop java_lang_Thread::park_blocker(oop java_thread) {
1829   return java_thread->obj_field(_park_blocker_offset);
1830 }
1831 





















































































































1832 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1833   JavaThreadStatus status = static_cast<JavaThreadStatus>(java_thread->int_field(_thread_status_offset));


1834   switch (status) {
1835     case JavaThreadStatus::NEW                      : return "NEW";
1836     case JavaThreadStatus::RUNNABLE                 : return "RUNNABLE";
1837     case JavaThreadStatus::SLEEPING                 : return "TIMED_WAITING (sleeping)";
1838     case JavaThreadStatus::IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1839     case JavaThreadStatus::IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1840     case JavaThreadStatus::PARKED                   : return "WAITING (parking)";
1841     case JavaThreadStatus::PARKED_TIMED             : return "TIMED_WAITING (parking)";
1842     case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1843     case JavaThreadStatus::TERMINATED               : return "TERMINATED";
1844     default                       : return "UNKNOWN";
1845   };
1846 }
1847 int java_lang_ThreadGroup::_parent_offset;
1848 int java_lang_ThreadGroup::_name_offset;
1849 int java_lang_ThreadGroup::_threads_offset;
1850 int java_lang_ThreadGroup::_groups_offset;
1851 int java_lang_ThreadGroup::_maxPriority_offset;
1852 int java_lang_ThreadGroup::_destroyed_offset;
1853 int java_lang_ThreadGroup::_daemon_offset;
1854 int java_lang_ThreadGroup::_nthreads_offset;
1855 int java_lang_ThreadGroup::_ngroups_offset;



1856 
1857 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
1858   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1859   return java_thread_group->obj_field(_parent_offset);
1860 }
1861 
1862 // ("name as oop" accessor is not necessary)
1863 
1864 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
1865   oop name = java_thread_group->obj_field(_name_offset);
1866   // ThreadGroup.name can be null
1867   if (name != NULL) {
1868     return java_lang_String::as_utf8_string(name);
1869   }
1870   return NULL;
1871 }
1872 
1873 int java_lang_ThreadGroup::nthreads(oop java_thread_group) {
1874   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1875   return java_thread_group->int_field(_nthreads_offset);
1876 }
1877 
1878 objArrayOop java_lang_ThreadGroup::threads(oop java_thread_group) {
1879   oop threads = java_thread_group->obj_field(_threads_offset);
1880   assert(threads != NULL, "threadgroups should have threads");
1881   assert(threads->is_objArray(), "just checking"); // Todo: Add better type checking code
1882   return objArrayOop(threads);
1883 }
1884 
1885 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
1886   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1887   return java_thread_group->int_field(_ngroups_offset);
1888 }
1889 
1890 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
1891   oop groups = java_thread_group->obj_field(_groups_offset);
1892   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
1893   return objArrayOop(groups);
1894 }
1895 
1896 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
1897   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1898   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
1899 }
1900 
1901 bool java_lang_ThreadGroup::is_destroyed(oop java_thread_group) {
1902   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1903   return java_thread_group->bool_field(_destroyed_offset) != 0;
1904 }
1905 
1906 bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) {
1907   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1908   return java_thread_group->bool_field(_daemon_offset) != 0;

1909 }
1910 
1911 #define THREADGROUP_FIELDS_DO(macro) \
1912   macro(_parent_offset,      k, vmSymbols::parent_name(),      threadgroup_signature,       false); \
1913   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature,            false); \
1914   macro(_threads_offset,     k, vmSymbols::threads_name(),     thread_array_signature,      false); \
1915   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
1916   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
1917   macro(_destroyed_offset,   k, vmSymbols::destroyed_name(),   bool_signature,              false); \
1918   macro(_daemon_offset,      k, vmSymbols::daemon_name(),      bool_signature,              false); \
1919   macro(_nthreads_offset,    k, vmSymbols::nthreads_name(),    int_signature,               false); \
1920   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false)
1921 
1922 void java_lang_ThreadGroup::compute_offsets() {
1923   assert(_parent_offset == 0, "offsets should be initialized only once");
1924 
1925   InstanceKlass* k = vmClasses::ThreadGroup_klass();
1926   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1927 }
1928 
1929 #if INCLUDE_CDS
1930 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
1931   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1932 }
1933 #endif
1934 




























































































1935 // java_lang_Throwable
1936 
1937 int java_lang_Throwable::_backtrace_offset;
1938 int java_lang_Throwable::_detailMessage_offset;
1939 int java_lang_Throwable::_stackTrace_offset;
1940 int java_lang_Throwable::_depth_offset;
1941 int java_lang_Throwable::_cause_offset;
1942 int java_lang_Throwable::_static_unassigned_stacktrace_offset;
1943 
1944 #define THROWABLE_FIELDS_DO(macro) \
1945   macro(_backtrace_offset,     k, "backtrace",     object_signature,                  false); \
1946   macro(_detailMessage_offset, k, "detailMessage", string_signature,                  false); \
1947   macro(_stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
1948   macro(_depth_offset,         k, "depth",         int_signature,                     false); \
1949   macro(_cause_offset,         k, "cause",         throwable_signature,               false); \
1950   macro(_static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
1951 
1952 void java_lang_Throwable::compute_offsets() {
1953   InstanceKlass* k = vmClasses::Throwable_klass();
1954   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);

2017 
2018 void java_lang_Throwable::print(oop throwable, outputStream* st) {
2019   ResourceMark rm;
2020   Klass* k = throwable->klass();
2021   assert(k != NULL, "just checking");
2022   st->print("%s", k->external_name());
2023   oop msg = message(throwable);
2024   if (msg != NULL) {
2025     st->print(": %s", java_lang_String::as_utf8_string(msg));
2026   }
2027 }
2028 
2029 // After this many redefines, the stack trace is unreliable.
2030 const int MAX_VERSION = USHRT_MAX;
2031 
2032 static inline bool version_matches(Method* method, int version) {
2033   assert(version < MAX_VERSION, "version is too big");
2034   return method != NULL && (method->constants()->version() == version);
2035 }
2036 
2037 // This class provides a simple wrapper over the internal structure of
2038 // exception backtrace to insulate users of the backtrace from needing
2039 // to know what it looks like.
2040 // The code of this class is not GC safe. Allocations can only happen
2041 // in expand().
2042 class BacktraceBuilder: public StackObj {
2043  friend class BacktraceIterator;
2044  private:
2045   Handle          _backtrace;
2046   objArrayOop     _head;
2047   typeArrayOop    _methods;
2048   typeArrayOop    _bcis;
2049   objArrayOop     _mirrors;
2050   typeArrayOop    _names; // Needed to insulate method name against redefinition.
2051   // True if the top frame of the backtrace is omitted because it shall be hidden.
2052   bool            _has_hidden_top_frame;
2053   int             _index;
2054   NoSafepointVerifier _nsv;
2055 
2056   enum {
2057     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
2058     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
2059     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
2060     trace_names_offset   = java_lang_Throwable::trace_names_offset,
2061     trace_next_offset    = java_lang_Throwable::trace_next_offset,
2062     trace_hidden_offset  = java_lang_Throwable::trace_hidden_offset,
2063     trace_size           = java_lang_Throwable::trace_size,
2064     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
2065   };
2066 
2067   // get info out of chunks
2068   static typeArrayOop get_methods(objArrayHandle chunk) {
2069     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
2070     assert(methods != NULL, "method array should be initialized in backtrace");
2071     return methods;
2072   }
2073   static typeArrayOop get_bcis(objArrayHandle chunk) {
2074     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
2075     assert(bcis != NULL, "bci array should be initialized in backtrace");
2076     return bcis;
2077   }
2078   static objArrayOop get_mirrors(objArrayHandle chunk) {
2079     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
2080     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
2081     return mirrors;
2082   }
2083   static typeArrayOop get_names(objArrayHandle chunk) {
2084     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
2085     assert(names != NULL, "names array should be initialized in backtrace");
2086     return names;
2087   }
2088   static bool has_hidden_top_frame(objArrayHandle chunk) {
2089     oop hidden = chunk->obj_at(trace_hidden_offset);
2090     return hidden != NULL;
2091   }
2092 
2093  public:
2094 
2095   // constructor for new backtrace
2096   BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL), _has_hidden_top_frame(false) {
2097     expand(CHECK);
2098     _backtrace = Handle(THREAD, _head);
2099     _index = 0;
2100   }
2101 
2102   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
2103     _methods = get_methods(backtrace);
2104     _bcis = get_bcis(backtrace);
2105     _mirrors = get_mirrors(backtrace);
2106     _names = get_names(backtrace);
2107     _has_hidden_top_frame = has_hidden_top_frame(backtrace);
2108     assert(_methods->length() == _bcis->length() &&
2109            _methods->length() == _mirrors->length() &&
2110            _mirrors->length() == _names->length(),
2111            "method and source information arrays should match");
2112 
2113     // head is the preallocated backtrace
2114     _head = backtrace();
2115     _backtrace = Handle(thread, _head);
2116     _index = 0;
2117   }
2118 
2119   void expand(TRAPS) {
2120     objArrayHandle old_head(THREAD, _head);
2121     PauseNoSafepointVerifier pnsv(&_nsv);
2122 
2123     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
2124     objArrayHandle new_head(THREAD, head);
2125 
2126     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
2127     typeArrayHandle new_methods(THREAD, methods);
2128 
2129     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
2130     typeArrayHandle new_bcis(THREAD, bcis);
2131 
2132     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
2133     objArrayHandle new_mirrors(THREAD, mirrors);
2134 
2135     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
2136     typeArrayHandle new_names(THREAD, names);
2137 
2138     if (!old_head.is_null()) {
2139       old_head->obj_at_put(trace_next_offset, new_head());
2140     }
2141     new_head->obj_at_put(trace_methods_offset, new_methods());
2142     new_head->obj_at_put(trace_bcis_offset, new_bcis());
2143     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
2144     new_head->obj_at_put(trace_names_offset, new_names());
2145     new_head->obj_at_put(trace_hidden_offset, NULL);
2146 
2147     _head    = new_head();
2148     _methods = new_methods();
2149     _bcis = new_bcis();
2150     _mirrors = new_mirrors();
2151     _names  = new_names();
2152     _index = 0;
2153   }
2154 
2155   oop backtrace() {
2156     return _backtrace();
2157   }
2158 
2159   inline void push(Method* method, int bci, TRAPS) {
2160     // Smear the -1 bci to 0 since the array only holds unsigned
2161     // shorts.  The later line number lookup would just smear the -1
2162     // to a 0 even if it could be recorded.
2163     if (bci == SynchronizationEntryBCI) bci = 0;
2164 
2165     if (_index >= trace_chunk_size) {
2166       methodHandle mhandle(THREAD, method);
2167       expand(CHECK);
2168       method = mhandle();
2169     }
2170 
2171     _methods->ushort_at_put(_index, method->orig_method_idnum());
2172     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
2173 
2174     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
2175     // klass owning the symbols alive so their refcounts aren't decremented.
2176     Symbol* name = method->name();
2177     _names->symbol_at_put(_index, name);
2178 
2179     // We need to save the mirrors in the backtrace to keep the class
2180     // from being unloaded while we still have this stack trace.
2181     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
2182     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
2183     _index++;
2184   }
2185 
2186   void set_has_hidden_top_frame() {
2187     if (!_has_hidden_top_frame) {
2188       // It would be nice to add java/lang/Boolean::TRUE here
2189       // to indicate that this backtrace has a hidden top frame.
2190       // But this code is used before TRUE is allocated.
2191       // Therefore let's just use an arbitrary legal oop
2192       // available right here. _methods is a short[].
2193       assert(_methods != NULL, "we need a legal oop");
2194       _has_hidden_top_frame = true;
2195       _head->obj_at_put(trace_hidden_offset, _methods);
2196     }
2197   }
2198 };
2199 
2200 struct BacktraceElement : public StackObj {
2201   int _method_id;
2202   int _bci;
2203   int _version;
2204   Symbol* _name;
2205   Handle _mirror;
2206   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name) :
2207                    _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror) {}
2208 };
2209 
2210 class BacktraceIterator : public StackObj {
2211   int _index;
2212   objArrayHandle  _result;
2213   objArrayHandle  _mirrors;
2214   typeArrayHandle _methods;
2215   typeArrayHandle _bcis;
2216   typeArrayHandle _names;
2217 
2218   void init(objArrayHandle result, Thread* thread) {
2219     // Get method id, bci, version and mirror from chunk
2220     _result = result;
2221     if (_result.not_null()) {
2222       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
2223       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
2224       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
2225       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));
2226       _index = 0;
2227     }
2228   }
2229  public:
2230   BacktraceIterator(objArrayHandle result, Thread* thread) {
2231     init(result, thread);
2232     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
2233   }
2234 
2235   BacktraceElement next(Thread* thread) {
2236     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
2237                         _methods->ushort_at(_index),
2238                         Backtrace::version_at(_bcis->int_at(_index)),
2239                         Backtrace::bci_at(_bcis->int_at(_index)),
2240                         _names->symbol_at(_index));
2241     _index++;
2242 
2243     if (_index >= java_lang_Throwable::trace_chunk_size) {
2244       int next_offset = java_lang_Throwable::trace_next_offset;
2245       // Get next chunk
2246       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
2247       init(result, thread);
2248     }
2249     return e;
2250   }
2251 
2252   bool repeat() {
2253     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
2254   }
2255 };
2256 
2257 
2258 // Print stack trace element to resource allocated buffer
2259 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2260                                           int version, int bci, Symbol* name) {
2261   ResourceMark rm;
2262 
2263   // Get strings and string lengths
2264   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2265   const char* klass_name  = holder->external_name();
2266   int buf_len = (int)strlen(klass_name);
2267 
2268   char* method_name = name->as_C_string();
2269   buf_len += (int)strlen(method_name);
2270 
2271   char* source_file_name = NULL;
2272   Symbol* source = Backtrace::get_source_file_name(holder, version);
2273   if (source != NULL) {
2274     source_file_name = source->as_C_string();
2275     buf_len += (int)strlen(source_file_name);
2276   }
2277 

2386   }
2387 }
2388 
2389 /**
2390  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2391  */
2392 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2393   assert(throwable->is_a(vmClasses::Throwable_klass()), "Throwable instance expected");
2394   JavaValue result(T_VOID);
2395   JavaCalls::call_virtual(&result,
2396                           throwable,
2397                           vmClasses::Throwable_klass(),
2398                           vmSymbols::printStackTrace_name(),
2399                           vmSymbols::void_method_signature(),
2400                           THREAD);
2401 }
2402 
2403 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
2404   if (!StackTraceInThrowable) return;
2405   ResourceMark rm(THREAD);

2406 
2407   // Start out by clearing the backtrace for this object, in case the VM
2408   // runs out of memory while allocating the stack trace
2409   set_backtrace(throwable(), NULL);
2410   // Clear lazily constructed Java level stacktrace if refilling occurs
2411   // This is unnecessary in 1.7+ but harmless
2412   clear_stacktrace(throwable());
2413 
2414   int max_depth = MaxJavaStackTraceDepth;
2415   JavaThread* thread = THREAD;
2416 
2417   BacktraceBuilder bt(CHECK);
2418 
2419   // If there is no Java frame just return the method that was being called
2420   // with bci 0
2421   if (!thread->has_last_Java_frame()) {
2422     if (max_depth >= 1 && method() != NULL) {
2423       bt.push(method(), 0, CHECK);
2424       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2425       set_depth(throwable(), 1);
2426       set_backtrace(throwable(), bt.backtrace());
2427     }
2428     return;
2429   }
2430 
2431   // Instead of using vframe directly, this version of fill_in_stack_trace
2432   // basically handles everything by hand. This significantly improved the
2433   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2434   // See bug 6333838 for  more details.
2435   // The "ASSERT" here is to verify this method generates the exactly same stack
2436   // trace as utilizing vframe.
2437 #ifdef ASSERT
2438   vframeStream st(thread, false /* stop_at_java_call_stub */, false /* process_frames */);
2439 #endif
2440   int total_count = 0;
2441   RegisterMap map(thread, false /* update */, false /* process_frames */);
2442   int decode_offset = 0;
2443   CompiledMethod* nm = NULL;
2444   bool skip_fillInStackTrace_check = false;
2445   bool skip_throwableInit_check = false;
2446   bool skip_hidden = !ShowHiddenFrames;
2447 

2448   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2449     Method* method = NULL;
2450     int bci = 0;

2451 
2452     // Compiled java method case.
2453     if (decode_offset != 0) {
2454       DebugInfoReadStream stream(nm, decode_offset);
2455       decode_offset = stream.read_int();
2456       method = (Method*)nm->metadata_at(stream.read_int());
2457       bci = stream.read_bci();
2458     } else {
2459       if (fr.is_first_frame()) break;









2460       address pc = fr.pc();
2461       if (fr.is_interpreted_frame()) {
2462         address bcp = fr.interpreter_frame_bcp();
2463         method = fr.interpreter_frame_method();






2464         bci =  method->bci_from(bcp);
2465         fr = fr.sender(&map);
2466       } else {
2467         CodeBlob* cb = fr.cb();
2468         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2469         // but non nmethod
2470         fr = fr.sender(&map);
2471         if (cb == NULL || !cb->is_compiled()) {
2472           continue;
2473         }
2474         nm = cb->as_compiled_method();

2475         if (nm->method()->is_native()) {
2476           method = nm->method();
2477           bci = 0;
2478         } else {
2479           PcDesc* pd = nm->pc_desc_at(pc);
2480           decode_offset = pd->scope_decode_offset();
2481           // if decode_offset is not equal to 0, it will execute the
2482           // "compiled java method case" at the beginning of the loop.
2483           continue;
2484         }
2485       }
2486     }
2487 #ifdef ASSERT
2488     assert(st.method() == method && st.bci() == bci,
2489            "Wrong stack trace");
2490     st.next();

2491 #endif
2492 
2493     // the format of the stacktrace will be:
2494     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2495     // - 0 or more <init> methods for the exception class (skipped)
2496     // - rest of the stack
2497 
2498     if (!skip_fillInStackTrace_check) {
2499       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2500           throwable->is_a(method->method_holder())) {
2501         continue;
2502       }
2503       else {
2504         skip_fillInStackTrace_check = true; // gone past them all
2505       }
2506     }
2507     if (!skip_throwableInit_check) {
2508       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2509 
2510       // skip <init> methods of the exception class and superclasses
2511       // This is simlar to classic VM.
2512       if (method->name() == vmSymbols::object_initializer_name() &&
2513           throwable->is_a(method->method_holder())) {
2514         continue;
2515       } else {
2516         // there are none or we've seen them all - either way stop checking
2517         skip_throwableInit_check = true;
2518       }
2519     }
2520     if (method->is_hidden()) {
2521       if (skip_hidden) {
2522         if (total_count == 0) {
2523           // The top frame will be hidden from the stack trace.
2524           bt.set_has_hidden_top_frame();
2525         }
2526         continue;
2527       }
2528     }
2529     bt.push(method, bci, CHECK);

2530     total_count++;
2531   }
2532 
2533   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2534 
2535   // Put completed stack trace into throwable object
2536   set_backtrace(throwable(), bt.backtrace());
2537   set_depth(throwable(), total_count);
2538 }
2539 
2540 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2541   // No-op if stack trace is disabled
2542   if (!StackTraceInThrowable) {
2543     return;
2544   }
2545 
2546   // Disable stack traces for some preallocated out of memory errors
2547   if (!Universe::should_fill_in_stack_trace(throwable)) {
2548     return;
2549   }

2574   if (!StackTraceInThrowable) return;
2575 
2576   assert(throwable->is_a(vmClasses::Throwable_klass()), "sanity check");
2577 
2578   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2579 
2580   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2581   assert(backtrace.not_null(), "backtrace should have been preallocated");
2582 
2583   ResourceMark rm(THREAD);
2584   vframeStream st(THREAD, false /* stop_at_java_call_stub */, false /* process_frames */);
2585 
2586   BacktraceBuilder bt(THREAD, backtrace);
2587 
2588   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2589   // methods as preallocated errors aren't created by "java" code.
2590 
2591   // fill in as much stack trace as possible
2592   int chunk_count = 0;
2593   for (;!st.at_end(); st.next()) {
2594     bt.push(st.method(), st.bci(), CHECK);
2595     chunk_count++;
2596 
2597     // Bail-out for deep stacks
2598     if (chunk_count >= trace_chunk_size) break;
2599   }
2600   set_depth(throwable(), chunk_count);
2601   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2602 
2603   // We support the Throwable immutability protocol defined for Java 7.
2604   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2605   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2606 }
2607 
2608 void java_lang_Throwable::get_stack_trace_elements(Handle throwable,
2609                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2610 
2611   if (throwable.is_null() || stack_trace_array_h.is_null()) {
2612     THROW(vmSymbols::java_lang_NullPointerException());
2613   }
2614 
2615   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
2616 
2617   if (stack_trace_array_h->length() != depth(throwable())) {
2618     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
2619   }
2620 
2621   objArrayHandle result(THREAD, objArrayOop(backtrace(throwable())));
2622   BacktraceIterator iter(result, THREAD);
2623 
2624   int index = 0;
2625   while (iter.repeat()) {
2626     BacktraceElement bte = iter.next(THREAD);
2627 
2628     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
2629 
2630     if (stack_trace_element.is_null()) {
2631       THROW(vmSymbols::java_lang_NullPointerException());
2632     }
2633 
2634     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2635     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
2636 
2637     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
2638                                          method,
2639                                          bte._version,
2640                                          bte._bci,
2641                                          bte._name, CHECK);


2642   }
2643 }
2644 
2645 Handle java_lang_Throwable::get_cause_with_stack_trace(Handle throwable, TRAPS) {
2646   // Call to JVM to fill in the stack trace and clear declaringClassObject to
2647   // not keep classes alive in the stack trace.
2648   // call this:  public StackTraceElement[] getStackTrace()
2649   assert(throwable.not_null(), "shouldn't be");
2650 
2651   JavaValue result(T_ARRAY);
2652   JavaCalls::call_virtual(&result, throwable,
2653                           vmClasses::Throwable_klass(),
2654                           vmSymbols::getStackTrace_name(),
2655                           vmSymbols::getStackTrace_signature(),
2656                           CHECK_NH);
2657   Handle stack_trace(THREAD, result.get_oop());
2658   assert(stack_trace->is_objArray(), "Should be an array");
2659 
2660   // Throw ExceptionInInitializerError as the cause with this exception in
2661   // the message and stack trace.

2701     return false;
2702   }
2703 
2704   // Get first backtrace element.
2705   BacktraceElement bte = iter.next(current);
2706 
2707   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2708   assert(holder != NULL, "first element should be non-null");
2709   Method* m = holder->method_with_orig_idnum(bte._method_id, bte._version);
2710 
2711   // Original version is no longer available.
2712   if (m == NULL || !version_matches(m, bte._version)) {
2713     return false;
2714   }
2715 
2716   *method = m;
2717   *bci = bte._bci;
2718   return true;
2719 }
2720 
2721 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) {
2722   // Allocate java.lang.StackTraceElement instance
2723   InstanceKlass* k = vmClasses::StackTraceElement_klass();
2724   assert(k != NULL, "must be loaded in 1.4+");
2725   if (k->should_be_initialized()) {
2726     k->initialize(CHECK_NULL);
2727   }
2728 
2729   Handle element = k->allocate_instance_handle(CHECK_NULL);
2730 
2731   int version = method->constants()->version();
2732   fill_in(element, method->method_holder(), method, version, bci, method->name(), CHECK_NULL);
2733   return element();
2734 }
2735 
2736 void java_lang_StackTraceElement::fill_in(Handle element,
2737                                           InstanceKlass* holder, const methodHandle& method,
2738                                           int version, int bci, Symbol* name, TRAPS) {
2739   assert(element->is_a(vmClasses::StackTraceElement_klass()), "sanity check");
2740 
2741   ResourceMark rm(THREAD);
2742   HandleMark hm(THREAD);
2743 
2744   // Fill in class name
2745   Handle java_class(THREAD, holder->java_mirror());
2746   oop classname = java_lang_Class::name(java_class, CHECK);
2747   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2748   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
2749 
2750   oop loader = holder->class_loader();
2751   if (loader != NULL) {
2752     oop loader_name = java_lang_ClassLoader::name(loader);
2753     if (loader_name != NULL)
2754       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
2755   }
2756 
2757   // Fill in method name
2758   oop methodname = StringTable::intern(name, CHECK);

2768       module_version = StringTable::intern(module->version(), CHECK);
2769     } else {
2770       module_version = NULL;
2771     }
2772     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
2773   }
2774 
2775   if (method() == NULL || !version_matches(method(), version)) {
2776     // The method was redefined, accurate line number information isn't available
2777     java_lang_StackTraceElement::set_fileName(element(), NULL);
2778     java_lang_StackTraceElement::set_lineNumber(element(), -1);
2779   } else {
2780     Symbol* source;
2781     oop source_file;
2782     int line_number;
2783     decode_file_and_line(java_class, holder, version, method, bci, source, source_file, line_number, CHECK);
2784 
2785     java_lang_StackTraceElement::set_fileName(element(), source_file);
2786     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2787   }



2788 }
2789 
2790 void java_lang_StackTraceElement::decode_file_and_line(Handle java_class,
2791                                                        InstanceKlass* holder,
2792                                                        int version,
2793                                                        const methodHandle& method,
2794                                                        int bci,
2795                                                        Symbol*& source,
2796                                                        oop& source_file,
2797                                                        int& line_number, TRAPS) {
2798   // Fill in source file name and line number.
2799   source = Backtrace::get_source_file_name(holder, version);
2800   source_file = java_lang_Class::source_file(java_class());
2801   if (source != NULL) {
2802     // Class was not redefined. We can trust its cache if set,
2803     // else we have to initialize it.
2804     if (source_file == NULL) {
2805       source_file = StringTable::intern(source, CHECK);
2806       java_lang_Class::set_source_file(java_class(), source_file);
2807     }

2820                                          Symbol*& filename, int& line_number, TRAPS) {
2821   ResourceMark rm(THREAD);
2822   HandleMark hm(THREAD);
2823 
2824   filename = NULL;
2825   line_number = -1;
2826 
2827   oop source_file;
2828   int version = method->constants()->version();
2829   InstanceKlass* holder = method->method_holder();
2830   Handle java_class(THREAD, holder->java_mirror());
2831   decode_file_and_line(java_class, holder, version, method, bci, filename, source_file, line_number, CHECK);
2832 }
2833 #endif // INCLUDE_JVMCI
2834 
2835 // java_lang_StackFrameInfo
2836 
2837 int java_lang_StackFrameInfo::_memberName_offset;
2838 int java_lang_StackFrameInfo::_bci_offset;
2839 int java_lang_StackFrameInfo::_version_offset;

2840 
2841 #define STACKFRAMEINFO_FIELDS_DO(macro) \
2842   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
2843   macro(_bci_offset,            k, "bci",         int_signature,    false)

2844 
2845 void java_lang_StackFrameInfo::compute_offsets() {
2846   InstanceKlass* k = vmClasses::StackFrameInfo_klass();
2847   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2848   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2849 }
2850 
2851 #if INCLUDE_CDS
2852 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
2853   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2854   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2855 }
2856 #endif
2857 
2858 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2859   HandleMark hm(THREAD);
2860   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2861   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2862   // we should expand MemberName::name when Throwable uses StackTrace
2863   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2864   return method;
2865 }
2866 
2867 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) {
2868   // set Method* or mid/cpref
2869   HandleMark hm(THREAD);
2870   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));

2871   InstanceKlass* ik = method->method_holder();
2872   CallInfo info(method(), ik, CHECK);
2873   MethodHandles::init_method_MemberName(mname, info);
2874   // set bci
2875   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2876   // method may be redefined; store the version
2877   int version = method->constants()->version();
2878   assert((jushort)version == version, "version should be short");
2879   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);



2880 }
2881 
2882 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
2883   ResourceMark rm(THREAD);
2884   HandleMark hm(THREAD);
2885   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
2886   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
2887   InstanceKlass* holder = InstanceKlass::cast(clazz);
2888   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);


2889 
2890   short version = stackFrame->short_field(_version_offset);
2891   int bci = stackFrame->int_field(_bci_offset);
2892   Symbol* name = method->name();
2893   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, methodHandle(THREAD, method),
2894                                        version, bci, name, CHECK);



2895 }
2896 
2897 void java_lang_StackFrameInfo::set_version(oop element, short value) {
2898   element->short_field_put(_version_offset, value);
2899 }
2900 
2901 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
2902   assert(value >= 0 && value < max_jushort, "must be a valid bci value");
2903   element->int_field_put(_bci_offset, value);
2904 }
2905 




2906 int java_lang_LiveStackFrameInfo::_monitors_offset;
2907 int java_lang_LiveStackFrameInfo::_locals_offset;
2908 int java_lang_LiveStackFrameInfo::_operands_offset;
2909 int java_lang_LiveStackFrameInfo::_mode_offset;
2910 
2911 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
2912   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
2913   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
2914   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
2915   macro(_mode_offset,       k, "mode",        int_signature,          false)
2916 
2917 void java_lang_LiveStackFrameInfo::compute_offsets() {
2918   InstanceKlass* k = vmClasses::LiveStackFrameInfo_klass();
2919   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2920 }
2921 
2922 #if INCLUDE_CDS
2923 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) {
2924   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2925 }

4474     _use_unaligned_access = UseUnalignedAccesses;
4475     _data_cache_line_flush_size = (int)VM_Version::data_cache_line_flush_size();
4476   }
4477 
4478   void do_field(fieldDescriptor* fd) {
4479     oop mirror = fd->field_holder()->java_mirror();
4480     assert(mirror != NULL, "UnsafeConstants must have mirror already");
4481     assert(fd->field_holder() == vmClasses::UnsafeConstants_klass(), "Should be UnsafeConstants");
4482     assert(fd->is_final(), "fields of UnsafeConstants must be final");
4483     assert(fd->is_static(), "fields of UnsafeConstants must be static");
4484     if (fd->name() == vmSymbols::address_size_name()) {
4485       mirror->int_field_put(fd->offset(), _address_size);
4486     } else if (fd->name() == vmSymbols::page_size_name()) {
4487       mirror->int_field_put(fd->offset(), _page_size);
4488     } else if (fd->name() == vmSymbols::big_endian_name()) {
4489       mirror->bool_field_put(fd->offset(), _big_endian);
4490     } else if (fd->name() == vmSymbols::use_unaligned_access_name()) {
4491       mirror->bool_field_put(fd->offset(), _use_unaligned_access);
4492     } else if (fd->name() == vmSymbols::data_cache_line_flush_size_name()) {
4493       mirror->int_field_put(fd->offset(), _data_cache_line_flush_size);


4494     } else {
4495       assert(false, "unexpected UnsafeConstants field");
4496     }
4497   }
4498 };
4499 
4500 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4501   UnsafeConstantsFixup fixup;
4502   vmClasses::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4503 }
4504 
4505 
4506 // java_lang_StackTraceElement
4507 
4508 int java_lang_StackTraceElement::_methodName_offset;
4509 int java_lang_StackTraceElement::_fileName_offset;
4510 int java_lang_StackTraceElement::_lineNumber_offset;
4511 int java_lang_StackTraceElement::_moduleName_offset;
4512 int java_lang_StackTraceElement::_moduleVersion_offset;
4513 int java_lang_StackTraceElement::_classLoaderName_offset;
4514 int java_lang_StackTraceElement::_declaringClass_offset;
4515 int java_lang_StackTraceElement::_declaringClassObject_offset;

4516 
4517 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4518   macro(_declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4519   macro(_classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4520   macro(_moduleName_offset,      k, "moduleName",      string_signature, false); \
4521   macro(_moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4522   macro(_declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4523   macro(_methodName_offset,      k, "methodName",      string_signature, false); \
4524   macro(_fileName_offset,        k, "fileName",        string_signature, false); \
4525   macro(_lineNumber_offset,      k, "lineNumber",      int_signature,    false)

4526 
4527 // Support for java_lang_StackTraceElement
4528 void java_lang_StackTraceElement::compute_offsets() {
4529   InstanceKlass* k = vmClasses::StackTraceElement_klass();
4530   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4531 }
4532 
4533 #if INCLUDE_CDS
4534 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) {
4535   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4536 }
4537 #endif
4538 
4539 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4540   element->obj_field_put(_fileName_offset, value);
4541 }
4542 
4543 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4544   element->obj_field_put(_declaringClass_offset, value);
4545 }

4599 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
4600   o->obj_field_put(_classes_offset, val);
4601 }
4602 
4603 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
4604   o->obj_field_put(_classEnabled_offset, val);
4605 }
4606 
4607 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
4608   o->obj_field_put(_packages_offset, val);
4609 }
4610 
4611 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
4612   o->obj_field_put(_packageEnabled_offset, val);
4613 }
4614 
4615 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
4616   o->bool_field_put(_deflt_offset, val);
4617 }
4618 
























































































4619 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset;
4620 
4621 #define AOS_FIELDS_DO(macro) \
4622   macro(_owner_offset, k, "exclusiveOwnerThread", thread_signature, false)
4623 
4624 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() {
4625   InstanceKlass* k = vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass();
4626   AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4627 }
4628 
4629 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
4630   assert(_owner_offset != 0, "Must be initialized");
4631   return obj->obj_field(_owner_offset);
4632 }
4633 
4634 #if INCLUDE_CDS
4635 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) {
4636   AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4637 }
4638 #endif

5021 void JavaClasses::check_offsets() {
5022   bool valid = true;
5023 
5024 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5025   valid &= check_offset(klass_name, cpp_klass_name :: _##field_name ## _offset, #field_name, field_sig)
5026 
5027 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5028   valid &= check_offset(klass_name, cpp_klass_name :: _##long_ ## field_name ## _offset, #field_name, field_sig)
5029 
5030   // Boxed primitive objects (java_lang_boxing_object)
5031 
5032   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
5033   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
5034   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
5035   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
5036   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
5037   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
5038   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
5039   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
5040 








5041   if (!valid) vm_exit_during_initialization("Field offset verification failed");
5042 }
5043 
5044 #endif // PRODUCT
5045 
5046 int InjectedField::compute_offset() {
5047   InstanceKlass* ik = InstanceKlass::cast(klass());
5048   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5049     if (!may_be_java && !fs.access_flags().is_internal()) {
5050       // Only look at injected fields
5051       continue;
5052     }
5053     if (fs.name() == name() && fs.signature() == signature()) {
5054       return fs.offset();
5055     }
5056   }
5057   ResourceMark rm;
5058   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5059 #ifndef PRODUCT
5060   ik->print();
5061   tty->print_cr("all fields:");
5062   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5063     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
5064   }
5065 #endif //PRODUCT
5066   vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
5067   return -1;
5068 }
5069 
5070 void javaClasses_init() {
5071   JavaClasses::compute_offsets();
5072   JavaClasses::check_offsets();

5073   FilteredFieldsMap::initialize();  // must be done after computing offsets.
5074 }

 178                            const char* name_string, Symbol* signature_symbol,
 179                            bool is_static = false) {
 180   TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
 181   if (name == NULL) {
 182     ResourceMark rm;
 183     log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
 184     vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name());
 185   }
 186   compute_offset(dest_offset, ik, name, signature_symbol, is_static);
 187 }
 188 
 189 
 190 #if INCLUDE_CDS
 191 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
 192   f->do_u4((u4*)&offset)
 193 #endif
 194 
 195 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
 196   compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
 197 
 198 // This class provides a simple wrapper over the internal structure of
 199 // exception backtrace to insulate users of the backtrace from needing
 200 // to know what it looks like.
 201 // The code of this class is not GC safe. Allocations can only happen
 202 // in expand().
 203 class BacktraceBuilder: public StackObj {
 204  friend class BacktraceIterator;
 205  private:
 206   Handle          _backtrace;
 207   objArrayOop     _head;
 208   typeArrayOop    _methods;
 209   typeArrayOop    _bcis;
 210   objArrayOop     _mirrors;
 211   typeArrayOop    _names; // Needed to insulate method name against redefinition.
 212   objArrayOop     _conts;
 213   // True if the top frame of the backtrace is omitted because it shall be hidden.
 214   bool            _has_hidden_top_frame;
 215   int             _index;
 216   NoSafepointVerifier _nsv;
 217 
 218   enum {
 219     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
 220     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
 221     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
 222     trace_names_offset   = java_lang_Throwable::trace_names_offset,
 223     trace_conts_offset   = java_lang_Throwable::trace_conts_offset,
 224     trace_next_offset    = java_lang_Throwable::trace_next_offset,
 225     trace_hidden_offset  = java_lang_Throwable::trace_hidden_offset,
 226     trace_size           = java_lang_Throwable::trace_size,
 227     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
 228   };
 229 
 230   // get info out of chunks
 231   static typeArrayOop get_methods(objArrayHandle chunk) {
 232     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
 233     assert(methods != NULL, "method array should be initialized in backtrace");
 234     return methods;
 235   }
 236   static typeArrayOop get_bcis(objArrayHandle chunk) {
 237     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
 238     assert(bcis != NULL, "bci array should be initialized in backtrace");
 239     return bcis;
 240   }
 241   static objArrayOop get_mirrors(objArrayHandle chunk) {
 242     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
 243     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
 244     return mirrors;
 245   }
 246   static typeArrayOop get_names(objArrayHandle chunk) {
 247     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
 248     assert(names != NULL, "names array should be initialized in backtrace");
 249     return names;
 250   }
 251   static objArrayOop get_conts(objArrayHandle chunk) {
 252     objArrayOop conts = objArrayOop(chunk->obj_at(trace_conts_offset));
 253     assert(conts != NULL, "conts array should be initialized in backtrace");
 254     return conts;
 255   }
 256   static bool has_hidden_top_frame(objArrayHandle chunk) {
 257     oop hidden = chunk->obj_at(trace_hidden_offset);
 258     return hidden != NULL;
 259   }
 260 
 261  public:
 262 
 263   // constructor for new backtrace
 264   BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL), _conts(NULL), _has_hidden_top_frame(false) {
 265     expand(CHECK);
 266     _backtrace = Handle(THREAD, _head);
 267     _index = 0;
 268   }
 269 
 270   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
 271     _methods = get_methods(backtrace);
 272     _bcis = get_bcis(backtrace);
 273     _mirrors = get_mirrors(backtrace);
 274     _names = get_names(backtrace);
 275     _conts = get_conts(backtrace);
 276     _has_hidden_top_frame = has_hidden_top_frame(backtrace);
 277     assert(_methods->length() == _bcis->length() &&
 278            _methods->length() == _mirrors->length() &&
 279            _mirrors->length() == _names->length() &&
 280            _names->length() == _conts->length(),
 281            "method and source information arrays should match");
 282 
 283     // head is the preallocated backtrace
 284     _head = backtrace();
 285     _backtrace = Handle(thread, _head);
 286     _index = 0;
 287   }
 288 
 289   void expand(TRAPS) {
 290     objArrayHandle old_head(THREAD, _head);
 291     PauseNoSafepointVerifier pnsv(&_nsv);
 292 
 293     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
 294     objArrayHandle new_head(THREAD, head);
 295 
 296     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
 297     typeArrayHandle new_methods(THREAD, methods);
 298 
 299     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
 300     typeArrayHandle new_bcis(THREAD, bcis);
 301 
 302     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
 303     objArrayHandle new_mirrors(THREAD, mirrors);
 304 
 305     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
 306     typeArrayHandle new_names(THREAD, names);
 307 
 308     objArrayOop conts = oopFactory::new_objectArray(trace_chunk_size, CHECK);
 309     objArrayHandle new_conts(THREAD, conts);
 310 
 311     if (!old_head.is_null()) {
 312       old_head->obj_at_put(trace_next_offset, new_head());
 313     }
 314     new_head->obj_at_put(trace_methods_offset, new_methods());
 315     new_head->obj_at_put(trace_bcis_offset, new_bcis());
 316     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
 317     new_head->obj_at_put(trace_names_offset, new_names());
 318     new_head->obj_at_put(trace_conts_offset, new_conts());
 319     new_head->obj_at_put(trace_hidden_offset, NULL);
 320 
 321     _head    = new_head();
 322     _methods = new_methods();
 323     _bcis = new_bcis();
 324     _mirrors = new_mirrors();
 325     _names  = new_names();
 326     _conts  = new_conts();
 327     _index = 0;
 328   }
 329 
 330   oop backtrace() {
 331     return _backtrace();
 332   }
 333 
 334   inline void push(Method* method, int bci, oop contScopeName, TRAPS) {
 335     // Smear the -1 bci to 0 since the array only holds unsigned
 336     // shorts.  The later line number lookup would just smear the -1
 337     // to a 0 even if it could be recorded.
 338     if (bci == SynchronizationEntryBCI) bci = 0;
 339 
 340     if (_index >= trace_chunk_size) {
 341       methodHandle mhandle(THREAD, method);
 342       Handle chandle(THREAD, contScopeName);
 343       expand(CHECK);
 344       method = mhandle();
 345       contScopeName = chandle();
 346     }
 347 
 348     _methods->ushort_at_put(_index, method->orig_method_idnum());
 349     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
 350 
 351     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
 352     // klass owning the symbols alive so their refcounts aren't decremented.
 353     Symbol* name = method->name();
 354     _names->symbol_at_put(_index, name);
 355 
 356     // We need to save the mirrors in the backtrace to keep the class
 357     // from being unloaded while we still have this stack trace.
 358     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
 359     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
 360 
 361     _conts->obj_at_put(_index, contScopeName);
 362 
 363     _index++;
 364   }
 365 
 366   void set_has_hidden_top_frame(TRAPS) {
 367     if (!_has_hidden_top_frame) {
 368       // It would be nice to add java/lang/Boolean::TRUE here
 369       // to indicate that this backtrace has a hidden top frame.
 370       // But this code is used before TRUE is allocated.
 371       // Therefore let's just use an arbitrary legal oop
 372       // available right here. _methods is a short[].
 373       assert(_methods != NULL, "we need a legal oop");
 374       _has_hidden_top_frame = true;
 375       _head->obj_at_put(trace_hidden_offset, _methods);
 376     }
 377   }
 378 };
 379 
 380 struct BacktraceElement : public StackObj {
 381   int _method_id;
 382   int _bci;
 383   int _version;
 384   Symbol* _name;
 385   Handle _mirror;
 386   Handle _cont; // the continuation scope name (String)
 387   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name, Handle cont) :
 388                    _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror), _cont(cont) {}
 389 };
 390 
 391 class BacktraceIterator : public StackObj {
 392   int _index;
 393   objArrayHandle  _result;
 394   objArrayHandle  _mirrors;
 395   typeArrayHandle _methods;
 396   typeArrayHandle _bcis;
 397   typeArrayHandle _names;
 398   objArrayHandle  _conts;
 399 
 400   void init(objArrayHandle result, Thread* thread) {
 401     // Get method id, bci, version and mirror from chunk
 402     _result = result;
 403     if (_result.not_null()) {
 404       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
 405       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
 406       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
 407       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));
 408       _conts = objArrayHandle(thread, BacktraceBuilder::get_conts(_result));
 409       _index = 0;
 410     }
 411   }
 412  public:
 413   BacktraceIterator(objArrayHandle result, Thread* thread) {
 414     init(result, thread);
 415     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
 416   }
 417 
 418   BacktraceElement next(Thread* thread) {
 419     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
 420                         _methods->ushort_at(_index),
 421                         Backtrace::version_at(_bcis->int_at(_index)),
 422                         Backtrace::bci_at(_bcis->int_at(_index)),
 423                         _names->symbol_at(_index),
 424                         Handle(thread, _conts->obj_at(_index)));
 425     _index++;
 426 
 427     if (_index >= java_lang_Throwable::trace_chunk_size) {
 428       int next_offset = java_lang_Throwable::trace_next_offset;
 429       // Get next chunk
 430       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
 431       init(result, thread);
 432     }
 433     return e;
 434   }
 435 
 436   bool repeat() {
 437     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
 438   }
 439 };
 440 
 441 // java_lang_String
 442 
 443 int java_lang_String::_value_offset;
 444 int java_lang_String::_hash_offset;
 445 int java_lang_String::_hashIsZero_offset;
 446 int java_lang_String::_coder_offset;
 447 int java_lang_String::_flags_offset;
 448 
 449 bool java_lang_String::_initialized;
 450 
 451 
 452 bool java_lang_String::test_and_set_flag(oop java_string, uint8_t flag_mask) {
 453   uint8_t* addr = flags_addr(java_string);
 454   uint8_t value = Atomic::load(addr);
 455   while ((value & flag_mask) == 0) {
 456     uint8_t old_value = value;
 457     value |= flag_mask;
 458     value = Atomic::cmpxchg(addr, old_value, value);
 459     if (value == old_value) return false; // Flag bit changed from 0 to 1.

1895 
1896 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1897   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1898   return the_class_mirror->int_field(_classRedefinedCount_offset);
1899 }
1900 
1901 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1902   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1903   the_class_mirror->int_field_put(_classRedefinedCount_offset, value);
1904 }
1905 
1906 
1907 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1908 //       platform thread structure, and a eetop offset which was used for thread
1909 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1910 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1911 //       instead of the privateInfo_offset.
1912 //
1913 // Note: The stackSize field is only present starting in 1.4.
1914 
1915 int java_lang_Thread_FieldHolder::_group_offset;
1916 int java_lang_Thread_FieldHolder::_priority_offset;
1917 int java_lang_Thread_FieldHolder::_stackSize_offset;
1918 int java_lang_Thread_FieldHolder::_stillborn_offset;
1919 int java_lang_Thread_FieldHolder::_daemon_offset;
1920 int java_lang_Thread_FieldHolder::_thread_status_offset;
1921 
1922 #define THREAD_FIELD_HOLDER_FIELDS_DO(macro) \
1923   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1924   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1925   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1926   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1927   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1928   macro(_thread_status_offset, k, "threadStatus", int_signature, false)
1929 
1930 void java_lang_Thread_FieldHolder::compute_offsets() {
1931   assert(_group_offset == 0, "offsets should be initialized only once");
1932 
1933   InstanceKlass* k = vmClasses::Thread_FieldHolder_klass();
1934   THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1935 }
1936 
1937 #if INCLUDE_CDS
1938 void java_lang_Thread_FieldHolder::serialize_offsets(SerializeClosure* f) {
1939   THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1940 }
1941 #endif
1942 
1943 oop java_lang_Thread_FieldHolder::threadGroup(oop holder) {
1944   return holder->obj_field(_group_offset);
1945 }
1946 
1947 ThreadPriority java_lang_Thread_FieldHolder::priority(oop holder) {
1948   return (ThreadPriority)holder->int_field(_priority_offset);
1949 }
1950 
1951 void java_lang_Thread_FieldHolder::set_priority(oop holder, ThreadPriority priority) {
1952   holder->int_field_put(_priority_offset, priority);
1953 }
1954 
1955 jlong java_lang_Thread_FieldHolder::stackSize(oop holder) {
1956   return holder->long_field(_stackSize_offset);
1957 }
1958 
1959 bool java_lang_Thread_FieldHolder::is_stillborn(oop holder) {
1960   return holder->bool_field(_stillborn_offset) != 0;
1961 }
1962 
1963 void java_lang_Thread_FieldHolder::set_stillborn(oop holder) {
1964   holder->bool_field_put(_stillborn_offset, true);
1965 }
1966 
1967 bool java_lang_Thread_FieldHolder::is_daemon(oop holder) {
1968   return holder->bool_field(_daemon_offset) != 0;
1969 }
1970 
1971 void java_lang_Thread_FieldHolder::set_daemon(oop holder) {
1972   holder->bool_field_put(_daemon_offset, true);
1973 }
1974 
1975 void java_lang_Thread_FieldHolder::set_thread_status(oop holder, JavaThreadStatus status) {
1976   holder->int_field_put(_thread_status_offset, static_cast<int>(status));
1977 }
1978 
1979 JavaThreadStatus java_lang_Thread_FieldHolder::get_thread_status(oop holder) {
1980   return static_cast<JavaThreadStatus>(holder->int_field(_thread_status_offset));
1981 }
1982 
1983 
1984 int java_lang_Thread_VirtualThreads::_static_THREAD_GROUP_offset = 0;
1985 
1986 #define THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(macro) \
1987   macro(_static_THREAD_GROUP_offset, k, "THREAD_GROUP", threadgroup_signature, true);
1988 
1989 void java_lang_Thread_VirtualThreads::compute_offsets() {
1990   assert(_static_THREAD_GROUP_offset == 0, "offsets should be initialized only once");
1991 
1992   InstanceKlass* k = vmClasses::Thread_VirtualThreads_klass();
1993   THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1994 }
1995 
1996 #if INCLUDE_CDS
1997 void java_lang_Thread_VirtualThreads::serialize_offsets(SerializeClosure* f) {
1998   THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1999 }
2000 #endif
2001 
2002 oop java_lang_Thread_VirtualThreads::get_THREAD_GROUP() {
2003   InstanceKlass* k = vmClasses::Thread_VirtualThreads_klass();
2004   oop base = k->static_field_base_raw();
2005   return base->obj_field(_static_THREAD_GROUP_offset);
2006 }
2007 
2008 
2009 int java_lang_Thread_ClassLoaders::_static_NOT_SUPPORTED_offset = 0;
2010 
2011 #define THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(macro) \
2012   macro(_static_NOT_SUPPORTED_offset, k, "NOT_SUPPORTED", classloader_signature, true);
2013 
2014 void java_lang_Thread_ClassLoaders::compute_offsets() {
2015   assert(_static_NOT_SUPPORTED_offset == 0, "offsets should be initialized only once");
2016 
2017   InstanceKlass* k = vmClasses::Thread_ClassLoaders_klass();
2018   THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2019 }
2020 
2021 #if INCLUDE_CDS
2022 void java_lang_Thread_ClassLoaders::serialize_offsets(SerializeClosure* f) {
2023   THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2024 }
2025 #endif
2026 
2027 oop java_lang_Thread_ClassLoaders::get_NOT_SUPPORTED() {
2028   InstanceKlass* k = vmClasses::Thread_ClassLoaders_klass();
2029   oop base = k->static_field_base_raw();
2030   return base->obj_field(_static_NOT_SUPPORTED_offset);
2031 }
2032 
2033 int java_lang_Thread::_holder_offset;
2034 int java_lang_Thread::_name_offset;

2035 int java_lang_Thread::_contextClassLoader_offset;
2036 int java_lang_Thread::_inheritedAccessControlContext_offset;

2037 int java_lang_Thread::_eetop_offset;
2038 int java_lang_Thread::_jvmti_thread_state_offset;
2039 int java_lang_Thread::_interrupted_offset;



2040 int java_lang_Thread::_tid_offset;
2041 int java_lang_Thread::_continuation_offset;
2042 int java_lang_Thread::_park_blocker_offset;
2043 int java_lang_Thread::_scopeLocalBindings_offset;
2044 
2045 #define THREAD_FIELDS_DO(macro) \
2046   macro(_holder_offset,        k, "holder", thread_fieldholder_signature, false); \
2047   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \

2048   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
2049   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \


2050   macro(_eetop_offset,         k, "eetop", long_signature, false); \
2051   macro(_interrupted_offset,   k, "interrupted", bool_signature, false); \


2052   macro(_tid_offset,           k, "tid", long_signature, false); \
2053   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
2054   macro(_continuation_offset,  k, "cont", continuation_signature, false); \
2055   macro(_scopeLocalBindings_offset, k, "scopeLocalBindings", scopeLocalSnapshot_name, false);
2056 
2057 void java_lang_Thread::compute_offsets() {
2058   assert(_holder_offset == 0, "offsets should be initialized only once");
2059 
2060   InstanceKlass* k = vmClasses::Thread_klass();
2061   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2062   THREAD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2063 }
2064 
2065 #if INCLUDE_CDS
2066 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
2067   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2068   THREAD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2069 }
2070 #endif
2071 
2072 JavaThread* java_lang_Thread::thread(oop java_thread) {
2073   return (JavaThread*)java_thread->address_field(_eetop_offset);
2074 }
2075 

2076 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
2077   java_thread->address_field_put(_eetop_offset, (address)thread);
2078 }
2079 
2080 JvmtiThreadState* java_lang_Thread::jvmti_thread_state(oop java_thread) {
2081   return (JvmtiThreadState*)java_thread->address_field(_jvmti_thread_state_offset);
2082 }
2083 
2084 void java_lang_Thread::set_jvmti_thread_state(oop java_thread, JvmtiThreadState* state) {
2085   java_thread->address_field_put(_jvmti_thread_state_offset, (address)state);
2086 }
2087 
2088 void java_lang_Thread::clear_scopeLocalBindings(oop java_thread) {
2089   java_thread->obj_field_put(_scopeLocalBindings_offset, NULL);
2090 }
2091 
2092 oop java_lang_Thread::holder(oop java_thread) {
2093     return java_thread->obj_field(_holder_offset);
2094 }
2095 
2096 bool java_lang_Thread::interrupted(oop java_thread) {
2097   // Make sure the caller can safely access oops.
2098   assert(Thread::current()->is_VM_thread() ||
2099          (JavaThread::current()->thread_state() != _thread_blocked &&
2100           JavaThread::current()->thread_state() != _thread_in_native),
2101          "Unsafe access to oop");
2102   return java_thread->bool_field_volatile(_interrupted_offset);
2103 }
2104 
2105 void java_lang_Thread::set_interrupted(oop java_thread, bool val) {
2106   // Make sure the caller can safely access oops.
2107   assert(Thread::current()->is_VM_thread() ||
2108          (JavaThread::current()->thread_state() != _thread_blocked &&
2109           JavaThread::current()->thread_state() != _thread_in_native),
2110          "Unsafe access to oop");
2111   java_thread->bool_field_put_volatile(_interrupted_offset, val);
2112 }
2113 
2114 
2115 oop java_lang_Thread::name(oop java_thread) {
2116   return java_thread->obj_field(_name_offset);
2117 }
2118 
2119 
2120 void java_lang_Thread::set_name(oop java_thread, oop name) {
2121   java_thread->obj_field_put(_name_offset, name);
2122 }
2123 
2124 
2125 ThreadPriority java_lang_Thread::priority(oop java_thread) {
2126   oop holder = java_lang_Thread::holder(java_thread);
2127   assert(holder != NULL, "Java Thread not initialized");
2128   return java_lang_Thread_FieldHolder::priority(holder);
2129 }
2130 
2131 
2132 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
2133   oop holder = java_lang_Thread::holder(java_thread);
2134   assert(holder != NULL, "Java Thread not initialized");
2135   java_lang_Thread_FieldHolder::set_priority(holder, priority);
2136 }
2137 
2138 
2139 oop java_lang_Thread::threadGroup(oop java_thread) {
2140   oop holder = java_lang_Thread::holder(java_thread);
2141   assert(holder != NULL, "Java Thread not initialized");
2142   return java_lang_Thread_FieldHolder::threadGroup(holder);
2143 }
2144 
2145 
2146 bool java_lang_Thread::is_stillborn(oop java_thread) {
2147   oop holder = java_lang_Thread::holder(java_thread);
2148   assert(holder != NULL, "Java Thread not initialized");
2149   return java_lang_Thread_FieldHolder::is_stillborn(holder);
2150 }
2151 
2152 
2153 // We never have reason to turn the stillborn bit off
2154 void java_lang_Thread::set_stillborn(oop java_thread) {
2155   oop holder = java_lang_Thread::holder(java_thread);
2156   assert(holder != NULL, "Java Thread not initialized");
2157   java_lang_Thread_FieldHolder::set_stillborn(holder);
2158 }
2159 
2160 
2161 bool java_lang_Thread::is_alive(oop java_thread) {
2162   JavaThread* thr = java_lang_Thread::thread(java_thread);
2163   return (thr != NULL);
2164 }
2165 
2166 
2167 bool java_lang_Thread::is_daemon(oop java_thread) {
2168   oop holder = java_lang_Thread::holder(java_thread);
2169   assert(holder != NULL, "Java Thread not initialized");
2170   return java_lang_Thread_FieldHolder::is_daemon(holder);
2171 }
2172 

2173 void java_lang_Thread::set_daemon(oop java_thread) {
2174   oop holder = java_lang_Thread::holder(java_thread);
2175   assert(holder != NULL, "Java Thread not initialized");
2176   java_lang_Thread_FieldHolder::set_daemon(holder);
2177 }
2178 
2179 oop java_lang_Thread::context_class_loader(oop java_thread) {
2180   return java_thread->obj_field(_contextClassLoader_offset);
2181 }
2182 
2183 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
2184   return java_thread->obj_field(_inheritedAccessControlContext_offset);
2185 }
2186 
2187 
2188 jlong java_lang_Thread::stackSize(oop java_thread) {
2189   oop holder = java_lang_Thread::holder(java_thread);
2190   assert(holder != NULL, "Java Thread not initialized");
2191   return java_lang_Thread_FieldHolder::stackSize(holder);
2192 }
2193 
2194 // Write the thread status value to threadStatus field in java.lang.Thread java class.
2195 void java_lang_Thread::set_thread_status(oop java_thread, JavaThreadStatus status) {
2196   oop holder = java_lang_Thread::holder(java_thread);
2197   assert(holder != NULL, "Java Thread not initialized");
2198   java_lang_Thread_FieldHolder::set_thread_status(holder, status);
2199 }
2200 
2201 // Read thread status value from threadStatus field in java.lang.Thread java class.
2202 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
2203   // Make sure the caller is operating on behalf of the VM or is
2204   // running VM code (state == _thread_in_vm).
2205   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
2206          JavaThread::current()->thread_state() == _thread_in_vm,
2207          "Java Thread is not running in vm");
2208   oop holder = java_lang_Thread::holder(java_thread);
2209   if (holder == NULL) {
2210     return JavaThreadStatus::NEW;  // Java Thread not initialized
2211   } else {
2212     return java_lang_Thread_FieldHolder::get_thread_status(holder);
2213   }
2214 }
2215 

2216 jlong java_lang_Thread::thread_id(oop java_thread) {
2217   // The 16 most significant bits can be used for tracing
2218   // so these bits are excluded using a mask.
2219   static const jlong tid_mask = (((jlong)1) << 48) - 1;
2220   return java_thread->long_field(_tid_offset) & tid_mask;
2221 }
2222 
2223 ByteSize java_lang_Thread::thread_id_offset() {
2224   return in_ByteSize(_tid_offset);
2225 }
2226 
2227 oop java_lang_Thread::park_blocker(oop java_thread) {
2228   return java_thread->obj_field(_park_blocker_offset);
2229 }
2230 
2231 oop java_lang_Thread::async_get_stack_trace(oop java_thread, TRAPS) {
2232   ThreadsListHandle tlh(JavaThread::current());
2233   JavaThread* thread;
2234   bool is_virtual = java_lang_VirtualThread::is_instance(java_thread);
2235   if (is_virtual) {
2236     oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread);
2237     if (carrier_thread == NULL) {
2238       return NULL;
2239     }
2240     thread = java_lang_Thread::thread(carrier_thread);
2241   } else {
2242     thread = java_lang_Thread::thread(java_thread);
2243   }
2244   if (thread == NULL) {
2245     return NULL;
2246   }
2247 
2248   class GetStackTraceClosure : public HandshakeClosure {
2249   public:
2250     const Handle _java_thread;
2251     int _depth;
2252     GrowableArray<Method*>* _methods;
2253     GrowableArray<int>*     _bcis;
2254     GrowableArray<oop>*     _continuations;
2255 
2256     GetStackTraceClosure(Handle java_thread) :
2257       HandshakeClosure("GetStackTraceClosure"), _java_thread(java_thread), _depth(0) {
2258       // Pick some initial length
2259       int init_length = MaxJavaStackTraceDepth/2;
2260       _methods = new GrowableArray<Method*>(init_length);
2261       _bcis = new GrowableArray<int>(init_length);
2262       _continuations = new GrowableArray<oop>(init_length);
2263     }
2264 
2265     bool can_be_processed_by(Thread* thread) {
2266       return thread->is_Java_thread();
2267     }
2268 
2269     void do_thread(Thread* th) {
2270       JavaThread* thread = JavaThread::cast(th);
2271 
2272       if (!thread->has_last_Java_frame()) {
2273         return;
2274       }
2275 
2276       bool carrier = false;
2277       oop vthread_scope = java_lang_VirtualThread::vthread_scope();
2278       if (java_lang_VirtualThread::is_instance(_java_thread())) {
2279         // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
2280         if (thread->last_continuation(vthread_scope)->cont_oop() !=
2281               java_lang_VirtualThread::continuation(_java_thread())) {
2282           return; // not mounted
2283         }
2284       } else {
2285         carrier = (thread->last_continuation(vthread_scope) != NULL);
2286       }
2287 
2288       const int max_depth = MaxJavaStackTraceDepth;
2289       const bool skip_hidden = !ShowHiddenFrames;
2290 
2291       int total_count = 0;
2292       for (vframeStream vfst(thread, false, false, carrier);
2293            !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
2294            vfst.next()) {
2295 
2296         if (skip_hidden && (vfst.method()->is_hidden() ||
2297                             vfst.method()->is_continuation_enter_intrinsic())) continue;
2298         _methods->push(vfst.method());
2299         _bcis->push(vfst.bci());
2300         _continuations->push(contScopeName(vfst.continuation()));
2301         total_count++;
2302       }
2303 
2304       _depth = total_count;
2305     }
2306 
2307     oop contScopeName(oop cont) {
2308       return cont == NULL ? NULL : jdk_internal_vm_ContinuationScope::name(Continuation::continuation_scope(cont));
2309     }
2310   };
2311 
2312   // Handshake with target
2313   ResourceMark rm(THREAD);
2314   HandleMark   hm(THREAD);
2315   GetStackTraceClosure gstc(Handle(THREAD, java_thread));
2316   Handshake::execute(&gstc, &tlh, thread);
2317 
2318   // Stop if no stack trace is found.
2319   if (gstc._depth == 0) {
2320     return NULL;
2321   }
2322 
2323   // Convert the continuations into handles before allocation.
2324   assert(gstc._depth == gstc._continuations->length(), "should be the same");
2325   GrowableArray<Handle>* cont_handles = new GrowableArray<Handle>(gstc._depth);
2326   for (int i = 0; i < gstc._depth; i++) {
2327     cont_handles->push(Handle(THREAD, gstc._continuations->at(i)));
2328   }
2329 
2330   // Convert to StackTraceElement array
2331   InstanceKlass* k = vmClasses::StackTraceElement_klass();
2332   assert(k != NULL, "must be loaded in 1.4+");
2333   if (k->should_be_initialized()) k->initialize(CHECK_NULL);
2334   objArrayHandle trace = oopFactory::new_objArray_handle(k, gstc._depth, CHECK_NULL);
2335 
2336   for (int i = 0; i < gstc._depth; i++) {
2337     methodHandle method(THREAD, gstc._methods->at(i));
2338     oop element = java_lang_StackTraceElement::create(method,
2339                                                       gstc._bcis->at(i),
2340                                                       cont_handles->at(i),
2341                                                       CHECK_NULL);
2342     trace->obj_at_put(i, element);
2343   }
2344 
2345   return trace();
2346 }
2347 
2348 const char* java_lang_Thread::thread_status_name(oop java_thread) {
2349   oop holder = java_lang_Thread::holder(java_thread);
2350   assert(holder != NULL, "Java Thread not initialized");
2351   JavaThreadStatus status = java_lang_Thread_FieldHolder::get_thread_status(holder);
2352   switch (status) {
2353     case JavaThreadStatus::NEW                      : return "NEW";
2354     case JavaThreadStatus::RUNNABLE                 : return "RUNNABLE";
2355     case JavaThreadStatus::SLEEPING                 : return "TIMED_WAITING (sleeping)";
2356     case JavaThreadStatus::IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
2357     case JavaThreadStatus::IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
2358     case JavaThreadStatus::PARKED                   : return "WAITING (parking)";
2359     case JavaThreadStatus::PARKED_TIMED             : return "TIMED_WAITING (parking)";
2360     case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
2361     case JavaThreadStatus::TERMINATED               : return "TERMINATED";
2362     default                       : return "UNKNOWN";
2363   };
2364 }
2365 int java_lang_ThreadGroup::_parent_offset;
2366 int java_lang_ThreadGroup::_name_offset;


2367 int java_lang_ThreadGroup::_maxPriority_offset;



2368 int java_lang_ThreadGroup::_ngroups_offset;
2369 int java_lang_ThreadGroup::_groups_offset;
2370 int java_lang_ThreadGroup::_nweaks_offset;
2371 int java_lang_ThreadGroup::_weaks_offset;
2372 
2373 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
2374   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2375   return java_thread_group->obj_field(_parent_offset);
2376 }
2377 
2378 // ("name as oop" accessor is not necessary)
2379 
2380 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
2381   oop name = java_thread_group->obj_field(_name_offset);
2382   // ThreadGroup.name can be null
2383   if (name != NULL) {
2384     return java_lang_String::as_utf8_string(name);
2385   }
2386   return NULL;
2387 }
2388 
2389 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
2390   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2391   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);







2392 }
2393 
2394 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
2395   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2396   return java_thread_group->int_field(_ngroups_offset);
2397 }
2398 
2399 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
2400   oop groups = java_thread_group->obj_field(_groups_offset);
2401   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
2402   return objArrayOop(groups);
2403 }
2404 
2405 int java_lang_ThreadGroup::nweaks(oop java_thread_group) {





2406   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2407   return java_thread_group->int_field(_nweaks_offset);
2408 }
2409 
2410 objArrayOop java_lang_ThreadGroup::weaks(oop java_thread_group) {
2411   oop weaks = java_thread_group->obj_field(_weaks_offset);
2412   assert(weaks == NULL || weaks->is_objArray(), "just checking");
2413   return objArrayOop(weaks);
2414 }
2415 
2416 #define THREADGROUP_FIELDS_DO(macro) \
2417   macro(_parent_offset,      k, vmSymbols::parent_name(),      threadgroup_signature,       false); \
2418   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature,            false); \


2419   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
2420   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false); \
2421   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
2422   macro(_nweaks_offset,      k, vmSymbols::nweaks_name(),      int_signature,               false); \
2423   macro(_weaks_offset,       k, vmSymbols::weaks_name(),       weakreference_array_signature, false);
2424 
2425 void java_lang_ThreadGroup::compute_offsets() {
2426   assert(_parent_offset == 0, "offsets should be initialized only once");
2427 
2428   InstanceKlass* k = vmClasses::ThreadGroup_klass();
2429   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2430 }
2431 
2432 #if INCLUDE_CDS
2433 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
2434   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2435 }
2436 #endif
2437 
2438 
2439 // java_lang_VirtualThread
2440 
2441 int java_lang_VirtualThread::static_notify_jvmti_events_offset;
2442 int java_lang_VirtualThread::static_vthread_scope_offset;
2443 int java_lang_VirtualThread::_carrierThread_offset;
2444 int java_lang_VirtualThread::_continuation_offset;
2445 int java_lang_VirtualThread::_state_offset;
2446 
2447 #define VTHREAD_FIELDS_DO(macro) \
2448   macro(static_notify_jvmti_events_offset,  k, "notifyJvmtiEvents",  bool_signature, true); \
2449   macro(static_vthread_scope_offset,  k, "VTHREAD_SCOPE",  continuationscope_signature, true); \
2450   macro(_carrierThread_offset,  k, "carrierThread",  thread_signature, false); \
2451   macro(_continuation_offset,  k, "cont",  continuation_signature, false); \
2452   macro(_state_offset,  k, "state",  int_signature, false)
2453 
2454 static jboolean vthread_notify_jvmti_events = JNI_FALSE;
2455 
2456 void java_lang_VirtualThread::compute_offsets() {
2457   InstanceKlass* k = vmClasses::VirtualThread_klass();
2458   VTHREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2459 }
2460 
2461 void java_lang_VirtualThread::init_static_notify_jvmti_events() {
2462   if (vthread_notify_jvmti_events) {
2463     InstanceKlass* ik = vmClasses::VirtualThread_klass();
2464     oop base = ik->static_field_base_raw();
2465     base->release_bool_field_put(static_notify_jvmti_events_offset, vthread_notify_jvmti_events);
2466   }
2467 }
2468 
2469 bool java_lang_VirtualThread::is_instance(oop obj) {
2470   return obj != NULL && is_subclass(obj->klass());
2471 }
2472 
2473 oop java_lang_VirtualThread::carrier_thread(oop vthread) {
2474   oop thread = vthread->obj_field(_carrierThread_offset);
2475   return thread;
2476 }
2477 
2478 oop java_lang_VirtualThread::continuation(oop vthread) {
2479   oop cont = vthread->obj_field(_continuation_offset);
2480   return cont;
2481 }
2482 
2483 jshort java_lang_VirtualThread::state(oop vthread) {
2484   return vthread->short_field_acquire(_state_offset);
2485 }
2486 
2487 JavaThreadStatus java_lang_VirtualThread::map_state_to_thread_status(jint state) {
2488   JavaThreadStatus status = JavaThreadStatus::NEW;
2489   switch (state) {
2490     case NEW :
2491       status = JavaThreadStatus::NEW;
2492       break;
2493     case STARTED :
2494     case RUNNABLE :
2495     case RUNNABLE_SUSPENDED :
2496     case RUNNING :
2497     case PARKING :
2498     case YIELDING :
2499       status = JavaThreadStatus::RUNNABLE;
2500       break;
2501     case PARKED :
2502     case PARKED_SUSPENDED :
2503     case PINNED :
2504       status = JavaThreadStatus::PARKED;
2505       break;
2506     case TERMINATED :
2507       status = JavaThreadStatus::TERMINATED;
2508       break;
2509     default:
2510       ShouldNotReachHere();
2511   }
2512   return status;
2513 }
2514 
2515 #if INCLUDE_CDS
2516 void java_lang_VirtualThread::serialize_offsets(SerializeClosure* f) {
2517    VTHREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2518 }
2519 #endif
2520 
2521 bool java_lang_VirtualThread::notify_jvmti_events() {
2522   return vthread_notify_jvmti_events == JNI_TRUE;
2523 }
2524 
2525 void java_lang_VirtualThread::set_notify_jvmti_events(jboolean enable) {
2526   vthread_notify_jvmti_events = enable;
2527 }
2528 
2529 
2530 // java_lang_Throwable
2531 
2532 int java_lang_Throwable::_backtrace_offset;
2533 int java_lang_Throwable::_detailMessage_offset;
2534 int java_lang_Throwable::_stackTrace_offset;
2535 int java_lang_Throwable::_depth_offset;
2536 int java_lang_Throwable::_cause_offset;
2537 int java_lang_Throwable::_static_unassigned_stacktrace_offset;
2538 
2539 #define THROWABLE_FIELDS_DO(macro) \
2540   macro(_backtrace_offset,     k, "backtrace",     object_signature,                  false); \
2541   macro(_detailMessage_offset, k, "detailMessage", string_signature,                  false); \
2542   macro(_stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
2543   macro(_depth_offset,         k, "depth",         int_signature,                     false); \
2544   macro(_cause_offset,         k, "cause",         throwable_signature,               false); \
2545   macro(_static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
2546 
2547 void java_lang_Throwable::compute_offsets() {
2548   InstanceKlass* k = vmClasses::Throwable_klass();
2549   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);

2612 
2613 void java_lang_Throwable::print(oop throwable, outputStream* st) {
2614   ResourceMark rm;
2615   Klass* k = throwable->klass();
2616   assert(k != NULL, "just checking");
2617   st->print("%s", k->external_name());
2618   oop msg = message(throwable);
2619   if (msg != NULL) {
2620     st->print(": %s", java_lang_String::as_utf8_string(msg));
2621   }
2622 }
2623 
2624 // After this many redefines, the stack trace is unreliable.
2625 const int MAX_VERSION = USHRT_MAX;
2626 
2627 static inline bool version_matches(Method* method, int version) {
2628   assert(version < MAX_VERSION, "version is too big");
2629   return method != NULL && (method->constants()->version() == version);
2630 }
2631 





























































































































































































































2632 // Print stack trace element to resource allocated buffer
2633 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2634                                           int version, int bci, Symbol* name) {
2635   ResourceMark rm;
2636 
2637   // Get strings and string lengths
2638   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2639   const char* klass_name  = holder->external_name();
2640   int buf_len = (int)strlen(klass_name);
2641 
2642   char* method_name = name->as_C_string();
2643   buf_len += (int)strlen(method_name);
2644 
2645   char* source_file_name = NULL;
2646   Symbol* source = Backtrace::get_source_file_name(holder, version);
2647   if (source != NULL) {
2648     source_file_name = source->as_C_string();
2649     buf_len += (int)strlen(source_file_name);
2650   }
2651 

2760   }
2761 }
2762 
2763 /**
2764  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2765  */
2766 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2767   assert(throwable->is_a(vmClasses::Throwable_klass()), "Throwable instance expected");
2768   JavaValue result(T_VOID);
2769   JavaCalls::call_virtual(&result,
2770                           throwable,
2771                           vmClasses::Throwable_klass(),
2772                           vmSymbols::printStackTrace_name(),
2773                           vmSymbols::void_method_signature(),
2774                           THREAD);
2775 }
2776 
2777 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
2778   if (!StackTraceInThrowable) return;
2779   ResourceMark rm(THREAD);
2780   HandleMark hm(THREAD);
2781 
2782   // Start out by clearing the backtrace for this object, in case the VM
2783   // runs out of memory while allocating the stack trace
2784   set_backtrace(throwable(), NULL);
2785   // Clear lazily constructed Java level stacktrace if refilling occurs
2786   // This is unnecessary in 1.7+ but harmless
2787   clear_stacktrace(throwable());
2788 
2789   int max_depth = MaxJavaStackTraceDepth;
2790   JavaThread* thread = THREAD;
2791 
2792   BacktraceBuilder bt(CHECK);
2793 
2794   // If there is no Java frame just return the method that was being called
2795   // with bci 0
2796   if (!thread->has_last_Java_frame()) {
2797     if (max_depth >= 1 && method() != NULL) {
2798       bt.push(method(), 0, NULL, CHECK);
2799       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2800       set_depth(throwable(), 1);
2801       set_backtrace(throwable(), bt.backtrace());
2802     }
2803     return;
2804   }
2805 
2806   // Instead of using vframe directly, this version of fill_in_stack_trace
2807   // basically handles everything by hand. This significantly improved the
2808   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2809   // See bug 6333838 for  more details.
2810   // The "ASSERT" here is to verify this method generates the exactly same stack
2811   // trace as utilizing vframe.
2812 #ifdef ASSERT
2813   vframeStream st(thread, false /* stop_at_java_call_stub */, false /* process_frames */);
2814 #endif
2815   int total_count = 0;
2816   RegisterMap map(thread, false /* update */, false /* process_frames */, true /* walk_cont */);
2817   int decode_offset = 0;
2818   CompiledMethod* nm = NULL;
2819   bool skip_fillInStackTrace_check = false;
2820   bool skip_throwableInit_check = false;
2821   bool skip_hidden = !ShowHiddenFrames;
2822   bool show_carrier = ShowCarrierFrames;
2823   Handle cont_h(THREAD, thread->last_continuation()->cont_oop());
2824   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2825     Method* method = NULL;
2826     int bci = 0;
2827     oop contScopeName = (cont_h() != NULL) ? jdk_internal_vm_ContinuationScope::name(jdk_internal_vm_Continuation::scope(cont_h())) : (oop)NULL;
2828 
2829     // Compiled java method case.
2830     if (decode_offset != 0) {
2831       DebugInfoReadStream stream(nm, decode_offset);
2832       decode_offset = stream.read_int();
2833       method = (Method*)nm->metadata_at(stream.read_int());
2834       bci = stream.read_bci();
2835     } else {
2836       if (fr.is_first_frame()) break;
2837 
2838       if (cont_h() != NULL && Continuation::is_continuation_enterSpecial(fr)) {
2839         oop scope = jdk_internal_vm_Continuation::scope(cont_h());
2840         if (!show_carrier && scope == java_lang_VirtualThread::vthread_scope()) break;
2841 
2842         Handle parent_h(THREAD, jdk_internal_vm_Continuation::parent(cont_h()));
2843         cont_h =  parent_h;
2844       }
2845 
2846       address pc = fr.pc();
2847       if (fr.is_interpreted_frame()) {
2848         address bcp;
2849         if (!map.in_cont()) {
2850           bcp = fr.interpreter_frame_bcp();
2851           method = fr.interpreter_frame_method();
2852         } else {
2853           bcp = map.stack_chunk()->interpreter_frame_bcp(fr);
2854           method = map.stack_chunk()->interpreter_frame_method(fr);
2855         }
2856         bci =  method->bci_from(bcp);
2857         fr = fr.sender(&map);
2858       } else {
2859         CodeBlob* cb = fr.cb();
2860         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2861         // but non nmethod
2862         fr = fr.sender(&map);
2863         if (cb == NULL || !cb->is_compiled()) {
2864           continue;
2865         }
2866         nm = cb->as_compiled_method();
2867         assert (nm->method() != NULL, "must be");
2868         if (nm->method()->is_native()) {
2869           method = nm->method();
2870           bci = 0;
2871         } else {
2872           PcDesc* pd = nm->pc_desc_at(pc);
2873           decode_offset = pd->scope_decode_offset();
2874           // if decode_offset is not equal to 0, it will execute the
2875           // "compiled java method case" at the beginning of the loop.
2876           continue;
2877         }
2878       }
2879     }
2880 #ifdef ASSERT
2881     if (!st.at_end()) { // TODO LOOM remove once we show only vthread trace
2882       assert(st.method() == method && st.bci() == bci, "Wrong stack trace");
2883       st.next();
2884     }
2885 #endif
2886 
2887     // the format of the stacktrace will be:
2888     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2889     // - 0 or more <init> methods for the exception class (skipped)
2890     // - rest of the stack
2891 
2892     if (!skip_fillInStackTrace_check) {
2893       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2894           throwable->is_a(method->method_holder())) {
2895         continue;
2896       }
2897       else {
2898         skip_fillInStackTrace_check = true; // gone past them all
2899       }
2900     }
2901     if (!skip_throwableInit_check) {
2902       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2903 
2904       // skip <init> methods of the exception class and superclasses
2905       // This is simlar to classic VM.
2906       if (method->name() == vmSymbols::object_initializer_name() &&
2907           throwable->is_a(method->method_holder())) {
2908         continue;
2909       } else {
2910         // there are none or we've seen them all - either way stop checking
2911         skip_throwableInit_check = true;
2912       }
2913     }
2914     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2915       if (skip_hidden) {
2916         if (total_count == 0) {
2917           // The top frame will be hidden from the stack trace.
2918           bt.set_has_hidden_top_frame(CHECK);
2919         }
2920         continue;
2921       }
2922     }
2923 
2924     bt.push(method, bci, contScopeName, CHECK);
2925     total_count++;
2926   }
2927 
2928   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2929 
2930   // Put completed stack trace into throwable object
2931   set_backtrace(throwable(), bt.backtrace());
2932   set_depth(throwable(), total_count);
2933 }
2934 
2935 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2936   // No-op if stack trace is disabled
2937   if (!StackTraceInThrowable) {
2938     return;
2939   }
2940 
2941   // Disable stack traces for some preallocated out of memory errors
2942   if (!Universe::should_fill_in_stack_trace(throwable)) {
2943     return;
2944   }

2969   if (!StackTraceInThrowable) return;
2970 
2971   assert(throwable->is_a(vmClasses::Throwable_klass()), "sanity check");
2972 
2973   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2974 
2975   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2976   assert(backtrace.not_null(), "backtrace should have been preallocated");
2977 
2978   ResourceMark rm(THREAD);
2979   vframeStream st(THREAD, false /* stop_at_java_call_stub */, false /* process_frames */);
2980 
2981   BacktraceBuilder bt(THREAD, backtrace);
2982 
2983   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2984   // methods as preallocated errors aren't created by "java" code.
2985 
2986   // fill in as much stack trace as possible
2987   int chunk_count = 0;
2988   for (;!st.at_end(); st.next()) {
2989     bt.push(st.method(), st.bci(), NULL, CHECK);
2990     chunk_count++;
2991 
2992     // Bail-out for deep stacks
2993     if (chunk_count >= trace_chunk_size) break;
2994   }
2995   set_depth(throwable(), chunk_count);
2996   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2997 
2998   // We support the Throwable immutability protocol defined for Java 7.
2999   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
3000   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
3001 }
3002 
3003 void java_lang_Throwable::get_stack_trace_elements(int depth, Handle backtrace,
3004                                                    objArrayHandle stack_trace_array_h, TRAPS) {
3005 
3006   if (backtrace.is_null() || stack_trace_array_h.is_null()) {
3007     THROW(vmSymbols::java_lang_NullPointerException());
3008   }
3009 
3010   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
3011 
3012   if (stack_trace_array_h->length() != depth) {
3013     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
3014   }
3015 
3016   objArrayHandle result(THREAD, objArrayOop(backtrace()));
3017   BacktraceIterator iter(result, THREAD);
3018 
3019   int index = 0;
3020   while (iter.repeat()) {
3021     BacktraceElement bte = iter.next(THREAD);
3022 
3023     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
3024 
3025     if (stack_trace_element.is_null()) {
3026       THROW(vmSymbols::java_lang_NullPointerException());
3027     }
3028 
3029     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
3030     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
3031 
3032     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
3033                                          method,
3034                                          bte._version,
3035                                          bte._bci,
3036                                          bte._name,
3037                                          bte._cont,
3038                                          CHECK);
3039   }
3040 }
3041 
3042 Handle java_lang_Throwable::get_cause_with_stack_trace(Handle throwable, TRAPS) {
3043   // Call to JVM to fill in the stack trace and clear declaringClassObject to
3044   // not keep classes alive in the stack trace.
3045   // call this:  public StackTraceElement[] getStackTrace()
3046   assert(throwable.not_null(), "shouldn't be");
3047 
3048   JavaValue result(T_ARRAY);
3049   JavaCalls::call_virtual(&result, throwable,
3050                           vmClasses::Throwable_klass(),
3051                           vmSymbols::getStackTrace_name(),
3052                           vmSymbols::getStackTrace_signature(),
3053                           CHECK_NH);
3054   Handle stack_trace(THREAD, result.get_oop());
3055   assert(stack_trace->is_objArray(), "Should be an array");
3056 
3057   // Throw ExceptionInInitializerError as the cause with this exception in
3058   // the message and stack trace.

3098     return false;
3099   }
3100 
3101   // Get first backtrace element.
3102   BacktraceElement bte = iter.next(current);
3103 
3104   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
3105   assert(holder != NULL, "first element should be non-null");
3106   Method* m = holder->method_with_orig_idnum(bte._method_id, bte._version);
3107 
3108   // Original version is no longer available.
3109   if (m == NULL || !version_matches(m, bte._version)) {
3110     return false;
3111   }
3112 
3113   *method = m;
3114   *bci = bte._bci;
3115   return true;
3116 }
3117 
3118 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, Handle contScope, TRAPS) {
3119   // Allocate java.lang.StackTraceElement instance
3120   InstanceKlass* k = vmClasses::StackTraceElement_klass();
3121   assert(k != NULL, "must be loaded in 1.4+");
3122   if (k->should_be_initialized()) {
3123     k->initialize(CHECK_NULL);
3124   }
3125 
3126   Handle element = k->allocate_instance_handle(CHECK_NULL);
3127 
3128   int version = method->constants()->version();
3129   fill_in(element, method->method_holder(), method, version, bci, method->name(), contScope, CHECK_NULL);
3130   return element();
3131 }
3132 
3133 void java_lang_StackTraceElement::fill_in(Handle element,
3134                                           InstanceKlass* holder, const methodHandle& method,
3135                                           int version, int bci, Symbol* name, Handle contScopeName, TRAPS) {
3136   assert(element->is_a(vmClasses::StackTraceElement_klass()), "sanity check");
3137 
3138   ResourceMark rm(THREAD);
3139   HandleMark hm(THREAD);
3140 
3141   // Fill in class name
3142   Handle java_class(THREAD, holder->java_mirror());
3143   oop classname = java_lang_Class::name(java_class, CHECK);
3144   java_lang_StackTraceElement::set_declaringClass(element(), classname);
3145   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
3146 
3147   oop loader = holder->class_loader();
3148   if (loader != NULL) {
3149     oop loader_name = java_lang_ClassLoader::name(loader);
3150     if (loader_name != NULL)
3151       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
3152   }
3153 
3154   // Fill in method name
3155   oop methodname = StringTable::intern(name, CHECK);

3165       module_version = StringTable::intern(module->version(), CHECK);
3166     } else {
3167       module_version = NULL;
3168     }
3169     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
3170   }
3171 
3172   if (method() == NULL || !version_matches(method(), version)) {
3173     // The method was redefined, accurate line number information isn't available
3174     java_lang_StackTraceElement::set_fileName(element(), NULL);
3175     java_lang_StackTraceElement::set_lineNumber(element(), -1);
3176   } else {
3177     Symbol* source;
3178     oop source_file;
3179     int line_number;
3180     decode_file_and_line(java_class, holder, version, method, bci, source, source_file, line_number, CHECK);
3181 
3182     java_lang_StackTraceElement::set_fileName(element(), source_file);
3183     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
3184   }
3185 
3186   // Fill in continuation scope
3187   java_lang_StackTraceElement::set_contScopeName(element(), contScopeName());
3188 }
3189 
3190 void java_lang_StackTraceElement::decode_file_and_line(Handle java_class,
3191                                                        InstanceKlass* holder,
3192                                                        int version,
3193                                                        const methodHandle& method,
3194                                                        int bci,
3195                                                        Symbol*& source,
3196                                                        oop& source_file,
3197                                                        int& line_number, TRAPS) {
3198   // Fill in source file name and line number.
3199   source = Backtrace::get_source_file_name(holder, version);
3200   source_file = java_lang_Class::source_file(java_class());
3201   if (source != NULL) {
3202     // Class was not redefined. We can trust its cache if set,
3203     // else we have to initialize it.
3204     if (source_file == NULL) {
3205       source_file = StringTable::intern(source, CHECK);
3206       java_lang_Class::set_source_file(java_class(), source_file);
3207     }

3220                                          Symbol*& filename, int& line_number, TRAPS) {
3221   ResourceMark rm(THREAD);
3222   HandleMark hm(THREAD);
3223 
3224   filename = NULL;
3225   line_number = -1;
3226 
3227   oop source_file;
3228   int version = method->constants()->version();
3229   InstanceKlass* holder = method->method_holder();
3230   Handle java_class(THREAD, holder->java_mirror());
3231   decode_file_and_line(java_class, holder, version, method, bci, filename, source_file, line_number, CHECK);
3232 }
3233 #endif // INCLUDE_JVMCI
3234 
3235 // java_lang_StackFrameInfo
3236 
3237 int java_lang_StackFrameInfo::_memberName_offset;
3238 int java_lang_StackFrameInfo::_bci_offset;
3239 int java_lang_StackFrameInfo::_version_offset;
3240 int java_lang_StackFrameInfo::_contScope_offset;
3241 
3242 #define STACKFRAMEINFO_FIELDS_DO(macro) \
3243   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
3244   macro(_bci_offset,            k, "bci",         int_signature,    false); \
3245   macro(_contScope_offset,      k, "contScope",   continuationscope_signature, false)
3246 
3247 void java_lang_StackFrameInfo::compute_offsets() {
3248   InstanceKlass* k = vmClasses::StackFrameInfo_klass();
3249   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3250   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3251 }
3252 
3253 #if INCLUDE_CDS
3254 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
3255   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3256   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3257 }
3258 #endif
3259 
3260 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
3261   HandleMark hm(THREAD);
3262   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
3263   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
3264   // we should expand MemberName::name when Throwable uses StackTrace
3265   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
3266   return method;
3267 }
3268 
3269 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, oop cont, TRAPS) {
3270   // set Method* or mid/cpref
3271   HandleMark hm(THREAD);
3272   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
3273   Handle cont_h (THREAD, cont);
3274   InstanceKlass* ik = method->method_holder();
3275   CallInfo info(method(), ik, CHECK);
3276   MethodHandles::init_method_MemberName(mname, info);
3277   // set bci
3278   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
3279   // method may be redefined; store the version
3280   int version = method->constants()->version();
3281   assert((jushort)version == version, "version should be short");
3282   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
3283 
3284   oop contScope = cont_h() != NULL ? jdk_internal_vm_Continuation::scope(cont_h()) : (oop)NULL;
3285   java_lang_StackFrameInfo::set_contScope(stackFrame(), contScope);
3286 }
3287 
3288 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
3289   ResourceMark rm(THREAD);
3290   HandleMark hm(THREAD);
3291   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
3292   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
3293   InstanceKlass* holder = InstanceKlass::cast(clazz);
3294   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
3295   oop contScope = stackFrame->obj_field(java_lang_StackFrameInfo::_contScope_offset);
3296   Handle contScopeName(THREAD, contScope != (oop)NULL ? jdk_internal_vm_ContinuationScope::name(contScope) : (oop)NULL);
3297 
3298   short version = stackFrame->short_field(_version_offset);
3299   int bci = stackFrame->int_field(_bci_offset);
3300   Symbol* name = method->name();
3301   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, methodHandle(THREAD, method), version, bci, name, contScopeName, CHECK);
3302 }
3303 
3304 void java_lang_StackTraceElement::set_contScopeName(oop element, oop value) {
3305   element->obj_field_put(_contScopeName_offset, value);
3306 }
3307 
3308 void java_lang_StackFrameInfo::set_version(oop element, short value) {
3309   element->short_field_put(_version_offset, value);
3310 }
3311 
3312 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
3313   assert(value >= 0 && value < max_jushort, "must be a valid bci value");
3314   element->int_field_put(_bci_offset, value);
3315 }
3316 
3317 void java_lang_StackFrameInfo::set_contScope(oop element, oop value) {
3318   element->obj_field_put(_contScope_offset, value);
3319 }
3320 
3321 int java_lang_LiveStackFrameInfo::_monitors_offset;
3322 int java_lang_LiveStackFrameInfo::_locals_offset;
3323 int java_lang_LiveStackFrameInfo::_operands_offset;
3324 int java_lang_LiveStackFrameInfo::_mode_offset;
3325 
3326 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
3327   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
3328   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
3329   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
3330   macro(_mode_offset,       k, "mode",        int_signature,          false)
3331 
3332 void java_lang_LiveStackFrameInfo::compute_offsets() {
3333   InstanceKlass* k = vmClasses::LiveStackFrameInfo_klass();
3334   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3335 }
3336 
3337 #if INCLUDE_CDS
3338 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) {
3339   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3340 }

4889     _use_unaligned_access = UseUnalignedAccesses;
4890     _data_cache_line_flush_size = (int)VM_Version::data_cache_line_flush_size();
4891   }
4892 
4893   void do_field(fieldDescriptor* fd) {
4894     oop mirror = fd->field_holder()->java_mirror();
4895     assert(mirror != NULL, "UnsafeConstants must have mirror already");
4896     assert(fd->field_holder() == vmClasses::UnsafeConstants_klass(), "Should be UnsafeConstants");
4897     assert(fd->is_final(), "fields of UnsafeConstants must be final");
4898     assert(fd->is_static(), "fields of UnsafeConstants must be static");
4899     if (fd->name() == vmSymbols::address_size_name()) {
4900       mirror->int_field_put(fd->offset(), _address_size);
4901     } else if (fd->name() == vmSymbols::page_size_name()) {
4902       mirror->int_field_put(fd->offset(), _page_size);
4903     } else if (fd->name() == vmSymbols::big_endian_name()) {
4904       mirror->bool_field_put(fd->offset(), _big_endian);
4905     } else if (fd->name() == vmSymbols::use_unaligned_access_name()) {
4906       mirror->bool_field_put(fd->offset(), _use_unaligned_access);
4907     } else if (fd->name() == vmSymbols::data_cache_line_flush_size_name()) {
4908       mirror->int_field_put(fd->offset(), _data_cache_line_flush_size);
4909     } else if (fd->name() == vmSymbols::scoped_cache_shift_name()) {
4910       mirror->int_field_put(fd->offset(), ScopeLocalCacheSize ? exact_log2(ScopeLocalCacheSize) : -1);
4911     } else {
4912       assert(false, "unexpected UnsafeConstants field");
4913     }
4914   }
4915 };
4916 
4917 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4918   UnsafeConstantsFixup fixup;
4919   vmClasses::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4920 }
4921 
4922 
4923 // java_lang_StackTraceElement
4924 
4925 int java_lang_StackTraceElement::_methodName_offset;
4926 int java_lang_StackTraceElement::_fileName_offset;
4927 int java_lang_StackTraceElement::_lineNumber_offset;
4928 int java_lang_StackTraceElement::_moduleName_offset;
4929 int java_lang_StackTraceElement::_moduleVersion_offset;
4930 int java_lang_StackTraceElement::_classLoaderName_offset;
4931 int java_lang_StackTraceElement::_declaringClass_offset;
4932 int java_lang_StackTraceElement::_declaringClassObject_offset;
4933 int java_lang_StackTraceElement::_contScopeName_offset;
4934 
4935 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4936   macro(_declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4937   macro(_classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4938   macro(_moduleName_offset,      k, "moduleName",      string_signature, false); \
4939   macro(_moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4940   macro(_declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4941   macro(_methodName_offset,      k, "methodName",      string_signature, false); \
4942   macro(_fileName_offset,        k, "fileName",        string_signature, false); \
4943   macro(_lineNumber_offset,      k, "lineNumber",      int_signature,    false); \
4944   macro(_contScopeName_offset,   k, "contScopeName",   string_signature, false)
4945 
4946 // Support for java_lang_StackTraceElement
4947 void java_lang_StackTraceElement::compute_offsets() {
4948   InstanceKlass* k = vmClasses::StackTraceElement_klass();
4949   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4950 }
4951 
4952 #if INCLUDE_CDS
4953 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) {
4954   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4955 }
4956 #endif
4957 
4958 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4959   element->obj_field_put(_fileName_offset, value);
4960 }
4961 
4962 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4963   element->obj_field_put(_declaringClass_offset, value);
4964 }

5018 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
5019   o->obj_field_put(_classes_offset, val);
5020 }
5021 
5022 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
5023   o->obj_field_put(_classEnabled_offset, val);
5024 }
5025 
5026 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
5027   o->obj_field_put(_packages_offset, val);
5028 }
5029 
5030 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
5031   o->obj_field_put(_packageEnabled_offset, val);
5032 }
5033 
5034 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
5035   o->bool_field_put(_deflt_offset, val);
5036 }
5037 
5038 // Support for jdk.internal.vm.Continuation
5039 
5040 int jdk_internal_vm_ContinuationScope::_name_offset;
5041 int jdk_internal_vm_Continuation::_scope_offset;
5042 int jdk_internal_vm_Continuation::_target_offset;
5043 int jdk_internal_vm_Continuation::_tail_offset;
5044 int jdk_internal_vm_Continuation::_parent_offset;
5045 int jdk_internal_vm_Continuation::_yieldInfo_offset;
5046 int jdk_internal_vm_Continuation::_cs_offset;
5047 int jdk_internal_vm_Continuation::_mounted_offset;
5048 int jdk_internal_vm_Continuation::_done_offset;
5049 int jdk_internal_vm_Continuation::_preempted_offset;
5050 
5051 #define CONTINUATIONSCOPE_FIELDS_DO(macro) \
5052   macro(_name_offset, k, vmSymbols::name_name(), string_signature, false);
5053 
5054 void jdk_internal_vm_ContinuationScope::compute_offsets() {
5055   InstanceKlass* k = vmClasses::ContinuationScope_klass();
5056   CONTINUATIONSCOPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5057 }
5058 
5059 #if INCLUDE_CDS
5060 void jdk_internal_vm_ContinuationScope::serialize_offsets(SerializeClosure* f) {
5061   CONTINUATIONSCOPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5062 }
5063 #endif
5064 
5065 // Support for jdk.internal.vm.Continuation
5066 
5067 #define CONTINUATION_FIELDS_DO(macro) \
5068   macro(_scope_offset,     k, vmSymbols::scope_name(),     continuationscope_signature, false); \
5069   macro(_target_offset,    k, vmSymbols::target_name(),    runnable_signature,          false); \
5070   macro(_parent_offset,    k, vmSymbols::parent_name(),    continuation_signature,      false); \
5071   macro(_yieldInfo_offset, k, vmSymbols::yieldInfo_name(), object_signature,            false); \
5072   macro(_tail_offset,      k, vmSymbols::tail_name(),      stackchunk_signature,        false); \
5073   macro(_cs_offset,        k, vmSymbols::cs_name(),        short_signature,             false); \
5074   macro(_mounted_offset,   k, vmSymbols::mounted_name(),   bool_signature,              false); \
5075   macro(_done_offset,      k, vmSymbols::done_name(),      bool_signature,              false); \
5076   macro(_preempted_offset, k, "preempted",                 bool_signature,              false);
5077 
5078 void jdk_internal_vm_Continuation::compute_offsets() {
5079   InstanceKlass* k = vmClasses::Continuation_klass();
5080   CONTINUATION_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5081 }
5082 
5083 #if INCLUDE_CDS
5084 void jdk_internal_vm_Continuation::serialize_offsets(SerializeClosure* f) {
5085   CONTINUATION_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5086 }
5087 #endif
5088 
5089 // Support for jdk.internal.vm.StackChunk
5090 
5091 int jdk_internal_vm_StackChunk::_parent_offset;
5092 int jdk_internal_vm_StackChunk::_size_offset;
5093 int jdk_internal_vm_StackChunk::_sp_offset;
5094 int jdk_internal_vm_StackChunk::_pc_offset;
5095 int jdk_internal_vm_StackChunk::_argsize_offset;
5096 int jdk_internal_vm_StackChunk::_flags_offset;
5097 int jdk_internal_vm_StackChunk::_gcSP_offset;
5098 int jdk_internal_vm_StackChunk::_markCycle_offset;
5099 int jdk_internal_vm_StackChunk::_maxSize_offset;
5100 int jdk_internal_vm_StackChunk::_cont_offset;
5101 
5102 #define STACKCHUNK_FIELDS_DO(macro) \
5103   macro(_parent_offset,    k, vmSymbols::parent_name(),    stackchunk_signature, false); \
5104   macro(_size_offset,      k, vmSymbols::size_name(),      int_signature,        false); \
5105   macro(_sp_offset,        k, vmSymbols::sp_name(),        int_signature,        false); \
5106   macro(_pc_offset,        k, vmSymbols::pc_name(),        long_signature,       false); \
5107   macro(_argsize_offset,   k, vmSymbols::argsize_name(),   int_signature,        false); \
5108   macro(_flags_offset,     k, "flags",                     byte_signature,       false); \
5109   macro(_gcSP_offset,      k, "gcSP",                      int_signature,        false); \
5110   macro(_markCycle_offset, k, "markCycle",                 long_signature,       false); \
5111   macro(_maxSize_offset,   k, vmSymbols::maxSize_name(),   int_signature,        false); \
5112   macro(_cont_offset,      k, "cont",                      continuation_signature, false);
5113 
5114 void jdk_internal_vm_StackChunk::compute_offsets() {
5115   InstanceKlass* k = vmClasses::StackChunk_klass();
5116   STACKCHUNK_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5117 }
5118 
5119 #if INCLUDE_CDS
5120 void jdk_internal_vm_StackChunk::serialize_offsets(SerializeClosure* f) {
5121   STACKCHUNK_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5122 }
5123 #endif
5124 
5125 
5126 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset;
5127 
5128 #define AOS_FIELDS_DO(macro) \
5129   macro(_owner_offset, k, "exclusiveOwnerThread", thread_signature, false)
5130 
5131 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() {
5132   InstanceKlass* k = vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass();
5133   AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5134 }
5135 
5136 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
5137   assert(_owner_offset != 0, "Must be initialized");
5138   return obj->obj_field(_owner_offset);
5139 }
5140 
5141 #if INCLUDE_CDS
5142 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) {
5143   AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5144 }
5145 #endif

5528 void JavaClasses::check_offsets() {
5529   bool valid = true;
5530 
5531 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5532   valid &= check_offset(klass_name, cpp_klass_name :: _##field_name ## _offset, #field_name, field_sig)
5533 
5534 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5535   valid &= check_offset(klass_name, cpp_klass_name :: _##long_ ## field_name ## _offset, #field_name, field_sig)
5536 
5537   // Boxed primitive objects (java_lang_boxing_object)
5538 
5539   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
5540   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
5541   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
5542   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
5543   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
5544   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
5545   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
5546   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
5547 
5548   // jdk.internal.vm.Continuation
5549 
5550   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, target,   "Ljava/lang/Runnable;");
5551   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, stack,    "[I");
5552   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, parent,   "Ljdk/internal/vm/Continuation;");
5553   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, lastFP,   "I");
5554   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, lastSP,   "I");
5555 
5556   if (!valid) vm_exit_during_initialization("Field offset verification failed");
5557 }
5558 
5559 #endif // PRODUCT
5560 
5561 int InjectedField::compute_offset() {
5562   InstanceKlass* ik = InstanceKlass::cast(klass());
5563   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5564     if (!may_be_java && !fs.access_flags().is_internal()) {
5565       // Only look at injected fields
5566       continue;
5567     }
5568     if (fs.name() == name() && fs.signature() == signature()) {
5569       return fs.offset();
5570     }
5571   }
5572   ResourceMark rm;
5573   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5574 #ifndef PRODUCT
5575   ik->print();
5576   tty->print_cr("all fields:");
5577   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5578     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
5579   }
5580 #endif //PRODUCT
5581   vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
5582   return -1;
5583 }
5584 
5585 void javaClasses_init() {
5586   JavaClasses::compute_offsets();
5587   JavaClasses::check_offsets();
5588   java_lang_VirtualThread::init_static_notify_jvmti_events();
5589   FilteredFieldsMap::initialize();  // must be done after computing offsets.
5590 }
< prev index next >