b318d1cdcf8c592e8db6ff399b7d0125cd929ead
3 # SPDX-License-Identifier: GPL-2.0-only
5 # Copyright (C) 2012 Yannick Brosseau <yannick.brosseau@gmail.com>
7 from __future__
import print_function
15 class Usage(Exception):
16 def __init__(self
, msg
):
22 #undef TRACEPOINT_PROVIDER
23 #define TRACEPOINT_PROVIDER {providerName}
25 #undef TRACEPOINT_INCLUDE
26 #define TRACEPOINT_INCLUDE "./{headerFilename}"
28 #if !defined({includeGuard}) || defined(TRACEPOINT_HEADER_MULTI_READ)
29 #define {includeGuard}
31 #include <lttng/tracepoint.h>
35 #endif /* {includeGuard} */
37 #include <lttng/tracepoint-event.h>
40 def __init__(self
, filename
, template
):
41 self
.outputFilename
= filename
42 self
.template
= template
45 outputFile
= open(self
.outputFilename
, "w")
46 # Include guard macro will be created by uppercasing the filename and
47 # replacing all non alphanumeric characters with '_'
48 includeGuard
= re
.sub('[^0-9a-zA-Z]', '_', self
.outputFilename
.upper())
50 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
51 includeGuard
=includeGuard
,
52 headerFilename
=self
.outputFilename
))
53 outputFile
.write(self
.template
.text
)
54 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
=includeGuard
))
60 #define TRACEPOINT_CREATE_PROBES
62 * The header containing our LTTNG_UST_TRACEPOINT_EVENTs.
64 #define LTTNG_UST_TRACEPOINT_DEFINE
65 #include "{headerFilename}"
68 def __init__(self
, filename
, template
):
69 self
.outputFilename
= filename
70 self
.template
= template
73 outputFile
= open(self
.outputFilename
, "w")
75 headerFilename
= self
.outputFilename
76 if headerFilename
.endswith(".c"):
77 headerFilename
= headerFilename
[:-2] + ".h"
79 outputFile
.write(CFile
.FILE_TPL
.format(
80 headerFilename
=headerFilename
))
85 def __init__(self
, filename
, template
):
86 self
.outputFilename
= filename
87 self
.template
= template
91 if 'CC' in os
.environ
:
94 subprocess
.call(cc
.split(),
95 stdout
=subprocess
.PIPE
,
96 stderr
=subprocess
.PIPE
)
97 except OSError as msg
:
98 print("Invalid CC environment variable")
102 # Try c first, if that fails try gcc
105 subprocess
.call("cc",
106 stdout
=subprocess
.PIPE
,
107 stderr
=subprocess
.PIPE
)
108 except OSError as msg
:
116 subprocess
.call("gcc",
117 stdout
=subprocess
.PIPE
,
118 stderr
=subprocess
.PIPE
)
119 except OSError as msg
:
126 cFilename
= self
.outputFilename
127 if cFilename
.endswith(".o"):
128 cFilename
= cFilename
[:-2] + ".c"
130 cc
= self
._detectCC
()
132 raise RuntimeError("No C Compiler detected")
133 if 'CPPFLAGS' in os
.environ
:
134 cppflags
= " " + os
.environ
['CPPFLAGS']
137 if 'CFLAGS' in os
.environ
:
138 cflags
= " " + os
.environ
['CFLAGS']
142 command
= cc
+ " -c" + cppflags
+ cflags
+ " -I. -o " + self
.outputFilename
+ " " + cFilename
144 print("Compile command: " + command
)
145 subprocess
.call(command
.split())
149 def __init__(self
, filename
):
151 self
.inputFilename
= filename
154 def parseTemplate(self
):
155 f
= open(self
.inputFilename
, "r")
159 # Remove # comments (from input and output file) but keep
160 # #include in the output file
161 removeComments
= re
.compile("#[^include].*$", flags
=re
.MULTILINE
)
162 self
.text
= removeComments
.sub("", self
.text
)
163 # Remove #include directive from the parsed text
164 removePreprocess
= re
.compile("#.*$", flags
=re
.MULTILINE
)
165 noPreprocess
= removePreprocess
.sub("", self
.text
)
167 removeLineComment
= re
.compile("\/\/.*$", flags
=re
.MULTILINE
)
168 nolinecomment
= removeLineComment
.sub("", noPreprocess
)
169 # Remove all spaces and lines
170 cleantext
= re
.sub("\s*", "", nolinecomment
)
171 # Remove multine C style comments
172 nocomment
= re
.sub("/\*.*?\*/", "", cleantext
)
173 entries
= re
.split("LTTNG_UST_TRACEPOINT_.*?", nocomment
)
175 for entry
in entries
:
177 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
179 domain
= decomp
[0][1]
182 if self
.domain
== "":
185 if self
.domain
!= domain
:
186 print("Warning: different domain provided (%s,%s)" % (self
.domain
, domain
))
192 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
194 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
196 If no OUTPUT_FILE is given, the .h and .c file will be generated.
197 (The basename of the template file with be used for the generated file.
198 for example sample.tp will generate sample.h, sample.c and sample.o)
200 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
201 The -o option can be repeated multiple times.
203 The template file must contains LTTNG_UST_TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
204 as per defined in the lttng/tracepoint.h file.
205 See the lttng-ust(3) man page for more details on the format.
215 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:av", ["help", "verbose"])
216 except getopt
.error
as msg
:
220 print(err
.msg
, file=sys
.stderr
)
221 print("for help use --help", file=sys
.stderr
)
226 if o
in ("-h", "--help"):
230 outputNames
.append(a
)
233 if o
in ("-v", "--verbose"):
238 raise Usage("No template file given")
241 print(err
.msg
, file=sys
.stderr
)
242 print("for help use --help", file=sys
.stderr
)
248 headerFilename
= None
252 if len(outputNames
) > 0:
254 print("Cannot process more than one input if you specify an output")
257 for outputName
in outputNames
:
258 if outputName
[-2:] == ".h":
260 headerFilename
= outputName
261 elif outputName
[-2:] == ".c":
263 cFilename
= outputName
264 elif outputName
[-2:] == ".o":
266 objFilename
= outputName
268 print("output file type unsupported")
277 if arg
[-3:] != ".tp":
278 print(arg
+ " does not end in .tp. Skipping.")
283 tpl
= TemplateFile(arg
)
284 except IOError as args
:
285 print("Cannot read input file " + args
.filename
+ " " + args
.strerror
)
290 curFilename
= headerFilename
292 curFilename
= re
.sub("\.tp$", ".h", arg
)
293 doth
= HeaderFile(curFilename
, tpl
)
297 curFilename
= cFilename
299 curFilename
= re
.sub("\.tp$", ".c", arg
)
300 dotc
= CFile(curFilename
, tpl
)
304 curFilename
= objFilename
306 curFilename
= re
.sub("\.tp$", ".o", arg
)
307 dotobj
= ObjFile(curFilename
, tpl
)
309 except IOError as args
:
310 print("Cannot write output file " + args
.filename
+ " " + args
.strerror
)
314 if __name__
== "__main__":
This page took 0.050634 seconds and 3 git commands to generate.