summaryrefslogtreecommitdiff
path: root/rtl/unix/scripts/check_sys.sh
blob: 1a748b28129541296809e4835a61140d7daf4f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env bash

# Script to test fpc to system syscall numbers

# Location of syscall header in system

syscall_header=/usr/include/syscall.h
fpc_sysnr=./sysnr.inc

i=0
for arg in $* ; do
  let i++
  echo "Handling arg $i, \"$arg\""
  if [ "${arg//=}" != "$arg" ] ; then
    echo "Evaluating $arg"
    eval $arg
  elif [ "$arg" == "-v" ] ; then
    verbose=1
  else
    echo "arg not handled!"
  fi
done

start_pwd=`pwd`
start_dir=`basename $start_pwd`

if [ -d "rtl" ] ; then
  echo "Entering rtl directory"
  cd rtl
fi

os=`uname -s | tr [:upper:] [:lower:] `
now_pwd=`pwd`
now_dir=`basename $now_pwd`
if [ -d "$os" ] ; then
  echo "Entering $os directory"
  cd $os
fi

case "$os" in
  freebsd|openbsd|netbsd) c_syscall_header=sys/syscall.h;;
  *)  c_syscall_header=syscall.h;;
esac

if [ -z "$FPC" ] ; then
  FPC=fpc
fi

if [ ! -f $fpc_sysnr ] ; then
  cpu=`$FPC -iTP`
  if [ "${cpu//sparc/}" != "$cpu" ] ; then
    cpu=sparcgen
  fi
  fpc_sysnr=./$cpu/sysnr.inc
  if [ ! -f "$fpc_sysnr" ] ; then
    fpc_sysnr=`ls -1 ./${cpu}*/sysnr.inc| head -1`
  fi
  if [ ! -f "$fpc_sysnr" ] ; then
    if [ "${cpu//sparc/}" != "$cpu" ] ; then
      cpu=sparcgen
    fi
    fpc_sysnr=./$cpu/sysnr.inc
    if [ ! -f "$fpc_sysnr" ] ; then
      echo "sysnr.inc file not found, try again in rtl/$os directory"
      exit
    fi
  fi
fi

if [ -f "$fpc_sysnr" ] ; then
  echo "Checking $fpc_sysnr content for Free Pascal syscall numbers"
  fpc_sysnr_dir=`dirname $fpc_sysnr `
  sysnr_includes=`grep -o '{\$i  *[a-z_A-Z0-9/.]*' $fpc_sysnr | sed 's:.*{\$i *:'$fpc_sysnr_dir/: `
  if [ -n "$sysnr_includes" ] ; then
    echo "Found $sysnr_includes include files"
    fpc_sysnr="$fpc_sysnr $sysnr_includes"
  fi
fi

if [ -z "$verbose" ] ; then
  verbose=0
fi

os=`uname -s`

# Test C file to grab all loaded headers
cat > test-syscall.c <<EOF
#include <${c_syscall_header}>

int
main ()
{
  return 0;
}
EOF

# Default C compiler is gcc
# Can be overwritten by setting CC variable
# But I don't know if other compilers also generate
# .i files with --save-temps option
if [ -z "$CC" ] ; then
  CC=gcc
fi

cpu=`$FPC -iTP`
is_16=0
is_32=0
is_64=0
case $cpu in
  aarch64)   is_64=1;;
  alpha)     is_32=1;;
  arm)       is_32=1;;
  avr)       is_16=1;;
  i386)      is_32=1;;
  i8086)     is_16=1;;
  ia64)      is_64=1;;
  jvm)       is_32=1;;
  m68k)      is_32=1;;
  mips)      is_32=1;;
  mipsel)    is_32=1;;
  powerpc)   is_32=1;;
  powerpc64) is_64=1;;
  riscv32)   is_32=1;;
  riscv64)   is_64=1;;
  sparc)     is_32=1;;
  sparc64)   is_64=1;;
  vis)       is_32=1;;
  x86_64)    is_64=1;;
