3 # Copyright (c) 2012 Yannick Brosseau <yannick.brosseau@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; only version 2
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 class Usage(Exception):
26 def __init__(self
, msg
):
31 #undef TRACEPOINT_PROVIDER
32 #define TRACEPOINT_PROVIDER {providerName}
34 #undef TRACEPOINT_INCLUDE
35 #define TRACEPOINT_INCLUDE "./{headerFilename}"
39 #endif /* __cplusplus */
42 #if !defined({includeGuard}) || defined(TRACEPOINT_HEADER_MULTI_READ)
43 #define {includeGuard}
45 #include <lttng/tracepoint.h>
49 #endif /* {includeGuard} */
51 #include <lttng/tracepoint-event.h>
55 #endif /* __cplusplus */
58 def __init__(self
, filename
, template
):
59 self
.outputFilename
= filename
60 self
.template
= template
63 outputFile
= open(self
.outputFilename
,"w")
64 includeGuard
= self
.outputFilename
.upper().replace(".","_")
66 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
67 includeGuard
= includeGuard
,
68 headerFilename
= self
.outputFilename
))
69 outputFile
.write(self
.template
.text
)
70 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
= includeGuard
))
75 #define TRACEPOINT_CREATE_PROBES
77 * The header containing our TRACEPOINT_EVENTs.
79 #define TRACEPOINT_DEFINE
80 #include "{headerFilename}"
82 def __init__(self
, filename
, template
):
83 self
.outputFilename
= filename
84 self
.template
= template
87 outputFile
= open(self
.outputFilename
,"w")
89 headerFilename
= self
.outputFilename
.replace(".c",".h")
91 outputFile
.write(CFile
.FILE_TPL
.format(
92 headerFilename
= headerFilename
))
96 def __init__(self
, filename
, template
):
97 self
.outputFilename
= filename
98 self
.template
= template
101 if os
.environ
.has_key('CC'):
102 cc
= os
.environ
['CC']
105 stdout
=subprocess
.PIPE
,
106 stderr
=subprocess
.PIPE
)
108 print "Invalid CC environment variable"
112 # Try c first, if that fails try gcc
115 subprocess
.call("cc",
116 stdout
=subprocess
.PIPE
,
117 stderr
=subprocess
.PIPE
)
126 subprocess
.call("gcc",
127 stdout
=subprocess
.PIPE
,
128 stderr
=subprocess
.PIPE
)
136 cFilename
= self
.outputFilename
.replace(".o",".c")
137 cc
= self
._detectCC
()
139 raise RuntimeError("No C Compiler detected")
140 if os
.environ
.has_key('CFLAGS'):
141 cflags
= os
.environ
['CFLAGS']
145 command
= cc
+ " -c " + cflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
146 subprocess
.call(command
.split())
149 def __init__(self
, filename
):
151 self
.inputFilename
= filename
155 def parseTemplate(self
):
156 f
= open(self
.inputFilename
,"r")
160 #Remove # comments (from input and output file
161 removeComments
= re
.compile("#.*$",flags
=re
.MULTILINE
)
162 self
.text
= removeComments
.sub("",self
.text
)
164 removeLineComment
= re
.compile("\/\/.*$",flags
=re
.MULTILINE
)
165 nolinecomment
= removeLineComment
.sub("",self
.text
)
166 #Remove all spaces and lines
167 cleantext
= re
.sub("\s*","",nolinecomment
)
168 #Remove multine C style comments
169 nocomment
= re
.sub("/\*.*?\*/","",cleantext
)
170 entries
= re
.split("TRACEPOINT_.*?",nocomment
)
172 for entry
in entries
:
174 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
176 domain
= decomp
[0][1]
179 if self
.domain
== "":
182 if self
.domain
!= domain
:
183 print "Warning: different domain provided (%s,%s)" % (self
.domain
, domain
)
186 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
188 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
190 If no OUTPUT_FILE is given, the .h and .c file will be generated.
191 (The basename of the template file with be used for the generated file.
192 for example sample.tp will generate sample.h, sample.c and sample.o)
194 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
195 The -o option can be repeated multiple times.
197 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
198 as per defined in the lttng/tracepoint.h file.
199 See the lttng-ust(3) man page for more details on the format.
207 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:a", ["help"])
208 except getopt
.error
, msg
:
212 print >>sys
.stderr
, err
.msg
213 print >>sys
.stderr
, "for help use --help"
218 if o
in ("-h", "--help"):
222 outputNames
.append(a
)
227 raise Usage("No template file given")
230 print >>sys
.stderr
, err
.msg
231 print >>sys
.stderr
, "for help use --help"
237 headerFilename
= None
241 if len(outputNames
) > 0:
243 print "Cannot process more than one input if you specify an output"
246 for outputName
in outputNames
:
247 if outputName
[-2:] == ".h":
249 headerFilename
= outputName
250 elif outputName
[-2:] == ".c":
252 cFilename
= outputName
253 elif outputName
[-2:] == ".o":
255 objFilename
= outputName
257 print "output file type unsupported"
266 if arg
[-3:] != ".tp":
267 print arg
+ " does not end in .tp. Skipping."
272 tpl
= TemplateFile(arg
)
273 except IOError as args
:
274 print "Cannot read input file " + args
.filename
+ " " + args
.strerror
279 curFilename
= headerFilename
281 curFilename
= re
.sub("\.tp$",".h",arg
)
282 doth
= HeaderFile(curFilename
, tpl
)
286 curFilename
= cFilename
288 curFilename
= re
.sub("\.tp$",".c",arg
)
289 dotc
= CFile(curFilename
, tpl
)
293 curFilename
= objFilename
295 curFilename
= re
.sub("\.tp$",".o",arg
)
296 dotobj
= ObjFile(curFilename
, tpl
)
298 except IOError as args
:
299 print "Cannot write output file " + args
.filename
+ " " + args
.strerror
302 if __name__
== "__main__":
This page took 0.046709 seconds and 4 git commands to generate.