Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.
   *
   * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  *
  */
 
 package sun.jvm.hotspot.runtime;
 
 import java.io.*;
 import java.util.*;
This is an abstract class because there are certain OS- and CPU-specific operations (like the setting and getting of the last Java frame pointer) which need to be factored out. These operations are implemented by, for example, SolarisSPARCJavaThread, and the concrete subclasses are instantiated by the JavaThreadFactory in the Threads class.
 
 
 public class JavaThread extends Thread {
   private static final boolean DEBUG = System.getProperty("sun.jvm.hotspot.runtime.JavaThread.DEBUG") != null;
 
   private static AddressField  nextField;
   private static sun.jvm.hotspot.types.OopField threadObjField;
   private static AddressField  anchorField;
   private static AddressField  lastJavaSPField;
   private static AddressField  lastJavaPCField;
   private static CIntegerField threadStateField;
   private static AddressField  osThreadField;
   private static AddressField  stackBaseField;
   private static CIntegerField stackSizeField;
 
   private static JavaThreadPDAccess access;
 
   // JavaThreadStates read from underlying process
   private static int           UNINITIALIZED;
   private static int           NEW;
   private static int           NEW_TRANS;
   private static int           IN_NATIVE;
   private static int           IN_NATIVE_TRANS;
   private static int           IN_VM;
   private static int           IN_VM_TRANS;
   private static int           IN_JAVA;
   private static int           IN_JAVA_TRANS;
   private static int           BLOCKED;
   private static int           BLOCKED_TRANS;
 
   static {
         public void update(Observable oObject data) {
           initialize(VM.getVM().getTypeDataBase());
         }
       });
   }
 
   private static synchronized void initialize(TypeDataBase db) {
     Type type = db.lookupType("JavaThread");
     Type anchorType = db.lookupType("JavaFrameAnchor");
 
              = type.getAddressField("_next");
         = type.getOopField("_threadObj");
            = type.getAddressField("_anchor");
        = anchorType.getAddressField("_last_Java_sp");
        = anchorType.getAddressField("_last_Java_pc");
       = type.getCIntegerField("_thread_state");
          = type.getAddressField("_osthread");
         = type.getAddressField("_stack_base");
         = type.getCIntegerField("_stack_size");
 
          = db.lookupIntConstant("_thread_uninitialized").intValue();
                    = db.lookupIntConstant("_thread_new").intValue();
              = db.lookupIntConstant("_thread_new_trans").intValue();
              = db.lookupIntConstant("_thread_in_native").intValue();
        = db.lookupIntConstant("_thread_in_native_trans").intValue();
                  = db.lookupIntConstant("_thread_in_vm").intValue();
            = db.lookupIntConstant("_thread_in_vm_trans").intValue();
                = db.lookupIntConstant("_thread_in_Java").intValue();
          = db.lookupIntConstant("_thread_in_Java_trans").intValue();
               = db.lookupIntConstant("_thread_blocked").intValue();
         = db.lookupIntConstant("_thread_blocked_trans").intValue();
  }
  public JavaThread(Address addr) {
    super(addr);
  }
    this. = access;
  }
  public JavaThread next() {
    Address threadAddr = .getValue();
    if (threadAddr == null) {
      return null;
    }
    return VM.getVM().getThreads().createJavaThreadWrapper(threadAddr);
  }

  
NOTE: for convenience, this differs in definition from the underlying VM. Only "pure" JavaThreads return true; CompilerThreads and JVMDIDebuggerThreads return false. FIXME: consider encapsulating platform-specific functionality in an object instead of using inheritance (which is the primary reason we can't traverse CompilerThreads, etc; didn't want to have, for example, "SolarisSPARCCompilerThread".)
  public boolean isJavaThread() { return true; }
  public static AddressField getAnchorField() { return ; }

  
Get the last Java stack pointer
  public Address getLastJavaSP() {
    return sp;
  }
  public Address getLastJavaPC() {
    return pc;
  }

  
Abstract accessor to last Java frame pointer, implemented by OS/CPU-specific JavaThread implementation. May return null if there is no frame pointer or if it is not necessary on this platform.
  public Address getLastJavaFP(){
        return .getLastJavaFP();
  }

  
