Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 124c8ac16e5c39c1ee6be6cbf25f54e07331baf4 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*******************************************************************************
 * Copyright (c) 2010 Wind River Systems, Inc. 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.tm.internal.tcf.cdt.ui;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.debug.core.model.IMoveToAddress;
import org.eclipse.cdt.debug.core.model.IMoveToLine;
import org.eclipse.cdt.debug.core.model.IResumeAtAddress;
import org.eclipse.cdt.debug.core.model.IResumeAtLine;
import org.eclipse.cdt.debug.core.model.IRunToAddress;
import org.eclipse.cdt.debug.core.model.IRunToLine;
import org.eclipse.cdt.debug.internal.core.sourcelookup.CSourceLookupDirector;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.core.model.ISuspendResume;
import org.eclipse.tm.internal.tcf.debug.model.TCFContextState;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFChildren;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFDebugTask;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNode;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNodeExecContext;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNodeRegister;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.services.IBreakpoints;
import org.eclipse.tm.tcf.services.ILineNumbers;
import org.eclipse.tm.tcf.services.ILineNumbers.CodeArea;
import org.eclipse.tm.tcf.services.IMemory;
import org.eclipse.tm.tcf.services.IRegisters;
import org.eclipse.tm.tcf.services.IRegisters.RegistersContext;
import org.eclipse.tm.tcf.services.IRunControl;
import org.eclipse.tm.tcf.services.IRunControl.RunControlContext;
import org.eclipse.tm.tcf.services.IRunControl.RunControlListener;
import org.eclipse.tm.tcf.util.TCFDataCache;
import org.eclipse.tm.tcf.util.TCFTask;

/**
 * A {@link ISuspendResume} adapter for TCF execution contexts enabling special
 * run control actions run-to-line, move-to-line and resume-at-line.
 */
