< prev index next >

src/hotspot/share/classfile/javaClasses.cpp

Print this page




1595 
1596 
1597 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1598 //       platform thread structure, and a eetop offset which was used for thread
1599 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1600 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1601 //       instead of the privateInfo_offset.
1602 //
1603 // Note: The stackSize field is only present starting in 1.4.
1604 
1605 int java_lang_Thread::_name_offset = 0;
1606 int java_lang_Thread::_group_offset = 0;
1607 int java_lang_Thread::_contextClassLoader_offset = 0;
1608 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1609 int java_lang_Thread::_priority_offset = 0;
1610 int java_lang_Thread::_eetop_offset = 0;
1611 int java_lang_Thread::_daemon_offset = 0;
1612 int java_lang_Thread::_stillborn_offset = 0;
1613 int java_lang_Thread::_stackSize_offset = 0;
1614 int java_lang_Thread::_tid_offset = 0;


1615 int java_lang_Thread::_thread_status_offset = 0;
1616 int java_lang_Thread::_park_blocker_offset = 0;
1617 
1618 #define THREAD_FIELDS_DO(macro) \
1619   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1620   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1621   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1622   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1623   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1624   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1625   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1626   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1627   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1628   macro(_tid_offset,           k, "tid", long_signature, false); \
1629   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1630   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false)


1631 
1632 void java_lang_Thread::compute_offsets() {
1633   assert(_group_offset == 0, "offsets should be initialized only once");
1634 
1635   InstanceKlass* k = SystemDictionary::Thread_klass();
1636   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1637 }
1638 
1639 #if INCLUDE_CDS
1640 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
1641   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1642 }
1643 #endif
1644 
1645 JavaThread* java_lang_Thread::thread(oop java_thread) {
1646   return (JavaThread*)java_thread->address_field(_eetop_offset);
1647 }
1648 
1649 
1650 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {


1720 void java_lang_Thread::set_thread_status(oop java_thread,
1721                                          java_lang_Thread::ThreadStatus status) {
1722   java_thread->int_field_put(_thread_status_offset, status);
1723 }
1724 
1725 // Read thread status value from threadStatus field in java.lang.Thread java class.
1726 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1727   // Make sure the caller is operating on behalf of the VM or is
1728   // running VM code (state == _thread_in_vm).
1729   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1730          JavaThread::current()->thread_state() == _thread_in_vm,
1731          "Java Thread is not running in vm");
1732   return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1733 }
1734 
1735 
1736 jlong java_lang_Thread::thread_id(oop java_thread) {
1737   return java_thread->long_field(_tid_offset);
1738 }
1739 













1740 oop java_lang_Thread::park_blocker(oop java_thread) {
1741   return java_thread->obj_field(_park_blocker_offset);
1742 }
1743 
1744 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1745   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1746   switch (status) {
1747     case NEW                      : return "NEW";
1748     case RUNNABLE                 : return "RUNNABLE";
1749     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1750     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1751     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1752     case PARKED                   : return "WAITING (parking)";
1753     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1754     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1755     case TERMINATED               : return "TERMINATED";
1756     default                       : return "UNKNOWN";
1757   };
1758 }
1759 int java_lang_ThreadGroup::_parent_offset = 0;


1827   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
1828   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
1829   macro(_destroyed_offset,   k, vmSymbols::destroyed_name(),   bool_signature,              false); \
1830   macro(_daemon_offset,      k, vmSymbols::daemon_name(),      bool_signature,              false); \
1831   macro(_nthreads_offset,    k, vmSymbols::nthreads_name(),    int_signature,               false); \
1832   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false)
1833 
1834 void java_lang_ThreadGroup::compute_offsets() {
1835   assert(_parent_offset == 0, "offsets should be initialized only once");
1836 
1837   InstanceKlass* k = SystemDictionary::ThreadGroup_klass();
1838   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1839 }
1840 
1841 #if INCLUDE_CDS
1842 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
1843   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1844 }
1845 #endif
1846 




















































1847 #define THROWABLE_FIELDS_DO(macro) \
1848   macro(backtrace_offset,     k, "backtrace",     object_signature,                  false); \
1849   macro(detailMessage_offset, k, "detailMessage", string_signature,                  false); \
1850   macro(stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
1851   macro(depth_offset,         k, "depth",         int_signature,                     false); \
1852   macro(static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
1853 
1854 void java_lang_Throwable::compute_offsets() {
1855   InstanceKlass* k = SystemDictionary::Throwable_klass();
1856   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1857 }
1858 
1859 #if INCLUDE_CDS
1860 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) {
1861   THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1862 }
1863 #endif
1864 
1865 oop java_lang_Throwable::unassigned_stacktrace() {
1866   InstanceKlass* ik = SystemDictionary::Throwable_klass();


1929 const int MAX_VERSION = USHRT_MAX;
1930 
1931 static inline bool version_matches(Method* method, int version) {
1932   assert(version < MAX_VERSION, "version is too big");
1933   return method != NULL && (method->constants()->version() == version);
1934 }
1935 
1936 
1937 // This class provides a simple wrapper over the internal structure of
1938 // exception backtrace to insulate users of the backtrace from needing
1939 // to know what it looks like.
1940 class BacktraceBuilder: public StackObj {
1941  friend class BacktraceIterator;
1942  private:
1943   Handle          _backtrace;
1944   objArrayOop     _head;
1945   typeArrayOop    _methods;
1946   typeArrayOop    _bcis;
1947   objArrayOop     _mirrors;
1948   typeArrayOop    _names; // needed to insulate method name against redefinition

1949   int             _index;
1950   NoSafepointVerifier _nsv;
1951 
1952   enum {
1953     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
1954     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
1955     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
1956     trace_names_offset   = java_lang_Throwable::trace_names_offset,

1957     trace_next_offset    = java_lang_Throwable::trace_next_offset,
1958     trace_size           = java_lang_Throwable::trace_size,
1959     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
1960   };
1961 
1962   // get info out of chunks
1963   static typeArrayOop get_methods(objArrayHandle chunk) {
1964     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
1965     assert(methods != NULL, "method array should be initialized in backtrace");
1966     return methods;
1967   }
1968   static typeArrayOop get_bcis(objArrayHandle chunk) {
1969     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
1970     assert(bcis != NULL, "bci array should be initialized in backtrace");
1971     return bcis;
1972   }
1973   static objArrayOop get_mirrors(objArrayHandle chunk) {
1974     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
1975     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
1976     return mirrors;
1977   }
1978   static typeArrayOop get_names(objArrayHandle chunk) {
1979     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
1980     assert(names != NULL, "names array should be initialized in backtrace");
1981     return names;
1982   }





1983 
1984  public:
1985 
1986   // constructor for new backtrace
1987   BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL) {
1988     expand(CHECK);
1989     _backtrace = Handle(THREAD, _head);
1990     _index = 0;
1991   }
1992 
1993   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
1994     _methods = get_methods(backtrace);
1995     _bcis = get_bcis(backtrace);
1996     _mirrors = get_mirrors(backtrace);
1997     _names = get_names(backtrace);

1998     assert(_methods->length() == _bcis->length() &&
1999            _methods->length() == _mirrors->length() &&
2000            _mirrors->length() == _names->length(),

2001            "method and source information arrays should match");
2002 
2003     // head is the preallocated backtrace
2004     _head = backtrace();
2005     _backtrace = Handle(thread, _head);
2006     _index = 0;
2007   }
2008 
2009   void expand(TRAPS) {
2010     objArrayHandle old_head(THREAD, _head);
2011     PauseNoSafepointVerifier pnsv(&_nsv);
2012 
2013     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
2014     objArrayHandle new_head(THREAD, head);
2015 
2016     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
2017     typeArrayHandle new_methods(THREAD, methods);
2018 
2019     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
2020     typeArrayHandle new_bcis(THREAD, bcis);
2021 
2022     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
2023     objArrayHandle new_mirrors(THREAD, mirrors);
2024 
2025     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
2026     typeArrayHandle new_names(THREAD, names);
2027 



2028     if (!old_head.is_null()) {
2029       old_head->obj_at_put(trace_next_offset, new_head());
2030     }
2031     new_head->obj_at_put(trace_methods_offset, new_methods());
2032     new_head->obj_at_put(trace_bcis_offset, new_bcis());
2033     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
2034     new_head->obj_at_put(trace_names_offset, new_names());

2035 
2036     _head    = new_head();
2037     _methods = new_methods();
2038     _bcis = new_bcis();
2039     _mirrors = new_mirrors();
2040     _names  = new_names();

2041     _index = 0;
2042   }
2043 
2044   oop backtrace() {
2045     return _backtrace();
2046   }
2047 
2048   inline void push(Method* method, int bci, TRAPS) {
2049     // Smear the -1 bci to 0 since the array only holds unsigned
2050     // shorts.  The later line number lookup would just smear the -1
2051     // to a 0 even if it could be recorded.
2052     if (bci == SynchronizationEntryBCI) bci = 0;
2053 
2054     if (_index >= trace_chunk_size) {
2055       methodHandle mhandle(THREAD, method);
2056       expand(CHECK);
2057       method = mhandle();
2058     }
2059 
2060     _methods->ushort_at_put(_index, method->orig_method_idnum());
2061     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
2062 
2063     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
2064     // klass owning the symbols alive so their refcounts aren't decremented.
2065     Symbol* name = method->name();
2066     _names->symbol_at_put(_index, name);
2067 
2068     // We need to save the mirrors in the backtrace to keep the class
2069     // from being unloaded while we still have this stack trace.
2070     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
2071     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());



2072     _index++;
2073   }
2074 
2075 };
2076 
2077 struct BacktraceElement : public StackObj {
2078   int _method_id;
2079   int _bci;
2080   int _version;
2081   Symbol* _name;
2082   Handle _mirror;
2083   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name) :
2084                    _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror) {}

2085 };
2086 
2087 class BacktraceIterator : public StackObj {
2088   int _index;
2089   objArrayHandle  _result;
2090   objArrayHandle  _mirrors;
2091   typeArrayHandle _methods;
2092   typeArrayHandle _bcis;
2093   typeArrayHandle _names;

2094 
2095   void init(objArrayHandle result, Thread* thread) {
2096     // Get method id, bci, version and mirror from chunk
2097     _result = result;
2098     if (_result.not_null()) {
2099       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
2100       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
2101       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
2102       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));

2103       _index = 0;
2104     }
2105   }
2106  public:
2107   BacktraceIterator(objArrayHandle result, Thread* thread) {
2108     init(result, thread);
2109     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
2110   }
2111 
2112   BacktraceElement next(Thread* thread) {
2113     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
2114                         _methods->ushort_at(_index),
2115                         Backtrace::version_at(_bcis->int_at(_index)),
2116                         Backtrace::bci_at(_bcis->int_at(_index)),
2117                         _names->symbol_at(_index));

