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