1 /*
   2  * Copyright (c) 2002, 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_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 
  36 public class BsdX86JavaThreadPDAccess implements JavaThreadPDAccess {
  37   private static AddressField  lastJavaFPField;
  38   private static AddressField  osThreadField;
  39 
  40   // Field from OSThread
  41   private static CIntegerField osThreadThreadIDField;
  42 
  43   // This is currently unneeded but is being kept in case we change
  44   // the currentFrameGuess algorithm
  45   private static final long GUESS_SCAN_RANGE = 128 * 1024;
  46 
  47   static {
  48     VM.registerVMInitializedObserver(new Observer() {
  49         public void update(Observable o, Object data) {
  50           initialize(VM.getVM().getTypeDataBase());
  51         }
  52       });
  53   }
  54 
  55   private static synchronized void initialize(TypeDataBase db) {
  56     Type type = db.lookupType("JavaThread");
  57     osThreadField           = type.getAddressField("_osthread");
  58 
  59     Type anchorType = db.lookupType("JavaFrameAnchor");
  60     lastJavaFPField         = anchorType.getAddressField("_last_Java_fp");
  61 
  62     Type osThreadType = db.lookupType("OSThread");
  63     osThreadThreadIDField   = osThreadType.getCIntegerField("_thread_id");
  64   }
  65 
  66   public    Address getLastJavaFP(Address addr) {
  67     return lastJavaFPField.getValue(addr.addOffsetTo(sun.jvm.hotspot.runtime.JavaThread.getAnchorField().getOffset()));
  68   }
  69 
  70   public    Address getLastJavaPC(Address addr) {
  71     return null;
  72   }
  73 
  74   public    Address getBaseOfStackPointer(Address addr) {
  75     return null;
  76   }
  77 
  78   public    Frame getLastFramePD(JavaThread thread, Address addr) {
  79     Address fp = thread.getLastJavaFP();
  80     if (fp == null) {
  81       return null; // no information
  82     }
  83     return new X86Frame(thread.getLastJavaSP(), fp);
  84   }
  85 
  86   public    RegisterMap newRegisterMap(JavaThread thread, boolean updateMap) {
  87     return new X86RegisterMap(thread, updateMap);
  88   }
  89 
  90   public    Frame getCurrentFrameGuess(JavaThread thread, Address addr) {
  91     ThreadProxy t = getThreadProxy(addr);
  92     X86ThreadContext context = (X86ThreadContext) t.getContext();
  93     X86CurrentFrameGuess guesser = new X86CurrentFrameGuess(context, thread);
  94     if (!guesser.run(GUESS_SCAN_RANGE)) {
  95       return null;
  96     }
  97     if (guesser.getPC() == null) {
  98       return new X86Frame(guesser.getSP(), guesser.getFP());
  99     } else {
 100       return new X86Frame(guesser.getSP(), guesser.getFP(), guesser.getPC());
 101     }
 102   }
 103 
 104   public    void printThreadIDOn(Address addr, PrintStream tty) {
 105     tty.print(getThreadProxy(addr));
 106   }
 107 
 108   public    void printInfoOn(Address threadAddr, PrintStream tty) {
 109     tty.print("Thread id: ");
 110     printThreadIDOn(threadAddr, tty);
 111 //    tty.println("\nPostJavaState: " + getPostJavaState(threadAddr));
 112   }
 113 
 114   public    Address getLastSP(Address addr) {
 115     ThreadProxy t = getThreadProxy(addr);
 116     X86ThreadContext context = (X86ThreadContext) t.getContext();
 117     return context.getRegisterAsAddress(X86ThreadContext.ESP);
 118   }
 119 
 120   public    ThreadProxy getThreadProxy(Address addr) {
 121     // Addr is the address of the JavaThread.
 122     // Fetch the OSThread (for now and for simplicity, not making a
 123     // separate "OSThread" class in this package)
 124     Address osThreadAddr = osThreadField.getValue(addr);
 125     // Get the address of the _thread_id from the OSThread
 126     Address threadIdAddr = osThreadAddr.addOffsetTo(osThreadThreadIDField.getOffset());
 127 
 128     JVMDebugger debugger = VM.getVM().getDebugger();
 129     return debugger.getThreadForIdentifierAddress(threadIdAddr);
 130   }
 131 }