xref: /linux/tools/verification/dot2/dot2k.py (revision 40648d246fa4307ef11d185933cb0d79fc9ff46c)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0-only
3#
4# Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
5#
6# dot2k: transform dot files into a monitor for the Linux kernel.
7#
8# For further information, see:
9#   Documentation/trace/rv/da_monitor_synthesis.rst
10
11from dot2.dot2c import Dot2c
12import platform
13import os
14
15class dot2k(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"
20
21    def __init__(self, file_path, MonitorType, extra_params={}):
22        super().__init__(file_path, extra_params.get("model_name"))
23
24        self.monitor_type = self.monitor_types.get(MonitorType)
25        if self.monitor_type is None:
26            raise ValueError("Unknown monitor type: %s" % MonitorType)
27
28        self.monitor_type = MonitorType
29        self.__fill_rv_templates_dir()
30        self.main_c = self.__read_file(self.monitor_templates_dir + "main.c")
31        self.trace_h = self.__read_file(self.monitor_templates_dir + "trace.h")
32        self.kconfig = self.__read_file(self.monitor_templates_dir + "Kconfig")
33        self.enum_suffix = "_%s" % self.name
34        self.description = extra_params.get("description", self.name) or "auto-generated"
35        self.auto_patch = extra_params.get("auto_patch")
36        if self.auto_patch:
37            self.__fill_rv_kernel_dir()
38
39    def __fill_rv_templates_dir(self):
40
41        if os.path.exists(self.monitor_templates_dir):
42            return
43
44        if platform.system() != "Linux":
45            raise OSError("I can only run on Linux.")
46
47        kernel_path = "/lib/modules/%s/build/tools/verification/dot2/dot2k_templates/" % (platform.release())
48
49        if os.path.exists(kernel_path):
50            self.monitor_templates_dir = kernel_path
51            return
52
53        if os.path.exists("/usr/share/dot2/dot2k_templates/"):
54            self.monitor_templates_dir = "/usr/share/dot2/dot2k_templates/"
55            return
56
57        raise FileNotFoundError("Could not find the template directory, do you have the kernel source installed?")
58
59    def __fill_rv_kernel_dir(self):
60
61        # first try if we are running in the kernel tree root
62        if os.path.exists(self.rv_dir):
63            return
64
65        # offset if we are running inside the kernel tree from verification/dot2
66        kernel_path = os.path.join("../..", self.rv_dir)
67
68        if os.path.exists(kernel_path):
69            self.rv_dir = kernel_path
70            return
71
72        if platform.system() != "Linux":
73            raise OSError("I can only run on Linux.")
74
75        kernel_path = os.path.join("/lib/modules/%s/build" % platform.release(), self.rv_dir)
76
77        # if the current kernel is from a distro this may not be a full kernel tree
78        # verify that one of the files we are going to modify is available
79        if os.path.exists(os.path.join(kernel_path, "rv_trace.h")):
80            self.rv_dir = kernel_path
81            return
82
83        raise FileNotFoundError("Could not find the rv directory, do you have the kernel source installed?")
84
85    def __read_file(self, path):
86        try:
87            fd = open(path, 'r')
88        except OSError:
89            raise Exception("Cannot open the file: %s" % path)
90
91        content = fd.read()
92
93        fd.close()
94        return content
95
96    def __buff_to_string(self, buff):
97        string = ""
98
99        for line in buff:
100            string = string + line + "\n"
101
102        # cut off the last \n
103        return string[:-1]
104
105    def fill_monitor_type(self):
106        return self.monitor_type.upper()
107
108    def fill_tracepoint_handlers_skel(self):
109        buff = []
110        for event in self.events:
111            buff.append("static void handle_%s(void *data, /* XXX: fill header */)" % event)
112            buff.append("{")
113            handle = "handle_event"
114            if self.is_start_event(event):
115                buff.append("\t/* XXX: validate that this event always leads to the initial state */")
116                handle = "handle_start_event"
117            elif self.is_start_run_event(event):
118                buff.append("\t/* XXX: validate that this event is only valid in the initial state */")
119                handle = "handle_start_run_event"
120            if self.monitor_type == "per_task":
121                buff.append("\tstruct task_struct *p = /* XXX: how do I get p? */;");
122                buff.append("\tda_%s_%s(p, %s%s);" % (handle, self.name, event, self.enum_suffix));
123            else:
124                buff.append("\tda_%s_%s(%s%s);" % (handle, self.name, event, self.enum_suffix));
125            buff.append("}")
126            buff.append("")
127        return self.__buff_to_string(buff)
128
129    def fill_tracepoint_attach_probe(self):
130        buff = []
131        for event in self.events:
132            buff.append("\trv_attach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
133        return self.__buff_to_string(buff)
134
135    def fill_tracepoint_detach_helper(self):
136        buff = []
137        for event in self.events:
138            buff.append("\trv_detach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
139        return self.__buff_to_string(buff)
140
141    def fill_main_c(self):
142        main_c = self.main_c
143        monitor_type = self.fill_monitor_type()
144        min_type = self.get_minimun_type()
145        nr_events = len(self.events)
146        tracepoint_handlers = self.fill_tracepoint_handlers_skel()
147        tracepoint_attach = self.fill_tracepoint_attach_probe()
148        tracepoint_detach = self.fill_tracepoint_detach_helper()
149
150        main_c = main_c.replace("%%MONITOR_TYPE%%", monitor_type)
151        main_c = main_c.replace("%%MIN_TYPE%%", min_type)
152        main_c = main_c.replace("%%MODEL_NAME%%", self.name)
153        main_c = main_c.replace("%%NR_EVENTS%%", str(nr_events))
154        main_c = main_c.replace("%%TRACEPOINT_HANDLERS_SKEL%%", tracepoint_handlers)
155        main_c = main_c.replace("%%TRACEPOINT_ATTACH%%", tracepoint_attach)
156        main_c = main_c.replace("%%TRACEPOINT_DETACH%%", tracepoint_detach)
157        main_c = main_c.replace("%%DESCRIPTION%%", self.description)
158
159        return main_c
160
161    def fill_model_h_header(self):
162        buff = []
163        buff.append("/*")
164        buff.append(" * Automatically generated C representation of %s automaton" % (self.name))
165        buff.append(" * For further information about this format, see kernel documentation:")
166        buff.append(" *   Documentation/trace/rv/deterministic_automata.rst")
167        buff.append(" */")
168        buff.append("")
169
170        return buff
171
172    def fill_model_h(self):
173        #
174        # Adjust the definition names
175        #
176        self.enum_states_def = "states_%s" % self.name
177        self.enum_events_def = "events_%s" % self.name
178        self.struct_automaton_def = "automaton_%s" % self.name
179        self.var_automaton_def = "automaton_%s" % self.name
180
181        buff = self.fill_model_h_header()
182        buff += self.format_model()
183
184        return self.__buff_to_string(buff)
185
186    def fill_monitor_class_type(self):
187        if self.monitor_type == "per_task":
188            return "DA_MON_EVENTS_ID"
189        return "DA_MON_EVENTS_IMPLICIT"
190
191    def fill_monitor_class(self):
192        if self.monitor_type == "per_task":
193            return "da_monitor_id"
194        return "da_monitor"
195
196    def fill_tracepoint_args_skel(self, tp_type):
197        buff = []
198        tp_args_event = [
199                ("char *", "state"),
200                ("char *", "event"),
201                ("char *", "next_state"),
202                ("bool ",  "final_state"),
203                ]
204        tp_args_error = [
205                ("char *", "state"),
206                ("char *", "event"),
207                ]
208        tp_args_id = ("int ", "id")
209        tp_args = tp_args_event if tp_type == "event" else tp_args_error
210        if self.monitor_type == "per_task":
211            tp_args.insert(0, tp_args_id)
212        tp_proto_c = ", ".join([a+b for a,b in tp_args])
213        tp_args_c = ", ".join([b for a,b in tp_args])
214        buff.append("	     TP_PROTO(%s)," % tp_proto_c)
215        buff.append("	     TP_ARGS(%s)" % tp_args_c)
216        return self.__buff_to_string(buff)
217
218    def fill_trace_h(self):
219        trace_h = self.trace_h
220        monitor_class = self.fill_monitor_class()
221        monitor_class_type = self.fill_monitor_class_type()
222        tracepoint_args_skel_event = self.fill_tracepoint_args_skel("event")
223        tracepoint_args_skel_error = self.fill_tracepoint_args_skel("error")
224        trace_h = trace_h.replace("%%MODEL_NAME%%", self.name)
225        trace_h = trace_h.replace("%%MODEL_NAME_UP%%", self.name.upper())
226        trace_h = trace_h.replace("%%MONITOR_CLASS%%", monitor_class)
227        trace_h = trace_h.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type)
228        trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_EVENT%%", tracepoint_args_skel_event)
229        trace_h = trace_h.replace("%%TRACEPOINT_ARGS_SKEL_ERROR%%", tracepoint_args_skel_error)
230        return trace_h
231
232    def fill_kconfig(self):
233        kconfig = self.kconfig
234        monitor_class_type = self.fill_monitor_class_type()
235        kconfig = kconfig.replace("%%MODEL_NAME%%", self.name)
236        kconfig = kconfig.replace("%%MODEL_NAME_UP%%", self.name.upper())
237        kconfig = kconfig.replace("%%MONITOR_CLASS_TYPE%%", monitor_class_type)
238        kconfig = kconfig.replace("%%DESCRIPTION%%", self.description)
239        return kconfig
240
241    def __patch_file(self, file, marker, line):
242        file_to_patch = os.path.join(self.rv_dir, file)
243        content = self.__read_file(file_to_patch)
244        content = content.replace(marker, line + "\n" + marker)
245        self.__write_file(file_to_patch, content)
246
247    def fill_tracepoint_tooltip(self):
248        monitor_class_type = self.fill_monitor_class_type()
249        if self.auto_patch:
250            self.__patch_file("rv_trace.h",
251                            "// Add new monitors based on CONFIG_%s here" % monitor_class_type,
252                            "#include <monitors/%s/%s_trace.h>" % (self.name, self.name))
253            return "  - Patching %s/rv_trace.h, double check the result" % self.rv_dir
254
255        return """  - Edit %s/rv_trace.h:
256Add this line where other tracepoints are included and %s is defined:
257#include <monitors/%s/%s_trace.h>
258""" % (self.rv_dir, monitor_class_type, self.name, self.name)
259
260    def fill_kconfig_tooltip(self):
261        if self.auto_patch:
262            self.__patch_file("Kconfig",
263                            "# Add new monitors here",
264                            "source \"kernel/trace/rv/monitors/%s/Kconfig\"" % (self.name))
265            return "  - Patching %s/Kconfig, double check the result" % self.rv_dir
266
267        return """  - Edit %s/Kconfig:
268Add this line where other monitors are included:
269source \"kernel/trace/rv/monitors/%s/Kconfig\"
270""" % (self.rv_dir, self.name)
271
272    def fill_makefile_tooltip(self):
273        name = self.name
274        name_up = name.upper()
275        if self.auto_patch:
276            self.__patch_file("Makefile",
277                            "# Add new monitors here",
278                            "obj-$(CONFIG_RV_MON_%s) += monitors/%s/%s.o" % (name_up, name, name))
279            return "  - Patching %s/Makefile, double check the result" % self.rv_dir
280
281        return """  - Edit %s/Makefile:
282Add this line where other monitors are included:
283obj-$(CONFIG_RV_MON_%s) += monitors/%s/%s.o
284""" % (self.rv_dir, name_up, name, name)
285
286    def fill_monitor_tooltip(self):
287        if self.auto_patch:
288            return "  - Monitor created in %s/monitors/%s" % (self.rv_dir, self. name)
289        return "  - Move %s/ to the kernel's monitor directory (%s/monitors)" % (self.name, self.rv_dir)
290
291    def __create_directory(self):
292        path = self.name
293        if self.auto_patch:
294            path = os.path.join(self.rv_dir, "monitors", path)
295        try:
296            os.mkdir(path)
297        except FileExistsError:
298            return
299        except:
300            print("Fail creating the output dir: %s" % self.name)
301
302    def __write_file(self, file_name, content):
303        try:
304            file = open(file_name, 'w')
305        except:
306            print("Fail writing to file: %s" % file_name)
307
308        file.write(content)
309
310        file.close()
311
312    def __create_file(self, file_name, content):
313        path = "%s/%s" % (self.name, file_name)
314        if self.auto_patch:
315            path = os.path.join(self.rv_dir, "monitors", path)
316        self.__write_file(path, content)
317
318    def __get_main_name(self):
319        path = "%s/%s" % (self.name, "main.c")
320        if not os.path.exists(path):
321            return "main.c"
322        return "__main.c"
323
324    def print_files(self):
325        main_c = self.fill_main_c()
326        model_h = self.fill_model_h()
327
328        self.__create_directory()
329
330        path = "%s.c" % self.name
331        self.__create_file(path, main_c)
332
333        path = "%s.h" % self.name
334        self.__create_file(path, model_h)
335
336        trace_h = self.fill_trace_h()
337        path = "%s_trace.h" % self.name
338        self.__create_file(path, trace_h)
339
340        kconfig = self.fill_kconfig()
341        self.__create_file("Kconfig", kconfig)
342