Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cfcb6ac0e9a28826e792c8816d9e52c4e3c2157b (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
/*******************************************************************************
 * Copyright (c) 2013, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.examples.internal.memory.core;

import java.math.BigInteger;
import java.util.ArrayList;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.DebugElement;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.examples.internal.memory.MemoryViewSamplePlugin;
import org.eclipse.debug.examples.internal.memory.launchconfig.SampleModelPresentation;
import org.eclipse.jface.viewers.IColorProvider;

/**
 * Memory Block Implementation
 *
 */
public class SampleMemoryBlock extends DebugElement implements IMemoryBlockExtension {

	private String fExpression;
	private SampleDebugTarget fDebugTarget;

	private boolean isEnabled = true;
	private BigInteger fBaseAddress;

	private ArrayList<Object> fConnections = new ArrayList<>();

	/**
	 * Creates memory block
	 *
	 * @param debugTarget
	 * @param expression
	 * @param address
	 */
	public SampleMemoryBlock(SampleDebugTarget debugTarget, String expression, BigInteger address) {
		super(debugTarget);
		fDebugTarget = debugTarget;
		fExpression = expression;
		fBaseAddress = address;
	}

	@Override
	public BigInteger getBigBaseAddress() throws DebugException {
		fBaseAddress = fDebugTarget.getEngine().evaluateExpression(fExpression, null);
		return fBaseAddress;
	}

	@Override
	public boolean supportBaseAddressModification() throws DebugException {
		return fDebugTarget.getEngine().suppostsBaseAddressModification(this);
	}

	@Override
	public void setBaseAddress(BigInteger address) throws DebugException {
		try {
			fDebugTarget.getEngine().setBaseAddress(this, address);
		} catch (CoreException e) {
			throw new DebugException(e.getStatus());
		}
	}

	@Override
	synchronized public MemoryByte[] getBytesFromOffset(BigInteger offset, long length) throws DebugException {
		BigInteger address = fBaseAddress.subtract(offset);
		return getBytesFromAddress(address, length);
	}

	@Override
	public MemoryByte[] getBytesFromAddress(BigInteger address, long length) throws DebugException {

		try {
			MemoryByte[] bytes = new MemoryByte[(int) length * fDebugTarget.getEngine().getAddressableSize()];
			BigInteger addressCnt = address;
			int lengthCnt = (int) length;
			int i = 0;

			// asks engine to get bytes from address
			MemoryByte[] engineBytes = fDebugTarget.getEngine().getBytesFromAddress(addressCnt, lengthCnt);
			System.arraycopy(engineBytes, 0, bytes, i, engineBytes.length);

			// if engine did not return enough memory, pad with dummy memory
			for (int j = i + engineBytes.length; j < bytes.length; j++) {
				MemoryByte mb = new MemoryByte((byte) 0);
				mb.setReadable(false);
				mb.setWritable(false);
				mb.setBigEndian(fDebugTarget.getEngine().isBigEndian(address.add(BigInteger.valueOf(j))));
				bytes[j] = mb;
			}

			return bytes;
		} catch (RuntimeException e) {
			throw e;
		}
	}

	@Override
	public void connect(Object object) {

		if (!fConnections.contains(object)) {
			fConnections.add(object);
		}

		if (fConnections.size() == 1) {
			enable();
		}
	}

	/**
	 * Enable this memory block
	 */
	private void enable() {
		isEnabled = true;
	}

	@Override
	public void disconnect(Object object) {

		if (fConnections.contains(object)) {
			fConnections.remove(object);
		}

		if (fConnections.size() == 0) {
			disable();
		}
	}

	@Override
	public Object[] getConnections() {
		return fConnections.toArray();
	}

	/**
	 * Disable this memory block
	 */
	private void disable() {
		isEnabled = false;
	}

	@Override
	public long getStartAddress() {
		// no need to implement this method as it belongs to IMemoryBlock
		return 0;
	}

	@Override
	public long getLength() {
		// no need to implement this method as it belongs to IMemoryBlock
		return 0;
	}

	@Override
	public byte[] getBytes() throws DebugException {
		// no need to implement this method as it belongs to IMemoryBlock
		return new byte[0];
	}

	@Override
	public boolean supportsValueModification() {
		return fDebugTarget.getEngine().supportsValueModification(this);
	}

	@Override
	public void setValue(BigInteger offset, byte[] bytes) throws DebugException {
		try {
			// ask the engine to modify memory at specified address
			fDebugTarget.getEngine().setValue(fBaseAddress.add(offset), bytes);
			fireContentChangeEvent();
		} catch (RuntimeException e) {
			IStatus status = new Status(IStatus.ERROR, MemoryViewSamplePlugin.PLUGIN_ID, 0, Messages.SampleMemoryBlock_0, e);
			DebugException exception = new DebugException(status);
			throw exception;
		}
	}

	@Override
	public String getModelIdentifier() {
		return getDebugTarget().getModelIdentifier();
	}

	@Override
	public IDebugTarget getDebugTarget() {
		return fDebugTarget;
	}

	@Override
	public ILaunch getLaunch() {
		return fDebugTarget.getLaunch();
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Class<T> adapter) {

		if (adapter.equals(IMemoryBlockRetrievalExtension.class)) {
			return (T) getDebugTarget();
		}

		if (adapter == IColorProvider.class) {
			return (T) SampleModelPresentation.getSampleModelPresentation();
		}

		return super.getAdapter(adapter);
	}

	@Override
	public String getExpression() {
		return fExpression;
	}

	@Override
	public void dispose() throws DebugException {
		// remove this memory block from debug target
		fDebugTarget.removeMemoryBlock(this);
	}

	/**
	 * @return is enabled
	 */
	public boolean isEnabled() {
		return isEnabled;
	}

	@Override
	public IMemoryBlockRetrieval getMemoryBlockRetrieval() {
		return getDebugTarget();
	}

	private void fireContentChangeEvent() {
		DebugEvent evt = new DebugEvent(this, DebugEvent.CHANGE);
		fireEvent(evt);
	}

	@Override
	public boolean supportsChangeManagement() {
		return false;
	}

	@Override
	public int getAddressableSize() throws DebugException {
		return fDebugTarget.getEngine().getAddressableSize();
	}

	@Override
	public int getAddressSize() throws DebugException {
		try {
			return fDebugTarget.getEngine().getAddressSize();
		} catch (CoreException e) {
			throw new DebugException(e.getStatus());
		}
	}

	@Override
	public BigInteger getMemoryBlockStartAddress() throws DebugException {

		// if (true)
		// return fBaseAddress.subtract(BigInteger.valueOf(250));
		// Return null by default.
		// Null is acceptable if default start address is to be used.
		// Default is 0.
		return null;
	}

	@Override
	public BigInteger getMemoryBlockEndAddress() throws DebugException {

		// if (true)
		// return fBaseAddress.add(BigInteger.valueOf(250));
		// Return null by default.
		// Null is accpetable if default end address is to be used.
		// Default end address is calculated based on address size.
		return null;
	}

	@Override
	public void setValue(long offset, byte[] bytes) throws DebugException {
		// do not need to implement for IMemoryBlockExtension
	}

	@Override
	public BigInteger getBigLength() throws DebugException {
		// return -1 by default and default length is calculated
		return BigInteger.valueOf(-1);
	}
}

Back to the top