2118     _index++;
2119 
2120     if (_index >= java_lang_Throwable::trace_chunk_size) {
2121       int next_offset = java_lang_Throwable::trace_next_offset;
2122       // Get next chunk
2123       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
2124       init(result, thread);
2125     }
2126     return e;
2127   }
2128 
2129   bool repeat() {
2130     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
2131   }
2132 };
2133 
2134 
2135 // Print stack trace element to resource allocated buffer
2136 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2137                                           int version, int bci, Symbol* name) {


2260         }
2261       }
2262     }
2263   }
2264 }
2265 
2266 /**
2267  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2268  */
2269 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2270   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "Throwable instance expected");
2271   JavaValue result(T_VOID);
2272   JavaCalls::call_virtual(&result,
2273                           throwable,
2274                           SystemDictionary::Throwable_klass(),
2275                           vmSymbols::printStackTrace_name(),
2276                           vmSymbols::void_method_signature(),
2277                           THREAD);
2278 }
2279 
2280 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {


2281   if (!StackTraceInThrowable) return;
2282   ResourceMark rm(THREAD);
2283 
2284   // Start out by clearing the backtrace for this object, in case the VM
2285   // runs out of memory while allocating the stack trace
2286   set_backtrace(throwable(), NULL);
2287   // Clear lazily constructed Java level stacktrace if refilling occurs
2288   // This is unnecessary in 1.7+ but harmless
2289   clear_stacktrace(throwable());
2290 
2291   int max_depth = MaxJavaStackTraceDepth;
2292   JavaThread* thread = (JavaThread*)THREAD;
2293 
2294   BacktraceBuilder bt(CHECK);
2295 
2296   // If there is no Java frame just return the method that was being called
2297   // with bci 0
2298   if (!thread->has_last_Java_frame()) {
2299     if (max_depth >= 1 && method() != NULL) {
2300       bt.push(method(), 0, CHECK);
2301       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2302       set_depth(throwable(), 1);
2303       set_backtrace(throwable(), bt.backtrace());
2304     }
2305     return;
2306   }
2307 
2308   // Instead of using vframe directly, this version of fill_in_stack_trace
2309   // basically handles everything by hand. This significantly improved the
2310   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2311   // See bug 6333838 for  more details.
2312   // The "ASSERT" here is to verify this method generates the exactly same stack
2313   // trace as utilizing vframe.
2314 #ifdef ASSERT
2315   vframeStream st(thread);
2316   methodHandle st_method(THREAD, st.method());
2317 #endif
2318   int total_count = 0;
2319   RegisterMap map(thread, false);
2320   int decode_offset = 0;
2321   CompiledMethod* nm = NULL;
2322   bool skip_fillInStackTrace_check = false;
2323   bool skip_throwableInit_check = false;
2324   bool skip_hidden = !ShowHiddenFrames;
2325 

2326   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2327     Method* method = NULL;
2328     int bci = 0;

2329 
2330     // Compiled java method case.
2331     if (decode_offset != 0) {
2332       DebugInfoReadStream stream(nm, decode_offset);
2333       decode_offset = stream.read_int();
2334       method = (Method*)nm->metadata_at(stream.read_int());
2335       bci = stream.read_bci();
2336     } else {
2337       if (fr.is_first_frame()) break;


















2338       address pc = fr.pc();
2339       if (fr.is_interpreted_frame()) {
2340         address bcp = fr.interpreter_frame_bcp();
2341         method = fr.interpreter_frame_method();






2342         bci =  method->bci_from(bcp);
2343         fr = fr.sender(&map);
2344       } else {
2345         CodeBlob* cb = fr.cb();
2346         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2347         // but non nmethod
2348         fr = fr.sender(&map);
2349         if (cb == NULL || !cb->is_compiled()) {
2350           continue;
2351         }
2352         nm = cb->as_compiled_method();

2353         if (nm->method()->is_native()) {
2354           method = nm->method();
2355           bci = 0;
2356         } else {
2357           PcDesc* pd = nm->pc_desc_at(pc);
2358           decode_offset = pd->scope_decode_offset();
2359           // if decode_offset is not equal to 0, it will execute the
2360           // "compiled java method case" at the beginning of the loop.
2361           continue;
2362         }
2363       }
2364     }
2365 #ifdef ASSERT
2366     assert(st_method() == method && st.bci() == bci,
2367            "Wrong stack trace");
2368     st.next();
2369     // vframeStream::method isn't GC-safe so store off a copy
2370     // of the Method* in case we GC.
2371     if (!st.at_end()) {
2372       st_method = st.method();
2373     }
2374 #endif
2375 
2376     // the format of the stacktrace will be:
2377     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2378     // - 0 or more <init> methods for the exception class (skipped)
2379     // - rest of the stack
2380 
2381     if (!skip_fillInStackTrace_check) {
2382       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2383           throwable->is_a(method->method_holder())) {
2384         continue;
2385       }
2386       else {
2387         skip_fillInStackTrace_check = true; // gone past them all
2388       }
2389     }
2390     if (!skip_throwableInit_check) {
2391       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2392 
2393       // skip <init> methods of the exception class and superclasses
2394       // This is simlar to classic VM.
2395       if (method->name() == vmSymbols::object_initializer_name() &&
2396           throwable->is_a(method->method_holder())) {
2397         continue;
2398       } else {
2399         // there are none or we've seen them all - either way stop checking
2400         skip_throwableInit_check = true;
2401       }
2402     }
2403     if (method->is_hidden()) {
2404       if (skip_hidden)  continue;
2405     }
2406     bt.push(method, bci, CHECK);

2407     total_count++;

2408   }
2409 
2410   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2411 
2412   // Put completed stack trace into throwable object
2413   set_backtrace(throwable(), bt.backtrace());
2414   set_depth(throwable(), total_count);
2415 }
2416 
2417 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2418   // No-op if stack trace is disabled
2419   if (!StackTraceInThrowable) {
2420     return;
2421   }
2422 
2423   // Disable stack traces for some preallocated out of memory errors
2424   if (!Universe::should_fill_in_stack_trace(throwable)) {
2425     return;
2426   }
2427 
2428   PRESERVE_EXCEPTION_MARK;
2429 
2430   JavaThread* thread = JavaThread::active();
2431   fill_in_stack_trace(throwable, method, thread);
2432   // ignore exceptions thrown during stack trace filling
2433   CLEAR_PENDING_EXCEPTION;
2434 }
2435 
2436 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2437   // Allocate stack trace - backtrace is created but not filled in
2438 
2439   // No-op if stack trace is disabled
2440   if (!StackTraceInThrowable) return;
2441   BacktraceBuilder bt(CHECK);   // creates a backtrace
2442   set_backtrace(throwable(), bt.backtrace());
2443 }
2444 
2445 
2446 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2447   // Fill in stack trace into preallocated backtrace (no GC)
2448 
2449   // No-op if stack trace is disabled
2450   if (!StackTraceInThrowable) return;
2451 
2452   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
2453 
2454   JavaThread* THREAD = JavaThread::current();
2455 
2456   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2457   assert(backtrace.not_null(), "backtrace should have been preallocated");
2458 
2459   ResourceMark rm(THREAD);
2460   vframeStream st(THREAD);
2461 
2462   BacktraceBuilder bt(THREAD, backtrace);
2463 
2464   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2465   // methods as preallocated errors aren't created by "java" code.
2466 
2467   // fill in as much stack trace as possible
2468   int chunk_count = 0;
2469   for (;!st.at_end(); st.next()) {
2470     bt.push(st.method(), st.bci(), CHECK);
2471     chunk_count++;
2472 
2473     // Bail-out for deep stacks
2474     if (chunk_count >= trace_chunk_size) break;
2475   }
2476   set_depth(throwable(), chunk_count);
2477   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2478 
2479   // We support the Throwable immutability protocol defined for Java 7.
2480   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2481   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2482 }
2483 
2484 void java_lang_Throwable::get_stack_trace_elements(Handle throwable,
2485                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2486 
2487   if (throwable.is_null() || stack_trace_array_h.is_null()) {
2488     THROW(vmSymbols::java_lang_NullPointerException());
2489   }
2490 


2497   objArrayHandle result(THREAD, objArrayOop(backtrace(throwable())));
2498   BacktraceIterator iter(result, THREAD);
2499 
2500   int index = 0;
2501   while (iter.repeat()) {
2502     BacktraceElement bte = iter.next(THREAD);
2503 
2504     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
2505 
2506     if (stack_trace_element.is_null()) {
2507       THROW(vmSymbols::java_lang_NullPointerException());
2508     }
2509 
2510     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2511     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
2512 
2513     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
2514                                          method,
2515                                          bte._version,
2516                                          bte._bci,
2517                                          bte._name, CHECK);


2518   }
2519 }
2520 
2521 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) {
2522   // Allocate java.lang.StackTraceElement instance
2523   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
2524   assert(k != NULL, "must be loaded in 1.4+");
2525   if (k->should_be_initialized()) {
2526     k->initialize(CHECK_0);
2527   }
2528 
2529   Handle element = k->allocate_instance_handle(CHECK_0);
2530 
2531   int version = method->constants()->version();
2532   fill_in(element, method->method_holder(), method, version, bci, method->name(), CHECK_0);
2533   return element();
2534 }
2535 
2536 void java_lang_StackTraceElement::fill_in(Handle element,
2537                                           InstanceKlass* holder, const methodHandle& method,
2538                                           int version, int bci, Symbol* name, TRAPS) {
2539   assert(element->is_a(SystemDictionary::StackTraceElement_klass()), "sanity check");
2540 
2541   ResourceMark rm(THREAD);
2542   HandleMark hm(THREAD);
2543 
2544   // Fill in class name
2545   Handle java_class(THREAD, holder->java_mirror());
2546   oop classname = java_lang_Class::name(java_class, CHECK);
2547   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2548   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
2549 
2550   oop loader = holder->class_loader();
2551   if (loader != NULL) {
2552     oop loader_name = java_lang_ClassLoader::name(loader);
2553     if (loader_name != NULL)
2554       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
2555   }
2556 
2557   // Fill in method name
2558   oop methodname = StringTable::intern(name, CHECK);


2586       if (source_file == NULL) {
2587         source_file = StringTable::intern(source, CHECK);
2588         java_lang_Class::set_source_file(java_class(), source_file);
2589       }
2590     } else {
2591       // Class was redefined. Dump the cache if it was set.
2592       if (source_file != NULL) {
2593         source_file = NULL;
2594         java_lang_Class::set_source_file(java_class(), source_file);
2595       }
2596       if (ShowHiddenFrames) {
2597         source = vmSymbols::unknown_class_name();
2598         source_file = StringTable::intern(source, CHECK);
2599       }
2600     }
2601     java_lang_StackTraceElement::set_fileName(element(), source_file);
2602 
2603     int line_number = Backtrace::get_line_number(method, bci);
2604     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2605   }



