Commit 29406207 authored by Johannes Roith's avatar Johannes Roith

Debugger: cleanup.

parent 90fe54f3
......@@ -93,47 +93,5 @@ public final class PluginUtility {
// fileMatching.add(new Tuple<IFile, IFile>(cupFile, javaFile));
return;
}
/**
* Find the generated file that matches our cup file.
* @param cupFile The original cup file.
* @return Can return null if no matching generated file is found.
*/
public static IFile findGenerated(IFile cupFile) {
if (cupFile == null){
return null;
}
IContainer container = cupFile.getParent();
String name = cupFile.getName();
// TODO: we should probably look for Parser.java or
// a file with the class name as defined in the grammar instead.
String withoutExt = name;
String ext = cupFile.getFileExtension();
if (ext != null && ext.length() > 0)
withoutExt = name.substring(0, name.length() - ext.length() - 1);
String generatedFile = withoutExt + ".java";
IResource res = container.findMember(generatedFile);
if (res instanceof IFile) {
// TODO: maybe search for "The following code was generated by CUP" string
// at the beginning of the file?
addFileTuple(cupFile,(IFile) res);
return (IFile) res;
}
return null;
}
}
package de.tum.in.www2.cupplugin.debug;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.debug.core.IBreakpointListener;
import org.eclipse.debug.core.model.IBreakpoint;
import de.tum.in.www2.cupplugin.PluginUtility;
//import org.eclipse.jdt.internal.debug.core.breakpoints.JavaLineBreakpoint;
public class BreakpointListener implements IBreakpointListener {
static BreakpointListener instance = null;;
......@@ -22,42 +17,24 @@ public class BreakpointListener implements IBreakpointListener {
}
public BreakpointListener() {
}
@Override
public void breakpointAdded(IBreakpoint breakpoint) {
// System.err.println("ADD: " + breakpoint);
if (breakpoint.getMarker().getResource() != null) {
// System.err.println("AT: "
// + breakpoint.getMarker().getResource()
// .getProjectRelativePath().toString()+" LINE: "+breakpoint.getMarker().getAttribute(IMarker.LINE_NUMBER, 0));
PluginUtility.findGenerated((IFile) breakpoint.getMarker().getResource());
}
if(breakpoint instanceof CupLineBreakpoint){
if(breakpoint instanceof CupLineBreakpoint)
mapper.add((CupLineBreakpoint) breakpoint);
}
System.out.println(breakpoint);
}
@Override
public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
// System.err.println("DELETE: " + breakpoint + " \n DELTA: " + delta);
if(breakpoint instanceof CupLineBreakpoint){
if(breakpoint instanceof CupLineBreakpoint)
mapper.remove((CupLineBreakpoint) breakpoint);
}
}
@Override
public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
if(breakpoint instanceof CupLineBreakpoint){
if(breakpoint instanceof CupLineBreakpoint)
mapper.change((CupLineBreakpoint) breakpoint);
}
}
}
......@@ -28,10 +28,16 @@ import de.tum.in.www2.cupplugin.GeneratedFileLocator;
import de.tum.in.www2.cupplugin.PluginUtility;
public class BreakpointMapper {
private Map<CupLineBreakpoint, IBreakpoint> mappings = new HashMap<CupLineBreakpoint, IBreakpoint>();
private Set<CupLineBreakpoint> breakpoints = new HashSet<CupLineBreakpoint>();
private GeneratedFileLocator locator = new GeneratedFileLocator();
private Map<CupLineBreakpoint, IBreakpoint> mappings;
private Set<CupLineBreakpoint> breakpoints;
private GeneratedFileLocator locator;
public BreakpointMapper() {
mappings = new HashMap<CupLineBreakpoint, IBreakpoint>();
breakpoints = new HashSet<CupLineBreakpoint>();
locator = new GeneratedFileLocator();
}
public IBreakpoint getRemoteBreakpoint(CupLineBreakpoint breakpoint) {
return mappings.get(breakpoint);
}
......@@ -101,20 +107,23 @@ public class BreakpointMapper {
debugIds.add(debugId.getDebugId());
int remoteLineNumber = lineMatcher.debuggerFindCase(debugIds).get(0) + debugId.getLineOffsetFromId();
int remoteLineNumber = lineMatcher.debuggerFindCase(debugIds).get(0)
+ debugId.getLineOffsetFromId();
doCreateTargetBreakpoint(origin, document, resource, remoteLineNumber);
if (remoteLineNumber != -1)
doCreateTargetBreakpoint(origin, document, resource, remoteLineNumber);
}
private void doCreateTargetBreakpoint(CupLineBreakpoint origin, IDocument doc, IFile remoteFile, int remoteLineNumber) {
if(remoteLineNumber == -1)
return;
private void doCreateTargetBreakpoint(CupLineBreakpoint origin,
IDocument doc, IFile remoteFile, int remoteLineNumber)
{
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put(IBreakpoint.PERSISTED, false);
IJavaLineBreakpoint remote = null;
if (remoteFile != null) {
try {
String targetClass = Debugger.getInstance(doc).getGeneratedTargetClass(origin);
Debugger dbg = Debugger.getInstance(doc);
String targetClass = dbg.getGeneratedTargetClass(origin);
remote = JDIDebugModel.createLineBreakpoint(remoteFile,
targetClass, remoteLineNumber, -1, -1, 0,
true, attributes);
......@@ -144,9 +153,6 @@ public class BreakpointMapper {
}
public void add(CupLineBreakpoint breakpoint) {
System.err.println(">>>>> ADDING " + breakpoint.getResource());
breakpoints.add(breakpoint);
remap();
}
......@@ -161,15 +167,13 @@ public class BreakpointMapper {
}
protected void removeAll() {
for (IBreakpoint breakpoint : mappings.values()) {
for (IBreakpoint breakpoint : mappings.values())
removeRemoteBreakpoint(breakpoint);
}
}
protected void addAll() {
for (CupLineBreakpoint breakpoint : breakpoints) {
for (CupLineBreakpoint breakpoint : breakpoints)
addRemoteBreakpoint(breakpoint);
}
}
public void remap() {
......
......@@ -12,6 +12,8 @@ import org.eclipse.jdt.debug.core.JDIDebugModel;
public class CupLineBreakpoint extends LineBreakpoint {
private static final String MARKER_ID = "org.eclipse.debug.core.lineBreakpointMarker";
private IResource resource = null;
private int lineNumber = -1;
......@@ -27,14 +29,11 @@ public class CupLineBreakpoint extends LineBreakpoint {
}
public CupLineBreakpoint() {
//eclipse needs that for workspace reconstruction
super();
}
public CupLineBreakpoint(final IResource resource, final int lineNumber)
throws CoreException {
this(resource, lineNumber, true);
}
......@@ -46,8 +45,7 @@ public class CupLineBreakpoint extends LineBreakpoint {
IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
IMarker marker = resource
.createMarker("org.eclipse.debug.core.lineBreakpointMarker");
IMarker marker = resource.createMarker(MARKER_ID);
setMarkerUnnoticed(marker);
marker.setAttribute(IBreakpoint.ENABLED, true);
marker.setAttribute(IBreakpoint.PERSISTED, persistent);
......@@ -64,34 +62,23 @@ public class CupLineBreakpoint extends LineBreakpoint {
public void setMarker(IMarker marker) throws CoreException {
super.setMarker(marker);
IResource resource = marker.getResource();
System.out.println("Markers: "+Arrays.toString(resource.findMarkers(null, true, IResource.DEPTH_INFINITE)));
BreakpointListener.getInstance();
// System.out.println("Markers: " +
// Arrays.toString(resource.findMarkers(null, true, IResource.DEPTH_INFINITE)));
BreakpointListener.getInstance();
Debugger.getBreakpointManager().addBreakpoint(this);
}
public void setMarkerUnnoticed(IMarker marker) throws CoreException {
super.setMarker(marker);
// System.out.println("Markers: "+Arrays.toString(marker.getResource().findMarkers(null, true, IResource.DEPTH_INFINITE)));//TODO: DELETE
}
@Override
public void delete() throws CoreException {
super.delete();
// System.out.println("delete called "+this);//TODO: DELETE
}
@Override
public String getModelIdentifier() {
return JDIDebugModel.getPluginIdentifier();
}
}
\ No newline at end of file
......@@ -27,119 +27,42 @@ import de.in.tum.www2.cup.ast.ClassName;
import de.in.tum.www2.cup.ast.ParserResult;
public class Debugger {
static final String DEBUG_ID = "de.tum.www2.cupplugin.debug.Debugger";
// Static reference to the controller instance per document
static HashMap<IDocument, Debugger> instances = new HashMap<IDocument, Debugger>();
// static HashMap<IResource, Debugger> delayedInstances = new HashMap<IResource, Debugger>();
private static final BreakpointMapper breakpointMapper = new BreakpointMapper();
private static final String DEFAULT_CLASS = "Parser";
/**
* Add Debug hooks to an editor
*
* @param editor
* the editor, the hooks shall be added to
* @return true, if adding hooks succeeded
*/
private static HashMap<IDocument, Debugger> instances = new HashMap<IDocument, Debugger>();
private static final BreakpointMapper breakpointMapper = new BreakpointMapper();
public static boolean addDebugHooksToTextEditor(CupTextEditor editor) {
// nothing so far...
return true;
}
// attributes
// the document, the instance is managing
// private CupTextEditor myEditor = null;
private IDocument document = null;
private static IBreakpointManager defaultBreakPointManager = null;
private ParserResult selfBuiltAstCache;
public static Debugger getInstance(IDocument document) {
Debugger instance = instances.get(document);
if (instance != null) {
/*
if (!instances.isEmpty()) {
for (Map.Entry<IDocument, Debugger> e : instances
.entrySet()) {
IEditorInput input = e.getKey().getEditorInput();
if (input == null) {
continue;
}
IResource resource = (IResource) input
.getAdapter(IResource.class);
if (delayedInstances.get(resource) != null) {
delayedInstances.remove(resource);
}
}
}*/
if (instance != null)
return instance;
} else {
instances.put(document, new Debugger(document));
return instances.get(document);
}
instances.put(document, new Debugger(document));
return instances.get(document);
}
public static BreakpointMapper getBreakpointMapper() {
return breakpointMapper;
}
/*
public static Debugger getInstance(IResource resource) {
IResource current = null;
for (Map.Entry<IDocument, Debugger> e : instances.entrySet()) {
IEditorInput input = e.getKey().getEditorInput();
if (input != null) {
current = (IResource) input.getAdapter(IResource.class);
} else {
continue;
}
System.out.println("RESOURCE: " + current);
if (current != null && current.equals(resource)) {
return e.getValue();
}
current = null;
}
//IDocumentProvider provider = new TextFileDocumentProvider();
//provider.connect(ifile);
//document = provider.getDocument(ifile);
throw new RuntimeException("CUP EDITOR NOT FOUND!");
//Debugger tmp = new Debugger(new CupTextEditor());// TODO: lösen
//delayedInstances.put(resource, tmp);
//return tmp;
}
*/
/*
* The Constructor, taking the document it's controlling as a parameter
*/
public Debugger(IDocument document) {
this.document = document;
defaultBreakPointManager = DebugPlugin.getDefault()
.getBreakpointManager();
BreakpointListener.getInstance();
}
public static IBreakpointManager getBreakpointManager() {
if (defaultBreakPointManager == null) {
defaultBreakPointManager = DebugPlugin.getDefault()
.getBreakpointManager();
}
return defaultBreakPointManager;
return DebugPlugin.getDefault().getBreakpointManager();
}
/**
* Get the next useful location for a breakpointmarker
*
* @param line
* the line, the action was triggered on
* @param line the line, the action was triggered on
* @return the next useful line
*/
public int getNextCodeBlockForLineBreakpoint(int line) {
......@@ -149,7 +72,6 @@ public class Debugger {
System.err.println("WARNING: did not use breakpoint snapping, because AST was not available.");
return line;
}
FindNextCodeBlockLineVisitor v = new FindNextCodeBlockLineVisitor(line);
astModel.accept(v, null);
return v.getResult();
......@@ -159,10 +81,6 @@ public class Debugger {
* The java class in which a remote breakpoint will be set. The Java
* debugger needs that information to break correctly. example: Parser
* Parser$NestedSubClass
*
* @param breakpoint
* the breakpoint that asks
* @return the class name
*/
public String getGeneratedTargetClass(CupLineBreakpoint breakpoint) {
ParserResult result = getParserResult();
......@@ -197,9 +115,9 @@ public class Debugger {
ParserResult astModel = getParserResult();
if (astModel == null)
return null;
// TODO: this is too expensive! -> pass all line numbers in array!
// create Visitor
int lines[] = { lineNumber };
GetDebuggerMappingVisitor v = new GetDebuggerMappingVisitor(lines);
astModel.accept(v, null);
......@@ -209,7 +127,6 @@ public class Debugger {
return v.getMappings().get(0);
}
private ParserResult selfBuiltAstCache;
public ParserResult getParserResult() {
if(document == null)
......@@ -234,9 +151,7 @@ public class Debugger {
} catch (Exception e1) {
e1.printStackTrace();
}
return selfBuiltAstCache;
} else {
Model m = Model.getInstanceForDocument(document);
ParserResult astModel = m.getAstModel();
......
......@@ -5,7 +5,6 @@ import java.util.List;
public interface IDebugLineMatcher {
/**
*
* @param indexes A list of debug ids in the original CUP file.
* @return A list of matching line numbers in the generated file.
*/
......
......@@ -38,7 +38,6 @@ public class SimpleDebugLineMatcher implements IDebugLineMatcher {
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......
......@@ -54,17 +54,13 @@ public class ToggleBreakpointFactory implements IToggleBreakpointsTargetFactory
@Override
public String getToggleTargetName(String targetID) {
// System.out.println("NAME:" + toggleTargets.get(targetID));// TODO:
// DELETE
// is that really right here?
// TODO
return "NAME:" + toggleTargets.get(targetID);
}
@Override
public String getToggleTargetDescription(String targetID) {
// System.out.println("DESCRIPTION:" + toggleTargets.get(targetID));//
// TODO:DELETE
// i don't know
// TODO
return "DESCRIPTION:" + toggleTargets.get(targetID);
}
......
......@@ -19,9 +19,6 @@ public class ToggleBreakpointsTarget implements IToggleBreakpointsTarget {
public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection)
throws CoreException {
CupTextEditor textEditor = null;
/*
* find out where the editor is
*/
if (part instanceof CupTextEditor) {
textEditor = (CupTextEditor) part;
} else if (part instanceof MultiPageEditor) {
......@@ -44,6 +41,8 @@ public class ToggleBreakpointsTarget implements IToggleBreakpointsTarget {
} else {
return;
}
lineNumber = lineNumber + 1;
// get doc identifier:
IDocument doc = null;
......@@ -53,42 +52,38 @@ public class ToggleBreakpointsTarget implements IToggleBreakpointsTarget {
if (doc == null)
return;
// create breakpoint and give it to IDE
boolean didRemove = false;
IBreakpoint[] breakpoints = (IBreakpoint[]) Debugger
.getBreakpointManager().getBreakpoints();
if (breakpoints != null) {
for (int i = 0; i < breakpoints.length; i++) {
IBreakpoint breakpoint = breakpoints[i];
if (breakpoint instanceof CupLineBreakpoint) {
if (resource.equals(breakpoint.getMarker().getResource())) {
int lnum = ((ILineBreakpoint) breakpoint).getLineNumber();
if (lnum == lineNumber + 1) {
// existing breakpoint; delete
Debugger.getBreakpointManager().removeBreakpoint(
breakpoint, true);
didRemove = true;
}
if (removeOldBreakpoints(resource, lineNumber))
return;
CupLineBreakpoint lineBreakpoint = new CupLineBreakpoint(resource, lineNumber );
Debugger.getBreakpointManager().addBreakpoint(lineBreakpoint);
}
}
private boolean removeOldBreakpoints(IResource resource, int lineNumber)
throws CoreException
{
boolean didRemove = false;
IBreakpoint[] breakpoints = (IBreakpoint[]) Debugger
.getBreakpointManager().getBreakpoints();
if (breakpoints != null) {
for (int i = 0; i < breakpoints.length; i++) {
IBreakpoint breakpoint = breakpoints[i];
if (breakpoint instanceof CupLineBreakpoint) {
if (resource.equals(breakpoint.getMarker().getResource())) {
int lnum = ((ILineBreakpoint) breakpoint).getLineNumber();
if (lnum == lineNumber) {
// existing breakpoint; delete
Debugger.getBreakpointManager().removeBreakpoint(
breakpoint, true);
didRemove = true;
}
}
}
}
if (didRemove)
return;
// new breakpoint; create
CupLineBreakpoint lineBreakpoint = new CupLineBreakpoint(resource,
lineNumber + 1);
Debugger.getBreakpointManager().addBreakpoint(lineBreakpoint);
/*
* System.out.println("CREATED! Length: " +
* Debugger.getBreakpointManager() .getBreakpoints().length);
*/// TODO: DELETE
}
return didRemove;
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment