1 /*
   2  * Copyright (c) 2003, 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.bsd_amd64;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.debugger.amd64.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.runtime.amd64.*;
  33 import sun.jvm.hotspot.runtime.x86.*;
  34 import sun.jvm.hotspot.types.*;
  35 import sun.jvm.hotspot.utilities.*;
  36 
  37 public class BsdAMD64JavaThreadPDAccess 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 X86Frame(thread.getLastJavaSP(), fp);
  85   }
  86 
  87   public    RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
  88     return new X86RegisterMap(thread, updateMap);
  89   }
  90 
  91   public    Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
  92     ThreadProxy t = getThreadProxy(addr);
  93     AMD64ThreadContext context = (AMD64ThreadContext) t.getContext();
  94     AMD64CurrentFrameGuess guesser = new AMD64CurrentFrameGuess(context, thread);
  95     if (!guesser.run(GUESS_SCAN_RANGE)) {
  96       return null;
  97     }
  98     if (guesser.getPC() == null) {
  99       return new X86Frame(guesser.getSP(), guesser.getFP());
 100     } else {
 101       return new X86Frame(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     AMD64ThreadContext context = (AMD64ThreadContext) t.getContext();
 118     return context.getRegisterAsAddress(AMD64ThreadContext.RSP);
 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 }