Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c153534f7f061a0b837a048473287d8f81fa51b2 (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
/*******************************************************************************
 * Copyright (c) 2004,2008 Red Hat, Inc.
 * 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:
 *    Keith Seitz <keiths@redhat.com> - initial API and implementation
 *    Kent Sebastian <ksebasti@redhat.com> - 
 *******************************************************************************/
package org.eclipse.linuxtools.internal.oprofile.launch.configuration;

import java.text.MessageFormat;

import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.linuxtools.internal.oprofile.core.Oprofile;
import org.eclipse.linuxtools.internal.oprofile.core.daemon.OprofileDaemonOptions;
import org.eclipse.linuxtools.internal.oprofile.launch.OprofileLaunchMessages;
import org.eclipse.linuxtools.internal.oprofile.launch.OprofileLaunchPlugin;
import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy;
import org.eclipse.linuxtools.profiling.launch.RemoteProxyManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This tab is used by the launcher to configure global oprofile run options.
 */
public class OprofileSetupTab extends AbstractLaunchConfigurationTab {
	protected Text kernelImageFileText;
	
	protected Button checkSeparateLibrary;
	protected Button checkSeparateKernel;
	//maybe these later
//	protected Button _checkSeparateThread;
//	protected Button _checkSeparateCpu;

	protected LaunchOptions options = null;

	private IRemoteFileProxy proxy;

	public String getName() {
		return OprofileLaunchMessages.getString("tab.global.name"); //$NON-NLS-1$
	}

	@Override
	public boolean isValid(ILaunchConfiguration config) {
		boolean b = options.isValid();
		// System.out.println("SetupTab isValid = " + b);
		return b;
	}

	public void performApply(ILaunchConfigurationWorkingCopy config) {
		options.saveConfiguration(config);
	}

	public void initializeFrom(ILaunchConfiguration config) {
		options.loadConfiguration(config);
		
		kernelImageFileText.setText(options.getKernelImageFile());
		
		int separate = options.getSeparateSamples();
		
		if (separate == OprofileDaemonOptions.SEPARATE_NONE) {
			checkSeparateLibrary.setSelection(false);
			checkSeparateKernel.setSelection(false);
		} else {
			//note that opcontrol will nicely ignore the trailing comma
			if ((separate & OprofileDaemonOptions.SEPARATE_LIBRARY) != 0)
				checkSeparateLibrary.setSelection(true);
			if ((separate & OprofileDaemonOptions.SEPARATE_KERNEL) != 0)
				checkSeparateKernel.setSelection(true);
//			if ((separate & OprofileDaemonOptions.SEPARATE_THREAD) != 0)
//				_checkSeparateThread.setSelection(true);
//			if ((separate & OprofileDaemonOptions.SEPARATE_CPU) != 0)
//				_checkSeparateCpu.setSelection(true);
		}
	}

	public void setDefaults(ILaunchConfigurationWorkingCopy config) {
		options = new LaunchOptions();
		options.saveConfiguration(config);
	}
	
	@Override
	public Image getImage() {
		return OprofileLaunchPlugin.getImageDescriptor(OprofileLaunchPlugin.ICON_GLOBAL_TAB).createImage();
	}

	public void createControl(Composite parent) {
		options = new LaunchOptions();

		Composite top = new Composite(parent, SWT.NONE);
		setControl(top);
		top.setLayout(new GridLayout());

		GridData data;
		GridLayout layout;
		createVerticalSpacer(top, 1);

		// Create container for kernel image file selection
		Composite p = new Composite(top, SWT.NONE);
		layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		p.setLayout(layout);
		data = new GridData(GridData.FILL_HORIZONTAL);
		p.setLayoutData(data);

		Label l = new Label(p, SWT.NONE);
		l.setText(OprofileLaunchMessages.getString("tab.global.kernelImage.label.text")); //$NON-NLS-1$
		data = new GridData();
		data.horizontalSpan = 2;
		l.setLayoutData(data);

		kernelImageFileText = new Text(p, SWT.SINGLE | SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		kernelImageFileText.setLayoutData(data);
		kernelImageFileText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent mev) {
				handleKernelImageFileTextModify(kernelImageFileText);
			};
		});

		Button button = createPushButton(p, OprofileLaunchMessages.getString("tab.global.kernelImage.browse.button.text"), null); //$NON-NLS-1$
		final Shell shell = top.getShell();
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent sev) {
				showFileDialog(shell);
			}
		});

		createVerticalSpacer(top, 1);

		// Create checkbox options container
		p = new Composite(top, SWT.NONE);
		layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		p.setLayout(layout);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		p.setLayoutData(data);

		checkSeparateLibrary = myCreateCheckButton(p, OprofileLaunchMessages.getString("tab.global.check.separateLibrary.text")); //$NON-NLS-1$
		checkSeparateKernel = myCreateCheckButton(p, OprofileLaunchMessages.getString("tab.global.check.separateKernel.text")); //$NON-NLS-1$
