提交 d00b4a92 编写于 作者: S serge-rider

Debug sessions model refactoring.

上级 b5b5333c
......@@ -26,9 +26,9 @@ import org.jkiss.dbeaver.debug.internal.DebugMessages;
import org.jkiss.dbeaver.debug.jdbc.DBGJDBCSession;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
public abstract class DBGBaseController implements DBGController {
......@@ -37,7 +37,6 @@ public abstract class DBGBaseController implements DBGController {
private final DBPDataSourceContainer dataSourceContainer;
private final Map<String, Object> configuration;
private final Map<Object, DBGJDBCSession> sessions = new HashMap<>(1);
private ListenerList<DBGEventHandler> eventHandlers = new ListenerList<>();
......@@ -57,7 +56,7 @@ public abstract class DBGBaseController implements DBGController {
}
@Override
public Object attach(DBRProgressMonitor monitor) throws DBGException {
public DBGSession openSession(DBRProgressMonitor monitor) throws DBGException {
if (!dataSourceContainer.isConnected()) {
try {
dataSourceContainer.connect(monitor, true, true);
......@@ -69,173 +68,25 @@ public abstract class DBGBaseController implements DBGController {
throw new DBGException("Not connected to database");
}
try {
DBGJDBCSession debugSession = createSession(monitor, configuration);
Object targetId = debugSession.getSessionInfo().getID();
sessions.put(targetId, debugSession);
return targetId;
return createSession(monitor, configuration);
} catch (DBException e) {
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context,
dataSourceContainer);
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context, dataSourceContainer);
log.error(message, e);
throw new DBGException(message, e);
}
}
@Override
public boolean canSuspend(Object sessionKey) {
return false;
}
@Override
public boolean canResume(Object sessionKey) {
return isSessionAccessible(sessionKey);
}
@Override
public void suspend(Object sessionkey) throws DBGException {
// not supported by default
}
@Override
public void resume(Object sessionKey) throws DBGException {
DBGSession session = ensureSessionAccessible(sessionKey);
session.execContinue();
}
@Override
public void detach(DBRProgressMonitor monitor, Object sessionkey) throws DBGException {
DBGSession session = sessions.remove(sessionkey);
if (session != null) {
session.closeSession(monitor);
}
}
@Override
public void dispose() {
Collection<DBGJDBCSession> values = sessions.values();
for (DBGJDBCSession session : values) {
try {
session.closeSession(new VoidProgressMonitor());
} catch (DBGException e) {
String message = NLS.bind("Error while closing session {0}", session);
log.error(message, e);
}
}
Object[] listeners = eventHandlers.getListeners();
for (Object listener : listeners) {
unregisterEventHandler((DBGEventHandler) listener);
}
}
@Override
public List<? extends DBGBreakpointDescriptor> getBreakpoints(Object sessionKey) throws DBGException {
DBGJDBCSession session = ensureSessionAccessible(sessionKey);
return session.getBreakpoints();
}
@Override
public void addBreakpoint(DBRProgressMonitor monitor, Object sessionKey, DBGBreakpointDescriptor descriptor) throws DBGException {
DBGJDBCSession session = ensureSessionAccessible(sessionKey);
session.addBreakpoint(monitor, descriptor);
}
@Override
public void removeBreakpoint(DBRProgressMonitor monitor, Object sessionKey, DBGBreakpointDescriptor descriptor) throws DBGException {
DBGJDBCSession session = ensureSessionAccessible(sessionKey);
session.removeBreakpoint(monitor, descriptor);
}
@Override
public List<? extends DBGStackFrame> getStack(Object id) throws DBGException {
DBGSession session = ensureSessionAccessible(id);
return session.getStack();
}
@Override
public List<? extends DBGVariable<?>> getVariables(Object id, DBGStackFrame stack) throws DBGException {
DBGSession session = ensureSessionAccessible(id);
if (stack != null) {
session.selectFrame(stack.getLevel());
}
return session.getVariables();
}
@Override
public String getSource(Object sessionKey, DBGStackFrame stack) throws DBGException {
DBGSession session = ensureSessionAccessible(sessionKey);
return session.getSource(stack);
}
public abstract DBGJDBCSession createSession(DBRProgressMonitor monitor, Map<String, Object> configuration)
throws DBGException;
protected DBGJDBCSession findSession(Object id) {
return sessions.get(id);
}
public boolean isSessionExists(Object id) {
return sessions.containsKey(id);
}
public List<DBGSession> getSessions() throws DBGException {
return new ArrayList<>(sessions.values());
}
@Override
public boolean canStepInto(Object sessionKey) {
return isSessionAccessible(sessionKey);
}
@Override
public boolean canStepOver(Object sessionKey) {
return isSessionAccessible(sessionKey);
}
@Override
public boolean canStepReturn(Object sessionKey) {
// hmm, not sure
return false;
}
@Override
public void stepInto(Object sessionKey) throws DBGException {
DBGSession session = ensureSessionAccessible(sessionKey);
session.execStepInto();
}
@Override
public void stepOver(Object sessionKey) throws DBGException {
DBGSession session = ensureSessionAccessible(sessionKey);
session.execStepOver();
}
@Override
public void stepReturn(Object sessionKey) throws DBGException {
// throw DBGException?
}
protected DBGJDBCSession ensureSessionAccessible(Object sessionKey) throws DBGException {
DBGJDBCSession session = findSession(sessionKey);
if (session == null) {
String message = NLS.bind("Session for {0} is not available", sessionKey);
throw new DBGException(message);
}
boolean isAccessible = session.isAttached() && !session.isWaiting() && session.isDone();
if (!isAccessible) {
String message = NLS.bind("Session for {0} is not accessible", sessionKey);
throw new DBGException(message);
}
return session;
}
protected boolean isSessionAccessible(Object sessionKey) {
DBGJDBCSession session = findSession(sessionKey);
return session != null && session.isAttached() && !session.isWaiting() && session.isDone();
}
@Override
public void registerEventHandler(DBGEventHandler eventHandler) {
eventHandlers.add(eventHandler);
......@@ -252,22 +103,4 @@ public abstract class DBGBaseController implements DBGController {
}
}
/*
* protected void executeProcedure(DBPDataSource dataSource, Map<String,
* Object> configuration, DBRProgressMonitor monitor) throws DBException {
* String procedureName = String.valueOf(configuration.get(PROCEDURE_NAME));
* String call = String.valueOf(configuration.get(PROCEDURE_CALL)); String
* taskName = NLS.bind("Execute procedure {0}", procedureName); Job job =
* new Job(taskName) {
*
* @Override protected IStatus run(IProgressMonitor monitor) { try { try
* (final DBCSession execSession = DBUtils.openUtilSession(new
* VoidProgressMonitor(), dataSource, taskName)) { try (final DBCStatement
* dbStat = execSession.prepareStatement(DBCStatementType.EXEC, call, true,
* false, false)) { dbStat.executeStatement(); } } } catch (DBCException e)
* { log.error(taskName, e); return DebugCore.newErrorStatus(taskName, e);
*
* } return Status.OK_STATUS; } }; job.schedule(); }
*/
}
......@@ -41,64 +41,17 @@ public interface DBGController {
*
* @return key to use for <code>detach</code>
*/
Object attach(DBRProgressMonitor monitor) throws DBGException;
/**
*
* @param sessionKey
* the key obtained as a result of <code>attach</code>
*/
void detach(DBRProgressMonitor monitor, Object sessionKey) throws DBGException;
void dispose();
DBGSession openSession(DBRProgressMonitor monitor) throws DBGException;
DBGBreakpointDescriptor describeBreakpoint(Map<String, Object> attributes);
List<? extends DBGBreakpointDescriptor> getBreakpoints(Object sessionKey) throws DBGException;
void addBreakpoint(DBRProgressMonitor monitor, Object sessionKey, DBGBreakpointDescriptor descriptor) throws DBGException;
void removeBreakpoint(DBRProgressMonitor monitor, Object sessionKey, DBGBreakpointDescriptor descriptor) throws DBGException;
List<? extends DBGStackFrame> getStack(Object sessionKey) throws DBGException;
List<? extends DBGVariable<?>> getVariables(Object sessionKey, DBGStackFrame stack) throws DBGException;
String getSource(Object sessionKey, DBGStackFrame stack) throws DBGException;
/*
* suspend/resume
*/
boolean canSuspend(Object sessionKey);
boolean canResume(Object sessionKey);
void suspend(Object sessionKey) throws DBGException;
void resume(Object sessionKey) throws DBGException;
/*
* Stepping
*/
boolean canStepInto(Object sessionKey);
boolean canStepOver(Object sessionKey);
boolean canStepReturn(Object sessionKey);
void stepInto(Object sessionKey) throws DBGException;
void stepOver(Object sessionKey) throws DBGException;
void stepReturn(Object sessionKey) throws DBGException;
/*
* Events
*/
void registerEventHandler(DBGEventHandler eventHandler);
void unregisterEventHandler(DBGEventHandler eventHandler);
void dispose();
}
......@@ -27,29 +27,40 @@ public interface DBGSession {
DBGSessionInfo getSessionInfo();
Object getSessionId();
List<? extends DBGBreakpointDescriptor> getBreakpoints();
void addBreakpoint(DBRProgressMonitor monitor, DBGBreakpointDescriptor descriptor) throws DBGException;
void removeBreakpoint(DBRProgressMonitor monitor, DBGBreakpointDescriptor descriptor) throws DBGException;
boolean canStepInto();
boolean canStepOver();
boolean canStepReturn();
void execContinue() throws DBGException;
void execStepInto() throws DBGException;
void execStepOver() throws DBGException;
void closeSession(DBRProgressMonitor monitor) throws DBGException;
void execStepReturn() throws DBGException;
List<? extends DBGVariable<?>> getVariables() throws DBGException;
void resume() throws DBGException;
void suspend() throws DBGException;
List<? extends DBGVariable<?>> getVariables(DBGStackFrame stack) throws DBGException;
void setVariableVal(DBGVariable<?> variable, Object value) throws DBGException;
List<? extends DBGStackFrame> getStack() throws DBGException;
Object getSessionId();
String getSource(DBGStackFrame stack) throws DBGException;
void selectFrame(int frameNumber) throws DBGException;
void closeSession(DBRProgressMonitor monitor) throws DBGException;
String getSource(DBGStackFrame stack) throws DBGException;
}
......@@ -56,7 +56,7 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
private boolean suspended = false;
private boolean terminated = false;
private Object sessionKey;
private DBGSession session;
public DatabaseDebugTarget(String modelIdentifier, ILaunch launch, IProcess process, DBGController controller) {
super(null);
......@@ -86,8 +86,8 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
}
@Override
public Object getSessionID() {
return sessionKey;
public DBGSession getSession() {
return session;
}
protected DatabaseThread newThread() {
......@@ -169,7 +169,7 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
@Override
public void connect(IProgressMonitor monitor) throws CoreException {
try {
sessionKey = this.controller.attach(new DefaultProgressMonitor(monitor));
session = this.controller.openSession(new DefaultProgressMonitor(monitor));
} catch (DBGException e) {
String message = NLS.bind("Failed to connect {0} to the target", getName());
IStatus error = DebugCore.newErrorStatus(message, e);
......@@ -199,7 +199,10 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
terminated = true;
suspended = false;
try {
controller.detach(getProgressMonitor(), sessionKey);
if (session != null) {
session.closeSession(new VoidProgressMonitor());
session = null;
}
controller.unregisterEventHandler(this);
} catch (DBGException e) {
String message = NLS.bind("Error terminating {0}", getName());
......@@ -247,7 +250,7 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
public void resume() throws DebugException {
suspended = false;
try {
controller.resume(sessionKey);
session.resume();
} catch (DBGException e) {
String message = NLS.bind("Error resuming {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e);
......@@ -262,7 +265,7 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
@Override
public void suspend() throws DebugException {
try {
controller.suspend(sessionKey);
session.suspend();
} catch (DBGException e) {
String message = NLS.bind("Error suspending {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e);
......@@ -270,10 +273,6 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
}
}
private VoidProgressMonitor getProgressMonitor() {
return new VoidProgressMonitor();
}
public void suspended(int detail) {
suspended = true;
thread.setStepping(false);
......@@ -296,7 +295,7 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
return;
}
try {
controller.addBreakpoint(new VoidProgressMonitor(), sessionKey, descriptor);
session.addBreakpoint(new VoidProgressMonitor(), descriptor);
} catch (DBGException e) {
String message = NLS.bind("Unable to add breakpoint {0}", breakpoint);
Status error = DebugCore.newErrorStatus(message, e);
......@@ -316,7 +315,7 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
return;
}
try {
controller.removeBreakpoint(new VoidProgressMonitor(), sessionKey, descriptor);
session.removeBreakpoint(new VoidProgressMonitor(), descriptor);
} catch (DBGException e) {
String message = NLS.bind("Unable to remove breakpoint {0}", breakpoint);
Status error = DebugCore.newErrorStatus(message, e);
......@@ -374,7 +373,10 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
@Override
public void disconnect() throws DebugException {
try {
controller.detach(getProgressMonitor(), sessionKey);
if (session != null) {
session.closeSession(new VoidProgressMonitor());
session = null;
}
} catch (DBGException e) {
String message = NLS.bind("Error disconnecting {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e);
......@@ -413,45 +415,42 @@ public class DatabaseDebugTarget extends DatabaseDebugElement implements IDataba
}
public boolean canStepInto() {
return controller.canStepInto(sessionKey);
return session != null && session.canStepInto();
}
public boolean canStepOver() {
return controller.canStepOver(sessionKey);
return session != null && session.canStepOver();
}
public boolean canStepReturn() {
return controller.canStepReturn(sessionKey);
return session != null && session.canStepReturn();
}
public void stepInto() throws DebugException {
DBGController controller = getController();
try {
controller.stepInto(sessionKey);
session.execStepInto();
} catch (DBGException e) {
String message = NLS.bind("Step into failed for session {0}", sessionKey);
String message = NLS.bind("Step into failed for session {0}", session.getSessionId());
IStatus status = DebugCore.newErrorStatus(message, e);
throw new DebugException(status);
}
}
public void stepOver() throws DebugException {
DBGController controller = getController();
try {
controller.stepOver(sessionKey);
session.execStepOver();
} catch (DBGException e) {
String message = NLS.bind("Step over failed for session {0}", sessionKey);
String message = NLS.bind("Step over failed for session {0}", session.getSessionId());
IStatus status = DebugCore.newErrorStatus(message, e);
throw new DebugException(status);
}
}
public void stepReturn() throws DebugException {
DBGController controller = getController();
try {
controller.stepReturn(sessionKey);
session.execStepReturn();
} catch (DBGException e) {
String message = NLS.bind("Step return failed for session {0}", sessionKey);
String message = NLS.bind("Step return failed for session {0}", session.getSessionId());
IStatus status = DebugCore.newErrorStatus(message, e);
throw new DebugException(status);
}
......
......@@ -141,7 +141,7 @@ public class DatabaseStackFrame extends DatabaseDebugElement implements IStackFr
/*if (refreshVariables)*/ {
try {
IDatabaseDebugTarget debugTarget = getDatabaseDebugTarget();
List<? extends DBGVariable<?>> variables = debugTarget.getController().getVariables(debugTarget.getSessionID(), dbgStackFrame);
List<? extends DBGVariable<?>> variables = debugTarget.getSession().getVariables(dbgStackFrame);
rebuildVariables(variables);
} catch (DBGException e) {
log.error(e);
......@@ -232,7 +232,7 @@ public class DatabaseStackFrame extends DatabaseDebugElement implements IStackFr
String source;
try {
IDatabaseDebugTarget debugTarget = getDatabaseDebugTarget();
source = debugTarget.getController().getSource(debugTarget.getSessionID(), dbgStackFrame);
source = debugTarget.getSession().getSource(dbgStackFrame);
} catch (DBGException e) {
String message = NLS.bind("Unable to retrieve sources for stack {0}", dbgStackFrame);
IStatus status = DebugCore.newErrorStatus(message, e);
......
......@@ -158,7 +158,7 @@ public class DatabaseThread extends DatabaseDebugElement implements IThread {
protected void extractStackFrames() throws DebugException {
try {
IDatabaseDebugTarget debugTarget = getDatabaseDebugTarget();
List<? extends DBGStackFrame> stackFrames = debugTarget.getController().getStack(debugTarget.getSessionID());
List<? extends DBGStackFrame> stackFrames = debugTarget.getSession().getStack();
rebuildStack(stackFrames);
} catch (DBGException e) {
String message = NLS.bind("Error reading stack for {0}", getName());
......
......@@ -25,12 +25,13 @@ import org.eclipse.debug.core.IBreakpointManagerListener;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.model.IDebugTarget;
import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGSession;
public interface IDatabaseDebugTarget extends IDebugTarget, IDebugEventSetListener, IBreakpointManagerListener {
DBGController getController();
Object getSessionID();
DBGSession getSession();
void connect(IProgressMonitor monitor) throws CoreException;
......
......@@ -34,6 +34,7 @@ import java.util.Scanner;
@SuppressWarnings("nls")
public class Debugger {
/*
public static final String PROMPT = ">";
public static final String COMMAND_ATTACH = "A";
public static final String COMMAND_CLOSE = "X";
......@@ -61,7 +62,7 @@ public class Debugger {
DBGVariable<?> v = null;
List<? extends DBGVariable<?>> vars = session.getVariables();
List<? extends DBGVariable<?>> vars = session.getVariables(null);
Scanner scArg;
......@@ -332,7 +333,7 @@ public class Debugger {
break;
}
controller.detach(new VoidProgressMonitor(), debugSessionC.getSessionId());
debugSessionC.closeSession(new VoidProgressMonitor());
System.out.println("Session closed");
......@@ -380,7 +381,7 @@ public class Debugger {
break;
}
List<? extends DBGVariable<?>> vars = debugSessionVL.getVariables();
List<? extends DBGVariable<?>> vars = debugSessionVL.getVariables(null);
if (vars.size() == 0) {
System.out.println("No vars defined");
......@@ -706,5 +707,6 @@ public class Debugger {
}
}
*/
}
......@@ -425,6 +425,21 @@ public class PostgreDebugSession extends DBGJDBCSession {
}
@Override
public void execStepReturn() throws DBGException {
throw new DBGException("Exec return not implemented");
}
@Override
public void resume() throws DBGException {
throw new DBGException("Resume not implemented");
}
@Override
public void suspend() throws DBGException {
throw new DBGException("Suspend not implemented");
}
/**
* Execute step SQL command asynchronously, set debug session name to
* [sessionID] name [managerPID]
......@@ -447,7 +462,11 @@ public class PostgreDebugSession extends DBGJDBCSession {
}
@Override
public List<DBGVariable<?>> getVariables() throws DBGException {
public List<DBGVariable<?>> getVariables(DBGStackFrame stack) throws DBGException {
if (stack != null) {
selectFrame(stack.getLevel());
}
List<DBGVariable<?>> vars = new ArrayList<>();
String sql = SQL_GET_VARS.replaceAll("\\?sessionid", String.valueOf(sessionId));
......@@ -616,6 +635,21 @@ public class PostgreDebugSession extends DBGJDBCSession {
return sessionId;
}
@Override
public boolean canStepInto() {
return true;
}
@Override
public boolean canStepOver() {
return true;
}
@Override
public boolean canStepReturn() {
return true;
}
/**
* Return true if debug session up and running on server
*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册