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}"
38 #if !defined({includeGuard}) || defined(TRACEPOINT_HEADER_MULTI_READ)
39 #define {includeGuard}
41 #include <lttng/tracepoint.h>
45 #endif /* {includeGuard} */
47 #include <lttng/tracepoint-event.h>
49 def __init__(self
, filename
, template
):
50 self
.outputFilename
= filename
51 self
.template
= template
54 outputFile
= open(self
.outputFilename
,"w")
55 # Include guard macro will be created by uppercasing the filename and
56 # replacing all non alphanumeric characters with '_'
57 includeGuard
= re
.sub('[^0-9a-zA-Z]', '_', self
.outputFilename
.upper())
59 outputFile
.write(HeaderFile
.HEADER_TPL
.format(providerName
=self
.template
.domain
,
60 includeGuard
= includeGuard
,
61 headerFilename
= self
.outputFilename
))
62 outputFile
.write(self
.template
.text
)
63 outputFile
.write(HeaderFile
.FOOTER_TPL
.format(includeGuard
= includeGuard
))
68 #define TRACEPOINT_CREATE_PROBES
70 * The header containing our TRACEPOINT_EVENTs.
72 #define TRACEPOINT_DEFINE
73 #include "{headerFilename}"
75 def __init__(self
, filename
, template
):
76 self
.outputFilename
= filename
77 self
.template
= template
80 outputFile
= open(self
.outputFilename
,"w")
82 headerFilename
= self
.outputFilename
83 if headerFilename
.endswith(".c"):
84 headerFilename
= headerFilename
[:-2] + ".h"
86 outputFile
.write(CFile
.FILE_TPL
.format(
87 headerFilename
= headerFilename
))
91 def __init__(self
, filename
, template
):
92 self
.outputFilename
= filename
93 self
.template
= template
96 if 'CC' in os
.environ
:
99 subprocess
.call(cc
.split(),
100 stdout
=subprocess
.PIPE
,
101 stderr
=subprocess
.PIPE
)
102 except OSError as msg
:
103 print("Invalid CC environment variable")
107 # Try c first, if that fails try gcc
110 subprocess
.call("cc",
111 stdout
=subprocess
.PIPE
,
112 stderr
=subprocess
.PIPE
)
113 except OSError as msg
:
121 subprocess
.call("gcc",
122 stdout
=subprocess
.PIPE
,
123 stderr
=subprocess
.PIPE
)
124 except OSError as msg
:
131 cFilename
= self
.outputFilename
132 if cFilename
.endswith(".o"):
133 cFilename
= cFilename
[:-2] + ".c"
135 cc
= self
._detectCC
()
137 raise RuntimeError("No C Compiler detected")
138 if 'CPPFLAGS' in os
.environ
:
139 cppflags
= " " + os
.environ
['CPPFLAGS']
142 if 'CFLAGS' in os
.environ
:
143 cflags
= " " + os
.environ
['CFLAGS']
146 if 'LDFLAGS' in os
.environ
:
147 ldflags
= " " + os
.environ
['LDFLAGS']
151 command
= cc
+ " -c" + cppflags
+ cflags
+ ldflags
+ " -I. -llttng-ust" + " -o " + self
.outputFilename
+ " " + cFilename
153 print("Compile command: " + command
)
154 subprocess
.call(command
.split())
157 def __init__(self
, filename
):
159 self
.inputFilename
= filename
163 def parseTemplate(self
):
164 f
= open(self
.inputFilename
,"r")
168 #Remove # comments (from input and output file) but keep
169 # #include in the output file
170 removeComments
= re
.compile("#[^include].*$",flags
=re
.MULTILINE
)
171 self
.text
= removeComments
.sub("",self
.text
)
172 # Remove #include directive from the parsed text
173 removePreprocess
= re
.compile("#.*$",flags
=re
.MULTILINE
)
174 noPreprocess
= removePreprocess
.sub("", self
.text
)
176 removeLineComment
= re
.compile("\/\/.*$",flags
=re
.MULTILINE
)
177 nolinecomment
= removeLineComment
.sub("", noPreprocess
)
178 #Remove all spaces and lines
179 cleantext
= re
.sub("\s*","",nolinecomment
)
180 #Remove multine C style comments
181 nocomment
= re
.sub("/\*.*?\*/","",cleantext
)
182 entries
= re
.split("TRACEPOINT_.*?",nocomment
)
184 for entry
in entries
:
186 decomp
= re
.findall("(\w*?)\((\w*?),(\w*?),", entry
)
188 domain
= decomp
[0][1]
191 if self
.domain
== "":
194 if self
.domain
!= domain
:
195 print("Warning: different domain provided (%s,%s)" % (self
.domain
, domain
))
200 lttng-gen-tp - Generate the LTTng-UST header and source based on a simple template
202 usage: lttng-gen-tp TEMPLATE_FILE [-o OUTPUT_FILE][-o OUTPUT_FILE]
204 If no OUTPUT_FILE is given, the .h and .c file will be generated.
205 (The basename of the template file with be used for the generated file.
206 for example sample.tp will generate sample.h, sample.c and sample.o)
208 When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
209 The -o option can be repeated multiple times.
211 The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
212 as per defined in the lttng/tracepoint.h file.
213 See the lttng-ust(3) man page for more details on the format.
221 opts
, args
= getopt
.gnu_getopt(argv
[1:], "ho:av", ["help","verbose"])
222 except getopt
.error
as msg
:
226 print(err
.msg
, file=sys
.stderr
)
227 print("for help use --help", file=sys
.stderr
)
232 if o
in ("-h", "--help"):
236 outputNames
.append(a
)
239 if o
in ("-v", "--verbose"):
244 raise Usage("No template file given")
247 print(err
.msg
, file=sys
.stderr
)
248 print("for help use --help", file=sys
.stderr
)
254 headerFilename
= None
258 if len(outputNames
) > 0:
260 print("Cannot process more than one input if you specify an output")
263 for outputName
in outputNames
:
264 if outputName
[-2:] == ".h":
266 headerFilename
= outputName
267 elif outputName
[-2:] == ".c":
269 cFilename
= outputName
270 elif outputName
[-2:] == ".o":
272 objFilename
= outputName
274 print("output file type unsupported")
283 if arg
[-3:] != ".tp":
284 print(arg
+ " does not end in .tp. Skipping.")
289 tpl
= TemplateFile(arg
)
290 except IOError as args
:
291 print("Cannot read input file " + args
.filename
+ " " + args
.strerror
)
296 curFilename
= headerFilename
298 curFilename
= re
.sub("\.tp$",".h",arg
)
299 doth
= HeaderFile(curFilename
, tpl
)
303 curFilename
= cFilename
305 curFilename
= re
.sub("\.tp$",".c",arg
)
306 dotc
= CFile(curFilename
, tpl
)
310 curFilename
= objFilename
312 curFilename
= re
.sub("\.tp$",".o",arg
)
313 dotobj
= ObjFile(curFilename
, tpl
)
315 except IOError as args
:
316 print("Cannot write output file " + args
.filename
+ " " + args
.strerror
)
319 if __name__
== "__main__":
This page took 0.038987 seconds and 4 git commands to generate.