Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e67320e453a7b439a4568574e25f2056c826aa41 (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
/*******************************************************************************
 * Copyright (c) 2004, 2009 QNX Software Systems 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:
 * QNX Software Systems - Initial API and implementation
 * Gaetano Santoro (gaetano.santoro@st.com): patch for 
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=274499
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.core; 

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.ICGlobalVariableManager;
import org.eclipse.cdt.debug.core.model.ICGlobalVariable;
import org.eclipse.cdt.debug.core.model.IGlobalVariableDescriptor;
import org.eclipse.cdt.debug.internal.core.model.CDebugTarget;
import org.eclipse.cdt.debug.internal.core.model.CVariable;
import org.eclipse.cdt.debug.internal.core.model.CVariableFactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Manages all global variables registered with a debug target.
 */
public class CGlobalVariableManager implements ICGlobalVariableManager {

	private static final String GLOBAL_VARIABLE_LIST = "globalVariableList"; //$NON-NLS-1$
	private static final String GLOBAL_VARIABLE = "globalVariable"; //$NON-NLS-1$
	private static final String ATTR_GLOBAL_VARIABLE_PATH = "path"; //$NON-NLS-1$
	private static final String ATTR_GLOBAL_VARIABLE_NAME = "name"; //$NON-NLS-1$

	private CDebugTarget fDebugTarget;

	private IGlobalVariableDescriptor[] fInitialDescriptors = new IGlobalVariableDescriptor[0];

	private List<ICGlobalVariable> fGlobals;

	/** 
	 * Constructor for CGlobalVariableManager. 
	 */
	public CGlobalVariableManager( CDebugTarget target ) {
		super();
		setDebugTarget( target );
		initialize();
	}

	protected CDebugTarget getDebugTarget() {
		return fDebugTarget;
	}
	
	private void setDebugTarget( CDebugTarget debugTarget ) {
		fDebugTarget = debugTarget;
	}

	public ICGlobalVariable[] getGlobals() {
		if ( fGlobals == null ) {
			try {
				addGlobals( getInitialDescriptors() );
			}
			catch( DebugException e ) {
				DebugPlugin.log( e );
			}
		}
		return fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#addGlobals(IGlobalVariableDescriptor[])
	 */
	public void addGlobals( IGlobalVariableDescriptor[] descriptors ) throws DebugException {
		fGlobals = new ArrayList<ICGlobalVariable>( 10 );
		MultiStatus ms = new MultiStatus( CDebugCorePlugin.getUniqueIdentifier(), 0, "", null ); //$NON-NLS-1$
		List<ICGlobalVariable> globals = new ArrayList<ICGlobalVariable>( descriptors.length );
		for ( int i = 0; i < descriptors.length; ++i ) {
			try {
				globals.add( getDebugTarget().createGlobalVariable( descriptors[i] ) );
			}
			catch( DebugException e ) {
				ms.add( e.getStatus() );
			}
		}
		if ( globals.size() > 0 ) {
			synchronized( fGlobals ) {
				fGlobals.addAll( globals );
			}
		}
        getDebugTarget().fireChangeEvent( DebugEvent.CONTENT );
		if ( !ms.isOK() ) {
			throw new DebugException( ms );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#removeGlobals(ICGlobalVariable[])
	 */
	public void removeGlobals( ICGlobalVariable[] globals ) {
		synchronized( fGlobals ) {
			fGlobals.removeAll( Arrays.asList( globals ) );
		}
		for ( int i = 0; i < globals.length; ++i ) {
			if ( globals[i] instanceof CVariable )
				((CVariable)globals[i]).dispose();
		}
		getDebugTarget().fireChangeEvent( DebugEvent.CONTENT );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#removeAllGlobals()
	 */
	public void removeAllGlobals() {
		if (fGlobals == null ) {
			return;
		}

		ICGlobalVariable[] globals = new ICGlobalVariable[0];
		synchronized( fGlobals ) {
			globals = fGlobals.toArray( new ICGlobalVariable[fGlobals.size()] );
			fGlobals.clear();
		}
		for ( int i = 0; i < globals.length; ++i ) {
			if ( globals[i] instanceof CVariable )
				((CVariable)globals[i]).dispose();
		}
		getDebugTarget().fireChangeEvent( DebugEvent.CONTENT );
	}

	public void dispose() {
		if ( fGlobals != null ) {
			for (ICGlobalVariable global : fGlobals) {
				((CVariable)global).dispose();
			}
			fGlobals.clear();
			fGlobals = null;
		}
	}

	public String getMemento() {
		Document document = null;
		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Element node = document.createElement( GLOBAL_VARIABLE_LIST );
			document.appendChild( node );
			ICGlobalVariable[] globals = getGlobals();
			for (ICGlobalVariable global : globals) {
				IGlobalVariableDescriptor descriptor = global.getDescriptor();
				// children of globals don't have a descriptor, though getGlobals() shouldn't return only top level globals
				if (descriptor != null) {
					Element child = document.createElement( GLOBAL_VARIABLE );
					child.setAttribute( ATTR_GLOBAL_VARIABLE_NAME, descriptor.getName() );
					child.setAttribute( ATTR_GLOBAL_VARIABLE_PATH, descriptor.getPath().toOSString() );
					node.appendChild( child );
				}
			}
			return CDebugUtils.serializeDocument( document );
		}
		catch( ParserConfigurationException e ) {
			DebugPlugin.log( e );
		}
		catch( IOException e ) {
			DebugPlugin.log( e );
		}
		catch( TransformerException e ) {
			DebugPlugin.log( e );
		}
		return null;
	}

	private void initializeFromMemento( String memento ) throws CoreException {
		Exception ex = null;
		try {
			Element root = null;
			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			StringReader reader = new StringReader( memento );
			InputSource source = new InputSource( reader );
			root = parser.parse( source ).getDocumentElement();
			if ( root.getNodeName().equalsIgnoreCase( GLOBAL_VARIABLE_LIST ) ) {
				List<IGlobalVariableDescriptor> descriptors = new ArrayList<IGlobalVariableDescriptor>();
				NodeList list = root.getChildNodes();
				int length = list.getLength();
				for( int i = 0; i < length; ++i ) {
					Node node = list.item( i );
					short type = node.getNodeType();
					if ( type == Node.ELEMENT_NODE ) {
						Element entry = (Element)node;
						if ( entry.getNodeName().equalsIgnoreCase( GLOBAL_VARIABLE ) ) {
							String name = entry.getAttribute( ATTR_GLOBAL_VARIABLE_NAME );
							String pathString = entry.getAttribute( ATTR_GLOBAL_VARIABLE_PATH );
							IPath path = new Path( pathString );
							if ( path.isValidPath( pathString ) ) {
								descriptors.add( CVariableFactory.createGlobalVariableDescriptor( name, path ) );
							}
						}
					}
				}
				fInitialDescriptors = descriptors.toArray( new IGlobalVariableDescriptor[descriptors.size()] );
				return;
			}
		}
		catch( ParserConfigurationException e ) {
			ex = e;
		}
		catch( SAXException e ) {
			ex = e;
		}
		catch( IOException e ) {
			ex = e;
		}
		abort( InternalDebugCoreMessages.getString( "CGlobalVariableManager.0" ), ex ); //$NON-NLS-1$
	}

	private void initialize() {
		ILaunchConfiguration config = getDebugTarget().getLaunch().getLaunchConfiguration();
		try {
			String memento = config.getAttribute( ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_GLOBAL_VARIABLES, "" ); //$NON-NLS-1$
			if ( memento != null && memento.trim().length() != 0 )
				initializeFromMemento( memento );
		}
		catch( CoreException e ) {
			DebugPlugin.log( e );
		}
	}

	/**
	 * Throws an internal error exception
	 */
	private void abort( String message, Throwable e ) throws CoreException {
		IStatus s = new Status( IStatus.ERROR, CDebugCorePlugin.getUniqueIdentifier(), CDebugCorePlugin.INTERNAL_ERROR, message, e );
		throw new CoreException( s );
	}

	private IGlobalVariableDescriptor[] getInitialDescriptors() {
		return fInitialDescriptors;
	}

	public void save() {
		ILaunchConfiguration config = getDebugTarget().getLaunch().getLaunchConfiguration();
		try {
			ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
			wc.setAttribute( ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_GLOBAL_VARIABLES, getMemento() );
			wc.doSave();
		}
		catch( CoreException e ) {
			DebugPlugin.log( e );
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.ICGlobalVariableManager#getDescriptors()
	 */
	public IGlobalVariableDescriptor[] getDescriptors() {
		if ( fGlobals == null )
			return getInitialDescriptors();
		List<IGlobalVariableDescriptor> descrs = new ArrayList<IGlobalVariableDescriptor>();
		for (ICGlobalVariable global : fGlobals) {
			IGlobalVariableDescriptor descr = global.getDescriptor();
			if (descr != null) {	// children of globals don't have a descriptor, though 'fGlobals' should contain only top level globals
				descrs.add(descr);
			}
		}
		return descrs.toArray(new IGlobalVariableDescriptor[descrs.size()]);
	}
}

Back to the top