8ae243f682360b2916985dad0bc39facb60b5904
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.
19 from __future__
import print_function
26 class Usage(Exception):
27 def __init__(self
, msg
):
32 #undef TRACEPOINT_PROVIDER
33 #define TRACEPOINT_PROVIDER {providerName}
35 #undef TRACEPOINT_INCLUDE
36 #define TRACEPOINT_INCLUDE "./{headerFilename}"
40 #endif /* __cplusplus */
43 #if !defined({includeGuard}) || defined(TRACEPOINT_HEADER_MULTI_READ)
44 #define {includeGuard}
46 #include <lttng/tracepoint.h>
50 #endif /* {includeGuard} */
52 #include <lttng/tracepoint-event.h>
56 #endif /* __cplusplus */
59 def __init__(self
, filename
, template
):
60 self
.outputFilename
= filename
61 self
.template
= template
64 outputFile
= open(self
.outputFilename
,"w")
65 # Include guard macro will be created by uppercasing the filename and
66 # replacing all non alphanumeric characters with '_'
67 includeGuard
= re
.sub('[^0-9a-zA-Z]', '_', self
.outputFilename
.upper())
69 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
70 includeGuard
= includeGuard
,
71 headerFilename
= self
.outputFilename
))
72 outputFile
.write(self
.template
.text
)
73 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
= includeGuard
))
78 #define TRACEPOINT_CREATE_PROBES
80 * The header containing our TRACEPOINT_EVENTs.
82 #define TRACEPOINT_DEFINE
83 #include "{headerFilename}"
85 def __init__(self
, filename
, template
):
86 self
.outputFilename
= filename
87 self
.template
= template
90 outputFile
= open(self
.outputFilename
,"w")
92 headerFilename
= self
.outputFilename
.replace(".c",".h")
94 outputFile
.write(CFile
.FILE_TPL
.format(
95 headerFilename
= headerFilename
))
99 def __init__(self
, filename
, template
):
100 self
.outputFilename
= filename
101 self
.template
= template
104 if 'CC' in os
.environ
:
105 cc
= os
.environ
['CC']
108 stdout
=subprocess
.PIPE
,
109 stderr
=subprocess
.PIPE
)
110 except OSError as msg
:
111 print("Invalid CC environment variable")
115 # Try c first, if that fails try gcc
118 subprocess
.call("cc",
119 stdout
=subprocess
.PIPE
,
120 stderr
=subprocess
.PIPE
)
121 except OSError as msg
:
129 subprocess
.call("gcc",
130 stdout
=subprocess
.PIPE
,
131 stderr
=subprocess
.PIPE
)
132 except OSError as msg
:
139 cFilename
= self
.outputFilename
.replace(".o",".c")
140 cc
= self
._detectCC
()
142 raise RuntimeError("No C Compiler detected")
143 if 'CFLAGS' in os
.environ
:
144 cflags
= os
.environ
['CFLAGS']
148 command
= cc
+ " -c " + cflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
150 print("Compile command: " + command
)
151 subprocess
.call(command
.split())
154 def __init__(self
, filename
):
156 self
.inputFilename
= filename
160 def parseTemplate(self
):
161 f
= open(self
.inputFilename
,"r")
165 #Remove # comments (from input and output file) but keep
166 # #include in the output file
167 removeComments
= re
.compile("#[^include].*$",flags
=re
.MULTILINE
)
168 self
.text
= removeComments
.sub("",self
.text
)
169 # Remove #include directive from the parsed text
170 removePreprocess
= re
.compile("#.*$",flags
=re
.MULTILINE
)
171 noPreprocess
= removePreprocess
.sub("", self
.text
)
173 removeLineComment
= re
.compile("\/\/.*$",flags
=re
.MULTILINE
)
174 nolinecomment
= removeLineComment
.sub("", noPreprocess
)
175 #Remove all spaces and lines
176 cleantext
= re
.sub("\s*","",nolinecomment
)
177 #Remove multine C style comments
178 nocomment
= re
.sub("/\*.*?\*/","",cleantext
)
179 entries
= re
.split("TRACEPOINT_.*?",nocomment
)
181 for entry
in entries
:
183 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
185 domain
= decomp
[0][1]
188 if self
.domain
== "":
191 if self
.domain
!= domain
:
192 print("Warning: different domain provided (%s,%s)" % (self
.domain
, domain
))
197 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
199 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
201 If no OUTPUT_FILE is given, the .h and .c file will be generated.
202 (The basename of the template file with be used for the generated file.
203 for example sample.tp will generate sample.h, sample.c and sample.o)
205 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
206 The -o option can be repeated multiple times.
208 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
209 as per defined in the lttng/tracepoint.h file.
210 See the lttng-ust(3) man page for more details on the format.
218 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:av", ["help","verbose"])
219 except getopt
.error
as msg
:
223 print(err
.msg
, file=sys
.stderr
)
224 print("for help use --help", file=sys
.stderr
)
229 if o
in ("-h", "--help"):
233 outputNames
.append(a
)
236 if o
in ("-v", "--verbose"):
241 raise Usage("No template file given")
244 print(err
.msg
, file=sys
.stderr
)
245 print("for help use --help", file=sys
.stderr
)
251 headerFilename
= None
255 if len(outputNames
) > 0:
257 print("Cannot process more than one input if you specify an output")
260 for outputName
in outputNames
:
261 if outputName
[-2:] == ".h":
263 headerFilename
= outputName
264 elif outputName
[-2:] == ".c":
266 cFilename
= outputName
267 elif outputName
[-2:] == ".o":
269 objFilename
= outputName
271 print("output file type unsupported")
280 if arg
[-3:] != ".tp":
281 print(arg
+ " does not end in .tp. Skipping.")
286 tpl
= TemplateFile(arg
)
287 except IOError as args
:
288 print("Cannot read input file " + args
.filename
+ " " + args
.strerror
)
293 curFilename
= headerFilename
295 curFilename
= re
.sub("\.tp$",".h",arg
)
296 doth
= HeaderFile(curFilename
, tpl
)
300 curFilename
= cFilename
302 curFilename
= re
.sub("\.tp$",".c",arg
)
303 dotc
= CFile(curFilename
, tpl
)
307 curFilename
= objFilename
309 curFilename
= re
.sub("\.tp$",".o",arg
)
310 dotobj
= ObjFile(curFilename
, tpl
)
312 except IOError as args
:
313 print("Cannot write output file " + args
.filename
+ " " + args
.strerror
)
316 if __name__
== "__main__":
This page took 0.035815 seconds and 4 git commands to generate.