esac

if [ $is_64 -eq 1 ] ; then
  CC_OPT="$CC_OPT -m64"
  CPUBITS=64
elif [ $is_32 -eq 1 ] ;then
  CC_OPT="$CC_OPT -m32"
  CPUBITS=32
elif [ $is_16 -eq 1 ] ; then
  CPUBITS=16
else
  CPUBITS=unknown
fi

# Use gcc with --save-temps option to create .i file
echo "Calling $CC $CC_OPT --save-temps -o test-syscall ./test-syscall.c"
$CC $CC_OPT --save-temps -o ./test-syscall ./test-syscall.c
res=$?
if [ $res -ne 0 ] ; then
  echo "Call to $CC failed"
  exit
else
  rm -f ./test-syscall.c ./test-syscall
fi
# list of errno.h headers listed
syscall_headers=` sed -n "s:.*\"\(.*/.*\.h\)\".*:\1:p" test-syscall.i |sort | uniq`
rm -f test-syscall.*
echo "C syscall headers files found are \"$syscall_headers\""

if [ -n "$syscall_headers" ] ; then
  syscall_header="$syscall_headers"
fi

fpc_syscall_prefix=syscall_nr_
if [ "$os" == "Linux" ] ; then
  # On Linux system, system call number are defined indirectly
  # with #define SYS_XXX __NR_XXX
  # We look directly for the __NT_ version
  syscall_prefix=__NR_
else
  syscall_prefix=SYS_
fi

# You should only need to change the variables above

awkfile=preproc.awk
tmp_fpc_sysnr=tmp-sysnr-${cpu}.inc

c_syscall_source=test-syscall-${cpu}.c

# Test C file to grab all loaded headers
# must be called with -DSYS_MACRO=$sys
cat > $c_syscall_source <<EOF
#include <${c_syscall_header}>
#include <stdio.h>

int
main ()
{
  printf ("%d\n", (int) SYS_MACRO);
  return 0;
}
EOF

