blob: 5fd9d3578210614bdc56daae9827b9c375b0f951 [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#
drh521cc842008-04-15 02:36:33 +000014# $Id: tester.tcl,v 1.115 2008/04/15 02:36:34 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} {
76 set s "INSERT INTO ostrace VALUES( '$zCall', $nClick, '$zFile', $i32, $i64);"
77 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
91 }
92 set argv [lreplace $argv $i $i]
93 }
94}
95
drh6a288a32008-01-07 19:20:24 +000096#
97# Check the command-line arguments to set the maximum number of
98# errors tolerated before halting.
99#
100if {![info exists maxErr]} {
101 set maxErr 1000
102}
103for {set i 0} {$i<[llength $argv]} {incr i} {
104 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
105 set argv [lreplace $argv $i $i]
106 }
107}
108#puts "Max error = $maxErr"
109
drh4a50aac2007-08-23 02:47:53 +0000110
drh9eb9e262004-02-11 02:18:05 +0000111# Use the pager codec if it is available
112#
drhef4ac8f2004-06-19 00:16:31 +0000113if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
114 rename sqlite3 sqlite_orig
115 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000116 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
117 lappend args -key {xyzzy}
118 }
119 uplevel 1 sqlite_orig $args
120 }
121}
122
123
drhbec2bf42000-05-29 23:48:22 +0000124# Create a test database
125#
drh254cba22001-09-20 01:44:42 +0000126catch {db close}
127file delete -force test.db
128file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000129sqlite3 db ./test.db
130set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000131if {[info exists ::SETUP_SQL]} {
132 db eval $::SETUP_SQL
133}
drhbec2bf42000-05-29 23:48:22 +0000134
135# Abort early if this script has been run before.
136#
137if {[info exists nTest]} return
138
139# Set the test counters to zero
140#
drh348784e2000-05-29 20:41:49 +0000141set nErr 0
142set nTest 0
drh767c2002000-10-19 14:10:08 +0000143set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000144set failList {}
drh521cc842008-04-15 02:36:33 +0000145set omitList {}
drhb62c3352006-11-23 09:39:16 +0000146if {![info exists speedTest]} {
147 set speedTest 0
148}
drh348784e2000-05-29 20:41:49 +0000149
drh521cc842008-04-15 02:36:33 +0000150# Record the fact that a sequence of tests were omitted.
151#
152proc omit_test {name reason} {
153 global omitList
154 lappend omitList [list $name $reason]
155}
156
drh348784e2000-05-29 20:41:49 +0000157# Invoke the do_test procedure to run a single test
158#
159proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000160 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000161 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +0000162 if {$skip_test} {
163 set skip_test 0
164 return
165 }
166 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000167 set go 1
168 } else {
169 set go 0
170 foreach pattern $argv {
171 if {[string match $pattern $name]} {
172 set go 1
173 break
174 }
175 }
176 }
177 if {!$go} return
178 incr nTest
drh5edc3122001-09-13 21:53:09 +0000179 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000180 flush stdout
181 if {[catch {uplevel #0 "$cmd;\n"} result]} {
182 puts "\nError: $result"
183 incr nErr
drha1b351a2001-09-14 16:42:12 +0000184 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000185 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000186 } elseif {[string compare $result $expected]} {
187 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
188 incr nErr
drha1b351a2001-09-14 16:42:12 +0000189 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000190 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000191 } else {
192 puts " Ok"
193 }
drh6558db82007-04-13 03:23:21 +0000194 flush stdout
drh348784e2000-05-29 20:41:49 +0000195}
196
drhb62c3352006-11-23 09:39:16 +0000197# Run an SQL script.
198# Return the number of microseconds per statement.
199#
drh3590f152006-11-23 21:09:10 +0000200proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000201 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000202 flush stdout
203 set speed [time {sqlite3_exec_nr db $sql}]
204 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000205 if {$tm == 0} {
206 set rate [format %20s "many"]
207 } else {
208 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
209 }
drh3590f152006-11-23 21:09:10 +0000210 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000211 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000212 global total_time
213 set total_time [expr {$total_time+$tm}]
214}
drh45c236d2008-03-22 01:08:00 +0000215proc speed_trial_tcl {name numstmt units script} {
216 puts -nonewline [format {%-21.21s } $name...]
217 flush stdout
218 set speed [time {eval $script}]
219 set tm [lindex $speed 0]
220 if {$tm == 0} {
221 set rate [format %20s "many"]
222 } else {
223 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
224 }
225 set u2 $units/s
226 puts [format {%12d uS %s %s} $tm $rate $u2]
227 global total_time
228 set total_time [expr {$total_time+$tm}]
229}
drhdd924312007-03-31 22:34:16 +0000230proc speed_trial_init {name} {
231 global total_time
232 set total_time 0
233}
234proc speed_trial_summary {name} {
235 global total_time
236 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000237}
238
drh348784e2000-05-29 20:41:49 +0000239# Run this routine last
240#
241proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000242 finalize_testing
243}
244proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000245 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000246
drh6e142f52000-06-08 13:36:40 +0000247 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000248 catch {db2 close}
249 catch {db3 close}
250
drh9bc54492007-10-23 14:49:59 +0000251 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000252 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000253 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000254 db close
drh984bfaa2008-03-19 16:08:53 +0000255 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000256 set heaplimit [sqlite3_soft_heap_limit]
257 if {$heaplimit!=$::soft_limit} {
258 puts "soft-heap-limit changed by this script\
259 from $::soft_limit to $heaplimit"
260 } elseif {$heaplimit!="" && $heaplimit>0} {
261 puts "soft-heap-limit set to $heaplimit"
262 }
263 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000264 incr nTest
drh348784e2000-05-29 20:41:49 +0000265 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000266 if {$nErr>0} {
267 puts "Failures on these tests: $::failList"
268 }
drh521cc842008-04-15 02:36:33 +0000269 if {[llength $omitList]>0} {
270 puts "Omitted test cases:"
271 set prec {}
272 foreach {rec} [lsort $omitList] {
273 if {$rec==$prec} continue
274 set prec $rec
275 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
276 }
277 }
drh80788d82006-09-02 14:50:23 +0000278 if {$nErr>0 && ![working_64bit_int]} {
279 puts "******************************************************************"
280 puts "N.B.: The version of TCL that you used to build this test harness"
281 puts "is defective in that it does not support 64-bit integers. Some or"
282 puts "all of the test failures above might be a result from this defect"
283 puts "in your TCL build."
284 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000285 }
drh94e92032003-02-16 22:21:32 +0000286 if {$sqlite_open_file_count} {
287 puts "$sqlite_open_file_count files were left open"
288 incr nErr
289 }
danielk19771e21fd52008-04-10 17:27:38 +0000290 if {[info exists ::tester_do_ostrace]} {
291 puts "Writing ostrace.sql..."
292 set fd $::ostrace_fd
293
294 puts -nonewline $fd "CREATE TABLE ossummary"
295 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
296 foreach row [sqlite3_instvfs report ostrace] {
297 foreach {method count clicks} $row break
298 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
299 }
300 puts $fd "COMMIT;"
301 close $fd
302 sqlite3_instvfs destroy ostrace
303 }
drhf3a65f72007-08-22 20:18:21 +0000304 if {[sqlite3_memory_used]>0} {
305 puts "Unfreed memory: [sqlite3_memory_used] bytes"
306 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000307 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000308 puts "Writing unfreed memory log to \"./memleak.txt\""
309 sqlite3_memdebug_dump ./memleak.txt
310 }
drhf3a65f72007-08-22 20:18:21 +0000311 } else {
312 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000313 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000314 sqlite3_memdebug_dump ./memusage.txt
315 }
drhf3a65f72007-08-22 20:18:21 +0000316 }
drh91fd4d42008-01-19 20:11:25 +0000317 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
318 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000319 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
320 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
321 }
danielk197735754ac2008-03-21 17:29:37 +0000322 if {[info exists ::tester_do_malloctrace]} {
323 puts "Writing mallocs.sql..."
324 memdebug_log_sql
325 sqlite3_memdebug_log stop
326 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000327
328 if {[sqlite3_memory_used]>0} {
329 puts "Writing leaks.sql..."
330 sqlite3_memdebug_log sync
331 memdebug_log_sql leaks.sql
332 }
danielk197735754ac2008-03-21 17:29:37 +0000333 }
drhd9910fe2006-10-04 11:55:49 +0000334 foreach f [glob -nocomplain test.db-*-journal] {
335 file delete -force $f
336 }
337 foreach f [glob -nocomplain test.db-mj*] {
338 file delete -force $f
339 }
drhdb25e382001-03-15 18:21:22 +0000340 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000341}
342
drh348784e2000-05-29 20:41:49 +0000343# A procedure to execute SQL
344#
drhc4a3c772001-04-04 11:48:57 +0000345proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000346 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000347 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000348}
drh3aadb2e2000-05-31 17:59:25 +0000349
drhadbca9c2001-09-27 15:11:53 +0000350# Execute SQL and catch exceptions.
351#
352proc catchsql {sql {db db}} {
353 # puts "SQL = $sql"
354 set r [catch {$db eval $sql} msg]
355 lappend r $msg
356 return $r
357}
358
drh04096482001-11-09 22:41:44 +0000359# Do an VDBE code dump on the SQL given
360#
361proc explain {sql {db db}} {
362 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000363 puts "addr opcode p1 p2 p3 p4 p5 #"
364 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000365 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000366 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
367 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
368 ]
drh04096482001-11-09 22:41:44 +0000369 }
370}
371
drh3aadb2e2000-05-31 17:59:25 +0000372# Another procedure to execute SQL. This one includes the field
373# names in the returned list.
374#
375proc execsql2 {sql} {
376 set result {}
377 db eval $sql data {
378 foreach f $data(*) {
379 lappend result $f $data($f)
380 }
381 }
382 return $result
383}
drh17a68932001-01-31 13:28:08 +0000384
drhdde85d92003-03-01 19:45:34 +0000385# Use the non-callback API to execute multiple SQL statements
386#
387proc stepsql {dbptr sql} {
388 set sql [string trim $sql]
389 set r 0
390 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000391 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000392 return [list 1 $vm]
393 }
394 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000395# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
396# foreach v $VAL {lappend r $v}
397# }
398 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
399 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
400 lappend r [sqlite3_column_text $vm $i]
401 }
drhdde85d92003-03-01 19:45:34 +0000402 }
danielk1977106bb232004-05-21 10:08:53 +0000403 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000404 return [list 1 $errmsg]
405 }
406 }
407 return $r
408}
409
drh17a68932001-01-31 13:28:08 +0000410# Delete a file or directory
411#
412proc forcedelete {filename} {
413 if {[catch {file delete -force $filename}]} {
414 exec rm -rf $filename
415 }
416}
drh21504322002-06-25 13:16:02 +0000417
418# Do an integrity check of the entire database
419#
420proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000421 ifcapable integrityck {
422 do_test $name {
423 execsql {PRAGMA integrity_check}
424 } {ok}
425 }
426}
427
danielk1977db4e8862008-01-16 08:24:46 +0000428proc fix_ifcapable_expr {expr} {
429 set ret ""
430 set state 0
431 for {set i 0} {$i < [string length $expr]} {incr i} {
432 set char [string range $expr $i $i]
433 set newstate [expr {[string is alnum $char] || $char eq "_"}]
434 if {$newstate && !$state} {
435 append ret {$::sqlite_options(}
436 }
437 if {!$newstate && $state} {
438 append ret )
439 }
440 append ret $char
441 set state $newstate
442 }
443 if {$state} {append ret )}
444 return $ret
445}
446
drh27d258a2004-10-30 20:23:09 +0000447# Evaluate a boolean expression of capabilities. If true, execute the
448# code. Omit the code if false.
449#
danielk19773e8c37e2005-01-21 03:12:14 +0000450proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000451 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
452 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000453 if ($e2) {
454 set c [catch {uplevel 1 $code} r]
455 } else {
456 set c [catch {uplevel 1 $elsecode} r]
457 }
458 return -code $c $r
drh21504322002-06-25 13:16:02 +0000459}
danielk197745901d62004-11-10 15:27:38 +0000460
danielk1977aca790a2005-01-13 11:07:52 +0000461# This proc execs a seperate process that crashes midway through executing
462# the SQL script $sql on database test.db.
463#
464# The crash occurs during a sync() of file $crashfile. When the crash
465# occurs a random subset of all unsynced writes made by the process are
466# written into the files on disk. Argument $crashdelay indicates the
467# number of file syncs to wait before crashing.
468#
469# The return value is a list of two elements. The first element is a
470# boolean, indicating whether or not the process actually crashed or
471# reported some other error. The second element in the returned list is the
472# error message. This is "child process exited abnormally" if the crash
473# occured.
474#
danielk1977f8940ae2007-08-23 11:07:10 +0000475# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000476#
477proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000478 if {$::tcl_platform(platform)!="unix"} {
479 error "crashsql should only be used on unix"
480 }
danielk197759a33f92007-03-17 10:26:59 +0000481
482 set blocksize ""
483 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000484 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000485 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000486 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000487 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000488 set sql [lindex $args end]
489
490 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
491 set z [lindex $args $ii]
492 set n [string length $z]
493 set z2 [lindex $args [expr $ii+1]]
494
495 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000496 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000497 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000498 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000499 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000500 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000501 else { error "Unrecognized option: $z" }
502 }
503
504 if {$crashfile eq ""} {
505 error "Compulsory option -file missing"
506 }
507
danielk1977aca790a2005-01-13 11:07:52 +0000508 set cfile [file join [pwd] $crashfile]
509
510 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000511 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000512 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000513 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000514 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000515
516 # This block sets the cache size of the main database to 10
517 # pages. This is done in case the build is configured to omit
518 # "PRAGMA cache_size".
519 puts $f {db eval {SELECT * FROM sqlite_master;}}
520 puts $f {set bt [btree_from_db db]}
521 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000522 if {$prngseed} {
523 set seed [expr {$prngseed%10007+1}]
524 # puts seed=$seed
525 puts $f "db eval {SELECT randomblob($seed)}"
526 }
danielk1977933bbd62007-03-17 07:22:42 +0000527
drhf8587402008-01-08 16:03:49 +0000528 if {[string length $tclbody]>0} {
529 puts $f $tclbody
530 }
531 if {[string length $sql]>0} {
532 puts $f "db eval {"
533 puts $f "$sql"
534 puts $f "}"
535 }
danielk1977aca790a2005-01-13 11:07:52 +0000536 close $f
537
538 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000539 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000540 } msg]
541 lappend r $msg
542}
543
danielk197732554c12005-01-22 03:39:39 +0000544# Usage: do_ioerr_test <test number> <options...>
545#
546# This proc is used to implement test cases that check that IO errors
547# are correctly handled. The first argument, <test number>, is an integer
548# used to name the tests executed by this proc. Options are as follows:
549#
550# -tclprep TCL script to run to prepare test.
551# -sqlprep SQL script to run to prepare test.
552# -tclbody TCL script to run with IO error simulation.
553# -sqlbody TCL script to run with IO error simulation.
554# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000555# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000556# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000557# -start Value of 'N' to begin with (default 1)
558#
559# -cksum Boolean. If true, test that the database does
560# not change during the execution of the test case.
561#
562proc do_ioerr_test {testname args} {
563
danielk197732554c12005-01-22 03:39:39 +0000564 set ::ioerropts(-start) 1
565 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000566 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000567 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000568 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000569 array set ::ioerropts $args
570
571 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000572 #reset_prng_state
573 save_prng_state
danielk197732554c12005-01-22 03:39:39 +0000574 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000575 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000576 incr ::ioerropts(-count) -1
577 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000578
579 # Skip this IO error if it was specified with the "-exclude" option.
580 if {[info exists ::ioerropts(-exclude)]} {
581 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
582 }
drh93aed5a2008-01-16 17:46:38 +0000583 restore_prng_state
danielk197732554c12005-01-22 03:39:39 +0000584
585 # Delete the files test.db and test2.db, then execute the TCL and
586 # SQL (in that order) to prepare for the test case.
587 do_test $testname.$n.1 {
588 set ::sqlite_io_error_pending 0
589 catch {db close}
590 catch {file delete -force test.db}
591 catch {file delete -force test.db-journal}
592 catch {file delete -force test2.db}
593 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000594 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000595 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000596 if {[info exists ::ioerropts(-tclprep)]} {
597 eval $::ioerropts(-tclprep)
598 }
599 if {[info exists ::ioerropts(-sqlprep)]} {
600 execsql $::ioerropts(-sqlprep)
601 }
602 expr 0
603 } {0}
604
605 # Read the 'checksum' of the database.
606 if {$::ioerropts(-cksum)} {
607 set checksum [cksum]
608 }
drh93aed5a2008-01-16 17:46:38 +0000609
danielk197732554c12005-01-22 03:39:39 +0000610 # Set the Nth IO error to fail.
611 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000612 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000613 set ::sqlite_io_error_pending $n
614 }] $n
615
616 # Create a single TCL script from the TCL and SQL specified
617 # as the body of the test.
618 set ::ioerrorbody {}
619 if {[info exists ::ioerropts(-tclbody)]} {
620 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
621 }
622 if {[info exists ::ioerropts(-sqlbody)]} {
623 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
624 }
625
626 # Execute the TCL Script created in the above block. If
627 # there are at least N IO operations performed by SQLite as
628 # a result of the script, the Nth will fail.
629 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000630 set ::sqlite_io_error_hit 0
631 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000632 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000633 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000634 set rc [sqlite3_errcode $::DB]
635 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000636 # If we are in extended result code mode, make sure all of the
637 # IOERRs we get back really do have their extended code values.
638 # If an extended result code is returned, the sqlite3_errcode
639 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
640 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000641 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
642 return $rc
643 }
644 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000645 # If we are not in extended result code mode, make sure no
646 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000647 if {[regexp {\+\d} $rc]} {
648 return $rc
649 }
650 }
drh93aed5a2008-01-16 17:46:38 +0000651 # The test repeats as long as $::go is non-zero. $::go starts out
652 # as 1. When a test runs to completion without hitting an I/O
653 # error, that means there is no point in continuing with this test
654 # case so set $::go to zero.
655 #
656 if {$::sqlite_io_error_pending>0} {
657 set ::go 0
658 set q 0
659 set ::sqlite_io_error_pending 0
660 } else {
661 set q 1
662 }
663
drhc9ac5ca2005-11-04 22:03:30 +0000664 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000665 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
666 set r 1
667 }
danielk1977f2fa8312006-01-24 13:09:33 +0000668 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000669
670 # One of two things must have happened. either
671 # 1. We never hit the IO error and the SQL returned OK
672 # 2. An IO error was hit and the SQL failed
673 #
drh93aed5a2008-01-16 17:46:38 +0000674 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000675 } {1}
676
677 # If an IO error occured, then the checksum of the database should
678 # be the same as before the script that caused the IO error was run.
drh1aa5af12008-03-07 19:51:14 +0000679 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk197732554c12005-01-22 03:39:39 +0000680 do_test $testname.$n.4 {
681 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000682 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000683 cksum
684 } $checksum
685 }
686
drh1aa5af12008-03-07 19:51:14 +0000687 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000688 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000689 if {[info exists ::ioerropts(-cleanup)]} {
690 catch $::ioerropts(-cleanup)
691 }
danielk197732554c12005-01-22 03:39:39 +0000692 }
693 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000694 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000695 unset ::ioerropts
696}
697
drh93aed5a2008-01-16 17:46:38 +0000698# Return a checksum based on the contents of the main database associated
699# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000700#
701proc cksum {{db db}} {
702 set txt [$db eval {
703 SELECT name, type, sql FROM sqlite_master order by name
704 }]\n
705 foreach tbl [$db eval {
706 SELECT name FROM sqlite_master WHERE type='table' order by name
707 }] {
708 append txt [$db eval "SELECT * FROM $tbl"]\n
709 }
710 foreach prag {default_synchronous default_cache_size} {
711 append txt $prag-[$db eval "PRAGMA $prag"]\n
712 }
713 set cksum [string length $txt]-[md5 $txt]
714 # puts $cksum-[file size test.db]
715 return $cksum
716}
717
drh93aed5a2008-01-16 17:46:38 +0000718# Generate a checksum based on the contents of the main and temp tables
719# database $db. If the checksum of two databases is the same, and the
720# integrity-check passes for both, the two databases are identical.
721#
drh1db639c2008-01-17 02:36:28 +0000722proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000723 set ret [list]
724 ifcapable tempdb {
725 set sql {
726 SELECT name FROM sqlite_master WHERE type = 'table' UNION
727 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
728 SELECT 'sqlite_master' UNION
729 SELECT 'sqlite_temp_master' ORDER BY 1
730 }
731 } else {
732 set sql {
733 SELECT name FROM sqlite_master WHERE type = 'table' UNION
734 SELECT 'sqlite_master' ORDER BY 1
735 }
736 }
737 set tbllist [$db eval $sql]
738 set txt {}
739 foreach tbl $tbllist {
740 append txt [$db eval "SELECT * FROM $tbl"]
741 }
742 foreach prag {default_cache_size} {
743 append txt $prag-[$db eval "PRAGMA $prag"]\n
744 }
745 # puts txt=$txt
746 return [md5 $txt]
747}
748
danielk197735754ac2008-03-21 17:29:37 +0000749proc memdebug_log_sql {{filename mallocs.sql}} {
750
danielk19776f332c12008-03-21 14:22:44 +0000751 set data [sqlite3_memdebug_log dump]
752 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000753 if {$nFrame < 0} { return "" }
754
danielk197735754ac2008-03-21 17:29:37 +0000755 set database temp
756
danielk19776f332c12008-03-21 14:22:44 +0000757 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
758 for {set ii 1} {$ii <= $nFrame} {incr ii} {
759 append tbl ", f${ii}"
760 }
761 append tbl ");\n"
762
763 set sql ""
764 foreach e $data {
765 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
766 foreach f [lrange $e 2 end] {
767 set frames($f) 1
768 }
769 }
770
771 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000772 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000773
774 foreach f [array names frames] {
775 set addr [format %x $f]
776 set cmd "addr2line -e [info nameofexec] $addr"
777 set line [eval exec $cmd]
778 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000779
780 set file [lindex [split $line :] 0]
781 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000782 }
783
danielk197735754ac2008-03-21 17:29:37 +0000784 foreach f [array names files] {
785 set contents ""
786 catch {
787 set fd [open $f]
788 set contents [read $fd]
789 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000790 }
danielk197735754ac2008-03-21 17:29:37 +0000791 set contents [string map {' ''} $contents]
792 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000793 }
danielk19776f332c12008-03-21 14:22:44 +0000794
danielk197735754ac2008-03-21 17:29:37 +0000795 set fd [open $filename w]
796 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
797 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000798}
drh93aed5a2008-01-16 17:46:38 +0000799
danielk197732554c12005-01-22 03:39:39 +0000800# Copy file $from into $to. This is used because some versions of
801# TCL for windows (notably the 8.4.1 binary package shipped with the
802# current mingw release) have a broken "file copy" command.
803#
804proc copy_file {from to} {
805 if {$::tcl_platform(platform)=="unix"} {
806 file copy -force $from $to
807 } else {
808 set f [open $from]
809 fconfigure $f -translation binary
810 set t [open $to w]
811 fconfigure $t -translation binary
812 puts -nonewline $t [read $f [file size $from]]
813 close $t
814 close $f
815 }
816}
817
danielk197745901d62004-11-10 15:27:38 +0000818# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
819# to non-zero, then set the global variable $AUTOVACUUM to 1.
820set AUTOVACUUM $sqlite_options(default_autovacuum)