2606 }
2607 
2608 #if INCLUDE_JVMCI
2609 void java_lang_StackTraceElement::decode(Handle mirror, methodHandle method, int bci, Symbol*& methodname, Symbol*& filename, int& line_number) {
2610   int method_id = method->orig_method_idnum();
2611   int cpref = method->name_index();
2612   decode(mirror, method_id, method->constants()->version(), bci, cpref, methodname, filename, line_number);
2613 }
2614 
2615 void java_lang_StackTraceElement::decode(Handle mirror, int method_id, int version, int bci, int cpref, Symbol*& methodname, Symbol*& filename, int& line_number) {
2616   // Fill in class name
2617   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2618   Method* method = holder->method_with_orig_idnum(method_id, version);
2619 
2620   // The method can be NULL if the requested class version is gone
2621   Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
2622 
2623   // Fill in method name
2624   methodname = sym;
2625 


2636     assert(holder != NULL, "sanity check");
2637     Symbol* source = holder->source_file_name();
2638     if (ShowHiddenFrames && source == NULL) {
2639       source = vmSymbols::unknown_class_name();
2640     }
2641     filename = source;
2642     line_number = Backtrace::get_line_number(method, bci);
2643   }
2644 }
2645 #endif // INCLUDE_JVMCI
2646 
2647 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2648   HandleMark hm(THREAD);
2649   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2650   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2651   // we should expand MemberName::name when Throwable uses StackTrace
2652   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2653   return method;
2654 }
2655 
2656 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) {
2657   // set Method* or mid/cpref
2658   HandleMark hm(THREAD);
2659   Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset));
2660   InstanceKlass* ik = method->method_holder();
2661   CallInfo info(method(), ik, CHECK);
2662   MethodHandles::init_method_MemberName(mname, info);
2663   // set bci
2664   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2665   // method may be redefined; store the version
2666   int version = method->constants()->version();
2667   assert((jushort)version == version, "version should be short");
2668   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);



2669 }
2670 
2671 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
2672   ResourceMark rm(THREAD);
2673   HandleMark hm(THREAD);
2674   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
2675   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
2676   InstanceKlass* holder = InstanceKlass::cast(clazz);
2677   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);


2678 
2679   short version = stackFrame->short_field(_version_offset);
2680   short bci = stackFrame->short_field(_bci_offset);
2681   Symbol* name = method->name();
2682   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, method, version, bci, name, CHECK);
2683 }
2684 
2685 #define STACKFRAMEINFO_FIELDS_DO(macro) \
2686   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
2687   macro(_bci_offset,            k, "bci",         short_signature,  false)

2688 
2689 void java_lang_StackFrameInfo::compute_offsets() {
2690   InstanceKlass* k = SystemDictionary::StackFrameInfo_klass();
2691   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2692   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2693 }
2694 
2695 #if INCLUDE_CDS
2696 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
2697   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2698   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2699 }
2700 #endif
2701 
2702 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
2703   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
2704   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
2705   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
2706   macro(_mode_offset,       k, "mode",        int_signature,          false)
2707 


4109 int java_lang_reflect_Constructor::parameter_annotations_offset;
4110 int java_lang_reflect_Field::clazz_offset;
4111 int java_lang_reflect_Field::name_offset;
4112 int java_lang_reflect_Field::type_offset;
4113 int java_lang_reflect_Field::slot_offset;
4114 int java_lang_reflect_Field::modifiers_offset;
4115 int java_lang_reflect_Field::signature_offset;
4116 int java_lang_reflect_Field::annotations_offset;
4117 int java_lang_reflect_Parameter::name_offset;
4118 int java_lang_reflect_Parameter::modifiers_offset;
4119 int java_lang_reflect_Parameter::index_offset;
4120 int java_lang_reflect_Parameter::executable_offset;
4121 int java_lang_boxing_object::value_offset;
4122 int java_lang_boxing_object::long_value_offset;
4123 int java_lang_ref_Reference::referent_offset;
4124 int java_lang_ref_Reference::queue_offset;
4125 int java_lang_ref_Reference::next_offset;
4126 int java_lang_ref_Reference::discovered_offset;
4127 int java_lang_ref_SoftReference::timestamp_offset;
4128 int java_lang_ref_SoftReference::static_clock_offset;





















4129 int java_lang_ClassLoader::parent_offset;
4130 int java_lang_System::static_in_offset;
4131 int java_lang_System::static_out_offset;
4132 int java_lang_System::static_err_offset;
4133 int java_lang_System::static_security_offset;
4134 int java_lang_StackTraceElement::methodName_offset;
4135 int java_lang_StackTraceElement::fileName_offset;
4136 int java_lang_StackTraceElement::lineNumber_offset;
4137 int java_lang_StackTraceElement::moduleName_offset;
4138 int java_lang_StackTraceElement::moduleVersion_offset;
4139 int java_lang_StackTraceElement::classLoaderName_offset;
4140 int java_lang_StackTraceElement::declaringClass_offset;
4141 int java_lang_StackTraceElement::declaringClassObject_offset;

4142 int java_lang_StackFrameInfo::_memberName_offset;
4143 int java_lang_StackFrameInfo::_bci_offset;
4144 int java_lang_StackFrameInfo::_version_offset;

4145 int java_lang_LiveStackFrameInfo::_monitors_offset;
4146 int java_lang_LiveStackFrameInfo::_locals_offset;
4147 int java_lang_LiveStackFrameInfo::_operands_offset;
4148 int java_lang_LiveStackFrameInfo::_mode_offset;
4149 int java_lang_AssertionStatusDirectives::classes_offset;
4150 int java_lang_AssertionStatusDirectives::classEnabled_offset;
4151 int java_lang_AssertionStatusDirectives::packages_offset;
4152 int java_lang_AssertionStatusDirectives::packageEnabled_offset;
4153 int java_lang_AssertionStatusDirectives::deflt_offset;
4154 int java_nio_Buffer::_limit_offset;
4155 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset;
4156 int reflect_ConstantPool::_oop_offset;
4157 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
4158 int java_lang_Integer_IntegerCache::_static_cache_offset;
4159 int java_lang_Long_LongCache::_static_cache_offset;
4160 int java_lang_Character_CharacterCache::_static_cache_offset;
4161 int java_lang_Short_ShortCache::_static_cache_offset;
4162 int java_lang_Byte_ByteCache::_static_cache_offset;
4163 int java_lang_Boolean::_static_TRUE_offset;
4164 int java_lang_Boolean::_static_FALSE_offset;
4165 
4166 
4167 
4168 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4169   macro(declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4170   macro(classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4171   macro(moduleName_offset,      k, "moduleName",      string_signature, false); \
4172   macro(moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4173   macro(declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4174   macro(methodName_offset,      k, "methodName",      string_signature, false); \
4175   macro(fileName_offset,        k, "fileName",        string_signature, false); \
4176   macro(lineNumber_offset,      k, "lineNumber",      int_signature,    false)

4177 
4178 // Support for java_lang_StackTraceElement
4179 void java_lang_StackTraceElement::compute_offsets() {
4180   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
4181   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4182 }
4183 
4184 #if INCLUDE_CDS
4185 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) {
4186   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4187 }
4188 #endif
4189 
4190 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4191   element->obj_field_put(fileName_offset, value);
4192 }
4193 
4194 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4195   element->obj_field_put(declaringClass_offset, value);
4196 }


4202 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
4203   element->int_field_put(lineNumber_offset, value);
4204 }
4205 
4206 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
4207   element->obj_field_put(moduleName_offset, value);
4208 }
4209 
4210 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
4211   element->obj_field_put(moduleVersion_offset, value);
4212 }
4213 
4214 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) {
4215   element->obj_field_put(classLoaderName_offset, value);
4216 }
4217 
4218 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) {
4219   element->obj_field_put(declaringClassObject_offset, value);
4220 }
4221 




4222 void java_lang_StackFrameInfo::set_version(oop element, short value) {
4223   element->short_field_put(_version_offset, value);
4224 }
4225 
4226 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
4227   element->int_field_put(_bci_offset, value);
4228 }
4229 




4230 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
4231   element->obj_field_put(_monitors_offset, value);
4232 }
4233 
4234 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
4235   element->obj_field_put(_locals_offset, value);
4236 }
4237 
4238 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
4239   element->obj_field_put(_operands_offset, value);
4240 }
4241 
4242 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) {
4243   element->int_field_put(_mode_offset, value);
4244 }
4245 
4246 // Support for java Assertions - java_lang_AssertionStatusDirectives.
4247 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \
4248   macro(classes_offset,        k, "classes",        string_array_signature, false); \
4249   macro(classEnabled_offset,   k, "classEnabled",   bool_array_signature, false); \


4265 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
4266   o->obj_field_put(classes_offset, val);
4267 }
4268 
4269 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
4270   o->obj_field_put(classEnabled_offset, val);
4271 }
4272 
4273 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
4274   o->obj_field_put(packages_offset, val);
4275 }
4276 
4277 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
4278   o->obj_field_put(packageEnabled_offset, val);
4279 }
4280 
4281 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
4282   o->bool_field_put(deflt_offset, val);
4283 }
4284 





























































4285 
4286 // Support for intrinsification of java.nio.Buffer.checkIndex
4287 int java_nio_Buffer::limit_offset() {
4288   return _limit_offset;
4289 }
4290 
4291 #define BUFFER_FIELDS_DO(macro) \
4292   macro(_limit_offset, k, "limit", int_signature, false)
4293 
4294 void java_nio_Buffer::compute_offsets() {
4295   InstanceKlass* k = SystemDictionary::nio_Buffer_klass();
4296   assert(k != NULL, "must be loaded in 1.4+");
4297   BUFFER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4298 }
4299 
4300 #if INCLUDE_CDS
4301 void java_nio_Buffer::serialize_offsets(SerializeClosure* f) {
4302   BUFFER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4303 }
4304 #endif
4305 
4306 #define AOS_FIELDS_DO(macro) \
4307   macro(_owner_offset, k, "exclusiveOwnerThread", thread_signature, false)
4308 
4309 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() {
4310   InstanceKlass* k = SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass();
4311   AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4312 }
4313 
4314 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
4315   assert(_owner_offset != 0, "Must be initialized");
4316   return obj->obj_field(_owner_offset);
4317 }
4318 
4319 #if INCLUDE_CDS
4320 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) {
4321   AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4322 }
4323 #endif
4324 
4325 #define INTEGER_CACHE_FIELDS_DO(macro) \
4326   macro(_static_cache_offset, k, "cache", java_lang_Integer_array_signature, true)
4327 


