Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Implement new method ICDISession.getSessionProcess

 2002-10-22 Alain Magloire
 
	Change the framewor to support access to gdb prompt.
	Trying to reuse as much as possible Eclipse framework.
	The session/gdb process is available via CDI.

	* src/.../core/GDBStreamsProxy.java: Removed.
	* src/.../core/GDBProcess.java: Removed.
	* src/.../core/GDBStreamMonitor.java: Removed.
	* src/.../core/SessionProcess.java: New file.
	* src/.../core/MISession.java (getMISessionProcess):
	New method, returns a "fake" Process that wraps the input/outpu
	stream of gdb.
	(getGDBProcess): was getMIProcess(), renamed.
	* src/.../core/cdi/CSession.java (getSessionProcess): New method
	return gdb process.

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/ChangeLog,v
retrieving revision 1.16
diff -u -r1.16 ChangeLog
--- ChangeLog	22 Oct 2002 15:54:25 -0000	1.16
+++ ChangeLog	22 Oct 2002 20:26:48 -0000
@@ -1,5 +1,22 @@
 2002-10-22 Alain Magloire
 
+	Change the framewor to support access to gdb prompt.
+	Trying to reuse as much as possible Eclipse framework.
+	The session/gdb process is available via CDI.
+
+	* src/.../core/GDBStreamsProxy.java: Removed.
+	* src/.../core/GDBProcess.java: Removed.
+	* src/.../core/GDBStreamMonitor.java: Removed.
+	* src/.../core/SessionProcess.java: New file.
+	* src/.../core/MISession.java (getMISessionProcess):
+	New method, returns a "fake" Process that wraps the input/outpu
+	stream of gdb.
+	(getGDBProcess): was getMIProcess(), renamed.
+	* src/.../core/cdi/CSession.java (getSessionProcess): New method
+	return gdb process.
+
+2002-10-22 Alain Magloire
+
 	* src/.../core/GDBStreamsProxy.java (write):
 	Replace the OutputStream with a Write class.
 	And clear the buffer on flush().
