Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 99db55ff929f9cf62b85ba8ee3e9a943dae980b4 (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
/*******************************************************************************
 * Copyright (c) 2012, 2015 Tilera 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:
 *     William R. Swanson (Tilera Corporation) - initial API and implementation
 *     Marc Khouzam (Ericsson)                 - Added knowledge about execution 
 *                                               state and os/gdb thread ids
 *     Marc Dumais (Ericsson) -  Bug 405390
 *     Marc Dumais (Ericsson) -  Bug 409965
 *     Xavier Raynaud (Kalray) - Add tooltip support (Bug 431935)
 *******************************************************************************/

package org.eclipse.cdt.dsf.gdb.multicorevisualizer.internal.ui.model;

import org.eclipse.cdt.dsf.debug.service.IStack.IFrameDMData;


/** Represents single thread. */
public class VisualizerThread
	implements Comparable<VisualizerThread>, IVisualizerModelObject
{
	// --- members ---
	
	/** Current core this thread is on. */
	protected VisualizerCore m_core;
	
	/** Process ID (pid). */
	protected int m_pid;
	
	/** OS Thread ID (tid). */
	protected int m_tid;

	/** Thread ID as chosen by GDB. */
	protected int m_gdbtid;

	/** Thread execution state. */
	protected VisualizerExecutionState m_threadState;

	/** Location of this Thread, if any, based on his MIFrame */
    protected String m_locInfo;

	
	// --- constructors/destructors ---

	/** Constructor. */
	public VisualizerThread(VisualizerCore core, int pid, int tid, int gdbtid, VisualizerExecutionState state) {
		this(core, pid, tid, gdbtid, state, null);
	}

	/** Constructor. */
	public VisualizerThread(VisualizerCore core, int pid, int tid, int gdbtid, VisualizerExecutionState state, IFrameDMData frame) {
		m_core = core;
		m_pid = pid;
		m_tid = tid;
		m_gdbtid = gdbtid;
		m_threadState = state;
		setLocationInfo(frame);
	}
	
	/** Dispose method */
	public void dispose() {
		m_core = null;
	}
	
	
	// --- Object methods ---
	
	/** Equality comparison. */
	@Override
	public boolean equals(Object obj) {
		boolean result = false;
		if (obj instanceof VisualizerThread) {
			VisualizerThread v = (VisualizerThread) obj;
			result = (
				v.m_pid == m_pid &&
				v.m_tid == m_tid &&
				v.m_gdbtid == m_gdbtid
			);
		}
		return result;
	}

	@Override
	public int hashCode() {
		return m_pid ^ m_tid ^ m_gdbtid;
	}
	
	/** Returns string representation. */
	@Override
	public String toString() {
		StringBuffer output = new StringBuffer();
		output.append(m_core).append(",Proc:").append(m_pid) //$NON-NLS-1$
		      .append(",Thread:(").append(m_tid).append(",").append(m_gdbtid).append(")");  //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
		return output.toString();
	}

	
	// --- accessors ---
	
	/** Gets core. */
	public VisualizerCore getCore()	{
		return m_core;
	}
	
	public void setCore(VisualizerCore core) {
		m_core = core;
	}

	/** Returns true if this is the "process" thread, i.e.
	 *  its PID and OS TID are the same.
	 */
	public boolean isProcessThread()
	{
		return m_pid == m_tid;
	}
	
	/** Gets process id (pid). */
	public int getPID() {
		return m_pid;
	}
	
	/** Gets thread id (tid). */
	public int getTID()	{
		return m_tid;
	}
	
	/** Sets thread id (tid). */
	public void setTID(int tid)	{
		m_tid = tid;
	}

	/** Gets thread id (gdbtid). */
	@Override
	public int getID() {
		return getGDBTID();
	}

	/** Return core the thread is on */
	@Override
	public IVisualizerModelObject getParent() {
		return getCore();
	}
	
	/** Gets gdb thread id. */
	public int getGDBTID()	{
		return m_gdbtid;
	}

	/** Gets thread execution state. */
	public VisualizerExecutionState getState() {
		return m_threadState;
	}
	
	/** Sets thread execution state. */
	public void setState(VisualizerExecutionState state) {
		m_threadState = state;
	}
	
	
	// --- methods ---
	

	
	// --- Comparable implementation ---
	
	/** Compares this item to the specified item. */
	@Override
	public int compareTo(VisualizerThread o) {
		int result = 0;
		if (o != null) {
			if (m_pid < o.m_pid) {
				result = -1;
			}
			else if (m_pid > o.m_pid) {
				result = 1;
			}
			else if (getID() < o.getID()) {
				result = -1;
			}
			else if (getID() > o.getID()) {
				result = 1;
			}
		}
		return result;
	}
	
	/** IVisualizerModelObject version of compareTo() */
	@Override
	public int compareTo(IVisualizerModelObject o) {
		if (o != null) {
			if (o.getClass() == this.getClass()) {
				return compareTo((VisualizerThread)o);
			}
		}
		return 1;
	}

	/**
	 * Sets the location info of this thread
	 * @param s a string, displayinf location information of this thread.
	 */
    public void setLocationInfo(String s) {
        this.m_locInfo = s;
    }

	/**
	 * Sets the location info of this thread, based on given
	 * {@link IFrameDMData}
	 * 
	 * @param dmData
	 *            a {@link IFrameDMData} (can be <code>null</code>)
	 */
	public void setLocationInfo(IFrameDMData dmData) {
		if (dmData == null) {
			this.m_locInfo = null;
		} else {
			StringBuilder label = new StringBuilder();
			// Add the function name
			if (dmData.getFunction() != null
					&& dmData.getFunction().length() != 0) {
				label.append(" "); //$NON-NLS-1$
				label.append(dmData.getFunction());
				label.append("()"); //$NON-NLS-1$
			}

			boolean hasFileName = dmData.getFile() != null
					&& dmData.getFile().length() != 0;

			// Add full file name
			if (hasFileName) {
				label.append(" at "); //$NON-NLS-1$
				label.append(dmData.getFile());

				// Add line number
				if (dmData.getLine() >= 0) {
					label.append(":"); //$NON-NLS-1$
					label.append(dmData.getLine());
					label.append(" "); //$NON-NLS-1$
				}
			}

			// Add module
			if (!hasFileName
					&& (dmData.getModule() != null && dmData.getModule()
							.length() != 0)) {
				label.append(" "); //$NON-NLS-1$
				label.append(dmData.getModule());
				label.append(" "); //$NON-NLS-1$
			}

			// Add the address
			if (dmData.getAddress() != null) {
				label.append("- 0x" + dmData.getAddress().toString(16)); //$NON-NLS-1$
			}
			this.m_locInfo = label.toString();
		}
	}

	/**
	 * Gets the location of this thread or <code>null</code> if none.
	 * 
	 * @return a String, or <code>null</code>
	 * @since 3.0
	 */
	public String getLocationInfo() {
		if (m_threadState == VisualizerExecutionState.RUNNING
				|| m_threadState == VisualizerExecutionState.EXITED) {
			return null;
		}
		return m_locInfo;
	}

}

Back to the top