summaryrefslogtreecommitdiff
path: root/testsuite/lib/ltrace.exp
blob: b56d50d029d6591e975fbcc49bdb01c030d9a8f8 (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
# This file was written by Yao Qi. (qiyao@cn.ibm.com)

# Generic ltrace test subroutines that should work for any target.  If these
# need to be modified for any target, it can be done with a variable
# or by passing arguments.


global LTRACE
if [info exists TOOL_EXECUTABLE] {
	set LTRACE $TOOL_EXECUTABLE
} else {
	set LTRACE $objdir/../ltrace
}

global LTRACE_OPTIONS
set LTRACE_OPTIONS "";

# ltrace_compile SOURCE DEST TYPE OPTIONS 
#
# Compile PUT(program under test) by native compiler.   ltrace_compile runs 
# the right compiler, and TCL captures the output, and I evaluate the output.
#
# SOURCE is the name of program under test, with full directory.
# DEST is the name of output of compilation, with full directory.
# TYPE is an enum-like variable to affect the format or result of compiler
#   output.  Values:
#   executable   if output is an executable.
#   object       if output is an object.
# OPTIONS is option to compiler in this compilation.
proc ltrace_compile {source dest type options} {
    global LTRACE_TESTCASE_OPTIONS;
    
  # Add platform-specific options if a shared library was specified using
  # "shlib=librarypath" in OPTIONS.
  set new_options ""
  set shlib_found 0

  foreach opt $options {
      if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] {
	  if [test_compiler_info "xlc*"] {
	      # IBM xlc compiler doesn't accept shared library named other
	      # than .so: use "-Wl," to bypass this
	      lappend source "-Wl,$shlib_name"
	  } else {
	      lappend source $shlib_name
	  }
	  
	  if {$shlib_found == 0} {
	      set shlib_found 1
	      
	      if { ([test_compiler_info "gcc-*"]&& ([istarget "powerpc*-*-aix*"]|| [istarget "rs6000*-*-aix*"] ))} {
		  lappend options "additional_flags=-L${objdir}/${subdir}"
	      } elseif { [istarget "mips-sgi-irix*"] } {
		  lappend options "additional_flags=-rpath ${objdir}/${subdir}"
	      }
	  }
	  
      } else {
	  lappend new_options $opt
      }
  }
    #end of for loop
    set options $new_options
    # dump some information for debug purpose.
    verbose "options are $options"
    verbose "source is $source $dest $type $options"
    
    set result [target_compile $source $dest $type $options];
    verbose "result is $result"
    regsub "\[\r\n\]*$" "$result" "" result;
    regsub "^\[\r\n\]*" "$result" "" result;
    if { $result != "" && [lsearch $options quiet] == -1} {
	clone_output "compile failed for ltrace test, $result"
    }
    return $result;
}

proc get_compiler_info {binfile args} {
    # For compiler.c and compiler.cc
    global srcdir

    # I am going to play with the log to keep noise out.
    global outdir
    global tool

    # These come from compiler.c or compiler.cc
    global compiler_info

    # Legacy global data symbols.
    #global gcc_compiled

    # Choose which file to preprocess.
    set ifile "${srcdir}/lib/compiler.c"
    if { [llength $args] > 0 && [lindex $args 0] == "c++" } {
	    set ifile "${srcdir}/lib/compiler.cc"
    }

    # Run $ifile through the right preprocessor.
    # Toggle ltrace.log to keep the compiler output out of the log.
    #log_file
    set cppout [ ltrace_compile "${ifile}" "" preprocess [list "$args" quiet] ]
    #log_file -a "$outdir/$tool.log" 

    # Eval the output.
    set unknown 0
    foreach cppline [ split "$cppout" "\n" ] {
	    if { [ regexp "^#" "$cppline" ] } {
	      # line marker
	    } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } {
	      # blank line
	    } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } {
	    # eval this line
	      verbose "get_compiler_info: $cppline" 2
	      eval "$cppline"
	  } else {
	    # unknown line
	    verbose "get_compiler_info: $cppline"
	    set unknown 1
	  }
      }

    # Reset to unknown compiler if any diagnostics happened.
    if { $unknown } {
	    set compiler_info "unknown"
    }
  return 0
}

proc test_compiler_info { {compiler ""} } {
    global compiler_info
    verbose "compiler_info=$compiler_info"
     # if no arg, return the compiler_info string

     if [string match "" $compiler] {
         if [info exists compiler_info] {
             return $compiler_info
         } else {
             perror "No compiler info found."
         }
     }

    return [string match $compiler $compiler_info]
}

