blob: fa7033cd9a64c405cab6317611afca450e95b034 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009, 2010 Nokia 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:
* Nokia - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.debug.edc.debugger.tests;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.eclipse.cdt.debug.edc.internal.EDCDebugger;
import org.eclipse.cdt.debug.edc.services.EDCServicesTracker;
import org.eclipse.cdt.debug.edc.services.IFrameRegisters;
import org.eclipse.cdt.debug.edc.services.Stack;
import org.eclipse.cdt.debug.edc.tests.TestUtils;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.debug.service.IRunControl2;
import org.eclipse.cdt.dsf.debug.service.IStack;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
public class StackService {
final static private String dbg_derived_types_cpp
= "C:\\myprog\\BlackFlagWascana\\src\\dbg_derived_types.cpp";
static final int sLineNumber = 57; // line in above source file.
private static EDCServicesTracker edcTracker;
private static Stack stackService;
private static class StackServiceLaunch extends BaseLaunchTest {
@Override
protected String getExeFileName() {
// This is the executable built by Cygwin gcc 3.4.4
// All source files are built from this folder:
// "C:\\myprog\\BlackFlagWascana\\src\\"
// Note we don't need any source file to perform the test.
//
return "BlackFlagMinGW_NoHardcodedBreak.exe";
}
@Override
protected boolean getStopAtMain() {
return true;
}
@Override
protected IStack getStackService() {
if (stackService == null) {
EDCServicesTracker edcTracker
= new EDCServicesTracker(EDCDebugger.getBundleContext(), session.getId());
stackService = edcTracker.getService(Stack.class);
Assert.assertNotNull(stackService);
}
return stackService;
}
}
private static StackServiceLaunch testLaunch;
@BeforeClass
public static void launch() throws Exception {
testLaunch = new StackServiceLaunch();
testLaunch.basicLaunch();
edcTracker
= new EDCServicesTracker(EDCDebugger.getBundleContext(), testLaunch.session.getId());
stackService = edcTracker.getService(Stack.class);
Assert.assertNotNull(stackService);
}
@AfterClass
public static void shutdown() {
TestUtils.shutdownDebugSession(testLaunch.launch, testLaunch.session);
testLaunch = null;
}
@Test
/**
* comments in Stack.getArguments() says it's never
* called and that Dsf expects args in with locals,
* so this is a "coverage" test only (and will cover
* the case where getArguments() starts returning
* something.
*/
public void coverageGetArguments() throws Exception {
Query<IStack.IVariableDMContext[]> query = new Query<IStack.IVariableDMContext[]>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IVariableDMContext[]> drm) {
stackService.getArguments(testLaunch.frame, drm);
}
};
testLaunch.session.getExecutor().execute(query);
IStack.IVariableDMContext[] argsShouldBeNull = query.get(5, TimeUnit.SECONDS);
Assert.assertTrue(query.isDone());
Assert.assertNull(argsShouldBeNull);
}
@Test
public void testGetFrameData() throws Exception {
Query<IStack.IFrameDMData> query = new Query<IStack.IFrameDMData>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IFrameDMData> drm) {
stackService.getModelData(testLaunch.frame, drm);
}
};
testLaunch.session.getExecutor().execute(query);
IStack.IFrameDMData frameData = query.get(2, TimeUnit.SECONDS);
Assert.assertTrue(query.isDone());
Assert.assertNotNull(frameData);
Assert.assertTrue(frameData instanceof Stack.StackFrameData);
Stack.StackFrameData stackData = (Stack.StackFrameData)frameData;
Assert.assertEquals(0, stackData.getLevel());
query = new Query<IStack.IFrameDMData>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IFrameDMData> drm) {
stackService.getFrameData(testLaunch.frame, drm);
}
};
testLaunch.session.getExecutor().execute(query);
frameData = query.get(2, TimeUnit.SECONDS);
Assert.assertTrue(query.isDone());
Assert.assertTrue(frameData instanceof Stack.StackFrameData);
Assert.assertTrue(stackData.equals(frameData));
}
@Test
public void testAlwaysFailingFrameRegisters() {
Stack.AlwaysFailingFrameRegisters regs
= new Stack.AlwaysFailingFrameRegisters(new CoreException(Status.OK_STATUS));
Assert.assertNotNull(regs);
try {
regs.getRegister(0, 0);
Assert.fail("AlwaysFailingFrameRegisters.readRegister() did not throw exception; should have thrown CoreException");
} catch (CoreException ce) {
Assert.assertSame(Status.OK_STATUS, ce.getStatus());
} catch (Exception e) {
Assert.fail("AlwaysFailingFrameRegisters.readRegister() should have thrown CoreException;"
+ " instead threw exception with message" + e.getMessage());
}
try {
regs.writeRegister(0, 0, null);
Assert.fail("AlwaysFailingFrameRegisters.writeRegister() should have thrown CoreException");
} catch (CoreException ce) {
Assert.assertSame("Expecting CoreException with OK_STATUS", Status.OK_STATUS, ce.getStatus());
} catch (Exception e) {
Assert.fail("AlwaysFailingFrameRegisters.writeRegister() should have thrown CoreException;"
+ " instead threw exception with message" + e.getMessage());
}
}
@Test
public void testPreservedFrameRegisters () {
IStack.IFrameDMContext[] frames;
try {
frames = stackService.getFramesForDMC(testLaunch.threadDMC, 0, IStack.ALL_FRAMES);
Assert.assertNotNull(frames);
Assert.assertEquals("Checking number of frames", 4, frames.length);
Assert.assertTrue("Expected frame[0] would be instanceof StackFrameDMC",
frames[0] instanceof Stack.StackFrameDMC);
Assert.assertTrue("Expected frame[3] would be instanceof StackFrameDMC",
frames[3] instanceof Stack.StackFrameDMC);
Stack.StackFrameDMC frame0 = (Stack.StackFrameDMC)frames[0];
Stack.StackFrameDMC frame3 = (Stack.StackFrameDMC)frames[3];
Assert.assertEquals("Expected frame0 would be equal testLaunch.frame",
0, frame0.compareTo((Stack.StackFrameDMC)testLaunch.frame));
Assert.assertEquals("Expected frame0 would be 'greater than' testLaunch.frame",
1, frame3.compareTo((Stack.StackFrameDMC)testLaunch.frame));
IFrameRegisters frame3Regs = frame3.getFrameRegisters();
Assert.assertTrue("Expected frame1 regs would be instanceof PreservedFrameRegisters",
frame3Regs instanceof Stack.PreservedFrameRegisters);
BigInteger r5 = frame3Regs.getRegister(5, 4);
frame3Regs.writeRegister(5, 4, r5.add(BigInteger.ONE));
Thread.sleep(400); // allow write to memory to finish
Assert.assertEquals(r5.intValue()+1, frame3Regs.getRegister(5, 4).intValue());
frame3Regs.writeRegister(5, 4, r5); // put it back the way it was
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
// @Test
// public void testStackFrameDMCFindEnumeratorByName () {
// // TODO no enumerators in this stack
// // need to create an example with IEnumerators
// }
@Test
public void testVariableData () throws Exception {
testLaunch.waitRunToLine(edcTracker.getService(IRunControl2.class),
dbg_derived_types_cpp, sLineNumber, 800); // 800ms to wait for run
TestUtils.waitForUIUpdate(1500); // 1500ms to wait for UI update
Assert.assertTrue(testLaunch.frame instanceof Stack.StackFrameDMC);
Query<IStack.IVariableDMContext[]> localsQuery = new Query<IStack.IVariableDMContext[]>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IVariableDMContext[]> drm) {
stackService.getLocals(testLaunch.frame, drm);
}
};
testLaunch.session.getExecutor().execute(localsQuery);
final IStack.IVariableDMContext[] locals = localsQuery.get(5, TimeUnit.SECONDS);
Assert.assertTrue(localsQuery.isDone());
Assert.assertEquals(3, locals.length);
Query<IStack.IVariableDMData> query = new Query<IStack.IVariableDMData>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IVariableDMData> drm) {
stackService.getVariableData(locals[0], drm);
}
};
testLaunch.session.getExecutor().execute(query);
IStack.IVariableDMData varData = query.get(2, TimeUnit.SECONDS);
Assert.assertTrue(query.isDone());
Assert.assertTrue(varData instanceof Stack.VariableData);
Stack.VariableData var0 = (Stack.VariableData)varData;
Assert.assertEquals("i", var0.getName());
// TODO getValue() currently returns null
// if/when it is finally impelemnted, uncomment this
// and remove the Assert.assertNull() on the following line
// Assert.assertEquals("0", var0.getValue());
Assert.assertNull("Stack.VariableData.getValue() may now be properly implemented",
var0.getValue());
query = new Query<IStack.IVariableDMData>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IVariableDMData> drm) {
stackService.getVariableData(locals[1], drm);
}
};
testLaunch.session.getExecutor().execute(query);
varData = query.get(2, TimeUnit.SECONDS);
Assert.assertTrue(query.isDone());
Assert.assertTrue(varData instanceof Stack.VariableData);
Stack.VariableData var1 = (Stack.VariableData)varData;
Assert.assertEquals("lstruct", var1.getName());
// TODO getValue() currently returns null
// if/when it is finally impelemnted, uncomment this
// and remove the Assert.assertNull() on the following line
// Assert.assertEquals("0x22feb0", var1.getValue());
Assert.assertNull("Stack.VariableData.getValue() may now be properly implemented",
var1.getValue());
query = new Query<IStack.IVariableDMData>() {
@Override
protected void execute(final DataRequestMonitor<IStack.IVariableDMData> drm) {
stackService.getVariableData(locals[2], drm);
}
};
testLaunch.session.getExecutor().execute(query);
varData = query.get(2, TimeUnit.SECONDS);
Assert.assertTrue(query.isDone());
Assert.assertTrue(varData instanceof Stack.VariableData);
Stack.VariableData var2 = (Stack.VariableData)varData;
Assert.assertEquals("lnested_struct", var2.getName());
// TODO getValue() currently returns null
// if/when it is finally impelemnted, uncomment this
// and remove the Assert.assertNull() on the following line
// Assert.assertEquals("0x22fa60", var2.getValue());
Assert.assertNull("Stack.VariableData.getValue() may now be properly implemented",
var2.getValue());
}
}