blob: 4366dc8e0521e87974e5b70373f534b8cd0304db [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#
drhec424a52008-07-25 15:39:03 +000014# $Id: tester.tcl,v 1.133 2008/07/25 15:39:04 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
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"
drhec424a52008-07-25 15:39:03 +0000385 ifcapable yytrackmaxstackdepth {
386 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
387 set val [format { max %10d} [lindex $x 2]]
388 puts "Parser stack depth: $val"
389 }
drhf7141992008-06-19 00:16:08 +0000390 set x [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
391 puts "Maximum alloc size: [lindex $x 2]"
392}
393
drh348784e2000-05-29 20:41:49 +0000394# A procedure to execute SQL
395#
drhc4a3c772001-04-04 11:48:57 +0000396proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000397 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000398 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000399}
drh3aadb2e2000-05-31 17:59:25 +0000400
drhadbca9c2001-09-27 15:11:53 +0000401# Execute SQL and catch exceptions.
402#
403proc catchsql {sql {db db}} {
404 # puts "SQL = $sql"
405 set r [catch {$db eval $sql} msg]
406 lappend r $msg
407 return $r
408}
409
drh04096482001-11-09 22:41:44 +0000410# Do an VDBE code dump on the SQL given
411#
412proc explain {sql {db db}} {
413 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000414 puts "addr opcode p1 p2 p3 p4 p5 #"
415 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000416 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000417 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
418 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
419 ]
drh04096482001-11-09 22:41:44 +0000420 }
421}
422
drhdafc0ce2008-04-17 19:14:02 +0000423# Show the VDBE program for an SQL statement but omit the Trace
424# opcode at the beginning. This procedure can be used to prove
425# that different SQL statements generate exactly the same VDBE code.
426#
427proc explain_no_trace {sql} {
428 set tr [db eval "EXPLAIN $sql"]
429 return [lrange $tr 7 end]
430}
431
drh3aadb2e2000-05-31 17:59:25 +0000432# Another procedure to execute SQL. This one includes the field
433# names in the returned list.
434#
435proc execsql2 {sql} {
436 set result {}
437 db eval $sql data {
438 foreach f $data(*) {
439 lappend result $f $data($f)
440 }
441 }
442 return $result
443}
drh17a68932001-01-31 13:28:08 +0000444
drhdde85d92003-03-01 19:45:34 +0000445# Use the non-callback API to execute multiple SQL statements
446#
447proc stepsql {dbptr sql} {
448 set sql [string trim $sql]
449 set r 0
450 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000451 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000452 return [list 1 $vm]
453 }
454 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000455# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
456# foreach v $VAL {lappend r $v}
457# }
458 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
459 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
460 lappend r [sqlite3_column_text $vm $i]
461 }
drhdde85d92003-03-01 19:45:34 +0000462 }
danielk1977106bb232004-05-21 10:08:53 +0000463 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000464 return [list 1 $errmsg]
465 }
466 }
467 return $r
468}
469
drh17a68932001-01-31 13:28:08 +0000470# Delete a file or directory
471#
472proc forcedelete {filename} {
473 if {[catch {file delete -force $filename}]} {
474 exec rm -rf $filename
475 }
476}
drh21504322002-06-25 13:16:02 +0000477
478# Do an integrity check of the entire database
479#
480proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000481 ifcapable integrityck {
482 do_test $name {
483 execsql {PRAGMA integrity_check}
484 } {ok}
485 }
486}
487
danielk1977db4e8862008-01-16 08:24:46 +0000488proc fix_ifcapable_expr {expr} {
489 set ret ""
490 set state 0
491 for {set i 0} {$i < [string length $expr]} {incr i} {
492 set char [string range $expr $i $i]
493 set newstate [expr {[string is alnum $char] || $char eq "_"}]
494 if {$newstate && !$state} {
495 append ret {$::sqlite_options(}
496 }
497 if {!$newstate && $state} {
498 append ret )
499 }
500 append ret $char
501 set state $newstate
502 }
503 if {$state} {append ret )}
504 return $ret
505}
506
drh27d258a2004-10-30 20:23:09 +0000507# Evaluate a boolean expression of capabilities. If true, execute the
508# code. Omit the code if false.
509#
danielk19773e8c37e2005-01-21 03:12:14 +0000510proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000511 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
512 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000513 if ($e2) {
514 set c [catch {uplevel 1 $code} r]
515 } else {
516 set c [catch {uplevel 1 $elsecode} r]
517 }
518 return -code $c $r
drh21504322002-06-25 13:16:02 +0000519}
danielk197745901d62004-11-10 15:27:38 +0000520
danielk1977aca790a2005-01-13 11:07:52 +0000521# This proc execs a seperate process that crashes midway through executing
522# the SQL script $sql on database test.db.
523#
524# The crash occurs during a sync() of file $crashfile. When the crash
525# occurs a random subset of all unsynced writes made by the process are
526# written into the files on disk. Argument $crashdelay indicates the
527# number of file syncs to wait before crashing.
528#
529# The return value is a list of two elements. The first element is a
530# boolean, indicating whether or not the process actually crashed or
531# reported some other error. The second element in the returned list is the
532# error message. This is "child process exited abnormally" if the crash
533# occured.
534#
danielk1977f8940ae2007-08-23 11:07:10 +0000535# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000536#
537proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000538 if {$::tcl_platform(platform)!="unix"} {
539 error "crashsql should only be used on unix"
540 }
danielk197759a33f92007-03-17 10:26:59 +0000541
542 set blocksize ""
543 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000544 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000545 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000546 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000547 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000548 set sql [lindex $args end]
549
550 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
551 set z [lindex $args $ii]
552 set n [string length $z]
553 set z2 [lindex $args [expr $ii+1]]
554
555 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000556 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000557 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000558 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000559 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000560 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000561 else { error "Unrecognized option: $z" }
562 }
563
564 if {$crashfile eq ""} {
565 error "Compulsory option -file missing"
566 }
567
danielk1977aca790a2005-01-13 11:07:52 +0000568 set cfile [file join [pwd] $crashfile]
569
570 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000571 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000572 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000573 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000574 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000575
576 # This block sets the cache size of the main database to 10
577 # pages. This is done in case the build is configured to omit
578 # "PRAGMA cache_size".
579 puts $f {db eval {SELECT * FROM sqlite_master;}}
580 puts $f {set bt [btree_from_db db]}
581 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000582 if {$prngseed} {
583 set seed [expr {$prngseed%10007+1}]
584 # puts seed=$seed
585 puts $f "db eval {SELECT randomblob($seed)}"
586 }
danielk1977933bbd62007-03-17 07:22:42 +0000587
drhf8587402008-01-08 16:03:49 +0000588 if {[string length $tclbody]>0} {
589 puts $f $tclbody
590 }
591 if {[string length $sql]>0} {
592 puts $f "db eval {"
593 puts $f "$sql"
594 puts $f "}"
595 }
danielk1977aca790a2005-01-13 11:07:52 +0000596 close $f
597
598 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000599 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000600 } msg]
601 lappend r $msg
602}
603
danielk197732554c12005-01-22 03:39:39 +0000604# Usage: do_ioerr_test <test number> <options...>
605#
606# This proc is used to implement test cases that check that IO errors
607# are correctly handled. The first argument, <test number>, is an integer
608# used to name the tests executed by this proc. Options are as follows:
609#
610# -tclprep TCL script to run to prepare test.
611# -sqlprep SQL script to run to prepare test.
612# -tclbody TCL script to run with IO error simulation.
613# -sqlbody TCL script to run with IO error simulation.
614# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000615# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000616# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000617# -start Value of 'N' to begin with (default 1)
618#
619# -cksum Boolean. If true, test that the database does
620# not change during the execution of the test case.
621#
622proc do_ioerr_test {testname args} {
623
danielk197732554c12005-01-22 03:39:39 +0000624 set ::ioerropts(-start) 1
625 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000626 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000627 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000628 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000629 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000630 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000631 array set ::ioerropts $args
632
danielk197747cd39c2008-05-12 12:41:15 +0000633 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
634 # a couple of obscure IO errors that do not return them.
635 set ::ioerropts(-erc) 0
636
danielk197732554c12005-01-22 03:39:39 +0000637 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000638 #reset_prng_state
639 save_prng_state
danielk1977861f7452008-06-05 11:39:11 +0000640 for {set n $::ioerropts(-start)} {$::go && $n<200} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000641 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000642 incr ::ioerropts(-count) -1
643 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000644
645 # Skip this IO error if it was specified with the "-exclude" option.
646 if {[info exists ::ioerropts(-exclude)]} {
647 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
648 }
danielk1977861f7452008-06-05 11:39:11 +0000649 if {$::ioerropts(-restoreprng)} {
650 restore_prng_state
651 }
danielk197732554c12005-01-22 03:39:39 +0000652
653 # Delete the files test.db and test2.db, then execute the TCL and
654 # SQL (in that order) to prepare for the test case.
655 do_test $testname.$n.1 {
656 set ::sqlite_io_error_pending 0
657 catch {db close}
658 catch {file delete -force test.db}
659 catch {file delete -force test.db-journal}
660 catch {file delete -force test2.db}
661 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000662 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000663 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000664 if {[info exists ::ioerropts(-tclprep)]} {
665 eval $::ioerropts(-tclprep)
666 }
667 if {[info exists ::ioerropts(-sqlprep)]} {
668 execsql $::ioerropts(-sqlprep)
669 }
670 expr 0
671 } {0}
672
673 # Read the 'checksum' of the database.
674 if {$::ioerropts(-cksum)} {
675 set checksum [cksum]
676 }
drh93aed5a2008-01-16 17:46:38 +0000677
danielk197732554c12005-01-22 03:39:39 +0000678 # Set the Nth IO error to fail.
679 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000680 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000681 set ::sqlite_io_error_pending $n
682 }] $n
683
684 # Create a single TCL script from the TCL and SQL specified
685 # as the body of the test.
686 set ::ioerrorbody {}
687 if {[info exists ::ioerropts(-tclbody)]} {
688 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
689 }
690 if {[info exists ::ioerropts(-sqlbody)]} {
691 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
692 }
693
694 # Execute the TCL Script created in the above block. If
695 # there are at least N IO operations performed by SQLite as
696 # a result of the script, the Nth will fail.
697 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000698 set ::sqlite_io_error_hit 0
699 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000700 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000701 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000702 set rc [sqlite3_errcode $::DB]
703 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000704 # If we are in extended result code mode, make sure all of the
705 # IOERRs we get back really do have their extended code values.
706 # If an extended result code is returned, the sqlite3_errcode
707 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
708 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000709 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
710 return $rc
711 }
712 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000713 # If we are not in extended result code mode, make sure no
714 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000715 if {[regexp {\+\d} $rc]} {
716 return $rc
717 }
718 }
drh93aed5a2008-01-16 17:46:38 +0000719 # The test repeats as long as $::go is non-zero. $::go starts out
720 # as 1. When a test runs to completion without hitting an I/O
721 # error, that means there is no point in continuing with this test
722 # case so set $::go to zero.
723 #
724 if {$::sqlite_io_error_pending>0} {
725 set ::go 0
726 set q 0
727 set ::sqlite_io_error_pending 0
728 } else {
729 set q 1
730 }
731
drhc9ac5ca2005-11-04 22:03:30 +0000732 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000733 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
734 set r 1
735 }
danielk1977f2fa8312006-01-24 13:09:33 +0000736 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000737
738 # One of two things must have happened. either
739 # 1. We never hit the IO error and the SQL returned OK
740 # 2. An IO error was hit and the SQL failed
741 #
drh93aed5a2008-01-16 17:46:38 +0000742 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000743 } {1}
744
danielk197780daec62008-05-12 10:57:02 +0000745 set ::sqlite_io_error_hit 0
746 set ::sqlite_io_error_pending 0
747
danielk197752b472a2008-05-05 16:23:55 +0000748 # Check that no page references were leaked. There should be
749 # a single reference if there is still an active transaction,
750 # or zero otherwise.
751 #
danielk197781620542008-06-07 05:19:37 +0000752 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
753 # locks are established on database files (i.e. if the error
754 # occurs while attempting to detect a hot-journal file), then
755 # there may 0 page references and an active transaction according
756 # to [sqlite3_get_autocommit].
757 #
danielk19774abd5442008-05-05 15:26:50 +0000758 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000759 do_test $testname.$n.4 {
760 set bt [btree_from_db db]
761 db_enter db
762 array set stats [btree_pager_stats $bt]
763 db_leave db
danielk197781620542008-06-07 05:19:37 +0000764 set nRef $stats(ref)
765 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
766 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000767 }
768
danielk197752b472a2008-05-05 16:23:55 +0000769 # If there is an open database handle and no open transaction,
770 # and the pager is not running in exclusive-locking mode,
771 # check that the pager is in "unlocked" state. Theoretically,
772 # if a call to xUnlock() failed due to an IO error the underlying
773 # file may still be locked.
774 #
775 ifcapable pragma {
776 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000777 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000778 && [db one {pragma locking_mode}] eq "normal"
779 && [sqlite3_get_autocommit db]
780 } {
781 do_test $testname.$n.5 {
782 set bt [btree_from_db db]
783 db_enter db
784 array set stats [btree_pager_stats $bt]
785 db_leave db
786 set stats(state)
787 } 0
788 }
789 }
790
danielk197732554c12005-01-22 03:39:39 +0000791 # If an IO error occured, then the checksum of the database should
792 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000793 #
drh1aa5af12008-03-07 19:51:14 +0000794 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000795 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000796 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000797 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000798 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000799 cksum
800 } $checksum
801 }
802
drh1aa5af12008-03-07 19:51:14 +0000803 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000804 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000805 if {[info exists ::ioerropts(-cleanup)]} {
806 catch $::ioerropts(-cleanup)
807 }
danielk197732554c12005-01-22 03:39:39 +0000808 }
809 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000810 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000811 unset ::ioerropts
812}
813
drh93aed5a2008-01-16 17:46:38 +0000814# Return a checksum based on the contents of the main database associated
815# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000816#
817proc cksum {{db db}} {
818 set txt [$db eval {
819 SELECT name, type, sql FROM sqlite_master order by name
820 }]\n
821 foreach tbl [$db eval {
822 SELECT name FROM sqlite_master WHERE type='table' order by name
823 }] {
824 append txt [$db eval "SELECT * FROM $tbl"]\n
825 }
826 foreach prag {default_synchronous default_cache_size} {
827 append txt $prag-[$db eval "PRAGMA $prag"]\n
828 }
829 set cksum [string length $txt]-[md5 $txt]
830 # puts $cksum-[file size test.db]
831 return $cksum
832}
833
drh93aed5a2008-01-16 17:46:38 +0000834# Generate a checksum based on the contents of the main and temp tables
835# database $db. If the checksum of two databases is the same, and the
836# integrity-check passes for both, the two databases are identical.
837#
drh1db639c2008-01-17 02:36:28 +0000838proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000839 set ret [list]
840 ifcapable tempdb {
841 set sql {
842 SELECT name FROM sqlite_master WHERE type = 'table' UNION
843 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
844 SELECT 'sqlite_master' UNION
845 SELECT 'sqlite_temp_master' ORDER BY 1
846 }
847 } else {
848 set sql {
849 SELECT name FROM sqlite_master WHERE type = 'table' UNION
850 SELECT 'sqlite_master' ORDER BY 1
851 }
852 }
853 set tbllist [$db eval $sql]
854 set txt {}
855 foreach tbl $tbllist {
856 append txt [$db eval "SELECT * FROM $tbl"]
857 }
858 foreach prag {default_cache_size} {
859 append txt $prag-[$db eval "PRAGMA $prag"]\n
860 }
861 # puts txt=$txt
862 return [md5 $txt]
863}
864
danielk197735754ac2008-03-21 17:29:37 +0000865proc memdebug_log_sql {{filename mallocs.sql}} {
866
danielk19776f332c12008-03-21 14:22:44 +0000867 set data [sqlite3_memdebug_log dump]
868 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000869 if {$nFrame < 0} { return "" }
870
danielk197735754ac2008-03-21 17:29:37 +0000871 set database temp
872
danielk19776f332c12008-03-21 14:22:44 +0000873 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
874 for {set ii 1} {$ii <= $nFrame} {incr ii} {
875 append tbl ", f${ii}"
876 }
877 append tbl ");\n"
878
879 set sql ""
880 foreach e $data {
881 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
882 foreach f [lrange $e 2 end] {
883 set frames($f) 1
884 }
885 }
886
887 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000888 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000889
890 foreach f [array names frames] {
891 set addr [format %x $f]
892 set cmd "addr2line -e [info nameofexec] $addr"
893 set line [eval exec $cmd]
894 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000895
896 set file [lindex [split $line :] 0]
897 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000898 }
899
danielk197735754ac2008-03-21 17:29:37 +0000900 foreach f [array names files] {
901 set contents ""
902 catch {
903 set fd [open $f]
904 set contents [read $fd]
905 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000906 }
danielk197735754ac2008-03-21 17:29:37 +0000907 set contents [string map {' ''} $contents]
908 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000909 }
danielk19776f332c12008-03-21 14:22:44 +0000910
danielk197735754ac2008-03-21 17:29:37 +0000911 set fd [open $filename w]
912 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
913 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000914}
drh93aed5a2008-01-16 17:46:38 +0000915
danielk197732554c12005-01-22 03:39:39 +0000916# Copy file $from into $to. This is used because some versions of
917# TCL for windows (notably the 8.4.1 binary package shipped with the
918# current mingw release) have a broken "file copy" command.
919#
920proc copy_file {from to} {
921 if {$::tcl_platform(platform)=="unix"} {
922 file copy -force $from $to
923 } else {
924 set f [open $from]
925 fconfigure $f -translation binary
926 set t [open $to w]
927 fconfigure $t -translation binary
928 puts -nonewline $t [read $f [file size $from]]
929 close $t
930 close $f
931 }
932}
933
danielk197745901d62004-11-10 15:27:38 +0000934# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
935# to non-zero, then set the global variable $AUTOVACUUM to 1.
936set AUTOVACUUM $sqlite_options(default_autovacuum)