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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/sh -efu
  2. # Copyright (c) 2018 Dmitry V. Levin <ldv@altlinux.org>
  3. # Copyright (c) 2018-2020 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. EOF
  18. for struct in $(sed -n 's/^struct \([^[:space:]]\+_struct\) .*/\1/p' < "$input"); do
  19. case "$struct" in
  20. BPF_*) type_name='union bpf_attr' ;;
  21. *) type_name="struct ${struct%_struct}" ;;
  22. esac
  23. TYPE_NAME="$(printf %s "$type_name" |tr '[:lower:] ' '[:upper:]_')"
  24. enum="$(sed -n 's/^struct '"$struct"' \/\* \([^[:space:]]\+\) \*\/ {.*/\1/p' < "$input")"
  25. ENUM="$(printf %s "$enum" |tr '[:lower:]' '[:upper:]')"
  26. enum="$enum${enum:+.}"
  27. ENUM="$ENUM${ENUM:+_}"
  28. sed -n '/^struct '"$struct"' [^{]*{/,/^};$/p' < "$input" |
  29. sed -n 's/^[[:space:]]\+[^][;:]*[[:space:]]\([^][[:space:];:]\+\)\(\[[^;:]*\]\)\?;$/\1/p' |
  30. while read field; do
  31. FIELD="$(printf %s "$field" |tr '[:lower:]' '[:upper:]')"
  32. cat <<EOF
  33. # ifdef HAVE_${TYPE_NAME}_$ENUM$FIELD
  34. static_assert(sizeof_field(struct $struct, $field) == sizeof_field($type_name, $enum$field),
  35. "$struct.$field size mismatch");
  36. static_assert(offsetof(struct $struct, $field) == offsetof($type_name, $enum$field),
  37. "$struct.$field offset mismatch");
  38. # endif /* HAVE_${TYPE_NAME}_$ENUM$FIELD */
  39. EOF
  40. done
  41. cat <<EOF
  42. static_assert(${struct}_size == expected_${struct}_size,
  43. "${struct}_size mismatch");
  44. EOF
  45. done
  46. cat <<'EOF'
  47. #endif /* HAVE_LINUX_BPF_H */
  48. EOF