summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.gdbtk/defs
blob: 8e3fe5b453144ff35b13674659bfe351aa8b0d8d (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
# This file contains support code for the gdbtk test suite.
# Copyright 2001 Red Hat, Inc.
#
# Based on the Tcl testsuite support code, portions of this file
# are Copyright (c) 1990-1994 The Regents of the University of California and
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
#
global _test env srcdir objdir

if {![info exists srcdir]} {
  if {[info exists env(SRCDIR)]} {
    set srcdir $env(SRCDIR)
  } else {
    set srcdir .
  }
}

if {![info exists objdir]} {
  if {[info exists env(OBJDIR)]} {
    set objdir $env(OBJDIR)
  } elseif {$_test(interactive)} {
    # If running interactively, assume that the objdir is
    # relative to the executable's location
    set objdir [file join [file dirname [info nameofexecutable]] testsuite gdb.gdbtk]
  } else {
    set objdir .
  }
}

if {![info exists _test(verbose)]} {
  if {[info exists env(GDBTK_VERBOSE)]} {
    set _test(verbose) $env(GDBTK_VERBOSE)
  } else {
    set _test(verbose) 0
  }
}
if {![info exists _test(tests)]} {

  if {[info exists env(GDBTK_TESTS)]} {
    set _test(tests) $env(GDBTK_TESTS)
  } else {
    set _test(tests) {}
  }
}

if {[info exists env(GDBTK_LOGFILE)]} {
  set _test(logfile) [open $env(GDBTK_LOGFILE) a+]
  fconfigure $_test(logfile) -buffering none
} else {
  set _test(logfile) {}
}

# Informs gdbtk internals that testsuite is running. An example
# where this is needed is the window manager, which must place
# all windows at some place on the screen so that the system's
# window manager does not interfere. This is reset in gdbtk_test_done.
set env(GDBTK_TEST_RUNNING) 1

# The gdb "file" command to use for gdbtk testing
# NOTE: This proc appends ".exe" to all windows' programs
proc gdbtk_test_file {filename} {
  global tcl_platform

  if {$tcl_platform(platform) == "windows"} {
    append filename ".exe"
  }

  set err [catch {gdb_cmd "file $filename" 1} text]
  if {$err} {
    error $text
  }

  return $text
}

proc gdbtk_test_run {{prog_args {}}} {
  global env

  # Get the target_info array from the testsuite
  array set target_info $env(TARGET_INFO)

  # We get the target ready by:
  # 1. Run all init commands
  # 2. Issue target command
  # 3. Issue load command
  # 4. Issue run command
  foreach cmd $target_info(init) {
    set err [catch {gdb_cmd $cmd 0} txt]
    if {$err} {
      _report_error "Target initialization command \"$cmd\" failed: $txt"
      return 0
    }
  }

  if {$target_info(target) != ""} {
    set err [catch {gdb_cmd $target_info(target) 0} txt]
    if {$err} {
      _report_error "Failed to connect to target: $txt"
      return 0
    }
  }

  if {$target_info(load) != ""} {
    set err [catch {gdb_cmd $target_info(load) 0} txt]
    if {$err} {
      _report_error "Failed to load: $txt"
      return 0
    }
  }

  if {$target_info(run) != ""} {
    set err [catch {gdb_cmd $target_info(run) 0} txt]
    if {$err} {
      _report_error "Could not run target with \"$target_info(run)\": $txt"
      return 0
    }
  }

  return 1
}

proc _report_error {msg} {
  global _test

  if {[info exists _test(interactive)] && $_test(interactive)} {
    # Dialog
    tk_messageBox -message $msg -icon error -type ok
  } else {
    # to stderr
    puts stderr $msg
  }
}

proc gdbtk_print_verbose {status name description script code answer} {
  global _test

  switch $code {
    0 {
      set code_words {}
    }
    1 {
      set code_words "Test generated error: $answer"
    }

    2 {
      set code_words "Test generated return exception;  result was: $answer"
    }

    3 {
      set code_words "Test generated break exception"
    }

    4 {
      set code_words "Test generated continue exception"
    }

    5 {
      set code_words "Test generated exception $code;  message was:$answer"
    }
  }

  if {$_test(verbose) > 1 \
	|| ($_test(verbose) != 1 && ($status == "ERROR" || $status == "FAIL"))} {
    # Printed when user verbose mode (verbose > 1) or an error/failure occurs
    # not running the testsuite (dejagnu)
    puts stdout "\n"
    puts stdout "==== $name $description"
    puts stdout "==== Contents of test case:"
    puts stdout "$script"
    if {$code_words != ""} {
      puts stdout $code_words
    }
    puts stdout "==== Result was:"
    puts stdout "$answer"
  } elseif {$_test(verbose)} {
    # Printed for the testsuite (verbose = 1)
    puts stdout "[list $status $name $description $code_words]"

    if {$_test(logfile) != ""} {
      puts $_test(logfile) "\n"
      puts $_test(logfile) "==== $name $description"
      puts $_test(logfile) "==== Contents of test case:"
      puts $_test(logfile) "$script"
      if {$code_words != ""} {
	puts $_test(logfile) $code_words
      }
      puts $_test(logfile) "==== Result was:"
      puts $_test(logfile) "$answer"
    }
  }
}

# gdbtk_test
#
# This procedure runs a test and prints an error message if the
# test fails.
#
# Arguments:
# name -		Name of test, in the form foo-1.2.
# description -		Short textual description of the test, to
#			help humans understand what it does.
# script -		Script to run to carry out the test.  It must
#			return a result that can be checked for
#			correctness.
# answer -		Expected result from script.

proc gdbtk_test {name description script answer} {
  global _test test_ran

  set test_ran 0
  if {[string compare $_test(tests) ""] != 0} then {
    set ok 0
    foreach test $_test(tests) {
      if [string match $test $name] then {
	set ok 1
	break
      }
    }
    if !$ok then return
  }

  set code [catch {uplevel $script} result]
  set test_ran 1
  if {$code != 0} {
    # Error
    gdbtk_print_verbose ERROR $name $description $script \
      $code $result
  } elseif {[string compare $result $answer] == 0} { 
    if {[string index $name 0] == "*"} {
      # XPASS
      set HOW XPASS
    } else {
      set HOW PASS
    }

    if {$_test(verbose)} {
      gdbtk_print_verbose $HOW $name $description $script \
	$code $result
      if {$_test(verbose) != 1} {
	puts stdout "++++ $name ${HOW}ED"
      }
    }
    if {$_test(logfile) != ""} {
      puts $_test(logfile) "++++ $name ${HOW}ED"
    }
  } else {
    if {[string index $name 0] == "*"} {
      # XFAIL
      set HOW XFAIL
    } else {
      set HOW FAIL
    }

    gdbtk_print_verbose $HOW $name $description $script \
      $code $result
    if {$_test(verbose) != 1} {
      puts stdout "---- Result should have been:"
      puts stdout "$answer"
      puts stdout "---- $name ${HOW}ED" 
    }
    if {$_test(logfile) != ""} {
      puts $_test(logfile) "---- Result should have been:"
      puts $_test(logfile) "$answer"
      puts $_test(logfile) "---- $name ${HOW}ED" 
    }
  }
}

proc gdbtk_dotests {file args} {
  global _test
  set savedTests $_test(tests)
  set _test(tests) $args
  source $file
  set _test(tests) $savedTests
}

proc gdbtk_test_done {} {
  global _test env

  if {$_test(logfile) != ""} {
    close $_test(logfile)
  }

  set env(GDBTK_TEST_RUNNING) 0
  if {![info exists _test(interactive)] || !$_test(interactive)} {
    gdbtk_force_quit
  }
}

proc gdbtk_test_error {desc} {
  set desc [join [split $desc \n] |]
  puts "ERROR \{$desc\} \{\} \{\}"
  gdbtk_test_done
}

# Override the warning dialog. We don't want to see them.
rename show_warning real_show_warning
proc show_warning {msg} {
  global _test

  set str "INSIGHT TESTSUITE WARNING: $msg"
  puts stdout $str
  if {$_test(logfile) != ""} {
    puts $_test(logfile) $str
  }
}