Abstract accessor to last Java pc, implemented by OS/CPU-specific JavaThread implementation. May return null if there is no frame pointer or if it is not necessary on this platform.
  /*
  public Address getLastJavaPC(){
        return access.getLastJavaPC(addr);
  }
  */
  // FIXME: not yet implementable
  //  public abstract void    setLastJavaFP(Address fp);

  
A stack pointer older than any java frame stack pointer. Only needed on some platforms; for example, see thread_solaris_sparc.hpp.
        return .getBaseOfStackPointer();
  }
  // FIXME: not yet implementable
  //  public abstract void    setBaseOfStackPointer(Address fp);

  
Tells whether the last Java frame is set
  public boolean hasLastJavaFrame() {
    return (getLastJavaSP() != null);
  }

  
Accessing frames
  public Frame getLastFrame() {
    // FIXME: would need to implement runtime routine
    // "cacheStatePD(boolean)" for reflective system to be able to
    // flush register windows on SPARC
    return cookLastFrame(getLastFramePD());
  }

  
Internal routine implemented by platform-dependent subclasses
  protected Frame getLastFramePD(){
        return .getLastFramePD(this);
  }

  
Accessing frames. Returns the last Java VFrame or null if none was present. (NOTE that this is mostly unusable in a debugging system; see getLastJavaVFrameDbg, below, which provides very different functionality.)
    if (.) {
      Assert.that(regMap != null"a map must be given");
    }
    Frame f = getLastFrame();
    if (f == null) {
      return null;
    }
    for (VFrame vf = VFrame.newVFrame(fregMapthis); vf != nullvf = vf.sender()) {
      if (vf.isJavaFrame()) {
        return (JavaVFramevf;
      }
    }
    return null;
  }

  
