blob: e014732ccc61d6ccfaf2f0570631a38b8eb44080 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhf7141992008-06-19 00:16:08 +000014# $Id: tester.tcl,v 1.127 2008/06/19 00:16:08 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drh8359d8c2008-03-29 11:00:54 +000016#
17# What for user input before continuing. This gives an opportunity
18# to connect profiling tools to the process.
19#
20for {set i 0} {$i<[llength $argv]} {incr i} {
21 if {[regexp {^-+pause$} [lindex $argv $i] all value]} {
22 puts -nonewline "Press RETURN to begin..."
23 flush stdout
24 gets stdin
25 set argv [lreplace $argv $i $i]
26 }
27}
drh348784e2000-05-29 20:41:49 +000028
drh92febd92004-08-20 18:34:20 +000029set tcl_precision 15
drh49285702005-09-17 15:20:26 +000030set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000031
drh3a7fb7c2007-08-10 16:41:08 +000032#
33# Check the command-line arguments for a default soft-heap-limit.
34# Store this default value in the global variable ::soft_limit and
35# update the soft-heap-limit each time this script is run. In that
36# way if an individual test file changes the soft-heap-limit, it
37# will be reset at the start of the next test file.
38#
39if {![info exists soft_limit]} {
40 set soft_limit 0
41 for {set i 0} {$i<[llength $argv]} {incr i} {
42 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
43 if {$value!="off"} {
44 set soft_limit $value
45 }
46 set argv [lreplace $argv $i $i]
47 }
48 }
49}
50sqlite3_soft_heap_limit $soft_limit
51
drh4a50aac2007-08-23 02:47:53 +000052#
53# Check the command-line arguments to set the memory debugger
54# backtrace depth.
55#
56# See the sqlite3_memdebug_backtrace() function in mem2.c or
57# test_malloc.c for additional information.
58#
59for {set i 0} {$i<[llength $argv]} {incr i} {
danielk197735754ac2008-03-21 17:29:37 +000060 if {[lindex $argv $i] eq "--malloctrace"} {
61 set argv [lreplace $argv $i $i]
danielk19775f096132008-03-28 15:44:09 +000062 sqlite3_memdebug_backtrace 10
danielk197735754ac2008-03-21 17:29:37 +000063 sqlite3_memdebug_log start
danielk197735754ac2008-03-21 17:29:37 +000064 set tester_do_malloctrace 1
65 }
66}
67for {set i 0} {$i<[llength $argv]} {incr i} {
drh4a50aac2007-08-23 02:47:53 +000068 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
69 sqlite3_memdebug_backtrace $value
70 set argv [lreplace $argv $i $i]
71 }
72}
73
danielk19771e21fd52008-04-10 17:27:38 +000074
75proc ostrace_call {zCall nClick zFile i32 i64} {
danielk197727467042008-05-12 07:42:20 +000076 set s "INSERT INTO ostrace VALUES('$zCall', $nClick, '$zFile', $i32, $i64);"
danielk19771e21fd52008-04-10 17:27:38 +000077 puts $::ostrace_fd $s
78}
79
80for {set i 0} {$i<[llength $argv]} {incr i} {
81 if {[lindex $argv $i] eq "--ossummary" || [lindex $argv $i] eq "--ostrace"} {
82 sqlite3_instvfs create -default ostrace
83 set tester_do_ostrace 1
84 set ostrace_fd [open ostrace.sql w]
85 puts $ostrace_fd "BEGIN;"
86 if {[lindex $argv $i] eq "--ostrace"} {
87 set s "CREATE TABLE ostrace"
88 append s "(method TEXT, clicks INT, file TEXT, i32 INT, i64 INT);"
89 puts $ostrace_fd $s
90 sqlite3_instvfs configure ostrace ostrace_call
danielk1977374177e2008-05-08 15:58:06 +000091 sqlite3_instvfs configure ostrace ostrace_call
danielk19771e21fd52008-04-10 17:27:38 +000092 }
93 set argv [lreplace $argv $i $i]
94 }
danielk1977374177e2008-05-08 15:58:06 +000095 if {[lindex $argv $i] eq "--binarylog"} {
96 set tester_do_binarylog 1
danielk197727467042008-05-12 07:42:20 +000097
98 # sqlite3_simulate_device -char safe_append
99 # sqlite3_instvfs binarylog -default -parent devsym binarylog ostrace.bin
danielk1977374177e2008-05-08 15:58:06 +0000100 sqlite3_instvfs binarylog -default binarylog ostrace.bin
101 set argv [lreplace $argv $i $i]
danielk197727467042008-05-12 07:42:20 +0000102 sqlite3_instvfs marker binarylog "$argv0 $argv"
danielk1977374177e2008-05-08 15:58:06 +0000103 }
danielk19771e21fd52008-04-10 17:27:38 +0000104}
105
drh6a288a32008-01-07 19:20:24 +0000106#
107# Check the command-line arguments to set the maximum number of
108# errors tolerated before halting.
109#
110if {![info exists maxErr]} {
111 set maxErr 1000
112}
113for {set i 0} {$i<[llength $argv]} {incr i} {
114 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
115 set argv [lreplace $argv $i $i]
116 }
117}
118#puts "Max error = $maxErr"
119
drh4a50aac2007-08-23 02:47:53 +0000120
drh9eb9e262004-02-11 02:18:05 +0000121# Use the pager codec if it is available
122#
drhef4ac8f2004-06-19 00:16:31 +0000123if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
124 rename sqlite3 sqlite_orig
125 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000126 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
127 lappend args -key {xyzzy}
128 }
129 uplevel 1 sqlite_orig $args
130 }
131}
132
133
drhbec2bf42000-05-29 23:48:22 +0000134# Create a test database
135#
drh254cba22001-09-20 01:44:42 +0000136catch {db close}
137file delete -force test.db
138file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000139sqlite3 db ./test.db
140set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000141if {[info exists ::SETUP_SQL]} {
142 db eval $::SETUP_SQL
143}
drhbec2bf42000-05-29 23:48:22 +0000144
145# Abort early if this script has been run before.
146#
147if {[info exists nTest]} return
148
149# Set the test counters to zero
150#
drh348784e2000-05-29 20:41:49 +0000151set nErr 0
152set nTest 0
drh767c2002000-10-19 14:10:08 +0000153set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000154set failList {}
drh521cc842008-04-15 02:36:33 +0000155set omitList {}
drhb62c3352006-11-23 09:39:16 +0000156if {![info exists speedTest]} {
157 set speedTest 0
158}
drh348784e2000-05-29 20:41:49 +0000159
drh521cc842008-04-15 02:36:33 +0000160# Record the fact that a sequence of tests were omitted.
161#
162proc omit_test {name reason} {
163 global omitList
164 lappend omitList [list $name $reason]
165}
166
drh348784e2000-05-29 20:41:49 +0000167# Invoke the do_test procedure to run a single test
168#
169proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000170 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000171 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000172 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000173 sqlite3_instvfs marker binarylog "Start of $name"
174 }
drh767c2002000-10-19 14:10:08 +0000175 if {$skip_test} {
176 set skip_test 0
177 return
178 }
179 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000180 set go 1
181 } else {
182 set go 0
183 foreach pattern $argv {
184 if {[string match $pattern $name]} {
185 set go 1
186 break
187 }
188 }
189 }
190 if {!$go} return
191 incr nTest
drh5edc3122001-09-13 21:53:09 +0000192 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000193 flush stdout
194 if {[catch {uplevel #0 "$cmd;\n"} result]} {
195 puts "\nError: $result"
196 incr nErr
drha1b351a2001-09-14 16:42:12 +0000197 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000198 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000199 } elseif {[string compare $result $expected]} {
200 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
201 incr nErr
drha1b351a2001-09-14 16:42:12 +0000202 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000203 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000204 } else {
205 puts " Ok"
206 }
drh6558db82007-04-13 03:23:21 +0000207 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000208 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000209 sqlite3_instvfs marker binarylog "End of $name"
210 }
drh348784e2000-05-29 20:41:49 +0000211}
212
drhb62c3352006-11-23 09:39:16 +0000213# Run an SQL script.
214# Return the number of microseconds per statement.
215#
drh3590f152006-11-23 21:09:10 +0000216proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000217 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000218 flush stdout
219 set speed [time {sqlite3_exec_nr db $sql}]
220 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000221 if {$tm == 0} {
222 set rate [format %20s "many"]
223 } else {
224 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
225 }
drh3590f152006-11-23 21:09:10 +0000226 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000227 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000228 global total_time
229 set total_time [expr {$total_time+$tm}]
230}
drh45c236d2008-03-22 01:08:00 +0000231proc speed_trial_tcl {name numstmt units script} {
232 puts -nonewline [format {%-21.21s } $name...]
233 flush stdout
234 set speed [time {eval $script}]
235 set tm [lindex $speed 0]
236 if {$tm == 0} {
237 set rate [format %20s "many"]
238 } else {
239 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
240 }
241 set u2 $units/s
242 puts [format {%12d uS %s %s} $tm $rate $u2]
243 global total_time
244 set total_time [expr {$total_time+$tm}]
245}
drhdd924312007-03-31 22:34:16 +0000246proc speed_trial_init {name} {
247 global total_time
248 set total_time 0
249}
250proc speed_trial_summary {name} {
251 global total_time
252 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000253}
254
drh348784e2000-05-29 20:41:49 +0000255# Run this routine last
256#
257proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000258 finalize_testing
259}
260proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000261 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000262
drh6e142f52000-06-08 13:36:40 +0000263 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000264 catch {db2 close}
265 catch {db3 close}
266
drh9bc54492007-10-23 14:49:59 +0000267 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000268 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000269 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000270 db close
drh984bfaa2008-03-19 16:08:53 +0000271 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000272 set heaplimit [sqlite3_soft_heap_limit]
273 if {$heaplimit!=$::soft_limit} {
274 puts "soft-heap-limit changed by this script\
275 from $::soft_limit to $heaplimit"
276 } elseif {$heaplimit!="" && $heaplimit>0} {
277 puts "soft-heap-limit set to $heaplimit"
278 }
279 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000280 incr nTest
drh348784e2000-05-29 20:41:49 +0000281 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000282 if {$nErr>0} {
283 puts "Failures on these tests: $::failList"
284 }
drh521cc842008-04-15 02:36:33 +0000285 if {[llength $omitList]>0} {
286 puts "Omitted test cases:"
287 set prec {}
288 foreach {rec} [lsort $omitList] {
289 if {$rec==$prec} continue
290 set prec $rec
291 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
292 }
293 }
drh80788d82006-09-02 14:50:23 +0000294 if {$nErr>0 && ![working_64bit_int]} {
295 puts "******************************************************************"
296 puts "N.B.: The version of TCL that you used to build this test harness"
297 puts "is defective in that it does not support 64-bit integers. Some or"
298 puts "all of the test failures above might be a result from this defect"
299 puts "in your TCL build."
300 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000301 }
danielk1977374177e2008-05-08 15:58:06 +0000302 if {[info exists ::tester_do_binarylog]} {
303 sqlite3_instvfs destroy binarylog
304 }
drh94e92032003-02-16 22:21:32 +0000305 if {$sqlite_open_file_count} {
306 puts "$sqlite_open_file_count files were left open"
307 incr nErr
308 }
danielk19771e21fd52008-04-10 17:27:38 +0000309 if {[info exists ::tester_do_ostrace]} {
310 puts "Writing ostrace.sql..."
311 set fd $::ostrace_fd
312
313 puts -nonewline $fd "CREATE TABLE ossummary"
314 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
315 foreach row [sqlite3_instvfs report ostrace] {
316 foreach {method count clicks} $row break
317 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
318 }
319 puts $fd "COMMIT;"
320 close $fd
321 sqlite3_instvfs destroy ostrace
322 }
drhf3a65f72007-08-22 20:18:21 +0000323 if {[sqlite3_memory_used]>0} {
324 puts "Unfreed memory: [sqlite3_memory_used] bytes"
325 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000326 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000327 puts "Writing unfreed memory log to \"./memleak.txt\""
328 sqlite3_memdebug_dump ./memleak.txt
329 }
drhf3a65f72007-08-22 20:18:21 +0000330 } else {
331 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000332 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000333 sqlite3_memdebug_dump ./memusage.txt
334 }
drhf3a65f72007-08-22 20:18:21 +0000335 }
drhf7141992008-06-19 00:16:08 +0000336 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000337 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
338 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000339 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
340 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
341 }
danielk197735754ac2008-03-21 17:29:37 +0000342 if {[info exists ::tester_do_malloctrace]} {
343 puts "Writing mallocs.sql..."
344 memdebug_log_sql
345 sqlite3_memdebug_log stop
346 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000347
348 if {[sqlite3_memory_used]>0} {
349 puts "Writing leaks.sql..."
350 sqlite3_memdebug_log sync
351 memdebug_log_sql leaks.sql
352 }
danielk197735754ac2008-03-21 17:29:37 +0000353 }
drhd9910fe2006-10-04 11:55:49 +0000354 foreach f [glob -nocomplain test.db-*-journal] {
355 file delete -force $f
356 }
357 foreach f [glob -nocomplain test.db-mj*] {
358 file delete -force $f
359 }
drhdb25e382001-03-15 18:21:22 +0000360 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000361}
362
drhf7141992008-06-19 00:16:08 +0000363# Display memory statistics for analysis and debugging purposes.
364#
365proc show_memstats {} {
366 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
367 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
368 puts "Memory used: $val"
369 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
370 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
371 puts "Page-cache used: $val"
372 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
373 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
374 puts "Page-cache overflow: $val"
375 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
376 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
377 puts "Scratch memory used: $val"
378 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
379 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
380 puts "Scratch overflow: $val"
381 set x [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
382 puts "Maximum alloc size: [lindex $x 2]"
383}
384
drh348784e2000-05-29 20:41:49 +0000385# A procedure to execute SQL
386#
drhc4a3c772001-04-04 11:48:57 +0000387proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000388 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000389 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000390}
drh3aadb2e2000-05-31 17:59:25 +0000391
drhadbca9c2001-09-27 15:11:53 +0000392# Execute SQL and catch exceptions.
393#
394proc catchsql {sql {db db}} {
395 # puts "SQL = $sql"
396 set r [catch {$db eval $sql} msg]
397 lappend r $msg
398 return $r
399}
400
drh04096482001-11-09 22:41:44 +0000401# Do an VDBE code dump on the SQL given
402#
403proc explain {sql {db db}} {
404 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000405 puts "addr opcode p1 p2 p3 p4 p5 #"
406 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000407 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000408 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
409 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
410 ]
drh04096482001-11-09 22:41:44 +0000411 }
412}
413
drhdafc0ce2008-04-17 19:14:02 +0000414# Show the VDBE program for an SQL statement but omit the Trace
415# opcode at the beginning. This procedure can be used to prove
416# that different SQL statements generate exactly the same VDBE code.
417#
418proc explain_no_trace {sql} {
419 set tr [db eval "EXPLAIN $sql"]
420 return [lrange $tr 7 end]
421}
422
drh3aadb2e2000-05-31 17:59:25 +0000423# Another procedure to execute SQL. This one includes the field
424# names in the returned list.
425#
426proc execsql2 {sql} {
427 set result {}
428 db eval $sql data {
429 foreach f $data(*) {
430 lappend result $f $data($f)
431 }
432 }
433 return $result
434}
drh17a68932001-01-31 13:28:08 +0000435
drhdde85d92003-03-01 19:45:34 +0000436# Use the non-callback API to execute multiple SQL statements
437#
438proc stepsql {dbptr sql} {
439 set sql [string trim $sql]
440 set r 0
441 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000442 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000443 return [list 1 $vm]
444 }
445 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000446# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
447# foreach v $VAL {lappend r $v}
448# }
449 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
450 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
451 lappend r [sqlite3_column_text $vm $i]
452 }
drhdde85d92003-03-01 19:45:34 +0000453 }
danielk1977106bb232004-05-21 10:08:53 +0000454 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000455 return [list 1 $errmsg]
456 }
457 }
458 return $r
459}
460
drh17a68932001-01-31 13:28:08 +0000461# Delete a file or directory
462#
463proc forcedelete {filename} {
464 if {[catch {file delete -force $filename}]} {
465 exec rm -rf $filename
466 }
467}
drh21504322002-06-25 13:16:02 +0000468
469# Do an integrity check of the entire database
470#
471proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000472 ifcapable integrityck {
473 do_test $name {
474 execsql {PRAGMA integrity_check}
475 } {ok}
476 }
477}
478
danielk1977db4e8862008-01-16 08:24:46 +0000479proc fix_ifcapable_expr {expr} {
480 set ret ""
481 set state 0
482 for {set i 0} {$i < [string length $expr]} {incr i} {
483 set char [string range $expr $i $i]
484 set newstate [expr {[string is alnum $char] || $char eq "_"}]
485 if {$newstate && !$state} {
486 append ret {$::sqlite_options(}
487 }
488 if {!$newstate && $state} {
489 append ret )
490 }
491 append ret $char
492 set state $newstate
493 }
494 if {$state} {append ret )}
495 return $ret
496}
497
drh27d258a2004-10-30 20:23:09 +0000498# Evaluate a boolean expression of capabilities. If true, execute the
499# code. Omit the code if false.
500#
danielk19773e8c37e2005-01-21 03:12:14 +0000501proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000502 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
503 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000504 if ($e2) {
505 set c [catch {uplevel 1 $code} r]
506 } else {
507 set c [catch {uplevel 1 $elsecode} r]
508 }
509 return -code $c $r
drh21504322002-06-25 13:16:02 +0000510}
danielk197745901d62004-11-10 15:27:38 +0000511
danielk1977aca790a2005-01-13 11:07:52 +0000512# This proc execs a seperate process that crashes midway through executing
513# the SQL script $sql on database test.db.
514#
515# The crash occurs during a sync() of file $crashfile. When the crash
516# occurs a random subset of all unsynced writes made by the process are
517# written into the files on disk. Argument $crashdelay indicates the
518# number of file syncs to wait before crashing.
519#
520# The return value is a list of two elements. The first element is a
521# boolean, indicating whether or not the process actually crashed or
522# reported some other error. The second element in the returned list is the
523# error message. This is "child process exited abnormally" if the crash
524# occured.
525#
danielk1977f8940ae2007-08-23 11:07:10 +0000526# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000527#
528proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000529 if {$::tcl_platform(platform)!="unix"} {
530 error "crashsql should only be used on unix"
531 }
danielk197759a33f92007-03-17 10:26:59 +0000532
533 set blocksize ""
534 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000535 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000536 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000537 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000538 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000539 set sql [lindex $args end]
540
541 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
542 set z [lindex $args $ii]
543 set n [string length $z]
544 set z2 [lindex $args [expr $ii+1]]
545
546 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000547 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000548 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000549 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000550 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000551 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000552 else { error "Unrecognized option: $z" }
553 }
554
555 if {$crashfile eq ""} {
556 error "Compulsory option -file missing"
557 }
558
danielk1977aca790a2005-01-13 11:07:52 +0000559 set cfile [file join [pwd] $crashfile]
560
561 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000562 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000563 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000564 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000565 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000566
567 # This block sets the cache size of the main database to 10
568 # pages. This is done in case the build is configured to omit
569 # "PRAGMA cache_size".
570 puts $f {db eval {SELECT * FROM sqlite_master;}}
571 puts $f {set bt [btree_from_db db]}
572 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000573 if {$prngseed} {
574 set seed [expr {$prngseed%10007+1}]
575 # puts seed=$seed
576 puts $f "db eval {SELECT randomblob($seed)}"
577 }
danielk1977933bbd62007-03-17 07:22:42 +0000578
drhf8587402008-01-08 16:03:49 +0000579 if {[string length $tclbody]>0} {
580 puts $f $tclbody
581 }
582 if {[string length $sql]>0} {
583 puts $f "db eval {"
584 puts $f "$sql"
585 puts $f "}"
586 }
danielk1977aca790a2005-01-13 11:07:52 +0000587 close $f
588
589 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000590 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000591 } msg]
592 lappend r $msg
593}
594
danielk197732554c12005-01-22 03:39:39 +0000595# Usage: do_ioerr_test <test number> <options...>
596#
597# This proc is used to implement test cases that check that IO errors
598# are correctly handled. The first argument, <test number>, is an integer
599# used to name the tests executed by this proc. Options are as follows:
600#
601# -tclprep TCL script to run to prepare test.
602# -sqlprep SQL script to run to prepare test.
603# -tclbody TCL script to run with IO error simulation.
604# -sqlbody TCL script to run with IO error simulation.
605# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000606# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000607# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000608# -start Value of 'N' to begin with (default 1)
609#
610# -cksum Boolean. If true, test that the database does
611# not change during the execution of the test case.
612#
613proc do_ioerr_test {testname args} {
614
danielk197732554c12005-01-22 03:39:39 +0000615 set ::ioerropts(-start) 1
616 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000617 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000618 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000619 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000620 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000621 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000622 array set ::ioerropts $args
623
danielk197747cd39c2008-05-12 12:41:15 +0000624 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
625 # a couple of obscure IO errors that do not return them.
626 set ::ioerropts(-erc) 0
627
danielk197732554c12005-01-22 03:39:39 +0000628 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000629 #reset_prng_state
630 save_prng_state
danielk1977861f7452008-06-05 11:39:11 +0000631 for {set n $::ioerropts(-start)} {$::go && $n<200} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000632 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000633 incr ::ioerropts(-count) -1
634 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000635
636 # Skip this IO error if it was specified with the "-exclude" option.
637 if {[info exists ::ioerropts(-exclude)]} {
638 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
639 }
danielk1977861f7452008-06-05 11:39:11 +0000640 if {$::ioerropts(-restoreprng)} {
641 restore_prng_state
642 }
danielk197732554c12005-01-22 03:39:39 +0000643
644 # Delete the files test.db and test2.db, then execute the TCL and
645 # SQL (in that order) to prepare for the test case.
646 do_test $testname.$n.1 {
647 set ::sqlite_io_error_pending 0
648 catch {db close}
649 catch {file delete -force test.db}
650 catch {file delete -force test.db-journal}
651 catch {file delete -force test2.db}
652 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000653 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000654 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000655 if {[info exists ::ioerropts(-tclprep)]} {
656 eval $::ioerropts(-tclprep)
657 }
658 if {[info exists ::ioerropts(-sqlprep)]} {
659 execsql $::ioerropts(-sqlprep)
660 }
661 expr 0
662 } {0}
663
664 # Read the 'checksum' of the database.
665 if {$::ioerropts(-cksum)} {
666 set checksum [cksum]
667 }
drh93aed5a2008-01-16 17:46:38 +0000668
danielk197732554c12005-01-22 03:39:39 +0000669 # Set the Nth IO error to fail.
670 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000671 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000672 set ::sqlite_io_error_pending $n
673 }] $n
674
675 # Create a single TCL script from the TCL and SQL specified
676 # as the body of the test.
677 set ::ioerrorbody {}
678 if {[info exists ::ioerropts(-tclbody)]} {
679 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
680 }
681 if {[info exists ::ioerropts(-sqlbody)]} {
682 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
683 }
684
685 # Execute the TCL Script created in the above block. If
686 # there are at least N IO operations performed by SQLite as
687 # a result of the script, the Nth will fail.
688 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000689 set ::sqlite_io_error_hit 0
690 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000691 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000692 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000693 set rc [sqlite3_errcode $::DB]
694 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000695 # If we are in extended result code mode, make sure all of the
696 # IOERRs we get back really do have their extended code values.
697 # If an extended result code is returned, the sqlite3_errcode
698 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
699 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000700 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
701 return $rc
702 }
703 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000704 # If we are not in extended result code mode, make sure no
705 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000706 if {[regexp {\+\d} $rc]} {
707 return $rc
708 }
709 }
drh93aed5a2008-01-16 17:46:38 +0000710 # The test repeats as long as $::go is non-zero. $::go starts out
711 # as 1. When a test runs to completion without hitting an I/O
712 # error, that means there is no point in continuing with this test
713 # case so set $::go to zero.
714 #
715 if {$::sqlite_io_error_pending>0} {
716 set ::go 0
717 set q 0
718 set ::sqlite_io_error_pending 0
719 } else {
720 set q 1
721 }
722
drhc9ac5ca2005-11-04 22:03:30 +0000723 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000724 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
725 set r 1
726 }
danielk1977f2fa8312006-01-24 13:09:33 +0000727 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000728
729 # One of two things must have happened. either
730 # 1. We never hit the IO error and the SQL returned OK
731 # 2. An IO error was hit and the SQL failed
732 #
drh93aed5a2008-01-16 17:46:38 +0000733 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000734 } {1}
735
danielk197780daec62008-05-12 10:57:02 +0000736 set ::sqlite_io_error_hit 0
737 set ::sqlite_io_error_pending 0
738
danielk197752b472a2008-05-05 16:23:55 +0000739 # Check that no page references were leaked. There should be
740 # a single reference if there is still an active transaction,
741 # or zero otherwise.
742 #
danielk197781620542008-06-07 05:19:37 +0000743 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
744 # locks are established on database files (i.e. if the error
745 # occurs while attempting to detect a hot-journal file), then
746 # there may 0 page references and an active transaction according
747 # to [sqlite3_get_autocommit].
748 #
danielk19774abd5442008-05-05 15:26:50 +0000749 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000750 do_test $testname.$n.4 {
751 set bt [btree_from_db db]
752 db_enter db
753 array set stats [btree_pager_stats $bt]
754 db_leave db
danielk197781620542008-06-07 05:19:37 +0000755 set nRef $stats(ref)
756 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
757 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000758 }
759
danielk197752b472a2008-05-05 16:23:55 +0000760 # If there is an open database handle and no open transaction,
761 # and the pager is not running in exclusive-locking mode,
762 # check that the pager is in "unlocked" state. Theoretically,
763 # if a call to xUnlock() failed due to an IO error the underlying
764 # file may still be locked.
765 #
766 ifcapable pragma {
767 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000768 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000769 && [db one {pragma locking_mode}] eq "normal"
770 && [sqlite3_get_autocommit db]
771 } {
772 do_test $testname.$n.5 {
773 set bt [btree_from_db db]
774 db_enter db
775 array set stats [btree_pager_stats $bt]
776 db_leave db
777 set stats(state)
778 } 0
779 }
780 }
781
danielk197732554c12005-01-22 03:39:39 +0000782 # If an IO error occured, then the checksum of the database should
783 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000784 #
drh1aa5af12008-03-07 19:51:14 +0000785 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000786 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000787 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000788 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000789 cksum
790 } $checksum
791 }
792
drh1aa5af12008-03-07 19:51:14 +0000793 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000794 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000795 if {[info exists ::ioerropts(-cleanup)]} {
796 catch $::ioerropts(-cleanup)
797 }
danielk197732554c12005-01-22 03:39:39 +0000798 }
799 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000800 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000801 unset ::ioerropts
802}
803
drh93aed5a2008-01-16 17:46:38 +0000804# Return a checksum based on the contents of the main database associated
805# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000806#
807proc cksum {{db db}} {
808 set txt [$db eval {
809 SELECT name, type, sql FROM sqlite_master order by name
810 }]\n
811 foreach tbl [$db eval {
812 SELECT name FROM sqlite_master WHERE type='table' order by name
813 }] {
814 append txt [$db eval "SELECT * FROM $tbl"]\n
815 }
816 foreach prag {default_synchronous default_cache_size} {
817 append txt $prag-[$db eval "PRAGMA $prag"]\n
818 }
819 set cksum [string length $txt]-[md5 $txt]
820 # puts $cksum-[file size test.db]
821 return $cksum
822}
823
drh93aed5a2008-01-16 17:46:38 +0000824# Generate a checksum based on the contents of the main and temp tables
825# database $db. If the checksum of two databases is the same, and the
826# integrity-check passes for both, the two databases are identical.
827#
drh1db639c2008-01-17 02:36:28 +0000828proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000829 set ret [list]
830 ifcapable tempdb {
831 set sql {
832 SELECT name FROM sqlite_master WHERE type = 'table' UNION
833 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
834 SELECT 'sqlite_master' UNION
835 SELECT 'sqlite_temp_master' ORDER BY 1
836 }
837 } else {
838 set sql {
839 SELECT name FROM sqlite_master WHERE type = 'table' UNION
840 SELECT 'sqlite_master' ORDER BY 1
841 }
842 }
843 set tbllist [$db eval $sql]
844 set txt {}
845 foreach tbl $tbllist {
846 append txt [$db eval "SELECT * FROM $tbl"]
847 }
848 foreach prag {default_cache_size} {
849 append txt $prag-[$db eval "PRAGMA $prag"]\n
850 }
851 # puts txt=$txt
852 return [md5 $txt]
853}
854
danielk197735754ac2008-03-21 17:29:37 +0000855proc memdebug_log_sql {{filename mallocs.sql}} {
856
danielk19776f332c12008-03-21 14:22:44 +0000857 set data [sqlite3_memdebug_log dump]
858 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000859 if {$nFrame < 0} { return "" }
860
danielk197735754ac2008-03-21 17:29:37 +0000861 set database temp
862
danielk19776f332c12008-03-21 14:22:44 +0000863 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
864 for {set ii 1} {$ii <= $nFrame} {incr ii} {
865 append tbl ", f${ii}"
866 }
867 append tbl ");\n"
868
869 set sql ""
870 foreach e $data {
871 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
872 foreach f [lrange $e 2 end] {
873 set frames($f) 1
874 }
875 }
876
877 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000878 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000879
880 foreach f [array names frames] {
881 set addr [format %x $f]
882 set cmd "addr2line -e [info nameofexec] $addr"
883 set line [eval exec $cmd]
884 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000885
886 set file [lindex [split $line :] 0]
887 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000888 }
889
danielk197735754ac2008-03-21 17:29:37 +0000890 foreach f [array names files] {
891 set contents ""
892 catch {
893 set fd [open $f]
894 set contents [read $fd]
895 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000896 }
danielk197735754ac2008-03-21 17:29:37 +0000897 set contents [string map {' ''} $contents]
898 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000899 }
danielk19776f332c12008-03-21 14:22:44 +0000900
danielk197735754ac2008-03-21 17:29:37 +0000901 set fd [open $filename w]
902 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
903 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000904}
drh93aed5a2008-01-16 17:46:38 +0000905
danielk197732554c12005-01-22 03:39:39 +0000906# Copy file $from into $to. This is used because some versions of
907# TCL for windows (notably the 8.4.1 binary package shipped with the
908# current mingw release) have a broken "file copy" command.
909#
910proc copy_file {from to} {
911 if {$::tcl_platform(platform)=="unix"} {
912 file copy -force $from $to
913 } else {
914 set f [open $from]
915 fconfigure $f -translation binary
916 set t [open $to w]
917 fconfigure $t -translation binary
918 puts -nonewline $t [read $f [file size $from]]
919 close $t
920 close $f
921 }
922}
923
danielk197745901d62004-11-10 15:27:38 +0000924# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
925# to non-zero, then set the global variable $AUTOVACUUM to 1.
926set AUTOVACUUM $sqlite_options(default_autovacuum)