1 /*
   2  * Copyright (c) 2020, Microsoft Corporation. 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.win32_aarch64;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.debugger.aarch64.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.runtime.aarch64.*;
  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 /** This class is only public to allow using the VMObjectFactory to
  39     instantiate these.
  40 */
  41 
  42 public class Win32AARCH64JavaThreadPDAccess implements JavaThreadPDAccess {
  43   private static AddressField  lastJavaFPField;
  44   private static AddressField  osThreadField;
  45 
  46   // Field from OSThread
  47   private static Field         osThreadThreadIDField;
  48 
  49   // This is currently unneeded but is being kept in case we change
  50   // the currentFrameGuess algorithm
  51   private static final long GUESS_SCAN_RANGE = 128 * 1024;
  52 
  53   static {
  54     VM.registerVMInitializedObserver(new Observer() {
  55         public void update(Observable o, Object data) {
  56           initialize(VM.getVM().getTypeDataBase());
  57         }
  58       });
  59   }
  60 
  61   private static synchronized void initialize(TypeDataBase db) {
  62     Type type = db.lookupType("JavaThread");
  63     osThreadField           = type.getAddressField("_osthread");
  64 
  65     Type anchorType = db.lookupType("JavaFrameAnchor");
  66     lastJavaFPField         = anchorType.getAddressField("_last_Java_fp");
  67 
  68     Type osThreadType = db.lookupType("OSThread");
  69     osThreadThreadIDField = osThreadType.getField("_thread_id");
  70   }
  71 
  72   public Address getLastJavaFP(Address addr) {
  73     return lastJavaFPField.getValue(addr.addOffsetTo(sun.jvm.hotspot.runtime.JavaThread.getAnchorField().getOffset()));
  74   }
  75 
  76   public Address getLastJavaPC(Address addr) {
  77     return null;
  78   }
  79 
  80   public Address getBaseOfStackPointer(Address addr) {
  81     return null;
  82   }
  83 
  84   public Frame getLastFramePD(JavaThread thread, Address addr) {
  85     Address fp = thread.getLastJavaFP();
  86     if (fp == null) {
  87       return null; // no information
  88     }
  89     Address pc =  thread.getLastJavaPC();
  90     if ( pc != null ) {
  91       return new AARCH64Frame(thread.getLastJavaSP(), fp, pc);
  92     } else {
  93       return new AARCH64Frame(thread.getLastJavaSP(), fp);
  94     }
  95   }
  96 
  97   public RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
  98     return new AARCH64RegisterMap(thread, updateMap);
  99   }
 100 
 101   public Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
 102     ThreadProxy t = getThreadProxy(addr);
 103     AARCH64ThreadContext context = (AARCH64ThreadContext) t.getContext();
 104     AARCH64CurrentFrameGuess guesser = new AARCH64CurrentFrameGuess(context, thread);
 105     if (!guesser.run(GUESS_SCAN_RANGE)) {
 106       return null;
 107     }
 108     if (guesser.getPC() == null) {
 109       return new AARCH64Frame(guesser.getSP(), guesser.getFP());
 110     } else {
 111       return new AARCH64Frame(guesser.getSP(), guesser.getFP(), guesser.getPC());
 112     }
 113   }
 114 
 115   public void printThreadIDOn(Address addr, PrintStream tty) {
 116     tty.print(getThreadProxy(addr));
 117   }
 118 
 119   public void printInfoOn(Address threadAddr, PrintStream tty) {
 120   }
 121 
 122   public Address getLastSP(Address addr) {
 123     ThreadProxy t = getThreadProxy(addr);
 124     AARCH64ThreadContext context = (AARCH64ThreadContext) t.getContext();
 125     return context.getRegisterAsAddress(AARCH64ThreadContext.SP);
 126   }
 127 
 128   public ThreadProxy getThreadProxy(Address addr) {
 129     // Addr is the address of the JavaThread.
 130     // Fetch the OSThread (for now and for simplicity, not making a
 131     // separate "OSThread" class in this package)
 132     Address osThreadAddr = osThreadField.getValue(addr);
 133     // Get the address of the thread_id within the OSThread
 134     Address threadIdAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 135 
 136     JVMDebugger debugger = VM.getVM().getDebugger();
 137     return debugger.getThreadForIdentifierAddress(threadIdAddr);
 138   }
 139 }