1 /*
   2  * Copyright (c) 2006, 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.linux_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 LinuxSPARCJavaThreadPDAccess 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     osThreadField           = type.getAddressField("_osthread");
  61     hasFlushed              = db.lookupIntConstant("JavaFrameAnchor::flushed").intValue();
  62 
  63     type = db.lookupType("OSThread");
  64     osThreadThreadIDField   = type.getCIntegerField("_thread_id");
  65   }
  66 
  67   public    Address getLastJavaFP(Address addr) {
  68         return null;
  69 
  70   }
  71 
  72   public    Address getLastJavaPC(Address addr) {
  73     return null;
  74   }
  75 
  76   public Address getBaseOfStackPointer(Address addr) {
  77     return baseOfStackPointerField.getValue(addr);
  78   }
  79 
  80   public Frame getLastFramePD(JavaThread thread, Address addr) {
  81 
  82     // This assert doesn't work in the debugging case for threads
  83     // which are running Java code and which haven't re-entered the
  84     // runtime (e.g., through a Method.invoke() or otherwise). They
  85     // haven't yet "decached" their last Java stack pointer to the
  86     // thread.
  87 
  88     //    if (Assert.ASSERTS_ENABLED) {
  89     //      Assert.that(hasLastJavaFrame(), "must have last_Java_sp() when suspended");
  90     //      // FIXME: add assertion about flushing register windows for runtime system
  91     //      // (not appropriate for debugging system, though, unless at safepoin t)
  92     //    }
  93 
  94     // FIXME: I don't think this is necessary, but might be useful
  95     // while debugging
  96     if (thread.getLastJavaSP() == null) {
  97       return null;
  98     }
  99 
 100     // sparc does a lazy window flush. The _flags field of the JavaFrameAnchor
 101     // encodes whether the windows have flushed. Whenever the windows have flushed
 102     // there will be a last_Java_pc.
 103     // In a relective system we'd have to  do something to force the thread to flush
 104     // its windows and give us the pc (or the younger_sp so we can find it ourselves)
 105     // In a debugger situation (process or core) the flush should have happened and
 106     // so if we don't have the younger sp we can find it
 107     //
 108     if (thread.getLastJavaPC() != null) {
 109       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()), thread.getLastJavaPC());
 110     } else {
 111       Frame top = getCurrentFrameGuess(thread, addr);
 112       return new SPARCFrame(SPARCFrame.biasSP(thread.getLastJavaSP()),
 113                             SPARCFrame.biasSP(SPARCFrame.findYoungerSP(top.getSP(), thread.getLastJavaSP())),
 114                             false);
 115     }
 116 
 117 
 118   }
 119 
 120   public RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
 121     return new SPARCRegisterMap(thread, updateMap);
 122   }
 123 
 124   public Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
 125     ThreadProxy t = getThreadProxy(addr);
 126     SPARCThreadContext context = (SPARCThreadContext) t.getContext();
 127     // For now, let's see what happens if we do a similar thing to
 128     // what the runtime code does. I suspect this may cause us to lose
 129     // the top frame from the stack.
 130     Address sp = context.getRegisterAsAddress(SPARCThreadContext.R_SP);
 131     Address pc = context.getRegisterAsAddress(SPARCThreadContext.R_PC);
 132 
 133     if ((sp == null) || (pc == null)) {
 134       // Problems (have not hit this case so far, but would be bad to continue if we did)
 135       return null;
 136     }
 137 
 138     return new SPARCFrame(sp, pc);
 139   }
 140 
 141 
 142   public void printThreadIDOn(Address addr, PrintStream tty) {
 143     tty.print(getThreadProxy(addr));
 144   }
 145 
 146   public Address getLastSP(Address addr) {
 147     ThreadProxy t = getThreadProxy(addr);
 148     SPARCThreadContext context = (SPARCThreadContext) t.getContext();
 149     return SPARCFrame.unBiasSP(context.getRegisterAsAddress(SPARCThreadContext.R_SP));
 150   }
 151 
 152   public void printInfoOn(Address threadAddr, PrintStream tty) {
 153   }
 154 
 155   public ThreadProxy getThreadProxy(Address addr) {
 156     // Fetch the OSThread (for now and for simplicity, not making a
 157     // separate "OSThread" class in this package)
 158     Address osThreadAddr = osThreadField.getValue(addr);
 159     // Get the address of the thread ID from the OSThread
 160     Address tidAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 161 
 162     JVMDebugger debugger = VM.getVM().getDebugger();
 163     return debugger.getThreadForIdentifierAddress(tidAddr);
 164   }
 165 
 166 
 167 }