Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6779f8f0ba5cbd396fb6ee622950144b17b6bc73 (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
/*******************************************************************************
 * Copyright (c) 2012 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.debug.test;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;

import org.eclipse.debug.internal.ui.viewers.model.provisional.VirtualItem;
import org.eclipse.tcf.debug.test.services.IWaitForEventCache;
import org.eclipse.tcf.debug.test.services.RunControlCM;
import org.eclipse.tcf.debug.test.services.RunControlCM.ContextState;
import org.eclipse.tcf.debug.test.util.ICache;
import org.eclipse.tcf.debug.test.util.RangeCache;
import org.eclipse.tcf.debug.test.util.Transaction;
import org.eclipse.tcf.services.ILineNumbers.CodeArea;
import org.eclipse.tcf.services.IRunControl;
import org.eclipse.tcf.services.IRunControl.RunControlContext;
import org.eclipse.tcf.services.IStackTrace.StackTraceContext;
import org.eclipse.tcf.services.ISymbols;
import org.eclipse.tcf.services.ISymbols.Symbol;
import org.eclipse.test.performance.Performance;
import org.eclipse.test.performance.PerformanceMeter;
import org.junit.Assert;

@SuppressWarnings("restriction")
public class SampleTest extends AbstractTcfUITest {


    public void testDebugViewContent() throws Exception {
        initProcessModel("tcf_test_func0");

        VirtualItem launchItem = fDebugViewListener.findElement(new Pattern[] { Pattern.compile(".*" + fLaunch.getLaunchConfiguration().getName() + ".*") }  );
        Assert.assertTrue(launchItem != null);

        VirtualItem processItem = fDebugViewListener.findElement(launchItem, new Pattern[] { Pattern.compile(".*agent.*") }  );
        if (processItem == null) {
            /* Windows? */
            processItem = fDebugViewListener.findElement(launchItem, new Pattern[] { Pattern.compile("P[0-9]*") }  );
        }
        Assert.assertTrue(processItem != null);

        VirtualItem threadItem = fDebugViewListener.findElement(processItem, new Pattern[] { Pattern.compile(".*" + fThreadId + ".*") }  );
        Assert.assertTrue(threadItem != null);
        VirtualItem frameItem = fDebugViewListener.findElement(threadItem, new Pattern[] { Pattern.compile(".*tcf_test_func0.*")});
        Assert.assertTrue(frameItem != null);
    }

    public void testSteppingDebugViewOnly() throws Exception {
        initProcessModel("tcf_test_func0");

        // Execute step loop
        String previousThreadLabel = null;
        for (int stepNum = 0; stepNum < 100; stepNum++) {
            fDebugViewListener.reset();

            resumeAndWaitForSuspend(fThreadCtx, IRunControl.RM_STEP_INTO_LINE);

            fDebugViewListener.waitTillFinished(MODEL_CHANGED_COMPLETE | CONTENT_SEQUENCE_COMPLETE | LABEL_UPDATES_RUNNING);
            VirtualItem topFrameItem = fDebugViewListener.findElement(
                new Pattern[] { Pattern.compile(".*"), Pattern.compile(".*"), Pattern.compile(".*" + fProcessId + ".*\\(Step.*"), Pattern.compile(".*")});
            if (topFrameItem == null) {
                Assert.fail("Top stack frame not found. \n\nDebug view dump: \n:" + fDebugViewViewer.toString());
            }
            String topFrameLabel = ((String[])topFrameItem.getData(VirtualItem.LABEL_KEY))[0];
            Assert.assertTrue(!topFrameLabel.equals(previousThreadLabel));
            previousThreadLabel = topFrameLabel;
        }
    }

    public void testSteppingWithVariablesAndRegisters() throws Exception {
        fVariablesViewViewer.setActive(true);
        fRegistersViewViewer.setActive(true);

        initProcessModel("tcf_test_func0");

        // Execute step loop
        String previousThreadLabel = null;
        for (int stepNum = 0; stepNum < 100; stepNum++) {
            fDebugViewListener.reset();
            fVariablesViewListener.reset();
            fRegistersViewListener.reset();

            resumeAndWaitForSuspend(fThreadCtx, IRunControl.RM_STEP_INTO_LINE);

            fDebugViewListener.waitTillFinished(MODEL_CHANGED_COMPLETE | CONTENT_SEQUENCE_COMPLETE | LABEL_UPDATES_RUNNING);
            fVariablesViewListener.waitTillFinished(CONTENT_SEQUENCE_COMPLETE | LABEL_UPDATES_RUNNING);
            fRegistersViewListener.waitTillFinished(CONTENT_SEQUENCE_COMPLETE | LABEL_UPDATES_RUNNING);
            VirtualItem topFrameItem = fDebugViewListener.findElement(
                new Pattern[] { Pattern.compile(".*"), Pattern.compile(".*"), Pattern.compile(".*" + fProcessId + ".*\\(Step.*"), Pattern.compile(".*")});
            Assert.assertTrue(topFrameItem != null);
            String topFrameLabel = ((String[])topFrameItem.getData(VirtualItem.LABEL_KEY))[0];
            Assert.assertTrue(!topFrameLabel.equals(previousThreadLabel));
            previousThreadLabel = topFrameLabel;
        }
    }

    public void testSteppingPerformanceWithSourceDisplay() throws Exception {
        initProcessModel("tcf_test_func0");

        final Number sym_func0_address = new Transaction<Number>() {
            protected Number process() throws Transaction.InvalidCacheException ,ExecutionException {
                return validate( fDiagnosticsCM.getSymbol(fProcessId, "tcf_test_func0") ).getValue();
            };
        }.get();

        final Number sym_func3_address = new Transaction<Number>() {
            protected Number process() throws Transaction.InvalidCacheException ,ExecutionException {
                return validate( fDiagnosticsCM.getSymbol(fProcessId, "tcf_test_func3") ).getValue();
            };
        }.get();

        Performance perf = Performance.getDefault();
        PerformanceMeter meter = perf.createPerformanceMeter(perf.getDefaultScenarioId(this));

        try {
            // Execute step loop
            for (int stepNum = 0; stepNum < 100; stepNum++) {
                fDebugViewListener.reset();
                fSourceDisplayListener.reset();

                meter.start();

                ContextState state = resumeAndWaitForSuspend(fThreadCtx, IRunControl.RM_STEP_INTO_LINE);

                CodeArea area = calcPCCodeArea();
                if (area != null) {
                    fSourceDisplayListener.setCodeArea(calcPCCodeArea());
                }

                fDebugViewListener.waitTillFinished(MODEL_CHANGED_COMPLETE | CONTENT_SEQUENCE_COMPLETE | LABEL_UPDATES_RUNNING);
                if (area != null) {
                    fSourceDisplayListener.waitTillFinished();
                }

                meter.stop();

                if (new BigInteger(state.pc).equals(new BigInteger(sym_func3_address.toString()))) {
                    moveToLocation(fThreadId, sym_func0_address);
                }

            }

            meter.commit();
            perf.assertPerformance(meter);
        } finally {
            meter.dispose();
        }

    }


    private CodeArea calcPCCodeArea() throws ExecutionException, InterruptedException {
        return new Transaction<CodeArea>() {
            @Override
            protected CodeArea process() throws Transaction.InvalidCacheException ,ExecutionException {
                String pc = validate(fRunControlCM.getState(fThreadId)).pc;
                BigInteger pcNumber = new BigInteger(pc);
                BigInteger pcNumberPlusOne = pcNumber.add(BigInteger.valueOf(1));
                CodeArea[] areas = validate(fLineNumbersCM.mapToSource(fThreadId, pcNumber, pcNumberPlusOne));
                if (areas.length >= 1) {
                    return areas[0];
                }
                return null;
            }
        }.get();
    }

    public void testSymbolsCMResetOnContextRemove() throws Exception {
        initProcessModel("tcf_test_func0");

        // Retrieve the current PC for use later
        final String pc = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                return validate(fRunControlCM.getState(fThreadId)).pc;
            }
        }.get();

        // Find symbol by name and valide the cache.
        final String symbolId = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                String symId = validate( fSymbolsCM.find(fProcessId, new BigInteger(pc), "tcf_test_func0") );
                Symbol sym = validate( fSymbolsCM.getContext(symId) );
                Assert.assertEquals(ISymbols.UPDATE_ON_MEMORY_MAP_CHANGES, sym.getUpdatePolicy());
                return symId;
            }
        }.get();

        // Find symbol by address and validate its context.  Save address for later.
        final Number symAddr = new Transaction<Number>() {
            @Override
            protected Number process() throws InvalidCacheException, ExecutionException {
                Symbol sym = validate( fSymbolsCM.getContext(symbolId) );
                String symId2 = validate( fSymbolsCM.findByAddr(fProcessId, sym.getAddress()) );
                Symbol sym2 = validate( fSymbolsCM.getContext(symId2) );
                Assert.assertEquals(sym.getAddress(), sym2.getAddress());
                return sym.getAddress();
            }
        }.get();

        // End test, check that all caches were reset and now return an error.
        new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                validate( fDiagnosticsCM.cancelTest(fTestId, this) );
                validate( fRunControlCM.waitForContextRemoved(fProcessId, this) );
                try {
                    validate( fSymbolsCM.getContext(symbolId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate( fSymbolsCM.find(fProcessId, new BigInteger(pc), "tcf_test_func0") );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate( fSymbolsCM.findByAddr(fProcessId, symAddr) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}

                return null;
            }
        }.get();
    }

    public void testLineNumbersCMResetOnContextRemove() throws Exception {
        initProcessModel("tcf_test_func0");

        // Retrieve the current PC for use later
        final String pc = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                return validate(fRunControlCM.getState(fThreadId)).pc;
            }
        }.get();

        final BigInteger pcNumber = new BigInteger(pc);
        final BigInteger pcNumberPlusOne = pcNumber.add(BigInteger.valueOf(1));

        // Retrieve the line number for current PC.
        final CodeArea[] pcCodeAreas = new Transaction<CodeArea[]>() {
            @Override
            protected CodeArea[] process() throws InvalidCacheException, ExecutionException {
                CodeArea[] areas = validate(fLineNumbersCM.mapToSource(fProcessId, pcNumber, pcNumberPlusOne));
                Assert.assertNotNull(areas);
                Assert.assertTrue(areas.length != 0);

                areas = validate(fLineNumbersCM.mapToSource(fThreadId, pcNumber, pcNumberPlusOne));
                Assert.assertNotNull(areas);
                Assert.assertTrue(areas.length != 0);

                CodeArea[] areas2 = validate(fLineNumbersCM.mapToMemory(fProcessId, areas[0].file, areas[0].start_line, areas[0].start_column));
                Assert.assertNotNull(areas2);
                Assert.assertTrue(areas2.length != 0);

                areas2 = validate(fLineNumbersCM.mapToMemory(fThreadId, areas[0].file, areas[0].start_line, areas[0].start_column));
                Assert.assertNotNull(areas2);
                Assert.assertTrue(areas2.length != 0);

                return areas;
            }
        }.get();

        // End test, check that all caches were reset and now return an error.
        new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                validate( fDiagnosticsCM.cancelTest(fTestId, this) );
                validate( fRunControlCM.waitForContextRemoved(fProcessId, this) );
                try {
                    validate(fLineNumbersCM.mapToSource(fProcessId, pcNumber, pcNumberPlusOne));
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate(fLineNumbersCM.mapToSource(fThreadId, pcNumber, pcNumberPlusOne));
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    CodeArea[] areas3 = validate(fLineNumbersCM.mapToMemory(fProcessId, pcCodeAreas[0].file, pcCodeAreas[0].start_line, pcCodeAreas[0].start_column));
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate(fLineNumbersCM.mapToMemory(fThreadId, pcCodeAreas[0].file, pcCodeAreas[0].start_line, pcCodeAreas[0].start_column));
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}

                return null;
            }
        }.get();
    }


    public void testSymbolsCMResetOnContextStateChange() throws Exception {
        initProcessModel("tcf_test_func2");

        // Retrieve the current PC and top frame for use later
        final String pc = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                return validate(fRunControlCM.getState(fThreadId)).pc;
            }
        }.get();
        final String topFrameId = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                String[] frameIds = validate( fStackTraceCM.getChildren(fThreadId) );
                return frameIds[frameIds.length - 1];
            }
        }.get();

        // Find symbol by name and valide the cache.
        final String symbolId = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                String symId = validate( fSymbolsCM.find(topFrameId, new BigInteger(pc), "func2_local1") );
                Symbol sym = validate( fSymbolsCM.getContext(symId) );
                Assert.assertEquals(ISymbols.UPDATE_ON_EXE_STATE_CHANGES, sym.getUpdatePolicy());
                return symId;
            }
        }.get();

        // Note: findByAddr doesn't seem to work on a local var.
