1 /*
   2  * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.runtime.solaris_sparc;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.debugger.sparc.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.runtime.sparc.*;
  33 import sun.jvm.hotspot.types.*;
  34 import sun.jvm.hotspot.utilities.*;
  35 
  36 public class SolarisSPARCJavaThreadPDAccess implements JavaThreadPDAccess {
  37   private static AddressField baseOfStackPointerField;
  38   private static AddressField postJavaStateField;
  39   private static AddressField osThreadField;
  40   private static int          isPC;
  41   private static int          hasFlushed;
  42 
  43   // Field from OSThread
  44   private static CIntegerField osThreadThreadIDField;
  45 
  46   static {
  47     VM.registerVMInitializedObserver(new Observer() {
  48         public void update(Observable o, Object data) {
  49           initialize(VM.getVM().getTypeDataBase());
  50         }
  51       });
  52   }
  53 
  54   private static synchronized void initialize(TypeDataBase db) {
  55     Type type = db.lookupType("JavaThread");
  56     Type anchorType = db.lookupType("JavaFrameAnchor");
  57 
  58     baseOfStackPointerField = type.getAddressField("_base_of_stack_pointer");
  59     osThreadField           = type.getAddressField("_osthread");
  60     hasFlushed              = db.lookupIntConstant("JavaFrameAnchor::flushed").intValue();
  61 
  62     type = db.lookupType("OSThread");
  63     osThreadThreadIDField   = type.getCIntegerField("_thread_id");
  64   }
  65 
  66   public    Address getLastJavaFP(Address addr) {
  67         return null;
  68 
  69   }
  70 
  71   public    Address getLastJavaPC(Address addr) {
  72     return null;
  73   }
  74 
  75   public Address getBaseOfStackPointer(Address addr) {
  76     return baseOfStackPointerField.getValue(addr);
  77   }
  78 
  79   public Frame getLastFramePD(JavaThread thread, Address addr) {
  80 
  81     // This assert doesn't work in the debugging case for threads
  82     // which are running Java code and which haven't re-entered the
  83     // runtime (e.g., through a Method.invoke() or otherwise). They
  84     // haven't yet "decached" their last Java stack pointer to the
  85     // thread.
  86 
  87     //    if (Assert.ASSERTS_ENABLED) {
  88     //      Assert.that(hasLastJavaFrame(), "must have last_Java_sp() when suspended");
  89     //      // FIXME: add assertion about flushing register windows for runtime system
  90     //      // (not appropriate for debugging system, though, unless at safepoin t)
  91     //    }
  92 
  93     // FIXME: I don't think this is necessary, but might be useful
  94     // while debugging
  95     if (thread.getLastJavaSP() == null) {
  96       return null;
  97     }
  98 
  99     // sparc does a lazy window flush. The _flags field of the JavaFrameAnchor
 100     // encodes whether the windows have flushed. Whenever the windows have flushed
 101     // there will be a last_Java_pc.
 102     // In a relective system we'd have to  do something to force the thread to flush
 103     // its windows and give us the pc (or the younger_sp so we can find it ourselves)
 104     // In a debugger situation (process or core) the flush should have happened and
 105     // so if we don't have the younger sp we can find it
 106     //
 107     if (thread.getLastJavaPC() != null) {
 108       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()), thread.getLastJavaPC());
 109     } else {
 110       Frame top = getCurrentFrameGuess(thread, addr);
 111       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()),
 112                             SPARCFrame.biasSP(SPARCFrame.findYoungerSP(top.getSP(), thread.getLastJavaSP())),
 113                             false);
 114     }
 115 
 116 
 117   }
 118 
 119   public RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
 120     return new SPARCRegisterMap(thread, updateMap);
 121   }
 122 
 123   public Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
 124 
 125     // If java stack is walkable then both last_Java_sp and last_Java_pc are
 126     // non null and we can start stack walk from this frame.
 127     if (thread.getLastJavaSP() != null && thread.getLastJavaPC() != null) {
 128       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()), thread.getLastJavaPC());
 129     }
 130 
 131     ThreadProxy t = getThreadProxy(addr);
 132     SPARCThreadContext context = (SPARCThreadContext) t.getContext();
 133     // For now, let's see what happens if we do a similar thing to
 134     // what the runtime code does. I suspect this may cause us to lose
 135     // the top frame from the stack.
 136     Address sp = context.getRegisterAsAddress(SPARCThreadContext.R_SP);
 137     Address pc = context.getRegisterAsAddress(SPARCThreadContext.R_PC);
 138 
 139     if ((sp == null) || (pc == null)) {
 140       // Problems (have not hit this case so far, but would be bad to continue if we did)
 141       return null;
 142     }
 143 
 144     return new SPARCFrame(sp, pc);
 145   }
 146 
 147 
 148   public void printThreadIDOn(Address addr, PrintStream tty) {
 149     tty.print(getThreadProxy(addr));
 150   }
 151 
 152   public Address getLastSP(Address addr) {
 153     ThreadProxy t = getThreadProxy(addr);
 154     SPARCThreadContext context = (SPARCThreadContext) t.getContext();
 155     return SPARCFrame.unBiasSP(context.getRegisterAsAddress(SPARCThreadContext.R_SP));
 156   }
 157 
 158   public void printInfoOn(Address threadAddr, PrintStream tty) {
 159   }
 160 
 161   public ThreadProxy getThreadProxy(Address addr) {
 162     // Fetch the OSThread (for now and for simplicity, not making a
 163     // separate "OSThread" class in this package)
 164     Address osThreadAddr = osThreadField.getValue(addr);
 165     // Get the address of the thread ID from the OSThread
 166     Address tidAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 167 
 168     JVMDebugger debugger = VM.getVM().getDebugger();
 169     return debugger.getThreadForIdentifierAddress(tidAddr);
 170   }
 171 
 172 
 173 }