1 /*
   2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * 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 import sun.jvm.hotspot.utilities.Observable;
  36 import sun.jvm.hotspot.utilities.Observer;
  37 
  38 public class SolarisSPARCJavaThreadPDAccess implements JavaThreadPDAccess {
  39   private static AddressField baseOfStackPointerField;
  40   private static AddressField postJavaStateField;
  41   private static AddressField osThreadField;
  42   private static int          isPC;
  43   private static int          hasFlushed;
  44 
  45   // Field from OSThread
  46   private static CIntegerField osThreadThreadIDField;
  47 
  48   static {
  49     VM.registerVMInitializedObserver(new Observer() {
  50         public void update(Observable o, Object data) {
  51           initialize(VM.getVM().getTypeDataBase());
  52         }
  53       });
  54   }
  55 
  56   private static synchronized void initialize(TypeDataBase db) {
  57     Type type = db.lookupType("JavaThread");
  58     Type anchorType = db.lookupType("JavaFrameAnchor");
  59 
  60     baseOfStackPointerField = type.getAddressField("_base_of_stack_pointer");
  61     osThreadField           = type.getAddressField("_osthread");
  62     hasFlushed              = db.lookupIntConstant("JavaFrameAnchor::flushed").intValue();
  63 
  64     type = db.lookupType("OSThread");
  65     osThreadThreadIDField   = type.getCIntegerField("_thread_id");
  66   }
  67 
  68   public    Address getLastJavaFP(Address addr) {
  69         return null;
  70 
  71   }
  72 
  73   public    Address getLastJavaPC(Address addr) {
  74     return null;
  75   }
  76 
  77   public Address getBaseOfStackPointer(Address addr) {
  78     return baseOfStackPointerField.getValue(addr);
  79   }
  80 
  81   public Frame getLastFramePD(JavaThread thread, Address addr) {
  82 
  83     // This assert doesn't work in the debugging case for threads
  84     // which are running Java code and which haven't re-entered the
  85     // runtime (e.g., through a Method.invoke() or otherwise). They
  86     // haven't yet "decached" their last Java stack pointer to the
  87     // thread.
  88 
  89     //    if (Assert.ASSERTS_ENABLED) {
  90     //      Assert.that(hasLastJavaFrame(), "must have last_Java_sp() when suspended");
  91     //      // FIXME: add assertion about flushing register windows for runtime system
  92     //      // (not appropriate for debugging system, though, unless at safepoin t)
  93     //    }
  94 
  95     // FIXME: I don't think this is necessary, but might be useful
  96     // while debugging
  97     if (thread.getLastJavaSP() == null) {
  98       return null;
  99     }
 100 
 101     // sparc does a lazy window flush. The _flags field of the JavaFrameAnchor
 102     // encodes whether the windows have flushed. Whenever the windows have flushed
 103     // there will be a last_Java_pc.
 104     // In a relective system we'd have to  do something to force the thread to flush
 105     // its windows and give us the pc (or the younger_sp so we can find it ourselves)
 106     // In a debugger situation (process or core) the flush should have happened and
 107     // so if we don't have the younger sp we can find it
 108     //
 109     if (thread.getLastJavaPC() != null) {
 110       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()), thread.getLastJavaPC());
 111     } else {
 112       Frame top = getCurrentFrameGuess(thread, addr);
 113       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()),
 114                             SPARCFrame.biasSP(SPARCFrame.findYoungerSP(top.getSP(), thread.getLastJavaSP())),
 115                             false);
 116     }
 117 
 118 
 119   }
 120 
 121   public RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
 122     return new SPARCRegisterMap(thread, updateMap);
 123   }
 124 
 125   public Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
 126 
 127     // If java stack is walkable then both last_Java_sp and last_Java_pc are
 128     // non null and we can start stack walk from this frame.
 129     if (thread.getLastJavaSP() != null && thread.getLastJavaPC() != null) {
 130       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()), thread.getLastJavaPC());
 131     }
 132 
 133     ThreadProxy t = getThreadProxy(addr);
 134     SPARCThreadContext context = (SPARCThreadContext) t.getContext();
 135     // For now, let's see what happens if we do a similar thing to
 136     // what the runtime code does. I suspect this may cause us to lose
 137     // the top frame from the stack.
 138     Address sp = context.getRegisterAsAddress(SPARCThreadContext.R_SP);
 139     Address pc = context.getRegisterAsAddress(SPARCThreadContext.R_PC);
 140 
 141     if ((sp == null) || (pc == null)) {
 142       // Problems (have not hit this case so far, but would be bad to continue if we did)
 143       return null;
 144     }
 145 
 146     return new SPARCFrame(sp, pc);
 147   }
 148 
 149 
 150   public void printThreadIDOn(Address addr, PrintStream tty) {
 151     tty.print(getThreadProxy(addr));
 152   }
 153 
 154   public Address getLastSP(Address addr) {
 155     ThreadProxy t = getThreadProxy(addr);
 156     SPARCThreadContext context = (SPARCThreadContext) t.getContext();
 157     return SPARCFrame.unBiasSP(context.getRegisterAsAddress(SPARCThreadContext.R_SP));
 158   }
 159 
 160   public void printInfoOn(Address threadAddr, PrintStream tty) {
 161   }
 162 
 163   public ThreadProxy getThreadProxy(Address addr) {
 164     // Fetch the OSThread (for now and for simplicity, not making a
 165     // separate "OSThread" class in this package)
 166     Address osThreadAddr = osThreadField.getValue(addr);
 167     // Get the address of the thread ID from the OSThread
 168     Address tidAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 169 
 170     JVMDebugger debugger = VM.getVM().getDebugger();
 171     return debugger.getThreadForIdentifierAddress(tidAddr);
 172   }
 173 
 174 
 175 }