4509 }
4510 
4511 static int member_offset(int hardcoded_offset) {
4512   return (hardcoded_offset * heapOopSize) + instanceOopDesc::base_offset_in_bytes();
4513 }
4514 
4515 // Compute hard-coded offsets
4516 // Invoked before SystemDictionary::initialize, so pre-loaded classes
4517 // are not available to determine the offset_of_static_fields.
4518 void JavaClasses::compute_hard_coded_offsets() {
4519 
4520   // java_lang_boxing_object
4521   java_lang_boxing_object::value_offset      = member_offset(java_lang_boxing_object::hc_value_offset);
4522   java_lang_boxing_object::long_value_offset = align_up(member_offset(java_lang_boxing_object::hc_value_offset), BytesPerLong);
4523 
4524   // java_lang_ref_Reference
4525   java_lang_ref_Reference::referent_offset    = member_offset(java_lang_ref_Reference::hc_referent_offset);
4526   java_lang_ref_Reference::queue_offset       = member_offset(java_lang_ref_Reference::hc_queue_offset);
4527   java_lang_ref_Reference::next_offset        = member_offset(java_lang_ref_Reference::hc_next_offset);
4528   java_lang_ref_Reference::discovered_offset  = member_offset(java_lang_ref_Reference::hc_discovered_offset);










4529 }
4530 
4531 #define DO_COMPUTE_OFFSETS(k) k::compute_offsets();
4532 
4533 // Compute non-hard-coded field offsets of all the classes in this file
4534 void JavaClasses::compute_offsets() {
4535   if (UseSharedSpaces) {
4536     assert(JvmtiExport::is_early_phase() && !(JvmtiExport::should_post_class_file_load_hook() &&
4537                                               JvmtiExport::has_early_class_hook_env()),
4538            "JavaClasses::compute_offsets() must be called in early JVMTI phase.");
4539     // None of the classes used by the rest of this function can be replaced by
4540     // JMVTI ClassFileLoadHook.
4541     // We are safe to use the archived offsets, which have already been restored
4542     // by JavaClasses::serialize_offsets, without computing the offsets again.
4543     return;
4544   }
4545 
4546   // We have already called the compute_offsets() of the
4547   // BASIC_JAVA_CLASSES_DO_PART1 classes (java_lang_String and java_lang_Class)
4548   // earlier inside SystemDictionary::resolve_well_known_classes()


4601 
4602   // Boxed primitive objects (java_lang_boxing_object)
4603 
4604   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
4605   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
4606   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
4607   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
4608   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
4609   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
4610   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
4611   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
4612 
4613   // java.lang.ref.Reference
4614 
4615   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
4616   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
4617   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
4618   // Fake field
4619   //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
4620 











4621   if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
4622 }
4623 
4624 #endif // PRODUCT
4625 
4626 int InjectedField::compute_offset() {
4627   InstanceKlass* ik = InstanceKlass::cast(klass());
4628   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4629     if (!may_be_java && !fs.access_flags().is_internal()) {
4630       // Only look at injected fields
4631       continue;
4632     }
4633     if (fs.name() == name() && fs.signature() == signature()) {
4634       return fs.offset();
4635     }
4636   }
4637   ResourceMark rm;
4638   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)" : "");
4639 #ifndef PRODUCT
4640   ik->print();
4641   tty->print_cr("all fields:");
4642   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4643     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
4644   }
4645 #endif //PRODUCT
4646   vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
4647   return -1;
4648 }
4649 
4650 void javaClasses_init() {
4651   JavaClasses::compute_offsets();
4652   JavaClasses::check_offsets();

4653   FilteredFieldsMap::initialize();  // must be done after computing offsets.
4654 }


1595 
1596 
1597 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1598 //       platform thread structure, and a eetop offset which was used for thread
1599 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1600 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1601 //       instead of the privateInfo_offset.
1602 //
1603 // Note: The stackSize field is only present starting in 1.4.
1604 
1605 int java_lang_Thread::_name_offset = 0;
1606 int java_lang_Thread::_group_offset = 0;
1607 int java_lang_Thread::_contextClassLoader_offset = 0;
1608 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1609 int java_lang_Thread::_priority_offset = 0;
1610 int java_lang_Thread::_eetop_offset = 0;
1611 int java_lang_Thread::_daemon_offset = 0;
1612 int java_lang_Thread::_stillborn_offset = 0;
1613 int java_lang_Thread::_stackSize_offset = 0;
1614 int java_lang_Thread::_tid_offset = 0;
1615 int java_lang_Thread::_continuation_offset = 0;
1616 int java_lang_Thread::_fiber_offset = 0 ;
1617 int java_lang_Thread::_thread_status_offset = 0;
1618 int java_lang_Thread::_park_blocker_offset = 0;
1619 
1620 #define THREAD_FIELDS_DO(macro) \
1621   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1622   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1623   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1624   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1625   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1626   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1627   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1628   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1629   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1630   macro(_tid_offset,           k, "tid", long_signature, false); \
1631   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1632   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
1633   macro(_continuation_offset,  k, "cont", continuation_signature, false); \
1634   macro(_fiber_offset,         k, "fiber", fiber_signature, false)
1635 
1636 void java_lang_Thread::compute_offsets() {
1637   assert(_group_offset == 0, "offsets should be initialized only once");
1638 
1639   InstanceKlass* k = SystemDictionary::Thread_klass();
1640   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1641 }
1642 
1643 #if INCLUDE_CDS
1644 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
1645   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1646 }
1647 #endif
1648 
1649 JavaThread* java_lang_Thread::thread(oop java_thread) {
1650   return (JavaThread*)java_thread->address_field(_eetop_offset);
1651 }
1652 
1653 
1654 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {


1724 void java_lang_Thread::set_thread_status(oop java_thread,
1725                                          java_lang_Thread::ThreadStatus status) {
1726   java_thread->int_field_put(_thread_status_offset, status);
1727 }
1728 
1729 // Read thread status value from threadStatus field in java.lang.Thread java class.
1730 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1731   // Make sure the caller is operating on behalf of the VM or is
1732   // running VM code (state == _thread_in_vm).
1733   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1734          JavaThread::current()->thread_state() == _thread_in_vm,
1735          "Java Thread is not running in vm");
1736   return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1737 }
1738 
1739 
1740 jlong java_lang_Thread::thread_id(oop java_thread) {
1741   return java_thread->long_field(_tid_offset);
1742 }
1743 
1744 oop java_lang_Thread::continuation(oop java_thread) {
1745   return java_thread->obj_field(_continuation_offset);
1746 }
1747 
1748 void java_lang_Thread::set_continuation(oop java_thread, oop continuation) {
1749   tty->print_cr(">>> java_lang_Thread::set_continuation %p", (oopDesc*)continuation);
1750   return java_thread->obj_field_put(_continuation_offset, continuation);
1751 }
1752 
1753 oop java_lang_Thread::fiber(oop java_thread) {
1754   return java_thread->obj_field(_fiber_offset);
1755 }
1756 
1757 oop java_lang_Thread::park_blocker(oop java_thread) {
1758   return java_thread->obj_field(_park_blocker_offset);
1759 }
1760 
1761 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1762   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1763   switch (status) {
1764     case NEW                      : return "NEW";
1765     case RUNNABLE                 : return "RUNNABLE";
1766     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1767     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1768     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1769     case PARKED                   : return "WAITING (parking)";
1770     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1771     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1772     case TERMINATED               : return "TERMINATED";
1773     default                       : return "UNKNOWN";
1774   };
1775 }
1776 int java_lang_ThreadGroup::_parent_offset = 0;


