check_sys.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. CPUBITS=64
  116. elif [ $is_32 -eq 1 ] ;then
  117. CC_OPT="$CC_OPT -m32"
  118. CPUBITS=32
  119. elif [ $is_16 -eq 1 ] ; then
  120. CPUBITS=16
  121. else
  122. CPUBITS=unknown
  123. fi
  124. # Use gcc with --save-temps option to create .i file
  125. echo "Calling $CC $CC_OPT --save-temps -o test-syscall ./test-syscall.c"
  126. $CC $CC_OPT --save-temps -o ./test-syscall ./test-syscall.c
  127. res=$?
  128. if [ $res -ne 0 ] ; then
  129. echo "Call to $CC failed"
  130. exit
  131. else
  132. rm -f ./test-syscall.c ./test-syscall
  133. fi
  134. # list of errno.h headers listed
  135. syscall_headers=` sed -n "s:.*\"\(.*/.*\.h\)\".*:\1:p" test-syscall.i |sort | uniq`
  136. rm -f test-syscall.*
  137. echo "C syscall headers files found are \"$syscall_headers\""
  138. if [ -n "$syscall_headers" ] ; then
  139. syscall_header="$syscall_headers"
  140. fi
  141. fpc_syscall_prefix=syscall_nr_
  142. if [ "$os" == "Linux" ] ; then
  143. # On Linux system, system call number are defined indirectly
  144. # with #define SYS_XXX __NR_XXX
  145. # We look directly for the __NT_ version
  146. syscall_prefix=__NR_
  147. else
  148. syscall_prefix=SYS_
  149. fi
  150. # You should only need to change the variables above
  151. awkfile=preproc.awk
  152. tmp_fpc_sysnr=tmp-sysnr-${cpu}.inc
  153. c_syscall_source=test-syscall-${cpu}.c
  154. # Test C file to grab all loaded headers
  155. # must be called with -DSYS_MACRO=$sys
  156. cat > $c_syscall_source <<EOF
  157. #include <${c_syscall_header}>
  158. #include <stdio.h>
  159. int
  160. main ()
  161. {
  162. printf ("%d\n", (int) SYS_MACRO);
  163. return 0;
  164. }
  165. EOF
  166. cat > $awkfile <<EOF
  167. BEGIN {IGNORECASE = 1;
  168. enable=1;
  169. macro="";
  170. incfile="";
  171. cpu= "cpu" proc;
  172. cpubits= "cpu" cpubits;
  173. }
  174. /\{\\\$i / { incfile=\$2;
  175. print "Include file " incfile " found"; }
  176. /\{\\\$ifdef / { macro=gensub("[^A-Za-z_0-9].*","","",\$2);
  177. if ( (macro == cpu) || (macro == cpubits)) { enable=1;
  178. print "// ifdef " macro " found and accepted at line " FNR;
  179. } else {enable=0;
  180. print "// ifdef " macro " found and rejected at line " FNR;
  181. };
  182. }
  183. /\{\\\$ifndef / { macro=gensub("[^A-Za-z_0-9].*","","",\$2);
  184. if ( (macro == cpu) || (macro == cpubits) ) { enable=0;
  185. print "// ifndef " macro " found and rejected at line " FNR;
  186. } else {enable=1;
  187. print "// ifndef " macro " found and accepted at line " FNR;
  188. };
  189. }
  190. /\{\\\$else/ { if (enable == 1) {enable=0;} else {enable = 1;}}
  191. /.*/ { if (enable == 1) {
  192. wholeline=\$0;
  193. code=gensub("{.*}","","g",\$0);
  194. code=gensub("[(][*].*[*][)]","","g",code);
  195. comments=gensub(code,"","",\$0);
  196. comments1=gensub(".*({.*}).*","\1","g",comments);
  197. if (comments == comments1)
  198. comments1="";
  199. comments2=gensub(".*[(][*].*[*][)]).*","\1","g",comments);
  200. if (comments == comments2)
  201. comments2="";
  202. comments3=gensub(".*//","","",comments);
  203. if (comments == comments3)
  204. comments3="";
  205. all_comments= comments1 comments2 comments3;
  206. if (all_comments != "")
  207. print code "// " comments1 comments2 comments3 ;
  208. else
  209. print code;
  210. }
  211. }
  212. /\{\\\$endif/ {enable=1;}
  213. EOF
  214. if [ -z "$AWK" ] ; then
  215. AWK=`which gawk 2> /dev/null`
  216. fi
  217. if [ -z "$AWK" ] ; then
  218. AWK=`which awk 2> /dev/null`
  219. fi
  220. if [ -n "$AWK" ] ; then
  221. echo "Preprocessing ${fpc_sysnr} to $tmp_fpc_sysnr"
  222. echo "$AWK -v proc=$cpu -v cpubits=$CPUBITS -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr"
  223. $AWK -v proc=$cpu -v cpubits=$CPUBITS -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr
  224. fpc_sysnr=$tmp_fpc_sysnr
  225. fi
  226. 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" $fpc_sysnr > check_sys_list.sh
  227. 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
  228. forward_count=0
  229. forward_ok_count=0
  230. forward_failure_count=0
  231. function check_c_syscall_number_from_fpc_rtl ()
  232. {
  233. bare_sys=$1
  234. sys=${syscall_prefix}$bare_sys
  235. value=$2
  236. comment="$3"
  237. if [[ ! ( ( -n "$value" ) && ( $value -ge 0 ) ) ]] ; then
  238. echo "Computing $2 value"
  239. let value=$2
  240. fi
  241. obsolete=0
  242. let forward_count++
  243. if [[ "$value" =~ ^[0-9]+$ ]] ; then
  244. eval $sys=\$$value
  245. if [ $verbose -ne 0 ] ; then
  246. echo "$sys is $value"
  247. fi
  248. else
  249. eval $sys=$value
  250. if [ $verbose -ne 0 ] ; then
  251. echo "$sys set to \"${$sys}\" trough \"$value\""
  252. fi
  253. fi
  254. # Remember this value for later
  255. eval $sys=$value
  256. echo -en "Testing $sys value $value \r"
  257. found=`sed -n "/#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_]/p" ${syscall_header}`
  258. val=`sed -n "s:#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${syscall_header}`
  259. $CC $CC_OPT -DSYS_MACRO=${syscall_prefix}${bare_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  260. C_COMP_RES=$?
  261. if [ $C_COMP_RES -eq 0 ] ; then
  262. CC_value=`./test_c_${bare_sys} `
  263. if [ "$value" != "$CC_value" ] ; then
  264. echo "$CC returns $CC_value, while $value is expected"
  265. let forward_failure_count++
  266. return
  267. else
  268. rm -f ./test_c_${bare_sys}
  269. fi
  270. rm -f ./test-${bare_sys}.comp-log
  271. else
  272. echo "$CC failed to compile code containing $sys syscall number $value"
  273. echo "$CC $CC_OPT -DSYS_MACRO=${syscall_prefix}${bare_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1"
  274. let forward_failure_count++
  275. return
  276. fi
  277. if [ $verbose -ne 0 ] ; then
  278. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  279. fi
  280. if [ "${val}" == "${value}" ] ; then
  281. if [ $verbose -ne 0 ] ; then
  282. echo ${sys} value ${val} is correct
  283. fi
  284. let forward_ok_count++
  285. else
  286. if [ -z "${val}" ] ; then
  287. found=`sed -n ".*define[[:space:]].*[^A-Za-z0-9_][[:space:]]${value}$/p" ${syscall_header}`
  288. if [ -z "${found}" ] ; then
  289. found=`sed -n "s:\/\* ${value} is compa:/* ${value} is compa:p" ${syscall_header}`
  290. if [ -n "$found" ] ; then
  291. obsolete=1
  292. fi
  293. fi
  294. fi
  295. if [ -z "$found" ] ; then
  296. found=`grep -n -w $value ${syscall_header}`
  297. fi
  298. if [ $obsolete -eq 1 ] ; then
  299. echo Warning: ${bare_sys} expected ${value}, is obsolete line is \"${found}\"
  300. else
  301. echo Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
  302. fi
  303. let forward_failure_count++
  304. fi
  305. }
  306. reverse_count=0
  307. reverse_ok_count=0
  308. reverse_failure_count=0
  309. add_file=./add_missing_syscalls.inc
  310. suggested_addition_count=0
  311. echo "{ Generated by check_rtl_sys.sh script }" > $add_file
  312. function check_c_syscall_number_in_fpc_rtl ()
  313. {
  314. bare_sys=$1
  315. sys=${fpc_syscall_prefix}${bare_sys}
  316. c_sys=${syscall_prefix}${bare_sys}
  317. value=$2
  318. comment="$3"
  319. echo -en "Testing $sys value $value \r"
  320. $CC $CC_OPT -DSYS_MACRO=${c_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  321. C_COMP_RES=$?
  322. if [ $C_COMP_RES -eq 0 ] ; then
  323. rm ./test_${bare_sys}.comp-log
  324. CC_value=`./test_c_${bare_sys} `
  325. if [ "$value" != "$CC_value" ] ; then
  326. echo "For sys=$sys, $CC returns $CC_value, while $value is expected"
  327. let reverse_failure_count++
  328. return
  329. else
  330. rm -f ./test_c_$bare_sys
  331. fi
  332. else
  333. # if C syscall is not accepted do nothing
  334. #echo "For sys=$sys, $CC compilation failed"
  335. #cat ./test_${bare_sys}.comp-log
  336. # let reverse_failure_count++
  337. rm -f ./test_c_${bare_sys}
  338. rm ./test_${bare_sys}.comp-log
  339. return
  340. fi
  341. if [ $verbose -ne 0 ] ; then
  342. echo "Full comment is \"$comment \""
  343. fi
  344. if [ "${comment/*\/\*/}" != "$comment" ] ; then
  345. comment="${comment/*\/\*/}"
  346. if [ $verbose -ne 0 ] ; then
  347. echo "comment is \"$comment \""
  348. fi
  349. comment="${comment/\*\/*/}"
  350. if [ $verbose -ne 0 ] ; then
  351. echo "comment is \"$comment \""
  352. fi
  353. comment=`echo $comment | sed 's:^[[:space:]]*\(.*\)[[:space:]]*$:\1' `
  354. if [ $verbose -ne 0 ] ; then
  355. echo "comment is \"$comment \""
  356. fi
  357. fi
  358. if [ $verbose -ne 0 ] ; then
  359. echo Testing syscall header entry $sys value $value
  360. fi
  361. let reverse_count++
  362. found=`sed -n "/.*${sys}/p" ${fpc_sysnr}`
  363. val=`sed -n "s:.*${sys}[ \t]*=[ \t]*\([0-9]*\).*:\1:p" ${fpc_sysnr}`
  364. if [ $verbose -ne 0 ] ; then
  365. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  366. fi
  367. if [ "${val}" == "${value}" ] ; then
  368. if [ $verbose -ne 0 ] ; then
  369. echo ${sys} value ${val} is correct
  370. fi
  371. let reverse_ok_count++
  372. else
  373. if [ -z "${val}" ] ; then
  374. found=`sed -n "/#[[:space:]]*define.*[^A-Za-z0-9_][[:space:]]*${value}([[:space:]]|$)/p" ${syscall_header}`
  375. if [ -z "${found}" ] ; then
  376. found=`sed -n "s:\/\*.*i[[:space:]]${value} is compa: ${value} is compa:p" ${syscall_header}`
  377. fi
  378. fi
  379. echo "Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\""
  380. if [ -n "$comment" ] ; then
  381. echo " ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }" >> $add_file
  382. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }"
  383. else
  384. echo " ${fpc_syscall_prefix}${bare_sys} = ${value};" >> $add_file
  385. echo "Suggest adding: ${fpc_syscall_prefix}${bare_sys} = ${value};"
  386. fi
  387. let suggested_addition_count++
  388. let reverse_failure_count++
  389. fi
  390. }
  391. # Sustitution made to pass from fpc syscall number
  392. # to system define
  393. set -f
  394. echo "Checking values from \"${fpc_sysnr}\" in C syscall headers"
  395. source ./check_sys_list.sh
  396. echo "Checking if values in C syscall headers are in \"${fpc_sysnr}\""
  397. source ./check_sys_list_reverse.sh
  398. echo "Forward counts: OK=$forward_ok_count, failures=$forward_failure_count, total=$forward_count"
  399. echo "Reverse counts: OK=$reverse_ok_count, failures=$reverse_failure_count, total=$reverse_count"
  400. if [ $suggested_addition_count -gt 0 ] ; then
  401. echo "Missing $suggested_addition_count syscall numbers in $add_file"
  402. else
  403. rm $add_file
  404. fi
  405. rm ./check_sys_list.sh ./check_sys_list_reverse.sh ./$awkfile
  406. if [ -f "$tmp_fpc_sysnr" ] ; then
  407. echo rm $tmp_fpc_sysnr
  408. fi