2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
6 # dot2k: transform dot files into a monitor for the Linux kernel.
8 # For further information, see:
9 # Documentation/trace/rv/da_monitor_synthesis.rst
11 from dot2.dot2c import Dot2c
16 monitor_types = { "global" : 1, "per_cpu" : 2, "per_task" : 3 }
17 monitor_templates_dir = "dot2/dot2k_templates/"
18 rv_dir = "kernel/trace/rv"
19 monitor_type = "per_cpu"
21 def __init__(self, file_path, MonitorType, extra_params={}):
22 self.container = extra_params.get("container")
23 self.parent = extra_params.get("parent")
24 self.__fill_rv_templates_dir()
28 raise ValueError("A container does not require a dot file")
30 raise ValueError("A container does not require a monitor type")
32 raise ValueError("A container cannot have a parent")
33 self.name = extra_params.get("model_name")
36 self.main_c = self.__read_file(self.monitor_templates_dir + "main_container.c")
37 self.main_h = self.__read_file(self.monitor_templates_dir + "main_container.h")
39 super().__init__(file_path, extra_params.get("model_name"))
41 self.monitor_type = self.monitor_types.get(MonitorType)
42 if self.monitor_type is None:
43 raise ValueError("Unknown monitor type: %s" % MonitorType)
44 self.monitor_type = MonitorType
45 self.main_c = self.__read_file(self.monitor_templates_dir + "main.c")
46 self.trace_h = self.__read_file(self.monitor_templates_dir + "trace.h")
47 self.kconfig = self.__read_file(self.monitor_templates_dir + "Kconfig")
48 self.enum_suffix = "_%s" % self.name
49 self.description = extra_params.get("description", self.name) or "auto-generated"
50 self.auto_patch = extra_params.get("auto_patch")
52 self.__fill_rv_kernel_dir()
54 def __fill_rv_templates_dir(self):
56 if os.path.exists(self.monitor_templates_dir):
59 if platform.system() != "Linux":
60 raise OSError("I can only run on Linux.")
62 kernel_path = "/lib/modules/%s/build/tools/verification/dot2/dot2k_templates/" % (platform.release())
64 if os.path.exists(kernel_path):
65 self.monitor_templates_dir = kernel_path
68 if os.path.exists("/usr/share/dot2/dot2k_templates/"):
69 self.monitor_templates_dir = "/usr/share/dot2/dot2k_templates/"
72 raise FileNotFoundError("Could not find the template directory, do you have the kernel source installed?")
74 def __fill_rv_kernel_dir(self):
76 # first try if we are running in the kernel tree root
77 if os.path.exists(self.rv_dir):
80 # offset if we are running inside the kernel tree from verification/dot2
81 kernel_path = os.path.join("../..", self.rv_dir)
83 if os.path.exists(kernel_path):
84 self.rv_dir = kernel_path
87 if platform.system() != "Linux":
88 raise OSError("I can only run on Linux.")
90 kernel_path = os.path.join("/lib/modules/%s/build" % platform.release(), self.rv_dir)
92 # if the current kernel is from a distro this may not be a full kernel tree
93 # verify that one of the files we are going to modify is available
94 if os.path.exists(os.path.join(kernel_path, "rv_trace.h")):
95 self.rv_dir = kernel_path
98 raise FileNotFoundError("Could not find the rv directory, do you have the kernel source installed?")
100 def __read_file(self, path):
104 raise Exception("Cannot open the file: %s" % path)
111 def __buff_to_string(self, buff):
115 string = string + line + "\n"
117 # cut off the last \n
120 def fill_monitor_type(self):
121 return self.monitor_type.upper()
123 def fill_parent(self):
124 return "&rv_%s" % self.parent if self.parent else "NULL"
126 def fill_include_parent(self):
128 return "#include <monitors/%s/%s.h>\n" % (self.parent, self.parent)
131 def fill_tracepoint_handlers_skel(self):
133 for event in self.events:
134 buff.append("static void handle_%s(void *data, /* XXX: fill header */)" % event)
136 handle = "handle_event"
137 if self.is_start_event(event):
138 buff.append("\t/* XXX: validate that this event always leads to the initial state */")
139 handle = "handle_start_event"
140 elif self.is_start_run_event(event):
141 buff.append("\t/* XXX: validate that this event is only valid in the initial state */")
142 handle = "handle_start_run_event"
143 if self.monitor_type == "per_task":
144 buff.append("\tstruct task_struct *p = /* XXX: how do I get p? */;");
145 buff.append("\tda_%s_%s(p, %s%s);" % (handle, self.name, event, self.enum_suffix));
147 buff.append("\tda_%s_%s(%s%s);" % (handle, self.name, event, self.enum_suffix));
150 return self.__buff_to_string(buff)
152 def fill_tracepoint_attach_probe(self):
154 for event in self.events:
155 buff.append("\trv_attach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
156 return self.__buff_to_string(buff)
158 def fill_tracepoint_detach_helper(self):
160 for event in self.events:
161 buff.append("\trv_detach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
162 return self.__buff_to_string(buff)
164 def fill_main_c(self):
166 monitor_type = self.fill_monitor_type()
167 min_type = self.get_minimun_type()
168 nr_events = len(self.events)
169 tracepoint_handlers = self.fill_tracepoint_handlers_skel()
170 tracepoint_attach = self.fill_tracepoint_attach_probe()
171 tracepoint_detach = self.fill_tracepoint_detach_helper()
172 parent = self.fill_parent()
173 parent_include = self.fill_include_parent()
175 main_c = main_c.replace("%%MONITOR_TYPE%%", monitor_type)
176 main_c = main_c.replace("%%MIN_TYPE%%", min_type)
177 main_c = main_c.replace("%%MODEL_NAME%%", self.name)
178 main_c = main_c.replace("%%NR_EVENTS%%", str(nr_events))
179 main_c = main_c.replace("%%TRACEPOINT_HANDLERS_SKEL%%", tracepoint_handlers)
180 main_c = main_c.replace("%%TRACEPOINT_ATTACH%%", tracepoint_attach)
181 main_c = main_c.replace("%%TRACEPOINT_DETACH%%", tracepoint_detach)
182 main_c = main_c.replace("%%DESCRIPTION%%", self.description)
183 main_c = main_c.replace("%%PARENT%%", parent)
184 main_c = main_c.replace("%%INCLUDE_PARENT%%", parent_include)
188 def fill_model_h_header(self):
190 buff.append("/* SPDX-License-Identifier: GPL-2.0 */")
192 buff.append(" * Automatically generated C representation of %s automaton" % (self.name))
193 buff.append(" * For further information about this format, see kernel documentation:")
194 buff.append(" * Documentation/trace/rv/deterministic_automata.rst")
200 def fill_model_h(self):
202 # Adjust the definition names
204 self.enum_states_def = "states_%s" % self.name
205 self.enum_events_def = "events_%s" % self.name
206 self.struct_automaton_def = "automaton_%s" % self.name
207 self.var_automaton_def = "automaton_%s" % self.name
209 buff = self.fill_model_h_header()
210 buff += self.format_model()
212 return self.__buff_to_string(buff)
214 def fill_monitor_class_type(self):
215 if self.monitor_type == "per_task":
216 return "DA_MON_EVENTS_ID"
217 return "DA_MON_EVENTS_IMPLICIT"
219 def fill_monitor_class(self):
220 if self.monitor_type == "per_task":
221 return "da_monitor_id"
224 def fill_tracepoint_args_skel(self, tp_type):
229 ("char *", "next_state"),
230 ("bool ", "final_state"),
236 tp_args_id = ("int ", "id")
237 tp_args = tp_args_event if tp_type == "event" else tp_args_error
238 if self.monitor_type == "per_task":
239 tp_args.insert(0, tp_args_id)
240 tp_proto_c = ", ".join([a+b for a,b in tp_args])
241 tp_args_c = ", ".join([b for a,b in tp_args])
242 buff.append(" TP_PROTO(%s)," % tp_proto_c)
243 buff.append(" TP_ARGS(%s)" % tp_args_c)
244 return self.__buff_to_string(buff)
246 def fill_monitor_deps(self):
248 buff.append(" # XXX: add dependencies if there")
250 buff.append(" depends on RV_MON_%s" % self.parent.upper())
251 buff.append(" default y")
252 return self.__buff_to_string(buff)
254 def fill_trace_h(self):
255 trace_h = self.trace_h
256 monitor_class = self.fill_monitor_class()
257 monitor_class_type = self.fill_monitor_class_type()
258 tracepoint_args_skel_event = self.fill_tracepoint_args_skel("event")
259 tracepoint_args_skel_error = self.fill_tracepoint_args_skel("error")
260 trace_h = trace_h.replace("%%MODEL_NAME%%", self.name)
261 trace_h = trace_h.replace("%%MODEL_NAME_UP%%", self.name.upper())
262 trace_h = trace_h.replace("%%MONITOR_CLASS%%", monitor_class)
263 trace_h = trace_h.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type)
264 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_EVENT%%", tracepoint_args_skel_event)
265 trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_ERROR%%", tracepoint_args_skel_error)
268 def fill_kconfig(self):
269 kconfig = self.kconfig
270 monitor_class_type = self.fill_monitor_class_type()
271 monitor_deps = self.fill_monitor_deps()
272 kconfig = kconfig.replace("%%MODEL_NAME%%", self.name)
273 kconfig = kconfig.replace("%%MODEL_NAME_UP%%", self.name.upper())
274 kconfig = kconfig.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type)
275 kconfig = kconfig.replace("%%DESCRIPTION%%", self.description)
276 kconfig = kconfig.replace("%%MONITOR_DEPS%%", monitor_deps)
279 def fill_main_container_h(self):
281 main_h = main_h.replace("%%MODEL_NAME%%", self.name)
284 def __patch_file(self, file, marker, line):
285 file_to_patch = os.path.join(self.rv_dir, file)
286 content = self.__read_file(file_to_patch)
287 content = content.replace(marker, line + "\n" + marker)
288 self.__write_file(file_to_patch, content)
290 def fill_tracepoint_tooltip(self):
291 monitor_class_type = self.fill_monitor_class_type()
293 self.__patch_file("rv_trace.h",
294 "// Add new monitors based on CONFIG_%s here" % monitor_class_type,
295 "#include <monitors/%s/%s_trace.h>" % (self.name, self.name))
296 return " - Patching %s/rv_trace.h, double check the result" % self.rv_dir
298 return """ - Edit %s/rv_trace.h:
299 Add this line where other tracepoints are included and %s is defined:
300 #include <monitors/%s/%s_trace.h>
301 """ % (self.rv_dir, monitor_class_type, self.name, self.name)
303 def fill_kconfig_tooltip(self):
305 self.__patch_file("Kconfig",
306 "# Add new monitors here",
307 "source \"kernel/trace/rv/monitors/%s/Kconfig\"" % (self.name))
308 return " - Patching %s/Kconfig, double check the result" % self.rv_dir
310 return """ - Edit %s/Kconfig:
311 Add this line where other monitors are included:
312 source \"kernel/trace/rv/monitors/%s/Kconfig\"
313 """ % (self.rv_dir, self.name)
315 def fill_makefile_tooltip(self):
317 name_up = name.upper()
319 self.__patch_file("Makefile",
320 "# Add new monitors here",
321 "obj-$(CONFIG_RV_MON_%s) += monitors/%s/%s.o" % (name_up, name, name))
322 return " - Patching %s/Makefile, double check the result" % self.rv_dir
324 return """ - Edit %s/Makefile:
325 Add this line where other monitors are included:
326 obj-$(CONFIG_RV_MON_%s) += monitors/%s/%s.o
327 """ % (self.rv_dir, name_up, name, name)
329 def fill_monitor_tooltip(self):
331 return " - Monitor created in %s/monitors/%s" % (self.rv_dir, self. name)
332 return " - Move %s/ to the kernel's monitor directory (%s/monitors)" % (self.name, self.rv_dir)
334 def __create_directory(self):
337 path = os.path.join(self.rv_dir, "monitors", path)
340 except FileExistsError:
343 print("Fail creating the output dir: %s" % self.name)
345 def __write_file(self, file_name, content):
347 file = open(file_name, 'w')
349 print("Fail writing to file: %s" % file_name)
355 def __create_file(self, file_name, content):
356 path = "%s/%s" % (self.name, file_name)
358 path = os.path.join(self.rv_dir, "monitors", path)
359 self.__write_file(path, content)
361 def __get_main_name(self):
362 path = "%s/%s" % (self.name, "main.c")
363 if not os.path.exists(path):
367 def print_files(self):
368 main_c = self.fill_main_c()
370 self.__create_directory()
372 path = "%s.c" % self.name
373 self.__create_file(path, main_c)
376 main_h = self.fill_main_container_h()
377 path = "%s.h" % self.name
378 self.__create_file(path, main_h)
380 model_h = self.fill_model_h()
381 path = "%s.h" % self.name
382 self.__create_file(path, model_h)
384 trace_h = self.fill_trace_h()
385 path = "%s_trace.h" % self.name
386 self.__create_file(path, trace_h)
388 kconfig = self.fill_kconfig()
389 self.__create_file("Kconfig", kconfig)