1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015, Red Hat Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 package sun.jvm.hotspot.runtime.linux_aarch64;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.debugger.aarch64.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.runtime.aarch64.*;
  34 import sun.jvm.hotspot.types.*;
  35 import sun.jvm.hotspot.utilities.*;
  36 
  37 public class LinuxAARCH64JavaThreadPDAccess implements JavaThreadPDAccess {
  38   private static AddressField  lastJavaFPField;
  39   private static AddressField  osThreadField;
  40 
  41   // Field from OSThread
  42   private static CIntegerField osThreadThreadIDField;
  43 
  44   // This is currently unneeded but is being kept in case we change
  45   // the currentFrameGuess algorithm
  46   private static final long GUESS_SCAN_RANGE = 128 * 1024;
  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     osThreadField           = type.getAddressField("_osthread");
  59 
  60     Type anchorType = db.lookupType("JavaFrameAnchor");
  61     lastJavaFPField         = anchorType.getAddressField("_last_Java_fp");
  62 
  63     Type osThreadType = db.lookupType("OSThread");
  64     osThreadThreadIDField   = osThreadType.getCIntegerField("_thread_id");
  65   }
  66 
  67   public    Address getLastJavaFP(Address addr) {
  68     return lastJavaFPField.getValue(addr.addOffsetTo(sun.jvm.hotspot.runtime.JavaThread.getAnchorField().getOffset()));
  69   }
  70 
  71   public    Address getLastJavaPC(Address addr) {
  72     return null;
  73   }
  74 
  75   public    Address getBaseOfStackPointer(Address addr) {
  76     return null;
  77   }
  78 
  79   public    Frame getLastFramePD(JavaThread thread, Address addr) {
  80     Address fp = thread.getLastJavaFP();
  81     if (fp == null) {
  82       return null; // no information
  83     }
  84     return new AARCH64Frame(thread.getLastJavaSP(), fp);
  85   }
  86 
  87   public    RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
  88     return new AARCH64RegisterMap(thread, updateMap);
  89   }
  90 
  91   public    Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
  92     ThreadProxy t = getThreadProxy(addr);
  93     AARCH64ThreadContext context = (AARCH64ThreadContext) t.getContext();
  94     AARCH64CurrentFrameGuess guesser = new AARCH64CurrentFrameGuess(context, thread);
  95     if (!guesser.run(GUESS_SCAN_RANGE)) {
  96       return null;
  97     }
  98     if (guesser.getPC() == null) {
  99       return new AARCH64Frame(guesser.getSP(), guesser.getFP());
 100     } else {
 101       return new AARCH64Frame(guesser.getSP(), guesser.getFP(), guesser.getPC());
 102     }
 103   }
 104 
 105   public    void printThreadIDOn(Address addr, PrintStream tty) {
 106     tty.print(getThreadProxy(addr));
 107   }
 108 
 109   public    void printInfoOn(Address threadAddr, PrintStream tty) {
 110     tty.print("Thread id: ");
 111     printThreadIDOn(threadAddr, tty);
 112 //    tty.println("\nPostJavaState: " + getPostJavaState(threadAddr));
 113   }
 114 
 115   public    Address getLastSP(Address addr) {
 116     ThreadProxy t = getThreadProxy(addr);
 117     AARCH64ThreadContext context = (AARCH64ThreadContext) t.getContext();
 118     return context.getRegisterAsAddress(AARCH64ThreadContext.SP);
 119   }
 120 
 121   public    ThreadProxy getThreadProxy(Address addr) {
 122     // Addr is the address of the JavaThread.
 123     // Fetch the OSThread (for now and for simplicity, not making a
 124     // separate "OSThread" class in this package)
 125     Address osThreadAddr = osThreadField.getValue(addr);
 126     // Get the address of the _thread_id from the OSThread
 127     Address threadIdAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 128 
 129     JVMDebugger debugger = VM.getVM().getDebugger();
 130     return debugger.getThreadForIdentifierAddress(threadIdAddr);
 131   }
 132 }