Beispiel #1
0
  /**
   * Starts a process by executing the following command: gdb -q -nw -i <mi_version>(extracted from
   * the command factory) -tty<pty_name> (if <code>usePTY</code> is <code>true</code>) extraArgs
   * program (if <code>program</code> is not <code>null</code>)
   *
   * @param sessionType the type of debugging session: <code>MISession.PROGRAM</code>, <code>
   *     MISession.ATTACH</code> or <code>MISession.CORE</code>
   * @param gdb the name of the gdb file
   * @param factory the command set supported by gdb
   * @param program a program to debug or <code>null</code>
   * @param extraArgs arguments to pass to gdb
   * @param usePty whether to use pty or not
   * @param monitor a progress monitor
   * @return an instance of <code>ICDISession</code>
   * @throws IOException
   * @throws MIException
   */
  public Session createSession(
      int sessionType,
      String gdb,
      CommandFactory factory,
      File program,
      String[] extraArgs,
      boolean usePty,
      IProgressMonitor monitor)
      throws IOException, MIException {
    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }

    if (gdb == null || gdb.length() == 0) {
      gdb = GDB;
    }

    IMITTY pty = null;

    if (usePty) {
      try {
        PTY pseudo = new PTY();
        pseudo.validateSlaveName();
        pty = new MITTYAdapter(pseudo);
      } catch (IOException e) {
        // Should we not print/log this ?
      }
    }

    ArrayList argList = new ArrayList(extraArgs.length + 8);
    argList.add(gdb);
    argList.add("-q"); // $NON-NLS-1$
    argList.add("-nw"); // $NON-NLS-1$
    argList.add("-i"); // $NON-NLS-1$
    argList.add(factory.getMIVersion());
    if (pty != null) {
      argList.add("-tty"); // $NON-NLS-1$
      argList.add(pty.getSlaveName());
    }
    argList.addAll(Arrays.asList(extraArgs));
    if (program != null) {
      argList.add(program.getAbsolutePath());
    }
    String[] args = (String[]) argList.toArray(new String[argList.size()]);
    int launchTimeout =
        MIPlugin.getDefault()
            .getPluginPreferences()
            .getInt(IMIConstants.PREF_REQUEST_LAUNCH_TIMEOUT);

    MISession miSession = null;
    MIProcess pgdb = null;
    boolean failed = false;
    try {
      pgdb = factory.createMIProcess(args, launchTimeout, monitor);

      if (MIPlugin.DEBUG) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < args.length; ++i) {
          sb.append(args[i]);
          sb.append(' ');
        }
        MIPlugin.getDefault().debugLog(sb.toString());
      }

      miSession = createMISession0(sessionType, pgdb, factory, pty, getCommandTimeout());
    } catch (MIException e) {
      failed = true;
      throw e;
    } catch (IOException e) {
      failed = true;
      throw e;
    } finally {
      if (failed) {
        // Kill gdb
        if (pgdb != null) pgdb.destroy();
        // Shutdown the pty console.
        if (pty != null) {
          try {
            OutputStream out = pty.getOutputStream();
            if (out != null) {
              out.close();
            }
            InputStream in = pty.getInputStream();
            if (in != null) {
              in.close();
            }
          } catch (IOException e) {
          }
        }
      }
    }

    return new Session(miSession);
  }
Beispiel #2
0
  /**
   * Method createCSession; lauch gdb in mi mode for local debugging
   *
   * @param program
   * @return ICDISession
   * @throws IOException
   * @deprecated use <code>createSession</code>
   */
  @Deprecated
  public Session createCSession(
      String gdb,
      String miVersion,
      File program,
      File cwd,
      String gdbinit,
      IMITTY pty,
      IProgressMonitor monitor)
      throws IOException, MIException {
    if (gdb == null || gdb.length() == 0) {
      gdb = GDB;
    }

    String commandFile =
        (gdbinit != null && gdbinit.length() > 0)
            ? "--command=" + gdbinit
            : "--nx"; //$NON-NLS-1$ //$NON-NLS-2$

    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }

    String[] args;
    if (pty != null) {
      if (program == null) {
        args =
            new String[] {
              gdb,
              "--cd=" + cwd.getAbsolutePath(),
              commandFile,
              "-q",
              "-nw",
              "-tty",
              pty.getSlaveName(),
              "-i",
              miVersion
            }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
      } else {
        args =
            new String[] {
              gdb,
              "--cd=" + cwd.getAbsolutePath(),
              commandFile,
              "-q",
              "-nw",
              "-tty",
              pty.getSlaveName(),
              "-i",
              miVersion,
              program.getAbsolutePath()
            }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
      }
    } else {
      if (program == null) {
        args =
            new String[] {
              gdb, "--cd=" + cwd.getAbsolutePath(), commandFile, "-q", "-nw", "-i", miVersion
            }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      } else {
        args =
            new String[] {
              gdb,
              "--cd=" + cwd.getAbsolutePath(),
              commandFile,
              "-q",
              "-nw",
              "-i",
              miVersion,
              program.getAbsolutePath()
            }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
    }

    int launchTimeout =
        MIPlugin.getDefault()
            .getPluginPreferences()
            .getInt(IMIConstants.PREF_REQUEST_LAUNCH_TIMEOUT);
    MIProcess pgdb = new MIProcessAdapter(args, launchTimeout, monitor);

    if (MIPlugin.DEBUG) {
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < args.length; ++i) {
        sb.append(args[i]);
        sb.append(' ');
      }
      MIPlugin.getDefault().debugLog(sb.toString());
    }

    MISession session;
    try {
      session = createMISession(pgdb, pty, MISession.PROGRAM, miVersion, monitor);
    } catch (MIException e) {
      pgdb.destroy();
      throw e;
    }
    // Try to detect if we have been attach/connected via "target remote localhost:port"
    // or "attach" and set the state to be suspended.
    try {
      CommandFactory factory = session.getCommandFactory();
      MIStackListFrames frames = factory.createMIStackListFrames();
      session.postCommand(frames);
      MIInfo info = frames.getMIInfo();
      if (info == null) {
        pgdb.destroy();
        throw new MIException(getResourceString("src.common.No_answer")); // $NON-NLS-1$
      }
      // @@@ We have to manually set the suspended state since we have some stackframes
      session.getMIInferior().setSuspended();
      session.getMIInferior().update();
    } catch (MIException e) {
      // If an exception is thrown that means ok
      // we did not attach/connect to any target.
    }
    return new Session(session, false);
  }