@SuppressWarnings("restriction")
public class TCFSuspendResumeAdapter implements ISuspendResume, IRunToLine,
        IRunToAddress, IMoveToLine, IMoveToAddress, IResumeAtLine,
        IResumeAtAddress, IAdaptable {

    private static class Location {
        String fFile;
        int fLine;
        Number fAddress;
        Location(String file, int line) {
            fFile = file;
            fLine = line;
        }
        Location(Number address) {
            fAddress = address;
        }
    }

    private final TCFNodeExecContext fExecCtx;

    public TCFSuspendResumeAdapter(TCFNodeExecContext execCtx) {
        fExecCtx = execCtx;
    }

    @SuppressWarnings("rawtypes")
    public Object getAdapter(Class adapter) {
        if (adapter.isInstance(this)) {
            return this;
        }
        return null;
    }

    public boolean canResume() {
        return isSuspended();
    }

    public boolean canSuspend() {
        return !isSuspended();
    }

    public boolean isSuspended() {
        try {
            Boolean result = new TCFTask<Boolean>() {
                public void run() {
                    if (fExecCtx.isDisposed()) {
                        done(Boolean.FALSE);
                        return;
                    }
                    TCFDataCache<TCFContextState> state = fExecCtx.getState();
                    if (!state.validate(this)) {
                        return;
                    }
                    if (state.getError() == null && state.getData() != null) {
                        done(state.getData().is_suspended);
                        return;
                    }
                    done(Boolean.FALSE);
                }
            }.get();
            return result != null ? result : false;
        }
        catch (Exception e) {
            // ignored
        }
        return false;
    }

    public void resume() throws DebugException {
        new TCFDebugTask<Object>() {
            public void run() {
                if (fExecCtx.isDisposed()) {
                    // ignore silently
                    done(null);
                    return;
                }
                TCFDataCache<IRunControl.RunControlContext> cache = fExecCtx.getRunContext();
                if (!cache.validate(this)) {
                    return;
                }
                IRunControl.RunControlContext runCtx = cache.getData();
                if (runCtx.canResume(IRunControl.RM_RESUME)) {
                    runCtx.resume(IRunControl.RM_RESUME, 1, new IRunControl.DoneCommand() {
                        public void doneCommand(IToken token, Exception error) {
                            if (error != null) {
                                error(error);
                            } else {
                                done(null);
                            }
                        }
                    });
                }
            }
        }.getD();
    }

    public void suspend() throws DebugException {
        new TCFDebugTask<Object>() {
            public void run() {
                if (fExecCtx.isDisposed()) {
                    // ignore silently
                    done(null);
                    return;
                }
                TCFDataCache<IRunControl.RunControlContext> cache = fExecCtx.getRunContext();
                if (!cache.validate(this)) {
                    return;
                }
                IRunControl.RunControlContext runCtx = cache.getData();
                if (runCtx.canSuspend()) {
                    runCtx.suspend(new IRunControl.DoneCommand() {
                        public void doneCommand(IToken token, Exception error) {
                            if (error != null) {
                                error(error);
                            } else {
                                done(null);
                            }
                        }
                    });
                }
            }
        }.getD();
    }

    public boolean canResumeAtAddress(IAddress address) {
        return true;
    }

    public void resumeAtAddress(IAddress address) throws DebugException {
        moveToLocation(new Location(address.getValue()), true);
    }

    public boolean canResumeAtLine(IFile file, int lineNumber) {
        return true;
    }

    public void resumeAtLine(IFile file, int lineNumber) throws DebugException {
        IPath location = file.getLocation();
        if (location == null) {
            throw new DebugException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot resume at line: Not a local file."));
        }
        resumeAtLine(location.toOSString(), lineNumber);
    }

    public boolean canResumeAtLine(String fileName, int lineNumber) {
        return true;
    }

    public void resumeAtLine(String fileName, int lineNumber) throws DebugException {
        String debuggerPath = mapToDebuggerPath(fileName);
        moveToLocation(new Location(debuggerPath, lineNumber), true);
    }

    public boolean canMoveToAddress(IAddress address) {
        return true;
    }

    public void moveToAddress(IAddress address) throws DebugException {
        moveToLocation(new Location(address.getValue()), false);
    }

    public boolean canMoveToLine(String fileName, int lineNumber) {
        return true;
    }

    public void moveToLine(String fileName, int lineNumber) throws DebugException {
        String debuggerPath = mapToDebuggerPath(fileName);
        moveToLocation(new Location(debuggerPath, lineNumber), false);
    }

    public boolean canRunToAddress(IAddress address) {
        return canResume();
    }

    public void runToAddress(IAddress address, boolean skipBreakpoints) throws DebugException {
        runToLocation(new Location(address.getValue()), skipBreakpoints);
    }

    public boolean canRunToLine(IFile file, int lineNumber) {
        return canResume();
    }

    public void runToLine(IFile file, int lineNumber, boolean skipBreakpoints) throws DebugException {
        IPath location = file.getLocation();
        if (location == null) {
            throw new DebugException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot run to line: Not a local file."));
        }
        runToLine(location.toOSString(), lineNumber, skipBreakpoints);
    }

    public boolean canRunToLine(String fileName, int lineNumber) {
        return canResume();
    }

    public void runToLine(String fileName, final int lineNumber, final boolean skipBreakpoints) throws DebugException {
        String debuggerPath = mapToDebuggerPath(fileName);
        runToLocation(new Location(debuggerPath, lineNumber), skipBreakpoints);
    }

    private String mapToDebuggerPath(String fileName) {
        ISourceLocator locator = fExecCtx.getModel().getLaunch().getSourceLocator();
        if (locator instanceof CSourceLookupDirector) {
            IPath compilationPath = ((CSourceLookupDirector) locator).getCompilationPath(fileName);
            if (compilationPath != null) {
                return compilationPath.toString();
            }
        }
        // fallback: use basename
        return new Path(fileName).lastSegment();
    }

    private void runToLocation(final Location location, final boolean skipBreakpoints) throws DebugException {
        new TCFDebugTask<Object>() {
            public void run() {
                if (fExecCtx.isDisposed()) {
                    // ignore silently
                    done(null);
                    return;
                }
                final TCFDataCache<IRunControl.RunControlContext> cache = fExecCtx.getRunContext();
                if (!cache.validate(this)) {
                    return;
                }
                final IChannel channel = fExecCtx.getChannel();
                final IBreakpoints breakpoints = channel.getRemoteService(IBreakpoints.class);
                if (breakpoints == null) {
                    error("Cannot set breakpoint.");
                    return;
                }
                final String contextId = fExecCtx.getID();
                Map<String, Object> properties = new HashMap<String, Object>();
                if (location.fFile != null) {
                    properties.put(IBreakpoints.PROP_FILE, location.fFile);
                    properties.put(IBreakpoints.PROP_LINE, location.fLine);
                } else {
                    properties.put(IBreakpoints.PROP_LOCATION, location.fAddress.toString());
                }
//                properties.put(IBreakpoints.PROP_CONTEXTIDS, new String[] { contextId });
                properties.put(IBreakpoints.PROP_ENABLED, Boolean.TRUE);
//                properties.put(IBreakpoints.PROP_TEMPORARY, Boolean.TRUE);
                final String breakpointId = "runtoline"+location.hashCode();
                properties.put(IBreakpoints.PROP_ID, breakpointId);
                breakpoints.add(properties, new IBreakpoints.DoneCommand() {
                    public void doneCommand(IToken token, Exception error) {
                        if (error != null) {
                            error(error);
                            return;
                        }
                        final Runnable removeBreakpoint = new Runnable() {
                            public void run() {
                                breakpoints.remove(new String[] { breakpointId }, new IBreakpoints.DoneCommand() {
                                    public void doneCommand(IToken token, Exception error) {
                                        // ignore errors?
                                    }
                                });
                            }
                        };
                        Runnable resume = new Runnable() {
                            public void run() {
                                final IRunControl runControl = channel.getRemoteService(IRunControl.class);
                                if (runControl == null) {
                                    error("Cannot resume.");
                                    removeBreakpoint.run();
                                    return;
                                }
                                if (!cache.validate(this)) {
                                    removeBreakpoint.run();
                                    return;
                                }
                                if (cache.getError() != null) {
                                    error(cache.getError());
                                    removeBreakpoint.run();
                                    return;
                                }
                                runControl.addListener(new RunControlListener() {
                                    boolean fRunning;
                                    public void contextSuspended(String context, String pc, String reason,
                                            Map<String, Object> params) {
                                        if (contextId.equals(context) && fRunning) {
                                            finished();
                                        }
                                    }
                                    private void finished() {
                                        runControl.removeListener(this);
                                        removeBreakpoint.run();
                                    }
                                    public void contextResumed(String context) {
                                        if (contextId.equals(context) && !fRunning) {
                                            fRunning = true;
                                        }
                                    }
                                    public void contextRemoved(String[] context_ids) {
                                        for (String context : context_ids) {
                                            if (contextId.equals(context)) {
                                                finished();
                                                return;
                                            }
                                        }
                                    }
                                    public void contextException(String context, String msg) {
                                    }
                                    public void contextChanged(RunControlContext[] contexts) {
                                    }
                                    public void contextAdded(RunControlContext[] contexts) {
                                    }
                                    public void containerSuspended(String context, String pc, String reason,
                                            Map<String, Object> params, String[] suspended_ids) {
                                        if (fRunning) {
                                            for (String context2 : suspended_ids) {
                                                if (contextId.equals(context2)) {
                                                    finished();
                                                }
                                            }
                                        }
                                    }
                                    public void containerResumed(String[] context_ids) {
                                        if (!fRunning) {
                                            for (String context2 : context_ids) {
                                                if (contextId.equals(context2)) {
                                                    fRunning = true;
                                                    return;
                                                }
                                            }
                                        }
                                    }
                                });
                                IRunControl.RunControlContext runCtx = cache.getData();
                                if (runCtx.canResume(IRunControl.RM_RESUME)) {
                                    runCtx.resume(IRunControl.RM_RESUME, 1, new IRunControl.DoneCommand() {
                                        public void doneCommand(IToken token, Exception error) {
                                            if (error != null) {
                                                error(error);
                                            } else {
                                                done(null);
                                            }
                                        }
                                    });
                                }
                            }
                        };
                        if (cache.validate(resume)) {
                            resume.run();
                        }
                    }
                });
            }
        }.getD();
    }

    private void moveToLocation(final Location location, final boolean resume) throws DebugException {
        new TCFDebugTask<Object>() {
            private RegistersContext fPCReg;
            public void run() {
                if (fExecCtx.isDisposed()) {
                    // ignore silently
                    done(null);
                    return;
                }

                LinkedList<TCFChildren> queue = new LinkedList<TCFChildren>();
                queue.add(fExecCtx.getRegisters());
                while (fPCReg == null && !queue.isEmpty()) {
                    TCFChildren regNodesCache = queue.removeFirst();
                    if (!regNodesCache.validate(this)) return;
                    Map<String,TCFNode> regNodes = regNodesCache.getData();
                    if (regNodes == null || regNodes.size() == 0) {
                        if (regNodesCache.getError() != null) error(regNodesCache.getError());
                        else error("Cannot retrive registers info");
                        return;
                    }

                    for (TCFNode node : regNodes.values()) {
                        TCFNodeRegister regNode = (TCFNodeRegister)node;
                        TCFDataCache<IRegisters.RegistersContext> regCtxCache = regNode.getContext();
                        if (!regCtxCache.validate(this)) return;
                        IRegisters.RegistersContext context = regCtxCache.getData();
                        if (context != null && IRegisters.ROLE_PC.equals(context.getRole())) {
                            fPCReg = context;
                            break;
                        }
                        queue.add(regNode.getChildren());
                    }
                }

                if (fPCReg == null) {
                    error("Cannot determine PC register.");
                    return;
                }

                if (location.fAddress == null) {
                    final IChannel channel = fExecCtx.getChannel();
                    final ILineNumbers lineNumbers = channel.getRemoteService(ILineNumbers.class);
                    if (lineNumbers == null) {
                        error("No line numbers service.");
                        return;
                    }
                    TCFDataCache<TCFNodeExecContext> memNodeCache = fExecCtx.getMemoryNode();
                    if (!memNodeCache.validate(this)) return;
                    TCFNodeExecContext memNode = memNodeCache.getData();
                    if (memNode == null) {
                        if (memNodeCache.getError() != null) error(memNodeCache.getError());
                        else error("Cannot determine memory context.");
                        return;
                    }
                    TCFDataCache<IMemory.MemoryContext> memCtxCache = memNode.getMemoryContext();
                    if (!memCtxCache.validate(this)) return;
                    final IMemory.MemoryContext memCtx = memCtxCache.getData();
                    if (memCtx == null) {
                        if (memNodeCache.getError() != null) error(memNodeCache.getError());
                        else error("Cannot determine memory context.");
                        return;
                    }
                    lineNumbers.mapToMemory(memCtx.getID(), location.fFile, location.fLine, 0, new ILineNumbers.DoneMapToMemory() {
                        public void doneMapToMemory(IToken token, Exception error, CodeArea[] areas) {
                            if (error != null) {
                                error(error);
                                return;
                            }
                            if (areas == null || areas.length == 0) {
                                error("Cannot map source location to address.");
                                return;
                            }
                            doneGetLocationAddress(areas[0].start_address);
                        }
                    });
                }
                else {
                    doneGetLocationAddress(location.fAddress);
                }
            }
            private void doneGetLocationAddress(Number address) {
                if (address == null) {
                    error("Cannot map source location to address.");
                    return;
                }
                byte[] value = addressToByteArray(address, fPCReg.getSize(), fPCReg.isBigEndian());
                fPCReg.set(value, new IRegisters.DoneSet() {
                    public void doneSet(IToken token, Exception error) {
                        if (error != null) {
                            error(error);
                            return;
                        }
                        fExecCtx.getModel().setDebugViewSelection(fExecCtx, "Move");
                        if (resume) {
                            final TCFDataCache<IRunControl.RunControlContext> cache = fExecCtx.getRunContext();
                            final IChannel channel = fExecCtx.getChannel();
                            Runnable resume = new Runnable() {
                                public void run() {
                                    final IRunControl runControl = channel.getRemoteService(IRunControl.class);
                                    if (runControl == null) {
                                        error("Cannot resume.");
                                        return;
                                    }
                                    if (!cache.validate(this)) {
                                        return;
                                    }
                                    if (cache.getError() != null) {
                                        error(cache.getError());
                                        return;
                                    }
                                    IRunControl.RunControlContext runCtx = cache.getData();
                                    if (runCtx.canResume(IRunControl.RM_RESUME)) {
                                        runCtx.resume(IRunControl.RM_RESUME, 1, new IRunControl.DoneCommand() {
                                            public void doneCommand(IToken token, Exception error) {
                                                if (error != null) {
                                                    error(error);
                                                } else {
                                                    done(null);
                                                }
                                            }
                                        });
                                    }
                                }
                            };
                            if (cache.validate(resume)) {
                                resume.run();
                            }
                        } else {
                            done(null);
                        }
                    }
                });
            }
            private byte[] addressToByteArray(Number address, int size, boolean bigEndian) {
                byte[] bytes = new byte[size];
                byte[] addrBytes = JSON.toBigInteger(address).toByteArray();
                for (int i=0; i < bytes.length; ++i) {
                    byte b = 0;
                    if (i < addrBytes.length) {
                        b = addrBytes[addrBytes.length - i - 1];
                    }
                    bytes[bigEndian ? size -i - 1 : i] = b;
                }
                return bytes;
            }
        }.getD();
    }
}

Back to the top