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 TRACEPOINT_EVENTs.
64 #define 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']
141 if 'LDFLAGS' in os
.environ
:
142 ldflags
= " " + os
.environ
['LDFLAGS']
146 command
= cc
+ " -c" + cppflags
+ cflags
+ ldflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
148 print("Compile command: " + command
)
149 subprocess
.call(command
.split())
153 def __init__(self
, filename
):
155 self
.inputFilename
= filename
158 def parseTemplate(self
):
159 f
= open(self
.inputFilename
, "r")
163 # Remove # comments (from input and output file) but keep
164 # #include in the output file
165 removeComments
= re
.compile("#[^include].*$", flags
=re
.MULTILINE
)
166 self
.text
= removeComments
.sub("", self
.text
)
167 # Remove #include directive from the parsed text
168 removePreprocess
= re
.compile("#.*$", flags
=re
.MULTILINE
)
169 noPreprocess
= removePreprocess
.sub("", self
.text
)
171 removeLineComment
= re
.compile("\/\/.*$", flags
=re
.MULTILINE
)
172 nolinecomment
= removeLineComment
.sub("", noPreprocess
)
173 # Remove all spaces and lines
174 cleantext
= re
.sub("\s*", "", nolinecomment
)
175 # Remove multine C style comments
176 nocomment
= re
.sub("/\*.*?\*/", "", cleantext
)
177 entries
= re
.split("TRACEPOINT_.*?", nocomment
)
179 for entry
in entries
:
181 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
183 domain
= decomp
[0][1]
186 if self
.domain
== "":
189 if self
.domain
!= domain
:
190 print("Warning: different domain provided (%s,%s)" % (self
.domain
, domain
))
196 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
198 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
200 If no OUTPUT_FILE is given, the .h and .c file will be generated.
201 (The basename of the template file with be used for the generated file.
202 for example sample.tp will generate sample.h, sample.c and sample.o)
204 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
205 The -o option can be repeated multiple times.
207 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
208 as per defined in the lttng/tracepoint.h file.
209 See the lttng-ust(3) man page for more details on the format.
219 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:av", ["help", "verbose"])
220 except getopt
.error
as msg
:
224 print(err
.msg
, file=sys
.stderr
)
225 print("for help use --help", file=sys
.stderr
)
230 if o
in ("-h", "--help"):
234 outputNames
.append(a
)
237 if o
in ("-v", "--verbose"):
242 raise Usage("No template file given")
245 print(err
.msg
, file=sys
.stderr
)
246 print("for help use --help", file=sys
.stderr
)
252 headerFilename
= None
256 if len(outputNames
) > 0:
258 print("Cannot process more than one input if you specify an output")
261 for outputName
in outputNames
:
262 if outputName
[-2:] == ".h":
264 headerFilename
= outputName
265 elif outputName
[-2:] == ".c":
267 cFilename
= outputName
268 elif outputName
[-2:] == ".o":
270 objFilename
= outputName
272 print("output file type unsupported")
281 if arg
[-3:] != ".tp":
282 print(arg
+ " does not end in .tp. Skipping.")
287 tpl
= TemplateFile(arg
)
288 except IOError as args
:
289 print("Cannot read input file " + args
.filename
+ " " + args
.strerror
)
294 curFilename
= headerFilename
296 curFilename
= re
.sub("\.tp$", ".h", arg
)
297 doth
= HeaderFile(curFilename
, tpl
)
301 curFilename
= cFilename
303 curFilename
= re
.sub("\.tp$", ".c", arg
)
304 dotc
= CFile(curFilename
, tpl
)
308 curFilename
= objFilename
310 curFilename
= re
.sub("\.tp$", ".o", arg
)
311 dotobj
= ObjFile(curFilename
, tpl
)
313 except IOError as args
:
314 print("Cannot write output file " + args
.filename
+ " " + args
.strerror
)
318 if __name__
== "__main__":
This page took 0.034976 seconds and 4 git commands to generate.