1844   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
1845   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
1846   macro(_destroyed_offset,   k, vmSymbols::destroyed_name(),   bool_signature,              false); \
1847   macro(_daemon_offset,      k, vmSymbols::daemon_name(),      bool_signature,              false); \
1848   macro(_nthreads_offset,    k, vmSymbols::nthreads_name(),    int_signature,               false); \
1849   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false)
1850 
1851 void java_lang_ThreadGroup::compute_offsets() {
1852   assert(_parent_offset == 0, "offsets should be initialized only once");
1853 
1854   InstanceKlass* k = SystemDictionary::ThreadGroup_klass();
1855   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1856 }
1857 
1858 #if INCLUDE_CDS
1859 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
1860   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1861 }
1862 #endif
1863 
1864 
1865 // java_lang_Fiber
1866 
1867 int java_lang_Fiber::static_notify_jvmti_events_offset = 0;
1868 int java_lang_Fiber::_carrierThread_offset = 0;
1869 int java_lang_Fiber::_continuation_offset = 0;
1870 
1871 #define FIBER_FIELDS_DO(macro) \
1872   macro(static_notify_jvmti_events_offset,  k, "notifyJvmtiEvents",  bool_signature, true); \
1873   macro(_carrierThread_offset,  k, "carrierThread",  thread_signature, false); \
1874   macro(_continuation_offset,  k, "cont",  continuation_signature, false)
1875 
1876 static jboolean fiber_notify_jvmti_events = JNI_FALSE;
1877 
1878 void java_lang_Fiber::compute_offsets() {
1879   InstanceKlass* k = SystemDictionary::Fiber_klass();
1880   FIBER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1881 }
1882 
1883 void java_lang_Fiber::init_static_notify_jvmti_events() {
1884   if (fiber_notify_jvmti_events) {
1885     InstanceKlass* ik = SystemDictionary::Fiber_klass();
1886     oop base = ik->static_field_base_raw();
1887     base->release_bool_field_put(static_notify_jvmti_events_offset, fiber_notify_jvmti_events);
1888   }
1889 }
1890 
1891 bool java_lang_Fiber::is_instance(oop obj) {
1892   return obj != NULL && is_subclass(obj->klass());
1893 }
1894 
1895 oop java_lang_Fiber::carrier_thread(oop fiber) {
1896   oop thread = fiber->obj_field(_carrierThread_offset);
1897   return thread;
1898 }
1899  
1900 oop java_lang_Fiber::continuation(oop fiber) {
1901   oop cont = fiber->obj_field(_continuation_offset);
1902   return cont;
1903 }
1904 
1905 #if INCLUDE_CDS
1906 void java_lang_Fiber::serialize_offsets(SerializeClosure* f) {
1907    FIBER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1908 }
1909 #endif
1910 
1911 void java_lang_Fiber::set_notify_jvmti_events(jboolean enable) {
1912   fiber_notify_jvmti_events = enable;
1913 }
1914 
1915 
1916 #define THROWABLE_FIELDS_DO(macro) \
1917   macro(backtrace_offset,     k, "backtrace",     object_signature,                  false); \
1918   macro(detailMessage_offset, k, "detailMessage", string_signature,                  false); \
1919   macro(stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
1920   macro(depth_offset,         k, "depth",         int_signature,                     false); \
1921   macro(static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
1922 
1923 void java_lang_Throwable::compute_offsets() {
1924   InstanceKlass* k = SystemDictionary::Throwable_klass();
1925   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1926 }
1927 
1928 #if INCLUDE_CDS
1929 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) {
1930   THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1931 }
1932 #endif
1933 
1934 oop java_lang_Throwable::unassigned_stacktrace() {
1935   InstanceKlass* ik = SystemDictionary::Throwable_klass();


1998 const int MAX_VERSION = USHRT_MAX;
1999 
2000 static inline bool version_matches(Method* method, int version) {
2001   assert(version < MAX_VERSION, "version is too big");
2002   return method != NULL && (method->constants()->version() == version);
2003 }
2004 
2005 
2006 // This class provides a simple wrapper over the internal structure of
2007 // exception backtrace to insulate users of the backtrace from needing
2008 // to know what it looks like.
2009 class BacktraceBuilder: public StackObj {
2010  friend class BacktraceIterator;
2011  private:
2012   Handle          _backtrace;
2013   objArrayOop     _head;
2014   typeArrayOop    _methods;
2015   typeArrayOop    _bcis;
2016   objArrayOop     _mirrors;
2017   typeArrayOop    _names; // needed to insulate method name against redefinition
2018   objArrayOop     _conts;
2019   int             _index;
2020   NoSafepointVerifier _nsv;
2021 
2022   enum {
2023     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
2024     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
2025     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
2026     trace_names_offset   = java_lang_Throwable::trace_names_offset,
2027     trace_conts_offset   = java_lang_Throwable::trace_conts_offset,
2028     trace_next_offset    = java_lang_Throwable::trace_next_offset,
2029     trace_size           = java_lang_Throwable::trace_size,
2030     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
2031   };
2032 
2033   // get info out of chunks
2034   static typeArrayOop get_methods(objArrayHandle chunk) {
2035     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
2036     assert(methods != NULL, "method array should be initialized in backtrace");
2037     return methods;
2038   }
2039   static typeArrayOop get_bcis(objArrayHandle chunk) {
2040     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
2041     assert(bcis != NULL, "bci array should be initialized in backtrace");
2042     return bcis;
2043   }
2044   static objArrayOop get_mirrors(objArrayHandle chunk) {
2045     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
2046     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
2047     return mirrors;
2048   }
2049   static typeArrayOop get_names(objArrayHandle chunk) {
2050     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
2051     assert(names != NULL, "names array should be initialized in backtrace");
2052     return names;
2053   }
2054   static objArrayOop get_conts(objArrayHandle chunk) {
2055     objArrayOop conts = objArrayOop(chunk->obj_at(trace_conts_offset));
2056     assert(conts != NULL, "conts array should be initialized in backtrace");
2057     return conts;
2058   }
2059 
2060  public:
2061 
2062   // constructor for new backtrace
2063   BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL), _conts(NULL) {
2064     expand(CHECK);
2065     _backtrace = Handle(THREAD, _head);
2066     _index = 0;
2067   }
2068 
2069   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
2070     _methods = get_methods(backtrace);
2071     _bcis = get_bcis(backtrace);
2072     _mirrors = get_mirrors(backtrace);
2073     _names = get_names(backtrace);
2074     _conts = get_conts(backtrace);
2075     assert(_methods->length() == _bcis->length() &&
2076            _methods->length() == _mirrors->length() &&
2077            _mirrors->length() == _names->length() && 
2078            _names->length() == _conts->length(),
2079            "method and source information arrays should match");
2080 
2081     // head is the preallocated backtrace
2082     _head = backtrace();
2083     _backtrace = Handle(thread, _head);
2084     _index = 0;
2085   }
2086 
2087   void expand(TRAPS) {
2088     objArrayHandle old_head(THREAD, _head);
2089     PauseNoSafepointVerifier pnsv(&_nsv);
2090 
2091     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
2092     objArrayHandle new_head(THREAD, head);
2093 
2094     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
2095     typeArrayHandle new_methods(THREAD, methods);
2096 
2097     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
2098     typeArrayHandle new_bcis(THREAD, bcis);
2099 
2100     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
2101     objArrayHandle new_mirrors(THREAD, mirrors);
2102 
2103     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
2104     typeArrayHandle new_names(THREAD, names);
2105 
2106     objArrayOop conts = oopFactory::new_objectArray(trace_chunk_size, CHECK);
2107     objArrayHandle new_conts(THREAD, conts);
2108 
2109     if (!old_head.is_null()) {
2110       old_head->obj_at_put(trace_next_offset, new_head());
2111     }
2112     new_head->obj_at_put(trace_methods_offset, new_methods());
2113     new_head->obj_at_put(trace_bcis_offset, new_bcis());
2114     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
2115     new_head->obj_at_put(trace_names_offset, new_names());
2116     new_head->obj_at_put(trace_conts_offset, new_conts());
2117 
2118     _head    = new_head();
2119     _methods = new_methods();
2120     _bcis = new_bcis();
2121     _mirrors = new_mirrors();
2122     _names  = new_names();
2123     _conts  = new_conts();
2124     _index = 0;
2125   }
2126 
2127   oop backtrace() {
2128     return _backtrace();
2129   }
2130 
2131   inline void push(Method* method, int bci, oop contScopeName, TRAPS) {
2132     // Smear the -1 bci to 0 since the array only holds unsigned
2133     // shorts.  The later line number lookup would just smear the -1
2134     // to a 0 even if it could be recorded.
2135     if (bci == SynchronizationEntryBCI) bci = 0;
2136 
2137     if (_index >= trace_chunk_size) {
2138       methodHandle mhandle(THREAD, method);
2139       expand(CHECK);
2140       method = mhandle();
2141     }
2142 
2143     _methods->ushort_at_put(_index, method->orig_method_idnum());
2144     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
2145 
2146     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
2147     // klass owning the symbols alive so their refcounts aren't decremented.
2148     Symbol* name = method->name();
2149     _names->symbol_at_put(_index, name);
2150 
2151     // We need to save the mirrors in the backtrace to keep the class
2152     // from being unloaded while we still have this stack trace.
2153     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
2154     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
2155 
2156     _conts->obj_at_put(_index, contScopeName);
2157 
2158     _index++;
2159   }
2160 
2161 };
2162 
2163 struct BacktraceElement : public StackObj {
2164   int _method_id;
2165   int _bci;
2166   int _version;
2167   Symbol* _name;
2168   Handle _mirror;
2169   Handle _cont; // the continuation scope name (String)
2170   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name, Handle cont) :
2171                    _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror), _cont(cont) {}
2172 };
2173 
2174 class BacktraceIterator : public StackObj {
2175   int _index;
2176   objArrayHandle  _result;
2177   objArrayHandle  _mirrors;
2178   typeArrayHandle _methods;
2179   typeArrayHandle _bcis;
2180   typeArrayHandle _names;
2181   objArrayHandle  _conts;
2182 
2183   void init(objArrayHandle result, Thread* thread) {
2184     // Get method id, bci, version and mirror from chunk
2185     _result = result;
2186     if (_result.not_null()) {
2187       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
2188       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
2189       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
2190       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));
2191       _conts = objArrayHandle(thread, BacktraceBuilder::get_conts(_result));
2192       _index = 0;
2193     }
2194   }
2195  public:
2196   BacktraceIterator(objArrayHandle result, Thread* thread) {
2197     init(result, thread);
2198     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
2199   }
2200 
2201   BacktraceElement next(Thread* thread) {
2202     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
2203                         _methods->ushort_at(_index),
2204                         Backtrace::version_at(_bcis->int_at(_index)),
2205                         Backtrace::bci_at(_bcis->int_at(_index)),
2206                         _names->symbol_at(_index),
2207                         Handle(thread, _conts->obj_at(_index)));
2208     _index++;
2209 
2210     if (_index >= java_lang_Throwable::trace_chunk_size) {
2211       int next_offset = java_lang_Throwable::trace_next_offset;
2212       // Get next chunk
2213       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
2214       init(result, thread);
2215     }
2216     return e;
2217   }
2218 
2219   bool repeat() {
2220     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
2221   }
2222 };
2223 
2224 
2225 // Print stack trace element to resource allocated buffer
2226 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2227                                           int version, int bci, Symbol* name) {


2350         }
2351       }
2352     }
2353   }
2354 }
2355 
2356 /**
2357  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2358  */
2359 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2360   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "Throwable instance expected");
2361   JavaValue result(T_VOID);
2362   JavaCalls::call_virtual(&result,
2363                           throwable,
2364                           SystemDictionary::Throwable_klass(),
2365                           vmSymbols::printStackTrace_name(),
2366                           vmSymbols::void_method_signature(),
2367                           THREAD);
2368 }
2369 
2370 extern "C" void pfl();
2371 
2372 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, Handle contScope, const methodHandle& method, TRAPS) {
2373   if (!StackTraceInThrowable) return;
2374   ResourceMark rm(THREAD);
2375 
2376   // Start out by clearing the backtrace for this object, in case the VM
2377   // runs out of memory while allocating the stack trace
2378   set_backtrace(throwable(), NULL);
2379   // Clear lazily constructed Java level stacktrace if refilling occurs
2380   // This is unnecessary in 1.7+ but harmless
2381   clear_stacktrace(throwable());
2382 
2383   int max_depth = MaxJavaStackTraceDepth;
2384   JavaThread* thread = (JavaThread*)THREAD;
2385 
2386   BacktraceBuilder bt(CHECK);
2387 
2388   // If there is no Java frame just return the method that was being called
2389   // with bci 0
2390   if (!thread->has_last_Java_frame()) {
2391     if (max_depth >= 1 && method() != NULL) {
2392       bt.push(method(), 0, NULL, CHECK);
2393       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2394       set_depth(throwable(), 1);
2395       set_backtrace(throwable(), bt.backtrace());
2396     }
2397     return;
2398   }
2399 
2400   // Instead of using vframe directly, this version of fill_in_stack_trace
2401   // basically handles everything by hand. This significantly improved the
2402   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2403   // See bug 6333838 for  more details.
2404   // The "ASSERT" here is to verify this method generates the exactly same stack
2405   // trace as utilizing vframe.
2406 #ifdef ASSERT
2407   vframeStream st(thread, contScope);
2408   methodHandle st_method(THREAD, st.method());
2409 #endif
2410   int total_count = 0;
2411   RegisterMap map(thread, false, true);
2412   int decode_offset = 0;
2413   CompiledMethod* nm = NULL;
2414   bool skip_fillInStackTrace_check = false;
2415   bool skip_throwableInit_check = false;
2416   bool skip_hidden = !ShowHiddenFrames;
2417   bool is_last = false;
2418   oop cont = thread->last_continuation();
2419   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2420     Method* method = NULL;
2421     int bci = 0;
2422     oop contScopeName = (cont != NULL) ? java_lang_ContinuationScope::name(java_lang_Continuation::scope(cont)) : (oop)NULL;
2423 
2424     // Compiled java method case.
2425     if (decode_offset != 0) {
2426       DebugInfoReadStream stream(nm, decode_offset);
2427       decode_offset = stream.read_int();
2428       method = (Method*)nm->metadata_at(stream.read_int());
2429       bci = stream.read_bci();
2430     } else {
2431       if (fr.is_first_frame()) break;
2432 
2433       assert (contScope.is_null() || cont != NULL, "must be");
2434       if (cont != NULL && Continuation::is_continuation_entry_frame(fr, &map)) {
2435         oop scope = java_lang_Continuation::scope(cont);
2436         if (contScope.not_null() && oopDesc::equals(scope, contScope())) {
2437           is_last = true;
2438         } else {
2439           // if (!Continuation::is_frame_in_continuation(fr, cont)) {
2440           //   tty->print_cr(">>>>>");
2441           //   fr.print_on(tty);
2442           //   tty->print_cr("<<<<<");
2443           //   pfl();
2444           // }
2445           assert (Continuation::is_frame_in_continuation(fr, cont), "must be");
2446           cont = java_lang_Continuation::parent(cont);
2447         }
2448       }
2449 
2450       address pc = fr.pc();
2451       if (fr.is_interpreted_frame()) {
2452         address bcp;
2453         if (!map.in_cont()) {
2454           bcp = fr.interpreter_frame_bcp();
2455           method = fr.interpreter_frame_method();
2456         } else {
2457           bcp = Continuation::interpreter_frame_bcp(fr, &map);
2458           method = Continuation::interpreter_frame_method(fr, &map);
2459         }
2460         bci =  method->bci_from(bcp);
2461         fr = fr.sender(&map);
2462       } else {
2463         CodeBlob* cb = fr.cb();
2464         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2465         // but non nmethod
2466         fr = fr.sender(&map);
2467         if (cb == NULL || !cb->is_compiled()) {
2468           continue;
2469         }
2470         nm = cb->as_compiled_method();
2471         assert (nm->method() != NULL, "must be");
2472         if (nm->method()->is_native()) {
2473           method = nm->method();
2474           bci = 0;
2475         } else {
2476           PcDesc* pd = nm->pc_desc_at(pc);
2477           decode_offset = pd->scope_decode_offset();
2478           // if decode_offset is not equal to 0, it will execute the
2479           // "compiled java method case" at the beginning of the loop.
2480           continue;
2481         }
2482       }
2483     }
2484 #ifdef ASSERT
2485     assert(st_method() == method && st.bci() == bci, "Wrong stack trace");

2486     st.next();
2487     if (!st.at_end()) st_method = st.method();// vframeStream::method isn't GC-safe so store off a copy of the Method* in case we GC.




2488 #endif
2489 
2490     // the format of the stacktrace will be:
2491     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2492     // - 0 or more <init> methods for the exception class (skipped)
2493     // - rest of the stack
2494 
2495     if (!skip_fillInStackTrace_check) {
2496       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2497           throwable->is_a(method->method_holder())) {
2498         continue;
2499       }
2500       else {
2501         skip_fillInStackTrace_check = true; // gone past them all
2502       }
2503     }
2504     if (!skip_throwableInit_check) {
2505       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2506 
2507       // skip <init> methods of the exception class and superclasses
2508       // This is simlar to classic VM.
2509       if (method->name() == vmSymbols::object_initializer_name() &&
2510           throwable->is_a(method->method_holder())) {
2511         continue;
2512       } else {
2513         // there are none or we've seen them all - either way stop checking
2514         skip_throwableInit_check = true;
2515       }
2516     }
2517     if (method->is_hidden()) {
2518       if (skip_hidden)  continue;
2519     }
2520 
2521     bt.push(method, bci, contScopeName, CHECK);
2522     total_count++;
2523     if (is_last) break;
2524   }
2525 
2526   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2527 
2528   // Put completed stack trace into throwable object
2529   set_backtrace(throwable(), bt.backtrace());
2530   set_depth(throwable(), total_count);
2531 }
2532 
2533 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, Handle contScope, const methodHandle& method) {
2534   // No-op if stack trace is disabled
2535   if (!StackTraceInThrowable) {
2536     return;
2537   }
2538 
2539   // Disable stack traces for some preallocated out of memory errors
2540   if (!Universe::should_fill_in_stack_trace(throwable)) {
2541     return;
2542   }
2543 
2544   PRESERVE_EXCEPTION_MARK;
2545 
2546   JavaThread* thread = JavaThread::active();
2547   fill_in_stack_trace(throwable, contScope, method, thread);
2548   // ignore exceptions thrown during stack trace filling
2549   CLEAR_PENDING_EXCEPTION;
2550 }
2551 
2552 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2553   // Allocate stack trace - backtrace is created but not filled in
2554 
2555   // No-op if stack trace is disabled
2556   if (!StackTraceInThrowable) return;
2557   BacktraceBuilder bt(CHECK);   // creates a backtrace
2558   set_backtrace(throwable(), bt.backtrace());
2559 }
2560 
2561 
2562 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2563   // Fill in stack trace into preallocated backtrace (no GC)
2564 
2565   // No-op if stack trace is disabled
2566   if (!StackTraceInThrowable) return;
2567 
2568   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
2569 
2570   JavaThread* THREAD = JavaThread::current();
2571 
2572   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2573   assert(backtrace.not_null(), "backtrace should have been preallocated");
2574 
2575   ResourceMark rm(THREAD);
2576   vframeStream st(THREAD);
2577 
2578   BacktraceBuilder bt(THREAD, backtrace);
2579 
2580   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2581   // methods as preallocated errors aren't created by "java" code.
2582 
2583   // fill in as much stack trace as possible
2584   int chunk_count = 0;
2585   for (;!st.at_end(); st.next()) {
2586     bt.push(st.method(), st.bci(), NULL, CHECK);
2587     chunk_count++;
2588 
2589     // Bail-out for deep stacks
2590     if (chunk_count >= trace_chunk_size) break;
2591   }
2592   set_depth(throwable(), chunk_count);
2593   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2594 
2595   // We support the Throwable immutability protocol defined for Java 7.
2596   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2597   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2598 }
2599 
2600 void java_lang_Throwable::get_stack_trace_elements(Handle throwable,
2601                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2602 
2603   if (throwable.is_null() || stack_trace_array_h.is_null()) {
2604     THROW(vmSymbols::java_lang_NullPointerException());
2605   }
2606 


2613   objArrayHandle result(THREAD, objArrayOop(backtrace(throwable())));
2614   BacktraceIterator iter(result, THREAD);
2615 
2616   int index = 0;
2617   while (iter.repeat()) {
2618     BacktraceElement bte = iter.next(THREAD);
2619 
2620     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
2621 
2622     if (stack_trace_element.is_null()) {
2623       THROW(vmSymbols::java_lang_NullPointerException());
2624     }
2625 
2626     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2627     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
2628 
2629     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
2630                                          method,
2631                                          bte._version,
2632                                          bte._bci,
2633                                          bte._name, 
2634                                          bte._cont,
2635                                          CHECK);
2636   }
2637 }
2638 
2639 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, Handle contScope, TRAPS) {
2640   // Allocate java.lang.StackTraceElement instance
2641   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
2642   assert(k != NULL, "must be loaded in 1.4+");
2643   if (k->should_be_initialized()) {
2644     k->initialize(CHECK_0);
2645   }
2646 
2647   Handle element = k->allocate_instance_handle(CHECK_0);
2648 
2649   int version = method->constants()->version();
2650   fill_in(element, method->method_holder(), method, version, bci, method->name(), contScope, CHECK_0);
2651   return element();
2652 }
2653 
2654 void java_lang_StackTraceElement::fill_in(Handle element,
2655                                           InstanceKlass* holder, const methodHandle& method,
2656                                           int version, int bci, Symbol* name, Handle contScopeName, TRAPS) {
2657   assert(element->is_a(SystemDictionary::StackTraceElement_klass()), "sanity check");
2658 
2659   ResourceMark rm(THREAD);
2660   HandleMark hm(THREAD);
2661 
2662   // Fill in class name
2663   Handle java_class(THREAD, holder->java_mirror());
2664   oop classname = java_lang_Class::name(java_class, CHECK);
2665   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2666   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
2667 
2668   oop loader = holder->class_loader();
2669   if (loader != NULL) {
2670     oop loader_name = java_lang_ClassLoader::name(loader);
2671     if (loader_name != NULL)
2672       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
2673   }
2674 
2675   // Fill in method name
2676   oop methodname = StringTable::intern(name, CHECK);


2704       if (source_file == NULL) {
2705         source_file = StringTable::intern(source, CHECK);
2706         java_lang_Class::set_source_file(java_class(), source_file);
2707       }
2708     } else {
2709       // Class was redefined. Dump the cache if it was set.
2710       if (source_file != NULL) {
2711         source_file = NULL;
2712         java_lang_Class::set_source_file(java_class(), source_file);
2713       }
2714       if (ShowHiddenFrames) {
2715         source = vmSymbols::unknown_class_name();
2716         source_file = StringTable::intern(source, CHECK);
2717       }
2718     }
2719     java_lang_StackTraceElement::set_fileName(element(), source_file);
2720 
2721     int line_number = Backtrace::get_line_number(method, bci);
2722     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2723   }
2724 
2725   // Fill in continuation scope
2726   java_lang_StackTraceElement::set_contScopeName(element(), contScopeName());
2727 }
2728 
2729 #if INCLUDE_JVMCI
2730 void java_lang_StackTraceElement::decode(Handle mirror, methodHandle method, int bci, Symbol*& methodname, Symbol*& filename, int& line_number) {
2731   int method_id = method->orig_method_idnum();
2732   int cpref = method->name_index();
2733   decode(mirror, method_id, method->constants()->version(), bci, cpref, methodname, filename, line_number);
2734 }
2735 
2736 void java_lang_StackTraceElement::decode(Handle mirror, int method_id, int version, int bci, int cpref, Symbol*& methodname, Symbol*& filename, int& line_number) {
2737   // Fill in class name
2738   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2739   Method* method = holder->method_with_orig_idnum(method_id, version);
2740 
2741   // The method can be NULL if the requested class version is gone
2742   Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
2743 
2744   // Fill in method name
2745   methodname = sym;
2746 


2757     assert(holder != NULL, "sanity check");
2758     Symbol* source = holder->source_file_name();
2759     if (ShowHiddenFrames && source == NULL) {
2760       source = vmSymbols::unknown_class_name();
2761     }
2762     filename = source;
2763     line_number = Backtrace::get_line_number(method, bci);
2764   }
2765 }
2766 #endif // INCLUDE_JVMCI
2767 
2768 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2769   HandleMark hm(THREAD);
2770   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2771   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2772   // we should expand MemberName::name when Throwable uses StackTrace
2773   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2774   return method;
2775 }
2776 
2777 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, oop cont, TRAPS) {
2778   // set Method* or mid/cpref
2779   HandleMark hm(THREAD);
2780   Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset));
2781   InstanceKlass* ik = method->method_holder();
2782   CallInfo info(method(), ik, CHECK);
2783   MethodHandles::init_method_MemberName(mname, info);
2784   // set bci
2785   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2786   // method may be redefined; store the version
2787   int version = method->constants()->version();
2788   assert((jushort)version == version, "version should be short");
2789   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
2790 
2791   oop contScope = cont != NULL ? java_lang_Continuation::scope(cont) : (oop)NULL;
2792   java_lang_StackFrameInfo::set_contScope(stackFrame(), contScope);
2793 }
2794 
2795 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
2796   ResourceMark rm(THREAD);
2797   HandleMark hm(THREAD);
2798   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
2799   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
2800   InstanceKlass* holder = InstanceKlass::cast(clazz);
2801   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
2802   oop contScope = stackFrame->obj_field(java_lang_StackFrameInfo::_contScope_offset);
2803   Handle contScopeName(THREAD, contScope != (oop)NULL ? java_lang_ContinuationScope::name(contScope) : (oop)NULL);
2804 
2805   short version = stackFrame->short_field(_version_offset);
2806   short bci = stackFrame->short_field(_bci_offset);
2807   Symbol* name = method->name();
2808   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, method, version, bci, name, contScopeName, CHECK);
2809 }
2810 
2811 #define STACKFRAMEINFO_FIELDS_DO(macro) \
2812   macro(_memberName_offset,     k, "memberName", object_signature,            false); \
2813   macro(_bci_offset,            k, "bci",        short_signature,             false); \
2814   macro(_contScope_offset,      k, "contScope",  continuationscope_signature, false)
2815 
2816 void java_lang_StackFrameInfo::compute_offsets() {
2817   InstanceKlass* k = SystemDictionary::StackFrameInfo_klass();
2818   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2819   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2820 }
2821 
2822 #if INCLUDE_CDS
2823 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
2824   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2825   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2826 }
2827 #endif
2828 
2829 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
2830   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
2831   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
2832   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
2833   macro(_mode_offset,       k, "mode",        int_signature,          false)
2834 


