From 82e8f083530a781b36b6f1940c78e8d50fa8f49f Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Fri, 26 Sep 2003 16:58:24 +0000 Subject: [PATCH] Make sure gdb is ready to receive commnad before creating the MISession. --- .../eclipse/cdt/debug/mi/core/MIPlugin.java | 84 ++++++++++++++++++- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MIPlugin.java b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MIPlugin.java index 85cd7564747..425f28af4eb 100644 --- a/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MIPlugin.java +++ b/debug/org.eclipse.cdt.debug.mi.core/src/org/eclipse/cdt/debug/mi/core/MIPlugin.java @@ -4,8 +4,12 @@ */ package org.eclipse.cdt.debug.mi.core; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.text.MessageFormat; import org.eclipse.cdt.debug.core.cdi.ICDISession; @@ -131,7 +135,8 @@ public class MIPlugin extends Plugin { } } - Process pgdb = ProcessFactory.getFactory().exec(args); + Process pgdb = getGDBProcess(args, program); + MISession session; try { session = createMISession(pgdb, pty, MISession.PROGRAM); @@ -181,7 +186,7 @@ public class MIPlugin extends Plugin { } else { args = new String[] {gdb, "--cd="+cwd.getAbsolutePath(), "--command="+gdbinit, "--quiet", "-nw", "-i", "mi1", "-c", core.getAbsolutePath(), program.getAbsolutePath()}; } - Process pgdb = ProcessFactory.getFactory().exec(args); + Process pgdb = getGDBProcess(args, program); MISession session; try { session = createMISession(pgdb, null, MISession.CORE); @@ -214,7 +219,7 @@ public class MIPlugin extends Plugin { } else { args = new String[] {gdb, "--cd="+cwd.getAbsolutePath(), "--command="+gdbinit, "--quiet", "-nw", "-i", "mi1", program.getAbsolutePath()}; } - Process pgdb = ProcessFactory.getFactory().exec(args); + Process pgdb = getGDBProcess(args, program); MISession session; try { session = createMISession(pgdb, null, MISession.ATTACH); @@ -283,6 +288,79 @@ public class MIPlugin extends Plugin { } } + /** + * Retrieve the session timeout. + * + * Allow at least one second per megabyte as a minimum on the timeout + * (note one second is an arbitrary choice based on swapping performance). + * This is required for loading the symbols from very large programs. + */ + private int getAdjustedTimeout(File program) { + Preferences prefs = plugin.getPluginPreferences(); + int timeout = prefs.getInt(IMIConstants.PREF_REQUEST_TIMEOUT); //milliseconds + if(program != null && program.exists()) { + long programSize = program.length(); + int minimumTimeout = (int)(programSize / 1000L); + if(timeout < minimumTimeout) { + //debugLog("Adjusting timeout from " + timeout + "ms to " + minimumTimeout + "ms"); + timeout = minimumTimeout; + } + } + return timeout; + } + + /** + * Do some basic synchronisation, gdb make take some time to load + * for whatever reasons. + * @param args + * @param program + * @return Process + * @throws IOException + */ + protected Process getGDBProcess(String[] args, File program) throws IOException { + final Process pgdb = ProcessFactory.getFactory().exec(args); + Thread syncStartup = new Thread("GDB Start") { + public void run() { + try { + String line; + InputStream stream = pgdb.getInputStream(); + Reader r = new InputStreamReader(stream); + BufferedReader reader = new BufferedReader(r); + while ((line = reader.readLine()) != null) { + line = line.trim(); + //System.out.println("GDB " + line); + if (line.startsWith("(gdb)")) { + break; + } + } + } catch (Exception e) { + // Do nothing + } + synchronized (pgdb) { + pgdb.notifyAll(); + } + } + }; + syncStartup.start(); + + synchronized (pgdb) { + int timeout = getAdjustedTimeout(program); + while (syncStartup.isAlive()) { + try { + pgdb.wait(timeout); + break; + } catch (InterruptedException e) { + } + } + } + try { + syncStartup.interrupt(); + syncStartup.join(1000); + } catch (InterruptedException e) { + } + return pgdb; + } + /* (non-Javadoc) * @see org.eclipse.core.runtime.Plugin#startup() */