Index: src/org/eclipse/cdt/debug/mi/core/GDBProcess.java
===================================================================
RCS file: src/org/eclipse/cdt/debug/mi/core/GDBProcess.java
diff -N src/org/eclipse/cdt/debug/mi/core/GDBProcess.java
--- src/org/eclipse/cdt/debug/mi/core/GDBProcess.java	22 Oct 2002 04:48:02 -0000	1.2
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,133 +0,0 @@
-/*
- *(c) Copyright QNX Software Systems Ltd. 2002.
- * All Rights Reserved.
- * 
- */
-package org.eclipse.cdt.debug.mi.core;
-
-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;
-
-/**
- */
-public class GDBProcess extends PlatformObject implements IProcess {
-
-	MISession session;
-	ILaunch launch;
-	Properties props;
-	GDBStreamsProxy streams;
-	String label;
-	
-	public GDBProcess(ILaunch l, MISession s, String n) {
-		launch = l;
-		session = s;
-		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 {
-		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 label;
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.IProcess#getLaunch()
-	 */
-	public ILaunch getLaunch() {
-		return launch;
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.IProcess#getStreamsProxy()
-	 */
-	public IStreamsProxy getStreamsProxy() {
-		if (streams == null) {
-			streams = new GDBStreamsProxy(session);
-		}
-		return streams;
-	}
-
-	/**
-	 * @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);
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
-	 */
-	public boolean canTerminate() {
-		return !isTerminated();
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
-	 */
-	public boolean isTerminated() {
-		return session.isTerminated();
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.ITerminate#terminate()
-	 */
-	public void terminate() throws DebugException {
-		session.terminate();
-	}
-
-}
Index: src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java
===================================================================
RCS file: src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java
diff -N src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java
--- src/org/eclipse/cdt/debug/mi/core/GDBStreamMonitor.java	22 Oct 2002 04:48:50 -0000	1.2
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,92 +0,0 @@
-/*
- *(c) Copyright QNX Software Systems Ltd. 2002.
- * All Rights Reserved.
- * 
- */
-package org.eclipse.cdt.debug.mi.core;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.eclipse.debug.core.IStreamListener;
-import org.eclipse.debug.core.model.IStreamMonitor;
-
-/**
- */
-public class GDBStreamMonitor implements IStreamMonitor {
-
-	List listeners = Collections.synchronizedList(new LinkedList());
- 
-	StringBuffer contents = new StringBuffer();
-	InputStream stream;
-
-	public GDBStreamMonitor(InputStream s) {
-		stream = s;
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.IStreamMonitor#addListener(IStreamListener)
-	 */
-	public void addListener(IStreamListener listener) {
-		listeners.add(listener);
-	}
-
-	/**
-	 * @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 {
-			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
-		}
-	}
-
-	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: src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java
diff -N src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java
--- src/org/eclipse/cdt/debug/mi/core/GDBStreamsProxy.java	22 Oct 2002 15:54:02 -0000	1.3
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,94 +0,0 @@
-/*
- *(c) Copyright QNX Software Systems Ltd. 2002.
- * All Rights Reserved.
- * 
- */
-package org.eclipse.cdt.debug.mi.core;
-
-import java.io.IOException;
-import java.io.Writer;
-
-import org.eclipse.cdt.debug.mi.core.command.CLICommand;
-import org.eclipse.debug.core.model.IStreamMonitor;
-import org.eclipse.debug.core.model.IStreamsProxy;
-
-/**
- */
-public class GDBStreamsProxy implements IStreamsProxy {
-
-	MISession session;
-	GDBStreamMonitor miConsole;
-	GDBStreamMonitor miLog;
-	Writer out;
-	int offset;
-	
-	public GDBStreamsProxy(MISession ses) {
-		session = ses;
-	}
-	
-	/**
-	 * @see org.eclipse.debug.core.model.IStreamsProxy#getErrorStreamMonitor()
-	 */
-	public IStreamMonitor getErrorStreamMonitor() {
-		if (miLog == null) {
-			miLog = new GDBStreamMonitor(session.getMILogStream());
-			miLog.startMonitoring();
-		}
-		return miLog;
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.IStreamsProxy#getOutputStreamMonitor()
-	 */
-	public IStreamMonitor getOutputStreamMonitor() {
-		if (miConsole == null) {
-			miConsole = new GDBStreamMonitor(session.getMIConsoleStream());
-			miConsole.startMonitoring();
-		}
-		return miConsole;
-	}
-
-	/**
-	 * @see org.eclipse.debug.core.model.IStreamsProxy#write(String)
-	 */
-	public void write(String input) throws IOException {
-		if (out == null) {
-			out = new Writer() {
-				StringBuffer buf = new StringBuffer();
-				public void write(char[] cbuf, int off, int len) throws IOException {
-					for (int i = off; i < cbuf.length && len > 0; i++, len--) {
-						if (cbuf[i] == '\n') {
-							flush();
-						} else {
-							buf.append(cbuf[i]);
-						}
-					}
-				}
-				
-				public void close () {
-					buf.setLength(0);
-				}
-				
-				// Encapsulate the string sent to gdb in a fake
-				// command and post it to the TxThread.
-				public void flush() throws IOException {
-					CLICommand cmd = new CLICommand(buf.toString());
-					buf.setLength(0);
-					try {
-						session.postCommand(cmd);
-					} catch (MIException e) {
-						// throw new IOException("no session:" + e.getMessage());
-					}
-				}
-			};
-		}
-
-		if (input.length() > offset) {
-			input = input.substring(offset);
-			offset += input.length();
-		} else {
-			offset = input.length();
-		}
-		out.write(input);
-	}
-}
Index: src/org/eclipse/cdt/debug/mi/core/MIInferior.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MIInferior.java,v
retrieving revision 1.22
diff -u -r1.22 MIInferior.java
--- src/org/eclipse/cdt/debug/mi/core/MIInferior.java	13 Oct 2002 02:27:18 -0000	1.22
+++ src/org/eclipse/cdt/debug/mi/core/MIInferior.java	22 Oct 2002 20:26:48 -0000
@@ -176,7 +176,7 @@
 	}
 
 	public void interrupt() throws MIException {
-		Process gdb = session.getMIProcess();
+		Process gdb = session.getGDBProcess();
 		if (gdb instanceof Spawner) {
 			Spawner gdbSpawner = (Spawner)gdb;
 			gdbSpawner.interrupt();
Index: src/org/eclipse/cdt/debug/mi/core/MISession.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MISession.java,v
retrieving revision 1.37
diff -u -r1.37 MISession.java
--- src/org/eclipse/cdt/debug/mi/core/MISession.java	21 Oct 2002 21:38:24 -0000	1.37
+++ src/org/eclipse/cdt/debug/mi/core/MISession.java	22 Oct 2002 20:26:48 -0000
@@ -52,7 +52,8 @@
 	// hold the type of the session(post-mortem, attach etc ..)
 	int sessionType;
 
-	Process miProcess;
+	Process sessionProcess;
+	Process gdbProcess;
 	InputStream inChannel;
 	OutputStream outChannel;
 
@@ -79,6 +80,7 @@
 	MIInferior inferior;
 	
 	int cmdCount = 1;
+
 	
 	/**
 	 * Create the gdb session.
@@ -90,7 +92,7 @@
 	 */
 	public MISession(Process process, PTY pty, int timeout, int type) throws MIException {
 
-		miProcess = process;
+		gdbProcess = process;
 		inChannel = process.getInputStream();
 		outChannel = process.getOutputStream();
 
@@ -295,10 +297,21 @@
 	}
 
 	/**
-	 * Return the "gdb/mi" Process.
+	 * Return the "gdb" Process.
+	 */
+	public Process getGDBProcess() {
+		return gdbProcess;
+	}
+	
+	/**
+	 * Return a "fake" Process that will
+	 * encapsulate the call input/output of gdb.
 	 */
-	public Process getMIProcess() {
-		return miProcess;
+	public Process getSessionProcess() {
+		if (sessionProcess == null) {
+			sessionProcess = new SessionProcess(this);
+		}
+		return sessionProcess;
 	}
 
 	/**
@@ -346,8 +359,8 @@
 		// Make sure gdb is killed.
 		// FIX: the destroy() must be call before closing gdb streams
 		// on windows if the order is not follow the close() will hang.
-		if (miProcess != null) {
-			miProcess.destroy();
+		if (gdbProcess != null) {
+			gdbProcess.destroy();
 		}
 
 		// Close the input GDB prompt
Index: src/org/eclipse/cdt/debug/mi/core/SessionProcess.java
===================================================================
RCS file: src/org/eclipse/cdt/debug/mi/core/SessionProcess.java
diff -N src/org/eclipse/cdt/debug/mi/core/SessionProcess.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/debug/mi/core/SessionProcess.java	22 Oct 2002 20:26:48 -0000
@@ -0,0 +1,87 @@
+package org.eclipse.cdt.debug.mi.core;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.eclipse.cdt.debug.mi.core.command.CLICommand;
+
+/**
+ */
+public class SessionProcess extends Process {
+
+	MISession session;
+	OutputStream out;
+
+	public SessionProcess(MISession s) {
+		session = s;
+	}
+
+	/**
+	 * @see java.lang.Process#destroy()
+	 */
+	public void destroy() {
+		session.getGDBProcess().destroy();
+	}
+
+	/**
+	 * @see java.lang.Process#exitValue()
+	 */
+	public int exitValue() {
+		return session.getGDBProcess().exitValue();
+	}
+
+	/**
+	 * @see java.lang.Process#getErrorStream()
+	 */
+	public InputStream getErrorStream() {
+		return session.getMILogStream();
+	}
+
+	/**
+	 * @see java.lang.Process#getInputStream()
+	 */
+	public InputStream getInputStream() {
+		return session.getMIConsoleStream();
+	}
+
+	/**
+	 * @see java.lang.Process#getOutputStream()
+	 */
+	public OutputStream getOutputStream() {
+		if (out == null) {
+			out = new OutputStream() {
+				StringBuffer buf = new StringBuffer();
+				public void write(int b) throws IOException {
+					buf.append((char)b);
+					if (b == '\n') {
+						post();
+					}
+				}
+                                
+				// Encapsulate the string sent to gdb in a fake
+				// command and post it to the TxThread.
+				public void post() throws IOException {
+					// Throw away the newline.
+					String str = buf.toString().trim();
+					CLICommand cmd = new CLICommand(str);
+					buf.setLength(0);
+					try {
+						session.postCommand(cmd);
+					} catch (MIException e) {
+						throw new IOException(e.getMessage());
+					}
+				}
+			};
+		}
+		return out;
+	}
+
+	/**
+	 * @see java.lang.Process#waitFor()
+	 */
+	public int waitFor() throws InterruptedException {
+		return session.getGDBProcess().waitFor();
+	}
+
+}
Index: src/org/eclipse/cdt/debug/mi/core/cdi/CSession.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/cdi/CSession.java,v
retrieving revision 1.20
diff -u -r1.20 CSession.java
--- src/org/eclipse/cdt/debug/mi/core/cdi/CSession.java	9 Oct 2002 14:08:42 -0000	1.20
+++ src/org/eclipse/cdt/debug/mi/core/cdi/CSession.java	22 Oct 2002 20:26:48 -0000
@@ -208,4 +208,11 @@
 		}
 	}
 
+	/**
+	 * @see org.eclipse.cdt.debug.core.cdi.ICDISession#getSessionProcess()
+	 */
+	public Process getSessionProcess() throws CDIException {
+		return session.getSessionProcess();
+	}
+
 }
Index: src/org/eclipse/cdt/debug/mi/core/cdi/Configuration.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/cdi/Configuration.java,v
retrieving revision 1.7
diff -u -r1.7 Configuration.java
--- src/org/eclipse/cdt/debug/mi/core/cdi/Configuration.java	2 Oct 2002 04:40:20 -0000	1.7
+++ src/org/eclipse/cdt/debug/mi/core/cdi/Configuration.java	22 Oct 2002 20:26:48 -0000
@@ -113,7 +113,7 @@
 			os = System.getProperty("os.name", "");
 		} catch (SecurityException e) {
 		}
-		Process gdb = miSession.getMIProcess();
+		Process gdb = miSession.getGDBProcess();
 		if (gdb instanceof Spawner) {
 			// If we attached sending a control-c, seems to work.
 			if (fAttached) {



Back to the top