Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c4b1af4da59fa3ba64b817119ec1d2a1e644512 (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
/*******************************************************************************
 * Copyright (c) 2009 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:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.callgraph.launch.tests;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import junit.framework.TestCase;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.linuxtools.callgraph.launch.LaunchStapGraph;
import org.eclipse.linuxtools.internal.callgraph.core.LaunchConfigurationConstants;

public class SystemTapCommandLineTest extends TestCase {
	File tmpfile = new File("");
	public final String currentPath = tmpfile.getAbsolutePath();
	
	public String stapCommand;
	public final String scriptPath = currentPath+"/stapscript";
	public String binaryPath = "";
	public final String graphDataPath = currentPath+"/graph_data_output.graph";
	public final String parseFunctionPath = currentPath+"/parse_function_nomark.stp";
	
	
	
	
	//FOR TESTING RAW STAP SCRIPT OUTPUT
	public String getCommandOutput(String command, boolean needsBinary){
		Runtime rt = Runtime.getRuntime();
		try {
			//CREATE/ACCESS A TEMPORARY FILE TO HOLD THE SCRIPT
			File file = new File(scriptPath);
			file.createNewFile();
			
			//WRITE THE COMMAND TO THE FILE
			BufferedWriter wbuff = new BufferedWriter(new FileWriter(file));
			wbuff.write(command);
			wbuff.close();
			
			//EXECUTE THE COMMAND
			Process pr = null;
			rt.exec("kill stap");
			if (needsBinary){
				pr = rt.exec("stap -c '"+binaryPath+ "' "+ scriptPath + " " + binaryPath);
			}else{
				pr = rt.exec("stap "+scriptPath);				
			}
			pr.waitFor();
			
			InputStream inpstr = pr.getInputStream();
			BufferedReader rbuff = new BufferedReader (new InputStreamReader(inpstr));
			String line = "";
			String text = "";
			
			//READ THE STANDARD OUTPUT OF COMMAND
			while ((line = rbuff.readLine()) != null){
				text += line;
			}
			
			rbuff.close();
			return text;
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public void testBasicStapScript() {
		if (!TestConstants.canRunStap)
			return;
		final String expected = "probe_beginprobe_end";
		
		String command = "probe begin { " +
							"printf(\"probe_begin\")" +
							"exit()" +
						"}" +
						"probe end {" + 
							"printf(\"probe_end\")" +
						"}";
		String actual = getCommandOutput(command, false);
		
		assertEquals(expected, actual);
	}
	
	
	
	public void testFunctionProbes(){
		binaryPath = currentPath+"/basic";
		final String expected = "mainfoo";
		
		String command = "probe process(@1).function(\"*\"){ printf(\"%s\",probefunc()) }";
		String actual = getCommandOutput(command, true);
		
		assertEquals(expected, actual);
		
	}

	public void testBasicOperations(){
		if (!TestConstants.canRunStap)
			return;
		final String expected = "01234";
		
		String command = "global map\n" +
		"global num\n" +
		"probe begin {" +
		"for (num=0; num<5; num++){"+
		"map[num]=num"+
		"}"+
		"exit()"+
		"}"+
		"probe end {" +
		"foreach (tmp in map){"+
		"printf(\"%d\",map[tmp])"+
		"}"+
		"}";
		String actual = getCommandOutput(command, false);
		
		assertEquals(expected, actual);
		
	}

	public void testFailure(){
		try {
			LaunchStapGraph shortcut = new LaunchStapGraph();
			ILaunchConfiguration config = shortcut.outsideGetLaunchConfigType().newInstance(null, "Temp Name");
			ILaunchConfigurationWorkingCopy wc = config.copy("Temp Name");
			
			wc.setAttribute(LaunchConfigurationConstants.BINARY_PATH,currentPath + "/basic");
			wc.setAttribute(LaunchConfigurationConstants.SCRIPT_PATH,scriptPath);
			wc.setAttribute(LaunchConfigurationConstants.ARGUMENTS,"-e'()'");
			wc.setAttribute(LaunchConfigurationConstants.OUTPUT_PATH,graphDataPath);
			config = wc.doSave();
			config.launch("profile", null);
			
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	
	public void initializeFiles(){
		File scriptFile = new File(scriptPath);
		File graphDataFile = new File(graphDataPath);
		
		try {
			scriptFile.createNewFile();
			graphDataFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

Back to the top