4236 int java_lang_reflect_Constructor::parameter_annotations_offset;
4237 int java_lang_reflect_Field::clazz_offset;
4238 int java_lang_reflect_Field::name_offset;
4239 int java_lang_reflect_Field::type_offset;
4240 int java_lang_reflect_Field::slot_offset;
4241 int java_lang_reflect_Field::modifiers_offset;
4242 int java_lang_reflect_Field::signature_offset;
4243 int java_lang_reflect_Field::annotations_offset;
4244 int java_lang_reflect_Parameter::name_offset;
4245 int java_lang_reflect_Parameter::modifiers_offset;
4246 int java_lang_reflect_Parameter::index_offset;
4247 int java_lang_reflect_Parameter::executable_offset;
4248 int java_lang_boxing_object::value_offset;
4249 int java_lang_boxing_object::long_value_offset;
4250 int java_lang_ref_Reference::referent_offset;
4251 int java_lang_ref_Reference::queue_offset;
4252 int java_lang_ref_Reference::next_offset;
4253 int java_lang_ref_Reference::discovered_offset;
4254 int java_lang_ref_SoftReference::timestamp_offset;
4255 int java_lang_ref_SoftReference::static_clock_offset;
4256 int java_lang_ContinuationScope::_name_offset;
4257 int java_lang_Continuation::_scope_offset;
4258 int java_lang_Continuation::_target_offset;
4259 int java_lang_Continuation::_stack_offset;
4260 int java_lang_Continuation::_maxSize_offset;
4261 int java_lang_Continuation::_numFrames_offset;
4262 int java_lang_Continuation::_numInterpretedFrames_offset;
4263 int java_lang_Continuation::_refStack_offset;
4264 int java_lang_Continuation::_parent_offset;
4265 int java_lang_Continuation::_yieldInfo_offset;
4266 int java_lang_Continuation::_entrySP_offset;
4267 int java_lang_Continuation::_entryFP_offset;
4268 int java_lang_Continuation::_entryPC_offset;
4269 int java_lang_Continuation::_fp_offset;
4270 int java_lang_Continuation::_sp_offset;
4271 int java_lang_Continuation::_pc_offset;
4272 int java_lang_Continuation::_refSP_offset;
4273 int java_lang_Continuation::_cs_offset;
4274 int java_lang_Continuation::_flags_offset;
4275 int java_lang_Continuation::_reset_offset;
4276 int java_lang_Continuation::_mounted_offset;
4277 int java_lang_ClassLoader::parent_offset;
4278 int java_lang_System::static_in_offset;
4279 int java_lang_System::static_out_offset;
4280 int java_lang_System::static_err_offset;
4281 int java_lang_System::static_security_offset;
4282 int java_lang_StackTraceElement::methodName_offset;
4283 int java_lang_StackTraceElement::fileName_offset;
4284 int java_lang_StackTraceElement::lineNumber_offset;
4285 int java_lang_StackTraceElement::moduleName_offset;
4286 int java_lang_StackTraceElement::moduleVersion_offset;
4287 int java_lang_StackTraceElement::classLoaderName_offset;
4288 int java_lang_StackTraceElement::declaringClass_offset;
4289 int java_lang_StackTraceElement::declaringClassObject_offset;
4290 int java_lang_StackTraceElement::contScope_offset;
4291 int java_lang_StackFrameInfo::_memberName_offset;
4292 int java_lang_StackFrameInfo::_bci_offset;
4293 int java_lang_StackFrameInfo::_version_offset;
4294 int java_lang_StackFrameInfo::_contScope_offset;
4295 int java_lang_LiveStackFrameInfo::_monitors_offset;
4296 int java_lang_LiveStackFrameInfo::_locals_offset;
4297 int java_lang_LiveStackFrameInfo::_operands_offset;
4298 int java_lang_LiveStackFrameInfo::_mode_offset;
4299 int java_lang_AssertionStatusDirectives::classes_offset;
4300 int java_lang_AssertionStatusDirectives::classEnabled_offset;
4301 int java_lang_AssertionStatusDirectives::packages_offset;
4302 int java_lang_AssertionStatusDirectives::packageEnabled_offset;
4303 int java_lang_AssertionStatusDirectives::deflt_offset;
4304 int java_nio_Buffer::_limit_offset;
4305 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset;
4306 int reflect_ConstantPool::_oop_offset;
4307 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
4308 int java_lang_Integer_IntegerCache::_static_cache_offset;
4309 int java_lang_Long_LongCache::_static_cache_offset;
4310 int java_lang_Character_CharacterCache::_static_cache_offset;
4311 int java_lang_Short_ShortCache::_static_cache_offset;
4312 int java_lang_Byte_ByteCache::_static_cache_offset;
4313 int java_lang_Boolean::_static_TRUE_offset;
4314 int java_lang_Boolean::_static_FALSE_offset;
4315 
4316 
4317 
4318 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4319   macro(declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4320   macro(classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4321   macro(moduleName_offset,      k, "moduleName",      string_signature, false); \
4322   macro(moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4323   macro(declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4324   macro(methodName_offset,      k, "methodName",      string_signature, false); \
4325   macro(fileName_offset,        k, "fileName",        string_signature, false); \
4326   macro(lineNumber_offset,      k, "lineNumber",      int_signature,    false); \
4327   macro(contScope_offset,       k, "contScopeName",   string_signature, false)
4328 
4329 // Support for java_lang_StackTraceElement
4330 void java_lang_StackTraceElement::compute_offsets() {
4331   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
4332   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4333 }
4334 
4335 #if INCLUDE_CDS
4336 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) {
4337   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4338 }
4339 #endif
4340 
4341 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4342   element->obj_field_put(fileName_offset, value);
4343 }
4344 
4345 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4346   element->obj_field_put(declaringClass_offset, value);
4347 }


4353 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
4354   element->int_field_put(lineNumber_offset, value);
4355 }
4356 
4357 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
4358   element->obj_field_put(moduleName_offset, value);
4359 }
4360 
4361 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
4362   element->obj_field_put(moduleVersion_offset, value);
4363 }
4364 
4365 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) {
4366   element->obj_field_put(classLoaderName_offset, value);
4367 }
4368 
4369 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) {
4370   element->obj_field_put(declaringClassObject_offset, value);
4371 }
4372 
4373 void java_lang_StackTraceElement::set_contScopeName(oop element, oop value) {
4374   element->obj_field_put(contScope_offset, value);
4375 }
4376 
4377 void java_lang_StackFrameInfo::set_version(oop element, short value) {
4378   element->short_field_put(_version_offset, value);
4379 }
4380 
4381 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
4382   element->int_field_put(_bci_offset, value);
4383 }
4384 
4385 void java_lang_StackFrameInfo::set_contScope(oop element, oop value) {
4386   element->obj_field_put(_contScope_offset, value);
4387 }
4388 
4389 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
4390   element->obj_field_put(_monitors_offset, value);
4391 }
4392 
4393 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
4394   element->obj_field_put(_locals_offset, value);
4395 }
4396 
4397 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
4398   element->obj_field_put(_operands_offset, value);
4399 }
4400 
4401 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) {
4402   element->int_field_put(_mode_offset, value);
4403 }
4404 
4405 // Support for java Assertions - java_lang_AssertionStatusDirectives.
4406 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \
4407   macro(classes_offset,        k, "classes",        string_array_signature, false); \
4408   macro(classEnabled_offset,   k, "classEnabled",   bool_array_signature, false); \