cat > $awkfile <<EOF
BEGIN {IGNORECASE = 1;
enable=1;
macro="";
incfile="";
cpu= "cpu" proc;
cpubits= "cpu" cpubits;
}
/\{\\\$i / { incfile=\$2;
  print "Include file  " incfile " found"; }
/\{\\\$ifdef / { macro=gensub("[^A-Za-z_0-9].*","","",\$2);
  if ( (macro == cpu) || (macro == cpubits)) { enable=1;
    print "// ifdef " macro " found and accepted at line " FNR;
  } else {enable=0;
    print "// ifdef " macro " found and rejected at line " FNR;
  };
  }
/\{\\\$ifndef / { macro=gensub("[^A-Za-z_0-9].*","","",\$2);
  if ( (macro == cpu) || (macro == cpubits) ) { enable=0;
   print "// ifndef " macro " found and rejected at line " FNR;
 } else {enable=1;
   print "// ifndef " macro " found and accepted at line " FNR;
 };
  }
/\{\\\$else/ { if (enable == 1) {enable=0;} else {enable = 1;}}
/.*/ { if (enable == 1) {
  wholeline=\$0;
  code=gensub("{.*}","","g",\$0);
  code=gensub("[(][*].*[*][)]","","g",code);
  comments=gensub(code,"","",\$0);
  comments1=gensub(".*({.*}).*","\1","g",comments);
  if (comments == comments1)
    comments1="";
  comments2=gensub(".*[(][*].*[*][)]).*","\1","g",comments);
  if (comments == comments2)
    comments2="";
  comments3=gensub(".*//","","",comments);
  if (comments == comments3)
    comments3="";
  all_comments= comments1 comments2 comments3;
  if (all_comments != "")
    print code "// " comments1 comments2 comments3 ;
  else
    print code;
  } 
}
/\{\\\$endif/ {enable=1;}
EOF

if [ -z "$AWK" ] ; then
  AWK=`which gawk 2> /dev/null`
fi

if [ -z "$AWK" ] ; then
  AWK=`which awk 2> /dev/null`
fi

if [ -n "$AWK" ] ; then
  echo "Preprocessing ${fpc_sysnr} to $tmp_fpc_sysnr"
  echo "$AWK -v proc=$cpu -v cpubits=$CPUBITS -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr"
  $AWK -v proc=$cpu -v cpubits=$CPUBITS -f $awkfile ${fpc_sysnr} > $tmp_fpc_sysnr
  fpc_sysnr=$tmp_fpc_sysnr
fi
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


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
 
forward_count=0
forward_ok_count=0
forward_failure_count=0

function check_c_syscall_number_from_fpc_rtl ()
{
  bare_sys=$1
  sys=${syscall_prefix}$bare_sys
  value=$2
  comment="$3"
  if [[ ! ( ( -n "$value" ) && ( $value -ge 0 ) ) ]] ; then
    echo "Computing $2 value"
    let value=$2
  fi
  obsolete=0
  let forward_count++
  if [[ "$value" =~ ^[0-9]+$ ]] ; then
    eval $sys=\$$value
    if [ $verbose -ne 0 ] ; then
      echo "$sys is $value"
    fi
  else
    eval $sys=$value
    if [ $verbose -ne 0 ] ; then
      echo "$sys set to \"${$sys}\" trough \"$value\""
    fi
  fi
  # Remember this value for later
  eval $sys=$value
  echo -en "Testing $sys value $value                    \r"
  found=`sed -n "/#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_]/p" ${syscall_header}`
  val=`sed -n "s:#[[:space:]]*define[[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${syscall_header}`
  $CC $CC_OPT -DSYS_MACRO=${syscall_prefix}${bare_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  C_COMP_RES=$?
  if [ $C_COMP_RES -eq 0 ] ; then
    CC_value=`./test_c_${bare_sys} `
    if [ "$value" != "$CC_value" ] ; then
      echo "$CC returns $CC_value, while $value is expected"
      let forward_failure_count++
      return
    else
      rm -f ./test_c_${bare_sys}
    fi
    rm -f ./test-${bare_sys}.comp-log
  else
    echo "$CC failed to compile code containing $sys syscall number $value"
    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"
    let forward_failure_count++
    return
  fi

  if [ $verbose -ne 0 ] ; then
    echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  fi
  if [ "${val}" == "${value}" ] ; then
    if [ $verbose -ne 0 ] ; then
      echo ${sys} value ${val} is correct
    fi
    let forward_ok_count++
  else
    if [ -z "${val}" ] ; then
      found=`sed -n ".*define[[:space:]].*[^A-Za-z0-9_][[:space:]]${value}$/p" ${syscall_header}`
      if [ -z "${found}" ] ; then
        found=`sed -n "s:\/\* ${value} is compa:/* ${value} is compa:p" ${syscall_header}`
        if [ -n "$found" ] ; then
          obsolete=1
        fi
      fi
    fi
    if [ -z "$found" ] ; then
      found=`grep -n -w $value ${syscall_header}`
    fi
    if [ $obsolete -eq 1 ] ; then
      echo Warning: ${bare_sys} expected ${value}, is obsolete line is \"${found}\"
    else
      echo Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
    fi
    let forward_failure_count++
  fi
}

reverse_count=0
reverse_ok_count=0
reverse_failure_count=0
add_file=./add_missing_syscalls.inc
suggested_addition_count=0
echo "{ Generated by check_rtl_sys.sh script }" > $add_file

function check_c_syscall_number_in_fpc_rtl ()
{
  bare_sys=$1
  sys=${fpc_syscall_prefix}${bare_sys}
  c_sys=${syscall_prefix}${bare_sys}
  value=$2
  comment="$3"
  echo -en "Testing $sys value $value                        \r"
  $CC $CC_OPT -DSYS_MACRO=${c_sys} -o ./test_c_${bare_sys} $c_syscall_source > ./test_${bare_sys}.comp-log 2>&1
  C_COMP_RES=$?
  if [ $C_COMP_RES -eq 0 ] ; then
    rm ./test_${bare_sys}.comp-log
    CC_value=`./test_c_${bare_sys} `
    if [ "$value" != "$CC_value" ] ; then
      echo "For sys=$sys, $CC returns $CC_value, while $value is expected"
      let reverse_failure_count++
      return
    else
      rm -f ./test_c_$bare_sys
    fi
  else
    # if C syscall is not accepted do nothing
    #echo "For sys=$sys, $CC compilation failed"
    #cat ./test_${bare_sys}.comp-log
    # let reverse_failure_count++
    rm -f ./test_c_${bare_sys}
    rm ./test_${bare_sys}.comp-log
    return
  fi

  if [ $verbose -ne 0 ] ; then
    echo "Full comment is \"$comment \""
  fi
  if [ "${comment/*\/\*/}" != "$comment" ] ; then
    comment="${comment/*\/\*/}"
    if [ $verbose -ne 0 ] ; then
      echo "comment is \"$comment \""
    fi
    comment="${comment/\*\/*/}"
    if [ $verbose -ne 0 ] ; then
      echo "comment is \"$comment \""
    fi
    comment=`echo $comment | sed 's:^[[:space:]]*\(.*\)[[:space:]]*$:\1' `
    if [ $verbose -ne 0 ] ; then
      echo "comment is \"$comment \""
    fi
  fi
  if [ $verbose -ne 0 ] ; then
    echo Testing syscall header entry $sys value $value
  fi
  let reverse_count++
  found=`sed -n "/.*${sys}/p" ${fpc_sysnr}`
  val=`sed -n "s:.*${sys}[ \t]*=[ \t]*\([0-9]*\).*:\1:p" ${fpc_sysnr}`
  if [ $verbose -ne 0 ] ; then
    echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  fi
  if [ "${val}" == "${value}" ] ; then
    if [ $verbose -ne 0 ] ; then
      echo ${sys} value ${val} is correct
    fi
    let reverse_ok_count++
  else
    if [ -z "${val}" ] ; then
      found=`sed -n "/#[[:space:]]*define.*[^A-Za-z0-9_][[:space:]]*${value}([[:space:]]|$)/p" ${syscall_header}`
      if [ -z "${found}" ] ; then
        found=`sed -n "s:\/\*.*i[[:space:]]${value} is compa: ${value} is compa:p" ${syscall_header}`
      fi
    fi
    echo "Problem: ${bare_sys} expected ${value}, line is \"${found}\", val found is \"${val}\""
    if [ -n "$comment" ] ; then
      echo "    ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }" >> $add_file
      echo "Suggest adding:  ${fpc_syscall_prefix}${bare_sys} = ${value}; { $comment }"
    else
      echo "    ${fpc_syscall_prefix}${bare_sys} = ${value};" >> $add_file
      echo "Suggest adding:  ${fpc_syscall_prefix}${bare_sys} = ${value};"
    fi
    let suggested_addition_count++
    let reverse_failure_count++
  fi
}


# Sustitution made to pass from fpc syscall number
# to system define 
set -f

echo "Checking values from \"${fpc_sysnr}\" in C syscall headers"
source ./check_sys_list.sh
echo "Checking if values in C syscall headers are in \"${fpc_sysnr}\""
source ./check_sys_list_reverse.sh
echo "Forward counts: OK=$forward_ok_count, failures=$forward_failure_count, total=$forward_count"
echo "Reverse counts: OK=$reverse_ok_count, failures=$reverse_failure_count, total=$reverse_count"
if [ $suggested_addition_count -gt 0 ] ; then
  echo "Missing $suggested_addition_count syscall numbers in $add_file"
else
 rm $add_file
fi
rm ./check_sys_list.sh ./check_sys_list_reverse.sh ./$awkfile
if [ -f "$tmp_fpc_sysnr" ] ; then
  echo   rm $tmp_fpc_sysnr
fi