get_abi.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env python3
  2. # pylint: disable=R0903
  3. # Copyright(c) 2025: Mauro Carvalho Chehab <mchehab@kernel.org>.
  4. # SPDX-License-Identifier: GPL-2.0
  5. """
  6. Parse ABI documentation and produce results from it.
  7. """
  8. import argparse
  9. import logging
  10. import os
  11. import sys
  12. # Import Python modules
  13. LIB_DIR = "../lib/python"
  14. SRC_DIR = os.path.dirname(os.path.realpath(__file__))
  15. sys.path.insert(0, os.path.join(SRC_DIR, LIB_DIR))
  16. from abi.abi_parser import AbiParser # pylint: disable=C0413
  17. from abi.abi_regex import AbiRegex # pylint: disable=C0413
  18. from abi.helpers import ABI_DIR, DEBUG_HELP # pylint: disable=C0413
  19. from abi.system_symbols import SystemSymbols # pylint: disable=C0413
  20. # Command line classes
  21. REST_DESC = """
  22. Produce output in ReST format.
  23. The output is done on two sections:
  24. - Symbols: show all parsed symbols in alphabetic order;
  25. - Files: cross reference the content of each file with the symbols on it.
  26. """
  27. class AbiRest:
  28. """Initialize an argparse subparser for rest output"""
  29. def __init__(self, subparsers):
  30. """Initialize argparse subparsers"""
  31. parser = subparsers.add_parser("rest",
  32. formatter_class=argparse.RawTextHelpFormatter,
  33. description=REST_DESC)
  34. parser.add_argument("--enable-lineno", action="store_true",
  35. help="enable lineno")
  36. parser.add_argument("--raw", action="store_true",
  37. help="output text as contained in the ABI files. "
  38. "It not used, output will contain dynamically"
  39. " generated cross references when possible.")
  40. parser.add_argument("--no-file", action="store_true",
  41. help="Don't the files section")
  42. parser.add_argument("--show-hints", help="Show-hints")
  43. parser.set_defaults(func=self.run)
  44. def run(self, args):
  45. """Run subparser"""
  46. parser = AbiParser(args.dir, debug=args.debug)
  47. parser.parse_abi()
  48. parser.check_issues()
  49. for t in parser.doc(args.raw, not args.no_file):
  50. if args.enable_lineno:
  51. print (f".. LINENO {t[1]}#{t[2]}\n\n")
  52. print(t[0])
  53. class AbiValidate:
  54. """Initialize an argparse subparser for ABI validation"""
  55. def __init__(self, subparsers):
  56. """Initialize argparse subparsers"""
  57. parser = subparsers.add_parser("validate",
  58. formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  59. description="list events")
  60. parser.set_defaults(func=self.run)
  61. def run(self, args):
  62. """Run subparser"""
  63. parser = AbiParser(args.dir, debug=args.debug)
  64. parser.parse_abi()
  65. parser.check_issues()
  66. class AbiSearch:
  67. """Initialize an argparse subparser for ABI search"""
  68. def __init__(self, subparsers):
  69. """Initialize argparse subparsers"""
  70. parser = subparsers.add_parser("search",
  71. formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  72. description="Search ABI using a regular expression")
  73. parser.add_argument("expression",
  74. help="Case-insensitive search pattern for the ABI symbol")
  75. parser.set_defaults(func=self.run)
  76. def run(self, args):
  77. """Run subparser"""
  78. parser = AbiParser(args.dir, debug=args.debug)
  79. parser.parse_abi()
  80. parser.search_symbols(args.expression)
  81. UNDEFINED_DESC="""
  82. Check undefined ABIs on local machine.
  83. Read sysfs devnodes and check if the devnodes there are defined inside
  84. ABI documentation.
  85. The search logic tries to minimize the number of regular expressions to
  86. search per each symbol.
  87. By default, it runs on a single CPU, as Python support for CPU threads
  88. is still experimental, and multi-process runs on Python is very slow.
  89. On experimental tests, if the number of ABI symbols to search per devnode
  90. is contained on a limit of ~150 regular expressions, using a single CPU
  91. is a lot faster than using multiple processes. However, if the number of
  92. regular expressions to check is at the order of ~30000, using multiple
  93. CPUs speeds up the check.
  94. """
  95. class AbiUndefined:
  96. """
  97. Initialize an argparse subparser for logic to check undefined ABI at
  98. the current machine's sysfs
  99. """
  100. def __init__(self, subparsers):
  101. """Initialize argparse subparsers"""
  102. parser = subparsers.add_parser("undefined",
  103. formatter_class=argparse.RawTextHelpFormatter,
  104. description=UNDEFINED_DESC)
  105. parser.add_argument("-S", "--sysfs-dir", default="/sys",
  106. help="directory where sysfs is mounted")
  107. parser.add_argument("-s", "--search-string",
  108. help="search string regular expression to limit symbol search")
  109. parser.add_argument("-H", "--show-hints", action="store_true",
  110. help="Hints about definitions for missing ABI symbols.")
  111. parser.add_argument("-j", "--jobs", "--max-workers", type=int, default=1,
  112. help="If bigger than one, enables multiprocessing.")
  113. parser.add_argument("-c", "--max-chunk-size", type=int, default=50,
  114. help="Maximum number of chunk size")
  115. parser.add_argument("-f", "--found", action="store_true",
  116. help="Also show found items. "
  117. "Helpful to debug the parser."),
  118. parser.add_argument("-d", "--dry-run", action="store_true",
  119. help="Don't actually search for undefined. "
  120. "Helpful to debug the parser."),
  121. parser.set_defaults(func=self.run)
  122. def run(self, args):
  123. """Run subparser"""
  124. abi = AbiRegex(args.dir, debug=args.debug,
  125. search_string=args.search_string)
  126. abi_symbols = SystemSymbols(abi=abi, hints=args.show_hints,
  127. sysfs=args.sysfs_dir)
  128. abi_symbols.check_undefined_symbols(dry_run=args.dry_run,
  129. found=args.found,
  130. max_workers=args.jobs,
  131. chunk_size=args.max_chunk_size)
  132. def main():
  133. """Main program"""
  134. parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
  135. parser.add_argument("-d", "--debug", type=int, default=0, help="debug level")
  136. parser.add_argument("-D", "--dir", default=ABI_DIR, help=DEBUG_HELP)
  137. subparsers = parser.add_subparsers()
  138. AbiRest(subparsers)
  139. AbiValidate(subparsers)
  140. AbiSearch(subparsers)
  141. AbiUndefined(subparsers)
  142. args = parser.parse_args()
  143. if args.debug:
  144. level = logging.DEBUG
  145. else:
  146. level = logging.INFO
  147. logging.basicConfig(level=level, format="[%(levelname)s] %(message)s")
  148. if "func" in args:
  149. args.func(args)
  150. else:
  151. sys.exit(f"Please specify a valid command for {sys.argv[0]}")
  152. # Call main method
  153. if __name__ == "__main__":
  154. main()