Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 199527dbf1399c24fda51c82e08b6b4f00be1c9e (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*******************************************************************************
 * Copyright (c) 2007, 2015 Ericsson 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:
 *     Ericsson - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.examples.dsf.pda.service;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.Immutable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.AbstractDMContext;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints;
import org.eclipse.cdt.dsf.service.AbstractDsfService;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.examples.dsf.pda.PDAPlugin;
import org.eclipse.cdt.examples.dsf.pda.breakpoints.PDAWatchpoint;
import org.eclipse.cdt.examples.dsf.pda.service.commands.PDAClearBreakpointCommand;
import org.eclipse.cdt.examples.dsf.pda.service.commands.PDACommandResult;
import org.eclipse.cdt.examples.dsf.pda.service.commands.PDASetBreakpointCommand;
import org.eclipse.cdt.examples.dsf.pda.service.commands.PDAWatchCommand;
import org.eclipse.core.resources.IMarker;
import org.eclipse.debug.core.model.IBreakpoint;
import org.osgi.framework.BundleContext;

/**
 * Initial breakpoint service implementation.
 * Implements the IBreakpoints interface.
 */
public class PDABreakpoints extends AbstractDsfService implements IBreakpoints
{
    /**
     * Context representing a PDA line breakpoint.  In PDA debugger, since there is only 
     * one file being debugged at a time, a breakpoint is uniquely identified using the 
     * line number only.
     */
    @Immutable
    private static class BreakpointDMContext extends AbstractDMContext implements IBreakpointDMContext {

        final Integer fLine;

        public BreakpointDMContext(String sessionId, PDAVirtualMachineDMContext commandControlCtx, Integer line) {
            super(sessionId, new IDMContext[] { commandControlCtx });
            fLine = line;
        }

        @Override
        public boolean equals(Object obj) {
            return baseEquals(obj) && (fLine.equals(((BreakpointDMContext) obj).fLine));
        }

        @Override
        public int hashCode() {
            return baseHashCode() + fLine.hashCode();
        }

        @Override
        public String toString() {
            return baseToString() + ".breakpoint(" + fLine + ")";  //$NON-NLS-1$//$NON-NLS-2$*/
        }
    }

    /**
     * Context representing a watch point.  In PDA debugger, a watchpoint is 
     * uniquely identified using the function and variable.
     */
    @Immutable
    private static class WatchpointDMContext extends AbstractDMContext implements IBreakpointDMContext {
        final String fFunction;
        final String fVariable; 

        public WatchpointDMContext(String sessionId, PDAVirtualMachineDMContext commandControlCtx, String function, 
            String variable) 
        {
            super(sessionId, new IDMContext[] { commandControlCtx });
            fFunction = function;
            fVariable = variable;
        }

        @Override
        public boolean equals(Object obj) {
            if (baseEquals(obj)) {
                WatchpointDMContext watchpointCtx = (WatchpointDMContext)obj;
                return fFunction.equals(watchpointCtx.fFunction) && fVariable.equals(watchpointCtx.fVariable);
            }
            return false;
        }

        @Override
        public int hashCode() {
            return baseHashCode() + fFunction.hashCode() + fVariable.hashCode();
        }

        @Override
        public String toString() {
            return baseToString() + ".watchpoint(" + fFunction + "::" + fVariable + ")";
        }
    }

    // Attribute names
    public static final String ATTR_BREAKPOINT_TYPE = PDAPlugin.PLUGIN_ID + ".pdaBreakpointType";      //$NON-NLS-1$
    public static final String PDA_LINE_BREAKPOINT = "breakpoint";                 //$NON-NLS-1$
    public static final String PDA_WATCHPOINT = "watchpoint";                 //$NON-NLS-1$
    public static final String ATTR_PROGRAM_PATH = PDAPlugin.PLUGIN_ID + ".pdaProgramPath";      //$NON-NLS-1$

    // Services
    private PDACommandControl fCommandControl;

    // Breakpoints currently installed
    private Set<IBreakpointDMContext> fBreakpoints = new HashSet<IBreakpointDMContext>();

    /**
     * The service constructor
     * 
     * @param session The debugging session this service belongs to.
     */
    public PDABreakpoints(DsfSession session) {
        super(session);
    }

    @Override
    public void initialize(final RequestMonitor rm) {
        super.initialize(new RequestMonitor(getExecutor(), rm) {
            @Override
            protected void handleSuccess() {
                doInitialize(rm);
            }
        });
    }

    private void doInitialize(final RequestMonitor rm) {
        // Get the services references
        fCommandControl = getServicesTracker().getService(PDACommandControl.class);

        // Register this service
        register(new String[] { IBreakpoints.class.getName(), PDABreakpoints.class.getName() },
            new Hashtable<String, String>());

        rm.done();
    }

    @Override
    public void shutdown(final RequestMonitor rm) {
        unregister();
        rm.done();
    }

    @Override
    protected BundleContext getBundleContext() {
        return PDAPlugin.getBundleContext();
    }

    @Override
    public void getBreakpoints(final IBreakpointsTargetDMContext context, final DataRequestMonitor<IBreakpointDMContext[]> rm) {
        // Validate the context
        if (!fCommandControl.getContext().equals(context)) {
            PDAPlugin.failRequest(rm, INVALID_HANDLE, "Invalid breakpoints target context");
            return;
        }

        rm.setData(fBreakpoints.toArray(new IBreakpointDMContext[fBreakpoints.size()]));
        rm.done();
    }

    @Override
    public void getBreakpointDMData(IBreakpointDMContext dmc, DataRequestMonitor<IBreakpointDMData> rm) {
        PDAPlugin.failRequest(rm, NOT_SUPPORTED, "Retrieving breakpoint data is not supported");
    }

    @Override
    public void insertBreakpoint(IBreakpointsTargetDMContext context, Map<String, Object> attributes, 
        DataRequestMonitor<IBreakpointDMContext> rm) 
    {
        Boolean enabled = (Boolean)attributes.get(IBreakpoint.ENABLED);
        if (enabled != null && !enabled.booleanValue()) {
            // If the breakpoint is disabled, just fail the request. 
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "Breakpoint is disabled");
        } else {
            String type = (String) attributes.get(ATTR_BREAKPOINT_TYPE);

            if (PDA_LINE_BREAKPOINT.equals(type)) {
                // Retrieve the PDA program context from the context given in the 
                // argument.  This service is typically only called by the 
                // breakpoints mediator, which was called with the program context
                // in the services initialization sequence.  So checking if 
                // programCtx != null is mostly a formality.
                PDAVirtualMachineDMContext programCtx = DMContexts.getAncestorOfType(context, PDAVirtualMachineDMContext.class);
                if (programCtx != null) {
                    doInsertBreakpoint(programCtx, attributes, rm);
                } else {
                    PDAPlugin.failRequest(rm, INVALID_HANDLE, "Unknown breakpoint type");
                }
            }
            else if (PDA_WATCHPOINT.equals(type)) {
                doInsertWatchpoint(attributes, rm);
            }
            else {
                PDAPlugin.failRequest(rm, REQUEST_FAILED, "Unknown breakpoint type");
            }
        }
    }

    private void doInsertBreakpoint(PDAVirtualMachineDMContext programCtx, final Map<String, Object> attributes, final DataRequestMonitor<IBreakpointDMContext> rm) 
    {
        // Compare the program path in the breakpoint with the path in the PDA 
        // program context. Only insert the breakpoint if the program matches. 
        String program = (String)attributes.get(ATTR_PROGRAM_PATH);
        if (!programCtx.getProgram().equals(program)) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "Invalid file name");
            return;
        }

        // Retrieve the line.
        Integer line = (Integer)attributes.get(IMarker.LINE_NUMBER);
        if (line == null) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "No breakpoint line specified");
            return;
        }

        // Create a new breakpoint context object and check that it's not 
        // installed already. PDA can only track a single breakpoint at a 
        // given line, attempting to set the second breakpoint should fail.
        final BreakpointDMContext breakpointCtx = 
            new BreakpointDMContext(getSession().getId(), fCommandControl.getContext(), line);
        if (fBreakpoints.contains(breakpointCtx)) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "Breakpoint already set");
            return;
        }

        // Add the new breakpoint context to the list of known breakpoints.  
        // Adding it here, before the set command is completed will prevent 
        // a possibility of a second breakpoint being installed in the same 
        // location while this breakpoint is being processed.  It will also
        // allow the breakpoint to be removed or updated even while it is 
        // still being processed here.
        fBreakpoints.add(breakpointCtx);
        fCommandControl.queueCommand(
            new PDASetBreakpointCommand(fCommandControl.getContext(), line, false), 
            new DataRequestMonitor<PDACommandResult>(getExecutor(), rm) {
                @Override
                protected void handleSuccess() {
                    rm.setData(breakpointCtx);
                    rm.done();
                }

                @Override
                protected void handleFailure() {
                    // If inserting of the breakpoint failed, remove it from
                    // the set of installed breakpoints.
                    fBreakpoints.remove(breakpointCtx);
                    super.handleFailure();
                }
            });
    }

    private void doInsertWatchpoint(final Map<String, Object> attributes, final DataRequestMonitor<IBreakpointDMContext> rm) 
    {
        String function = (String)attributes.get(PDAWatchpoint.FUNCTION_NAME);
        if (function == null) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "No function specified");
            return;
        }

        String variable = (String)attributes.get(PDAWatchpoint.VAR_NAME);
        if (variable == null) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "No variable specified");
            return;
        }

        Boolean isAccess = (Boolean)attributes.get(PDAWatchpoint.ACCESS);
        isAccess = isAccess != null ? isAccess : Boolean.FALSE; 

        Boolean isModification = (Boolean)attributes.get(PDAWatchpoint.MODIFICATION);
        isModification = isModification != null ? isModification : Boolean.FALSE; 

        // Create a new watchpoint context object and check that it's not 
        // installed already. PDA can only track a single watchpoint for a given
        // function::variable, attempting to set the second breakpoint should fail.
        final WatchpointDMContext watchpointCtx = 
            new WatchpointDMContext(getSession().getId(), fCommandControl.getContext(), function, variable);
        if (fBreakpoints.contains(watchpointCtx)) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "Watchpoint already set");
            return;
        }

        // Determine the watch operation to perform.
        PDAWatchCommand.WatchOperation watchOperation = PDAWatchCommand.WatchOperation.NONE;
        if (isAccess && isModification) {
            watchOperation = PDAWatchCommand.WatchOperation.BOTH;
        } else if (isAccess) {
            watchOperation = PDAWatchCommand.WatchOperation.READ;
        } else if (isModification) {
            watchOperation = PDAWatchCommand.WatchOperation.WRITE;
        }

        // Add the new breakpoint context to the list of known breakpoints.  
        // Adding it here, before the set command is completed will prevent 
        // a possibility of a second breakpoint being installed in the same 
        // location while this breakpoint is being processed.  It will also
        // allow the breakpoint to be removed or updated even while it is 
        // still being processed here.
        fBreakpoints.add(watchpointCtx);
        fCommandControl.queueCommand(
            new PDAWatchCommand(fCommandControl.getContext(), function, variable, watchOperation), 
            new DataRequestMonitor<PDACommandResult>(getExecutor(), rm) {
                @Override
                protected void handleSuccess() {
                    rm.setData(watchpointCtx);
                    rm.done();
                }

                @Override
                protected void handleFailure() {
                    // Since the command failed, we need to remove the breakpoint from 
                    // the existing breakpoint set.
                    fBreakpoints.remove(watchpointCtx);
                    super.handleFailure();
                }
            });
    }

    @Override
    public void removeBreakpoint(IBreakpointDMContext bpCtx, RequestMonitor rm) {
        if (!fBreakpoints.contains(bpCtx)) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "Breakpoint already removed");
            return;
        }

        if (bpCtx instanceof BreakpointDMContext) {
            doRemoveBreakpoint((BreakpointDMContext)bpCtx, rm);
        } else if (bpCtx instanceof WatchpointDMContext) {
            doRemoveWatchpoint((WatchpointDMContext)bpCtx, rm);
        } else {
            PDAPlugin.failRequest(rm, INVALID_HANDLE, "Invalid breakpoint");
        }
    }

    private void doRemoveBreakpoint(BreakpointDMContext bpCtx, RequestMonitor rm) {
        // Remove the breakpoint from the table right away, so that even when 
        // the remove is being processed, a new breakpoint can be created at the same 
        // location.
        fBreakpoints.remove(bpCtx);

        fCommandControl.queueCommand(
            new PDAClearBreakpointCommand(fCommandControl.getContext(), bpCtx.fLine), 
            new DataRequestMonitor<PDACommandResult>(getExecutor(), rm));        
    }

    private void doRemoveWatchpoint(WatchpointDMContext bpCtx, RequestMonitor rm) {
        fBreakpoints.remove(bpCtx);

        // Watchpoints are cleared using the same command, but with a "no watch" operation
        fCommandControl.queueCommand(
            new PDAWatchCommand(
                fCommandControl.getContext(), bpCtx.fFunction, bpCtx.fVariable, PDAWatchCommand.WatchOperation.NONE), 
                new DataRequestMonitor<PDACommandResult>(getExecutor(), rm));        
    }

    @Override
    public void updateBreakpoint(final IBreakpointDMContext bpCtx, Map<String, Object> attributes, final RequestMonitor rm) {
        if (!fBreakpoints.contains(bpCtx)) {
            PDAPlugin.failRequest(rm, REQUEST_FAILED, "Breakpoint not installed");
            return;
        }

        if (bpCtx instanceof BreakpointDMContext) {
            PDAPlugin.failRequest(rm, NOT_SUPPORTED, "Modifying PDA breakpoints is not supported");
        } else if (bpCtx instanceof WatchpointDMContext) {
            WatchpointDMContext wpCtx = (WatchpointDMContext)bpCtx;
            if (!wpCtx.fFunction.equals(attributes.get(PDAWatchpoint.FUNCTION_NAME)) || 
                !wpCtx.fVariable.equals(attributes.get(PDAWatchpoint.VAR_NAME)) )
            {
                PDAPlugin.failRequest(rm, REQUEST_FAILED, "Cannot modify watchpoint function or variable");
                return;
            }
            
            // PDA debugger can only track one watchpoint in the same location, 
            // so we can simply remove the existing context from the set and 
            // call insert again.  
            fBreakpoints.remove(bpCtx);
            doInsertWatchpoint(
                attributes, 
                new DataRequestMonitor<IBreakpointDMContext>(getExecutor(), rm) {
                    @Override
                    protected void handleSuccess() {
                        // The inserted watchpoint context will equal the 
                        // current context.
                        assert bpCtx.equals(getData());
                        rm.done();
                    }
                });
        } else {
            PDAPlugin.failRequest(rm, INVALID_HANDLE, "Invalid breakpoint");
        }
    }
}

Back to the top