1 /*
   2  * Copyright (c) 2005, 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.win32_amd64;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.debugger.win32.*;
  31 import sun.jvm.hotspot.debugger.amd64.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.runtime.amd64.*;
  34 import sun.jvm.hotspot.runtime.x86.*;
  35 import sun.jvm.hotspot.types.*;
  36 import sun.jvm.hotspot.utilities.*;
  37 
  38 /** This class is only public to allow using the VMObjectFactory to
  39     instantiate these.
  40 */
  41 
  42 public class Win32AMD64JavaThreadPDAccess implements JavaThreadPDAccess {
  43   private static AddressField  lastJavaFPField;
  44   private static AddressField  osThreadField;
  45 
  46   // Field from OSThread
  47   private static Field         osThreadThreadHandleField;
  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     Type anchorType = db.lookupType("JavaFrameAnchor");
  64     lastJavaFPField         = anchorType.getAddressField("_last_Java_fp");
  65     osThreadField           = type.getAddressField("_osthread");
  66 
  67     type = db.lookupType("OSThread");
  68     osThreadThreadHandleField = type.getField("_thread_handle");
  69   }
  70 
  71   public Address getLastJavaFP(Address addr) {
  72     return lastJavaFPField.getValue(addr.addOffsetTo(sun.jvm.hotspot.runtime.JavaThread.getAnchorField().getOffset()));
  73   }
  74 
  75   public Address getLastJavaPC(Address addr) {
  76     return null;
  77   }
  78 
  79   public Address getBaseOfStackPointer(Address addr) {
  80     return null;
  81   }
  82 
  83   public Frame getLastFramePD(JavaThread thread, Address addr) {
  84     Address fp = thread.getLastJavaFP();
  85     if (fp == null) {
  86       return null; // no information
  87     }
  88     Address pc =  thread.getLastJavaPC();
  89     if ( pc != null ) {
  90       return new X86Frame(thread.getLastJavaSP(), fp, pc);
  91     } else {
  92       return new X86Frame(thread.getLastJavaSP(), fp);
  93     }
  94   }
  95 
  96   public RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
  97     return new X86RegisterMap(thread, updateMap);
  98   }
  99 
 100   public Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
 101     ThreadProxy t = getThreadProxy(addr);
 102     AMD64ThreadContext context = (AMD64ThreadContext) t.getContext();
 103     AMD64CurrentFrameGuess guesser = new AMD64CurrentFrameGuess(context, thread);
 104     if (!guesser.run(GUESS_SCAN_RANGE)) {
 105       return null;
 106     }
 107     if (guesser.getPC() == null) {
 108       return new X86Frame(guesser.getSP(), guesser.getFP());
 109     } else {
 110       return new X86Frame(guesser.getSP(), guesser.getFP(), guesser.getPC());
 111     }
 112   }
 113 
 114   public void printThreadIDOn(Address addr, PrintStream tty) {
 115     tty.print(getThreadProxy(addr));
 116   }
 117 
 118   public void printInfoOn(Address threadAddr, PrintStream tty) {
 119   }
 120 
 121   public Address getLastSP(Address addr) {
 122     ThreadProxy t = getThreadProxy(addr);
 123     AMD64ThreadContext context = (AMD64ThreadContext) t.getContext();
 124     return context.getRegisterAsAddress(AMD64ThreadContext.RSP);
 125   }
 126 
 127   public ThreadProxy getThreadProxy(Address addr) {
 128     // Addr is the address of the JavaThread.
 129     // Fetch the OSThread (for now and for simplicity, not making a
 130     // separate "OSThread" class in this package)
 131     Address osThreadAddr = osThreadField.getValue(addr);
 132     // Get the address of the HANDLE within the OSThread
 133     Address threadHandleAddr =
 134       osThreadAddr.addOffsetTo(osThreadThreadHandleField.getOffset());
 135     JVMDebugger debugger = VM.getVM().getDebugger();
 136     return debugger.getThreadForIdentifierAddress(threadHandleAddr);
 137   }
 138 }