//		_checkSeparateThread = _createCheckButton(p, OprofileLaunchMessages.getString("tab.global.check.separateThread.text")); //$NON-NLS-1$
//		_checkSeparateCpu = _createCheckButton(p, OprofileLaunchMessages.getString("tab.global.check.separateCpu.text")); //$NON-NLS-1$
	}

	// convenience method to create radio buttons with the given label
	private Button myCreateCheckButton(Composite parent, String label) {
		final Button b = new Button(parent, SWT.CHECK);
		b.setText(label);
		b.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent se) {
				handleCheckSelected(b);
			}
		});

		return b;
	}

	//sets the proper separation mask for sample separation 
	private void handleCheckSelected(Button button) {
		int oldSeparate = options.getSeparateSamples();
		int newSeparate = oldSeparate;		//initalize
		
		if (button == checkSeparateLibrary) {
			if (button.getSelection()) {
				newSeparate = oldSeparate | OprofileDaemonOptions.SEPARATE_LIBRARY;
			} else {
				newSeparate = oldSeparate & ~OprofileDaemonOptions.SEPARATE_LIBRARY;
			}
		} else if (button == checkSeparateKernel) {
			if (button.getSelection()) {
				newSeparate = oldSeparate | OprofileDaemonOptions.SEPARATE_KERNEL;
			} else {
				newSeparate = oldSeparate & ~OprofileDaemonOptions.SEPARATE_KERNEL;
			}
//		} else if (button == _checkSeparateThread) {
//			if (button.getSelection()) {
//				newSeparate = oldSeparate | OprofileDaemonOptions.SEPARATE_THREAD;
//			} else {
//				newSeparate = oldSeparate & ~OprofileDaemonOptions.SEPARATE_THREAD;
//			}
//		} else if (button == _checkSeparateCpu) {
//			if (button.getSelection()) {
//				newSeparate = oldSeparate | OprofileDaemonOptions.SEPARATE_CPU;
//			} else {
//				newSeparate = oldSeparate & ~OprofileDaemonOptions.SEPARATE_CPU;
//			}
		}
		
		options.setSeparateSamples(newSeparate);

		updateLaunchConfigurationDialog();
	}

	// handles text modification events for all text boxes in this tab
	private void handleKernelImageFileTextModify(Text text) {
		String errorMessage = null;
		String filename = text.getText();

		if (filename.length() > 0) {
			try {
				proxy = RemoteProxyManager.getInstance().getFileProxy(Oprofile.OprofileProject.getProject());
			} catch (CoreException e) {
				e.printStackTrace();
			}
			IFileStore fileStore = proxy.getResource(filename);
			if (!fileStore.fetchInfo().exists() || fileStore.fetchInfo().isDirectory()){
				String msg = OprofileLaunchMessages.getString("tab.global.kernelImage.kernel.nonexistent"); //$NON-NLS-1$
				Object[] args = new Object[] { filename };
				errorMessage = MessageFormat.format(msg, args);
			}

			//seems odd, but must set it even if it is invalid so that performApply
			// and isValid work properly
			options.setKernelImageFile(filename);
		} else {
			// no kernel image file
			options.setKernelImageFile(""); //$NON-NLS-1$
		}

		// Update dialog and error message
		setErrorMessage(errorMessage);
		updateLaunchConfigurationDialog();
	}

	// Displays a file dialog to allow the user to select the kernel image file
	private void showFileDialog(Shell shell) {
		try {
			proxy = RemoteProxyManager.getInstance().getFileProxy(Oprofile.OprofileProject.getProject());
		} catch (CoreException e) {
			e.printStackTrace();
		}

		FileDialog d = new FileDialog(shell, SWT.OPEN);
		IFileStore kernel = proxy.getResource(options.getKernelImageFile());
		if (!kernel.fetchInfo().exists()) {
			kernel = proxy.getResource("/boot");	//$NON-NLS-1$

			if (!kernel.fetchInfo().exists())
				kernel = proxy.getResource("/");	//$NON-NLS-1$
		}
		d.setFileName(kernel.toString());
		d.setText(OprofileLaunchMessages.getString("tab.global.selectKernelDialog.text")); //$NON-NLS-1$
		String newKernel = d.open();
		if (newKernel != null) {
			kernel = proxy.getResource(newKernel);
			if (!kernel.fetchInfo().exists()) {
				MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.RETRY | SWT.CANCEL);
				mb.setMessage(OprofileLaunchMessages.getString("tab.global.selectKernelDialog.error.kernelDoesNotExist.text")); 	//$NON-NLS-1$
				switch (mb.open()) {
					case SWT.RETRY:
						// Ok, it's recursive, but it shouldn't matter
						showFileDialog(shell);
						break;
					default:
					case SWT.CANCEL:
						break;
				}
			} else {
				kernelImageFileText.setText(newKernel);
			}
		}
	}
}

Back to the top