This should only be used by a debugger. Uses the current frame guess to attempt to get the topmost JavaVFrame. (getLastJavaVFrame, as a port of the VM's routine, assumes the VM is at a safepoint.)
    RegisterMap regMap = newRegisterMap(true);
    if (f == nullreturn null;
    boolean imprecise = true;
    if (f.isInterpretedFrame() && !f.isInterpretedFrameValid()) {
       if () {
         ..println("Correcting for invalid interpreter frame");
       }
       f = f.sender(regMap);
       imprecise = false;
    }
    VFrame vf = VFrame.newVFrame(fregMapthistrueimprecise);
    if (vf == null) {
      if () {
        ..println(" (Unable to create vframe for topmost frame guess)");
      }
      return null;
    }
    return vf.isJavaFrame() ? (JavaVFrame)vf : vf.javaSender();
  }

  
In this system, a JavaThread is the top-level factory for a RegisterMap, since the JavaThread implementation is already platform-specific and RegisterMap is also necessarily platform-specific. The updateMap argument indicates whether the register map needs to be updated, for example during stack traversal -- see frame.hpp.
  public RegisterMap newRegisterMap(boolean updateMap){
        return .newRegisterMap(thisupdateMap);
  }

  
This is only designed to be used by the debugging system. Returns a "best guess" of the topmost frame on the stack. This guess should be as "raw" as possible. For example, if the topmost frame is an interpreter frame (the return PC is in the interpreter) but is not a valid frame (i.e., the BCI has not yet been set up) this should still return the topmost frame and not the sender. Validity checks are done at higher levels.
  public  Frame getCurrentFrameGuess(){
        return .getCurrentFrameGuess(this);
  }

  
Also only intended for use by the debugging system. Provides the same effect of OSThread::print(); that is, prints a value which allows the user to intuitively understand which native OS thread maps to this Java thread. Does not print a newline or leading or trailing spaces.
  public  void printThreadIDOn(PrintStream tty) {
        .printThreadIDOn(,tty);
  }
  public void printThreadID() {
  }
  public ThreadProxy getThreadProxy() {
    return .getThreadProxy();
  }
  //
  // Safepoint support
  //
    int val = (int.getValue();
    if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else if (val == ) {
      return .;
    } else {
      throw new RuntimeException("Illegal thread state " + val);
    }
  }
  // FIXME: not yet implementable
  // public void setThreadState(JavaThreadState s);
  //
  // Miscellaneous operations
  //
  public OSThread getOSThread() {
    return (OSThread) VMObjectFactory.newObject(OSThread.class.getValue());
  }
  public Address getStackBase() {
    return .getValue();
  }
  public long getStackSize() {
    return .getValue();
  }

  
Gets the Java-side thread object for this JavaThread
  public Oop getThreadObj() {
    Oop obj = null;
    try {
    } catch (Exception e) {
      e.printStackTrace();
    }
    return obj;
  }

  
Get the Java-side name of this thread
  public String getThreadName() {
    Oop threadObj = getThreadObj();
    if (threadObj == null) {
        return "<null>";
    }
    return OopUtilities.threadOopGetName(threadObj);
  }
  //
  // Oop traversal
  //
  public void oopsDo(AddressVisitor oopVisitor) {
    super.oopsDo(oopVisitor);
    // FIXME: add in the rest of the routine from the VM
    // Traverse the execution stack
    for(StackFrameStream fst = new StackFrameStream(this); !fst.isDone(); fst.next()) {
      fst.getCurrent().oopsDo(oopVisitorfst.getRegisterMap());
    }
  }
  public boolean isInStack(Address a) {
    if (.) {
      Assert.that(VM.getVM().isDebugging(), "Not yet implemented for non-debugging system");
    }
    Address sp      = lastSPDbg();
    Address stackBase = getStackBase();
    // Be robust
    if (sp == nullreturn false;
    return stackBase.greaterThanOrEqual(a) && sp.lessThanOrEqual(a);
  }
  public boolean isLockOwned(Address a) {
    Address stackBase = getStackBase();
    Address stackLimit = stackBase.addOffsetTo(-getStackSize());
    return stackBase.greaterThanOrEqual(a) && stackLimit.lessThanOrEqual(a);
    // FIXME: should traverse MonitorArray/MonitorChunks as in VM
  }
  public Oop getCurrentParkBlocker() {
    Oop threadObj = getThreadObj();
    if (threadObj != null) {
      return OopUtilities.threadOopGetParkBlocker(threadObj);
    }
    return null;
  }
  public void printInfoOn(PrintStream tty) {
    tty.println("State: " + getThreadState().toString());
    // Attempt to figure out the addresses covered by Java frames.
    // NOTE: we should make this a method and let the Stackwalk panel use the result too.
    //
    if (tmpFrame != null ) {
      Address sp = tmpFrame.getSP();
      Address maxSP = sp;
      Address minSP = sp;
      RegisterMap tmpMap = newRegisterMap(false);
      while ((tmpFrame != null) && (!tmpFrame.isFirstFrame())) {
          tmpFrame = tmpFrame.sender(tmpMap);
          if (tmpFrame != null) {
            sp = tmpFrame.getSP();
            maxSP = AddressOps.max(maxSPsp);
            minSP = AddressOps.min(minSPsp);
          }
      }
      tty.println("Stack in use by Java: " + minSP + " .. " + maxSP);
    } else {
      tty.println("No Java frames present");
    }
    tty.println("Base of Stack: " + getBaseOfStackPointer());
    tty.println("Last_Java_SP: " + getLastJavaSP());
    tty.println("Last_Java_FP: " + getLastJavaFP());
    tty.println("Last_Java_PC: " + getLastJavaPC());
    // More stuff like saved_execption_pc, safepoint_state, ...
    .printInfoOn(tty);
  }
  ///////////////////////////////
  //                           //
  // FIXME: add more accessors //
  //                           //
  ///////////////////////////////
  //--------------------------------------------------------------------------------
  // Internals only below this point
  //
  private Frame cookLastFrame(Frame fr) {
    if (fr == null) {
      return null;
    }
    Address pc        = fr.getPC();
    if (.) {
      if (pc == null) {
        Assert.that(VM.getVM().isDebugging(), "must have PC");
      }
    }
    return fr;
  }
  private Address lastSPDbg() {
    return .getLastSP();
  }
New to GrepCode? Check out our FAQ X