Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 801b2c7d836cdf8fb7b786a611cf39b83b82e442 (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
/*******************************************************************************
 * Copyright (c) 2006, 2011 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.internal.ui.views.memory.renderings;

import java.math.BigInteger;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.memory.provisional.AbstractAsyncTableRendering;
import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil;
import org.eclipse.debug.ui.memory.MemoryRenderingElement;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.TableItem;

/**
 * @since 3.1
 *
 */
// TODO:  if we want "true" flexible hierarchy, also need to allow clients
// to plug cell modifier in case the element is not MemorySegment
public class AsyncTableRenderingCellModifier implements ICellModifier {

	private AbstractAsyncTableRendering fRendering;
	private boolean fMBSupportsValueModification = false;

	private ICellModifier fCustomModifier;

	public AsyncTableRenderingCellModifier(AbstractAsyncTableRendering rendering, ICellModifier customModifier) {
		fRendering = rendering;
		fCustomModifier = customModifier;

		Job job = new Job("AsyncTableRenderingCellModifier"){ //$NON-NLS-1$

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				fMBSupportsValueModification = fRendering.getMemoryBlock().supportsValueModification();
				return Status.OK_STATUS;
			}};
		job.setSystem(true);
		job.schedule();
	}

	@Override
	public boolean canModify(Object element, String property) {
		boolean canModify = true;
		try {
			if (!(element instanceof MemorySegment)) {
				return false;
			}

			if (!isValueModificationSupported()) {
				return false;
			}

			MemorySegment line = (MemorySegment) element;
			if (TableRenderingLine.P_ADDRESS.equals(property)) {
				return false;
			}

			// property is stored as number of addressable unit away from the
			// line address
			// to calculate offset to the memory line array, offset =
			// numberofAddressableUnit * addressableSize
			int addressableSize = getAddressableSize();

			int offset = Integer.valueOf(property, 16).intValue() * addressableSize;

			MemoryByte[] bytes = line.getBytes(offset, fRendering.getBytesPerColumn());

			if (fCustomModifier != null)
			{
				BigInteger address = line.getAddress().add(BigInteger.valueOf(offset));
				MemoryRenderingElement mElement = new MemoryRenderingElement(fRendering, address, bytes);
				return fCustomModifier.canModify(mElement, null);
			}

			for (MemoryByte b : bytes) {
				if (!b.isWritable()) {
					canModify = false;
				}
			}
			return canModify;
		} catch (NumberFormatException e) {
			canModify = false;
			return canModify;
		}
	}

	/**
	 * @return the rendering addressable size
	 */
	private int getAddressableSize() {
		int addressableSize = fRendering.getAddressableSize();
		if (addressableSize < 1) {
			addressableSize = 1;
		}
		return addressableSize;
	}

	@Override
	public Object getValue(Object element, String property) {

		// give back the value of the column

		if (!(element instanceof MemorySegment)) {
			return null;
		}

		MemorySegment line = (MemorySegment) element;
		try {
			if (TableRenderingLine.P_ADDRESS.equals(property)) {
				return line.getAddress();
			}

			int offsetToLineBuffer = Integer.valueOf(property, 16).intValue() * getAddressableSize();
			MemoryByte[] memory = line.getBytes(offsetToLineBuffer, fRendering.getBytesPerColumn());

			int offsetFromLineAddress = Integer.valueOf(property, 16).intValue();
			BigInteger address = line.getAddress().add(BigInteger.valueOf(offsetFromLineAddress));

			if (fCustomModifier != null)
			{
				MemoryRenderingElement mElement = new MemoryRenderingElement(fRendering, address, memory);
				return fCustomModifier.getValue(mElement, null);
			}

			// ask the rendering for a string representation of the bytes
			return fRendering.getString(fRendering.getRenderingId(), address, memory);

		} catch (NumberFormatException e) {
			return "00"; //$NON-NLS-1$
		}
	}

	@Override
	public void modify(Object element, final String property, final Object value) {

		MemorySegment segment = null;
		if (element instanceof TableItem) {
			Object data = ((TableItem)element).getData();
			if (data != null && data instanceof MemorySegment) {
				segment = (MemorySegment)data;
			}

		} else if (element instanceof MemorySegment){
			segment = (MemorySegment) element;
		}

		if (segment == null) {
			return;
		}

		final MemorySegment line = segment;

		Job job = new Job("Set Values"){ //$NON-NLS-1$

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				try {
					// calculate offset to update
					final IMemoryBlock memoryBlk = fRendering.getMemoryBlock();

					// number of addressable units from the line's start address
					int offsetFromLineAddress = Integer.valueOf(property, 16).intValue();

					// this offset is number of addressable unit from memory block's base address
					final BigInteger offsetFromMBBase = getOffset(memoryBlk, line.getAddress(), offsetFromLineAddress);

					// property is number of addressable unit from line address
					// to calculate proper offset in the memoryViewLine's array
					// offset = numberOfAddressableUnit * addressableSize
					int offsetToLineBuffer = Integer.valueOf(property, 16).intValue() * getAddressableSize();

					MemoryByte[] oldArray = line.getBytes(offsetToLineBuffer, fRendering.getBytesPerColumn());

					// address is line address + addressable unit into the line
					BigInteger address = line.getAddress();
					address = address.add(BigInteger.valueOf(offsetFromLineAddress));

					if (fCustomModifier != null) {
						MemoryRenderingElement mElement = new MemoryRenderingElement(fRendering, address, oldArray);
						fCustomModifier.modify(mElement, null, value);
						return Status.OK_STATUS;
					}

					if (!(value instanceof String))
					{
						DebugUIPlugin.logErrorMessage("Cell modifier cannot handle non-string values."); //$NON-NLS-1$
						return Status.OK_STATUS;
					}

					byte[] bytes = null;
					String oldValue = (String) getValue(line, property);
					if (!oldValue.equals(value)) {
						bytes = fRendering.getBytes(fRendering.getRenderingId(), address, oldArray, (String) value);

						if (bytes == null) {
							return Status.OK_STATUS;
						}

						if (bytes.length == 0) {
							return Status.OK_STATUS;
						}

						if (bytes.length <= oldArray.length) {
							boolean changed = false;
							// check that the bytes returned has actually changed
							for (int i = 0; i < bytes.length; i++) {
								if (bytes[i] != oldArray[i].getValue()) {
									changed = true;
									break;
								}
							}
							if (!changed) {
								return Status.OK_STATUS;
							}
						}
					} else {
						// return if value has not changed
						return Status.OK_STATUS;
					}

					final byte[] newByteValues = bytes;

					if (memoryBlk instanceof IMemoryBlockExtension) {
						((IMemoryBlockExtension) memoryBlk).setValue(offsetFromMBBase, newByteValues);
					} else {
						memoryBlk.setValue(offsetFromMBBase.longValue(), newByteValues);
					}
				} catch (DebugException e) {
					MemoryViewUtil.openError(DebugUIMessages.MemoryViewCellModifier_failure_title, DebugUIMessages.MemoryViewCellModifier_failed, e);
				} catch (NumberFormatException e) {
					MemoryViewUtil.openError(DebugUIMessages.MemoryViewCellModifier_failure_title, DebugUIMessages.MemoryViewCellModifier_failed + "\n" + DebugUIMessages.MemoryViewCellModifier_data_is_invalid, null); //$NON-NLS-1$
				}
				return Status.OK_STATUS;
			}};

		job.setSystem(true);
		job.schedule();
	}

	private BigInteger getOffset(IMemoryBlock memory, BigInteger lineAddress, int lineOffset) throws DebugException {

		BigInteger memoryAddr;

		if (memory instanceof IMemoryBlockExtension) {
			memoryAddr = ((IMemoryBlockExtension) memory).getBigBaseAddress();
		} else {
			memoryAddr = BigInteger.valueOf(memory.getStartAddress());
		}

		if (memoryAddr == null)
		 {
			memoryAddr = new BigInteger("0"); //$NON-NLS-1$
		}

		return lineAddress.subtract(memoryAddr).add(BigInteger.valueOf(lineOffset));
	}

	private boolean isValueModificationSupported()
	{
		return fMBSupportsValueModification;
	}

}

Back to the top