crosstap 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #!/usr/bin/env python3
  2. #
  3. # Build a systemtap script for a given image, kernel
  4. #
  5. # Effectively script extracts needed information from set of
  6. # 'bitbake -e' commands and contructs proper invocation of stap on
  7. # host to build systemtap script for a given target.
  8. #
  9. # By default script will compile scriptname.ko that could be copied
  10. # to taget and activated with 'staprun scriptname.ko' command. Or if
  11. # --remote user@hostname option is specified script will build, load
  12. # execute script on target.
  13. #
  14. # This script is very similar and inspired by crosstap shell script.
  15. # The major difference that this script supports user-land related
  16. # systemtap script, whereas crosstap could deal only with scripts
  17. # related to kernel.
  18. #
  19. # Copyright (c) 2018, Cisco Systems.
  20. #
  21. # SPDX-License-Identifier: GPL-2.0-only
  22. #
  23. import sys
  24. import re
  25. import subprocess
  26. import os
  27. import optparse
  28. class Stap(object):
  29. def __init__(self, script, module, remote):
  30. self.script = script
  31. self.module = module
  32. self.remote = remote
  33. self.stap = None
  34. self.sysroot = None
  35. self.runtime = None
  36. self.tapset = None
  37. self.arch = None
  38. self.cross_compile = None
  39. self.kernel_release = None
  40. self.target_path = None
  41. self.target_ld_library_path = None
  42. if not self.remote:
  43. if not self.module:
  44. # derive module name from script
  45. self.module = os.path.basename(self.script)
  46. if self.module[-4:] == ".stp":
  47. self.module = self.module[:-4]
  48. # replace - if any with _
  49. self.module = self.module.replace("-", "_")
  50. def command(self, args):
  51. ret = []
  52. ret.append(self.stap)
  53. if self.remote:
  54. ret.append("--remote")
  55. ret.append(self.remote)
  56. else:
  57. ret.append("-p4")
  58. ret.append("-m")
  59. ret.append(self.module)
  60. ret.append("-a")
  61. ret.append(self.arch)
  62. ret.append("-B")
  63. ret.append("CROSS_COMPILE=" + self.cross_compile)
  64. ret.append("-r")
  65. ret.append(self.kernel_release)
  66. ret.append("-I")
  67. ret.append(self.tapset)
  68. ret.append("-R")
  69. ret.append(self.runtime)
  70. if self.sysroot:
  71. ret.append("--sysroot")
  72. ret.append(self.sysroot)
  73. ret.append("--sysenv=PATH=" + self.target_path)
  74. ret.append("--sysenv=LD_LIBRARY_PATH=" + self.target_ld_library_path)
  75. ret = ret + args
  76. ret.append(self.script)
  77. return ret
  78. def additional_environment(self):
  79. ret = {}
  80. ret["SYSTEMTAP_DEBUGINFO_PATH"] = "+:.debug:build"
  81. return ret
  82. def environment(self):
  83. ret = os.environ.copy()
  84. additional = self.additional_environment()
  85. for e in additional:
  86. ret[e] = additional[e]
  87. return ret
  88. def display_command(self, args):
  89. additional_env = self.additional_environment()
  90. command = self.command(args)
  91. print("#!/bin/sh")
  92. for e in additional_env:
  93. print("export %s=\"%s\"" % (e, additional_env[e]))
  94. print(" ".join(command))
  95. class BitbakeEnvInvocationException(Exception):
  96. def __init__(self, message):
  97. self.message = message
  98. class BitbakeEnv(object):
  99. BITBAKE="bitbake"
  100. def __init__(self, package):
  101. self.package = package
  102. self.cmd = BitbakeEnv.BITBAKE + " -e " + self.package
  103. self.popen = subprocess.Popen(self.cmd, shell=True,
  104. stdout=subprocess.PIPE,
  105. stderr=subprocess.STDOUT)
  106. self.__lines = self.popen.stdout.readlines()
  107. self.popen.wait()
  108. self.lines = []
  109. for line in self.__lines:
  110. self.lines.append(line.decode('utf-8'))
  111. def get_vars(self, vars):
  112. if self.popen.returncode:
  113. raise BitbakeEnvInvocationException(
  114. "\nFailed to execute '" + self.cmd +
  115. "' with the following message:\n" +
  116. ''.join(self.lines))
  117. search_patterns = []
  118. retdict = {}
  119. for var in vars:
  120. # regular not exported variable
  121. rexpr = "^" + var + "=\"(.*)\""
  122. re_compiled = re.compile(rexpr)
  123. search_patterns.append((var, re_compiled))
  124. # exported variable
  125. rexpr = "^export " + var + "=\"(.*)\""
  126. re_compiled = re.compile(rexpr)
  127. search_patterns.append((var, re_compiled))
  128. for line in self.lines:
  129. for var, rexpr in search_patterns:
  130. m = rexpr.match(line)
  131. if m:
  132. value = m.group(1)
  133. retdict[var] = value
  134. # fill variables values in order how they were requested
  135. ret = []
  136. for var in vars:
  137. ret.append(retdict.get(var))
  138. # if it is single value list return it as scalar, not the list
  139. if len(ret) == 1:
  140. ret = ret[0]
  141. return ret
  142. class ParamDiscovery(object):
  143. SYMBOLS_CHECK_MESSAGE = """
  144. WARNING: image '%s' does not have dbg-pkgs IMAGE_FEATURES enabled and no
  145. "image-combined-dbg" in inherited classes is specified. As result the image
  146. does not have symbols for user-land processes DWARF based probes. Consider
  147. adding 'dbg-pkgs' to EXTRA_IMAGE_FEATURES or adding "image-combined-dbg" to
  148. USER_CLASSES. I.e add this line 'USER_CLASSES += "image-combined-dbg"' to
  149. local.conf file.
  150. Or you may use IMAGE_GEN_DEBUGFS="1" option, and then after build you need
  151. recombine/unpack image and image-dbg tarballs and pass resulting dir location
  152. with --sysroot option.
  153. """
  154. def __init__(self, image):
  155. self.image = image
  156. self.image_rootfs = None
  157. self.image_features = None
  158. self.image_gen_debugfs = None
  159. self.inherit = None
  160. self.base_bindir = None
  161. self.base_sbindir = None
  162. self.base_libdir = None
  163. self.bindir = None
  164. self.sbindir = None
  165. self.libdir = None
  166. self.staging_bindir_toolchain = None
  167. self.target_prefix = None
  168. self.target_arch = None
  169. self.target_kernel_builddir = None
  170. self.staging_dir_native = None
  171. self.image_combined_dbg = False
  172. def discover(self):
  173. if self.image:
  174. benv_image = BitbakeEnv(self.image)
  175. (self.image_rootfs,
  176. self.image_features,
  177. self.image_gen_debugfs,
  178. self.inherit,
  179. self.base_bindir,
  180. self.base_sbindir,
  181. self.base_libdir,
  182. self.bindir,
  183. self.sbindir,
  184. self.libdir
  185. ) = benv_image.get_vars(
  186. ("IMAGE_ROOTFS",
  187. "IMAGE_FEATURES",
  188. "IMAGE_GEN_DEBUGFS",
  189. "INHERIT",
  190. "base_bindir",
  191. "base_sbindir",
  192. "base_libdir",
  193. "bindir",
  194. "sbindir",
  195. "libdir"
  196. ))
  197. benv_kernel = BitbakeEnv("virtual/kernel")
  198. (self.staging_bindir_toolchain,
  199. self.target_prefix,
  200. self.target_arch,
  201. self.target_kernel_builddir
  202. ) = benv_kernel.get_vars(
  203. ("STAGING_BINDIR_TOOLCHAIN",
  204. "TARGET_PREFIX",
  205. "TRANSLATED_TARGET_ARCH",
  206. "B"
  207. ))
  208. benv_systemtap = BitbakeEnv("systemtap-native")
  209. (self.staging_dir_native
  210. ) = benv_systemtap.get_vars(["STAGING_DIR_NATIVE"])
  211. if self.inherit:
  212. if "image-combined-dbg" in self.inherit.split():
  213. self.image_combined_dbg = True
  214. def check(self, sysroot_option):
  215. ret = True
  216. if self.image_rootfs:
  217. sysroot = self.image_rootfs
  218. if not os.path.isdir(self.image_rootfs):
  219. print("ERROR: Cannot find '" + sysroot +
  220. "' directory. Was '" + self.image + "' image built?")
  221. ret = False
  222. stap = self.staging_dir_native + "/usr/bin/stap"
  223. if not os.path.isfile(stap):
  224. print("ERROR: Cannot find '" + stap +
  225. "'. Was 'systemtap-native' built?")
  226. ret = False
  227. if not os.path.isdir(self.target_kernel_builddir):
  228. print("ERROR: Cannot find '" + self.target_kernel_builddir +
  229. "' directory. Was 'kernel/virtual' built?")
  230. ret = False
  231. if not sysroot_option and self.image_rootfs:
  232. dbg_pkgs_found = False
  233. if self.image_features:
  234. image_features = self.image_features.split()
  235. if "dbg-pkgs" in image_features:
  236. dbg_pkgs_found = True
  237. if not dbg_pkgs_found \
  238. and not self.image_combined_dbg:
  239. print(ParamDiscovery.SYMBOLS_CHECK_MESSAGE % (self.image))
  240. if not ret:
  241. print("")
  242. return ret
  243. def __map_systemtap_arch(self):
  244. a = self.target_arch
  245. ret = a
  246. if re.match('(athlon|x86.64)$', a):
  247. ret = 'x86_64'
  248. elif re.match('i.86$', a):
  249. ret = 'i386'
  250. elif re.match('arm$', a):
  251. ret = 'arm'
  252. elif re.match('aarch64$', a):
  253. ret = 'arm64'
  254. elif re.match('mips(isa|)(32|64|)(r6|)(el|)$', a):
  255. ret = 'mips'
  256. elif re.match('p(pc|owerpc)(|64)', a):
  257. ret = 'powerpc'
  258. return ret
  259. def fill_stap(self, stap):
  260. stap.stap = self.staging_dir_native + "/usr/bin/stap"
  261. if not stap.sysroot:
  262. if self.image_rootfs:
  263. if self.image_combined_dbg:
  264. stap.sysroot = self.image_rootfs + "-dbg"
  265. else:
  266. stap.sysroot = self.image_rootfs
  267. stap.runtime = self.staging_dir_native + "/usr/share/systemtap/runtime"
  268. stap.tapset = self.staging_dir_native + "/usr/share/systemtap/tapset"
  269. stap.arch = self.__map_systemtap_arch()
  270. stap.cross_compile = self.staging_bindir_toolchain + "/" + \
  271. self.target_prefix
  272. stap.kernel_release = self.target_kernel_builddir
  273. # do we have standard that tells in which order these need to appear
  274. target_path = []
  275. if self.sbindir:
  276. target_path.append(self.sbindir)
  277. if self.bindir:
  278. target_path.append(self.bindir)
  279. if self.base_sbindir:
  280. target_path.append(self.base_sbindir)
  281. if self.base_bindir:
  282. target_path.append(self.base_bindir)
  283. stap.target_path = ":".join(target_path)
  284. target_ld_library_path = []
  285. if self.libdir:
  286. target_ld_library_path.append(self.libdir)
  287. if self.base_libdir:
  288. target_ld_library_path.append(self.base_libdir)
  289. stap.target_ld_library_path = ":".join(target_ld_library_path)
  290. def main():
  291. usage = """usage: %prog -s <systemtap-script> [options] [-- [systemtap options]]
  292. %prog cross compile given SystemTap script against given image, kernel
  293. It needs to run in environtment set for bitbake - it uses bitbake -e
  294. invocations to retrieve information to construct proper stap cross build
  295. invocation arguments. It assumes that systemtap-native is built in given
  296. bitbake workspace.
  297. Anything after -- option is passed directly to stap.
  298. Legacy script invocation style supported but depreciated:
  299. %prog <user@hostname> <sytemtap-script> [systemtap options]
  300. To enable most out of systemtap the following site.conf or local.conf
  301. configuration is recommended:
  302. # enables symbol + target binaries rootfs-dbg in workspace
  303. IMAGE_GEN_DEBUGFS = "1"
  304. IMAGE_FSTYPES_DEBUGFS = "tar.bz2"
  305. USER_CLASSES += "image-combined-dbg"
  306. # enables kernel debug symbols
  307. KERNEL_EXTRA_FEATURES_append = " features/debug/debug-kernel.scc"
  308. # minimal, just run-time systemtap configuration in target image
  309. PACKAGECONFIG_pn-systemtap = "monitor"
  310. # add systemtap run-time into target image if it is not there yet
  311. IMAGE_INSTALL_append = " systemtap"
  312. """
  313. option_parser = optparse.OptionParser(usage=usage)
  314. option_parser.add_option("-s", "--script", dest="script",
  315. help="specify input script FILE name",
  316. metavar="FILE")
  317. option_parser.add_option("-i", "--image", dest="image",
  318. help="specify image name for which script should be compiled")
  319. option_parser.add_option("-r", "--remote", dest="remote",
  320. help="specify username@hostname of remote target to run script "
  321. "optional, it assumes that remote target can be accessed through ssh")
  322. option_parser.add_option("-m", "--module", dest="module",
  323. help="specify module name, optional, has effect only if --remote is not used, "
  324. "if not specified module name will be derived from passed script name")
  325. option_parser.add_option("-y", "--sysroot", dest="sysroot",
  326. help="explicitely specify image sysroot location. May need to use it in case "
  327. "when IMAGE_GEN_DEBUGFS=\"1\" option is used and recombined with symbols "
  328. "in different location",
  329. metavar="DIR")
  330. option_parser.add_option("-o", "--out", dest="out",
  331. action="store_true",
  332. help="output shell script that equvivalent invocation of this script with "
  333. "given set of arguments, in given bitbake environment. It could be stored in "
  334. "separate shell script and could be repeated without incuring bitbake -e "
  335. "invocation overhead",
  336. default=False)
  337. option_parser.add_option("-d", "--debug", dest="debug",
  338. action="store_true",
  339. help="enable debug output. Use this option to see resulting stap invocation",
  340. default=False)
  341. # is invocation follow syntax from orignal crosstap shell script
  342. legacy_args = False
  343. # check if we called the legacy way
  344. if len(sys.argv) >= 3:
  345. if sys.argv[1].find("@") != -1 and os.path.exists(sys.argv[2]):
  346. legacy_args = True
  347. # fill options values for legacy invocation case
  348. options = optparse.Values
  349. options.script = sys.argv[2]
  350. options.remote = sys.argv[1]
  351. options.image = None
  352. options.module = None
  353. options.sysroot = None
  354. options.out = None
  355. options.debug = None
  356. remaining_args = sys.argv[3:]
  357. if not legacy_args:
  358. (options, remaining_args) = option_parser.parse_args()
  359. if not options.script or not os.path.exists(options.script):
  360. print("'-s FILE' option is missing\n")
  361. option_parser.print_help()
  362. else:
  363. stap = Stap(options.script, options.module, options.remote)
  364. discovery = ParamDiscovery(options.image)
  365. discovery.discover()
  366. if not discovery.check(options.sysroot):
  367. option_parser.print_help()
  368. else:
  369. stap.sysroot = options.sysroot
  370. discovery.fill_stap(stap)
  371. if options.out:
  372. stap.display_command(remaining_args)
  373. else:
  374. cmd = stap.command(remaining_args)
  375. env = stap.environment()
  376. if options.debug:
  377. print(" ".join(cmd))
  378. os.execve(cmd[0], cmd, env)
  379. main()