Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 65d9d71aab49d2f6ebf88f3e37c837d9709f48b9 (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
/*******************************************************************************
 * Copyright (c) 2004, 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
 *	   WindRiver - Bug 192028 [Memory View] Memory view does not
 *                 display memory blocks that do not reference IDebugTarget
 *******************************************************************************/

package org.eclipse.debug.internal.core;

import java.util.ArrayList;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.SafeRunner;
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.IMemoryBlockListener;
import org.eclipse.debug.core.IMemoryBlockManager;
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;


/**
 * Implementation of IMemoryBlockManager
 * The manager is responsible to manage all memory blocks in the workbench.
 *
 * @since 3.1
 */
public class MemoryBlockManager implements IMemoryBlockManager, IDebugEventSetListener {

	private ArrayList<IMemoryBlockListener> listeners = new ArrayList<>();
	private ArrayList<IMemoryBlock> memoryBlocks = new ArrayList<>();

	private static final int ADDED = 0;
	private static final int REMOVED = 1;
	/**
	 * Notifies a memory block listener in a safe runnable to
	 * handle exceptions.
	 */
	class MemoryBlockNotifier implements ISafeRunnable {

		private IMemoryBlockListener fListener;
		private int fType;
		private IMemoryBlock[] fMemoryBlocks;

		@Override
		public void handleException(Throwable exception) {
			DebugPlugin.log(exception);
		}

		@Override
		public void run() throws Exception {
			switch (fType) {
				case ADDED:
					fListener.memoryBlocksAdded(fMemoryBlocks);
					break;
				case REMOVED:
					fListener.memoryBlocksRemoved(fMemoryBlocks);
					break;
				default:
					break;
			}
		}

		/**
		 * Notify listeners of added/removed memory block events
		 *
		 * @param memBlocks blocks that have changed
		 * @param update type of change
		 */
		public void notify(IMemoryBlock[] memBlocks, int update) {
			if (listeners != null) {
				fType = update;
				Object[] copiedListeners= listeners.toArray(new IMemoryBlockListener[listeners.size()]);
				for (int i= 0; i < copiedListeners.length; i++) {
					fListener = (IMemoryBlockListener)copiedListeners[i];
					fMemoryBlocks = memBlocks;
					SafeRunner.run(this);
				}
			}
			fListener = null;
			fMemoryBlocks = null;
		}
	}

	/**
	 * Returns the <code>MemoryBlockNotifier</code>
	 * @return the <code>MemoryBlockNotifier</code>
	 *
	 * TODO consider using only one of these, and sync where needed,
	 * this way we are not creating a new every single time.
	 */
	private MemoryBlockNotifier getMemoryBlockNotifier() {
		return new MemoryBlockNotifier();
	}

	@Override
	public void addMemoryBlocks(IMemoryBlock[] mem) {
		if (memoryBlocks == null) {
			return;
		}
		if (mem == null) {
			DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addMemoryBlock", null); //$NON-NLS-1$
			return;
		}

		if(mem.length > 0) {
			ArrayList<IMemoryBlock> newMemoryBlocks = new ArrayList<>();
			for (int i=0; i<mem.length; i++) {
				// do not allow duplicates
				if (!memoryBlocks.contains(mem[i])) {
					newMemoryBlocks.add(mem[i]);
					memoryBlocks.add(mem[i]);
					// add listener for the first memory block added
					if (memoryBlocks.size() == 1) {
						DebugPlugin.getDefault().addDebugEventListener(this);
					}
				}
			}
			notifyListeners(newMemoryBlocks.toArray(new IMemoryBlock[newMemoryBlocks.size()]), ADDED);
		}
	}

	@Override
	public void removeMemoryBlocks(IMemoryBlock[] memBlocks) {
		if (memoryBlocks == null) {
			return;
		}
		if (memBlocks == null){
			DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.removeMemoryBlock", null); //$NON-NLS-1$
			return;
		}

		if(memBlocks.length > 0) {
			for (int i=0; i<memBlocks.length; i++) {
				memoryBlocks.remove(memBlocks[i]);
				// remove listener after the last memory block has been removed
				if (memoryBlocks.isEmpty()) {
					DebugPlugin.getDefault().removeDebugEventListener(this);
				}
				if (memBlocks[i] instanceof IMemoryBlockExtension) {
					try {
						((IMemoryBlockExtension)memBlocks[i]).dispose();
					} catch (DebugException e) {
						DebugPlugin.log(e);
					}
				}
			}
			notifyListeners(memBlocks, REMOVED);
		}
	}

	@Override
	public void addListener(IMemoryBlockListener listener) {

		if(listeners == null) {
			return;
		}
		if(listener == null) {
			DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addListener", null); //$NON-NLS-1$
			return;
		}
		if (!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}

	@Override
	public void removeListener(IMemoryBlockListener listener) {

		if(listeners == null) {
			return;
		}
		if(listener == null) {
			DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.removeListener", null); //$NON-NLS-1$
			return;
		}
		if (listeners.contains(listener)) {
			listeners.remove(listener);
		}
	}

	@Override
	public IMemoryBlock[] getMemoryBlocks() {
		return memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]);
	}

	@Override
	public IMemoryBlock[] getMemoryBlocks(IDebugTarget debugTarget) {
		ArrayList<IMemoryBlock> memoryBlocksList = new ArrayList<>();
		for (IMemoryBlock block : memoryBlocks) {
			if (block.getDebugTarget() == debugTarget) {
				memoryBlocksList.add(block);
			}
		}
		return memoryBlocksList.toArray(new IMemoryBlock[memoryBlocksList.size()]);
	}

	@Override
	public IMemoryBlock[] getMemoryBlocks(IMemoryBlockRetrieval retrieve) {
		ArrayList<IMemoryBlock> memoryBlocksList = new ArrayList<>();
		for (IMemoryBlock block : memoryBlocks) {
			if (block instanceof IMemoryBlockExtension) {
				if (((IMemoryBlockExtension) block).getMemoryBlockRetrieval() == retrieve) {
					memoryBlocksList.add(block);
				}
			}
			else {
				IMemoryBlockRetrieval mbRetrieval = block.getAdapter(IMemoryBlockRetrieval.class);
				// standard memory block always uses the debug target as the memory block retrieval
				if (mbRetrieval == null) {
					mbRetrieval = block.getDebugTarget();
				}
				if (mbRetrieval == retrieve) {
					memoryBlocksList.add(block);
				}
			}
		}
		return memoryBlocksList.toArray(new IMemoryBlock[memoryBlocksList.size()]);
	}

	/**
	 * Notifies the listeners about the given memory blocks and the event to be sent
	 * @param memBlocks the array of memory blocks
	 * @param event the event to notify to the blocks
	 */
	private void notifyListeners(IMemoryBlock[] memBlocks, int event) {
		getMemoryBlockNotifier().notify(memBlocks, event);
	}

	@Override
	public void handleDebugEvents(DebugEvent[] events) {
		for (int i=0; i < events.length; i++) {
			handleDebugEvent(events[i]);
		}
	}

	/**
	 * Handles a debug event
	 * @param event the {@link DebugEvent}
	 */
	public void handleDebugEvent(DebugEvent event) {
		Object obj = event.getSource();
		IDebugTarget dt = null;

		if (event.getKind() == DebugEvent.TERMINATE) {
			// a terminate event could happen from an IThread or IDebugTarget
			// only handle a debug event from the debug target
			if (obj instanceof IDebugTarget) {
				dt = ((IDebugTarget)obj);

				// getMemoryBlocks will return an empty array if it is null
				IMemoryBlock[] deletedMemoryBlocks = getMemoryBlocks(dt);
				removeMemoryBlocks(deletedMemoryBlocks);
			}
		}
	}

	/**
	 * Clean up when the plugin is shut down
	 */
	public void shutdown() {
		if (listeners != null) {
			listeners.clear();
			listeners = null;
		}

		if (memoryBlocks != null) {
			memoryBlocks.clear();
			memoryBlocks = null;
		}
	}

}

Back to the top