blob: a3dd224a8111b764cd956aea856610a9c20192de [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#
danielk197781620542008-06-07 05:19:37 +000014# $Id: tester.tcl,v 1.126 2008/06/07 05:19:38 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#
drh254cba22001-09-20 01:44:42 +0000136catch {db close}
137file delete -force test.db
138file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000139sqlite3 db ./test.db
140set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000141if {[info exists ::SETUP_SQL]} {
142 db eval $::SETUP_SQL
143}
drhbec2bf42000-05-29 23:48:22 +0000144
145# Abort early if this script has been run before.
146#
147if {[info exists nTest]} return
148
149# Set the test counters to zero
150#
drh348784e2000-05-29 20:41:49 +0000151set nErr 0
152set nTest 0
drh767c2002000-10-19 14:10:08 +0000153set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000154set failList {}
drh521cc842008-04-15 02:36:33 +0000155set omitList {}
drhb62c3352006-11-23 09:39:16 +0000156if {![info exists speedTest]} {
157 set speedTest 0
158}
drh348784e2000-05-29 20:41:49 +0000159
drh521cc842008-04-15 02:36:33 +0000160# Record the fact that a sequence of tests were omitted.
161#
162proc omit_test {name reason} {
163 global omitList
164 lappend omitList [list $name $reason]
165}
166
drh348784e2000-05-29 20:41:49 +0000167# Invoke the do_test procedure to run a single test
168#
169proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000170 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000171 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000172 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000173 sqlite3_instvfs marker binarylog "Start of $name"
174 }
drh767c2002000-10-19 14:10:08 +0000175 if {$skip_test} {
176 set skip_test 0
177 return
178 }
179 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000180 set go 1
181 } else {
182 set go 0
183 foreach pattern $argv {
184 if {[string match $pattern $name]} {
185 set go 1
186 break
187 }
188 }
189 }
190 if {!$go} return
191 incr nTest
drh5edc3122001-09-13 21:53:09 +0000192 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000193 flush stdout
194 if {[catch {uplevel #0 "$cmd;\n"} result]} {
195 puts "\nError: $result"
196 incr nErr
drha1b351a2001-09-14 16:42:12 +0000197 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000198 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000199 } elseif {[string compare $result $expected]} {
200 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
201 incr nErr
drha1b351a2001-09-14 16:42:12 +0000202 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000203 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000204 } else {
205 puts " Ok"
206 }
drh6558db82007-04-13 03:23:21 +0000207 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000208 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000209 sqlite3_instvfs marker binarylog "End of $name"
210 }
drh348784e2000-05-29 20:41:49 +0000211}
212
drhb62c3352006-11-23 09:39:16 +0000213# Run an SQL script.
214# Return the number of microseconds per statement.
215#
drh3590f152006-11-23 21:09:10 +0000216proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000217 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000218 flush stdout
219 set speed [time {sqlite3_exec_nr db $sql}]
220 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000221 if {$tm == 0} {
222 set rate [format %20s "many"]
223 } else {
224 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
225 }
drh3590f152006-11-23 21:09:10 +0000226 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000227 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000228 global total_time
229 set total_time [expr {$total_time+$tm}]
230}
drh45c236d2008-03-22 01:08:00 +0000231proc speed_trial_tcl {name numstmt units script} {
232 puts -nonewline [format {%-21.21s } $name...]
233 flush stdout
234 set speed [time {eval $script}]
235 set tm [lindex $speed 0]
236 if {$tm == 0} {
237 set rate [format %20s "many"]
238 } else {
239 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
240 }
241 set u2 $units/s
242 puts [format {%12d uS %s %s} $tm $rate $u2]
243 global total_time
244 set total_time [expr {$total_time+$tm}]
245}
drhdd924312007-03-31 22:34:16 +0000246proc speed_trial_init {name} {
247 global total_time
248 set total_time 0
249}
250proc speed_trial_summary {name} {
251 global total_time
252 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000253}
254
drh348784e2000-05-29 20:41:49 +0000255# Run this routine last
256#
257proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000258 finalize_testing
259}
260proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000261 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000262
drh6e142f52000-06-08 13:36:40 +0000263 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000264 catch {db2 close}
265 catch {db3 close}
266
drh9bc54492007-10-23 14:49:59 +0000267 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000268 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000269 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000270 db close
drh984bfaa2008-03-19 16:08:53 +0000271 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000272 set heaplimit [sqlite3_soft_heap_limit]
273 if {$heaplimit!=$::soft_limit} {
274 puts "soft-heap-limit changed by this script\
275 from $::soft_limit to $heaplimit"
276 } elseif {$heaplimit!="" && $heaplimit>0} {
277 puts "soft-heap-limit set to $heaplimit"
278 }
279 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000280 incr nTest
drh348784e2000-05-29 20:41:49 +0000281 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000282 if {$nErr>0} {
283 puts "Failures on these tests: $::failList"
284 }
drh521cc842008-04-15 02:36:33 +0000285 if {[llength $omitList]>0} {
286 puts "Omitted test cases:"
287 set prec {}
288 foreach {rec} [lsort $omitList] {
289 if {$rec==$prec} continue
290 set prec $rec
291 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
292 }
293 }
drh80788d82006-09-02 14:50:23 +0000294 if {$nErr>0 && ![working_64bit_int]} {
295 puts "******************************************************************"
296 puts "N.B.: The version of TCL that you used to build this test harness"
297 puts "is defective in that it does not support 64-bit integers. Some or"
298 puts "all of the test failures above might be a result from this defect"
299 puts "in your TCL build."
300 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000301 }
danielk1977374177e2008-05-08 15:58:06 +0000302 if {[info exists ::tester_do_binarylog]} {
303 sqlite3_instvfs destroy binarylog
304 }
drh94e92032003-02-16 22:21:32 +0000305 if {$sqlite_open_file_count} {
306 puts "$sqlite_open_file_count files were left open"
307 incr nErr
308 }
danielk19771e21fd52008-04-10 17:27:38 +0000309 if {[info exists ::tester_do_ostrace]} {
310 puts "Writing ostrace.sql..."
311 set fd $::ostrace_fd
312
313 puts -nonewline $fd "CREATE TABLE ossummary"
314 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
315 foreach row [sqlite3_instvfs report ostrace] {
316 foreach {method count clicks} $row break
317 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
318 }
319 puts $fd "COMMIT;"
320 close $fd
321 sqlite3_instvfs destroy ostrace
322 }
drhf3a65f72007-08-22 20:18:21 +0000323 if {[sqlite3_memory_used]>0} {
324 puts "Unfreed memory: [sqlite3_memory_used] bytes"
325 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000326 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000327 puts "Writing unfreed memory log to \"./memleak.txt\""
328 sqlite3_memdebug_dump ./memleak.txt
329 }
drhf3a65f72007-08-22 20:18:21 +0000330 } else {
331 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000332 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000333 sqlite3_memdebug_dump ./memusage.txt
334 }
drhf3a65f72007-08-22 20:18:21 +0000335 }
drh91fd4d42008-01-19 20:11:25 +0000336 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
337 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000338 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
339 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
340 }
danielk197735754ac2008-03-21 17:29:37 +0000341 if {[info exists ::tester_do_malloctrace]} {
342 puts "Writing mallocs.sql..."
343 memdebug_log_sql
344 sqlite3_memdebug_log stop
345 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000346
347 if {[sqlite3_memory_used]>0} {
348 puts "Writing leaks.sql..."
349 sqlite3_memdebug_log sync
350 memdebug_log_sql leaks.sql
351 }
danielk197735754ac2008-03-21 17:29:37 +0000352 }
drhd9910fe2006-10-04 11:55:49 +0000353 foreach f [glob -nocomplain test.db-*-journal] {
354 file delete -force $f
355 }
356 foreach f [glob -nocomplain test.db-mj*] {
357 file delete -force $f
358 }
drhdb25e382001-03-15 18:21:22 +0000359 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000360}
361
drh348784e2000-05-29 20:41:49 +0000362# A procedure to execute SQL
363#
drhc4a3c772001-04-04 11:48:57 +0000364proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000365 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000366 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000367}
drh3aadb2e2000-05-31 17:59:25 +0000368
drhadbca9c2001-09-27 15:11:53 +0000369# Execute SQL and catch exceptions.
370#
371proc catchsql {sql {db db}} {
372 # puts "SQL = $sql"
373 set r [catch {$db eval $sql} msg]
374 lappend r $msg
375 return $r
376}
377
drh04096482001-11-09 22:41:44 +0000378# Do an VDBE code dump on the SQL given
379#
380proc explain {sql {db db}} {
381 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000382 puts "addr opcode p1 p2 p3 p4 p5 #"
383 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000384 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000385 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
386 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
387 ]
drh04096482001-11-09 22:41:44 +0000388 }
389}
390
drhdafc0ce2008-04-17 19:14:02 +0000391# Show the VDBE program for an SQL statement but omit the Trace
392# opcode at the beginning. This procedure can be used to prove
393# that different SQL statements generate exactly the same VDBE code.
394#
395proc explain_no_trace {sql} {
396 set tr [db eval "EXPLAIN $sql"]
397 return [lrange $tr 7 end]
398}
399
drh3aadb2e2000-05-31 17:59:25 +0000400# Another procedure to execute SQL. This one includes the field
401# names in the returned list.
402#
403proc execsql2 {sql} {
404 set result {}
405 db eval $sql data {
406 foreach f $data(*) {
407 lappend result $f $data($f)
408 }
409 }
410 return $result
411}
drh17a68932001-01-31 13:28:08 +0000412
drhdde85d92003-03-01 19:45:34 +0000413# Use the non-callback API to execute multiple SQL statements
414#
415proc stepsql {dbptr sql} {
416 set sql [string trim $sql]
417 set r 0
418 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000419 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000420 return [list 1 $vm]
421 }
422 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000423# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
424# foreach v $VAL {lappend r $v}
425# }
426 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
427 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
428 lappend r [sqlite3_column_text $vm $i]
429 }
drhdde85d92003-03-01 19:45:34 +0000430 }
danielk1977106bb232004-05-21 10:08:53 +0000431 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000432 return [list 1 $errmsg]
433 }
434 }
435 return $r
436}
437
drh17a68932001-01-31 13:28:08 +0000438# Delete a file or directory
439#
440proc forcedelete {filename} {
441 if {[catch {file delete -force $filename}]} {
442 exec rm -rf $filename
443 }
444}
drh21504322002-06-25 13:16:02 +0000445
446# Do an integrity check of the entire database
447#
448proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000449 ifcapable integrityck {
450 do_test $name {
451 execsql {PRAGMA integrity_check}
452 } {ok}
453 }
454}
455
danielk1977db4e8862008-01-16 08:24:46 +0000456proc fix_ifcapable_expr {expr} {
457 set ret ""
458 set state 0
459 for {set i 0} {$i < [string length $expr]} {incr i} {
460 set char [string range $expr $i $i]
461 set newstate [expr {[string is alnum $char] || $char eq "_"}]
462 if {$newstate && !$state} {
463 append ret {$::sqlite_options(}
464 }
465 if {!$newstate && $state} {
466 append ret )
467 }
468 append ret $char
469 set state $newstate
470 }
471 if {$state} {append ret )}
472 return $ret
473}
474
drh27d258a2004-10-30 20:23:09 +0000475# Evaluate a boolean expression of capabilities. If true, execute the
476# code. Omit the code if false.
477#
danielk19773e8c37e2005-01-21 03:12:14 +0000478proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000479 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
480 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000481 if ($e2) {
482 set c [catch {uplevel 1 $code} r]
483 } else {
484 set c [catch {uplevel 1 $elsecode} r]
485 }
486 return -code $c $r
drh21504322002-06-25 13:16:02 +0000487}
danielk197745901d62004-11-10 15:27:38 +0000488
danielk1977aca790a2005-01-13 11:07:52 +0000489# This proc execs a seperate process that crashes midway through executing
490# the SQL script $sql on database test.db.
491#
492# The crash occurs during a sync() of file $crashfile. When the crash
493# occurs a random subset of all unsynced writes made by the process are
494# written into the files on disk. Argument $crashdelay indicates the
495# number of file syncs to wait before crashing.
496#
497# The return value is a list of two elements. The first element is a
498# boolean, indicating whether or not the process actually crashed or
499# reported some other error. The second element in the returned list is the
500# error message. This is "child process exited abnormally" if the crash
501# occured.
502#
danielk1977f8940ae2007-08-23 11:07:10 +0000503# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000504#
505proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000506 if {$::tcl_platform(platform)!="unix"} {
507 error "crashsql should only be used on unix"
508 }
danielk197759a33f92007-03-17 10:26:59 +0000509
510 set blocksize ""
511 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000512 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000513 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000514 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000515 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000516 set sql [lindex $args end]
517
518 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
519 set z [lindex $args $ii]
520 set n [string length $z]
521 set z2 [lindex $args [expr $ii+1]]
522
523 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000524 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000525 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000526 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000527 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000528 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000529 else { error "Unrecognized option: $z" }
530 }
531
532 if {$crashfile eq ""} {
533 error "Compulsory option -file missing"
534 }
535
danielk1977aca790a2005-01-13 11:07:52 +0000536 set cfile [file join [pwd] $crashfile]
537
538 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000539 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000540 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000541 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000542 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000543
544 # This block sets the cache size of the main database to 10
545 # pages. This is done in case the build is configured to omit
546 # "PRAGMA cache_size".
547 puts $f {db eval {SELECT * FROM sqlite_master;}}
548 puts $f {set bt [btree_from_db db]}
549 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000550 if {$prngseed} {
551 set seed [expr {$prngseed%10007+1}]
552 # puts seed=$seed
553 puts $f "db eval {SELECT randomblob($seed)}"
554 }
danielk1977933bbd62007-03-17 07:22:42 +0000555
drhf8587402008-01-08 16:03:49 +0000556 if {[string length $tclbody]>0} {
557 puts $f $tclbody
558 }
559 if {[string length $sql]>0} {
560 puts $f "db eval {"
561 puts $f "$sql"
562 puts $f "}"
563 }
danielk1977aca790a2005-01-13 11:07:52 +0000564 close $f
565
566 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000567 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000568 } msg]
569 lappend r $msg
570}
571
danielk197732554c12005-01-22 03:39:39 +0000572# Usage: do_ioerr_test <test number> <options...>
573#
574# This proc is used to implement test cases that check that IO errors
575# are correctly handled. The first argument, <test number>, is an integer
576# used to name the tests executed by this proc. Options are as follows:
577#
578# -tclprep TCL script to run to prepare test.
579# -sqlprep SQL script to run to prepare test.
580# -tclbody TCL script to run with IO error simulation.
581# -sqlbody TCL script to run with IO error simulation.
582# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000583# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000584# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000585# -start Value of 'N' to begin with (default 1)
586#
587# -cksum Boolean. If true, test that the database does
588# not change during the execution of the test case.
589#
590proc do_ioerr_test {testname args} {
591
danielk197732554c12005-01-22 03:39:39 +0000592 set ::ioerropts(-start) 1
593 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000594 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000595 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000596 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000597 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000598 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000599 array set ::ioerropts $args
600
danielk197747cd39c2008-05-12 12:41:15 +0000601 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
602 # a couple of obscure IO errors that do not return them.
603 set ::ioerropts(-erc) 0
604
danielk197732554c12005-01-22 03:39:39 +0000605 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000606 #reset_prng_state
607 save_prng_state
danielk1977861f7452008-06-05 11:39:11 +0000608 for {set n $::ioerropts(-start)} {$::go && $n<200} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000609 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000610 incr ::ioerropts(-count) -1
611 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000612
613 # Skip this IO error if it was specified with the "-exclude" option.
614 if {[info exists ::ioerropts(-exclude)]} {
615 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
616 }
danielk1977861f7452008-06-05 11:39:11 +0000617 if {$::ioerropts(-restoreprng)} {
618 restore_prng_state
619 }
danielk197732554c12005-01-22 03:39:39 +0000620
621 # Delete the files test.db and test2.db, then execute the TCL and
622 # SQL (in that order) to prepare for the test case.
623 do_test $testname.$n.1 {
624 set ::sqlite_io_error_pending 0
625 catch {db close}
626 catch {file delete -force test.db}
627 catch {file delete -force test.db-journal}
628 catch {file delete -force test2.db}
629 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000630 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000631 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000632 if {[info exists ::ioerropts(-tclprep)]} {
633 eval $::ioerropts(-tclprep)
634 }
635 if {[info exists ::ioerropts(-sqlprep)]} {
636 execsql $::ioerropts(-sqlprep)
637 }
638 expr 0
639 } {0}
640
641 # Read the 'checksum' of the database.
642 if {$::ioerropts(-cksum)} {
643 set checksum [cksum]
644 }
drh93aed5a2008-01-16 17:46:38 +0000645
danielk197732554c12005-01-22 03:39:39 +0000646 # Set the Nth IO error to fail.
647 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000648 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000649 set ::sqlite_io_error_pending $n
650 }] $n
651
652 # Create a single TCL script from the TCL and SQL specified
653 # as the body of the test.
654 set ::ioerrorbody {}
655 if {[info exists ::ioerropts(-tclbody)]} {
656 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
657 }
658 if {[info exists ::ioerropts(-sqlbody)]} {
659 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
660 }
661
662 # Execute the TCL Script created in the above block. If
663 # there are at least N IO operations performed by SQLite as
664 # a result of the script, the Nth will fail.
665 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000666 set ::sqlite_io_error_hit 0
667 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000668 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000669 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000670 set rc [sqlite3_errcode $::DB]
671 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000672 # If we are in extended result code mode, make sure all of the
673 # IOERRs we get back really do have their extended code values.
674 # If an extended result code is returned, the sqlite3_errcode
675 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
676 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000677 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
678 return $rc
679 }
680 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000681 # If we are not in extended result code mode, make sure no
682 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000683 if {[regexp {\+\d} $rc]} {
684 return $rc
685 }
686 }
drh93aed5a2008-01-16 17:46:38 +0000687 # The test repeats as long as $::go is non-zero. $::go starts out
688 # as 1. When a test runs to completion without hitting an I/O
689 # error, that means there is no point in continuing with this test
690 # case so set $::go to zero.
691 #
692 if {$::sqlite_io_error_pending>0} {
693 set ::go 0
694 set q 0
695 set ::sqlite_io_error_pending 0
696 } else {
697 set q 1
698 }
699
drhc9ac5ca2005-11-04 22:03:30 +0000700 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000701 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
702 set r 1
703 }
danielk1977f2fa8312006-01-24 13:09:33 +0000704 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000705
706 # One of two things must have happened. either
707 # 1. We never hit the IO error and the SQL returned OK
708 # 2. An IO error was hit and the SQL failed
709 #
drh93aed5a2008-01-16 17:46:38 +0000710 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000711 } {1}
712
danielk197780daec62008-05-12 10:57:02 +0000713 set ::sqlite_io_error_hit 0
714 set ::sqlite_io_error_pending 0
715
danielk197752b472a2008-05-05 16:23:55 +0000716 # Check that no page references were leaked. There should be
717 # a single reference if there is still an active transaction,
718 # or zero otherwise.
719 #
danielk197781620542008-06-07 05:19:37 +0000720 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
721 # locks are established on database files (i.e. if the error
722 # occurs while attempting to detect a hot-journal file), then
723 # there may 0 page references and an active transaction according
724 # to [sqlite3_get_autocommit].
725 #
danielk19774abd5442008-05-05 15:26:50 +0000726 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000727 do_test $testname.$n.4 {
728 set bt [btree_from_db db]
729 db_enter db
730 array set stats [btree_pager_stats $bt]
731 db_leave db
danielk197781620542008-06-07 05:19:37 +0000732 set nRef $stats(ref)
733 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
734 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000735 }
736
danielk197752b472a2008-05-05 16:23:55 +0000737 # If there is an open database handle and no open transaction,
738 # and the pager is not running in exclusive-locking mode,
739 # check that the pager is in "unlocked" state. Theoretically,
740 # if a call to xUnlock() failed due to an IO error the underlying
741 # file may still be locked.
742 #
743 ifcapable pragma {
744 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000745 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000746 && [db one {pragma locking_mode}] eq "normal"
747 && [sqlite3_get_autocommit db]
748 } {
749 do_test $testname.$n.5 {
750 set bt [btree_from_db db]
751 db_enter db
752 array set stats [btree_pager_stats $bt]
753 db_leave db
754 set stats(state)
755 } 0
756 }
757 }
758
danielk197732554c12005-01-22 03:39:39 +0000759 # If an IO error occured, then the checksum of the database should
760 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000761 #
drh1aa5af12008-03-07 19:51:14 +0000762 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000763 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000764 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000765 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000766 cksum
767 } $checksum
768 }
769
drh1aa5af12008-03-07 19:51:14 +0000770 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000771 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000772 if {[info exists ::ioerropts(-cleanup)]} {
773 catch $::ioerropts(-cleanup)
774 }
danielk197732554c12005-01-22 03:39:39 +0000775 }
776 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000777 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000778 unset ::ioerropts
779}
780
drh93aed5a2008-01-16 17:46:38 +0000781# Return a checksum based on the contents of the main database associated
782# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000783#
784proc cksum {{db db}} {
785 set txt [$db eval {
786 SELECT name, type, sql FROM sqlite_master order by name
787 }]\n
788 foreach tbl [$db eval {
789 SELECT name FROM sqlite_master WHERE type='table' order by name
790 }] {
791 append txt [$db eval "SELECT * FROM $tbl"]\n
792 }
793 foreach prag {default_synchronous default_cache_size} {
794 append txt $prag-[$db eval "PRAGMA $prag"]\n
795 }
796 set cksum [string length $txt]-[md5 $txt]
797 # puts $cksum-[file size test.db]
798 return $cksum
799}
800
drh93aed5a2008-01-16 17:46:38 +0000801# Generate a checksum based on the contents of the main and temp tables
802# database $db. If the checksum of two databases is the same, and the
803# integrity-check passes for both, the two databases are identical.
804#
drh1db639c2008-01-17 02:36:28 +0000805proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000806 set ret [list]
807 ifcapable tempdb {
808 set sql {
809 SELECT name FROM sqlite_master WHERE type = 'table' UNION
810 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
811 SELECT 'sqlite_master' UNION
812 SELECT 'sqlite_temp_master' ORDER BY 1
813 }
814 } else {
815 set sql {
816 SELECT name FROM sqlite_master WHERE type = 'table' UNION
817 SELECT 'sqlite_master' ORDER BY 1
818 }
819 }
820 set tbllist [$db eval $sql]
821 set txt {}
822 foreach tbl $tbllist {
823 append txt [$db eval "SELECT * FROM $tbl"]
824 }
825 foreach prag {default_cache_size} {
826 append txt $prag-[$db eval "PRAGMA $prag"]\n
827 }
828 # puts txt=$txt
829 return [md5 $txt]
830}
831
danielk197735754ac2008-03-21 17:29:37 +0000832proc memdebug_log_sql {{filename mallocs.sql}} {
833
danielk19776f332c12008-03-21 14:22:44 +0000834 set data [sqlite3_memdebug_log dump]
835 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000836 if {$nFrame < 0} { return "" }
837
danielk197735754ac2008-03-21 17:29:37 +0000838 set database temp
839
danielk19776f332c12008-03-21 14:22:44 +0000840 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
841 for {set ii 1} {$ii <= $nFrame} {incr ii} {
842 append tbl ", f${ii}"
843 }
844 append tbl ");\n"
845
846 set sql ""
847 foreach e $data {
848 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
849 foreach f [lrange $e 2 end] {
850 set frames($f) 1
851 }
852 }
853
854 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000855 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000856
857 foreach f [array names frames] {
858 set addr [format %x $f]
859 set cmd "addr2line -e [info nameofexec] $addr"
860 set line [eval exec $cmd]
861 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000862
863 set file [lindex [split $line :] 0]
864 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000865 }
866
danielk197735754ac2008-03-21 17:29:37 +0000867 foreach f [array names files] {
868 set contents ""
869 catch {
870 set fd [open $f]
871 set contents [read $fd]
872 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000873 }
danielk197735754ac2008-03-21 17:29:37 +0000874 set contents [string map {' ''} $contents]
875 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000876 }
danielk19776f332c12008-03-21 14:22:44 +0000877
danielk197735754ac2008-03-21 17:29:37 +0000878 set fd [open $filename w]
879 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
880 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000881}
drh93aed5a2008-01-16 17:46:38 +0000882
danielk197732554c12005-01-22 03:39:39 +0000883# Copy file $from into $to. This is used because some versions of
884# TCL for windows (notably the 8.4.1 binary package shipped with the
885# current mingw release) have a broken "file copy" command.
886#
887proc copy_file {from to} {
888 if {$::tcl_platform(platform)=="unix"} {
889 file copy -force $from $to
890 } else {
891 set f [open $from]
892 fconfigure $f -translation binary
893 set t [open $to w]
894 fconfigure $t -translation binary
895 puts -nonewline $t [read $f [file size $from]]
896 close $t
897 close $f
898 }
899}
900
danielk197745901d62004-11-10 15:27:38 +0000901# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
902# to non-zero, then set the global variable $AUTOVACUUM to 1.
903set AUTOVACUUM $sqlite_options(default_autovacuum)