Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Monitor threads for the gdb stream

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/ChangeLog,v
retrieving revision 1.14
diff -u -r1.14 ChangeLog
--- ChangeLog	21 Oct 2002 21:38:52 -0000	1.14
+++ ChangeLog	22 Oct 2002 04:44:24 -0000
@@ -1,5 +1,22 @@
 2002-10-21 Alain Magloire
 
+	* src/.../core/GDBProcess.java (getExitValue): Catch
+	IllegalThreadStateException.
+	(getAttribute): Only create Properties, when call.
+	(setAttribute): Only create Properties, when call.
+	(getAdapter): Implemented.
+	(canTerminate): true only of the process is alive.
+	(GDBProcess): Takes one more argument the name.
+	* src/.../core/GDBStreamMonitor.java (fireStreamAppend): New method.
+	Use a synchronized LinkedList for the listeners.
+	(read): new Method.
+	(startMonitoring): New method, start a thread in the background
+	to monitor the input.
+	* src/.../core/GDBStreamsProxy.java (getErrorStream): Start the monitor thread.
+	(getOutputStream): Start the monitor thread.
+
+2002-10-21 Alain Magloire
+
 	Framework for having a gdb console.  The idea is to reuse
 	the eclipse console and save a lot of work.  We "adapt"
 	the gdb Process to what eclipse debug LaunchView wants(IProcess).
Index: src/org/eclipse/cdt/debug/mi/core/GDBProcess.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/GDBProcess.java,v
retrieving revision 1.1
diff -u -r1.1 GDBProcess.java
--- src/org/eclipse/cdt/debug/mi/core/GDBProcess.java	21 Oct 2002 21:37:37 -0000	1.1
+++ src/org/eclipse/cdt/debug/mi/core/GDBProcess.java	22 Oct 2002 04:44:25 -0000
@@ -7,9 +7,12 @@
 
 import java.util.Properties;
 
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.PlatformObject;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.ILaunch;
+import org.eclipse.debug.core.model.IDebugTarget;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IStreamsProxy;
 
@@ -21,32 +24,52 @@
 	ILaunch launch;
 	Properties props;
 	GDBStreamsProxy streams;
+	String label;
 	
