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.debugger.windbg.x86;
  26 
  27 import sun.jvm.hotspot.debugger.*;
  28 import sun.jvm.hotspot.debugger.x86.*;
  29 import sun.jvm.hotspot.debugger.windbg.*;
  30 
  31 class WindbgX86Thread implements ThreadProxy {
  32   private WindbgDebugger debugger;
  33   private long           sysId;
  34   private boolean        gotID;
  35   private long           id;
  36 
  37   /** The address argument must be the address of the HANDLE of the
  38       desired thread in the target process. */
  39   WindbgX86Thread(WindbgDebugger debugger, Address addr) {
  40     this.debugger = debugger;
  41     // FIXME: size of data fetched here should be configurable.
  42     // However, making it so would produce a dependency on the "types"
  43     // package from the debugger package, which is not desired.
  44 
  45     // another hack here is that we use sys thread id instead of handle.
  46     // windbg can't get details based on handles it seems.
  47     // I assume that osThread_win32 thread struct has _thread_id (which
  48     // sys thread id) just after handle field.
  49 
  50     this.sysId   = (int) addr.addOffsetTo(debugger.getAddressSize()).getCIntegerAt(0, 4, true);
  51     gotID = false;
  52   }
  53 
  54   WindbgX86Thread(WindbgDebugger debugger, long sysId) {
  55     this.debugger = debugger;
  56     this.sysId    = sysId;
  57     gotID         = false;
  58   }
  59 
  60   public ThreadContext getContext() throws IllegalThreadStateException {
  61     long[] data = debugger.getThreadIntegerRegisterSet(getThreadID());
  62     WindbgX86ThreadContext context = new WindbgX86ThreadContext(debugger);
  63     for (int i = 0; i < data.length; i++) {
  64       context.setRegister(i, data[i]);
  65     }
  66     return context;
  67   }
  68 
  69   public boolean canSetContext() throws DebuggerException {
  70     return false;
  71   }
  72 
  73   public void setContext(ThreadContext thrCtx)
  74     throws IllegalThreadStateException, DebuggerException {
  75     throw new DebuggerException("Unimplemented");
  76   }
  77 
  78   public boolean equals(Object obj) {
  79     if ((obj == null) || !(obj instanceof WindbgX86Thread)) {
  80       return false;
  81     }
  82 
  83     return (((WindbgX86Thread) obj).getThreadID() == getThreadID());
  84   }
  85 
  86   public int hashCode() {
  87     return (int) getThreadID();
  88   }
  89 
  90   public String toString() {
  91     return Long.toString(getThreadID());
  92   }
  93 
  94   /** Retrieves the thread ID of this thread by examining the Thread
  95       Information Block. */
  96   private long getThreadID() {
  97     if (!gotID) {
  98        id = debugger.getThreadIdFromSysId(sysId);
  99     }
 100 
 101     return id;
 102   }
 103 }