4424 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
4425   o->obj_field_put(classes_offset, val);
4426 }
4427 
4428 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
4429   o->obj_field_put(classEnabled_offset, val);
4430 }
4431 
4432 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
4433   o->obj_field_put(packages_offset, val);
4434 }
4435 
4436 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
4437   o->obj_field_put(packageEnabled_offset, val);
4438 }
4439 
4440 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
4441   o->bool_field_put(deflt_offset, val);
4442 }
4443 
4444 // Support for java.lang.ContinuationScope
4445 
4446 #define CONTINUATIONSCOPE_FIELDS_DO(macro) \
4447   macro(_name_offset, k, vmSymbols::name_name(), string_signature, false);
4448 
4449 void java_lang_ContinuationScope::compute_offsets() {
4450   InstanceKlass* k = SystemDictionary::ContinuationScope_klass();
4451   CONTINUATIONSCOPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4452 }
4453 
4454 #if INCLUDE_CDS
4455 void java_lang_ContinuationScope::serialize_offsets(SerializeClosure* f) {
4456   CONTINUATIONSCOPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4457 }
4458 #endif
4459 
4460 // Support for java.lang.Continuation
4461 
4462 #define CONTINUATION_FIELDS_DO(macro) \
4463   macro(_scope_offset,     k, vmSymbols::scope_name(),     continuationscope_signature, false); \
4464   macro(_target_offset,    k, vmSymbols::target_name(),    runnable_signature,          false); \
4465   macro(_parent_offset,    k, vmSymbols::parent_name(),    continuation_signature,      false); \
4466   macro(_yieldInfo_offset, k, vmSymbols::yieldInfo_name(), object_signature,            false); \
4467   macro(_stack_offset,     k, vmSymbols::stack_name(),     int_array_signature,         false); \
4468   macro(_maxSize_offset,   k, vmSymbols::maxSize_name(),   int_signature,               false); \
4469   macro(_refStack_offset,  k, vmSymbols::refStack_name(),  object_array_signature,      false); \
4470   macro(_entrySP_offset,   k, vmSymbols::entrySP_name(),   long_signature,              false); \
4471   macro(_entryFP_offset,   k, vmSymbols::entryFP_name(),   long_signature,              false); \
4472   macro(_entryPC_offset,   k, vmSymbols::entryPC_name(),   long_signature,              false); \
4473   macro(_fp_offset,        k, vmSymbols::fp_name(),        long_signature,              false); \
4474   macro(_sp_offset,        k, vmSymbols::sp_name(),        int_signature,               false); \
4475   macro(_pc_offset,        k, vmSymbols::pc_name(),        long_signature,              false); \
4476   macro(_refSP_offset,     k, vmSymbols::refSP_name(),     int_signature,               false); \
4477   macro(_flags_offset,     k, vmSymbols::flags_name(),     byte_signature,              false); \
4478   macro(_cs_offset,        k, vmSymbols::cs_name(),        short_signature,             false); \
4479   macro(_reset_offset,     k, vmSymbols::reset_name(),     bool_signature,              false); \
4480   macro(_mounted_offset,   k, vmSymbols::mounted_name(),   bool_signature,              false); \
4481   macro(_numFrames_offset, k, vmSymbols::numFrames_name(), short_signature,             false); \
4482   macro(_numInterpretedFrames_offset, k, vmSymbols::numInterpretedFrames_name(), short_signature, false);
4483 
4484 void java_lang_Continuation::compute_offsets() {
4485   InstanceKlass* k = SystemDictionary::Continuation_klass();
4486   CONTINUATION_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4487 }
4488 
4489 #if INCLUDE_CDS
4490 void java_lang_Continuation::serialize_offsets(SerializeClosure* f) {
4491   CONTINUATION_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4492 }
4493 #endif
4494 
4495 bool java_lang_Continuation::on_local_stack(oop ref, address adr) {
4496   arrayOop s = stack(ref);
4497   void* base = s->base(T_INT);
4498   return adr >= base && (char*)adr < ((char*)base + (s->length() * 4));
4499 }
4500  
4501 bool java_lang_Continuation::is_mounted(oop ref) {
4502   return ref->bool_field(_mounted_offset) != 0;
4503 }
4504 
4505 
4506 // Support for intrinsification of java.nio.Buffer.checkIndex
4507 int java_nio_Buffer::limit_offset() {
4508   return _limit_offset;
4509 }
4510 
4511 #define BUFFER_FIELDS_DO(macro) \
4512   macro(_limit_offset, k, "limit", int_signature, false)
4513 
4514 void java_nio_Buffer::compute_offsets() {
4515   InstanceKlass* k = SystemDictionary::nio_Buffer_klass();
4516   assert(k != NULL, "must be loaded in 1.4+");
4517   BUFFER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4518 }
4519 
4520 #if INCLUDE_CDS
4521 void java_nio_Buffer::serialize_offsets(SerializeClosure* f) {
4522   BUFFER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4523 }
4524 #endif
4525 
4526 #define AOS_FIELDS_DO(macro) \
4527   macro(_owner_offset, k, "exclusiveOwner", object_signature, false)
4528 
4529 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() {
4530   InstanceKlass* k = SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass();
4531   AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4532 }
4533 
4534 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
4535   assert(_owner_offset != 0, "Must be initialized");
4536   return obj->obj_field(_owner_offset);
4537 }
4538 
4539 #if INCLUDE_CDS
4540 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) {
4541   AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4542 }
4543 #endif
4544 
4545 #define INTEGER_CACHE_FIELDS_DO(macro) \
4546   macro(_static_cache_offset, k, "cache", java_lang_Integer_array_signature, true)
4547 


