Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen_bpf_attr_check.sh 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/sh -efu
  2. # Copyright (c) 2018 Dmitry V. Levin <ldv@altlinux.org>
  3. # Copyright (c) 2018-2019 The strace developers.
  4. # All rights reserved.
  5. #
  6. # SPDX-License-Identifier: LGPL-2.1-or-later
  7. [ "x${D:-0}" != x1 ] || set -x
  8. input="$1"
  9. shift
  10. cat <<EOF
  11. /* Generated by $0 from $input; do not edit. */
  12. #include "defs.h"
  13. #ifdef HAVE_LINUX_BPF_H
  14. # include <linux/bpf.h>
  15. # include "bpf_attr.h"
  16. # include "static_assert.h"
  17. # define SoM(type_, member_) (sizeof(((type_ *)0)->member_))
  18. EOF
  19. for struct in $(sed -n 's/^struct \([^[:space:]]\+_struct\) .*/\1/p' < "$input"); do
  20. case "$struct" in
  21. BPF_*) type_name='union bpf_attr' ;;
  22. *) type_name="struct ${struct%_struct}" ;;
  23. esac
  24. TYPE_NAME="$(printf %s "$type_name" |tr '[:lower:] ' '[:upper:]_')"
  25. enum="$(sed -n 's/^struct '"$struct"' \/\* \([^[:space:]]\+\) \*\/ {.*/\1/p' < "$input")"
  26. ENUM="$(printf %s "$enum" |tr '[:lower:]' '[:upper:]')"
  27. enum="$enum${enum:+.}"
  28. ENUM="$ENUM${ENUM:+_}"
  29. sed -n '/^struct '"$struct"' [^{]*{/,/^};$/p' < "$input" |
  30. sed -n 's/^[[:space:]]\+[^][;:]*[[:space:]]\([^][[:space:];:]\+\)\(\[[^;:]*\]\)\?;$/\1/p' |
  31. while read field; do
  32. FIELD="$(printf %s "$field" |tr '[:lower:]' '[:upper:]')"
  33. cat <<EOF
  34. # ifdef HAVE_${TYPE_NAME}_$ENUM$FIELD
  35. static_assert(SoM(struct $struct, $field) == SoM($type_name, $enum$field),
  36. "$struct.$field size mismatch");
  37. static_assert(offsetof(struct $struct, $field) == offsetof($type_name, $enum$field),
  38. "$struct.$field offset mismatch");
  39. # endif /* HAVE_${TYPE_NAME}_$ENUM$FIELD */
  40. EOF
  41. done
  42. cat <<EOF
  43. static_assert(${struct}_size == expected_${struct}_size,
  44. "${struct}_size mismatch");
  45. EOF
  46. done
  47. cat <<'EOF'
  48. #endif /* HAVE_LINUX_BPF_H */
  49. EOF