Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 844f628e3c93a7e90033967cf72e514356e6ee7a (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
/*******************************************************************************
 * Copyright (c) 2004, 2013 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;

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

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
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.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IMemoryBlock;
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.ITerminate;
import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
import org.eclipse.debug.internal.ui.DebugPluginImages;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.debug.ui.memory.IMemoryRenderingContainer;
import org.eclipse.debug.ui.memory.IMemoryRenderingSite;
import org.eclipse.debug.ui.memory.IMemoryRenderingType;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * Action for adding memory block.
 *
 * @since 3.0
 */
public class AddMemoryBlockAction extends Action implements IDebugContextListener, IDebugEventSetListener {

	protected IAdaptable fCurrentContext = null;
	protected IMemoryBlock fLastMemoryBlock;
	private boolean fAddDefaultRenderings = true;
	protected IMemoryRenderingSite fSite;

	public AddMemoryBlockAction(IMemoryRenderingSite site) {
		initialize(site);
	}

	/**
	 * @param site the site to add the action to
	 * @param addDefaultRenderings - specify if the action should add default
	 *            renderings for the new memory block when it is run
	 */
	AddMemoryBlockAction(IMemoryRenderingSite site, boolean addDefaultRenderings) {
		initialize(site);
		fAddDefaultRenderings = addDefaultRenderings;
	}

	/**
	 * @param site the site to initialize
	 */
	private void initialize(IMemoryRenderingSite site) {
		setText(DebugUIMessages.AddMemoryBlockAction_title);
		doInitialization(site);
	}

	public AddMemoryBlockAction(String text, int style, IMemoryRenderingSite site) {
		super(text, style);
		doInitialization(site);
	}

	/**
	 * @param site the site to initialize
	 */
	private void doInitialization(IMemoryRenderingSite site) {
		fSite = site;
		setToolTipText(DebugUIMessages.AddMemoryBlockAction_tooltip);
		setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_MONITOR_EXPRESSION));
		setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_LCL_ADD));
		setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_MONITOR_EXPRESSION));
		PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".addMemoryMonitorAction_context"); //$NON-NLS-1$

		// listen for context changed
		DebugUITools.addPartDebugContextListener(fSite.getSite(), this);

		// get current context
		fCurrentContext = DebugUITools.getPartDebugContext(site.getSite());

		// set up enablement based on current selection
		updateAction(fCurrentContext);

		DebugPlugin.getDefault().addDebugEventListener(this);
	}

	@Override
	public void run() {
		boolean exit = false;
		String prefillExp = null;
		String prefillLength = null;
		while (!exit) {
			exit = true;

			Object elem = fCurrentContext;

			IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(elem);

			if (retrieval == null) {
				return;
			}

			Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
			// create dialog to ask for expression/address to block
			MonitorMemoryBlockDialog dialog = new MonitorMemoryBlockDialog(shell, retrieval, prefillExp, prefillLength);
			dialog.open();
			int returnCode = dialog.getReturnCode();
			if (returnCode == Window.CANCEL) {
				return;
			}
			// get expression entered in dialog
			String input = dialog.getExpression();

			// remember expression and length
			prefillExp = input;
			prefillLength = dialog.getLength();

			ArrayList<String> expressions = new ArrayList<>();

			if (input.length() == 0) {
				expressions.add(IInternalDebugCoreConstants.EMPTY_STRING);
			} else {
				StringTokenizer tokenizer = new StringTokenizer(input, ","); //$NON-NLS-1$
				while (tokenizer.hasMoreTokens()) {
					expressions.add(tokenizer.nextToken());
				}
			}
			final String[] expressionsArray = expressions.toArray(new String[expressions.size()]);
			exit = true;

			final boolean finalExit = exit;
			final Object finalElement = elem;
			final IMemoryBlockRetrieval finalRetrieval = retrieval;
			final MonitorMemoryBlockDialog finalDialog = dialog;
			Job job = new Job("Add Memory Block") { //$NON-NLS-1$
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					addMemoryBlocks(finalExit, finalElement, finalRetrieval, finalDialog, expressionsArray);
					return Status.OK_STATUS;
				}
			};
			job.setSystem(true);
			job.schedule();
		}
	}

	/**
	 * @param exit the exit value
	 * @param elem the element context
	 * @param standardMemRetrieval the {@link IMemoryBlockRetrieval}
	 * @param dialog the dialog to use
	 * @param expressionsArray the array of memory expressions
	 * @return if we should exit
	 */
	private boolean addMemoryBlocks(boolean exit, Object elem, IMemoryBlockRetrieval standardMemRetrieval, MonitorMemoryBlockDialog dialog, final String[] expressionsArray) {
		for (String e : expressionsArray) {
			String expression = e.trim();
			try {
				if (standardMemRetrieval instanceof IMemoryBlockRetrievalExtension) {
					// if the debug session supports
					// IMemoryBlockExtensionRetrieval
					IMemoryBlockRetrievalExtension memRetrieval = (IMemoryBlockRetrievalExtension) standardMemRetrieval;

					// get extended memory block with the expression entered
					IMemoryBlockExtension memBlock = memRetrieval.getExtendedMemoryBlock(expression, elem);

					// add block to memory block manager
					if (memBlock != null) {
						fLastMemoryBlock = memBlock;

						IMemoryBlock[] memArray = new IMemoryBlock[] { memBlock };

						MemoryViewUtil.getMemoryBlockManager().addMemoryBlocks(memArray);
						if (fAddDefaultRenderings) {
							addDefaultRenderings(memBlock);
						}
					} else {
						// open error if it failed to retrieve a memory block
						MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_noMemoryBlock, null);
						exit = false;
					}
				} else {
					// if the debug session does not support
					// IMemoryBlockExtensionRetrieval
					expression = expression.toUpperCase();
					String hexPrefix = "0X"; //$NON-NLS-1$
					if (expression.startsWith(hexPrefix)) {
						expression = expression.substring(hexPrefix.length());
					}

					// convert the expression to an address
					BigInteger address = new BigInteger(expression, 16);

					long longAddress = address.longValue();

					// get the length of memory to block
					String strLength = dialog.getLength();

					long length = Long.parseLong(strLength);

					// must monitor at least one line
					if (length <= 0) {
						String message = DebugUIMessages.AddMemoryBlockAction_failed + "\n" + DebugUIMessages.AddMemoryBlockAction_input_invalid; //$NON-NLS-1$
						MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, message, null);
						exit = false;
						continue;
					}

					// get standard memory block
					IMemoryBlock memBlock = standardMemRetrieval.getMemoryBlock(longAddress, length);

					// make sure the memory block returned is not an instance of
					// IMemoryBlockExtension
					if (memBlock instanceof IMemoryBlockExtension) {
						Status status = new Status(IStatus.WARNING, DebugUIPlugin.getUniqueIdentifier(), 0, "IMemoryBlockRetrieval returns IMemoryBlockExtension.  This may result in unexpected behavior.", null); //$NON-NLS-1$
						DebugUIPlugin.log(status);
					}

					if (memBlock != null) {
						// add memory block to memory block manager
						fLastMemoryBlock = memBlock;
						IMemoryBlock[] memArray = new IMemoryBlock[] { memBlock };

						MemoryViewUtil.getMemoryBlockManager().addMemoryBlocks(memArray);
						if (fAddDefaultRenderings) {
							addDefaultRenderings(memBlock);
						}
					} else {
						// otherwise open up an error doalog
						MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_noMemoryBlock, null);
						exit = false;
					}
				}
			} catch (DebugException e1) {
				MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_failed, e1);
				exit = false;
			} catch (NumberFormatException e2) {
				String message = DebugUIMessages.AddMemoryBlockAction_failed + "\n" + DebugUIMessages.AddMemoryBlockAction_input_invalid; //$NON-NLS-1$
				MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, message, null);
				exit = false;
			}
		}
		return exit;
	}

	@Override
	public void handleDebugEvents(DebugEvent[] events) {
		for (DebugEvent event : events) {
			handleDebugEvent(event);
		}
	}

	private void handleDebugEvent(DebugEvent event) {
		// update action enablement based on debug event
		Object src = event.getSource();
		IDebugTarget srcDT = null;
		IDebugTarget selectionDT = null;

		if (event.getKind() == DebugEvent.TERMINATE) {
			if (src instanceof ITerminate && src instanceof IDebugElement) {
				srcDT = ((IDebugElement) src).getDebugTarget();
			}

			if (fCurrentContext instanceof IDebugElement) {
				selectionDT = ((IDebugElement) fCurrentContext).getDebugTarget();
			}

			// disable action if the debug target is terminated.
			if (srcDT == selectionDT) {
				setEnabled(false);
			}
		}
		// handle change event from memory block retrieval object
		// to allow non-standard debug models to update the action
		else if (event.getKind() == DebugEvent.CHANGE && event.getDetail() == DebugEvent.STATE) {
			Object evtSrc = event.getSource();
			if (evtSrc == MemoryViewUtil.getMemoryBlockRetrieval(fCurrentContext)) {
				updateAction(fCurrentContext);
			}
		}
	}

	/**
	 * Return the last memory block added to memory block manager via this
	 * action.
	 *
	 * @return Returns the fLastMemoryBlock.
	 */
	public IMemoryBlock getLastMemoryBlock() {
		return fLastMemoryBlock;
	}

	protected void dispose() {

		// remove listeners
		DebugPlugin.getDefault().removeDebugEventListener(this);
		DebugUITools.removePartDebugContextListener(fSite.getSite(), this);
	}

	private void addDefaultRenderings(IMemoryBlock memoryBlock) {
		IMemoryRenderingType primaryType = DebugUITools.getMemoryRenderingManager().getPrimaryRenderingType(memoryBlock);
		IMemoryRenderingType renderingTypes[] = DebugUITools.getMemoryRenderingManager().getDefaultRenderingTypes(memoryBlock);

		// create primary rendering
		try {
			if (primaryType != null) {
				createRenderingInContainer(memoryBlock, primaryType, IDebugUIConstants.ID_RENDERING_VIEW_PANE_1);
			} else if (renderingTypes.length > 0) {
				primaryType = renderingTypes[0];
				createRenderingInContainer(memoryBlock, renderingTypes[0], IDebugUIConstants.ID_RENDERING_VIEW_PANE_1);
			}
		} catch (CoreException e1) {
			DebugUIPlugin.log(e1);
		}

		for (IMemoryRenderingType renderingType : renderingTypes) {
			try {
				boolean create = true;
				if (primaryType != null) {
					if (primaryType.getId().equals(renderingType.getId())) {
						create = false;
					}
				}
				if (create) {
					createRenderingInContainer(memoryBlock, renderingType, IDebugUIConstants.ID_RENDERING_VIEW_PANE_2);
				}
			}catch (CoreException e) {
				DebugUIPlugin.log(e);
			}
		}
	}

	/**
	 * @param memoryBlock the memory block to create a rendering for
	 * @param primaryType the type of the desired rendering
	 * @param paneId the id of the pane
	 * @throws CoreException if an exception occurs
	 */
	private void createRenderingInContainer(IMemoryBlock memoryBlock, IMemoryRenderingType primaryType, String paneId) throws CoreException {
		IMemoryRendering rendering = primaryType.createRendering();
		IMemoryRenderingContainer container = fSite.getContainer(paneId);
		rendering.init(container, memoryBlock);
		container.addMemoryRendering(rendering);
	}

	protected MemoryView getMemoryView() {
		if (fSite instanceof MemoryView) {
			return (MemoryView) fSite;
		}
		return null;
	}

	protected void updateAction(final Object debugContext) {
		Job job = new Job("Update Add Memory Block Action") { //$NON-NLS-1$
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				setEnabled(MemoryViewUtil.isValidContext(debugContext));
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.schedule();
	}

	@Override
	public void debugContextChanged(DebugContextEvent event) {
		if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
			IAdaptable context = DebugUITools.getPartDebugContext(fSite.getSite());
			updateAction(context);
			fCurrentContext = context;
		}
	}
}

Back to the top