check_sys.sh 10 KB

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