From 8b7c2b422eb5a79348ab56c8e8ce80a9ab1b62b9 Mon Sep 17 00:00:00 2001 From: Petr Machata Date: Thu, 30 Jan 2014 19:37:26 -0500 Subject: [PATCH] Rewrite system_calls.exp - Port this to new style test case - Be more clever about what syscalls were invoked from main, as opposed to the multitude of calls that the dynamic linker typically makes - Allow some basic calls to come in XXXat variants (e.g. openat instead of open), such is the case on aarch64. --- testsuite/ltrace.main/system_calls.c | 68 ------------ testsuite/ltrace.main/system_calls.exp | 191 +++++++++++++++++++++++---------- 2 files changed, 135 insertions(+), 124 deletions(-) delete mode 100644 testsuite/ltrace.main/system_calls.c diff --git a/testsuite/ltrace.main/system_calls.c b/testsuite/ltrace.main/system_calls.c deleted file mode 100644 index 7be3d04..0000000 --- a/testsuite/ltrace.main/system_calls.c +++ /dev/null @@ -1,68 +0,0 @@ -/* Ltrace Test : system_calls.c. - Objectives : Verify that Ltrace can trace all the system calls in - execution. - - You can add new system calls in it and add its verification in - system_calls correspondingly. - - This file was written by Yao Qi . */ - -#include -#include -#include -#include -#include - -void exit (int); - -#define BUF_SIZE 100 - -int -main () -{ - FILE* fp; - char s[]="system_calls"; - char buffer[BUF_SIZE]; - struct stat state; - - /* SYS_open. */ - fp = fopen ("system_calls.tmp", "w"); - if (fp == NULL) - { - printf("Can not create system_calls.tmp\n"); - exit (0); - } - /* SYS_write. */ - fwrite(s, sizeof(s), 1, fp); - /* SYS_lseek. */ - fseek (fp, 0, SEEK_CUR); - /* SYS_read. */ - fread(buffer, sizeof(s), 1, fp); - /* SYS_close. */ - fclose(fp); - - /* SYS_getcwd. */ - getcwd (buffer, BUF_SIZE); - /* SYS_chdir. */ - chdir ("."); - /* SYS_symlink. */ - symlink ("system_calls.tmp", "system_calls.link"); - /* SYS_unlink. */ - remove("system_calls.link"); - /* SYS_rename. */ - rename ("system_calls.tmp", "system_calls.tmp1"); - /* SYS_stat. */ - stat ("system_calls.tmp", &state); - /* SYS_access. */ - access ("system_calls.tmp", R_OK); - remove("system_calls.tmp1"); - - /* SYS_mkdir. */ - mkdir ("system_call_mkdir", 0777); - /* SYS_rmdir. */ - rmdir ("system_call_mkdir"); - - return 0; -} - - diff --git a/testsuite/ltrace.main/system_calls.exp b/testsuite/ltrace.main/system_calls.exp index a74fa04..f60e319 100644 --- a/testsuite/ltrace.main/system_calls.exp +++ b/testsuite/ltrace.main/system_calls.exp @@ -1,67 +1,146 @@ -# This file was written by Yao Qi . +# This file is part of ltrace. +# Copyright (C) 2014 Petr Machata, Red Hat Inc. +# Copyright (C) 2006 Yao Qi , IBM Corporation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA -set testfile "system_calls" -set srcfile ${testfile}.c -set binfile ${testfile} +# Objectives: Verify that Ltrace can trace all the system calls in +# execution. Note that this test is necessarily noisy. Dynamic +# linker adds a bunch of system calls of its own. +set empty [ltraceCompile {} [ltraceSource c { + int main (void) { return 0; } +}]] -verbose "compiling source file now....." -# Build the shared libraries this test case needs. -if { [ ltrace_compile "${srcdir}/${subdir}/${testfile}.c" "${objdir}/${subdir}/${binfile}" executable {debug} ] != "" } { - send_user "Testcase compile failed, so all tests in this file will automatically fail.\n" +set bin [ltraceCompile {} [ltraceSource c { + #include + #include + #include + #include + #include + #include + + int + main () + { + FILE* fp; + char s[]="system_calls"; + char buffer[1024]; + struct stat state; + + fp = fopen ("system_calls.tmp", "w"); + if (fp == NULL) + { + printf("Can not create system_calls.tmp\n"); + exit (0); + } + fwrite(s, sizeof(s), 1, fp); + fseek (fp, 0, SEEK_CUR); + fread(buffer, sizeof(s), 1, fp); + fclose(fp); + + getcwd (buffer, sizeof buffer); + chdir ("."); + symlink ("system_calls.tmp", "system_calls.link"); + remove("system_calls.link"); + rename ("system_calls.tmp", "system_calls.tmp1"); + stat ("system_calls.tmp", &state); + access ("system_calls.tmp", R_OK); + remove("system_calls.tmp1"); + + mkdir ("system_call_mkdir", 0777); + rmdir ("system_call_mkdir"); + + return 0; + } +}]] + +proc Calls {logfile} { + set fp [open $logfile] + set ret {} + + while {[gets $fp line] >= 0} { + if [regexp -- {^[a-zA-Z0-9]*@SYS} $line] { + set call [lindex [split $line @] 0] + dict incr ret $call + } + } + + close $fp + return $ret +} + +proc GetDefault {d key def} { + if {[dict exists $d $key]} { + return [dict get $d $key] + } else { + return $def + } } -# set options for ltrace. -ltrace_options "-S" +proc Diff {d1 d2} { + set keys [lsort -unique [concat [dict keys $d1] [dict keys $d2]]] + set ret {} + foreach key $keys { + set n1 [GetDefault $d1 $key 0] + set n2 [GetDefault $d2 $key 0] + set sum [expr $n1 - $n2] + if {[expr $sum != 0]} { + dict set ret $key $sum + } + } + return $ret +} + +proc Match {d patterns} { + foreach line $patterns { + set pattern [lindex $line 0] + set op [lindex $line 1] + set expect [lindex $line 2] -#Run PUT for ltarce. -set exec_output [ltrace_runtest $objdir/$subdir $objdir/$subdir/$binfile] + set count 0 + foreach key [dict keys $d] { + if [regexp -- $pattern $key] { + incr count [dict get $d $key] + } + } -#check the output of this program. -verbose "ltrace runtest output: $exec_output\n" + set msgMain "$pattern was recorded $count times" -if [regexp {ELF from incompatible architecture} $exec_output] { - fail "32-bit ltrace can not perform on 64-bit PUTs and rebuild ltrace in 64 bit mode!" - return -} elseif [ regexp {Couldn't get .hash data} $exec_output ] { - fail "Couldn't get .hash data!" - return + if {[eval expr $count $op $expect]} { + pass $msgMain + } else { + fail "$msgMain, expected $op $expect" + } + } } +Match [Diff [Calls [ltraceRun -L -S -- $bin]] \ + [Calls [ltraceRun -L -S -- $empty]]] { + { {^write$} == 1 } + { {^unlink(at)?$} >= 2 } + { {^open(at)?$} == 1 } + { {^(new|f)?stat(64)?$} == 1 } + { {^close$} == 1 } + { {^getcwd$} == 1 } + { {^chdir$} == 1 } + { {^symlink(at)?$} == 1 } + { {^f?access(at)?$} == 1 } + { {^rename(at)?$} == 1 } + { {^mkdir(at)?$} == 1 } +} -set pattern "^munmap@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 2 -set pattern "^write@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^unlink@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 - -set pattern "^brk@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^open@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^(new)?fstat(64)?@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 egrep -set pattern "^(old_)?mmap2?@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 egrep -set pattern "^close@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 - -set pattern "^getcwd@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^chdir@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^symlink@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^unlink@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^(new)?stat(64)?@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 egrep -set pattern "^access@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^rename@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^mkdir@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 -set pattern "^rmdir@SYS" -ltrace_verify_output ${objdir}/${subdir}/${testfile}.ltrace $pattern 1 +ltraceDone -- 2.7.4