proc ltrace_compile_shlib {sources dest options} {
    set obj_options $options
    verbose "+++++++ [test_compiler_info]" 
    switch -glob [test_compiler_info] {
	"xlc-*" {
	    lappend obj_options "additional_flags=-qpic"
	}
	"gcc-*" {
	    if { !([istarget "powerpc*-*-aix*"]
		   || [istarget "rs6000*-*-aix*"]) } {
                lappend obj_options "additional_flags=-fpic"
	    }
          }
  "xlc++-*" {
      lappend obj_options "additional_flags=-qpic"
  }

	default {
	    fail "Bad compiler!"
            }
    }
    
    set outdir [file dirname $dest]
    set objects ""
    foreach source $sources {
	set sourcebase [file tail $source]
       if {[ltrace_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} {
           return -1
       }
	lappend objects ${outdir}/${sourcebase}.o
    }

    set link_options $options
    if { [test_compiler_info "xlc-*"] || [test_compiler_info "xlc++-*"]} {
	lappend link_options "additional_flags=-qmkshrobj"
    } else {
	lappend link_options "additional_flags=-shared"
    }
    if {[ltrace_compile "${objects}" "${dest}" executable $link_options] != ""} {
	return -1
    }
}

#
# ltrace_options OPTIONS_LIST
# Pass ltrace commandline options.
# 
proc ltrace_options { args } {
	
	global LTRACE_OPTIONS
	set LTRACE_OPTIONS $args
}

#
# ltrace_runtest LD_LIBRARY_PATH BIN FILE
# Trace the execution of BIN and return result.
#
# BIN is program-under-test.
# LD_LIBRARY_PATH is the env for program-under-test to run.
# FILE is to save the output from ltrace with default name $BIN.ltrace.
# Retrun output from ltrace.
#
proc ltrace_runtest { args } {

	global LTRACE
	global LTRACE_OPTIONS

	verbose "LTRACE = $LTRACE"
	
	set LD_LIBRARY_PATH_ [lindex $args 0]
	set BIN [lindex $args 1]

	# specify the output file, the default one is $BIN.ltrace
	if [llength $args]==3 then {
		set file [lindex $args 2]
	} else {
		set file $BIN.ltrace
	}
	# append this option to LTRACE_OPTIONS.
	lappend LTRACE_OPTIONS "-o"
	lappend LTRACE_OPTIONS "$file"
	verbose "LTRACE_OPTIONS = $LTRACE_OPTIONS"
	#ltrace the PUT.
	catch "exec sh -c {export LD_LIBRARY_PATH=$LD_LIBRARY_PATH_; $LTRACE $LTRACE_OPTIONS $BIN;exit}" output
	
	# return output from ltrace.
	return $output
}

#
# ltrace_saveoutput OUTPUT FILE
# Save OUTPUT from ltrace to file FILE.
# OUTPUT is output from ltrace or return value of ltrace_runtest.
# FILE is file save output.
#
proc ltrace_saveoutput { args } {
	
	set output [lindex $args 0]
	set file [lindex $args 1]

	set fd [open $file w]
     	puts $fd $output
	close $fd
}


#
# ltrace_verify_output FILE_TO_SEARCH PATTERN MAX_LINE
# Verify the ltrace output by comparing the number of PATTERN in 
# FILE_TO_SEARCH with INSTANCE_NO.  Do not specify INSTANCE_NO if
# instance number is ignored in this test.
# Reutrn:
#      0 = number of PATTERN in FILE_TO_SEARCH inqual to INSTANCE_NO.
#      1 = number of PATTERN in FILE_TO_SEARCH qual to INSTANCE_NO.
#
proc ltrace_verify_output { file_to_search pattern {instance_no 0}} {

	# compute the number of PATTERN in FILE_TO_SEARCH by grep and wc.
	catch "exec sh -c {grep \"$pattern\" $file_to_search | wc -l ;exit}" output
	verbose "output = $output"

	if [ regexp "syntax error" $output ] then {
		fail "Invalid regular expression $pattern"
        } elseif { $instance_no == 0 } then {
		if { $output == 0 } then {
			fail "Fail to find $pattern in $file_to_search"
		} else {
			pass "$pattern in $file_to_search"
		}
	} elseif { $output >= $instance_no } then {
		pass "$pattern in $file_to_search for $output times"
	} else {
		fail "$pattern in $file_to_search for $output times ,should be $instance_no"
	}
}