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_x86;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.debugger.x86.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.runtime.x86.*;
  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 /** Placeholder for now to allow us to start the SA without support
  39     for stack traces */
  40 
  41 public class SolarisX86JavaThreadPDAccess implements JavaThreadPDAccess {
  42   private static AddressField lastJavaFPField;
  43   private static AddressField osThreadField;
  44   private static AddressField baseOfStackPointerField;
  45 
  46   // Field from OSThread
  47   private static CIntegerField 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 
  54   static {
  55     VM.registerVMInitializedObserver(new Observer() {
  56         public void update(Observable o, Object data) {
  57           initialize(VM.getVM().getTypeDataBase());
  58         }
  59       });
  60   }
  61 
  62   private static synchronized void initialize(TypeDataBase db) {
  63     Type type = db.lookupType("JavaThread");
  64     Type anchorType = db.lookupType("JavaFrameAnchor");
  65 
  66     lastJavaFPField    = anchorType.getAddressField("_last_Java_fp");
  67     osThreadField      = type.getAddressField("_osthread");
  68 
  69     type = db.lookupType("OSThread");
  70     osThreadThreadIDField   = type.getCIntegerField("_thread_id");
  71   }
  72 
  73   public    Address getLastJavaFP(Address addr) {
  74     return lastJavaFPField.getValue(addr.addOffsetTo(sun.jvm.hotspot.runtime.JavaThread.getAnchorField().getOffset()));
  75   }
  76 
  77   public    Address getLastJavaPC(Address addr) {
  78     return null;
  79   }
  80 
  81   public Address getBaseOfStackPointer(Address addr) {
  82     return null;
  83   }
  84 
  85   public Frame getLastFramePD(JavaThread thread, Address addr) {
  86     Address fp = thread.getLastJavaFP();
  87     if (fp == null) {
  88       return null; // no information
  89     }
  90     Address pc =  thread.getLastJavaPC();
  91     if ( pc != null ) {
  92       return new X86Frame(thread.getLastJavaSP(), fp, pc);
  93     } else {
  94       return new X86Frame(thread.getLastJavaSP(), fp);
  95     }
  96   }
  97 
  98   public RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
  99     return new X86RegisterMap(thread, updateMap);
 100   }
 101 
 102   public Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
 103     ThreadProxy t = getThreadProxy(addr);
 104     X86ThreadContext context = (X86ThreadContext) t.getContext();
 105     X86CurrentFrameGuess guesser = new X86CurrentFrameGuess(context, thread);
 106     if (!guesser.run(GUESS_SCAN_RANGE)) {
 107       return null;
 108     }
 109     if (guesser.getPC() == null) {
 110       return new X86Frame(guesser.getSP(), guesser.getFP());
 111     } else {
 112       return new X86Frame(guesser.getSP(), guesser.getFP(), guesser.getPC());
 113     }
 114   }
 115 
 116 
 117   public void printThreadIDOn(Address addr, PrintStream tty) {
 118     tty.print(getThreadProxy(addr));
 119   }
 120 
 121 
 122   public void printInfoOn(Address threadAddr, PrintStream tty) {
 123   }
 124 
 125   public Address getLastSP(Address addr) {
 126     ThreadProxy t = getThreadProxy(addr);
 127     X86ThreadContext context = (X86ThreadContext) t.getContext();
 128     return context.getRegisterAsAddress(X86ThreadContext.ESP);
 129   }
 130 
 131   public ThreadProxy getThreadProxy(Address addr) {
 132     // Fetch the OSThread (for now and for simplicity, not making a
 133     // separate "OSThread" class in this package)
 134     Address osThreadAddr = osThreadField.getValue(addr);
 135     // Get the address of the thread ID from the OSThread
 136     Address tidAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 137 
 138     JVMDebugger debugger = VM.getVM().getDebugger();
 139     return debugger.getThreadForIdentifierAddress(tidAddr);
 140   }
 141 
 142 }