< 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.

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























































































































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

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

1685 
1686 #define THREAD_FIELDS_DO(macro) \

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

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

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

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
















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


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


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


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


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


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


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


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


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

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





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







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

















































































































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


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



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

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



























































































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

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

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

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

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

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









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






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

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

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

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

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


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

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

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



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

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

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

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

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



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


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



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




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

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


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

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

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

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






























































































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

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








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

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

 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.

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

2034 int java_lang_Thread::_contextClassLoader_offset;
2035 int java_lang_Thread::_inheritedAccessControlContext_offset;

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



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

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


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


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

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

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

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


2362 int java_lang_ThreadGroup::_maxPriority_offset;



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







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





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


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

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





























































































































































































































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

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

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

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

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

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

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

5012 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
5013   o->obj_field_put(_classes_offset, val);
5014 }
5015 
5016 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
5017   o->obj_field_put(_classEnabled_offset, val);
5018 }
5019 
5020 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
5021   o->obj_field_put(_packages_offset, val);
5022 }
5023 
5024 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
5025   o->obj_field_put(_packageEnabled_offset, val);
5026 }
5027 
5028 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
5029   o->bool_field_put(_deflt_offset, val);
5030 }
5031 
5032 // Support for jdk.internal.vm.Continuation
5033 
5034 int jdk_internal_vm_ContinuationScope::_name_offset;
5035 int jdk_internal_vm_Continuation::_scope_offset;
5036 int jdk_internal_vm_Continuation::_target_offset;
5037 int jdk_internal_vm_Continuation::_tail_offset;
5038 int jdk_internal_vm_Continuation::_parent_offset;
5039 int jdk_internal_vm_Continuation::_yieldInfo_offset;
5040 int jdk_internal_vm_Continuation::_cs_offset;
5041 int jdk_internal_vm_Continuation::_reset_offset;
5042 int jdk_internal_vm_Continuation::_mounted_offset;
5043 int jdk_internal_vm_Continuation::_done_offset;
5044 int jdk_internal_vm_Continuation::_preempted_offset;
5045 
5046 #define CONTINUATIONSCOPE_FIELDS_DO(macro) \
5047   macro(_name_offset, k, vmSymbols::name_name(), string_signature, false);
5048 
5049 void jdk_internal_vm_ContinuationScope::compute_offsets() {
5050   InstanceKlass* k = vmClasses::ContinuationScope_klass();
5051   CONTINUATIONSCOPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5052 }
5053 
5054 #if INCLUDE_CDS
5055 void jdk_internal_vm_ContinuationScope::serialize_offsets(SerializeClosure* f) {
5056   CONTINUATIONSCOPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5057 }
5058 #endif
5059 
5060 // Support for jdk.internal.vm.Continuation
5061 
5062 #define CONTINUATION_FIELDS_DO(macro) \
5063   macro(_scope_offset,     k, vmSymbols::scope_name(),     continuationscope_signature, false); \
5064   macro(_target_offset,    k, vmSymbols::target_name(),    runnable_signature,          false); \
5065   macro(_parent_offset,    k, vmSymbols::parent_name(),    continuation_signature,      false); \
5066   macro(_yieldInfo_offset, k, vmSymbols::yieldInfo_name(), object_signature,            false); \
5067   macro(_tail_offset,      k, vmSymbols::tail_name(),      stackchunk_signature,        false); \
5068   macro(_cs_offset,        k, vmSymbols::cs_name(),        short_signature,             false); \
5069   macro(_reset_offset,     k, vmSymbols::reset_name(),     bool_signature,              false); \
5070   macro(_mounted_offset,   k, vmSymbols::mounted_name(),   bool_signature,              false); \
5071   macro(_done_offset,      k, vmSymbols::done_name(),      bool_signature,              false); \
5072   macro(_preempted_offset, k, "preempted",                 bool_signature,              false);
5073 
5074 void jdk_internal_vm_Continuation::compute_offsets() {
5075   InstanceKlass* k = vmClasses::Continuation_klass();
5076   CONTINUATION_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5077 }
5078 
5079 #if INCLUDE_CDS
5080 void jdk_internal_vm_Continuation::serialize_offsets(SerializeClosure* f) {
5081   CONTINUATION_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5082 }
5083 #endif
5084 
5085 // Support for jdk.internal.vm.StackChunk
5086 
5087 int jdk_internal_vm_StackChunk::_parent_offset;
5088 int jdk_internal_vm_StackChunk::_size_offset;
5089 int jdk_internal_vm_StackChunk::_sp_offset;
5090 int jdk_internal_vm_StackChunk::_pc_offset;
5091 int jdk_internal_vm_StackChunk::_argsize_offset;
5092 int jdk_internal_vm_StackChunk::_flags_offset;
5093 int jdk_internal_vm_StackChunk::_gcSP_offset;
5094 int jdk_internal_vm_StackChunk::_markCycle_offset;
5095 int jdk_internal_vm_StackChunk::_maxSize_offset;
5096 int jdk_internal_vm_StackChunk::_numFrames_offset;
5097 int jdk_internal_vm_StackChunk::_numOops_offset;
5098 int jdk_internal_vm_StackChunk::_cont_offset;
5099 
5100 #define STACKCHUNK_FIELDS_DO(macro) \
5101   macro(_parent_offset,    k, vmSymbols::parent_name(),    stackchunk_signature, false); \
5102   macro(_size_offset,      k, vmSymbols::size_name(),      int_signature,        false); \
5103   macro(_sp_offset,        k, vmSymbols::sp_name(),        int_signature,        false); \
5104   macro(_pc_offset,        k, vmSymbols::pc_name(),        long_signature,       false); \
5105   macro(_argsize_offset,   k, vmSymbols::argsize_name(),   int_signature,        false); \
5106   macro(_flags_offset,     k, "flags",                     byte_signature,       false); \
5107   macro(_gcSP_offset,      k, "gcSP",                      int_signature,        false); \
5108   macro(_markCycle_offset, k, "markCycle",                 long_signature,       false); \
5109   macro(_maxSize_offset,   k, vmSymbols::maxSize_name(),   int_signature,        false); \
5110   macro(_numFrames_offset, k, vmSymbols::numFrames_name(), int_signature,        false); \
5111   macro(_numOops_offset,   k, vmSymbols::numOops_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 >