Search in sources :

Example 1 with Init

use of org.exist.debuggee.dbgp.packets.Init in project exist by eXist-db.

the class DebuggeeImpl method joint.

public boolean joint(CompiledXQuery compiledXQuery) {
    synchronized (this) {
        IoSession session = connection.connect();
        if (session == null)
            return false;
        // link debugger session & script
        DebuggeeJointImpl joint = new DebuggeeJointImpl();
        joint.setCompiledScript(compiledXQuery);
        XQueryContext context = compiledXQuery.getContext();
        context.setDebuggeeJoint(joint);
        String idesession = "";
        if (context.isVarDeclared(Debuggee.SESSION)) {
            try {
                Variable var = context.resolveVariable(Debuggee.SESSION);
                idesession = var.getValue().toString();
            } catch (XPathException e) {
            }
        }
        String idekey = "";
        if (context.isVarDeclared(Debuggee.IDEKEY)) {
            try {
                Variable var = context.resolveVariable(Debuggee.IDEKEY);
                idekey = var.getValue().toString();
            } catch (XPathException e) {
            }
        }
        joint.continuation(new Init(session, idesession, idekey));
        return true;
    }
}
Also used : Init(org.exist.debuggee.dbgp.packets.Init) Variable(org.exist.xquery.Variable) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) IoSession(org.apache.mina.core.session.IoSession)

Example 2 with Init

use of org.exist.debuggee.dbgp.packets.Init in project exist by eXist-db.

the class DebuggeeJointImpl method expressionStart.

/* (non-Javadoc)
	 * @see org.exist.debuggee.DebuggeeJoint#expressionStart(org.exist.xquery.Expression)
	 */
public void expressionStart(Expression expr) throws TerminatedException {
    if (LOG.isDebugEnabled())
        LOG.debug("" + expr.getLine() + " expr = " + expr.toString());
    if (compiledXQuery == null)
        return;
    if (firstExpression == null)
        firstExpression = expr;
    stack.set(stackDepth, expr);
    String fileName = Command.getFileuri(expr.getSource());
    Integer lineNo = expr.getLine();
    // check breakpoint
    for (Breakpoint breakpoint : activeBreakpoints) {
        if (breakpoint.getFilename().equals(fileName) && breakpoint.getType().equals(Breakpoint.TYPE_LINE)) {
            if (breakpoint.getLineno() != lineNo) {
                activeBreakpoints.remove(breakpoint);
            }
        }
    }
    Map<Integer, Breakpoint> fileBreakpoints = null;
    while (true) {
        // didn't receive any command, wait for any
        if (command == null || // the status is break, wait for changes
        command.isStatus(BREAK)) {
            waitCommand();
            continue;
        }
        // wait for connection
        if (command.is(CommandContinuation.INIT) && command.isStatus(STARTING)) {
            Init init = (Init) command;
            init.getSession().setAttribute("joint", this);
            init.setFileURI(compiledXQuery.getSource());
            // break on first line
            command.setStatus(BREAK);
        }
        // disconnected
        if (compiledXQuery == null)
            return;
        // stop command, terminate
        if (command.is(CommandContinuation.STOP) && !command.isStatus(STOPPED)) {
            command.setStatus(STOPPED);
            sessionClosed(true);
            throw new TerminatedException(expr.getLine(), expr.getColumn(), "Debuggee STOP command.");
        }
        // step-into is done
        if (command.is(CommandContinuation.STEP_INTO) && command.isStatus(RUNNING)) {
            command.setStatus(BREAK);
        // step-over should stop on same call's stack depth
        } else if (command.is(CommandContinuation.STEP_OVER) && command.getCallStackDepth() == stackDepth && command.isStatus(RUNNING)) {
            command.setStatus(BREAK);
        }
        // checking breakpoints
        synchronized (breakpoints) {
            if (filesBreakpoints.containsKey(fileName)) {
                fileBreakpoints = filesBreakpoints.get(fileName);
                if (fileBreakpoints.containsKey(lineNo)) {
                    Breakpoint breakpoint = fileBreakpoints.get(lineNo);
                    if (!activeBreakpoints.contains(breakpoint) && breakpoint.getState() && breakpoint.getType().equals(Breakpoint.TYPE_LINE)) {
                        activeBreakpoints.add(breakpoint);
                        command.setStatus(BREAK);
                    // waitCommand();
                    // break;
                    }
                }
            }
        }
        // RUN command with status RUNNING can be break only on breakpoints
        if (command.getType() >= CommandContinuation.RUN && command.isStatus(RUNNING)) {
            break;
        // any continuation command with status RUNNING
        } else if (command.getType() >= CommandContinuation.RUN && command.isStatus(STARTING)) {
            command.setStatus(RUNNING);
            break;
        }
        waitCommand();
    }
}
Also used : Breakpoint(org.exist.debugger.model.Breakpoint) Init(org.exist.debuggee.dbgp.packets.Init) TerminatedException(org.exist.xquery.TerminatedException)

