|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Utility functions for rules package |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.engine.rules; |
|
20 |
|
21 import java.util.Iterator; |
|
22 import java.util.List; |
|
23 |
|
24 import com.nokia.tracecompiler.engine.TraceCompilerEngineConfiguration; |
|
25 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals; |
|
26 import com.nokia.tracecompiler.model.Trace; |
|
27 import com.nokia.tracecompiler.model.TraceCompilerException; |
|
28 import com.nokia.tracecompiler.model.TraceConstantTable; |
|
29 import com.nokia.tracecompiler.model.TraceModel; |
|
30 import com.nokia.tracecompiler.model.TraceModelExtension; |
|
31 import com.nokia.tracecompiler.model.TraceObjectUtils; |
|
32 import com.nokia.tracecompiler.model.TraceParameter; |
|
33 import com.nokia.tracecompiler.source.ParsedType; |
|
34 import com.nokia.tracecompiler.source.SourceContext; |
|
35 import com.nokia.tracecompiler.source.SourceParserException; |
|
36 import com.nokia.tracecompiler.source.SourceReturn; |
|
37 import com.nokia.tracecompiler.source.TypeMapping; |
|
38 |
|
39 /** |
|
40 * Utility functions for rules package |
|
41 * |
|
42 */ |
|
43 public final class RuleUtils { |
|
44 |
|
45 /** |
|
46 * Entry-exit template |
|
47 */ |
|
48 public static final int TYPE_ENTRY_EXIT = 0; // CodForChk_Dis_Magic |
|
49 |
|
50 /** |
|
51 * Start-stop event template |
|
52 */ |
|
53 public static final int TYPE_PERF_EVENT = 1; // CodForChk_Dis_Magic |
|
54 |
|
55 /** |
|
56 * State trace template |
|
57 */ |
|
58 public static final int TYPE_STATE_TRACE = 2; // CodForChk_Dis_Magic |
|
59 |
|
60 /** |
|
61 * Trace name format base |
|
62 */ |
|
63 static final String NAME_FORMAT_BASE = "{$CN}_{$FN}"; //$NON-NLS-1$ |
|
64 |
|
65 /** |
|
66 * Trace text format base |
|
67 */ |
|
68 static final String TEXT_FORMAT_BASE = "{$cn}::{$fn}"; //$NON-NLS-1$ |
|
69 |
|
70 /** |
|
71 * Template titles |
|
72 */ |
|
73 private static final String[] ENTRY_TEMPLATE_TITLES = { |
|
74 Messages.getString("RuleUtils.FunctionEntryTitle"), //$NON-NLS-1$ |
|
75 Messages.getString("RuleUtils.PerformanceEventTitle"), //$NON-NLS-1$ |
|
76 Messages.getString("RuleUtils.StateTraceTitle"), //$NON-NLS-1$ |
|
77 }; |
|
78 |
|
79 /** |
|
80 * Entry name prefixes |
|
81 */ |
|
82 public static final String[] ENTRY_NAME_PREFIXES = { "", //$NON-NLS-1$ |
|
83 "EVENT_", //$NON-NLS-1$ |
|
84 "STATE_" //$NON-NLS-1$ |
|
85 }; |
|
86 |
|
87 /** |
|
88 * Entry name suffixes |
|
89 */ |
|
90 public static final String[] ENTRY_NAME_SUFFIXES = { "_ENTRY", //$NON-NLS-1$ |
|
91 "_START", //$NON-NLS-1$ |
|
92 "" //$NON-NLS-1$ |
|
93 }; |
|
94 |
|
95 /** |
|
96 * Exit name prefixes |
|
97 */ |
|
98 private static final String[] EXIT_NAME_PREFIXES = { "", //$NON-NLS-1$ |
|
99 "EVENT_", //$NON-NLS-1$ |
|
100 "" //$NON-NLS-1$ |
|
101 }; |
|
102 |
|
103 /** |
|
104 * Exit name suffixes |
|
105 */ |
|
106 public static final String[] EXIT_NAME_SUFFIXES = { "_EXIT", //$NON-NLS-1$ |
|
107 "_STOP", //$NON-NLS-1$ |
|
108 "" //$NON-NLS-1$ |
|
109 }; |
|
110 |
|
111 /** |
|
112 * Gets an entry template title |
|
113 * |
|
114 * @param type |
|
115 * the template type |
|
116 * @return the template title |
|
117 */ |
|
118 static String getEntryTemplateTitle(int type) { |
|
119 return ENTRY_TEMPLATE_TITLES[type]; |
|
120 } |
|
121 |
|
122 /** |
|
123 * Creates exit name from entry name |
|
124 * |
|
125 * @param entryName |
|
126 * the entry name |
|
127 * @param type |
|
128 * the type of entry |
|
129 * @return the exit name |
|
130 */ |
|
131 static String createExitName(String entryName, int type) { |
|
132 // Strips the duplicate modifier from the name |
|
133 entryName = TraceObjectUtils.removeDuplicateModifier(entryName); |
|
134 StringBuffer sb = new StringBuffer(); |
|
135 if (entryName.startsWith(ENTRY_NAME_PREFIXES[type])) { |
|
136 entryName = entryName.substring(ENTRY_NAME_PREFIXES[type].length()); |
|
137 } |
|
138 if (entryName.endsWith(ENTRY_NAME_SUFFIXES[type])) { |
|
139 entryName = entryName.substring(0, entryName.length() |
|
140 - ENTRY_NAME_SUFFIXES[type].length()); |
|
141 } |
|
142 sb.append(EXIT_NAME_PREFIXES[type]); |
|
143 sb.append(entryName); |
|
144 sb.append(EXIT_NAME_SUFFIXES[type]); |
|
145 return sb.toString(); |
|
146 } |
|
147 |
|
148 /** |
|
149 * Creates an entry trace name format from type |
|
150 * |
|
151 * @param type |
|
152 * the trace format type |
|
153 * @return the trace name format |
|
154 */ |
|
155 public static String createEntryTraceNameFormat(int type) { |
|
156 StringBuffer sb = new StringBuffer(); |
|
157 if (type == TYPE_PERF_EVENT || type == TYPE_STATE_TRACE) { |
|
158 sb.append(NAME_FORMAT_BASE); |
|
159 } else { |
|
160 sb.append(ENTRY_NAME_PREFIXES[type]); |
|
161 sb.append(NAME_FORMAT_BASE); |
|
162 sb.append(ENTRY_NAME_SUFFIXES[type]); |
|
163 } |
|
164 return sb.toString(); |
|
165 } |
|
166 |
|
167 /** |
|
168 * Creates an exit trace name format based on type |
|
169 * |
|
170 * @param type |
|
171 * the format type |
|
172 * @return the trace name format |
|
173 */ |
|
174 public static String createExitTraceNameFormat(int type) { |
|
175 StringBuffer sb = new StringBuffer(); |
|
176 sb.append(EXIT_NAME_PREFIXES[type]); |
|
177 sb.append(NAME_FORMAT_BASE); |
|
178 sb.append(EXIT_NAME_SUFFIXES[type]); |
|
179 return sb.toString(); |
|
180 } |
|
181 |
|
182 /** |
|
183 * Gets the return statements from current context from the context manager. |
|
184 * |
|
185 * @param returnStatements |
|
186 * the list where the return statements are stored |
|
187 */ |
|
188 static void getCurrentContextReturns(List<SourceReturn> returnStatements) { |
|
189 SourceContext context = TraceCompilerEngineGlobals.getSourceContextManager() |
|
190 .getContext(); |
|
191 if (context != null) { |
|
192 try { |
|
193 context.parseReturnValues(returnStatements); |
|
194 } catch (SourceParserException e) { |
|
195 String msg = Messages |
|
196 .getString("RuleUtils.FailedToParseReturnValues"); //$NON-NLS-1$ |
|
197 String cname = context.getClassName(); |
|
198 String source; |
|
199 if (cname != null) { |
|
200 source = cname + "::" + context.getFunctionName(); //$NON-NLS-1$; |
|
201 } else { |
|
202 source = context.getFunctionName(); |
|
203 } |
|
204 TraceCompilerEngineGlobals.getEvents().postErrorMessage(msg, source, |
|
205 true); |
|
206 } |
|
207 } else { |
|
208 if (TraceCompilerEngineConfiguration.ASSERTIONS_ENABLED) { |
|
209 TraceCompilerEngineGlobals.getEvents().postAssertionFailed( |
|
210 "Function return not in function", null); //$NON-NLS-1$ |
|
211 } |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * Checks if the function is static. Currently this is just dummy, checking |
|
217 * that the class name exists and the function name is not New / NewL / |
|
218 * NewLC |
|
219 * |
|
220 * @param context |
|
221 * the context to be checked |
|
222 * @return true if static, false if not |
|
223 */ |
|
224 static boolean isStaticFunction(SourceContext context) { |
|
225 boolean retval; |
|
226 if (context.getClassName() == null) { |
|
227 retval = true; |
|
228 } else { |
|
229 String fname = context.getFunctionName(); |
|
230 if (fname.equals("New") //$NON-NLS-1$ |
|
231 || fname.equals("NewL") //$NON-NLS-1$ |
|
232 || fname.equals("NewLC") //$NON-NLS-1$ |
|
233 ) { |
|
234 retval = true; |
|
235 } else { |
|
236 retval = false; |
|
237 } |
|
238 } |
|
239 return retval; |
|
240 } |
|
241 |
|
242 /** |
|
243 * Locates a constant table based on type definition parsed from source |
|
244 * |
|
245 * @param model |
|
246 * the trace model |
|
247 * @param type |
|
248 * the parameter type |
|
249 * @return the constant table or null if not found |
|
250 */ |
|
251 static TraceConstantTable findConstantTableByType(TraceModel model, |
|
252 ParsedType type) { |
|
253 Iterator<TraceConstantTable> tables = model.getConstantTables(); |
|
254 TraceConstantTable foundTable = null; |
|
255 while (tables.hasNext() && foundTable == null) { |
|
256 TraceConstantTable table = tables.next(); |
|
257 if (type.typeEquals(table.getName())) { |
|
258 foundTable = table; |
|
259 } |
|
260 } |
|
261 return foundTable; |
|
262 } |
|
263 |
|
264 /** |
|
265 * Creates a parameter based on TypeMapping definition |
|
266 * |
|
267 * @param owner |
|
268 * the parameter owner |
|
269 * @param name |
|
270 * the parameter name |
|
271 * @param type |
|
272 * the type mapping |
|
273 * @return the parameter |
|
274 * @throws TraceCompilerException |
|
275 * if parameter cannot be created |
|
276 */ |
|
277 static TraceParameter createParameterFromType(Trace owner, String name, |
|
278 TypeMapping type) throws TraceCompilerException { |
|
279 int id = owner.getNextParameterID(); |
|
280 owner.getModel().getVerifier().checkTraceParameterProperties(owner, |
|
281 null, id, name, type.type); |
|
282 TraceModelExtension[] extensions = null; |
|
283 if (type.needsCasting || type.valueToPointer |
|
284 || type.type.equals(TraceParameter.TIME)) { |
|
285 extensions = new TraceModelExtension[1]; |
|
286 extensions[0] = new ParameterTypeMappingRule(type); |
|
287 } |
|
288 TraceParameter parameter = owner.getModel().getFactory() |
|
289 .createTraceParameter(owner, id, name, type.type, extensions); |
|
290 return parameter; |
|
291 } |
|
292 |
|
293 /** |
|
294 * Creates a parameter object from constant table |
|
295 * |
|
296 * @param owner |
|
297 * the parameter owner |
|
298 * @param name |
|
299 * the name for the parameter |
|
300 * @param table |
|
301 * the constant table |
|
302 * @return the parameter |
|
303 * @throws TraceCompilerException |
|
304 * if parameter cannot be created |
|
305 */ |
|
306 static TraceParameter createParameterFromConstantTable(Trace owner, |
|
307 String name, TraceConstantTable table) throws TraceCompilerException { |
|
308 int id = owner.getNextParameterID(); |
|
309 owner.getModel().getVerifier().checkTraceParameterProperties(owner, |
|
310 null, id, name, table.getType()); |
|
311 // Constant tables need a cast to constant table type |
|
312 TypeMapping type = new TypeMapping(table.getType()); |
|
313 type.needsCasting = true; |
|
314 TraceModelExtension[] extensions = new TraceModelExtension[1]; |
|
315 extensions[0] = new ParameterTypeMappingRule(type); |
|
316 return owner.getModel().getFactory().createTraceParameter(owner, id, |
|
317 name, table.getName(), extensions); |
|
318 } |
|
319 |
|
320 } |