001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.debug.jpda;
038:
039: import com.sun.jdi.*;
040: import edu.rice.cs.drjava.model.debug.DebugThreadData;
041:
042: /**
043: * Class for keeping track of the currently running threads.
044: * @version $Id: DebugThreadData.java 3901 2006-06-30 05:28:11Z rcartwright $
045: */
046: public class JPDAThreadData extends DebugThreadData {
047: private final ThreadReference _thread;
048:
049: /**
050: * Object for keeping track of a thread in the debuggee JVM.
051: * @param thread JPDA's reference to the thread
052: */
053: public JPDAThreadData(ThreadReference thread) {
054: super (threadName(thread), threadStatus(thread), thread
055: .uniqueID());
056: _thread = thread;
057: }
058:
059: private static String threadName(ThreadReference thread) {
060: try {
061: return thread.name();
062: } catch (VMDisconnectedException e) {
063: return "";
064: }
065: }
066:
067: private static String threadStatus(ThreadReference thread) {
068: String status = "(unknown)";
069: try {
070: switch (thread.status()) {
071: case ThreadReference.THREAD_STATUS_MONITOR:
072: status = "MONITOR";
073: break;
074: case ThreadReference.THREAD_STATUS_NOT_STARTED:
075: status = "NOT STARTED";
076: break;
077: case ThreadReference.THREAD_STATUS_RUNNING:
078: status = "RUNNING";
079: break;
080: case ThreadReference.THREAD_STATUS_SLEEPING:
081: status = "SLEEPING";
082: break;
083: case ThreadReference.THREAD_STATUS_UNKNOWN:
084: status = "UNKNOWN";
085: break;
086: case ThreadReference.THREAD_STATUS_WAIT:
087: status = "WAIT";
088: break;
089: case ThreadReference.THREAD_STATUS_ZOMBIE:
090: status = "ZOMBIE";
091: break;
092: }
093: } catch (VMDisconnectedException e) {
094: // status will be set to unknown
095: }
096: if (safeIsSuspended(thread) && status.equals("RUNNING")) {
097: status = "SUSPENDED";
098: }
099: return status;
100: }
101:
102: /**
103: * Tells whether or not the thread is suspended.
104: * @return true iff the thread is suspended
105: */
106: public boolean isSuspended() {
107: return safeIsSuspended(_thread);
108: }
109:
110: /** Invoke {@code t.isSuspended()} under the protection of a try-catch block */
111: private static boolean safeIsSuspended(ThreadReference t) {
112: try {
113: return t.isSuspended();
114: } catch (ObjectCollectedException e) {
115: return false;
116: } catch (VMDisconnectedException e) {
117: return false;
118: }
119: }
120:
121: }
|