blob: d8226e32f2e245c2b081749606d0b0627c53866f [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#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
danc1a60c52010-06-07 14:28:16 +000022# copy_file FROM TO
23# drop_all_table ?DB?
24# forcedelete FILENAME
25#
26# Test the capability of the SQLite version built into the interpreter to
27# determine if a specific test can be run:
28#
29# ifcapable EXPR
30#
31# Calulate checksums based on database contents:
32#
33# dbcksum DB DBNAME
34# allcksum ?DB?
35# cksum ?DB?
36#
37# Commands to execute/explain SQL statements:
38#
39# stepsql DB SQL
40# execsql2 SQL
41# explain_no_trace SQL
42# explain SQL ?DB?
43# catchsql SQL ?DB?
44# execsql SQL ?DB?
45#
46# Commands to run test cases:
47#
48# do_ioerr_test TESTNAME ARGS...
49# crashsql ARGS...
50# integrity_check TESTNAME ?DB?
51# do_test TESTNAME SCRIPT EXPECTED
52#
dan430e74c2010-06-07 17:47:26 +000053# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000054#
55# set_test_counter COUNTER ?VALUE?
56# omit_test TESTNAME REASON
57# fail_test TESTNAME
58# incr_ntest
59#
60# Command run at the end of each test file:
61#
62# finish_test
63#
dan430e74c2010-06-07 17:47:26 +000064# Commands to help create test files that run with the "WAL" and other
65# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000066#
67# wal_is_wal_mode
68# wal_set_journal_mode ?DB?
69# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000070# permutation
danc1a60c52010-06-07 14:28:16 +000071#
drh348784e2000-05-29 20:41:49 +000072
danc1a60c52010-06-07 14:28:16 +000073# Set the precision of FP arithmatic used by the interpreter. And
74# configure SQLite to take database file locks on the page that begins
75# 64KB into the database file instead of the one 1GB in. This means
76# the code that handles that special case can be tested without creating
77# very large database files.
78#
drh92febd92004-08-20 18:34:20 +000079set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000080sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000081
danc1a60c52010-06-07 14:28:16 +000082
83# If the pager codec is available, create a wrapper for the [sqlite3]
84# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000085#
danc1a60c52010-06-07 14:28:16 +000086# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000087#
danc1a60c52010-06-07 14:28:16 +000088# becomes
drh4a50aac2007-08-23 02:47:53 +000089#
danc1a60c52010-06-07 14:28:16 +000090# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000091#
dan430e74c2010-06-07 17:47:26 +000092if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000093 rename sqlite3 sqlite_orig
94 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000095 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +000096 # This command is opening a new database connection.
97 #
98 if {[info exists ::G(perm:sqlite3_args)]} {
99 set args [concat $args $::G(perm:sqlite3_args)]
100 }
101 if {[sqlite_orig -has-codec]} {
102 lappend args -key {xyzzy}
103 }
104
dan3ccd20a2010-06-09 19:01:02 +0000105 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000106
107 if {[info exists ::G(perm:presql)]} {
108 [lindex $args 0] eval $::G(perm:presql)
109 }
dan3ccd20a2010-06-09 19:01:02 +0000110
111 set res
dan430e74c2010-06-07 17:47:26 +0000112 } else {
113 # This command is not opening a new database connection. Pass the
114 # arguments through to the C implemenation as the are.
115 #
116 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000117 }
drh9eb9e262004-02-11 02:18:05 +0000118 }
119}
120
dan430e74c2010-06-07 17:47:26 +0000121# The following block only runs the first time this file is sourced. It
122# does not run in slave interpreters (since the ::cmdlinearg array is
123# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000124#
danc1a60c52010-06-07 14:28:16 +0000125if {[info exists cmdlinearg]==0} {
126
127 # Parse any options specified in the $argv array. This script accepts the
128 # following options:
129 #
130 # --pause
131 # --soft-heap-limit=NN
132 # --maxerror=NN
133 # --malloctrace=N
134 # --backtrace=N
135 # --binarylog=N
136 #
137 set cmdlinearg(soft-heap-limit) 0
138 set cmdlinearg(maxerror) 1000
139 set cmdlinearg(malloctrace) 0
140 set cmdlinearg(backtrace) 10
141 set cmdlinearg(binarylog) 0
142
143 set leftover [list]
144 foreach a $argv {
145 switch -regexp -- $a {
146 {^-+pause$} {
147 # Wait for user input before continuing. This is to give the user an
148 # opportunity to connect profiling tools to the process.
149 puts -nonewline "Press RETURN to begin..."
150 flush stdout
151 gets stdin
152 }
153 {^-+soft-heap-limit=.+$} {
154 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
155 }
156 {^-+maxerror=.+$} {
157 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
158 }
159 {^-+malloctrace=.+$} {
160 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
161 if {$cmdlinearg(malloctrace)} {
162 sqlite3_memdebug_log start
163 }
164 }
165 {^-+backtrace=.+$} {
166 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
167 }
168 sqlite3_memdebug_backtrace $value
169 {^-+binarylog=.+$} {
170 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
171 }
172 default {
173 lappend leftover $a
174 }
175 }
176 }
177 set argv $leftover
178
dan430e74c2010-06-07 17:47:26 +0000179 # Install the malloc layer used to inject OOM errors. And the 'automatic'
180 # extensions. This only needs to be done once for the process.
181 #
danielk1977f7b9d662008-06-23 18:49:43 +0000182 sqlite3_shutdown
183 install_malloc_faultsim 1
184 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000185 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000186
dan430e74c2010-06-07 17:47:26 +0000187 # If the --binarylog option was specified, create the logging VFS. This
188 # call installs the new VFS as the default for all SQLite connections.
189 #
danc1a60c52010-06-07 14:28:16 +0000190 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000191 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000192 }
193
194 # Set the backtrace depth, if malloc tracing is enabled.
195 #
196 if {$cmdlinearg(malloctrace)} {
197 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000198 }
danielk1977f7b9d662008-06-23 18:49:43 +0000199}
danielk197703ba3fa2009-01-09 10:49:14 +0000200
danc1a60c52010-06-07 14:28:16 +0000201# Update the soft-heap-limit each time this script is run. In that
202# way if an individual test file changes the soft-heap-limit, it
203# will be reset at the start of the next test file.
204#
205sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
206
207# Create a test database
208#
danielk197703ba3fa2009-01-09 10:49:14 +0000209proc reset_db {} {
210 catch {db close}
211 file delete -force test.db
212 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000213 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000214 sqlite3 db ./test.db
215 set ::DB [sqlite3_connection_pointer db]
216 if {[info exists ::SETUP_SQL]} {
217 db eval $::SETUP_SQL
218 }
drhcd61c282002-03-06 22:01:34 +0000219}
danielk197703ba3fa2009-01-09 10:49:14 +0000220reset_db
drhbec2bf42000-05-29 23:48:22 +0000221
222# Abort early if this script has been run before.
223#
danc1a60c52010-06-07 14:28:16 +0000224if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000225
danc1a60c52010-06-07 14:28:16 +0000226# Initialize the test counters and set up commands to access them.
227# Or, if this is a slave interpreter, set up aliases to write the
228# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000229#
danc1a60c52010-06-07 14:28:16 +0000230if {0==[info exists ::SLAVE]} {
231 set TC(errors) 0
232 set TC(count) 0
233 set TC(fail_list) [list]
234 set TC(omit_list) [list]
235
236 proc set_test_counter {counter args} {
237 if {[llength $args]} {
238 set ::TC($counter) [lindex $args 0]
239 }
240 set ::TC($counter)
241 }
drhb62c3352006-11-23 09:39:16 +0000242}
drh348784e2000-05-29 20:41:49 +0000243
drh521cc842008-04-15 02:36:33 +0000244# Record the fact that a sequence of tests were omitted.
245#
246proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000247 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000248 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000249 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000250}
251
danc1a60c52010-06-07 14:28:16 +0000252# Record the fact that a test failed.
253#
254proc fail_test {name} {
255 set f [set_test_counter fail_list]
256 lappend f $name
257 set_test_counter fail_list $f
258 set_test_counter errors [expr [set_test_counter errors] + 1]
259
260 set nFail [set_test_counter errors]
261 if {$nFail>=$::cmdlinearg(maxerror)} {
262 puts "*** Giving up..."
263 finalize_testing
264 }
265}
266
267# Increment the number of tests run
268#
269proc incr_ntest {} {
270 set_test_counter count [expr [set_test_counter count] + 1]
271}
272
273
drh348784e2000-05-29 20:41:49 +0000274# Invoke the do_test procedure to run a single test
275#
276proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000277
278 global argv cmdlinearg
279
drh4a50aac2007-08-23 02:47:53 +0000280 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000281
drh767c2002000-10-19 14:10:08 +0000282 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000283 set go 1
284 } else {
285 set go 0
286 foreach pattern $argv {
287 if {[string match $pattern $name]} {
288 set go 1
289 break
290 }
291 }
292 }
293 if {!$go} return
dan430e74c2010-06-07 17:47:26 +0000294
295 if {[info exists ::G(perm:name)]} {
296 set name "$::G(perm:name)-$name"
297 }
298
danc1a60c52010-06-07 14:28:16 +0000299 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000300 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000301 flush stdout
302 if {[catch {uplevel #0 "$cmd;\n"} result]} {
303 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000304 fail_test $name
drh348784e2000-05-29 20:41:49 +0000305 } elseif {[string compare $result $expected]} {
306 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000307 fail_test $name
drh348784e2000-05-29 20:41:49 +0000308 } else {
309 puts " Ok"
310 }
drh6558db82007-04-13 03:23:21 +0000311 flush stdout
drh348784e2000-05-29 20:41:49 +0000312}
313
drhb62c3352006-11-23 09:39:16 +0000314# Run an SQL script.
315# Return the number of microseconds per statement.
316#
drh3590f152006-11-23 21:09:10 +0000317proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000318 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000319 flush stdout
320 set speed [time {sqlite3_exec_nr db $sql}]
321 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000322 if {$tm == 0} {
323 set rate [format %20s "many"]
324 } else {
325 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
326 }
drh3590f152006-11-23 21:09:10 +0000327 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000328 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000329 global total_time
330 set total_time [expr {$total_time+$tm}]
331}
drh45c236d2008-03-22 01:08:00 +0000332proc speed_trial_tcl {name numstmt units script} {
333 puts -nonewline [format {%-21.21s } $name...]
334 flush stdout
335 set speed [time {eval $script}]
336 set tm [lindex $speed 0]
337 if {$tm == 0} {
338 set rate [format %20s "many"]
339 } else {
340 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
341 }
342 set u2 $units/s
343 puts [format {%12d uS %s %s} $tm $rate $u2]
344 global total_time
345 set total_time [expr {$total_time+$tm}]
346}
drhdd924312007-03-31 22:34:16 +0000347proc speed_trial_init {name} {
348 global total_time
349 set total_time 0
350}
351proc speed_trial_summary {name} {
352 global total_time
353 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000354}
355
drh348784e2000-05-29 20:41:49 +0000356# Run this routine last
357#
358proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000359 catch {db close}
360 catch {db2 close}
361 catch {db3 close}
362 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000363}
364proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000365 global sqlite_open_file_count
366
367 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000368
drh6e142f52000-06-08 13:36:40 +0000369 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000370 catch {db2 close}
371 catch {db3 close}
372
drh9bc54492007-10-23 14:49:59 +0000373 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000374 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000375 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000376 db close
drh984bfaa2008-03-19 16:08:53 +0000377 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000378
drh3a7fb7c2007-08-10 16:41:08 +0000379 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000380 set nTest [incr_ntest]
381 set nErr [set_test_counter errors]
382
drh348784e2000-05-29 20:41:49 +0000383 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000384 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000385 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000386 }
danielk1977ee8b7992009-03-26 17:13:06 +0000387 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000388 if {[llength $omitList]>0} {
389 puts "Omitted test cases:"
390 set prec {}
391 foreach {rec} [lsort $omitList] {
392 if {$rec==$prec} continue
393 set prec $rec
394 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
395 }
396 }
drh80788d82006-09-02 14:50:23 +0000397 if {$nErr>0 && ![working_64bit_int]} {
398 puts "******************************************************************"
399 puts "N.B.: The version of TCL that you used to build this test harness"
400 puts "is defective in that it does not support 64-bit integers. Some or"
401 puts "all of the test failures above might be a result from this defect"
402 puts "in your TCL build."
403 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000404 }
danc1a60c52010-06-07 14:28:16 +0000405 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000406 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000407 }
drh94e92032003-02-16 22:21:32 +0000408 if {$sqlite_open_file_count} {
409 puts "$sqlite_open_file_count files were left open"
410 incr nErr
411 }
drhf3a65f72007-08-22 20:18:21 +0000412 if {[sqlite3_memory_used]>0} {
413 puts "Unfreed memory: [sqlite3_memory_used] bytes"
414 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000415 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000416 puts "Writing unfreed memory log to \"./memleak.txt\""
417 sqlite3_memdebug_dump ./memleak.txt
418 }
drhf3a65f72007-08-22 20:18:21 +0000419 } else {
420 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000421 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000422 sqlite3_memdebug_dump ./memusage.txt
423 }
drhf3a65f72007-08-22 20:18:21 +0000424 }
drhf7141992008-06-19 00:16:08 +0000425 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000426 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
427 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000428 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
429 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
430 }
danc1a60c52010-06-07 14:28:16 +0000431 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000432 puts "Writing mallocs.sql..."
433 memdebug_log_sql
434 sqlite3_memdebug_log stop
435 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000436
437 if {[sqlite3_memory_used]>0} {
438 puts "Writing leaks.sql..."
439 sqlite3_memdebug_log sync
440 memdebug_log_sql leaks.sql
441 }
danielk197735754ac2008-03-21 17:29:37 +0000442 }
drhd9910fe2006-10-04 11:55:49 +0000443 foreach f [glob -nocomplain test.db-*-journal] {
444 file delete -force $f
445 }
446 foreach f [glob -nocomplain test.db-mj*] {
447 file delete -force $f
448 }
drhdb25e382001-03-15 18:21:22 +0000449 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000450}
451
drhf7141992008-06-19 00:16:08 +0000452# Display memory statistics for analysis and debugging purposes.
453#
454proc show_memstats {} {
455 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000456 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
457 set val [format {now %10d max %10d max-size %10d} \
458 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000459 puts "Memory used: $val"
460 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000461 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
462 set val [format {now %10d max %10d max-size %10d} \
463 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000464 puts "Page-cache used: $val"
465 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
466 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
467 puts "Page-cache overflow: $val"
468 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
469 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
470 puts "Scratch memory used: $val"
471 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000472 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
473 set val [format {now %10d max %10d max-size %10d} \
474 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000475 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000476 ifcapable yytrackmaxstackdepth {
477 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
478 set val [format { max %10d} [lindex $x 2]]
479 puts "Parser stack depth: $val"
480 }
drhf7141992008-06-19 00:16:08 +0000481}
482
drh348784e2000-05-29 20:41:49 +0000483# A procedure to execute SQL
484#
drhc4a3c772001-04-04 11:48:57 +0000485proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000486 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000487 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000488}
drh3aadb2e2000-05-31 17:59:25 +0000489
drhadbca9c2001-09-27 15:11:53 +0000490# Execute SQL and catch exceptions.
491#
492proc catchsql {sql {db db}} {
493 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000494 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000495 lappend r $msg
496 return $r
497}
498
drh04096482001-11-09 22:41:44 +0000499# Do an VDBE code dump on the SQL given
500#
501proc explain {sql {db db}} {
502 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000503 puts "addr opcode p1 p2 p3 p4 p5 #"
504 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000505 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000506 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
507 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
508 ]
drh04096482001-11-09 22:41:44 +0000509 }
510}
511
drhdafc0ce2008-04-17 19:14:02 +0000512# Show the VDBE program for an SQL statement but omit the Trace
513# opcode at the beginning. This procedure can be used to prove
514# that different SQL statements generate exactly the same VDBE code.
515#
516proc explain_no_trace {sql} {
517 set tr [db eval "EXPLAIN $sql"]
518 return [lrange $tr 7 end]
519}
520
drh3aadb2e2000-05-31 17:59:25 +0000521# Another procedure to execute SQL. This one includes the field
522# names in the returned list.
523#
524proc execsql2 {sql} {
525 set result {}
526 db eval $sql data {
527 foreach f $data(*) {
528 lappend result $f $data($f)
529 }
530 }
531 return $result
532}
drh17a68932001-01-31 13:28:08 +0000533
drhdde85d92003-03-01 19:45:34 +0000534# Use the non-callback API to execute multiple SQL statements
535#
536proc stepsql {dbptr sql} {
537 set sql [string trim $sql]
538 set r 0
539 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000540 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000541 return [list 1 $vm]
542 }
543 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000544# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
545# foreach v $VAL {lappend r $v}
546# }
547 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
548 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
549 lappend r [sqlite3_column_text $vm $i]
550 }
drhdde85d92003-03-01 19:45:34 +0000551 }
danielk1977106bb232004-05-21 10:08:53 +0000552 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000553 return [list 1 $errmsg]
554 }
555 }
556 return $r
557}
558
drh17a68932001-01-31 13:28:08 +0000559# Delete a file or directory
560#
561proc forcedelete {filename} {
562 if {[catch {file delete -force $filename}]} {
563 exec rm -rf $filename
564 }
565}
drh21504322002-06-25 13:16:02 +0000566
567# Do an integrity check of the entire database
568#
danielk197704103022009-02-03 16:51:24 +0000569proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000570 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000571 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000572 }
573}
574
danielk1977db4e8862008-01-16 08:24:46 +0000575proc fix_ifcapable_expr {expr} {
576 set ret ""
577 set state 0
578 for {set i 0} {$i < [string length $expr]} {incr i} {
579 set char [string range $expr $i $i]
580 set newstate [expr {[string is alnum $char] || $char eq "_"}]
581 if {$newstate && !$state} {
582 append ret {$::sqlite_options(}
583 }
584 if {!$newstate && $state} {
585 append ret )
586 }
587 append ret $char
588 set state $newstate
589 }
590 if {$state} {append ret )}
591 return $ret
592}
593
drh27d258a2004-10-30 20:23:09 +0000594# Evaluate a boolean expression of capabilities. If true, execute the
595# code. Omit the code if false.
596#
danielk19773e8c37e2005-01-21 03:12:14 +0000597proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000598 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
599 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000600 if ($e2) {
601 set c [catch {uplevel 1 $code} r]
602 } else {
603 set c [catch {uplevel 1 $elsecode} r]
604 }
605 return -code $c $r
drh21504322002-06-25 13:16:02 +0000606}
danielk197745901d62004-11-10 15:27:38 +0000607
danielk1977aca790a2005-01-13 11:07:52 +0000608# This proc execs a seperate process that crashes midway through executing
609# the SQL script $sql on database test.db.
610#
611# The crash occurs during a sync() of file $crashfile. When the crash
612# occurs a random subset of all unsynced writes made by the process are
613# written into the files on disk. Argument $crashdelay indicates the
614# number of file syncs to wait before crashing.
615#
616# The return value is a list of two elements. The first element is a
617# boolean, indicating whether or not the process actually crashed or
618# reported some other error. The second element in the returned list is the
619# error message. This is "child process exited abnormally" if the crash
620# occured.
621#
danielk1977f8940ae2007-08-23 11:07:10 +0000622# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000623#
624proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000625 if {$::tcl_platform(platform)!="unix"} {
626 error "crashsql should only be used on unix"
627 }
danielk197759a33f92007-03-17 10:26:59 +0000628
629 set blocksize ""
630 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000631 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000632 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000633 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000634 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000635 set sql [lindex $args end]
636
637 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
638 set z [lindex $args $ii]
639 set n [string length $z]
640 set z2 [lindex $args [expr $ii+1]]
641
642 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000643 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000644 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000645 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000646 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000647 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000648 else { error "Unrecognized option: $z" }
649 }
650
651 if {$crashfile eq ""} {
652 error "Compulsory option -file missing"
653 }
654
danielk1977aca790a2005-01-13 11:07:52 +0000655 set cfile [file join [pwd] $crashfile]
656
657 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000658 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000659 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000660 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000661 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000662
663 # This block sets the cache size of the main database to 10
664 # pages. This is done in case the build is configured to omit
665 # "PRAGMA cache_size".
666 puts $f {db eval {SELECT * FROM sqlite_master;}}
667 puts $f {set bt [btree_from_db db]}
668 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000669 if {$prngseed} {
670 set seed [expr {$prngseed%10007+1}]
671 # puts seed=$seed
672 puts $f "db eval {SELECT randomblob($seed)}"
673 }
danielk1977933bbd62007-03-17 07:22:42 +0000674
drhf8587402008-01-08 16:03:49 +0000675 if {[string length $tclbody]>0} {
676 puts $f $tclbody
677 }
678 if {[string length $sql]>0} {
679 puts $f "db eval {"
680 puts $f "$sql"
681 puts $f "}"
682 }
danielk1977aca790a2005-01-13 11:07:52 +0000683 close $f
684
685 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000686 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000687 } msg]
688 lappend r $msg
689}
690
danielk197732554c12005-01-22 03:39:39 +0000691# Usage: do_ioerr_test <test number> <options...>
692#
693# This proc is used to implement test cases that check that IO errors
694# are correctly handled. The first argument, <test number>, is an integer
695# used to name the tests executed by this proc. Options are as follows:
696#
697# -tclprep TCL script to run to prepare test.
698# -sqlprep SQL script to run to prepare test.
699# -tclbody TCL script to run with IO error simulation.
700# -sqlbody TCL script to run with IO error simulation.
701# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000702# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000703# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000704# -start Value of 'N' to begin with (default 1)
705#
706# -cksum Boolean. If true, test that the database does
707# not change during the execution of the test case.
708#
709proc do_ioerr_test {testname args} {
710
danielk197732554c12005-01-22 03:39:39 +0000711 set ::ioerropts(-start) 1
712 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000713 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000714 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000715 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000716 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000717 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000718 array set ::ioerropts $args
719
danielk197747cd39c2008-05-12 12:41:15 +0000720 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
721 # a couple of obscure IO errors that do not return them.
722 set ::ioerropts(-erc) 0
723
danielk197732554c12005-01-22 03:39:39 +0000724 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000725 #reset_prng_state
726 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000727 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000728 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000729 incr ::ioerropts(-count) -1
730 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000731
732 # Skip this IO error if it was specified with the "-exclude" option.
733 if {[info exists ::ioerropts(-exclude)]} {
734 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
735 }
danielk1977861f7452008-06-05 11:39:11 +0000736 if {$::ioerropts(-restoreprng)} {
737 restore_prng_state
738 }
danielk197732554c12005-01-22 03:39:39 +0000739
740 # Delete the files test.db and test2.db, then execute the TCL and
741 # SQL (in that order) to prepare for the test case.
742 do_test $testname.$n.1 {
743 set ::sqlite_io_error_pending 0
744 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000745 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000746 catch {file delete -force test.db}
747 catch {file delete -force test.db-journal}
748 catch {file delete -force test2.db}
749 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000750 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000751 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000752 if {[info exists ::ioerropts(-tclprep)]} {
753 eval $::ioerropts(-tclprep)
754 }
755 if {[info exists ::ioerropts(-sqlprep)]} {
756 execsql $::ioerropts(-sqlprep)
757 }
758 expr 0
759 } {0}
760
761 # Read the 'checksum' of the database.
762 if {$::ioerropts(-cksum)} {
763 set checksum [cksum]
764 }
drh93aed5a2008-01-16 17:46:38 +0000765
danielk197732554c12005-01-22 03:39:39 +0000766 # Set the Nth IO error to fail.
767 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000768 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000769 set ::sqlite_io_error_pending $n
770 }] $n
771
772 # Create a single TCL script from the TCL and SQL specified
773 # as the body of the test.
774 set ::ioerrorbody {}
775 if {[info exists ::ioerropts(-tclbody)]} {
776 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
777 }
778 if {[info exists ::ioerropts(-sqlbody)]} {
779 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
780 }
781
782 # Execute the TCL Script created in the above block. If
783 # there are at least N IO operations performed by SQLite as
784 # a result of the script, the Nth will fail.
785 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000786 set ::sqlite_io_error_hit 0
787 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000788 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000789 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000790 set rc [sqlite3_errcode $::DB]
791 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000792 # If we are in extended result code mode, make sure all of the
793 # IOERRs we get back really do have their extended code values.
794 # If an extended result code is returned, the sqlite3_errcode
795 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
796 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000797 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
798 return $rc
799 }
800 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000801 # If we are not in extended result code mode, make sure no
802 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000803 if {[regexp {\+\d} $rc]} {
804 return $rc
805 }
806 }
drh93aed5a2008-01-16 17:46:38 +0000807 # The test repeats as long as $::go is non-zero. $::go starts out
808 # as 1. When a test runs to completion without hitting an I/O
809 # error, that means there is no point in continuing with this test
810 # case so set $::go to zero.
811 #
812 if {$::sqlite_io_error_pending>0} {
813 set ::go 0
814 set q 0
815 set ::sqlite_io_error_pending 0
816 } else {
817 set q 1
818 }
819
drhc9ac5ca2005-11-04 22:03:30 +0000820 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000821 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
822 set r 1
823 }
danielk1977f2fa8312006-01-24 13:09:33 +0000824 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000825
826 # One of two things must have happened. either
827 # 1. We never hit the IO error and the SQL returned OK
828 # 2. An IO error was hit and the SQL failed
829 #
dand41a29a2010-05-06 15:56:28 +0000830 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000831 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000832 } {1}
833
danielk197780daec62008-05-12 10:57:02 +0000834 set ::sqlite_io_error_hit 0
835 set ::sqlite_io_error_pending 0
836
danielk197752b472a2008-05-05 16:23:55 +0000837 # Check that no page references were leaked. There should be
838 # a single reference if there is still an active transaction,
839 # or zero otherwise.
840 #
danielk197781620542008-06-07 05:19:37 +0000841 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
842 # locks are established on database files (i.e. if the error
843 # occurs while attempting to detect a hot-journal file), then
844 # there may 0 page references and an active transaction according
845 # to [sqlite3_get_autocommit].
846 #
danielk19774abd5442008-05-05 15:26:50 +0000847 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000848 do_test $testname.$n.4 {
849 set bt [btree_from_db db]
850 db_enter db
851 array set stats [btree_pager_stats $bt]
852 db_leave db
danielk197781620542008-06-07 05:19:37 +0000853 set nRef $stats(ref)
854 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
855 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000856 }
857
danielk197752b472a2008-05-05 16:23:55 +0000858 # If there is an open database handle and no open transaction,
859 # and the pager is not running in exclusive-locking mode,
860 # check that the pager is in "unlocked" state. Theoretically,
861 # if a call to xUnlock() failed due to an IO error the underlying
862 # file may still be locked.
863 #
864 ifcapable pragma {
865 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000866 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000867 && [db one {pragma locking_mode}] eq "normal"
868 && [sqlite3_get_autocommit db]
869 } {
870 do_test $testname.$n.5 {
871 set bt [btree_from_db db]
872 db_enter db
873 array set stats [btree_pager_stats $bt]
874 db_leave db
875 set stats(state)
876 } 0
877 }
878 }
879
danielk197732554c12005-01-22 03:39:39 +0000880 # If an IO error occured, then the checksum of the database should
881 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000882 #
drh1aa5af12008-03-07 19:51:14 +0000883 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000884 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000885 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000886 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000887 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000888 cksum
889 } $checksum
890 }
891
drh1aa5af12008-03-07 19:51:14 +0000892 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000893 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000894 if {[info exists ::ioerropts(-cleanup)]} {
895 catch $::ioerropts(-cleanup)
896 }
danielk197732554c12005-01-22 03:39:39 +0000897 }
898 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000899 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000900 unset ::ioerropts
901}
902
drh93aed5a2008-01-16 17:46:38 +0000903# Return a checksum based on the contents of the main database associated
904# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000905#
906proc cksum {{db db}} {
907 set txt [$db eval {
908 SELECT name, type, sql FROM sqlite_master order by name
909 }]\n
910 foreach tbl [$db eval {
911 SELECT name FROM sqlite_master WHERE type='table' order by name
912 }] {
913 append txt [$db eval "SELECT * FROM $tbl"]\n
914 }
915 foreach prag {default_synchronous default_cache_size} {
916 append txt $prag-[$db eval "PRAGMA $prag"]\n
917 }
918 set cksum [string length $txt]-[md5 $txt]
919 # puts $cksum-[file size test.db]
920 return $cksum
921}
922
drh93aed5a2008-01-16 17:46:38 +0000923# Generate a checksum based on the contents of the main and temp tables
924# database $db. If the checksum of two databases is the same, and the
925# integrity-check passes for both, the two databases are identical.
926#
drh1db639c2008-01-17 02:36:28 +0000927proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000928 set ret [list]
929 ifcapable tempdb {
930 set sql {
931 SELECT name FROM sqlite_master WHERE type = 'table' UNION
932 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
933 SELECT 'sqlite_master' UNION
934 SELECT 'sqlite_temp_master' ORDER BY 1
935 }
936 } else {
937 set sql {
938 SELECT name FROM sqlite_master WHERE type = 'table' UNION
939 SELECT 'sqlite_master' ORDER BY 1
940 }
941 }
942 set tbllist [$db eval $sql]
943 set txt {}
944 foreach tbl $tbllist {
945 append txt [$db eval "SELECT * FROM $tbl"]
946 }
947 foreach prag {default_cache_size} {
948 append txt $prag-[$db eval "PRAGMA $prag"]\n
949 }
950 # puts txt=$txt
951 return [md5 $txt]
952}
953
drhdc2c4912009-02-04 22:46:47 +0000954# Generate a checksum based on the contents of a single database with
955# a database connection. The name of the database is $dbname.
956# Examples of $dbname are "temp" or "main".
957#
958proc dbcksum {db dbname} {
959 if {$dbname=="temp"} {
960 set master sqlite_temp_master
961 } else {
962 set master $dbname.sqlite_master
963 }
964 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
965 set txt [$db eval "SELECT * FROM $master"]\n
966 foreach tab $alltab {
967 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
968 }
969 return [md5 $txt]
970}
971
danielk197735754ac2008-03-21 17:29:37 +0000972proc memdebug_log_sql {{filename mallocs.sql}} {
973
danielk19776f332c12008-03-21 14:22:44 +0000974 set data [sqlite3_memdebug_log dump]
975 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000976 if {$nFrame < 0} { return "" }
977
danielk197735754ac2008-03-21 17:29:37 +0000978 set database temp
979
danielk19776ab3a2e2009-02-19 14:39:25 +0000980 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +0000981
982 set sql ""
983 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +0000984 set nCall [lindex $e 0]
985 set nByte [lindex $e 1]
986 set lStack [lrange $e 2 end]
987 append sql "INSERT INTO ${database}.malloc VALUES"
988 append sql "('test', $nCall, $nByte, '$lStack');\n"
989 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +0000990 set frames($f) 1
991 }
992 }
993
994 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000995 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000996
997 foreach f [array names frames] {
998 set addr [format %x $f]
999 set cmd "addr2line -e [info nameofexec] $addr"
1000 set line [eval exec $cmd]
1001 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001002
1003 set file [lindex [split $line :] 0]
1004 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001005 }
1006
danielk197735754ac2008-03-21 17:29:37 +00001007 foreach f [array names files] {
1008 set contents ""
1009 catch {
1010 set fd [open $f]
1011 set contents [read $fd]
1012 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001013 }
danielk197735754ac2008-03-21 17:29:37 +00001014 set contents [string map {' ''} $contents]
1015 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001016 }
danielk19776f332c12008-03-21 14:22:44 +00001017
danielk197735754ac2008-03-21 17:29:37 +00001018 set fd [open $filename w]
1019 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1020 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001021}
drh93aed5a2008-01-16 17:46:38 +00001022
danielk197732554c12005-01-22 03:39:39 +00001023# Copy file $from into $to. This is used because some versions of
1024# TCL for windows (notably the 8.4.1 binary package shipped with the
1025# current mingw release) have a broken "file copy" command.
1026#
1027proc copy_file {from to} {
1028 if {$::tcl_platform(platform)=="unix"} {
1029 file copy -force $from $to
1030 } else {
1031 set f [open $from]
1032 fconfigure $f -translation binary
1033 set t [open $to w]
1034 fconfigure $t -translation binary
1035 puts -nonewline $t [read $f [file size $from]]
1036 close $t
1037 close $f
1038 }
1039}
1040
danf5894502009-10-07 18:41:19 +00001041# Drop all tables in database [db]
1042proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001043 ifcapable trigger&&foreignkey {
1044 set pk [$db one "PRAGMA foreign_keys"]
1045 $db eval "PRAGMA foreign_keys = OFF"
1046 }
drh9a6ffc82010-02-15 18:03:20 +00001047 foreach {idx name file} [db eval {PRAGMA database_list}] {
1048 if {$idx==1} {
1049 set master sqlite_temp_master
1050 } else {
1051 set master $name.sqlite_master
1052 }
1053 foreach {t type} [$db eval "
1054 SELECT name, type FROM $master
1055 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1056 "] {
1057 $db eval "DROP $type $t"
1058 }
danf5894502009-10-07 18:41:19 +00001059 }
shaneh4e7b32f2009-12-17 22:12:51 +00001060 ifcapable trigger&&foreignkey {
1061 $db eval "PRAGMA foreign_keys = $pk"
1062 }
danf5894502009-10-07 18:41:19 +00001063}
1064
dan71cb5182010-04-26 12:39:03 +00001065#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001066# If a test script is executed with global variable $::G(perm:name) set to
1067# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1068# in rollback mode. The following Tcl procs are used to make this less
1069# intrusive:
dan71cb5182010-04-26 12:39:03 +00001070#
1071# wal_set_journal_mode ?DB?
1072#
1073# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1074# connection handle DB. Otherwise, this command is a no-op.
1075#
1076# wal_check_journal_mode TESTNAME ?DB?
1077#
1078# If running a WAL test, execute a tests case that fails if the main
1079# database for connection handle DB is not currently a WAL database.
1080# Otherwise (if not running a WAL permutation) this is a no-op.
1081#
1082# wal_is_wal_mode
1083#
1084# Returns true if this test should be run in WAL mode. False otherwise.
1085#
1086proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001087 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001088}
1089proc wal_set_journal_mode {{db db}} {
1090 if { [wal_is_wal_mode] } {
1091 $db eval "PRAGMA journal_mode = WAL"
1092 }
1093}
1094proc wal_check_journal_mode {testname {db db}} {
1095 if { [wal_is_wal_mode] } {
1096 $db eval { SELECT * FROM sqlite_master }
1097 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1098 }
1099}
1100
dan430e74c2010-06-07 17:47:26 +00001101proc permutation {} {
1102 set perm ""
1103 catch {set perm $::G(perm:name)}
1104 set perm
1105}
1106
danc1a60c52010-06-07 14:28:16 +00001107#-------------------------------------------------------------------------
1108#
1109proc slave_test_script {script} {
1110
1111 # Create the interpreter used to run the test script.
1112 interp create tinterp
1113
1114 # Populate some global variables that tester.tcl expects to see.
1115 foreach {var value} [list \
1116 ::argv0 $::argv0 \
1117 ::argv {} \
1118 ::SLAVE 1 \
1119 ] {
1120 interp eval tinterp [list set $var $value]
1121 }
1122
1123 # The alias used to access the global test counters.
1124 tinterp alias set_test_counter set_test_counter
1125
1126 # Set up the ::cmdlinearg array in the slave.
1127 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1128
dan430e74c2010-06-07 17:47:26 +00001129 # Set up the ::G array in the slave.
1130 interp eval tinterp [list array set ::G [array get ::G]]
1131
danc1a60c52010-06-07 14:28:16 +00001132 # Load the various test interfaces implemented in C.
1133 load_testfixture_extensions tinterp
1134
1135 # Run the test script.
1136 interp eval tinterp $script
1137
1138 # Delete the interpreter used to run the test script.
1139 interp delete tinterp
1140}
1141
dan430e74c2010-06-07 17:47:26 +00001142proc slave_test_file {zFile} {
1143
1144 set ::sqlite_open_file_count 0
1145
danc1a60c52010-06-07 14:28:16 +00001146 set time [time {
1147 slave_test_script [list source $zFile]
1148 }]
dan430e74c2010-06-07 17:47:26 +00001149
1150 set tail [file tail $zFile]
1151 if {$::sqlite_open_file_count>0} {
1152 puts "$tail did not close all files: $::sqlite_open_file_count"
1153 fail_test $tail
1154 set ::sqlite_open_file_count 0
1155 exit
1156 }
1157 puts "time $tail [lrange $time 0 1]"
1158
1159 show_memstats
danc1a60c52010-06-07 14:28:16 +00001160}
1161
danf5894502009-10-07 18:41:19 +00001162
danielk197745901d62004-11-10 15:27:38 +00001163# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1164# to non-zero, then set the global variable $AUTOVACUUM to 1.
1165set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001166
1167source $testdir/thread_common.tcl