Example 3 with Init

use of org.exist.debuggee.dbgp.packets.Init in project exist by eXist-db.

the class DebuggeeImpl method start.

@Override
public String start(String uri) throws Exception {
    Database db = null;
    ScriptRunner runner = null;
    try {
        db = BrokerPool.getInstance();
        try (final DBBroker broker = db.getBroker()) {
            // Try to find the XQuery
            Source source = SourceFactory.getSource(broker, "", uri, true);
            if (source == null)
                return null;
            XQuery xquery = broker.getBrokerPool().getXQueryService();
            XQueryContext queryContext = new XQueryContext(broker.getBrokerPool());
            // Find correct script load path
            queryContext.setModuleLoadPath(XmldbURI.create(uri).removeLastSegment().toString());
            CompiledXQuery compiled;
            try {
                compiled = xquery.compile(broker, queryContext, source);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            String sessionId = String.valueOf(queryContext.hashCode());
            // link debugger session & script
            DebuggeeJointImpl joint = new DebuggeeJointImpl();
            SessionImpl session = new SessionImpl();
            joint.setCompiledScript(compiled);
            queryContext.setDebuggeeJoint(joint);
            joint.continuation(new Init(session, sessionId, "eXist"));
            runner = new ScriptRunner(session, compiled);
            runner.start();
            int count = 0;
            while (joint.firstExpression == null && runner.exception == null && count < 10) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                count++;
            }
            if (runner.exception != null) {
                throw runner.exception;
            }
            if (joint.firstExpression == null) {
                throw new XPathException("Can't run debug session.");
            }
            // queryContext.declareVariable(Debuggee.SESSION, sessionId);
            // XXX: make sure that it started up
            sessions.put(sessionId, session);
            return sessionId;
        }
    } catch (Exception e) {
        if (runner != null)
            runner.stop();
        throw e;
    }
}
Also used : XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) IOException(java.io.IOException) Source(org.exist.source.Source) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) DBBroker(org.exist.storage.DBBroker) Init(org.exist.debuggee.dbgp.packets.Init) Database(org.exist.Database)

Aggregations

Init (org.exist.debuggee.dbgp.packets.Init)3 XPathException (org.exist.xquery.XPathException)2 XQueryContext (org.exist.xquery.XQueryContext)2 IOException (java.io.IOException)1 IoSession (org.apache.mina.core.session.IoSession)1 Database (org.exist.Database)1 Breakpoint (org.exist.debugger.model.Breakpoint)1 Source (org.exist.source.Source)1 DBBroker (org.exist.storage.DBBroker)1 CompiledXQuery (org.exist.xquery.CompiledXQuery)1 TerminatedException (org.exist.xquery.TerminatedException)1 Variable (org.exist.xquery.Variable)1 XQuery (org.exist.xquery.XQuery)1