blob: 17f6b4d206c3a762df7ed049dac892e62a045343 [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#
drh5f7b5bf2007-04-19 12:30:54 +000014# $Id: tester.tcl,v 1.79 2007/04/19 12:30:54 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drhef4ac8f2004-06-19 00:16:31 +000016# Make sure tclsqlite3 was compiled correctly. Abort now with an
drhfbc3eab2001-04-06 16:13:42 +000017# error message if not.
18#
drhef4ac8f2004-06-19 00:16:31 +000019if {[sqlite3 -tcl-uses-utf]} {
drhfbc3eab2001-04-06 16:13:42 +000020 if {"\u1234"=="u1234"} {
21 puts stderr "***** BUILD PROBLEM *****"
22 puts stderr "$argv0 was linked against an older version"
23 puts stderr "of TCL that does not support Unicode, but uses a header"
24 puts stderr "file (\"tcl.h\") from a new TCL version that does support"
25 puts stderr "Unicode. This combination causes internal errors."
26 puts stderr "Recompile using a TCL library and header file that match"
27 puts stderr "and try again.\n**************************"
28 exit 1
29 }
30} else {
31 if {"\u1234"!="u1234"} {
32 puts stderr "***** BUILD PROBLEM *****"
33 puts stderr "$argv0 was linked against an newer version"
34 puts stderr "of TCL that supports Unicode, but uses a header file"
35 puts stderr "(\"tcl.h\") from a old TCL version that does not support"
36 puts stderr "Unicode. This combination causes internal errors."
37 puts stderr "Recompile using a TCL library and header file that match"
38 puts stderr "and try again.\n**************************"
39 exit 1
40 }
41}
drh348784e2000-05-29 20:41:49 +000042
drh92febd92004-08-20 18:34:20 +000043set tcl_precision 15
drh49285702005-09-17 15:20:26 +000044set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000045
drh9eb9e262004-02-11 02:18:05 +000046# Use the pager codec if it is available
47#
drhef4ac8f2004-06-19 00:16:31 +000048if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
49 rename sqlite3 sqlite_orig
50 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000051 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
52 lappend args -key {xyzzy}
53 }
54 uplevel 1 sqlite_orig $args
55 }
56}
57
58
drhbec2bf42000-05-29 23:48:22 +000059# Create a test database
60#
drh254cba22001-09-20 01:44:42 +000061catch {db close}
62file delete -force test.db
63file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000064sqlite3 db ./test.db
65set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000066if {[info exists ::SETUP_SQL]} {
67 db eval $::SETUP_SQL
68}
drhbec2bf42000-05-29 23:48:22 +000069
70# Abort early if this script has been run before.
71#
72if {[info exists nTest]} return
73
74# Set the test counters to zero
75#
drh348784e2000-05-29 20:41:49 +000076set nErr 0
77set nTest 0
drh767c2002000-10-19 14:10:08 +000078set skip_test 0
drha1b351a2001-09-14 16:42:12 +000079set failList {}
drhd3d39e92004-05-20 22:16:29 +000080set maxErr 1000
drhb62c3352006-11-23 09:39:16 +000081if {![info exists speedTest]} {
82 set speedTest 0
83}
drh348784e2000-05-29 20:41:49 +000084
85# Invoke the do_test procedure to run a single test
86#
87proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +000088 global argv nErr nTest skip_test maxErr
danielk19772e588c72005-12-09 14:25:08 +000089 set ::sqlite_malloc_id $name
drh767c2002000-10-19 14:10:08 +000090 if {$skip_test} {
91 set skip_test 0
92 return
93 }
94 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +000095 set go 1
96 } else {
97 set go 0
98 foreach pattern $argv {
99 if {[string match $pattern $name]} {
100 set go 1
101 break
102 }
103 }
104 }
105 if {!$go} return
106 incr nTest
drh5edc3122001-09-13 21:53:09 +0000107 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000108 flush stdout
109 if {[catch {uplevel #0 "$cmd;\n"} result]} {
110 puts "\nError: $result"
111 incr nErr
drha1b351a2001-09-14 16:42:12 +0000112 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000113 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000114 } elseif {[string compare $result $expected]} {
115 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
116 incr nErr
drha1b351a2001-09-14 16:42:12 +0000117 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000118 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000119 } else {
120 puts " Ok"
121 }
drh6558db82007-04-13 03:23:21 +0000122 flush stdout
drh348784e2000-05-29 20:41:49 +0000123}
124
drhb62c3352006-11-23 09:39:16 +0000125# Run an SQL script.
126# Return the number of microseconds per statement.
127#
drh3590f152006-11-23 21:09:10 +0000128proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000129 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000130 flush stdout
131 set speed [time {sqlite3_exec_nr db $sql}]
132 set tm [lindex $speed 0]
drhb62c3352006-11-23 09:39:16 +0000133 set rate [expr {1000000.0*$numstmt/$tm}]
drh3590f152006-11-23 21:09:10 +0000134 set u2 $units/s
drhdd924312007-03-31 22:34:16 +0000135 puts [format {%12d uS %20.5f %s} $tm $rate $u2]
136 global total_time
137 set total_time [expr {$total_time+$tm}]
138}
139proc speed_trial_init {name} {
140 global total_time
141 set total_time 0
142}
143proc speed_trial_summary {name} {
144 global total_time
145 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000146}
147
drhdaffd0e2001-04-11 14:28:42 +0000148# The procedure uses the special "sqlite_malloc_stat" command
drhc89b91b2005-01-03 01:32:59 +0000149# (which is only available if SQLite is compiled with -DSQLITE_DEBUG=1)
drh8c82b352000-12-10 18:23:50 +0000150# to see how many malloc()s have not been free()ed. The number
151# of surplus malloc()s is stored in the global variable $::Leak.
152# If the value in $::Leak grows, it may mean there is a memory leak
153# in the library.
154#
155proc memleak_check {} {
drhdaffd0e2001-04-11 14:28:42 +0000156 if {[info command sqlite_malloc_stat]!=""} {
157 set r [sqlite_malloc_stat]
158 set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}]
159 }
drh8c82b352000-12-10 18:23:50 +0000160}
161
drh348784e2000-05-29 20:41:49 +0000162# Run this routine last
163#
164proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000165 finalize_testing
166}
167proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000168 global nTest nErr sqlite_open_file_count
drha1b351a2001-09-14 16:42:12 +0000169 if {$nErr==0} memleak_check
danielk19772e588c72005-12-09 14:25:08 +0000170
drh6e142f52000-06-08 13:36:40 +0000171 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000172 catch {db2 close}
173 catch {db3 close}
174
danielk197797cb2e92005-12-09 14:39:04 +0000175 catch {
176 pp_check_for_leaks
177 }
drhb4bc7052006-01-11 23:40:33 +0000178 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000179 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000180 db close
181 if {$::sqlite3_tsd_count} {
182 puts "Thread-specific data leak: $::sqlite3_tsd_count instances"
183 incr nErr
184 } else {
185 puts "Thread-specific data deallocated properly"
186 }
187 incr nTest
drh348784e2000-05-29 20:41:49 +0000188 puts "$nErr errors out of $nTest tests"
drha1b351a2001-09-14 16:42:12 +0000189 puts "Failures on these tests: $::failList"
drh80788d82006-09-02 14:50:23 +0000190 if {$nErr>0 && ![working_64bit_int]} {
191 puts "******************************************************************"
192 puts "N.B.: The version of TCL that you used to build this test harness"
193 puts "is defective in that it does not support 64-bit integers. Some or"
194 puts "all of the test failures above might be a result from this defect"
195 puts "in your TCL build."
196 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000197 }
drh94e92032003-02-16 22:21:32 +0000198 if {$sqlite_open_file_count} {
199 puts "$sqlite_open_file_count files were left open"
200 incr nErr
201 }
drhd9910fe2006-10-04 11:55:49 +0000202 foreach f [glob -nocomplain test.db-*-journal] {
203 file delete -force $f
204 }
205 foreach f [glob -nocomplain test.db-mj*] {
206 file delete -force $f
207 }
drhdb25e382001-03-15 18:21:22 +0000208 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000209}
210
drh348784e2000-05-29 20:41:49 +0000211# A procedure to execute SQL
212#
drhc4a3c772001-04-04 11:48:57 +0000213proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000214 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000215 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000216}
drh3aadb2e2000-05-31 17:59:25 +0000217
drhadbca9c2001-09-27 15:11:53 +0000218# Execute SQL and catch exceptions.
219#
220proc catchsql {sql {db db}} {
221 # puts "SQL = $sql"
222 set r [catch {$db eval $sql} msg]
223 lappend r $msg
224 return $r
225}
226
drh04096482001-11-09 22:41:44 +0000227# Do an VDBE code dump on the SQL given
228#
229proc explain {sql {db db}} {
230 puts ""
231 puts "addr opcode p1 p2 p3 "
232 puts "---- ------------ ------ ------ ---------------"
233 $db eval "explain $sql" {} {
234 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
235 }
236}
237
drh3aadb2e2000-05-31 17:59:25 +0000238# Another procedure to execute SQL. This one includes the field
239# names in the returned list.
240#
241proc execsql2 {sql} {
242 set result {}
243 db eval $sql data {
244 foreach f $data(*) {
245 lappend result $f $data($f)
246 }
247 }
248 return $result
249}
drh17a68932001-01-31 13:28:08 +0000250
drhdde85d92003-03-01 19:45:34 +0000251# Use the non-callback API to execute multiple SQL statements
252#
253proc stepsql {dbptr sql} {
254 set sql [string trim $sql]
255 set r 0
256 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000257 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000258 return [list 1 $vm]
259 }
260 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000261# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
262# foreach v $VAL {lappend r $v}
263# }
264 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
265 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
266 lappend r [sqlite3_column_text $vm $i]
267 }
drhdde85d92003-03-01 19:45:34 +0000268 }
danielk1977106bb232004-05-21 10:08:53 +0000269 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000270 return [list 1 $errmsg]
271 }
272 }
273 return $r
274}
275
drh17a68932001-01-31 13:28:08 +0000276# Delete a file or directory
277#
278proc forcedelete {filename} {
279 if {[catch {file delete -force $filename}]} {
280 exec rm -rf $filename
281 }
282}
drh21504322002-06-25 13:16:02 +0000283
284# Do an integrity check of the entire database
285#
286proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000287 ifcapable integrityck {
288 do_test $name {
289 execsql {PRAGMA integrity_check}
290 } {ok}
291 }
292}
293
294# Evaluate a boolean expression of capabilities. If true, execute the
295# code. Omit the code if false.
296#
danielk19773e8c37e2005-01-21 03:12:14 +0000297proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000298 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000299 if ($e2) {
300 set c [catch {uplevel 1 $code} r]
301 } else {
302 set c [catch {uplevel 1 $elsecode} r]
303 }
304 return -code $c $r
drh21504322002-06-25 13:16:02 +0000305}
danielk197745901d62004-11-10 15:27:38 +0000306
danielk1977aca790a2005-01-13 11:07:52 +0000307# This proc execs a seperate process that crashes midway through executing
308# the SQL script $sql on database test.db.
309#
310# The crash occurs during a sync() of file $crashfile. When the crash
311# occurs a random subset of all unsynced writes made by the process are
312# written into the files on disk. Argument $crashdelay indicates the
313# number of file syncs to wait before crashing.
314#
315# The return value is a list of two elements. The first element is a
316# boolean, indicating whether or not the process actually crashed or
317# reported some other error. The second element in the returned list is the
318# error message. This is "child process exited abnormally" if the crash
319# occured.
320#
danielk197759a33f92007-03-17 10:26:59 +0000321# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE $sql
322#
323proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000324 if {$::tcl_platform(platform)!="unix"} {
325 error "crashsql should only be used on unix"
326 }
danielk197759a33f92007-03-17 10:26:59 +0000327
328 set blocksize ""
329 set crashdelay 1
330 set crashfile ""
331 set sql [lindex $args end]
332
333 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
334 set z [lindex $args $ii]
335 set n [string length $z]
336 set z2 [lindex $args [expr $ii+1]]
337
338 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
339 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
340 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize $z2} \
341 else { error "Unrecognized option: $z" }
342 }
343
344 if {$crashfile eq ""} {
345 error "Compulsory option -file missing"
346 }
347
danielk1977aca790a2005-01-13 11:07:52 +0000348 set cfile [file join [pwd] $crashfile]
349
350 set f [open crash.tcl w]
danielk197759a33f92007-03-17 10:26:59 +0000351 puts $f "sqlite3_crashparams $crashdelay $cfile $blocksize"
danielk1977933bbd62007-03-17 07:22:42 +0000352 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk1977aca790a2005-01-13 11:07:52 +0000353 puts $f "sqlite3 db test.db"
danielk1977933bbd62007-03-17 07:22:42 +0000354
355 # This block sets the cache size of the main database to 10
356 # pages. This is done in case the build is configured to omit
357 # "PRAGMA cache_size".
358 puts $f {db eval {SELECT * FROM sqlite_master;}}
359 puts $f {set bt [btree_from_db db]}
360 puts $f {btree_set_cache_size $bt 10}
361
danielk1977aca790a2005-01-13 11:07:52 +0000362 puts $f "db eval {"
363 puts $f "$sql"
364 puts $f "}"
365 close $f
366
367 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000368 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000369 } msg]
370 lappend r $msg
371}
372
danielk197732554c12005-01-22 03:39:39 +0000373# Usage: do_ioerr_test <test number> <options...>
374#
375# This proc is used to implement test cases that check that IO errors
376# are correctly handled. The first argument, <test number>, is an integer
377# used to name the tests executed by this proc. Options are as follows:
378#
379# -tclprep TCL script to run to prepare test.
380# -sqlprep SQL script to run to prepare test.
381# -tclbody TCL script to run with IO error simulation.
382# -sqlbody TCL script to run with IO error simulation.
383# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000384# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000385# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000386# -start Value of 'N' to begin with (default 1)
387#
388# -cksum Boolean. If true, test that the database does
389# not change during the execution of the test case.
390#
391proc do_ioerr_test {testname args} {
392
danielk197732554c12005-01-22 03:39:39 +0000393 set ::ioerropts(-start) 1
394 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000395 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000396 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000397 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000398 array set ::ioerropts $args
399
400 set ::go 1
401 for {set n $::ioerropts(-start)} {$::go} {incr n} {
drhc2ee76c2007-01-04 14:58:14 +0000402 incr ::ioerropts(-count) -1
403 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000404
405 # Skip this IO error if it was specified with the "-exclude" option.
406 if {[info exists ::ioerropts(-exclude)]} {
407 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
408 }
409
410 # Delete the files test.db and test2.db, then execute the TCL and
411 # SQL (in that order) to prepare for the test case.
412 do_test $testname.$n.1 {
413 set ::sqlite_io_error_pending 0
414 catch {db close}
415 catch {file delete -force test.db}
416 catch {file delete -force test.db-journal}
417 catch {file delete -force test2.db}
418 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000419 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000420 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000421 if {[info exists ::ioerropts(-tclprep)]} {
422 eval $::ioerropts(-tclprep)
423 }
424 if {[info exists ::ioerropts(-sqlprep)]} {
425 execsql $::ioerropts(-sqlprep)
426 }
427 expr 0
428 } {0}
429
430 # Read the 'checksum' of the database.
431 if {$::ioerropts(-cksum)} {
432 set checksum [cksum]
433 }
434
435 # Set the Nth IO error to fail.
436 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000437 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000438 set ::sqlite_io_error_pending $n
439 }] $n
440
441 # Create a single TCL script from the TCL and SQL specified
442 # as the body of the test.
443 set ::ioerrorbody {}
444 if {[info exists ::ioerropts(-tclbody)]} {
445 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
446 }
447 if {[info exists ::ioerropts(-sqlbody)]} {
448 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
449 }
450
451 # Execute the TCL Script created in the above block. If
452 # there are at least N IO operations performed by SQLite as
453 # a result of the script, the Nth will fail.
454 do_test $testname.$n.3 {
455 set r [catch $::ioerrorbody msg]
drhe49f9822006-09-15 12:29:16 +0000456 set rc [sqlite3_errcode $::DB]
457 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000458 # If we are in extended result code mode, make sure all of the
459 # IOERRs we get back really do have their extended code values.
460 # If an extended result code is returned, the sqlite3_errcode
461 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
462 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000463 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
464 return $rc
465 }
466 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000467 # If we are not in extended result code mode, make sure no
468 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000469 if {[regexp {\+\d} $rc]} {
470 return $rc
471 }
472 }
drh5f7b5bf2007-04-19 12:30:54 +0000473 # The test repeats as long as $::go is true.
danielk197732554c12005-01-22 03:39:39 +0000474 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000475 set s [expr $::sqlite_io_error_hit==0]
danielk1977f2fa8312006-01-24 13:09:33 +0000476 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000477
478 # One of two things must have happened. either
479 # 1. We never hit the IO error and the SQL returned OK
480 # 2. An IO error was hit and the SQL failed
481 #
danielk1977f2fa8312006-01-24 13:09:33 +0000482 expr { ($s && !$r && !$::go) || (!$s && $r && $::go) }
danielk197732554c12005-01-22 03:39:39 +0000483 } {1}
484
485 # If an IO error occured, then the checksum of the database should
486 # be the same as before the script that caused the IO error was run.
487 if {$::go && $::ioerropts(-cksum)} {
488 do_test $testname.$n.4 {
489 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000490 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000491 cksum
492 } $checksum
493 }
494
danielk1977c4da5b92006-01-21 12:08:54 +0000495 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000496 if {[info exists ::ioerropts(-cleanup)]} {
497 catch $::ioerropts(-cleanup)
498 }
danielk197732554c12005-01-22 03:39:39 +0000499 }
500 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000501 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000502 unset ::ioerropts
503}
504
505# Return a checksum based on the contents of database 'db'.
506#
507proc cksum {{db db}} {
508 set txt [$db eval {
509 SELECT name, type, sql FROM sqlite_master order by name
510 }]\n
511 foreach tbl [$db eval {
512 SELECT name FROM sqlite_master WHERE type='table' order by name
513 }] {
514 append txt [$db eval "SELECT * FROM $tbl"]\n
515 }
516 foreach prag {default_synchronous default_cache_size} {
517 append txt $prag-[$db eval "PRAGMA $prag"]\n
518 }
519 set cksum [string length $txt]-[md5 $txt]
520 # puts $cksum-[file size test.db]
521 return $cksum
522}
523
524# Copy file $from into $to. This is used because some versions of
525# TCL for windows (notably the 8.4.1 binary package shipped with the
526# current mingw release) have a broken "file copy" command.
527#
528proc copy_file {from to} {
529 if {$::tcl_platform(platform)=="unix"} {
530 file copy -force $from $to
531 } else {
532 set f [open $from]
533 fconfigure $f -translation binary
534 set t [open $to w]
535 fconfigure $t -translation binary
536 puts -nonewline $t [read $f [file size $from]]
537 close $t
538 close $f
539 }
540}
541
danielk19772e588c72005-12-09 14:25:08 +0000542# This command checks for outstanding calls to sqliteMalloc() from within
543# the current thread. A list is returned with one entry for each outstanding
544# malloc. Each list entry is itself a list of 5 items, as follows:
545#
546# { <number-bytes> <file-name> <line-number> <test-case> <stack-dump> }
547#
548proc check_for_leaks {} {
549 set ret [list]
danielk1977aef0bf62005-12-30 16:28:01 +0000550 set cnt 0
danielk19772e588c72005-12-09 14:25:08 +0000551 foreach alloc [sqlite_malloc_outstanding] {
552 foreach {nBytes file iLine userstring backtrace} $alloc {}
553 set stack [list]
554 set skip 0
555
556 # The first command in this block will probably fail on windows. This
557 # means there will be no stack dump available.
danielk1977da184232006-01-05 11:34:32 +0000558 if {$cnt < 25 && $backtrace!=""} {
danielk1977aef0bf62005-12-30 16:28:01 +0000559 catch {
560 set stuff [eval "exec addr2line -e ./testfixture -f $backtrace"]
561 foreach {func line} $stuff {
562 if {$func != "??" || $line != "??:0"} {
563 regexp {.*/(.*)} $line dummy line
564 lappend stack "${func}() $line"
565 } else {
566 if {[lindex $stack end] != "..."} {
567 lappend stack "..."
568 }
danielk19772e588c72005-12-09 14:25:08 +0000569 }
570 }
571 }
danielk1977aef0bf62005-12-30 16:28:01 +0000572 incr cnt
danielk19772e588c72005-12-09 14:25:08 +0000573 }
574
575 if {!$skip} {
576 lappend ret [list $nBytes $file $iLine $userstring $stack]
577 }
578 }
579 return $ret
580}
581
582# Pretty print a report based on the return value of [check_for_leaks] to
583# stdout.
584proc pp_check_for_leaks {} {
585 set l [check_for_leaks]
586 set n 0
587 foreach leak $l {
588 foreach {nBytes file iLine userstring stack} $leak {}
589 puts "$nBytes bytes leaked at $file:$iLine ($userstring)"
590 foreach frame $stack {
591 puts " $frame"
592 }
593 incr n $nBytes
594 }
595 puts "Memory leaked: $n bytes in [llength $l] allocations"
596 puts ""
597}
598
danielk197745901d62004-11-10 15:27:38 +0000599# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
600# to non-zero, then set the global variable $AUTOVACUUM to 1.
601set AUTOVACUUM $sqlite_options(default_autovacuum)