//        // Find symbol by address and validate its context.  Save address for later.
//        final Number symAddr = new Transaction<Number>() {
//            @Override
//            protected Number process() throws InvalidCacheException, ExecutionException {
//                Symbol sym = validate( fSymbolsCM.getContext(symbolId) );
//                String symId2 = validate( fSymbolsCM.findByAddr(topFrameId, sym.getAddress()) );
//                Symbol sym2 = validate( fSymbolsCM.getContext(symId2) );
//                Assert.assertEquals(sym.getAddress(), sym2.getAddress());
//                return sym.getAddress();
//            }
//        }.get();

        // Execute a step.
        resumeAndWaitForSuspend(fThreadCtx, IRunControl.RM_STEP_OUT);

        // End test, check that all caches were reset and now return an error.
        new Transaction<Object>() {
            @Override
            protected Object process() throws InvalidCacheException, ExecutionException {
                Assert.assertFalse(
                    "Expected cache to be reset",
                    fSymbolsCM.getContext(symbolId).isValid());
                Assert.assertFalse(
                    "Expected cache to be reset",
                    fSymbolsCM.find(topFrameId, new BigInteger(pc), "func2_local1").isValid() );
                return null;
            }
        }.get();
    }

    public void testStackTraceCMResetOnContextStateChange() throws Exception {
        initProcessModel("tcf_test_func2");

        // Retrieve the current PC and top frame for use later
        final String pc = new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                return validate(fRunControlCM.getState(fThreadId)).pc;
            }
        }.get();

        // Retrieve data from caches (make them valid).
        new Transaction<Object>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                String[] frameIds = validate( fStackTraceCM.getChildren(fThreadId) );
                validate (fStackTraceCM.getContexts(frameIds));
                RangeCache<StackTraceContext> framesRange = fStackTraceCM.getContextRange(fThreadId);
                validate( framesRange.getRange(0, frameIds.length) );
                return null;
            }
        }.get();


        // Execute a step.
        resumeAndWaitForSuspend(fThreadCtx, IRunControl.RM_STEP_OUT);

        // End test, check that all caches were reset and now return an error.
        new Transaction<Object>() {
            @Override
            protected Object process() throws InvalidCacheException, ExecutionException {
                ICache<String[]> frameIdsCache = fStackTraceCM.getChildren(fThreadId);
                Assert.assertFalse("Expected cache to be reset", frameIdsCache.isValid());
                return null;
            }
        }.get();

        new Transaction<Object>() {
            @Override
            protected Object process() throws InvalidCacheException, ExecutionException {
                String[] frameIds = validate( fStackTraceCM.getChildren(fThreadId) );
                ICache<StackTraceContext[]> cache = fStackTraceCM.getContexts(frameIds);
                Assert.assertFalse("Expected cache to be reset", cache.isValid());

                RangeCache<StackTraceContext> framesRange = fStackTraceCM.getContextRange(fThreadId);
                ICache<List<StackTraceContext>> framesRangeCache = framesRange.getRange(0, frameIds.length);
                Assert.assertFalse("Expected cache to be reset", framesRangeCache.isValid());

                return null;
            }
        }.get();
    }

    public void testRunControlCMChildrenInvalidation() throws Exception {
        initProcessModel("tcf_test_func0");

        createBreakpoint("testRunControlCMChildrenInvalidation", "tcf_test_func0");

        // Wait for each threads to start.
        final String[] threads = new Transaction<String[]>() {
            List<String> fThreads = new ArrayList<String>();
            @Override
            protected String[] process() throws InvalidCacheException, ExecutionException {
                IWaitForEventCache<RunControlContext[]> waitCache = fRunControlCM.waitForContextAdded(fProcessId, this);
                validate(fRunControlCM.resume(fTestCtx, this, IRunControl.RM_RESUME, 1));
                RunControlContext[] addedContexts = validate(waitCache);
                for (RunControlContext addedContext : addedContexts) {
                    fThreads.add(addedContext.getID());
                }
                if (fThreads.size() < 4) {
                    waitCache.reset();
                    validate(waitCache);
                }
                // Validate children cache
                String[] children = validate (fRunControlCM.getChildren(fProcessId));
                Assert.assertTrue(
                    "Expected children array to contain added ids",
                    Arrays.asList(children).containsAll(fThreads));

                return fThreads.toArray(new String[fThreads.size()]);
            }
        }.get();

        // Wait for each thread to suspend, update caches
        for (final String thread : threads) {
            new Transaction<Object>() {
                @Override
                protected Object process() throws InvalidCacheException, ExecutionException {
                    RunControlCM.ContextState state = validate(fRunControlCM.getState(thread));
                    if (!state.suspended) {
                        validate( fRunControlCM.waitForContextSuspended(thread, this) );
                    }
                    String symId = validate( fSymbolsCM.find(thread, new BigInteger(state.pc), "tcf_test_func0") );
                    Number symAddr = validate( fSymbolsCM.getContext(symId) ).getAddress();
                    Assert.assertEquals("Expected thread to suspend at breakpoint address", symAddr.toString(), state.pc);
                    String[] children = validate( fRunControlCM.getChildren(thread));
                    Assert.assertEquals("Expected thread to have no children contexts", 0, children.length);
                    return null;
                }
            }.get();
        }

        // End test, check for remvoed events and that state caches were cleared
        new Transaction<String>() {
            @Override
            protected String process() throws InvalidCacheException, ExecutionException {
                // Create wait caches
                fRunControlCM.waitForContextRemoved(fProcessId, this);
                IWaitForEventCache<?>[] waitCaches = new IWaitForEventCache<?>[threads.length];
                for (int i = 0; i < threads.length; i++) {
                    waitCaches[i] = fRunControlCM.waitForContextRemoved(threads[i], this);
                }
                validate( fDiagnosticsCM.cancelTest(fTestId, this) );
                validate(waitCaches);
                validate(fRunControlCM.waitForContextRemoved(fProcessId, this));

                try {
                    validate( fRunControlCM.getContext(fProcessId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    validate( fRunControlCM.getState(fProcessId) );
                    Assert.fail("Expected error");
                } catch (ExecutionException e) {}
                try {
                    String children[] = validate( fRunControlCM.getChildren(fProcessId) );
                    Assert.assertEquals("Expected no children", 0, children.length);
                } catch (ExecutionException e) {}

                for (String thread : threads) {
                    try {
                        validate( fRunControlCM.getContext(thread) );
                        Assert.fail("Expected error");
                    } catch (ExecutionException e) {}
                    try {
                        validate( fRunControlCM.getState(thread) );
                        Assert.fail("Expected error");
                    } catch (ExecutionException e) {}
                    try {
                        String children[] = validate( fRunControlCM.getChildren(fProcessId) );
                        Assert.assertEquals("Expected no children", 0, children.length);
                    } catch (ExecutionException e) {}
                }

                return null;
            }
        }.get();

        removeBreakpoint("testRunControlCMChildrenInvalidation");

    }

}

Back to the top