dot2k.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <[email protected]>
  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. from dot2.dot2c import Dot2c
  11. import platform
  12. import os
  13. class dot2k(Dot2c):
  14. monitor_types = { "global" : 1, "per_cpu" : 2, "per_task" : 3 }
  15. monitor_templates_dir = "dot2k/rv_templates/"
  16. monitor_type = "per_cpu"
  17. def __init__(self, file_path, MonitorType):
  18. super().__init__(file_path)
  19. self.monitor_type = self.monitor_types.get(MonitorType)
  20. if self.monitor_type == None:
  21. raise Exception("Unknown monitor type: %s" % MonitorType)
  22. self.monitor_type = MonitorType
  23. self.__fill_rv_templates_dir()
  24. self.main_c = self.__open_file(self.monitor_templates_dir + "main_" + MonitorType + ".c")
  25. self.enum_suffix = "_%s" % self.name
  26. def __fill_rv_templates_dir(self):
  27. if os.path.exists(self.monitor_templates_dir) == True:
  28. return
  29. if platform.system() != "Linux":
  30. raise Exception("I can only run on Linux.")
  31. kernel_path = "/lib/modules/%s/build/tools/verification/dot2/dot2k_templates/" % (platform.release())
  32. if os.path.exists(kernel_path) == True:
  33. self.monitor_templates_dir = kernel_path
  34. return
  35. if os.path.exists("/usr/share/dot2/dot2k_templates/") == True:
  36. self.monitor_templates_dir = "/usr/share/dot2/dot2k_templates/"
  37. return
  38. raise Exception("Could not find the template directory, do you have the kernel source installed?")
  39. def __open_file(self, path):
  40. try:
  41. fd = open(path)
  42. except OSError:
  43. raise Exception("Cannot open the file: %s" % path)
  44. content = fd.read()
  45. return content
  46. def __buff_to_string(self, buff):
  47. string = ""
  48. for line in buff:
  49. string = string + line + "\n"
  50. # cut off the last \n
  51. return string[:-1]
  52. def fill_tracepoint_handlers_skel(self):
  53. buff = []
  54. for event in self.events:
  55. buff.append("static void handle_%s(void *data, /* XXX: fill header */)" % event)
  56. buff.append("{")
  57. if self.monitor_type == "per_task":
  58. buff.append("\tstruct task_struct *p = /* XXX: how do I get p? */;");
  59. buff.append("\tda_handle_event_%s(p, %s%s);" % (self.name, event, self.enum_suffix));
  60. else:
  61. buff.append("\tda_handle_event_%s(%s%s);" % (self.name, event, self.enum_suffix));
  62. buff.append("}")
  63. buff.append("")
  64. return self.__buff_to_string(buff)
  65. def fill_tracepoint_attach_probe(self):
  66. buff = []
  67. for event in self.events:
  68. buff.append("\trv_attach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
  69. return self.__buff_to_string(buff)
  70. def fill_tracepoint_detach_helper(self):
  71. buff = []
  72. for event in self.events:
  73. buff.append("\trv_detach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
  74. return self.__buff_to_string(buff)
  75. def fill_main_c(self):
  76. main_c = self.main_c
  77. min_type = self.get_minimun_type()
  78. nr_events = self.events.__len__()
  79. tracepoint_handlers = self.fill_tracepoint_handlers_skel()
  80. tracepoint_attach = self.fill_tracepoint_attach_probe()
  81. tracepoint_detach = self.fill_tracepoint_detach_helper()
  82. main_c = main_c.replace("MIN_TYPE", min_type)
  83. main_c = main_c.replace("MODEL_NAME", self.name)
  84. main_c = main_c.replace("NR_EVENTS", str(nr_events))
  85. main_c = main_c.replace("TRACEPOINT_HANDLERS_SKEL", tracepoint_handlers)
  86. main_c = main_c.replace("TRACEPOINT_ATTACH", tracepoint_attach)
  87. main_c = main_c.replace("TRACEPOINT_DETACH", tracepoint_detach)
  88. return main_c
  89. def fill_model_h_header(self):
  90. buff = []
  91. buff.append("/*")
  92. buff.append(" * Automatically generated C representation of %s automaton" % (self.name))
  93. buff.append(" * For further information about this format, see kernel documentation:")
  94. buff.append(" * Documentation/trace/rv/deterministic_automata.rst")
  95. buff.append(" */")
  96. buff.append("")
  97. return buff
  98. def fill_model_h(self):
  99. #
  100. # Adjust the definition names
  101. #
  102. self.enum_states_def = "states_%s" % self.name
  103. self.enum_events_def = "events_%s" % self.name
  104. self.struct_automaton_def = "automaton_%s" % self.name
  105. self.var_automaton_def = "automaton_%s" % self.name
  106. buff = self.fill_model_h_header()
  107. buff += self.format_model()
  108. return self.__buff_to_string(buff)
  109. def __create_directory(self):
  110. try:
  111. os.mkdir(self.name)
  112. except FileExistsError:
  113. return
  114. except:
  115. print("Fail creating the output dir: %s" % self.name)
  116. def __create_file(self, file_name, content):
  117. path = "%s/%s" % (self.name, file_name)
  118. try:
  119. file = open(path, 'w')
  120. except FileExistsError:
  121. return
  122. except:
  123. print("Fail creating file: %s" % path)
  124. file.write(content)
  125. file.close()
  126. def __get_main_name(self):
  127. path = "%s/%s" % (self.name, "main.c")
  128. if os.path.exists(path) == False:
  129. return "main.c"
  130. return "__main.c"
  131. def print_files(self):
  132. main_c = self.fill_main_c()
  133. model_h = self.fill_model_h()
  134. self.__create_directory()
  135. path = "%s.c" % self.name
  136. self.__create_file(path, main_c)
  137. path = "%s.h" % self.name
  138. self.__create_file(path, model_h)