check_sys.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/usr/bin/env bash
  2. # Script to test fpc to system syscall numbers
  3. # Location of syscall header in system
  4. syscall_header=/usr/include/syscall.h
  5. fpc_sysnr=./sysnr.inc
  6. i=0
  7. for arg in $* ; do
  8. let i++
  9. echo "Handling arg $i, \"$arg\""
  10. if [ "${arg//=}" != "$arg" ] ; then
  11. echo "Evaluating $arg"
  12. eval $arg
  13. elif [ "$arg" == "-v" ] ; then
  14. verbose=1
  15. else
  16. echo "arg not handled!"
  17. fi
  18. done
  19. start_pwd=`pwd`
  20. start_dir=`basename $start_pwd`
  21. if [ -d "rtl" ] ; then
  22. echo "Entering rtl directory"
  23. cd rtl
  24. fi
  25. os=`uname -s | tr [:upper:] [:lower:] `
  26. now_pwd=`pwd`
  27. now_dir=`basename $now_pwd`
  28. if [ -d "$os" ] ; then
  29. echo "Entering $os directory"
  30. cd $os
  31. fi
  32. if [ "$os" == "openbsd" ] ; then
  33. c_syscall_header=sys/syscall.h
  34. else
  35. c_syscall_header=syscall.h
  36. fi
  37. if [ -z "$FPC" ] ; then
  38. FPC=fpc
  39. fi
  40. if [ ! -f $fpc_sysnr ] ; then
  41. cpu=`$FPC -iTP`
  42. if [ "${cpu//sparc/}" != "$cpu" ] ; then
  43. cpu=sparcgen
  44. fi
  45. fpc_sysnr=./$cpu/sysnr.inc
  46. if [ ! -f "$fpc_sysnr" ] ; then
  47. fpc_sysnr=`ls -1 ./${cpu}*/sysnr.inc| head -1`
  48. fi
  49. if [ ! -f "$fpc_sysnr" ] ; then
  50. if [ "${cpu//sparc/}" != "$cpu" ] ; then
  51. cpu=sparcgen
  52. fi
  53. fpc_sysnr=./$cpu/sysnr.inc
  54. if [ ! -f "$fpc_sysnr" ] ; then
  55. echo "sysnr.inc file not found, try again in rtl/$os directory"
  56. exit
  57. fi
  58. fi
  59. fi
  60. if [ -f "$fpc_sysnr" ] ; then
  61. echo "Checking $fpc_sysnr content for Free Pascal syscall numbers"
  62. fpc_sysnr_dir=`dirname $fpc_sysnr `
  63. sysnr_includes=`grep -o '{\$i *[a-z_A-Z0-9/.]*' $fpc_sysnr | sed 's:.*{\$i *:'$fpc_sysnr_dir/: `
  64. if [ -n "$sysnr_includes" ] ; then
  65. echo "Found $sysnr_includes include files"
  66. fpc_sysnr="$fpc_sysnr $sysnr_includes"
  67. fi
  68. fi
  69. if [ -z "$verbose" ] ; then
  70. verbose=0
  71. fi
  72. os=`uname -s`
  73. # Test C file to grab all loaded headers
  74. cat > test-syscall.c <<EOF
  75. #include <${c_syscall_header}>
  76. int
  77. main ()
  78. {
  79. return 0;
  80. }
  81. EOF
  82. # Default C compiler is gcc
  83. # Can be overwritten by setting CC variable
  84. # But I don't know if other compilers also generate
  85. # .i files with --save-temps option
  86. if [ -z "$CC" ] ; then
  87. CC=gcc
  88. fi
  89. cpu=`$FPC -iTP`
  90. is_16=0
  91. is_32=0
  92. is_64=0
  93. case $cpu in
  94. aarch64) is_64=1;;
  95. alpha) is_32=1;;
  96. arm) is_32=1;;
  97. avr) is_16=1;;
  98. i386) is_32=1;;
  99. i8086) is_16=1;;
  100. ia64) is_64=1;;
  101. jvm) is_32=1;;
  102. m68k) is_32=1;;
  103. mips) is_32=1;;
  104. mipsel) is_32=1;;
  105. powerpc) is_32=1;;
  106. powerpc64) is_64=1;;
  107. riscv32) is_32=1;;
  108. riscv64) is_64=1;;
  109. sparc) is_32=1;;
  110. sparc64) is_64=1;;
  111. vis) is_32=1;;
  112. x86_64) is_64=1;;
  113. esac
  114. if [ $is_64 -eq 1 ] ; then
  115. CC_OPT="$CC_OPT -m64"
  116. fi
  117. if [ $is_32 -eq 1 ] ;then
  118. CC_OPT="$CC_OPT -m32"
  119. fi
  120. # Use gcc with --save-temps option to create .i file
  121. echo "Calling $CC $CC_OPT --save-temps -o test-syscall ./test-syscall.c"
  122. $CC $CC_OPT --save-temps -o ./test-syscall ./test-syscall.c
  123. res=$?
  124. if [ $res -ne 0 ] ; then
  125. echo "Call to $CC failed"
  126. exit
  127. else
  128. rm -f ./test-syscall.c ./test-syscall
  129. fi
  130. # list of errno.h headers listed
  131. syscall_headers=` sed -n "s:.*\"\(.*/.*\.h\)\".*:\1:p" test-syscall.i |sort | uniq`
  132. rm -f test-syscall.*
  133. echo "C syscall headers files found are \"$syscall_headers\""
  134. if [ -n "$syscall_headers" ] ; then
  135. syscall_header="$syscall_headers"
  136. fi
  137. fpc_syscall_prefix=syscall_nr_
  138. if [ "$os" == "Linux" ] ; then
  139. # On Linux system, system call number are defined indirectly
  140. # with #define SYS_XXX __NR_XXX
  141. # We look directly for the __NT_ version
  142. syscall_prefix=__NR_
  143. else
  144. syscall_prefix=SYS_
  145. fi
  146. # You should only need to change the variables above
  147. cat > parse.awk <<EOF
  148. BEGIN {IGNORECASE = 1;
  149. enable=1;
  150. macro="";
  151. incfile="";
  152. cpu= "cpu" proc;
  153. }
  154. /\{\\\$i / { incfile=\$2;
  155. print "Include file " incfile " found"; }
  156. /\{\\\$ifdef / { macro=\$2; print "ifdef " macro " found\n";
  157. if ( macro == cpu ) { enable=1; print "// Macro " macro " found at line " FNR; } else {enable=0;};
  158. }
  159. /\{\\\$ifndef / { macro=\$2; print "ifndef " macro " found\n";
  160. if ( macro == cpu ) { enable=0; print "// Macro " macro " found at line " FNR; } else {enable=1;};
  161. }
  162. /\{\\\$else/ { if (enable == 1) {enable=0;} else {enable = 1;}}
  163. /\{\\\$endif/ {enable=1;}
  164. /.*/ { if (enable == 1) { print \$0;} }
  165. EOF
  166. if [ -z "$AWK" ] ; then
  167. AWK=`which gawk`
  168. fi
  169. if [ -z "$AWK" ] ; then
  170. AWK=`which awk`
  171. fi
  172. $AWK -v proc=$cpu -f parse.awk ${fpc_sysnr} | sed -n "s:^[ \t]*${fpc_syscall_prefix}\\([_a-zA-Z0-9]*\\)[ \t]*=[ \t]*\\([0-9]*\\)\\(.*\\)$:check_c_syscall_number_from_fpc_rtl \1 \2 \"\3\":p" > check_sys_list.sh
  173. sed -n "s:^.*#[[:space:]]*define[[:space:]]*${syscall_prefix}\\([_a-zA-Z0-9]*\\)[[:space:]]*\\([0-9]*\\)\\(.*\\)$:check_c_syscall_number_in_fpc_rtl \1 \2 \"\3\":p" ${syscall_header} > check_sys_list_reverse.sh
  174. forward_count=0
  175. forward_ok_count=0
  176. forward_failure_count=0
  177. function check_c_syscall_number_from_fpc_rtl ()
  178. {
  179. bare_sys=$1
  180. sys=${syscall_prefix}$bare_sys
  181. value=$2
  182. comment="$3"
  183. if [[ ! ( ( -n "$value" ) && ( $value -ge 0 ) ) ]] ; then
  184. echo "Computing $2 value"
  185. let value=$2
  186. fi
  187. obsolete=0
  188. let forward_count++
  189. if [[ "$value" =~ ^[0-9]+$ ]] ; then
  190. eval $sys=\$$value
  191. if [ $verbose -ne 0 ] ; then
  192. echo "$sys is $value"
  193. fi
  194. else
  195. eval $sys=$value
  196. if [ $verbose -ne 0 ] ; then
  197. echo "$sys set to \"${$sys}\" trough \"$value\""
  198. fi
  199. fi
  200. # Remember this value for later
  201. eval $sys=$value
  202. if [ $verbose -ne 0 ] ; then
  203. echo Testing $sys value $value
  204. fi
  205. let froward_count++
  206. found=`sed -n "/#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_]/p" ${syscall_header}`
  207. val=`sed -n "s:#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${syscall_header}`
  208. # Test C file to grab all loaded headers
  209. cat > test-syscall-${bare_sys}.c <<EOF
  210. #include <${c_syscall_header}>
  211. #include <stdio.h>
  212. int
  213. main ()
  214. {
  215. printf ("%d\n", $sys);
  216. return 0;
  217. }
  218. EOF
  219. $CC $CC_OPT -o ./test_$bare_sys test-syscall-${bare_sys}.c
  220. C_COMP_RES=$?
  221. if [ $C_COMP_RES -eq 0 ] ; then
  222. CC_value=`./test_$bare_sys`
  223. if [ "$value" != "$CC_value" ] ; then
  224. echo "$CC returns $CC_value, while $value is expected"
  225. let forward_failure_count++
  226. return
  227. else
  228. rm -f ./test_$bare_sys
  229. fi
  230. rm -f ./test-syscall-${bare_sys}.c
  231. else
  232. echo "$CC failed to compile code containing $sys syscall number $value"
  233. let forward_failure_count++
  234. return
  235. fi
  236. if [ $verbose -ne 0 ] ; then
  237. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  238. fi
  239. if [ "${val}" == "${value}" ] ; then
  240. if [ $verbose -ne 0 ] ; then
  241. echo ${sys} value ${val} is correct
  242. fi
  243. let forward_ok_count++
  244. else
  245. if [ -z "${val}" ] ; then
  246. found=`sed -n ".*define[[:space:]].*[^A-Za-z0-9_][[:space:]]${value}$/p" ${syscall_header}`
  247. if [ -z "${found}" ] ; then
  248. found=`sed -n "s:\/\* ${value} is compa:/* ${value} is compa:p" ${syscall_header}`
  249. if [ -n "$found" ] ; then
  250. obsolete=1
  251. fi
  252. fi
  253. fi
  254. if [ -z "$found" ] ; then
  255. found=`grep -n -w $value ${syscall_header}`
  256. fi
  257. if [ $obsolete -eq 1 ] ; then
  258. echo Warning: ${bare_sys} expected ${value}, is obsolete line is \"${found}\"
  259. else
  260. echo Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
  261. fi
  262. let forward_failure_count++
  263. fi
  264. }
  265. reverse_count=0
  266. reverse_ok_count=0
  267. reverse_failure_count=0
  268. add_file=./add_missing_syscalls.inc
  269. suggested_addition_count=0
  270. echo "{ Generated by check_rtl_sys.sh script }" > $add_file
  271. function check_c_syscall_number_in_fpc_rtl ()
  272. {
  273. bare_sys=$1
  274. sys=${fpc_syscall_prefix}${bare_sys}
  275. value=$2
  276. comment="$3"
  277. if [ $verbose -ne 0 ] ; then
  278. echo "Full comment is \"$comment \""
  279. fi
  280. if [ "${comment/*\/\*/}" != "$comment" ] ; then
  281. comment="${comment/*\/\*/}"
  282. if [ $verbose -ne 0 ] ; then
  283. echo "comment is \"$comment \""
  284. fi
  285. comment="${comment/\*\/*/}"
  286. if [ $verbose -ne 0 ] ; then
  287. echo "comment is \"$comment \""
  288. fi
  289. comment=`echo $comment | sed 's:^[[:space:]]*\(.*\)[[:space:]]*$:\1' `
  290. if [ $verbose -ne 0 ] ; then
  291. echo "comment is \"$comment \""
  292. fi
  293. fi
  294. if [ $verbose -ne 0 ] ; then
  295. echo Testing syscall header entry $sys value $value
  296. fi
  297. let reverse_count++
  298. found=`sed -n "/.*${sys}/p" ${fpc_sysnr}`
  299. val=`sed -n "s:.*${sys}[ \t]*=[ \t]*\([0-9]*\).*:\1:p" ${fpc_sysnr}`
  300. if [ $verbose -ne 0 ] ; then
  301. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  302. fi
  303. if [ "${val}" == "${value}" ] ; then
  304. if [ $verbose -ne 0 ] ; then
  305. echo ${sys} value ${val} is correct
  306. fi
  307. let reverse_ok_count++
  308. else
  309. if [ -z "${val}" ] ; then
  310. found=`sed -n "/#[[:space:]]*define.*[^A-Za-z0-9_][[:space:]]*${value}([[:space:]]|$)/p" ${syscall_header}`
  311. if [ -z "${found}" ] ; then
  312. found=`sed -n "s:\/\*.*i[[:space:]]${value} is compa: ${value} is compa:p" ${syscall_header}`
  313. fi
  314. fi
  315. echo "Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\""
  316. if [ -n "$comment" ] ; then
  317. echo " ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }" >> $add_file
  318. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }"
  319. else
  320. echo " ${fpc_syscall_prefix}${bare_sys} = ${value};" >> $add_file
  321. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value};"
  322. fi
  323. let suggested_addition_count++
  324. let reverse_failure_count++
  325. fi
  326. }
  327. # Sustitution made to pass from fpc syscall number
  328. # to system define
  329. set -f
  330. echo "Checking values from \"${fpc_sysnr}\" in C syscall headers"
  331. source ./check_sys_list.sh
  332. echo "Checking if values in C syscall headers are in \"${fpc_sysnr}\""
  333. source ./check_sys_list_reverse.sh
  334. echo "Forward counts: OK=$forward_ok_count, failures=$forward_failure_count, total=$forward_count"
  335. echo "Reverse counts: OK=$reverse_ok_count, failures=$reverse_failure_count, total=$reverse_count"
  336. if [ $suggested_addition_count -gt 0 ] ; then
  337. echo "Missing $suggested_addition_count syscall numbers in $add_file"
  338. else
  339. rm $add_file
  340. fi
  341. rm ./check_sys_list.sh ./check_sys_list_reverse.sh ./parse.awk