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

import java.math.BigInteger;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IDebugElement;
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.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.memory.AbstractMemoryRendering;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.debug.ui.memory.IMemoryRenderingContainer;
import org.eclipse.debug.ui.memory.IMemoryRenderingType;
import org.eclipse.debug.ui.memory.IRepositionableMemoryRendering;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;


/**
 * Go To Address Action for table rendering
 * 
 * @since 3.0
 */
public class GoToAddressAction extends Action
{
    private IMemoryRenderingContainer fContainer;
	private IRepositionableMemoryRendering fRendering;
	
	public GoToAddressAction(IMemoryRenderingContainer container, IRepositionableMemoryRendering rendering)
	{		
		super(DebugUIMessages.GoToAddressAction_title);
		fContainer = container;
		setToolTipText(DebugUIMessages.GoToAddressAction_title);
		
		fRendering = rendering;
		
		PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".GoToAddressAction_context"); //$NON-NLS-1$
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.IAction#run()
	 */
	@Override
	public void run()
	{
		try
		{	
			Shell shell= DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
		
			// create dialog to ask for expression/address to block
			GoToAddressDialog dialog = new GoToAddressDialog(shell);
			dialog.open();
		
			int returnCode = dialog.getReturnCode();
		
			if (returnCode == Window.CANCEL)
			{
				return;
			}
		
			// get expression from dialog
			String expression = dialog.getExpression();
			
			expression = parseExpression(expression);
			
			doGoToAddress(expression);
		}
		// open error in case of any error
		catch (DebugException e)
		{
			MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed, 
				DebugUIMessages.GoToAddressAction_Go_to_address_failed, e);
		}
		catch (NumberFormatException e1)
		{
			MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed, 
				DebugUIMessages.GoToAddressAction_Address_is_invalid, null);
		}
	}
	/**
	 * @param expression
	 * @return
	 */
	public String parseExpression(String expression) {
		expression = expression.toUpperCase();
		expression = expression.trim();
		
		if (expression.startsWith("0X")) //$NON-NLS-1$
		{
			expression = expression.substring(2);
		}
		return expression;
	}
	/**
	 * @param expression
	 * @throws DebugException
	 */
	public void doGoToAddress(String expression) throws DebugException, NumberFormatException {
		// convert expression to address
		BigInteger address = new BigInteger(expression, 16);
		
		// look at this address and figure out if a new memory block should
		// be opened.
		IMemoryBlock mb = fRendering.getMemoryBlock();
		if (mb instanceof IMemoryBlockExtension)
		{
			IMemoryBlockExtension mbExt = (IMemoryBlockExtension)mb;
			BigInteger mbStart = mbExt.getMemoryBlockStartAddress();
			BigInteger mbEnd = mbExt.getMemoryBlockEndAddress();
			
			if (mbStart != null)
			{
				// if trying to go beyond the start address
				// of the memory block
				if (address.compareTo(mbStart) < 0)
				{
					IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(mbExt);					
					
					// add a new memory block and then the same rendering as fRendering
					// in the same container.
					if (retrieval != null && retrieval instanceof IMemoryBlockRetrievalExtension)
					{
						addNewMemoryBlock(expression, (IMemoryBlockRetrievalExtension)retrieval);
						return;
					}
				}
			}
			if (mbEnd != null)
			{
				// if trying to go beyond the end address
				// of the memory block
				if (address.compareTo(mbEnd) > 0)
				{
					IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(mbExt);
										
					// add a new memory block and then the same rendering as fRendering
					// in the same container.
					if (retrieval != null && retrieval instanceof IMemoryBlockRetrievalExtension)
					{
						addNewMemoryBlock(expression, (IMemoryBlockRetrievalExtension)retrieval);
						return;
					}
				}
			}
		}
		
		// go to specified address
		fRendering.goToAddress(address);
	}
	
	private void addNewMemoryBlock(String expression, IMemoryBlockRetrievalExtension retrieval)
	{
		Object elem = DebugUITools.getPartDebugContext(fContainer.getMemoryRenderingSite().getSite());
		
		if (!(elem instanceof IDebugElement))
			return;
		 
		try {
			if (retrieval != null)
			{	
				IMemoryBlockExtension mbext = retrieval.getExtendedMemoryBlock(expression, elem);
				if (mbext != null)
				{
					IMemoryBlock[] memArray = new IMemoryBlock[]{mbext};
					DebugPlugin.getDefault().getMemoryBlockManager().addMemoryBlocks(memArray);
				}
				
				IMemoryRenderingType renderingType = DebugUITools.getMemoryRenderingManager().getRenderingType(fRendering.getRenderingId());
				
				if (renderingType != null)
				{
					IMemoryRendering rendering = renderingType.createRendering();
					
					if (rendering != null && fRendering instanceof AbstractMemoryRendering)
					{
						rendering.init(((AbstractMemoryRendering)fRendering).getMemoryRenderingContainer(), mbext);
						((AbstractMemoryRendering)fRendering).getMemoryRenderingContainer().addMemoryRendering(rendering);
					}
				}
			}
		} catch (DebugException e) {
			MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed, 
			DebugUIMessages.GoToAddressAction_Go_to_address_failed, e);
		} catch (CoreException e)
		{
			MemoryViewUtil.openError(DebugUIMessages.GoToAddressAction_Go_to_address_failed, 
			DebugUIMessages.GoToAddressAction_Go_to_address_failed, e);
		}
	}
}

Back to the top