Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env python3
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 #
0004 # Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
0005 #
0006 # dot2k: transform dot files into a monitor for the Linux kernel.
0007 #
0008 # For further information, see:
0009 #   Documentation/trace/rv/da_monitor_synthesis.rst
0010 
0011 from dot2.dot2c import Dot2c
0012 import platform
0013 import os
0014 
0015 class dot2k(Dot2c):
0016     monitor_types = { "global" : 1, "per_cpu" : 2, "per_task" : 3 }
0017     monitor_templates_dir = "dot2k/rv_templates/"
0018     monitor_type = "per_cpu"
0019 
0020     def __init__(self, file_path, MonitorType):
0021         super().__init__(file_path)
0022 
0023         self.monitor_type = self.monitor_types.get(MonitorType)
0024         if self.monitor_type == None:
0025             raise Exception("Unknown monitor type: %s" % MonitorType)
0026 
0027         self.monitor_type = MonitorType
0028         self.__fill_rv_templates_dir()
0029         self.main_c = self.__open_file(self.monitor_templates_dir + "main_" + MonitorType + ".c")
0030         self.enum_suffix = "_%s" % self.name
0031 
0032     def __fill_rv_templates_dir(self):
0033 
0034         if os.path.exists(self.monitor_templates_dir) == True:
0035             return
0036 
0037         if platform.system() != "Linux":
0038             raise Exception("I can only run on Linux.")
0039 
0040         kernel_path = "/lib/modules/%s/build/tools/verification/dot2/dot2k_templates/" % (platform.release())
0041 
0042         if os.path.exists(kernel_path) == True:
0043             self.monitor_templates_dir = kernel_path
0044             return
0045 
0046         if os.path.exists("/usr/share/dot2/dot2k_templates/") == True:
0047             self.monitor_templates_dir = "/usr/share/dot2/dot2k_templates/"
0048             return
0049 
0050         raise Exception("Could not find the template directory, do you have the kernel source installed?")
0051 
0052 
0053     def __open_file(self, path):
0054         try:
0055             fd = open(path)
0056         except OSError:
0057             raise Exception("Cannot open the file: %s" % path)
0058 
0059         content = fd.read()
0060 
0061         return content
0062 
0063     def __buff_to_string(self, buff):
0064         string = ""
0065 
0066         for line in buff:
0067             string = string + line + "\n"
0068 
0069         # cut off the last \n
0070         return string[:-1]
0071 
0072     def fill_tracepoint_handlers_skel(self):
0073         buff = []
0074         for event in self.events:
0075             buff.append("static void handle_%s(void *data, /* XXX: fill header */)" % event)
0076             buff.append("{")
0077             if self.monitor_type == "per_task":
0078                 buff.append("\tstruct task_struct *p = /* XXX: how do I get p? */;");
0079                 buff.append("\tda_handle_event_%s(p, %s%s);" % (self.name, event, self.enum_suffix));
0080             else:
0081                 buff.append("\tda_handle_event_%s(%s%s);" % (self.name, event, self.enum_suffix));
0082             buff.append("}")
0083             buff.append("")
0084         return self.__buff_to_string(buff)
0085 
0086     def fill_tracepoint_attach_probe(self):
0087         buff = []
0088         for event in self.events:
0089             buff.append("\trv_attach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
0090         return self.__buff_to_string(buff)
0091 
0092     def fill_tracepoint_detach_helper(self):
0093         buff = []
0094         for event in self.events:
0095             buff.append("\trv_detach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
0096         return self.__buff_to_string(buff)
0097 
0098     def fill_main_c(self):
0099         main_c = self.main_c
0100         min_type = self.get_minimun_type()
0101         nr_events = self.events.__len__()
0102         tracepoint_handlers = self.fill_tracepoint_handlers_skel()
0103         tracepoint_attach = self.fill_tracepoint_attach_probe()
0104         tracepoint_detach = self.fill_tracepoint_detach_helper()
0105 
0106         main_c = main_c.replace("MIN_TYPE", min_type)
0107         main_c = main_c.replace("MODEL_NAME", self.name)
0108         main_c = main_c.replace("NR_EVENTS", str(nr_events))
0109         main_c = main_c.replace("TRACEPOINT_HANDLERS_SKEL", tracepoint_handlers)
0110         main_c = main_c.replace("TRACEPOINT_ATTACH", tracepoint_attach)
0111         main_c = main_c.replace("TRACEPOINT_DETACH", tracepoint_detach)
0112 
0113         return main_c
0114 
0115     def fill_model_h_header(self):
0116         buff = []
0117         buff.append("/*")
0118         buff.append(" * Automatically generated C representation of %s automaton" % (self.name))
0119         buff.append(" * For further information about this format, see kernel documentation:")
0120         buff.append(" *   Documentation/trace/rv/deterministic_automata.rst")
0121         buff.append(" */")
0122         buff.append("")
0123 
0124         return buff
0125 
0126     def fill_model_h(self):
0127         #
0128         # Adjust the definition names
0129         #
0130         self.enum_states_def = "states_%s" % self.name
0131         self.enum_events_def = "events_%s" % self.name
0132         self.struct_automaton_def = "automaton_%s" % self.name
0133         self.var_automaton_def = "automaton_%s" % self.name
0134 
0135         buff = self.fill_model_h_header()
0136         buff += self.format_model()
0137 
0138         return self.__buff_to_string(buff)
0139 
0140     def __create_directory(self):
0141         try:
0142             os.mkdir(self.name)
0143         except FileExistsError:
0144             return
0145         except:
0146             print("Fail creating the output dir: %s" % self.name)
0147 
0148     def __create_file(self, file_name, content):
0149         path = "%s/%s" % (self.name, file_name)
0150         try:
0151             file = open(path, 'w')
0152         except FileExistsError:
0153             return
0154         except:
0155             print("Fail creating file: %s" % path)
0156 
0157         file.write(content)
0158 
0159         file.close()
0160 
0161     def __get_main_name(self):
0162         path = "%s/%s" % (self.name, "main.c")
0163         if os.path.exists(path) == False:
0164            return "main.c"
0165         return "__main.c"
0166 
0167     def print_files(self):
0168         main_c = self.fill_main_c()
0169         model_h = self.fill_model_h()
0170 
0171         self.__create_directory()
0172 
0173         path = "%s.c" % self.name
0174         self.__create_file(path, main_c)
0175 
0176         path = "%s.h" % self.name
0177         self.__create_file(path, model_h)