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 # Include guard macro will be created by uppercasing the filename and
65 # replacing all non alphanumeric characters with '_'
66 includeGuard
= re
.sub('[^0-9a-zA-Z]', '_', self
.outputFilename
.upper())
68 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
69 includeGuard
= includeGuard
,
70 headerFilename
= self
.outputFilename
))
71 outputFile
.write(self
.template
.text
)
72 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
= includeGuard
))
77 #define TRACEPOINT_CREATE_PROBES
79 * The header containing our TRACEPOINT_EVENTs.
81 #define TRACEPOINT_DEFINE
82 #include "{headerFilename}"
84 def __init__(self
, filename
, template
):
85 self
.outputFilename
= filename
86 self
.template
= template
89 outputFile
= open(self
.outputFilename
,"w")
91 headerFilename
= self
.outputFilename
.replace(".c",".h")
93 outputFile
.write(CFile
.FILE_TPL
.format(
94 headerFilename
= headerFilename
))
98 def __init__(self
, filename
, template
):
99 self
.outputFilename
= filename
100 self
.template
= template
103 if os
.environ
.has_key('CC'):
104 cc
= os
.environ
['CC']
107 stdout
=subprocess
.PIPE
,
108 stderr
=subprocess
.PIPE
)
110 print "Invalid CC environment variable"
114 # Try c first, if that fails try gcc
117 subprocess
.call("cc",
118 stdout
=subprocess
.PIPE
,
119 stderr
=subprocess
.PIPE
)
128 subprocess
.call("gcc",
129 stdout
=subprocess
.PIPE
,
130 stderr
=subprocess
.PIPE
)
138 cFilename
= self
.outputFilename
.replace(".o",".c")
139 cc
= self
._detectCC
()
141 raise RuntimeError("No C Compiler detected")
142 if os
.environ
.has_key('CFLAGS'):
143 cflags
= os
.environ
['CFLAGS']
147 command
= cc
+ " -c " + cflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
148 subprocess
.call(command
.split())
151 def __init__(self
, filename
):
153 self
.inputFilename
= filename
157 def parseTemplate(self
):
158 f
= open(self
.inputFilename
,"r")
162 #Remove # comments (from input and output file
163 removeComments
= re
.compile("#.*$",flags
=re
.MULTILINE
)
164 self
.text
= removeComments
.sub("",self
.text
)
166 removeLineComment
= re
.compile("\/\/.*$",flags
=re
.MULTILINE
)
167 nolinecomment
= removeLineComment
.sub("",self
.text
)
168 #Remove all spaces and lines
169 cleantext
= re
.sub("\s*","",nolinecomment
)
170 #Remove multine C style comments
171 nocomment
= re
.sub("/\*.*?\*/","",cleantext
)
172 entries
= re
.split("TRACEPOINT_.*?",nocomment
)
174 for entry
in entries
:
176 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
178 domain
= decomp
[0][1]
181 if self
.domain
== "":
184 if self
.domain
!= domain
:
185 print "Warning: different domain provided (%s,%s)" % (self
.domain
, domain
)
188 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
190 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
192 If no OUTPUT_FILE is given, the .h and .c file will be generated.
193 (The basename of the template file with be used for the generated file.
194 for example sample.tp will generate sample.h, sample.c and sample.o)
196 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
197 The -o option can be repeated multiple times.
199 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
200 as per defined in the lttng/tracepoint.h file.
201 See the lttng-ust(3) man page for more details on the format.
209 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:a", ["help"])
210 except getopt
.error
, msg
:
214 print >>sys
.stderr
, err
.msg
215 print >>sys
.stderr
, "for help use --help"
220 if o
in ("-h", "--help"):
224 outputNames
.append(a
)
229 raise Usage("No template file given")
232 print >>sys
.stderr
, err
.msg
233 print >>sys
.stderr
, "for help use --help"
239 headerFilename
= None
243 if len(outputNames
) > 0:
245 print "Cannot process more than one input if you specify an output"
248 for outputName
in outputNames
:
249 if outputName
[-2:] == ".h":
251 headerFilename
= outputName
252 elif outputName
[-2:] == ".c":
254 cFilename
= outputName
255 elif outputName
[-2:] == ".o":
257 objFilename
= outputName
259 print "output file type unsupported"
268 if arg
[-3:] != ".tp":
269 print arg
+ " does not end in .tp. Skipping."
274 tpl
= TemplateFile(arg
)
275 except IOError as args
:
276 print "Cannot read input file " + args
.filename
+ " " + args
.strerror
281 curFilename
= headerFilename
283 curFilename
= re
.sub("\.tp$",".h",arg
)
284 doth
= HeaderFile(curFilename
, tpl
)
288 curFilename
= cFilename
290 curFilename
= re
.sub("\.tp$",".c",arg
)
291 dotc
= CFile(curFilename
, tpl
)
295 curFilename
= objFilename
297 curFilename
= re
.sub("\.tp$",".o",arg
)
298 dotobj
= ObjFile(curFilename
, tpl
)
300 except IOError as args
:
301 print "Cannot write output file " + args
.filename
+ " " + args
.strerror
304 if __name__
== "__main__":
This page took 0.049115 seconds and 4 git commands to generate.