Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e10870f555160920693a17e06e567a188db3e0af (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
/*******************************************************************************
 * Copyright (c) 2013, 2018 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.debug.examples.internal.memory.engine;

import java.math.BigInteger;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.examples.internal.memory.core.SampleDebugTarget;
import org.eclipse.debug.examples.internal.memory.core.SampleMemoryBlock;
import org.eclipse.debug.examples.internal.memory.core.SampleStackFrame;
import org.eclipse.debug.examples.internal.memory.core.SampleThread;

/**
 * Sample engine for sample deug adapter This engine randomly generates content
 * for a memory block. To get to this engine, call
 * {@link SampleDebugTarget#getEngine()};
 */
public class SampleEngine {

	Random fRandom = new Random();
	byte[] fMemory;
	Hashtable<BigInteger, SampleMemoryUnit> memoryBlockTable;
	Hashtable<String, BigInteger> expressionAddressTable = new Hashtable<>();
	Hashtable<SampleDebugTarget, Object> threadTable = new Hashtable<>();
	Hashtable<SampleThread, Object> stackframeTable = new Hashtable<>();

	Random random = new Random();

	/**
	 * Allow debug adapters to get memory from an address
	 *
	 * @param address
	 * @param length
	 * @return memory byte from an address
	 * @throws RuntimeException
	 */
	synchronized public MemoryByte[] getBytesFromAddress(BigInteger address, long length) throws RuntimeException {

		if (memoryBlockTable == null) {
			// create new memoryBlock table
			memoryBlockTable = new Hashtable<>();
			byte[] bytes = new byte[(int) length * getAddressableSize()];
			BigInteger addressKey = address;

			random.nextBytes(bytes);

			for (int i = 0; i < bytes.length; i = i + getAddressableSize()) {
				addressKey = addressKey.add(BigInteger.valueOf(1));

				MemoryByte[] byteUnit = new MemoryByte[getAddressableSize()];
				for (int j = 0; j < getAddressableSize(); j++) {
					MemoryByte oneByte = new MemoryByte(bytes[i + j]);
					oneByte.setBigEndian(isBigEndian(addressKey));
					oneByte.setWritable(isWritable(addressKey));
					oneByte.setReadable(isReadable(addressKey));
					byteUnit[j] = oneByte;
				}
				SampleMemoryUnit unit = new SampleMemoryUnit(byteUnit);
				memoryBlockTable.put(addressKey, unit);
			}
		}

		MemoryByte[] returnBytes = new MemoryByte[(int) length * getAddressableSize()];
		BigInteger addressKey;

		for (int i = 0; i < returnBytes.length; i = i + getAddressableSize()) {
			addressKey = address.add(BigInteger.valueOf(i / getAddressableSize()));
			SampleMemoryUnit temp = (memoryBlockTable.get(addressKey));

			// if memoryBlock does not already exist in the table, generate a
			// value
			if (temp == null) {
				byte[] x = new byte[getAddressableSize()];
				random.nextBytes(x);
				byte flag = 0;
				flag |= MemoryByte.READABLE;
				flag |= MemoryByte.ENDIANESS_KNOWN;
				flag |= MemoryByte.WRITABLE;

				MemoryByte[] byteUnit = new MemoryByte[getAddressableSize()];
				for (int j = 0; j < getAddressableSize(); j++) {
					MemoryByte oneByte = new MemoryByte(x[j], flag);
					byteUnit[j] = oneByte;
					byteUnit[j].setBigEndian(isBigEndian(addressKey));
					byteUnit[j].setWritable(isWritable(addressKey));
					byteUnit[j].setReadable(isReadable(addressKey));
					returnBytes[i + j] = oneByte;
				}
				SampleMemoryUnit unit = new SampleMemoryUnit(byteUnit);
				memoryBlockTable.put(addressKey, unit);

			} else {
				MemoryByte[] bytes = temp.getBytes();

				for (int j = 0; j < bytes.length; j++) {
					MemoryByte oneByte = new MemoryByte(bytes[j].getValue(), bytes[j].getFlags());
					returnBytes[i + j] = oneByte;
					returnBytes[i + j].setBigEndian(isBigEndian(addressKey));
					returnBytes[i + j].setWritable(isWritable(addressKey));
				}
			}
		}

		return returnBytes;
	}

	/**
	 * Run the debuggee
	 */
	public void resume() {
		changeValue();
	}

	/**
	 * Convenience function to cause changes in a memoryBlock block. Changes
	 * could result from running the program, changing a variable, etc.
	 */
	synchronized public void changeValue() {
		if (memoryBlockTable == null) {
			return;
		}

		Enumeration<BigInteger> enumeration = memoryBlockTable.keys();
		long randomChange = random.nextInt(37);

		while (randomChange <= 5) {
			randomChange = random.nextInt(37);
		}

		while (enumeration.hasMoreElements()) {
			BigInteger key = enumeration.nextElement();
			if (key.remainder(BigInteger.valueOf(randomChange)).equals(BigInteger.valueOf(0))) {
				byte[] x = new byte[getAddressableSize()];
				random.nextBytes(x);

				MemoryByte unitBytes[] = new MemoryByte[getAddressableSize()];
				for (int i = 0; i < x.length; i++) {
					MemoryByte oneByte = new MemoryByte();
					oneByte.setValue(x[i]);
					oneByte.setReadable(true);
					oneByte.setChanged(true);
					oneByte.setHistoryKnown(true);
					oneByte.setBigEndian(isBigEndian(key));
					oneByte.setWritable(isWritable(key));
					oneByte.setReadable(isReadable(key));
					unitBytes[i] = oneByte;
				}

				SampleMemoryUnit unit = new SampleMemoryUnit(unitBytes);

				memoryBlockTable.put(key, unit);
			} else {
				SampleMemoryUnit unit = memoryBlockTable.get(key);

				MemoryByte[] bytes = unit.getBytes();

				for (int i = 0; i < bytes.length; i++) {
					bytes[i].setChanged(false);
					bytes[i].setHistoryKnown(true);
				}

				unit.setBytes(bytes);

				memoryBlockTable.put(key, unit);
			}
		}
	}

	/**
	 * Simulates evaluation of an expression. Given an expression, return ad
	 * address
	 *
	 * @param expression
	 * @param evalContext
	 * @return the address the expression is evaluated to
	 */
	public BigInteger evaluateExpression(String expression, Object evalContext) {
		BigInteger expAddress = expressionAddressTable.get(expression);
		if (expAddress == null) {
			int address = random.nextInt();

			// make sure number is positive
			if (address < 0) {
				address = address * -1;
			}

			expAddress = BigInteger.valueOf(address);
			expressionAddressTable.put(expression, expAddress);
		}
		return expAddress;
	}

	/**
	 * Simulates checking if storage retrieval is supported
	 *
	 * @return if the engine supports storage retrieval
	 */
	public boolean supportsStorageRetrieval() {
		return true;
	}

	/**
	 * Simulates modifying memory using BigInteger as the address
	 *
	 * @param address
	 * @param bytes
	 * @throws RuntimeException
	 */
	public void setValue(BigInteger address, byte[] bytes) throws RuntimeException {
		BigInteger convertedAddress = address;

		for (int i = 0; i < bytes.length; i = i + getAddressableSize()) {
			SampleMemoryUnit unit = memoryBlockTable.get(convertedAddress);

			MemoryByte[] unitBytes = unit.getBytes();
			for (int j = 0; j < unitBytes.length; j++) {
				unitBytes[j].setValue(bytes[i + j]);
				unitBytes[j].setChanged(true);
				unitBytes[j].setHistoryKnown(true);
			}
			convertedAddress = convertedAddress.add(BigInteger.valueOf(1));
		}
	}

	/**
	 * @return addrssablesize of the debuggee
	 */
	public int getAddressableSize() {
		return 1;
	}

	/**
	 * @param address
	 * @return true if the debuggee is big endian, false otherwise
	 */
	public boolean isBigEndian(BigInteger address) {
		// simulate mixed endianess in a memory block
		// memory before the boundary address is little endian
		// memory after the boundaress is big endian
		BigInteger boundary = new BigInteger("12345678", 16); //$NON-NLS-1$
		if (address.compareTo(boundary) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * @param address
	 * @return true if the address is writable, false otherwise Read only
	 *         segment: 0xab123456 to 0xab123556
	 */
	public boolean isWritable(BigInteger address) {
		BigInteger boundary = new BigInteger("ab123456", 16); //$NON-NLS-1$
		BigInteger boundaryEnd = new BigInteger("ab123556", 16); //$NON-NLS-1$
		if (address.compareTo(boundary) > 0 && address.compareTo(boundaryEnd) < 0) {
			return false;
		}

		boundary = new BigInteger("cd123456", 16); //$NON-NLS-1$
		boundaryEnd = new BigInteger("cd123576", 16); //$NON-NLS-1$
		if (address.compareTo(boundary) > 0 && address.compareTo(boundaryEnd) < 0) {
			return false;
		}

		return true;

	}

	/**
	 * @param address
	 * @return
	 */
	public boolean isReadable(BigInteger address) {
		BigInteger boundary = new BigInteger("cd123456", 16); //$NON-NLS-1$
		BigInteger boundaryEnd = new BigInteger("cd123576", 16); //$NON-NLS-1$
		if (address.compareTo(boundary) > 0 && address.compareTo(boundaryEnd) < 0) {
			return false;
		}
		return true;
	}

	/**
	 * @param target
	 * @return
	 */
	public SampleThread[] getThreads(SampleDebugTarget target) {
		Object thread = threadTable.get(target);
		if (thread == null) {
			thread = new SampleThread(target);
			threadTable.put(target, thread);
		}
		return new SampleThread[] { (SampleThread) thread };
	}

	/**
	 * @param thread
	 * @return
	 */
	public SampleStackFrame[] getStackframes(SampleThread thread) {
		Object stackframes = stackframeTable.get(thread);
		if (stackframes == null) {
			stackframes = createStackframes(thread);
			stackframeTable.put(thread, stackframes);
		}
		return (SampleStackFrame[]) stackframes;
	}

	/**
	 *
	 */
	private SampleStackFrame[] createStackframes(SampleThread thread) {
		SampleStackFrame[] stackframes = new SampleStackFrame[2];
		stackframes[0] = new SampleStackFrame(thread, "Frame1"); //$NON-NLS-1$
		stackframes[1] = new SampleStackFrame(thread, "Frame2"); //$NON-NLS-1$
		return stackframes;
	}

	/**
	 * @param mb
	 * @return true if memory block is to support base address modification,
	 *         false otherwise
	 */
	public boolean suppostsBaseAddressModification(SampleMemoryBlock mb) {
		return false;
	}

	/**
	 * Sets the base address of this memory block
	 *
	 * @param mb the memory block to change base address
	 * @param address the new base address of the memory block
	 * @throws CoreException
	 */
	public void setBaseAddress(SampleMemoryBlock mb, BigInteger address) throws CoreException {
	}

	/**
	 * @param mb
	 * @return true if this memory block supports value modification, false
	 *         otherwise
	 * @throws CoreException
	 */
	public boolean supportsValueModification(SampleMemoryBlock mb) {
		return true;
	}

	/**
	 * @return address size of the debuggee
	 * @throws CoreException
	 */
	public int getAddressSize() throws CoreException {
		return 4;
	}
}

Back to the top