Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98d2248ebb20f0c05fc9a54b23084386b3e9f57c (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
/*******************************************************************************
 * Copyright (c) 2000, 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
 *     Ling Wang, Nokia - Bug 179425
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.core.model;

import com.ibm.icu.text.MessageFormat;
import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.debug.core.cdi.CDIException;
import org.eclipse.cdt.debug.core.cdi.ICDISession;
import org.eclipse.cdt.debug.core.cdi.model.ICDITarget;
import org.eclipse.cdt.debug.core.model.CDebugElementState;
import org.eclipse.cdt.debug.core.model.ICDebugElement;
import org.eclipse.cdt.debug.core.model.ICDebugElementStatus;
import org.eclipse.cdt.debug.core.model.ICDebugTarget;
import org.eclipse.cdt.debug.core.model.IModuleRetrieval;
import org.eclipse.cdt.debug.internal.core.ICDebugInternalConstants;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
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.ILaunch;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IMemoryBlockRetrieval;

/**
 * The super class of all C/C++ debug model elements.
 */
abstract public class CDebugElement extends PlatformObject implements ICDebugElement, ICDebugElementStatus {

	/**
	 * Debug target associated with this element
	 */
	private CDebugTarget fDebugTarget;

	/**
	 * The severity code of this element's status 
	 */
	private int fSeverity = ICDebugElementStatus.OK;

	/**
	 * The message of this element's status 
	 */
	private String fMessage = null;

	/**
	 * The current state of this element.
	 */
	private CDebugElementState fState = CDebugElementState.UNDEFINED;

	/**
	 * The previous state of this element.
	 */
	private CDebugElementState fOldState = CDebugElementState.UNDEFINED;

	/**
	 * The current state info.
	 */
	private Object fCurrentStateInfo = null;

	/**
	 * Constructor for CDebugElement.
	 */
	public CDebugElement( CDebugTarget target ) {
		setDebugTarget( target );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
	 */
	@Override
	public String getModelIdentifier() {
		return CDIDebugModel.getPluginIdentifier();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
	 */
	@Override
	public IDebugTarget getDebugTarget() {
		return fDebugTarget;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
	 */
	@Override
	public ILaunch getLaunch() {
		return getDebugTarget().getLaunch();
	}

	protected void setDebugTarget( CDebugTarget target ) {
		fDebugTarget = target;
	}

	/**
	 * Convenience method to log errors
	 */
	protected void logError( Exception e ) {
		DebugPlugin.log( e );
	}

	/**
	 * Convenience method to log errors
	 *  
	 */
	protected void logError( String message ) {
		DebugPlugin.logMessage( message, null );
	}

	/**
	 * Fires a debug event
	 * 
	 * @param event The debug event to be fired to the listeners
	 * @see org.eclipse.debug.core.DebugEvent
	 */
	protected void fireEvent( DebugEvent event ) {
		DebugPlugin.getDefault().fireDebugEventSet( new DebugEvent[]{ event } );
	}

	protected void fireEventSet( DebugEvent[] events ) {
		DebugPlugin.getDefault().fireDebugEventSet( events );
	}

	/**
	 * Fires a debug event marking the creation of this element.
	 */
	public void fireCreationEvent() {
		fireEvent( new DebugEvent( this, DebugEvent.CREATE ) );
	}

	public DebugEvent createCreateEvent() {
		return new DebugEvent( this, DebugEvent.CREATE );
	}

	/**
	 * Fires a debug event marking the RESUME of this element with the associated detail.
	 * 
	 * @param detail The int detail of the event
	 * @see org.eclipse.debug.core.DebugEvent
	 */
	public void fireResumeEvent( int detail ) {
		fireEvent( new DebugEvent( this, DebugEvent.RESUME, detail ) );
	}

	public DebugEvent createResumeEvent( int detail ) {
		return new DebugEvent( this, DebugEvent.RESUME, detail );
	}

	/**
	 * Fires a debug event marking the SUSPEND of this element with the associated detail.
	 * 
	 * @param detail The int detail of the event
	 * @see org.eclipse.debug.core.DebugEvent
	 */
	public void fireSuspendEvent( int detail ) {
		fireEvent( new DebugEvent( this, DebugEvent.SUSPEND, detail ) );
	}

	public DebugEvent createSuspendEvent( int detail ) {
		return new DebugEvent( this, DebugEvent.SUSPEND, detail );
	}

	/**
	 * Fires a debug event marking the termination of this element.
	 */
	public void fireTerminateEvent() {
		fireEvent( new DebugEvent( this, DebugEvent.TERMINATE ) );
	}

	public DebugEvent createTerminateEvent() {
		return new DebugEvent( this, DebugEvent.TERMINATE );
	}

	/**
	 * Fires a debug event marking the CHANGE of this element with the specifed detail code.
	 * 
	 * @param detail
	 *            one of <code>STATE</code> or <code>CONTENT</code>
	 */
	public void fireChangeEvent( int detail ) {
		fireEvent( new DebugEvent( this, DebugEvent.CHANGE, detail ) );
	}

	public DebugEvent createChangeEvent( int detail ) {
		return new DebugEvent( this, DebugEvent.CHANGE, detail );
	}

	/**
	 * Returns the CDI session associated with this element.
	 * 
	 * @return the CDI session
	 */
	public ICDISession getCDISession() {
		final ICDITarget cdiTarget = getCDITarget();
		return cdiTarget != null ? cdiTarget.getSession() : null;
	}

	/**
	 * Returns the underlying CDI target associated with this element.
	 * 
	 * @return the underlying CDI target
	 */
	public ICDITarget getCDITarget() {
		return (ICDITarget)getDebugTarget().getAdapter( ICDITarget.class );
	}

	/**
	 * Throws a new debug exception with a status code of <code>REQUEST_FAILED</code>.
	 * 
	 * @param message Failure message
	 * @param e Exception that has occurred (<code>can be null</code>)
	 * @throws DebugException The exception with a status code of <code>REQUEST_FAILED</code>
	 */
	public static void requestFailed( String message, Exception e ) throws DebugException {
		requestFailed( message, e, DebugException.REQUEST_FAILED );
	}

	/**
	 * Throws a new debug exception with a status code of <code>TARGET_REQUEST_FAILED</code> with the given underlying exception. 
	 * 
	 * @param message Failure message
	 * @param e underlying exception that has occurred
	 * @throws DebugException The exception with a status code of <code>TARGET_REQUEST_FAILED</code>
	 */
	public static void targetRequestFailed( String message, CDIException e ) throws DebugException {
		String format = "Target request failed: {0}";   //$NON-NLS-1$
		// Append a period only when incoming message does not end with one.  
		if ( !message.endsWith( "." ) ) //$NON-NLS-1$
			format += "."; //$NON-NLS-1$

		requestFailed( MessageFormat.format( format, new String[] { message } ), e, DebugException.TARGET_REQUEST_FAILED );
	}

	/**
	 * Throws a new debug exception with the given status code.
	 * 
	 * @param message Failure message
	 * @param e Exception that has occurred (<code>can be null</code>)
	 * @param code status code
	 * @throws DebugException a new exception with given status code
	 */
	public static void requestFailed( String message, Throwable e, int code ) throws DebugException {
		throwDebugException( message, code, e );
	}

	/**
	 * Throws a new debug exception with a status code of <code>TARGET_REQUEST_FAILED</code>.
	 * 
	 * @param message Failure message
	 * @param e Throwable that has occurred
	 * @throws DebugException The exception with a status code of <code>TARGET_REQUEST_FAILED</code>
	 */
	public static void targetRequestFailed( String message, Throwable e ) throws DebugException {
		String format = "Target request failed: {0}";   //$NON-NLS-1$
		// Append a period only when incoming message does not end with one.  
		if ( !message.endsWith( "." ) ) //$NON-NLS-1$
			format += "."; //$NON-NLS-1$

		throwDebugException( MessageFormat.format( format, new String[]{ message } ), DebugException.TARGET_REQUEST_FAILED, e );
	}

	/**
	 * Throws a new debug exception with a status code of <code>NOT_SUPPORTED</code>.
	 * 
	 * @param message Failure message
	 * @throws DebugException The exception with a status code of <code>NOT_SUPPORTED</code>.
	 */
	public static void notSupported( String message ) throws DebugException {
		throwDebugException( message, DebugException.NOT_SUPPORTED, null );
	}

	/**
	 * Throws a debug exception with the given message, error code, and underlying exception.
	 */
	protected static void throwDebugException( String message, int code, Throwable exception ) throws DebugException {
		throw new DebugException( new Status( IStatus.ERROR, CDIDebugModel.getPluginIdentifier(), code, message, exception ) );
	}

	protected void infoMessage( Throwable e ) {
		IStatus newStatus = new Status( IStatus.INFO, CDebugCorePlugin.getUniqueIdentifier(), ICDebugInternalConstants.STATUS_CODE_INFO, e.getMessage(), null );
		CDebugUtils.info( newStatus, getDebugTarget() );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
	 */
	@Override
	public Object getAdapter( Class adapter ) {
		if ( adapter.equals( IDebugElement.class ) )
			return this;
		if ( adapter.equals( ICDebugElement.class ) )
			return this;
		if ( adapter.equals( CDebugElement.class ) )
			return this;
		if ( adapter.equals( ICDebugElementStatus.class ) )
			return this;
		if ( adapter.equals( ICDISession.class ) )
			return getCDISession();
		if ( adapter.equals( ICDebugTarget.class ) )
			return getDebugTarget();
		if ( adapter.equals( IDebugTarget.class ) )
			return getDebugTarget();
		// See bug #100261
		if ( adapter.equals( IMemoryBlockRetrieval.class ) )
			return getDebugTarget().getAdapter( adapter );
		if ( adapter.equals( IModuleRetrieval.class ) )
			return getDebugTarget().getAdapter( adapter );
		if ( adapter.equals( ILaunch.class ) )
			return getDebugTarget().getLaunch();
		return super.getAdapter( adapter );
	}

	protected void setStatus( int severity, String message ) {
		fSeverity = severity;
		fMessage = message;
		if ( fMessage != null )
			fMessage = fMessage.trim();
	}

	protected void resetStatus() {
		fSeverity = ICDebugElementStatus.OK;
		fMessage = null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICDebugElementStatus#isOK()
	 */
	@Override
	public boolean isOK() {
		return (fSeverity == ICDebugElementStatus.OK);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICDebugElementStatus#getSeverity()
	 */
	@Override
	public int getSeverity() {
		return fSeverity;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICDebugElementStatus#getMessage()
	 */
	@Override
	public String getMessage() {
		return fMessage;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICDebugElement#getState()
	 */
	@Override
	public CDebugElementState getState() {
		return fState;
	}

	protected synchronized void setState( CDebugElementState state ) throws IllegalArgumentException {
		fOldState = fState;
		fState = state;
	}

	protected synchronized void restoreState() {
		fState = fOldState;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.model.ICDebugElement#getCurrentStateInfo()
	 */
	@Override
	public Object getCurrentStateInfo() {
		return fCurrentStateInfo;
	}

	protected void setCurrentStateInfo( Object currentStateInfo ) {
		fCurrentStateInfo = currentStateInfo;
	}
}

Back to the top