1
/*******************************************************************************
2
* Copyright (c) 2009 STMicroelectronics.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Xavier Raynaud <xavier.raynaud@st.com> - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.linuxtools.internal.gprof.test;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.InputStreamReader;
19
import java.io.LineNumberReader;
21
import junit.framework.Test;
22
import junit.framework.TestCase;
23
import junit.framework.TestSuite;
25
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
26
import org.eclipse.core.runtime.Path;
27
import org.eclipse.jface.viewers.ITreeContentProvider;
28
import org.eclipse.linuxtools.binutils.utils.STSymbolManager;
29
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTTreeViewer;
30
import org.eclipse.linuxtools.internal.gprof.action.SwitchContentProviderAction;
31
import org.eclipse.linuxtools.internal.gprof.action.SwitchSampleTimeAction;
32
import org.eclipse.linuxtools.internal.gprof.parser.GmonDecoder;
33
import org.eclipse.linuxtools.internal.gprof.view.CallGraphContentProvider;
34
import org.eclipse.linuxtools.internal.gprof.view.FileHistogramContentProvider;
35
import org.eclipse.linuxtools.internal.gprof.view.FlatHistogramContentProvider;
36
import org.eclipse.linuxtools.internal.gprof.view.FunctionHistogramContentProvider;
37
import org.eclipse.linuxtools.internal.gprof.view.GmonView;
38
import org.eclipse.linuxtools.internal.gprof.view.fields.SampleProfField;
39
import org.eclipse.swt.widgets.TreeColumn;
43
* @author Xavier Raynaud <xavier.raynaud@st.com>
45
public class GprofTest extends TestCase {
46
private static final String GMON_BINARY_FILE = "a.out";
47
private static final String GMON_OUTPUT_FILE = "gmon.out";
48
private static final String GMON_DIRECTORY_SUFFIX = "_gprof_input";
53
public static Test suite() {
54
TestSuite ats = new TestSuite("GProf:View CSV Export");
55
boolean addr2line2_16 = false;
57
Process p = Runtime.getRuntime().exec("addr2line --version");
58
InputStream is = p.getInputStream();
59
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
61
while ((line = reader.readLine()) != null) {
62
if (line.contains("addr2line 2.16.")) {
67
} catch (Exception _) {
70
File[] testDirs = STJunitUtils.getTestDirs("org.eclipse.linuxtools.gprof.test",
71
".*" + GMON_DIRECTORY_SUFFIX);
72
for (File testDir : testDirs) {
73
final File gmonFile = new File(testDir, GMON_OUTPUT_FILE);
74
final File binaryFile = new File(testDir, GMON_BINARY_FILE);
76
File view_cg_RefFile_default = new File(testDir, "testCallgraphView.ref");
77
File view_cg_RefFile_alternate = new File(testDir, "testCallgraphView.ref.binutils-2.16");
78
File view_cg2_RefFile_default = new File(testDir, "testCallgraphTimeView.ref");
79
File view_cg2_RefFile_alternate = new File(testDir, "testCallgraphTimeView.ref.binutils-2.16");
80
final File view_cg_RefFile;
81
final File view_cg2_RefFile;
82
if (addr2line2_16 && view_cg_RefFile_alternate.exists()) {
83
view_cg_RefFile = view_cg_RefFile_alternate;
85
view_cg_RefFile = view_cg_RefFile_default;
87
if (addr2line2_16 && view_cg2_RefFile_alternate.exists()) {
88
view_cg2_RefFile = view_cg2_RefFile_alternate;
90
view_cg2_RefFile = view_cg2_RefFile_default;
92
final File view_cg2_DumpFile = new File(testDir, "testCallgraphTimeView.dump");
93
final File view_cg_DumpFile = new File(testDir, "testCallgraphView.dump");
95
final File view_samplesFile_RefFile = new File(testDir, "testSampleView.ref");
96
final File view_samplesFile_DumpFile = new File(testDir, "testSampleView.dump");
97
final File view_samplesFileT_RefFile = new File(testDir, "testTimeView.ref");
98
final File view_samplesFileT_DumpFile = new File(testDir, "testTimeView.dump");
100
final File view_samplesFunction_RefFile = new File(testDir, "testFunctionSampleView.ref");
101
final File view_samplesFunction_DumpFile = new File(testDir, "testFunctionSampleView.dump");
102
final File view_samplesFunctionT_RefFile = new File(testDir, "testFunctionTimeView.ref");
103
final File view_samplesFunctionT_DumpFile = new File(testDir, "testFunctionTimeView.dump");
104
final File view_samplesFlat_RefFile = new File(testDir, "testFlatSampleView.ref");
105
final File view_samplesFlat_DumpFile = new File(testDir, "testFlatSampleView.dump");
106
final File view_samplesFlatT_RefFile = new File(testDir, "testFlatTimeView.ref");
107
final File view_samplesFlatT_DumpFile = new File(testDir, "testFlatTimeView.dump");
109
IBinaryObject binary = STSymbolManager.sharedInstance.getBinaryObject(
110
new Path(binaryFile.getAbsolutePath()));
111
final GmonDecoder gd = new GmonDecoder(binary, null);
113
gd.read(gmonFile.getAbsolutePath());
114
} catch (IOException e) {
119
new TestCase(testDir.getName() + ":CSV-CALLGRAPH") {
120
public void runTest() throws Throwable {
121
testView(gmonFile, gd, view_cg_RefFile, view_cg_DumpFile,
122
CallGraphContentProvider.sharedInstance, false);
127
new TestCase(testDir.getName() + ":CSV-CALLGRAPH-TIMED") {
128
public void runTest() throws Throwable {
129
testView(gmonFile, gd, view_cg2_RefFile, view_cg2_DumpFile,
130
CallGraphContentProvider.sharedInstance, true);
135
new TestCase(testDir.getName() + ":CSV-FILE") {
136
public void runTest() throws Throwable {
137
testView(gmonFile, gd, view_samplesFile_RefFile,
138
view_samplesFile_DumpFile,
139
FileHistogramContentProvider.sharedInstance, false);
144
new TestCase(testDir.getName() + ":CSV-FILE-TIMED") {
145
public void runTest() throws Throwable {
146
testView(gmonFile, gd, view_samplesFileT_RefFile,
147
view_samplesFileT_DumpFile,
148
FileHistogramContentProvider.sharedInstance, true);
153
new TestCase(testDir.getName() + ":CSV-FUNCTION") {
154
public void runTest() throws Throwable {
155
testView(gmonFile, gd, view_samplesFunction_RefFile,
156
view_samplesFunction_DumpFile,
157
FunctionHistogramContentProvider.sharedInstance, false);
162
new TestCase(testDir.getName() + ":CSV-FUNCTION-TIMED") {
163
public void runTest() throws Throwable {
164
testView(gmonFile, gd, view_samplesFunctionT_RefFile,
165
view_samplesFunctionT_DumpFile,
166
FunctionHistogramContentProvider.sharedInstance, true);
171
new TestCase(testDir.getName() + ":CSV-FLAT") {
172
public void runTest() throws Throwable {
173
testView(gmonFile, gd, view_samplesFlat_RefFile,
174
view_samplesFlat_DumpFile,
175
FlatHistogramContentProvider.sharedInstance, false);
180
new TestCase(testDir.getName() + ":CSV-FLAT-TIMED") {
181
public void runTest() throws Throwable {
182
testView(gmonFile, gd, view_samplesFlatT_RefFile,
183
view_samplesFlatT_DumpFile,
184
FlatHistogramContentProvider.sharedInstance, true);
193
public static void changeMode(GmonView view, boolean timeModeRequested) {
194
AbstractSTTreeViewer gmonViewer = (AbstractSTTreeViewer)view.getSTViewer();
195
GmonDecoder decoder = (GmonDecoder) gmonViewer.getInput();
196
int prof_rate = decoder.getHistogramDecoder().getProf_rate();
197
if (prof_rate == 0) {
201
TreeColumn tc = gmonViewer.getViewer().getTree().getColumn(1);
202
SampleProfField spf = (SampleProfField) tc.getData();
204
if (spf.getColumnHeaderText().endsWith("Samples") ^ !timeModeRequested) {
205
new SwitchSampleTimeAction(view).run();
209
public static void testView(File gmonFile, GmonDecoder gd,
210
File refFile, File dumpFile,
211
ITreeContentProvider contentProvider, boolean timeMode) throws Exception {
212
GmonView view = GmonView.displayGprofView(gd, gmonFile.getAbsolutePath());
213
SwitchContentProviderAction action = new SwitchContentProviderAction("testAction",
214
"icons/ch_callees.png" /*to avoid error*/, view.getSTViewer().getViewer(),
217
changeMode(view, timeMode);
218
STJunitUtils.testCSVExport(view, dumpFile.getAbsolutePath(), refFile.getAbsolutePath());