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}"
37 #if !defined({includeGuard}) || defined(TRACEPOINT_HEADER_MULTI_READ)
38 #define {includeGuard}
40 #include <lttng/tracepoint.h>
44 #endif /* {includeGuard} */
46 #include <lttng/tracepoint-event.h>
48 def __init__(self
, filename
, template
):
49 self
.outputFilename
= filename
50 self
.template
= template
53 outputFile
= open(self
.outputFilename
,"w")
54 # Include guard macro will be created by uppercasing the filename and
55 # replacing all non alphanumeric characters with '_'
56 includeGuard
= re
.sub('[^0-9a-zA-Z]', '_', self
.outputFilename
.upper())
58 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
59 includeGuard
= includeGuard
,
60 headerFilename
= self
.outputFilename
))
61 outputFile
.write(self
.template
.text
)
62 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
= includeGuard
))
67 #define TRACEPOINT_CREATE_PROBES
69 * The header containing our TRACEPOINT_EVENTs.
71 #define TRACEPOINT_DEFINE
72 #include "{headerFilename}"
74 def __init__(self
, filename
, template
):
75 self
.outputFilename
= filename
76 self
.template
= template
79 outputFile
= open(self
.outputFilename
,"w")
81 headerFilename
= self
.outputFilename
.replace(".c",".h")
83 outputFile
.write(CFile
.FILE_TPL
.format(
84 headerFilename
= headerFilename
))
88 def __init__(self
, filename
, template
):
89 self
.outputFilename
= filename
90 self
.template
= template
93 if os
.environ
.has_key('CC'):
97 stdout
=subprocess
.PIPE
,
98 stderr
=subprocess
.PIPE
)
100 print "Invalid CC environment variable"
104 # Try c first, if that fails try gcc
107 subprocess
.call("cc",
108 stdout
=subprocess
.PIPE
,
109 stderr
=subprocess
.PIPE
)
118 subprocess
.call("gcc",
119 stdout
=subprocess
.PIPE
,
120 stderr
=subprocess
.PIPE
)
128 cFilename
= self
.outputFilename
.replace(".o",".c")
129 cc
= self
._detectCC
()
131 raise RuntimeError("No C Compiler detected")
132 if os
.environ
.has_key('CFLAGS'):
133 cflags
= os
.environ
['CFLAGS']
137 command
= cc
+ " -c " + cflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
138 subprocess
.call(command
.split())
141 def __init__(self
, filename
):
143 self
.inputFilename
= filename
147 def parseTemplate(self
):
148 f
= open(self
.inputFilename
,"r")
152 #Remove # comments (from input and output file
153 removeComments
= re
.compile("#.*$",flags
=re
.MULTILINE
)
154 self
.text
= removeComments
.sub("",self
.text
)
156 removeLineComment
= re
.compile("\/\/.*$",flags
=re
.MULTILINE
)
157 nolinecomment
= removeLineComment
.sub("",self
.text
)
158 #Remove all spaces and lines
159 cleantext
= re
.sub("\s*","",nolinecomment
)
160 #Remove multine C style comments
161 nocomment
= re
.sub("/\*.*?\*/","",cleantext
)
162 entries
= re
.split("TRACEPOINT_.*?",nocomment
)
164 for entry
in entries
:
166 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
168 domain
= decomp
[0][1]
171 if self
.domain
== "":
174 if self
.domain
!= domain
:
175 print "Warning: different domain provided (%s,%s)" % (self
.domain
, domain
)
178 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
180 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
182 If no OUTPUT_FILE is given, the .h and .c file will be generated.
183 (The basename of the template file with be used for the generated file.
184 for example sample.tp will generate sample.h, sample.c and sample.o)
186 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
187 The -o option can be repeated multiple times.
189 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
190 as per defined in the lttng/tracepoint.h file.
191 See the lttng-ust(3) man page for more details on the format.
199 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:a", ["help"])
200 except getopt
.error
, msg
:
204 print >>sys
.stderr
, err
.msg
205 print >>sys
.stderr
, "for help use --help"
210 if o
in ("-h", "--help"):
214 outputNames
.append(a
)
219 raise Usage("No template file given")
222 print >>sys
.stderr
, err
.msg
223 print >>sys
.stderr
, "for help use --help"
229 headerFilename
= None
233 if len(outputNames
) > 0:
235 print "Cannot process more than one input if you specify an output"
238 for outputName
in outputNames
:
239 if outputName
[-2:] == ".h":
241 headerFilename
= outputName
242 elif outputName
[-2:] == ".c":
244 cFilename
= outputName
245 elif outputName
[-2:] == ".o":
247 objFilename
= outputName
249 print "output file type unsupported"
258 if arg
[-3:] != ".tp":
259 print arg
+ " does not end in .tp. Skipping."
264 tpl
= TemplateFile(arg
)
265 except IOError as args
:
266 print "Cannot read input file " + args
.filename
+ " " + args
.strerror
271 curFilename
= headerFilename
273 curFilename
= re
.sub("\.tp$",".h",arg
)
274 doth
= HeaderFile(curFilename
, tpl
)
278 curFilename
= cFilename
280 curFilename
= re
.sub("\.tp$",".c",arg
)
281 dotc
= CFile(curFilename
, tpl
)
285 curFilename
= objFilename
287 curFilename
= re
.sub("\.tp$",".o",arg
)
288 dotobj
= ObjFile(curFilename
, tpl
)
290 except IOError as args
:
291 print "Cannot write output file " + args
.filename
+ " " + args
.strerror
294 if __name__
== "__main__":
This page took 0.045043 seconds and 4 git commands to generate.