1 /*
   2  * Copyright (c) 2002, 2013, 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.bsd;
  26 
  27 import sun.jvm.hotspot.debugger.*;
  28 
  29 class BsdThread implements ThreadProxy {
  30     private BsdDebugger debugger;
  31     private int         thread_id;
  32     private long        unique_thread_id;
  33 
  34     /** The address argument must be the address of the _thread_id in the
  35         OSThread. It's value is result ::gettid() call. */
  36     BsdThread(BsdDebugger debugger, Address threadIdAddr, Address uniqueThreadIdAddr) {
  37         this.debugger = debugger;
  38         // FIXME: size of data fetched here should be configurable.
  39         // However, making it so would produce a dependency on the "types"
  40         // package from the debugger package, which is not desired.
  41         this.thread_id = (int) threadIdAddr.getCIntegerAt(0, 4, true);
  42         this.unique_thread_id = uniqueThreadIdAddr.getCIntegerAt(0, 8, true);
  43     }
  44 
  45     BsdThread(BsdDebugger debugger, long id) {
  46         this.debugger = debugger;
  47         // use unique_thread_id to identify thread
  48         this.unique_thread_id = id;
  49     }
  50 
  51     public boolean equals(Object obj) {
  52         if ((obj == null) || !(obj instanceof BsdThread)) {
  53             return false;
  54         }
  55 
  56         return (((BsdThread) obj).unique_thread_id == unique_thread_id);
  57     }
  58 
  59     public int hashCode() {
  60         return thread_id;
  61     }
  62 
  63     public String toString() {
  64         return Integer.toString(thread_id);
  65     }
  66 
  67     public ThreadContext getContext() throws IllegalThreadStateException {
  68         long[] data = debugger.getThreadIntegerRegisterSet(unique_thread_id);
  69         ThreadContext context = BsdThreadContextFactory.createThreadContext(debugger);
  70         for (int i = 0; i < data.length; i++) {
  71             context.setRegister(i, data[i]);
  72         }
  73         return context;
  74     }
  75 
  76     public boolean canSetContext() throws DebuggerException {
  77         return false;
  78     }
  79 
  80     public void setContext(ThreadContext context)
  81       throws IllegalThreadStateException, DebuggerException {
  82         throw new DebuggerException("Unimplemented");
  83     }
  84 
  85     /** this is not interface function, used in core file to get unique thread id on Macosx*/
  86     public long getUniqueThreadId() {
  87         return unique_thread_id;
  88     }
  89 }