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
27 class Usage(Exception):
28 def __init__(self
, msg
):
34 #undef TRACEPOINT_PROVIDER
35 #define TRACEPOINT_PROVIDER {providerName}
37 #undef TRACEPOINT_INCLUDE
38 #define TRACEPOINT_INCLUDE "./{headerFilename}"
40 #if !defined({includeGuard}) || defined(TRACEPOINT_HEADER_MULTI_READ)
41 #define {includeGuard}
43 #include <lttng/tracepoint.h>
47 #endif /* {includeGuard} */
49 #include <lttng/tracepoint-event.h>
52 def __init__(self
, filename
, template
):
53 self
.outputFilename
= filename
54 self
.template
= template
57 outputFile
= open(self
.outputFilename
, "w")
58 # Include guard macro will be created by uppercasing the filename and
59 # replacing all non alphanumeric characters with '_'
60 includeGuard
= re
.sub('[^0-9a-zA-Z]', '_', self
.outputFilename
.upper())
62 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
63 includeGuard
=includeGuard
,
64 headerFilename
=self
.outputFilename
))
65 outputFile
.write(self
.template
.text
)
66 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
=includeGuard
))
72 #define TRACEPOINT_CREATE_PROBES
74 * The header containing our TRACEPOINT_EVENTs.
76 #define TRACEPOINT_DEFINE
77 #include "{headerFilename}"
80 def __init__(self
, filename
, template
):
81 self
.outputFilename
= filename
82 self
.template
= template
85 outputFile
= open(self
.outputFilename
, "w")
87 headerFilename
= self
.outputFilename
88 if headerFilename
.endswith(".c"):
89 headerFilename
= headerFilename
[:-2] + ".h"
91 outputFile
.write(CFile
.FILE_TPL
.format(
92 headerFilename
=headerFilename
))
97 def __init__(self
, filename
, template
):
98 self
.outputFilename
= filename
99 self
.template
= template
103 if 'CC' in os
.environ
:
104 cc
= os
.environ
['CC']
106 subprocess
.call(cc
.split(),
107 stdout
=subprocess
.PIPE
,
108 stderr
=subprocess
.PIPE
)
109 except OSError as msg
:
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
)
120 except OSError as msg
:
128 subprocess
.call("gcc",
129 stdout
=subprocess
.PIPE
,
130 stderr
=subprocess
.PIPE
)
131 except OSError as msg
:
138 cFilename
= self
.outputFilename
139 if cFilename
.endswith(".o"):
140 cFilename
= cFilename
[:-2] + ".c"
142 cc
= self
._detectCC
()
144 raise RuntimeError("No C Compiler detected")
145 if 'CPPFLAGS' in os
.environ
:
146 cppflags
= " " + os
.environ
['CPPFLAGS']
149 if 'CFLAGS' in os
.environ
:
150 cflags
= " " + os
.environ
['CFLAGS']
153 if 'LDFLAGS' in os
.environ
:
154 ldflags
= " " + os
.environ
['LDFLAGS']
158 command
= cc
+ " -c" + cppflags
+ cflags
+ ldflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
160 print("Compile command: " + command
)
161 subprocess
.call(command
.split())
165 def __init__(self
, filename
):
167 self
.inputFilename
= filename
170 def parseTemplate(self
):
171 f
= open(self
.inputFilename
, "r")
175 # Remove # comments (from input and output file) but keep
176 # #include in the output file
177 removeComments
= re
.compile("#[^include].*$", flags
=re
.MULTILINE
)
178 self
.text
= removeComments
.sub("", self
.text
)
179 # Remove #include directive from the parsed text
180 removePreprocess
= re
.compile("#.*$", flags
=re
.MULTILINE
)
181 noPreprocess
= removePreprocess
.sub("", self
.text
)
183 removeLineComment
= re
.compile("\/\/.*$", flags
=re
.MULTILINE
)
184 nolinecomment
= removeLineComment
.sub("", noPreprocess
)
185 # Remove all spaces and lines
186 cleantext
= re
.sub("\s*", "", nolinecomment
)
187 # Remove multine C style comments
188 nocomment
= re
.sub("/\*.*?\*/", "", cleantext
)
189 entries
= re
.split("TRACEPOINT_.*?", nocomment
)
191 for entry
in entries
:
193 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
195 domain
= decomp
[0][1]
198 if self
.domain
== "":
201 if self
.domain
!= domain
:
202 print("Warning: different domain provided (%s,%s)" % (self
.domain
, domain
))
208 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
210 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
212 If no OUTPUT_FILE is given, the .h and .c file will be generated.
213 (The basename of the template file with be used for the generated file.
214 for example sample.tp will generate sample.h, sample.c and sample.o)
216 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
217 The -o option can be repeated multiple times.
219 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
220 as per defined in the lttng/tracepoint.h file.
221 See the lttng-ust(3) man page for more details on the format.
231 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:av", ["help", "verbose"])
232 except getopt
.error
as msg
:
236 print(err
.msg
, file=sys
.stderr
)
237 print("for help use --help", file=sys
.stderr
)
242 if o
in ("-h", "--help"):
246 outputNames
.append(a
)
249 if o
in ("-v", "--verbose"):
254 raise Usage("No template file given")
257 print(err
.msg
, file=sys
.stderr
)
258 print("for help use --help", file=sys
.stderr
)
264 headerFilename
= None
268 if len(outputNames
) > 0:
270 print("Cannot process more than one input if you specify an output")
273 for outputName
in outputNames
:
274 if outputName
[-2:] == ".h":
276 headerFilename
= outputName
277 elif outputName
[-2:] == ".c":
279 cFilename
= outputName
280 elif outputName
[-2:] == ".o":
282 objFilename
= outputName
284 print("output file type unsupported")
293 if arg
[-3:] != ".tp":
294 print(arg
+ " does not end in .tp. Skipping.")
299 tpl
= TemplateFile(arg
)
300 except IOError as args
:
301 print("Cannot read input file " + args
.filename
+ " " + args
.strerror
)
306 curFilename
= headerFilename
308 curFilename
= re
.sub("\.tp$", ".h", arg
)
309 doth
= HeaderFile(curFilename
, tpl
)
313 curFilename
= cFilename
315 curFilename
= re
.sub("\.tp$", ".c", arg
)
316 dotc
= CFile(curFilename
, tpl
)
320 curFilename
= objFilename
322 curFilename
= re
.sub("\.tp$", ".o", arg
)
323 dotobj
= ObjFile(curFilename
, tpl
)
325 except IOError as args
:
326 print("Cannot write output file " + args
.filename
+ " " + args
.strerror
)
330 if __name__
== "__main__":
This page took 0.03664 seconds and 4 git commands to generate.