Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d799e50bf826cb78fd72a1af6a03d91e83d9d8c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2008, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.examples.ui.pda.breakpoints;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.examples.core.pda.model.PDAStackFrame;
import org.eclipse.debug.examples.core.pda.model.PDAVariable;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.progress.WorkbenchJob;
import org.eclipse.ui.texteditor.ITextEditor;


/**
 * Adapter to create specialized watchpoints in PDA files and the variables views.
 */
public class PDAToggleWatchpointsTarget extends PDABreakpointAdapter {

    final private boolean fAccessModeEnabled;
    final private boolean fModificationModeEnabled;
    
    PDAToggleWatchpointsTarget(boolean access, boolean modification) {
        fAccessModeEnabled = access;
        fModificationModeEnabled = modification;
    }
    
    @Override
	public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) {
        if (super.canToggleWatchpoints(part, selection)) {
            return true;
        } else {
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection ss = (IStructuredSelection)selection;
                return ss.getFirstElement() instanceof PDAVariable;
            }
        }
        return false;
    }
    
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void toggleWatchpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
	    String[] variableAndFunctionName = getVariableAndFunctionName(part, selection);
	    
	    if (variableAndFunctionName != null && part instanceof ITextEditor && selection instanceof ITextSelection) {
	    	// Selection inside text editor.  Create a watchpoint based on 
	    	// current source line.
	        ITextEditor editorPart = (ITextEditor)part;
	        int lineNumber = ((ITextSelection)selection).getStartLine();
	        IResource resource = editorPart.getEditorInput().getAdapter(IResource.class);
	        String var = variableAndFunctionName[0];
	        String fcn = variableAndFunctionName[1];
	        toggleWatchpoint(resource, lineNumber, fcn, var, fAccessModeEnabled, fModificationModeEnabled);
	    } else if (selection instanceof IStructuredSelection && 
	               ((IStructuredSelection)selection).getFirstElement() instanceof PDAVariable ) 
	    {
	        // Selection is inside a variables view.  Create a watchpoint 
	        // using information from  the variable.  Retrieving information
	        // from the model requires performing source lookup which should be 
	        // done on a background thread.
	        final PDAVariable var = (PDAVariable)((IStructuredSelection)selection).getFirstElement();
	        final PDAStackFrame frame = var.getStackFrame();
	        final Shell shell = part.getSite().getShell(); 
	        
			new Job("Toggle PDA Watchpoint") { //$NON-NLS-1$
	            { setSystem(true); }
	            
	            @Override
				protected IStatus run(IProgressMonitor monitor) {
	                try {
    	                IFile file = getResource(var.getStackFrame());
    	                String varName = var.getName();
    	                int line = findLine(file, varName);
    	                toggleWatchpoint(file, line, frame.getName(), varName, 
    	                    fAccessModeEnabled, fModificationModeEnabled);
	                } catch (final CoreException e) {
	                    // Need to switch back to the UI thread to show the error
	                    // dialog.
						new WorkbenchJob(shell.getDisplay(), "Toggle PDA Watchpoint") { //$NON-NLS-1$
	                        { setSystem(true); }
	                        
	                        @Override
							public IStatus runInUIThread(IProgressMonitor submonitor) {
								ErrorDialog.openError(shell, "Failed to create PDA watchpoint", "Failed to create PDA watchpoint.\n", e.getStatus()); //$NON-NLS-1$ //$NON-NLS-2$
	                            return Status.OK_STATUS;
	                        }
	                    }.schedule();
	                }
	                return Status.OK_STATUS;
	            }
	        }.schedule();
	    }
	}

	private IFile getResource(PDAStackFrame frame) {
	    ISourceLocator locator = frame.getLaunch().getSourceLocator();
	    Object sourceElement = locator.getSourceElement(frame);
	    if (sourceElement instanceof IFile) {
	        return (IFile)sourceElement;
	    }
	    return null;
	}
	
	private int findLine(IFile file, String var) throws CoreException {
	    BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents()));
	    
	    int lineNum = 0;
	    try {
    	    while(true) {
    	        String line = reader.readLine().trim();
				if (line.startsWith("var")) { //$NON-NLS-1$
					String varName = line.substring("var".length()).trim(); //$NON-NLS-1$
    	            if (varName.equals(var)) {
    	                break;
    	            }
    	        }
                lineNum++;
    	    }
	    } catch (IOException e) {
	        // end of file reached and line wasn't found
	        return -1;
	    } finally {
	        try {
	            reader.close();
	        } catch (IOException e) {}
	    }
	    return lineNum;
	}
	
    /* (non-Javadoc)
     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#toggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
     */
    @Override
	public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
        if (canToggleWatchpoints(part, selection)) {
            toggleWatchpoints(part, selection);
        } else {
            toggleLineBreakpoints(part, selection);
        }    
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#canToggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
     */
    @Override
	public boolean canToggleBreakpoints(IWorkbenchPart part, ISelection selection) {
        return canToggleLineBreakpoints(part, selection) || canToggleWatchpoints(part, selection);
    }
}

Back to the top