blob: 3b4c1c136bcc495b5175eab0ae1f020202f544e0 [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#
danielk1977185eac92008-07-12 15:55:54 +000014# $Id: tester.tcl,v 1.132 2008/07/12 15:55:55 danielk1977 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
danielk1977374177e2008-05-08 15:58:06 +000097 set argv [lreplace $argv $i $i]
98 }
danielk19771e21fd52008-04-10 17:27:38 +000099}
100
drh6a288a32008-01-07 19:20:24 +0000101#
102# Check the command-line arguments to set the maximum number of
103# errors tolerated before halting.
104#
105if {![info exists maxErr]} {
106 set maxErr 1000
107}
108for {set i 0} {$i<[llength $argv]} {incr i} {
109 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
110 set argv [lreplace $argv $i $i]
111 }
112}
113#puts "Max error = $maxErr"
114
drh4a50aac2007-08-23 02:47:53 +0000115
drh9eb9e262004-02-11 02:18:05 +0000116# Use the pager codec if it is available
117#
drhef4ac8f2004-06-19 00:16:31 +0000118if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
119 rename sqlite3 sqlite_orig
120 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000121 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
122 lappend args -key {xyzzy}
123 }
124 uplevel 1 sqlite_orig $args
125 }
126}
127
128
drhbec2bf42000-05-29 23:48:22 +0000129# Create a test database
130#
danielk1977f7b9d662008-06-23 18:49:43 +0000131if {![info exists nTest]} {
132 sqlite3_shutdown
133 install_malloc_faultsim 1
134 sqlite3_initialize
danielk1977185eac92008-07-12 15:55:54 +0000135 if {[info exists tester_do_binarylog]} {
136 sqlite3_instvfs binarylog -default binarylog ostrace.bin
137 sqlite3_instvfs marker binarylog "$argv0 $argv"
138 }
danielk1977f7b9d662008-06-23 18:49:43 +0000139}
drh254cba22001-09-20 01:44:42 +0000140catch {db close}
141file delete -force test.db
142file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000143sqlite3 db ./test.db
144set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000145if {[info exists ::SETUP_SQL]} {
146 db eval $::SETUP_SQL
147}
drhbec2bf42000-05-29 23:48:22 +0000148
149# Abort early if this script has been run before.
150#
151if {[info exists nTest]} return
152
153# Set the test counters to zero
154#
drh348784e2000-05-29 20:41:49 +0000155set nErr 0
156set nTest 0
drh767c2002000-10-19 14:10:08 +0000157set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000158set failList {}
drh521cc842008-04-15 02:36:33 +0000159set omitList {}
drhb62c3352006-11-23 09:39:16 +0000160if {![info exists speedTest]} {
161 set speedTest 0
162}
drh348784e2000-05-29 20:41:49 +0000163
drh521cc842008-04-15 02:36:33 +0000164# Record the fact that a sequence of tests were omitted.
165#
166proc omit_test {name reason} {
167 global omitList
168 lappend omitList [list $name $reason]
169}
170
drh348784e2000-05-29 20:41:49 +0000171# Invoke the do_test procedure to run a single test
172#
173proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000174 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000175 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000176 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000177 sqlite3_instvfs marker binarylog "Start of $name"
178 }
drh767c2002000-10-19 14:10:08 +0000179 if {$skip_test} {
180 set skip_test 0
181 return
182 }
183 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000184 set go 1
185 } else {
186 set go 0
187 foreach pattern $argv {
188 if {[string match $pattern $name]} {
189 set go 1
190 break
191 }
192 }
193 }
194 if {!$go} return
195 incr nTest
drh5edc3122001-09-13 21:53:09 +0000196 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000197 flush stdout
198 if {[catch {uplevel #0 "$cmd;\n"} result]} {
199 puts "\nError: $result"
200 incr nErr
drha1b351a2001-09-14 16:42:12 +0000201 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000202 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000203 } elseif {[string compare $result $expected]} {
204 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
205 incr nErr
drha1b351a2001-09-14 16:42:12 +0000206 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000207 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000208 } else {
209 puts " Ok"
210 }
drh6558db82007-04-13 03:23:21 +0000211 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000212 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000213 sqlite3_instvfs marker binarylog "End of $name"
214 }
drh348784e2000-05-29 20:41:49 +0000215}
216
drhb62c3352006-11-23 09:39:16 +0000217# Run an SQL script.
218# Return the number of microseconds per statement.
219#
drh3590f152006-11-23 21:09:10 +0000220proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000221 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000222 flush stdout
223 set speed [time {sqlite3_exec_nr db $sql}]
224 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000225 if {$tm == 0} {
226 set rate [format %20s "many"]
227 } else {
228 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
229 }
drh3590f152006-11-23 21:09:10 +0000230 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000231 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000232 global total_time
233 set total_time [expr {$total_time+$tm}]
234}
drh45c236d2008-03-22 01:08:00 +0000235proc speed_trial_tcl {name numstmt units script} {
236 puts -nonewline [format {%-21.21s } $name...]
237 flush stdout
238 set speed [time {eval $script}]
239 set tm [lindex $speed 0]
240 if {$tm == 0} {
241 set rate [format %20s "many"]
242 } else {
243 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
244 }
245 set u2 $units/s
246 puts [format {%12d uS %s %s} $tm $rate $u2]
247 global total_time
248 set total_time [expr {$total_time+$tm}]
249}
drhdd924312007-03-31 22:34:16 +0000250proc speed_trial_init {name} {
251 global total_time
252 set total_time 0
253}
254proc speed_trial_summary {name} {
255 global total_time
256 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000257}
258
drh348784e2000-05-29 20:41:49 +0000259# Run this routine last
260#
261proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000262 finalize_testing
263}
264proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000265 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000266
drh6e142f52000-06-08 13:36:40 +0000267 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000268 catch {db2 close}
269 catch {db3 close}
270
drh9bc54492007-10-23 14:49:59 +0000271 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000272 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000273 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000274 db close
drh984bfaa2008-03-19 16:08:53 +0000275 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000276 set heaplimit [sqlite3_soft_heap_limit]
277 if {$heaplimit!=$::soft_limit} {
278 puts "soft-heap-limit changed by this script\
279 from $::soft_limit to $heaplimit"
280 } elseif {$heaplimit!="" && $heaplimit>0} {
281 puts "soft-heap-limit set to $heaplimit"
282 }
283 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000284 incr nTest
drh348784e2000-05-29 20:41:49 +0000285 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000286 if {$nErr>0} {
287 puts "Failures on these tests: $::failList"
288 }
drh521cc842008-04-15 02:36:33 +0000289 if {[llength $omitList]>0} {
290 puts "Omitted test cases:"
291 set prec {}
292 foreach {rec} [lsort $omitList] {
293 if {$rec==$prec} continue
294 set prec $rec
295 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
296 }
297 }
drh80788d82006-09-02 14:50:23 +0000298 if {$nErr>0 && ![working_64bit_int]} {
299 puts "******************************************************************"
300 puts "N.B.: The version of TCL that you used to build this test harness"
301 puts "is defective in that it does not support 64-bit integers. Some or"
302 puts "all of the test failures above might be a result from this defect"
303 puts "in your TCL build."
304 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000305 }
danielk1977374177e2008-05-08 15:58:06 +0000306 if {[info exists ::tester_do_binarylog]} {
307 sqlite3_instvfs destroy binarylog
308 }
drh94e92032003-02-16 22:21:32 +0000309 if {$sqlite_open_file_count} {
310 puts "$sqlite_open_file_count files were left open"
311 incr nErr
312 }
danielk19771e21fd52008-04-10 17:27:38 +0000313 if {[info exists ::tester_do_ostrace]} {
314 puts "Writing ostrace.sql..."
315 set fd $::ostrace_fd
316
317 puts -nonewline $fd "CREATE TABLE ossummary"
318 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
319 foreach row [sqlite3_instvfs report ostrace] {
320 foreach {method count clicks} $row break
321 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
322 }
323 puts $fd "COMMIT;"
324 close $fd
325 sqlite3_instvfs destroy ostrace
326 }
drhf3a65f72007-08-22 20:18:21 +0000327 if {[sqlite3_memory_used]>0} {
328 puts "Unfreed memory: [sqlite3_memory_used] bytes"
329 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000330 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000331 puts "Writing unfreed memory log to \"./memleak.txt\""
332 sqlite3_memdebug_dump ./memleak.txt
333 }
drhf3a65f72007-08-22 20:18:21 +0000334 } else {
335 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000336 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000337 sqlite3_memdebug_dump ./memusage.txt
338 }
drhf3a65f72007-08-22 20:18:21 +0000339 }
drhf7141992008-06-19 00:16:08 +0000340 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000341 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
342 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000343 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
344 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
345 }
danielk197735754ac2008-03-21 17:29:37 +0000346 if {[info exists ::tester_do_malloctrace]} {
347 puts "Writing mallocs.sql..."
348 memdebug_log_sql
349 sqlite3_memdebug_log stop
350 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000351
352 if {[sqlite3_memory_used]>0} {
353 puts "Writing leaks.sql..."
354 sqlite3_memdebug_log sync
355 memdebug_log_sql leaks.sql
356 }
danielk197735754ac2008-03-21 17:29:37 +0000357 }
drhd9910fe2006-10-04 11:55:49 +0000358 foreach f [glob -nocomplain test.db-*-journal] {
359 file delete -force $f
360 }
361 foreach f [glob -nocomplain test.db-mj*] {
362 file delete -force $f
363 }
drhdb25e382001-03-15 18:21:22 +0000364 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000365}
366
drhf7141992008-06-19 00:16:08 +0000367# Display memory statistics for analysis and debugging purposes.
368#
369proc show_memstats {} {
370 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
371 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
372 puts "Memory used: $val"
373 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
374 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
375 puts "Page-cache used: $val"
376 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
377 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
378 puts "Page-cache overflow: $val"
379 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
380 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
381 puts "Scratch memory used: $val"
382 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
383 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
384 puts "Scratch overflow: $val"
385 set x [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
386 puts "Maximum alloc size: [lindex $x 2]"
387}
388
drh348784e2000-05-29 20:41:49 +0000389# A procedure to execute SQL
390#
drhc4a3c772001-04-04 11:48:57 +0000391proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000392 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000393 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000394}
drh3aadb2e2000-05-31 17:59:25 +0000395
drhadbca9c2001-09-27 15:11:53 +0000396# Execute SQL and catch exceptions.
397#
398proc catchsql {sql {db db}} {
399 # puts "SQL = $sql"
400 set r [catch {$db eval $sql} msg]
401 lappend r $msg
402 return $r
403}
404
drh04096482001-11-09 22:41:44 +0000405# Do an VDBE code dump on the SQL given
406#
407proc explain {sql {db db}} {
408 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000409 puts "addr opcode p1 p2 p3 p4 p5 #"
410 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000411 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000412 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
413 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
414 ]
drh04096482001-11-09 22:41:44 +0000415 }
416}
417
drhdafc0ce2008-04-17 19:14:02 +0000418# Show the VDBE program for an SQL statement but omit the Trace
419# opcode at the beginning. This procedure can be used to prove
420# that different SQL statements generate exactly the same VDBE code.
421#
422proc explain_no_trace {sql} {
423 set tr [db eval "EXPLAIN $sql"]
424 return [lrange $tr 7 end]
425}
426
drh3aadb2e2000-05-31 17:59:25 +0000427# Another procedure to execute SQL. This one includes the field
428# names in the returned list.
429#
430proc execsql2 {sql} {
431 set result {}
432 db eval $sql data {
433 foreach f $data(*) {
434 lappend result $f $data($f)
435 }
436 }
437 return $result
438}
drh17a68932001-01-31 13:28:08 +0000439
drhdde85d92003-03-01 19:45:34 +0000440# Use the non-callback API to execute multiple SQL statements
441#
442proc stepsql {dbptr sql} {
443 set sql [string trim $sql]
444 set r 0
445 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000446 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000447 return [list 1 $vm]
448 }
449 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000450# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
451# foreach v $VAL {lappend r $v}
452# }
453 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
454 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
455 lappend r [sqlite3_column_text $vm $i]
456 }
drhdde85d92003-03-01 19:45:34 +0000457 }
danielk1977106bb232004-05-21 10:08:53 +0000458 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000459 return [list 1 $errmsg]
460 }
461 }
462 return $r
463}
464
drh17a68932001-01-31 13:28:08 +0000465# Delete a file or directory
466#
467proc forcedelete {filename} {
468 if {[catch {file delete -force $filename}]} {
469 exec rm -rf $filename
470 }
471}
drh21504322002-06-25 13:16:02 +0000472
473# Do an integrity check of the entire database
474#
475proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000476 ifcapable integrityck {
477 do_test $name {
478 execsql {PRAGMA integrity_check}
479 } {ok}
480 }
481}
482
danielk1977db4e8862008-01-16 08:24:46 +0000483proc fix_ifcapable_expr {expr} {
484 set ret ""
485 set state 0
486 for {set i 0} {$i < [string length $expr]} {incr i} {
487 set char [string range $expr $i $i]
488 set newstate [expr {[string is alnum $char] || $char eq "_"}]
489 if {$newstate && !$state} {
490 append ret {$::sqlite_options(}
491 }
492 if {!$newstate && $state} {
493 append ret )
494 }
495 append ret $char
496 set state $newstate
497 }
498 if {$state} {append ret )}
499 return $ret
500}
501
drh27d258a2004-10-30 20:23:09 +0000502# Evaluate a boolean expression of capabilities. If true, execute the
503# code. Omit the code if false.
504#
danielk19773e8c37e2005-01-21 03:12:14 +0000505proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000506 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
507 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000508 if ($e2) {
509 set c [catch {uplevel 1 $code} r]
510 } else {
511 set c [catch {uplevel 1 $elsecode} r]
512 }
513 return -code $c $r
drh21504322002-06-25 13:16:02 +0000514}
danielk197745901d62004-11-10 15:27:38 +0000515
danielk1977aca790a2005-01-13 11:07:52 +0000516# This proc execs a seperate process that crashes midway through executing
517# the SQL script $sql on database test.db.
518#
519# The crash occurs during a sync() of file $crashfile. When the crash
520# occurs a random subset of all unsynced writes made by the process are
521# written into the files on disk. Argument $crashdelay indicates the
522# number of file syncs to wait before crashing.
523#
524# The return value is a list of two elements. The first element is a
525# boolean, indicating whether or not the process actually crashed or
526# reported some other error. The second element in the returned list is the
527# error message. This is "child process exited abnormally" if the crash
528# occured.
529#
danielk1977f8940ae2007-08-23 11:07:10 +0000530# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000531#
532proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000533 if {$::tcl_platform(platform)!="unix"} {
534 error "crashsql should only be used on unix"
535 }
danielk197759a33f92007-03-17 10:26:59 +0000536
537 set blocksize ""
538 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000539 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000540 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000541 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000542 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000543 set sql [lindex $args end]
544
545 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
546 set z [lindex $args $ii]
547 set n [string length $z]
548 set z2 [lindex $args [expr $ii+1]]
549
550 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000551 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000552 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000553 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000554 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000555 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000556 else { error "Unrecognized option: $z" }
557 }
558
559 if {$crashfile eq ""} {
560 error "Compulsory option -file missing"
561 }
562
danielk1977aca790a2005-01-13 11:07:52 +0000563 set cfile [file join [pwd] $crashfile]
564
565 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000566 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000567 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000568 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000569 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000570
571 # This block sets the cache size of the main database to 10
572 # pages. This is done in case the build is configured to omit
573 # "PRAGMA cache_size".
574 puts $f {db eval {SELECT * FROM sqlite_master;}}
575 puts $f {set bt [btree_from_db db]}
576 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000577 if {$prngseed} {
578 set seed [expr {$prngseed%10007+1}]
579 # puts seed=$seed
580 puts $f "db eval {SELECT randomblob($seed)}"
581 }
danielk1977933bbd62007-03-17 07:22:42 +0000582
drhf8587402008-01-08 16:03:49 +0000583 if {[string length $tclbody]>0} {
584 puts $f $tclbody
585 }
586 if {[string length $sql]>0} {
587 puts $f "db eval {"
588 puts $f "$sql"
589 puts $f "}"
590 }
danielk1977aca790a2005-01-13 11:07:52 +0000591 close $f
592
593 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000594 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000595 } msg]
596 lappend r $msg
597}
598
danielk197732554c12005-01-22 03:39:39 +0000599# Usage: do_ioerr_test <test number> <options...>
600#
601# This proc is used to implement test cases that check that IO errors
602# are correctly handled. The first argument, <test number>, is an integer
603# used to name the tests executed by this proc. Options are as follows:
604#
605# -tclprep TCL script to run to prepare test.
606# -sqlprep SQL script to run to prepare test.
607# -tclbody TCL script to run with IO error simulation.
608# -sqlbody TCL script to run with IO error simulation.
609# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000610# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000611# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000612# -start Value of 'N' to begin with (default 1)
613#
614# -cksum Boolean. If true, test that the database does
615# not change during the execution of the test case.
616#
617proc do_ioerr_test {testname args} {
618
danielk197732554c12005-01-22 03:39:39 +0000619 set ::ioerropts(-start) 1
620 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000621 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000622 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000623 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000624 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000625 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000626 array set ::ioerropts $args
627
danielk197747cd39c2008-05-12 12:41:15 +0000628 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
629 # a couple of obscure IO errors that do not return them.
630 set ::ioerropts(-erc) 0
631
danielk197732554c12005-01-22 03:39:39 +0000632 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000633 #reset_prng_state
634 save_prng_state
danielk1977861f7452008-06-05 11:39:11 +0000635 for {set n $::ioerropts(-start)} {$::go && $n<200} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000636 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000637 incr ::ioerropts(-count) -1
638 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000639
640 # Skip this IO error if it was specified with the "-exclude" option.
641 if {[info exists ::ioerropts(-exclude)]} {
642 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
643 }
danielk1977861f7452008-06-05 11:39:11 +0000644 if {$::ioerropts(-restoreprng)} {
645 restore_prng_state
646 }
danielk197732554c12005-01-22 03:39:39 +0000647
648 # Delete the files test.db and test2.db, then execute the TCL and
649 # SQL (in that order) to prepare for the test case.
650 do_test $testname.$n.1 {
651 set ::sqlite_io_error_pending 0
652 catch {db close}
653 catch {file delete -force test.db}
654 catch {file delete -force test.db-journal}
655 catch {file delete -force test2.db}
656 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000657 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000658 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000659 if {[info exists ::ioerropts(-tclprep)]} {
660 eval $::ioerropts(-tclprep)
661 }
662 if {[info exists ::ioerropts(-sqlprep)]} {
663 execsql $::ioerropts(-sqlprep)
664 }
665 expr 0
666 } {0}
667
668 # Read the 'checksum' of the database.
669 if {$::ioerropts(-cksum)} {
670 set checksum [cksum]
671 }
drh93aed5a2008-01-16 17:46:38 +0000672
danielk197732554c12005-01-22 03:39:39 +0000673 # Set the Nth IO error to fail.
674 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000675 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000676 set ::sqlite_io_error_pending $n
677 }] $n
678
679 # Create a single TCL script from the TCL and SQL specified
680 # as the body of the test.
681 set ::ioerrorbody {}
682 if {[info exists ::ioerropts(-tclbody)]} {
683 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
684 }
685 if {[info exists ::ioerropts(-sqlbody)]} {
686 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
687 }
688
689 # Execute the TCL Script created in the above block. If
690 # there are at least N IO operations performed by SQLite as
691 # a result of the script, the Nth will fail.
692 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000693 set ::sqlite_io_error_hit 0
694 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000695 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000696 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000697 set rc [sqlite3_errcode $::DB]
698 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000699 # If we are in extended result code mode, make sure all of the
700 # IOERRs we get back really do have their extended code values.
701 # If an extended result code is returned, the sqlite3_errcode
702 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
703 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000704 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
705 return $rc
706 }
707 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000708 # If we are not in extended result code mode, make sure no
709 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000710 if {[regexp {\+\d} $rc]} {
711 return $rc
712 }
713 }
drh93aed5a2008-01-16 17:46:38 +0000714 # The test repeats as long as $::go is non-zero. $::go starts out
715 # as 1. When a test runs to completion without hitting an I/O
716 # error, that means there is no point in continuing with this test
717 # case so set $::go to zero.
718 #
719 if {$::sqlite_io_error_pending>0} {
720 set ::go 0
721 set q 0
722 set ::sqlite_io_error_pending 0
723 } else {
724 set q 1
725 }
726
drhc9ac5ca2005-11-04 22:03:30 +0000727 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000728 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
729 set r 1
730 }
danielk1977f2fa8312006-01-24 13:09:33 +0000731 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000732
733 # One of two things must have happened. either
734 # 1. We never hit the IO error and the SQL returned OK
735 # 2. An IO error was hit and the SQL failed
736 #
drh93aed5a2008-01-16 17:46:38 +0000737 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000738 } {1}
739
danielk197780daec62008-05-12 10:57:02 +0000740 set ::sqlite_io_error_hit 0
741 set ::sqlite_io_error_pending 0
742
danielk197752b472a2008-05-05 16:23:55 +0000743 # Check that no page references were leaked. There should be
744 # a single reference if there is still an active transaction,
745 # or zero otherwise.
746 #
danielk197781620542008-06-07 05:19:37 +0000747 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
748 # locks are established on database files (i.e. if the error
749 # occurs while attempting to detect a hot-journal file), then
750 # there may 0 page references and an active transaction according
751 # to [sqlite3_get_autocommit].
752 #
danielk19774abd5442008-05-05 15:26:50 +0000753 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000754 do_test $testname.$n.4 {
755 set bt [btree_from_db db]
756 db_enter db
757 array set stats [btree_pager_stats $bt]
758 db_leave db
danielk197781620542008-06-07 05:19:37 +0000759 set nRef $stats(ref)
760 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
761 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000762 }
763
danielk197752b472a2008-05-05 16:23:55 +0000764 # If there is an open database handle and no open transaction,
765 # and the pager is not running in exclusive-locking mode,
766 # check that the pager is in "unlocked" state. Theoretically,
767 # if a call to xUnlock() failed due to an IO error the underlying
768 # file may still be locked.
769 #
770 ifcapable pragma {
771 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000772 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000773 && [db one {pragma locking_mode}] eq "normal"
774 && [sqlite3_get_autocommit db]
775 } {
776 do_test $testname.$n.5 {
777 set bt [btree_from_db db]
778 db_enter db
779 array set stats [btree_pager_stats $bt]
780 db_leave db
781 set stats(state)
782 } 0
783 }
784 }
785
danielk197732554c12005-01-22 03:39:39 +0000786 # If an IO error occured, then the checksum of the database should
787 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000788 #
drh1aa5af12008-03-07 19:51:14 +0000789 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000790 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000791 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000792 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000793 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000794 cksum
795 } $checksum
796 }
797
drh1aa5af12008-03-07 19:51:14 +0000798 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000799 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000800 if {[info exists ::ioerropts(-cleanup)]} {
801 catch $::ioerropts(-cleanup)
802 }
danielk197732554c12005-01-22 03:39:39 +0000803 }
804 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000805 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000806 unset ::ioerropts
807}
808
drh93aed5a2008-01-16 17:46:38 +0000809# Return a checksum based on the contents of the main database associated
810# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000811#
812proc cksum {{db db}} {
813 set txt [$db eval {
814 SELECT name, type, sql FROM sqlite_master order by name
815 }]\n
816 foreach tbl [$db eval {
817 SELECT name FROM sqlite_master WHERE type='table' order by name
818 }] {
819 append txt [$db eval "SELECT * FROM $tbl"]\n
820 }
821 foreach prag {default_synchronous default_cache_size} {
822 append txt $prag-[$db eval "PRAGMA $prag"]\n
823 }
824 set cksum [string length $txt]-[md5 $txt]
825 # puts $cksum-[file size test.db]
826 return $cksum
827}
828
drh93aed5a2008-01-16 17:46:38 +0000829# Generate a checksum based on the contents of the main and temp tables
830# database $db. If the checksum of two databases is the same, and the
831# integrity-check passes for both, the two databases are identical.
832#
drh1db639c2008-01-17 02:36:28 +0000833proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000834 set ret [list]
835 ifcapable tempdb {
836 set sql {
837 SELECT name FROM sqlite_master WHERE type = 'table' UNION
838 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
839 SELECT 'sqlite_master' UNION
840 SELECT 'sqlite_temp_master' ORDER BY 1
841 }
842 } else {
843 set sql {
844 SELECT name FROM sqlite_master WHERE type = 'table' UNION
845 SELECT 'sqlite_master' ORDER BY 1
846 }
847 }
848 set tbllist [$db eval $sql]
849 set txt {}
850 foreach tbl $tbllist {
851 append txt [$db eval "SELECT * FROM $tbl"]
852 }
853 foreach prag {default_cache_size} {
854 append txt $prag-[$db eval "PRAGMA $prag"]\n
855 }
856 # puts txt=$txt
857 return [md5 $txt]
858}
859
danielk197735754ac2008-03-21 17:29:37 +0000860proc memdebug_log_sql {{filename mallocs.sql}} {
861
danielk19776f332c12008-03-21 14:22:44 +0000862 set data [sqlite3_memdebug_log dump]
863 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000864 if {$nFrame < 0} { return "" }
865
danielk197735754ac2008-03-21 17:29:37 +0000866 set database temp
867
danielk19776f332c12008-03-21 14:22:44 +0000868 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
869 for {set ii 1} {$ii <= $nFrame} {incr ii} {
870 append tbl ", f${ii}"
871 }
872 append tbl ");\n"
873
874 set sql ""
875 foreach e $data {
876 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
877 foreach f [lrange $e 2 end] {
878 set frames($f) 1
879 }
880 }
881
882 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000883 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000884
885 foreach f [array names frames] {
886 set addr [format %x $f]
887 set cmd "addr2line -e [info nameofexec] $addr"
888 set line [eval exec $cmd]
889 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000890
891 set file [lindex [split $line :] 0]
892 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000893 }
894
danielk197735754ac2008-03-21 17:29:37 +0000895 foreach f [array names files] {
896 set contents ""
897 catch {
898 set fd [open $f]
899 set contents [read $fd]
900 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000901 }
danielk197735754ac2008-03-21 17:29:37 +0000902 set contents [string map {' ''} $contents]
903 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000904 }
danielk19776f332c12008-03-21 14:22:44 +0000905
danielk197735754ac2008-03-21 17:29:37 +0000906 set fd [open $filename w]
907 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
908 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000909}
drh93aed5a2008-01-16 17:46:38 +0000910
danielk197732554c12005-01-22 03:39:39 +0000911# Copy file $from into $to. This is used because some versions of
912# TCL for windows (notably the 8.4.1 binary package shipped with the
913# current mingw release) have a broken "file copy" command.
914#
915proc copy_file {from to} {
916 if {$::tcl_platform(platform)=="unix"} {
917 file copy -force $from $to
918 } else {
919 set f [open $from]
920 fconfigure $f -translation binary
921 set t [open $to w]
922 fconfigure $t -translation binary
923 puts -nonewline $t [read $f [file size $from]]
924 close $t
925 close $f
926 }
927}
928
danielk197745901d62004-11-10 15:27:38 +0000929# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
930# to non-zero, then set the global variable $AUTOVACUUM to 1.
931set AUTOVACUUM $sqlite_options(default_autovacuum)