4729 }
4730 
4731 static int member_offset(int hardcoded_offset) {
4732   return (hardcoded_offset * heapOopSize) + instanceOopDesc::base_offset_in_bytes();
4733 }
4734 
4735 // Compute hard-coded offsets
4736 // Invoked before SystemDictionary::initialize, so pre-loaded classes
4737 // are not available to determine the offset_of_static_fields.
4738 void JavaClasses::compute_hard_coded_offsets() {
4739 
4740   // java_lang_boxing_object
4741   java_lang_boxing_object::value_offset      = member_offset(java_lang_boxing_object::hc_value_offset);
4742   java_lang_boxing_object::long_value_offset = align_up(member_offset(java_lang_boxing_object::hc_value_offset), BytesPerLong);
4743 
4744   // java_lang_ref_Reference
4745   java_lang_ref_Reference::referent_offset    = member_offset(java_lang_ref_Reference::hc_referent_offset);
4746   java_lang_ref_Reference::queue_offset       = member_offset(java_lang_ref_Reference::hc_queue_offset);
4747   java_lang_ref_Reference::next_offset        = member_offset(java_lang_ref_Reference::hc_next_offset);
4748   java_lang_ref_Reference::discovered_offset  = member_offset(java_lang_ref_Reference::hc_discovered_offset);
4749 
4750   // // java_lang_Continuation Class
4751   // java_lang_Continuation::target_offset       = member_offset(java_lang_Continuation::hc_target_offset);
4752   // java_lang_Continuation::parent_offset       = member_offset(java_lang_Continuation::hc_parent_offset);
4753   // java_lang_Continuation::entrySP_offset      = member_offset(java_lang_Continuation::hc_entrySP_offset);
4754   // java_lang_Continuation::entryFP_offset      = member_offset(java_lang_Continuation::hc_entryFP_offset);
4755   // java_lang_Continuation::entryPC_offset      = member_offset(java_lang_Continuation::hc_entryPC_offset);
4756   // java_lang_Continuation::stack_offset        = member_offset(java_lang_Continuation::hc_stack_offset);
4757   // java_lang_Continuation::lastFP_offset       = member_offset(java_lang_Continuation::hc_lastFP_offset);
4758   // java_lang_Continuation::lastSP_offset       = member_offset(java_lang_Continuation::hc_lastSP_offset);
4759 }
4760 
4761 #define DO_COMPUTE_OFFSETS(k) k::compute_offsets();
4762 
4763 // Compute non-hard-coded field offsets of all the classes in this file
4764 void JavaClasses::compute_offsets() {
4765   if (UseSharedSpaces) {
4766     assert(JvmtiExport::is_early_phase() && !(JvmtiExport::should_post_class_file_load_hook() &&
4767                                               JvmtiExport::has_early_class_hook_env()),
4768            "JavaClasses::compute_offsets() must be called in early JVMTI phase.");
4769     // None of the classes used by the rest of this function can be replaced by
4770     // JMVTI ClassFileLoadHook.
4771     // We are safe to use the archived offsets, which have already been restored
4772     // by JavaClasses::serialize_offsets, without computing the offsets again.
4773     return;
4774   }
4775 
4776   // We have already called the compute_offsets() of the
4777   // BASIC_JAVA_CLASSES_DO_PART1 classes (java_lang_String and java_lang_Class)
4778   // earlier inside SystemDictionary::resolve_well_known_classes()


4831 
4832   // Boxed primitive objects (java_lang_boxing_object)
4833 
4834   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
4835   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
4836   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
4837   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
4838   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
4839   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
4840   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
4841   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
4842 
4843   // java.lang.ref.Reference
4844 
4845   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
4846   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
4847   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
4848   // Fake field
4849   //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
4850 
4851   // java.lang.Continuation
4852 
4853   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, target,   "Ljava/lang/Runnable;");
4854   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, stack,    "[I");
4855   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, parent,   "Ljava/lang/Continuation;");
4856   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, entrySP,  "J");
4857   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, entryFP,  "J");
4858   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, entryPC,  "J");
4859   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, lastFP,   "I");
4860   // CHECK_OFFSET("java/lang/Continuation", java_lang_Continuation, lastSP,   "I");
4861 
4862   if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
4863 }
4864 
4865 #endif // PRODUCT
4866 
4867 int InjectedField::compute_offset() {
4868   InstanceKlass* ik = InstanceKlass::cast(klass());
4869   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4870     if (!may_be_java && !fs.access_flags().is_internal()) {
4871       // Only look at injected fields
4872       continue;
4873     }
4874     if (fs.name() == name() && fs.signature() == signature()) {
4875       return fs.offset();
4876     }
4877   }
4878   ResourceMark rm;
4879   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)" : "");
4880 #ifndef PRODUCT
4881   ik->print();
4882   tty->print_cr("all fields:");
4883   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4884     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
4885   }
4886 #endif //PRODUCT
4887   vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
4888   return -1;
4889 }
4890 
4891 void javaClasses_init() {
4892   JavaClasses::compute_offsets();
4893   JavaClasses::check_offsets();
4894   java_lang_Fiber::init_static_notify_jvmti_events();
4895   FilteredFieldsMap::initialize();  // must be done after computing offsets.
4896 }
< prev index next >