-	public GDBProcess(ILaunch l, MISession s) {
+	public GDBProcess(ILaunch l, MISession s, String n) {
 		launch = l;
 		session = s;
-		props = new Properties();
+		label = n;
 	}
 	
 	/**
 	 * @see org.eclipse.debug.core.model.IProcess#getAttribute(String)
 	 */
 	public String getAttribute(String key) {
+		if (props == null) {
+			return null;
+		}
 		return props.getProperty(key);
 	}
 
 	/**
+	 * @see org.eclipse.debug.core.model.IProcess#setAttribute(String, String)
+	 */
+	public void setAttribute(String key, String value) {
+		if (props == null) {
+			props = new Properties();
+		}
+		props.setProperty(key, value);
+	}
+
+	/**
 	 * @see org.eclipse.debug.core.model.IProcess#getExitValue()
 	 */
 	public int getExitValue() throws DebugException {
-		return session.getMIProcess().exitValue();
+		try {
+			return session.getMIProcess().exitValue();
+		} catch (IllegalThreadStateException e) {
+			IStatus status = new Status(IStatus.ERROR,
+				MIPlugin.getUniqueIdentifier(), 1, "process not terminated", e);
+			throw new DebugException(status);
+		}
 	}
 
 	/**
 	 * @see org.eclipse.debug.core.model.IProcess#getLabel()
 	 */
 	public String getLabel() {
-		return "GDB MI Debugger";
+		return label;
 	}
 
 	/**
@@ -67,16 +90,22 @@
 	}
 
 	/**
-	 * @see org.eclipse.debug.core.model.IProcess#setAttribute(String, String)
-	 */
-	public void setAttribute(String key, String value) {
-		props.setProperty(key, value);
-	}
-
-	/**
 	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
 	 */
 	public Object getAdapter(Class adapter) {
+		if (adapter.equals(IProcess.class)) {
+			return this;
+		}
+		if (adapter.equals(IDebugTarget.class)) {
+			ILaunch launch = getLaunch();
+			IDebugTarget[] targets = launch.getDebugTargets();
+			for (int i = 0; i < targets.length; i++) {
+				if (this.equals(targets[i].getProcess())) {
+					return targets[i];
+				}
+			}
+			return null;
+		}
 		return super.getAdapter(adapter);
 	}
 
@@ -84,7 +113,7 @@
 	 * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
 	 */
 	public boolean canTerminate() {
-		return true;
+		return !isTerminated();
 	}
 
 	/**
Index: src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java,v
retrieving revision 1.1
diff -u -r1.1 GDBStreamMonitor.java
--- src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java	21 Oct 2002 21:37:41 -0000	1.1
+++ src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java	22 Oct 2002 04:44:25 -0000
@@ -7,7 +7,8 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;
 
 import org.eclipse.debug.core.IStreamListener;
@@ -17,13 +18,12 @@
  */
 public class GDBStreamMonitor implements IStreamMonitor {
 
-	List listeners;
-	StringBuffer buffer;
+	List listeners = Collections.synchronizedList(new LinkedList());
+ 
+	StringBuffer contents = new StringBuffer();
 	InputStream stream;
 
 	public GDBStreamMonitor(InputStream s) {
-		listeners = new ArrayList();
-		buffer = new StringBuffer();
 		stream = s;
 	}
 
@@ -35,26 +35,58 @@
 	}
 
 	/**
+	 * @see org.eclipse.debug.core.model.IStreamMonitor#removeListener(IStreamListener)
+	 */
+	public void removeListener(IStreamListener listener) {
+		listeners.remove(listener);
+	}
+
+	/**
+	 * Notifies the listeners.
+	 */
+	private void fireStreamAppended(String text) {
+		IStreamListener[] array = (IStreamListener[])listeners.toArray(new IStreamListener[0]);
+		for (int i = 0; i < array.length; i++) {
+			array[i].streamAppended(text, this);
+		}
+	}
+
+	/**
 	 * @see org.eclipse.debug.core.model.IStreamMonitor#getContents()
 	 */
 	public String getContents() {
+		return contents.toString();
+	}
+
+	/**
+	 * Continually reads from the stream.
+	 */
+	void read() {
+		byte[] bytes = new byte[1024];
+		int count = 0;
 		try {
-			int count = stream.available();
-			byte[] bytes = new byte[count];
-			count =  stream.read(bytes);
-			if (count > 0) {
-				buffer.append(bytes);
+			while ((count = stream.read(bytes)) >= 0) {
+				if (count > 0) {
+					String text = new String(bytes, 0, count);
+					contents.append(text);
+					fireStreamAppended(text);
+				}
 			}
+			stream.close();
 		} catch (IOException e) {
+			// e.printStackTrace();
+		} catch (NullPointerException e) {
+			// killing the stream monitor while reading can cause an NPE
 		}
-		return buffer.toString();
 	}
 
-	/**
-	 * @see org.eclipse.debug.core.model.IStreamMonitor#removeListener(IStreamListener)
-	 */
-	public void removeListener(IStreamListener listener) {
-		listeners.remove(listener);
+	public void startMonitoring() {
+		Thread thread = new Thread(new Runnable() {
+			public void run() {
+				read();
+			}
+		}, "GDB stream Monitor");
+		thread.setDaemon(true);
+		thread.start();
 	}
-
 }
Index: src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java,v
retrieving revision 1.1
diff -u -r1.1 GDBStreamsProxy.java
--- src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java	21 Oct 2002 21:37:32 -0000	1.1
+++ src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java	22 Oct 2002 04:44:24 -0000
@@ -31,6 +31,7 @@
 	public IStreamMonitor getErrorStreamMonitor() {
 		if (miLog == null) {
 			miLog = new GDBStreamMonitor(session.getMILogStream());
+			miLog.startMonitoring();
 		}
 		return miLog;
 	}
@@ -41,6 +42,7 @@
 	public IStreamMonitor getOutputStreamMonitor() {
 		if (miConsole == null) {
 			miConsole = new GDBStreamMonitor(session.getMIConsoleStream());
+			miConsole.startMonitoring();
 		}
 		return miConsole;
 	}



Back to the top