Skip to main content
summaryrefslogtreecommitdiffstats
blob: e42cc6fea8f29b6aceab5f7cbc834c42be753048 (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
/*******************************************************************************
 * Copyright (c) 2009 STMicroelectronics.
 * 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:
 *   Xavier Raynaud <xavier.raynaud@st.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.gprof.parser;

import java.io.BufferedInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;

import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
import org.eclipse.cdt.core.IBinaryParser.ISymbol;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.binutils.utils.STSymbolManager;
import org.eclipse.linuxtools.internal.gprof.utils.LEDataInputStream;
import org.eclipse.linuxtools.internal.gprof.view.histogram.HistRoot;

/** 
 * Parser of gmon file
 */
public class GmonDecoder {

	/** Histogram record type.  */ 
	public static final int VPF_GMON_RECORD_TYPE_HISTOGRAM = 0;
	/** Callgraph record type. */
	public static final int VPF_GMON_RECORD_TYPE_CALLGRAPH = 1;
	/** Unkwown record type. */
	public static final int VPF_GMON_RECORD_TYPE_UNKNOWN   = -1;
	
	public static final int GMONVERSION = 0x00051879;

	// header
	private String cookie;
	private int gmon_version;
	private byte[] spare;

	private final IBinaryObject program;
	final boolean _32_bit_platform;
	private HistogramDecoder histo;
	private CallGraphDecoder callGraph;
	private final PrintStream ps;
	private final HistRoot rootNode = new HistRoot(this);
	private String file;
	private int tag = -1;
	
	private final HashMap<ISymbol, String> filenames = new HashMap<ISymbol, String>();
	private IProject project;
	
	// for dump
	private boolean shouldDump = false;


	/**
	 * Constructor
	 * @param program 
	 * @throws IOException 
	 */
	public GmonDecoder(IBinaryObject program, IProject project) {
		this(program, null, project);
	}
	
	
	/**
	 * Constructor
	 * @param program 
	 * @throws IOException 
	 */
	public GmonDecoder(IBinaryObject program, PrintStream ps, IProject project) {
		this.program = program;
		this.ps = ps;
		this.project = project;
		program.getBinaryParser().getFormat();
		String cpu = program.getCPU();
		if ("x86_64".equals(cpu) || "ppc64".equals(cpu)){
			histo = new HistogramDecoder_64(this);
			callGraph = new CallGraphDecoder_64(this);
			_32_bit_platform = false;
		}
		else {
			_32_bit_platform = true;
			histo = new HistogramDecoder(this);
			callGraph = new CallGraphDecoder(this);
		}
	}
	

	/**
	 * Reads the given file
	 * @param file
	 * @throws IOException
	 */
	public void read(String file) throws IOException {
		this.file = file;
		DataInputStream beStream = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
		if(program.isLittleEndian()){
			LEDataInputStream leStream = new LEDataInputStream(beStream);
			leStream.mark(1000);
			boolean gmonType = readHeader(leStream);
			if (gmonType) ReadGmonContent(leStream);
			else {
				leStream.reset();
				histo.decodeOldHeader(leStream);
				histo.decodeHistRecord(leStream);
				try {
					do {
						this.callGraph.decodeCallGraphRecord(leStream, true);
					} while (true);
				} catch (EOFException _) {
					// normal. End of file reached.
				}
				this.callGraph.populate(rootNode);
				this.histo.AssignSamplesSymbol();	
			}
			leStream.close();
		} else {
			beStream.mark(1000);
			boolean gmonType = readHeader(beStream);
			if (gmonType) ReadGmonContent(beStream);
			else {
				beStream.reset();
				histo.decodeOldHeader(beStream);
				histo.decodeHistRecord(beStream);
				try {
					do {
						this.callGraph.decodeCallGraphRecord(beStream, true);
					} while (true);
				} catch (EOFException _) {
					// normal. End of file reached.
				}
				this.callGraph.populate(rootNode);
				this.histo.AssignSamplesSymbol();	
			}
			beStream.close();
		}
	}



	/**
	 * Read gmon header
	 * @param stream the gmon as a stream
	 * @throws IOException if an IO error occurs or if the stream is not a gmon file.
	 */
	public boolean readHeader(DataInput stream) throws IOException {
		byte[] _cookie = new byte[4];
		stream.readFully(_cookie);
		cookie = new String(_cookie);
		gmon_version = stream.readInt();
		spare = new byte[12];
		stream.readFully(spare);
		return "gmon".equals(cookie);
	}

	/**
	 * Read the whole content of the GMON file
	 * The header should be read before calling this function.
	 * @param stream
	 * @throws IOException 
	 */
	public void ReadGmonContent(DataInput stream) throws IOException
	{  
		do {
			//int tag = -1;
			tag = -1;
			
			try {
				tag = stream.readByte();
			} catch (EOFException _) {
				break;
			}
			switch (tag) {
			case VPF_GMON_RECORD_TYPE_HISTOGRAM:
				histo.decodeHeader(stream);
				histo.decodeHistRecord(stream);
				break;
			case VPF_GMON_RECORD_TYPE_CALLGRAPH:
				callGraph.decodeCallGraphRecord(stream, false);
				break;
			default:
				throw new IOException("Error while reading GMON content : Found bad tag (file corrupted?) ");
			}
			
			if (shouldDump == true)
			 dumpGmonResult(ps==null?System.out:ps);
						
		} while (true);

		this.callGraph.populate(rootNode);
		this.histo.AssignSamplesSymbol();	
	
	}

	
	public void dumpGmonResult(PrintStream ps) throws FileNotFoundException{
		
		ps.println("-- gmon Results --");
		ps.println("cookie "+cookie);
		ps.println("gmon_version "+gmon_version);
		//ps.println("spare "+new String(spare));
		ps.println("tag "+tag);
		
		switch (tag) {
		case VPF_GMON_RECORD_TYPE_HISTOGRAM:
			histo.printHistHeader(ps);
			histo.printHistRecords(ps);
			break;
		default :
			break;
		}
	}
	
	
	
	/**
	 * @return the histogram decoder
	 */
	public HistogramDecoder getHistogramDecoder() {
		return histo;
	}

	/**
	 * @return the call graph decoder
	 */
	public CallGraphDecoder getCallGraphDecoder() {
		return callGraph;
	}

	/**
	 * @return the program
	 */
	public IBinaryObject getProgram() {
		return program;
	}

	/**
	 * @return the rootNode
	 */
	public HistRoot getRootNode() {
		return rootNode;
	}

	/**
	 * Gets the version number parsed in the gmon file
	 * @return a gmon version
	 */
	public int getGmonVersion() {
		return gmon_version;
	}

	/**
	 * @return the (last) parsed gmon file
	 */
	public String getGmonFile() {
		return file;
	}


	public String getFileName(ISymbol s) {
		String ret = filenames.get(s);
		if (ret == null) {
			ret = STSymbolManager.sharedInstance.getFilename(s, project);
			if (ret == null) ret = "??";
			filenames.put(s, ret);
		}
		return ret;
	}


	public boolean isICache() {
		IPath p = new Path(this.file);
		String s = p.lastSegment();
		return (s.endsWith("ICACHE"));
	}

	public boolean isDCache() {
		IPath p = new Path(this.file);
		String s = p.lastSegment();
		return (s.endsWith("DCACHE"));
	}

	public void setShouldDump(boolean shouldDump) {
		this.shouldDump = shouldDump;
	}

	public IProject getProject() {
		return this.project;
	}
}

Back to the top