blob: f901df05ff57ac8297447c06a581a14ec188f9c0 [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#
danielk1977f7b9d662008-06-23 18:49:43 +000014# $Id: tester.tcl,v 1.128 2008/06/23 18:49:45 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
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#
danielk1977f7b9d662008-06-23 18:49:43 +0000136if {![info exists nTest]} {
137 sqlite3_shutdown
138 install_malloc_faultsim 1
139 sqlite3_initialize
140}
drh254cba22001-09-20 01:44:42 +0000141catch {db close}
142file delete -force test.db
143file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000144sqlite3 db ./test.db
145set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000146if {[info exists ::SETUP_SQL]} {
147 db eval $::SETUP_SQL
148}
drhbec2bf42000-05-29 23:48:22 +0000149
150# Abort early if this script has been run before.
151#
152if {[info exists nTest]} return
153
154# Set the test counters to zero
155#
drh348784e2000-05-29 20:41:49 +0000156set nErr 0
157set nTest 0
drh767c2002000-10-19 14:10:08 +0000158set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000159set failList {}
drh521cc842008-04-15 02:36:33 +0000160set omitList {}
drhb62c3352006-11-23 09:39:16 +0000161if {![info exists speedTest]} {
162 set speedTest 0
163}
drh348784e2000-05-29 20:41:49 +0000164
drh521cc842008-04-15 02:36:33 +0000165# Record the fact that a sequence of tests were omitted.
166#
167proc omit_test {name reason} {
168 global omitList
169 lappend omitList [list $name $reason]
170}
171
drh348784e2000-05-29 20:41:49 +0000172# Invoke the do_test procedure to run a single test
173#
174proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000175 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000176 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000177 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000178 sqlite3_instvfs marker binarylog "Start of $name"
179 }
drh767c2002000-10-19 14:10:08 +0000180 if {$skip_test} {
181 set skip_test 0
182 return
183 }
184 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000185 set go 1
186 } else {
187 set go 0
188 foreach pattern $argv {
189 if {[string match $pattern $name]} {
190 set go 1
191 break
192 }
193 }
194 }
195 if {!$go} return
196 incr nTest
drh5edc3122001-09-13 21:53:09 +0000197 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000198 flush stdout
199 if {[catch {uplevel #0 "$cmd;\n"} result]} {
200 puts "\nError: $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 } elseif {[string compare $result $expected]} {
205 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
206 incr nErr
drha1b351a2001-09-14 16:42:12 +0000207 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000208 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000209 } else {
210 puts " Ok"
211 }
drh6558db82007-04-13 03:23:21 +0000212 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000213 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000214 sqlite3_instvfs marker binarylog "End of $name"
215 }
drh348784e2000-05-29 20:41:49 +0000216}
217
drhb62c3352006-11-23 09:39:16 +0000218# Run an SQL script.
219# Return the number of microseconds per statement.
220#
drh3590f152006-11-23 21:09:10 +0000221proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000222 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000223 flush stdout
224 set speed [time {sqlite3_exec_nr db $sql}]
225 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000226 if {$tm == 0} {
227 set rate [format %20s "many"]
228 } else {
229 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
230 }
drh3590f152006-11-23 21:09:10 +0000231 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000232 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000233 global total_time
234 set total_time [expr {$total_time+$tm}]
235}
drh45c236d2008-03-22 01:08:00 +0000236proc speed_trial_tcl {name numstmt units script} {
237 puts -nonewline [format {%-21.21s } $name...]
238 flush stdout
239 set speed [time {eval $script}]
240 set tm [lindex $speed 0]
241 if {$tm == 0} {
242 set rate [format %20s "many"]
243 } else {
244 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
245 }
246 set u2 $units/s
247 puts [format {%12d uS %s %s} $tm $rate $u2]
248 global total_time
249 set total_time [expr {$total_time+$tm}]
250}
drhdd924312007-03-31 22:34:16 +0000251proc speed_trial_init {name} {
252 global total_time
253 set total_time 0
254}
255proc speed_trial_summary {name} {
256 global total_time
257 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000258}
259
drh348784e2000-05-29 20:41:49 +0000260# Run this routine last
261#
262proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000263 finalize_testing
264}
265proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000266 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000267
drh6e142f52000-06-08 13:36:40 +0000268 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000269 catch {db2 close}
270 catch {db3 close}
271
drh9bc54492007-10-23 14:49:59 +0000272 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000273 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000274 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000275 db close
drh984bfaa2008-03-19 16:08:53 +0000276 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000277 set heaplimit [sqlite3_soft_heap_limit]
278 if {$heaplimit!=$::soft_limit} {
279 puts "soft-heap-limit changed by this script\
280 from $::soft_limit to $heaplimit"
281 } elseif {$heaplimit!="" && $heaplimit>0} {
282 puts "soft-heap-limit set to $heaplimit"
283 }
284 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000285 incr nTest
drh348784e2000-05-29 20:41:49 +0000286 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000287 if {$nErr>0} {
288 puts "Failures on these tests: $::failList"
289 }
drh521cc842008-04-15 02:36:33 +0000290 if {[llength $omitList]>0} {
291 puts "Omitted test cases:"
292 set prec {}
293 foreach {rec} [lsort $omitList] {
294 if {$rec==$prec} continue
295 set prec $rec
296 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
297 }
298 }
drh80788d82006-09-02 14:50:23 +0000299 if {$nErr>0 && ![working_64bit_int]} {
300 puts "******************************************************************"
301 puts "N.B.: The version of TCL that you used to build this test harness"
302 puts "is defective in that it does not support 64-bit integers. Some or"
303 puts "all of the test failures above might be a result from this defect"
304 puts "in your TCL build."
305 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000306 }
danielk1977374177e2008-05-08 15:58:06 +0000307 if {[info exists ::tester_do_binarylog]} {
308 sqlite3_instvfs destroy binarylog
309 }
drh94e92032003-02-16 22:21:32 +0000310 if {$sqlite_open_file_count} {
311 puts "$sqlite_open_file_count files were left open"
312 incr nErr
313 }
danielk19771e21fd52008-04-10 17:27:38 +0000314 if {[info exists ::tester_do_ostrace]} {
315 puts "Writing ostrace.sql..."
316 set fd $::ostrace_fd
317
318 puts -nonewline $fd "CREATE TABLE ossummary"
319 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
320 foreach row [sqlite3_instvfs report ostrace] {
321 foreach {method count clicks} $row break
322 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
323 }
324 puts $fd "COMMIT;"
325 close $fd
326 sqlite3_instvfs destroy ostrace
327 }
drhf3a65f72007-08-22 20:18:21 +0000328 if {[sqlite3_memory_used]>0} {
329 puts "Unfreed memory: [sqlite3_memory_used] bytes"
330 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000331 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000332 puts "Writing unfreed memory log to \"./memleak.txt\""
333 sqlite3_memdebug_dump ./memleak.txt
334 }
drhf3a65f72007-08-22 20:18:21 +0000335 } else {
336 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000337 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000338 sqlite3_memdebug_dump ./memusage.txt
339 }
drhf3a65f72007-08-22 20:18:21 +0000340 }
drhf7141992008-06-19 00:16:08 +0000341 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000342 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
343 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000344 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
345 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
346 }
danielk197735754ac2008-03-21 17:29:37 +0000347 if {[info exists ::tester_do_malloctrace]} {
348 puts "Writing mallocs.sql..."
349 memdebug_log_sql
350 sqlite3_memdebug_log stop
351 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000352
353 if {[sqlite3_memory_used]>0} {
354 puts "Writing leaks.sql..."
355 sqlite3_memdebug_log sync
356 memdebug_log_sql leaks.sql
357 }
danielk197735754ac2008-03-21 17:29:37 +0000358 }
drhd9910fe2006-10-04 11:55:49 +0000359 foreach f [glob -nocomplain test.db-*-journal] {
360 file delete -force $f
361 }
362 foreach f [glob -nocomplain test.db-mj*] {
363 file delete -force $f
364 }
drhdb25e382001-03-15 18:21:22 +0000365 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000366}
367
drhf7141992008-06-19 00:16:08 +0000368# Display memory statistics for analysis and debugging purposes.
369#
370proc show_memstats {} {
371 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
372 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
373 puts "Memory used: $val"
374 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
375 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
376 puts "Page-cache used: $val"
377 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
378 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
379 puts "Page-cache overflow: $val"
380 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
381 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
382 puts "Scratch memory used: $val"
383 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
384 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
385 puts "Scratch overflow: $val"
386 set x [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
387 puts "Maximum alloc size: [lindex $x 2]"
388}
389
drh348784e2000-05-29 20:41:49 +0000390# A procedure to execute SQL
391#
drhc4a3c772001-04-04 11:48:57 +0000392proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000393 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000394 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000395}
drh3aadb2e2000-05-31 17:59:25 +0000396
drhadbca9c2001-09-27 15:11:53 +0000397# Execute SQL and catch exceptions.
398#
399proc catchsql {sql {db db}} {
400 # puts "SQL = $sql"
401 set r [catch {$db eval $sql} msg]
402 lappend r $msg
403 return $r
404}
405
drh04096482001-11-09 22:41:44 +0000406# Do an VDBE code dump on the SQL given
407#
408proc explain {sql {db db}} {
409 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000410 puts "addr opcode p1 p2 p3 p4 p5 #"
411 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000412 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000413 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
414 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
415 ]
drh04096482001-11-09 22:41:44 +0000416 }
417}
418
drhdafc0ce2008-04-17 19:14:02 +0000419# Show the VDBE program for an SQL statement but omit the Trace
420# opcode at the beginning. This procedure can be used to prove
421# that different SQL statements generate exactly the same VDBE code.
422#
423proc explain_no_trace {sql} {
424 set tr [db eval "EXPLAIN $sql"]
425 return [lrange $tr 7 end]
426}
427
drh3aadb2e2000-05-31 17:59:25 +0000428# Another procedure to execute SQL. This one includes the field
429# names in the returned list.
430#
431proc execsql2 {sql} {
432 set result {}
433 db eval $sql data {
434 foreach f $data(*) {
435 lappend result $f $data($f)
436 }
437 }
438 return $result
439}
drh17a68932001-01-31 13:28:08 +0000440
drhdde85d92003-03-01 19:45:34 +0000441# Use the non-callback API to execute multiple SQL statements
442#
443proc stepsql {dbptr sql} {
444 set sql [string trim $sql]
445 set r 0
446 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000447 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000448 return [list 1 $vm]
449 }
450 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000451# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
452# foreach v $VAL {lappend r $v}
453# }
454 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
455 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
456 lappend r [sqlite3_column_text $vm $i]
457 }
drhdde85d92003-03-01 19:45:34 +0000458 }
danielk1977106bb232004-05-21 10:08:53 +0000459 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000460 return [list 1 $errmsg]
461 }
462 }
463 return $r
464}
465
drh17a68932001-01-31 13:28:08 +0000466# Delete a file or directory
467#
468proc forcedelete {filename} {
469 if {[catch {file delete -force $filename}]} {
470 exec rm -rf $filename
471 }
472}
drh21504322002-06-25 13:16:02 +0000473
474# Do an integrity check of the entire database
475#
476proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000477 ifcapable integrityck {
478 do_test $name {
479 execsql {PRAGMA integrity_check}
480 } {ok}
481 }
482}
483
danielk1977db4e8862008-01-16 08:24:46 +0000484proc fix_ifcapable_expr {expr} {
485 set ret ""
486 set state 0
487 for {set i 0} {$i < [string length $expr]} {incr i} {
488 set char [string range $expr $i $i]
489 set newstate [expr {[string is alnum $char] || $char eq "_"}]
490 if {$newstate && !$state} {
491 append ret {$::sqlite_options(}
492 }
493 if {!$newstate && $state} {
494 append ret )
495 }
496 append ret $char
497 set state $newstate
498 }
499 if {$state} {append ret )}
500 return $ret
501}
502
drh27d258a2004-10-30 20:23:09 +0000503# Evaluate a boolean expression of capabilities. If true, execute the
504# code. Omit the code if false.
505#
danielk19773e8c37e2005-01-21 03:12:14 +0000506proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000507 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
508 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000509 if ($e2) {
510 set c [catch {uplevel 1 $code} r]
511 } else {
512 set c [catch {uplevel 1 $elsecode} r]
513 }
514 return -code $c $r
drh21504322002-06-25 13:16:02 +0000515}
danielk197745901d62004-11-10 15:27:38 +0000516
danielk1977aca790a2005-01-13 11:07:52 +0000517# This proc execs a seperate process that crashes midway through executing
518# the SQL script $sql on database test.db.
519#
520# The crash occurs during a sync() of file $crashfile. When the crash
521# occurs a random subset of all unsynced writes made by the process are
522# written into the files on disk. Argument $crashdelay indicates the
523# number of file syncs to wait before crashing.
524#
525# The return value is a list of two elements. The first element is a
526# boolean, indicating whether or not the process actually crashed or
527# reported some other error. The second element in the returned list is the
528# error message. This is "child process exited abnormally" if the crash
529# occured.
530#
danielk1977f8940ae2007-08-23 11:07:10 +0000531# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000532#
533proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000534 if {$::tcl_platform(platform)!="unix"} {
535 error "crashsql should only be used on unix"
536 }
danielk197759a33f92007-03-17 10:26:59 +0000537
538 set blocksize ""
539 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000540 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000541 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000542 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000543 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000544 set sql [lindex $args end]
545
546 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
547 set z [lindex $args $ii]
548 set n [string length $z]
549 set z2 [lindex $args [expr $ii+1]]
550
551 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000552 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000553 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000554 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000555 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000556 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000557 else { error "Unrecognized option: $z" }
558 }
559
560 if {$crashfile eq ""} {
561 error "Compulsory option -file missing"
562 }
563
danielk1977aca790a2005-01-13 11:07:52 +0000564 set cfile [file join [pwd] $crashfile]
565
566 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000567 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000568 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000569 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000570 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000571
572 # This block sets the cache size of the main database to 10
573 # pages. This is done in case the build is configured to omit
574 # "PRAGMA cache_size".
575 puts $f {db eval {SELECT * FROM sqlite_master;}}
576 puts $f {set bt [btree_from_db db]}
577 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000578 if {$prngseed} {
579 set seed [expr {$prngseed%10007+1}]
580 # puts seed=$seed
581 puts $f "db eval {SELECT randomblob($seed)}"
582 }
danielk1977933bbd62007-03-17 07:22:42 +0000583
drhf8587402008-01-08 16:03:49 +0000584 if {[string length $tclbody]>0} {
585 puts $f $tclbody
586 }
587 if {[string length $sql]>0} {
588 puts $f "db eval {"
589 puts $f "$sql"
590 puts $f "}"
591 }
danielk1977aca790a2005-01-13 11:07:52 +0000592 close $f
593
594 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000595 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000596 } msg]
597 lappend r $msg
598}
599
danielk197732554c12005-01-22 03:39:39 +0000600# Usage: do_ioerr_test <test number> <options...>
601#
602# This proc is used to implement test cases that check that IO errors
603# are correctly handled. The first argument, <test number>, is an integer
604# used to name the tests executed by this proc. Options are as follows:
605#
606# -tclprep TCL script to run to prepare test.
607# -sqlprep SQL script to run to prepare test.
608# -tclbody TCL script to run with IO error simulation.
609# -sqlbody TCL script to run with IO error simulation.
610# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000611# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000612# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000613# -start Value of 'N' to begin with (default 1)
614#
615# -cksum Boolean. If true, test that the database does
616# not change during the execution of the test case.
617#
618proc do_ioerr_test {testname args} {
619
danielk197732554c12005-01-22 03:39:39 +0000620 set ::ioerropts(-start) 1
621 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000622 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000623 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000624 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000625 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000626 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000627 array set ::ioerropts $args
628
danielk197747cd39c2008-05-12 12:41:15 +0000629 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
630 # a couple of obscure IO errors that do not return them.
631 set ::ioerropts(-erc) 0
632
danielk197732554c12005-01-22 03:39:39 +0000633 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000634 #reset_prng_state
635 save_prng_state
danielk1977861f7452008-06-05 11:39:11 +0000636 for {set n $::ioerropts(-start)} {$::go && $n<200} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000637 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000638 incr ::ioerropts(-count) -1
639 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000640
641 # Skip this IO error if it was specified with the "-exclude" option.
642 if {[info exists ::ioerropts(-exclude)]} {
643 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
644 }
danielk1977861f7452008-06-05 11:39:11 +0000645 if {$::ioerropts(-restoreprng)} {
646 restore_prng_state
647 }
danielk197732554c12005-01-22 03:39:39 +0000648
649 # Delete the files test.db and test2.db, then execute the TCL and
650 # SQL (in that order) to prepare for the test case.
651 do_test $testname.$n.1 {
652 set ::sqlite_io_error_pending 0
653 catch {db close}
654 catch {file delete -force test.db}
655 catch {file delete -force test.db-journal}
656 catch {file delete -force test2.db}
657 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000658 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000659 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000660 if {[info exists ::ioerropts(-tclprep)]} {
661 eval $::ioerropts(-tclprep)
662 }
663 if {[info exists ::ioerropts(-sqlprep)]} {
664 execsql $::ioerropts(-sqlprep)
665 }
666 expr 0
667 } {0}
668
669 # Read the 'checksum' of the database.
670 if {$::ioerropts(-cksum)} {
671 set checksum [cksum]
672 }
drh93aed5a2008-01-16 17:46:38 +0000673
danielk197732554c12005-01-22 03:39:39 +0000674 # Set the Nth IO error to fail.
675 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000676 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000677 set ::sqlite_io_error_pending $n
678 }] $n
679
680 # Create a single TCL script from the TCL and SQL specified
681 # as the body of the test.
682 set ::ioerrorbody {}
683 if {[info exists ::ioerropts(-tclbody)]} {
684 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
685 }
686 if {[info exists ::ioerropts(-sqlbody)]} {
687 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
688 }
689
690 # Execute the TCL Script created in the above block. If
691 # there are at least N IO operations performed by SQLite as
692 # a result of the script, the Nth will fail.
693 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000694 set ::sqlite_io_error_hit 0
695 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000696 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000697 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000698 set rc [sqlite3_errcode $::DB]
699 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000700 # If we are in extended result code mode, make sure all of the
701 # IOERRs we get back really do have their extended code values.
702 # If an extended result code is returned, the sqlite3_errcode
703 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
704 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000705 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
706 return $rc
707 }
708 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000709 # If we are not in extended result code mode, make sure no
710 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000711 if {[regexp {\+\d} $rc]} {
712 return $rc
713 }
714 }
drh93aed5a2008-01-16 17:46:38 +0000715 # The test repeats as long as $::go is non-zero. $::go starts out
716 # as 1. When a test runs to completion without hitting an I/O
717 # error, that means there is no point in continuing with this test
718 # case so set $::go to zero.
719 #
720 if {$::sqlite_io_error_pending>0} {
721 set ::go 0
722 set q 0
723 set ::sqlite_io_error_pending 0
724 } else {
725 set q 1
726 }
727
drhc9ac5ca2005-11-04 22:03:30 +0000728 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000729 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
730 set r 1
731 }
danielk1977f2fa8312006-01-24 13:09:33 +0000732 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000733
734 # One of two things must have happened. either
735 # 1. We never hit the IO error and the SQL returned OK
736 # 2. An IO error was hit and the SQL failed
737 #
drh93aed5a2008-01-16 17:46:38 +0000738 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000739 } {1}
740
danielk197780daec62008-05-12 10:57:02 +0000741 set ::sqlite_io_error_hit 0
742 set ::sqlite_io_error_pending 0
743
danielk197752b472a2008-05-05 16:23:55 +0000744 # Check that no page references were leaked. There should be
745 # a single reference if there is still an active transaction,
746 # or zero otherwise.
747 #
danielk197781620542008-06-07 05:19:37 +0000748 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
749 # locks are established on database files (i.e. if the error
750 # occurs while attempting to detect a hot-journal file), then
751 # there may 0 page references and an active transaction according
752 # to [sqlite3_get_autocommit].
753 #
danielk19774abd5442008-05-05 15:26:50 +0000754 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000755 do_test $testname.$n.4 {
756 set bt [btree_from_db db]
757 db_enter db
758 array set stats [btree_pager_stats $bt]
759 db_leave db
danielk197781620542008-06-07 05:19:37 +0000760 set nRef $stats(ref)
761 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
762 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000763 }
764
danielk197752b472a2008-05-05 16:23:55 +0000765 # If there is an open database handle and no open transaction,
766 # and the pager is not running in exclusive-locking mode,
767 # check that the pager is in "unlocked" state. Theoretically,
768 # if a call to xUnlock() failed due to an IO error the underlying
769 # file may still be locked.
770 #
771 ifcapable pragma {
772 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000773 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000774 && [db one {pragma locking_mode}] eq "normal"
775 && [sqlite3_get_autocommit db]
776 } {
777 do_test $testname.$n.5 {
778 set bt [btree_from_db db]
779 db_enter db
780 array set stats [btree_pager_stats $bt]
781 db_leave db
782 set stats(state)
783 } 0
784 }
785 }
786
danielk197732554c12005-01-22 03:39:39 +0000787 # If an IO error occured, then the checksum of the database should
788 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000789 #
drh1aa5af12008-03-07 19:51:14 +0000790 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000791 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000792 catch {db 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)