4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file contains code used to implement test interfaces to the
14 ** memory allocation subsystem.
16 #include "sqliteInt.h"
17 #if defined(INCLUDE_SQLITE_TCL_H)
18 # include "sqlite_tcl.h"
27 ** This structure is used to encapsulate the global state variables used
28 ** by malloc() fault simulation.
30 static struct MemFault
{
31 int iCountdown
; /* Number of pending successes before a failure */
32 int nRepeat
; /* Number of times to repeat the failure */
33 int nBenign
; /* Number of benign failures seen since last config */
34 int nFail
; /* Number of failures seen since last config */
35 u8 enable
; /* True if enabled */
36 int isInstalled
; /* True if the fault simulation layer is installed */
37 int isBenignMode
; /* True if malloc failures are considered benign */
38 sqlite3_mem_methods m
; /* 'Real' malloc implementation */
42 ** This routine exists as a place to set a breakpoint that will
43 ** fire on any simulated malloc() failure.
45 static void sqlite3Fault(void){
51 ** Check to see if a fault should be simulated. Return true to simulate
52 ** the fault. Return false if the fault should not be simulated.
54 static int faultsimStep(void){
55 if( likely(!memfault
.enable
) ){
58 if( memfault
.iCountdown
>0 ){
59 memfault
.iCountdown
--;
64 if( memfault
.isBenignMode
>0 ){
68 if( memfault
.nRepeat
<=0 ){
75 ** A version of sqlite3_mem_methods.xMalloc() that includes fault simulation
78 static void *faultsimMalloc(int n
){
80 if( !faultsimStep() ){
81 p
= memfault
.m
.xMalloc(n
);
88 ** A version of sqlite3_mem_methods.xRealloc() that includes fault simulation
91 static void *faultsimRealloc(void *pOld
, int n
){
93 if( !faultsimStep() ){
94 p
= memfault
.m
.xRealloc(pOld
, n
);
100 ** The following method calls are passed directly through to the underlying
109 static void faultsimFree(void *p
){
112 static int faultsimSize(void *p
){
113 return memfault
.m
.xSize(p
);
115 static int faultsimRoundup(int n
){
116 return memfault
.m
.xRoundup(n
);
118 static int faultsimInit(void *p
){
119 return memfault
.m
.xInit(memfault
.m
.pAppData
);
121 static void faultsimShutdown(void *p
){
122 memfault
.m
.xShutdown(memfault
.m
.pAppData
);
126 ** This routine configures the malloc failure simulation. After
127 ** calling this routine, the next nDelay mallocs will succeed, followed
128 ** by a block of nRepeat failures, after which malloc() calls will begin
131 static void faultsimConfig(int nDelay
, int nRepeat
){
132 memfault
.iCountdown
= nDelay
;
133 memfault
.nRepeat
= nRepeat
;
134 memfault
.nBenign
= 0;
136 memfault
.enable
= nDelay
>=0;
138 /* Sometimes, when running multi-threaded tests, the isBenignMode
139 ** variable is not properly incremented/decremented so that it is
140 ** 0 when not inside a benign malloc block. This doesn't affect
141 ** the multi-threaded tests, as they do not use this system. But
142 ** it does affect OOM tests run later in the same process. So
143 ** zero the variable here, just to be sure.
145 memfault
.isBenignMode
= 0;
149 ** Return the number of faults (both hard and benign faults) that have
150 ** occurred since the injector was last configured.
152 static int faultsimFailures(void){
153 return memfault
.nFail
;
157 ** Return the number of benign faults that have occurred since the
158 ** injector was last configured.
160 static int faultsimBenignFailures(void){
161 return memfault
.nBenign
;
165 ** Return the number of successes that will occur before the next failure.
166 ** If no failures are scheduled, return -1.
168 static int faultsimPending(void){
169 if( memfault
.enable
){
170 return memfault
.iCountdown
;
177 static void faultsimBeginBenign(void){
178 memfault
.isBenignMode
++;
180 static void faultsimEndBenign(void){
181 memfault
.isBenignMode
--;
185 ** Add or remove the fault-simulation layer using sqlite3_config(). If
186 ** the argument is non-zero, the
188 static int faultsimInstall(int install
){
189 static struct sqlite3_mem_methods m
= {
190 faultsimMalloc
, /* xMalloc */
191 faultsimFree
, /* xFree */
192 faultsimRealloc
, /* xRealloc */
193 faultsimSize
, /* xSize */
194 faultsimRoundup
, /* xRoundup */
195 faultsimInit
, /* xInit */
196 faultsimShutdown
, /* xShutdown */
201 install
= (install
? 1 : 0);
202 assert(memfault
.isInstalled
==1 || memfault
.isInstalled
==0);
204 if( install
==memfault
.isInstalled
){
209 rc
= sqlite3_config(SQLITE_CONFIG_GETMALLOC
, &memfault
.m
);
210 assert(memfault
.m
.xMalloc
);
212 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, &m
);
214 sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS
,
215 faultsimBeginBenign
, faultsimEndBenign
218 sqlite3_mem_methods m2
;
219 assert(memfault
.m
.xMalloc
);
221 /* One should be able to reset the default memory allocator by storing
222 ** a zeroed allocator then calling GETMALLOC. */
223 memset(&m2
, 0, sizeof(m2
));
224 sqlite3_config(SQLITE_CONFIG_MALLOC
, &m2
);
225 sqlite3_config(SQLITE_CONFIG_GETMALLOC
, &m2
);
226 assert( memcmp(&m2
, &memfault
.m
, sizeof(m2
))==0 );
228 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, &memfault
.m
);
229 sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS
,
234 memfault
.isInstalled
= 1;
242 ** This function is implemented in main.c. Returns a pointer to a static
243 ** buffer containing the symbolic SQLite error code that corresponds to
244 ** the least-significant 8-bits of the integer passed as an argument.
247 ** sqlite3ErrName(1) -> "SQLITE_ERROR"
249 extern const char *sqlite3ErrName(int);
252 ** Transform pointers to text and back again
254 static void pointerToText(void *p
, char *z
){
255 static const char zHex
[] = "0123456789abcdef";
263 if( sizeof(n
)==sizeof(p
) ){
264 memcpy(&n
, &p
, sizeof(p
));
265 }else if( sizeof(u
)==sizeof(p
) ){
266 memcpy(&u
, &p
, sizeof(u
));
271 for(i
=0, k
=sizeof(p
)*2-1; i
<sizeof(p
)*2; i
++, k
--){
277 static int hexToInt(int h
){
278 if( h
>='0' && h
<='9' ){
280 }else if( h
>='a' && h
<='f' ){
286 static int textToPointer(const char *z
, void **pp
){
287 sqlite3_uint64 n
= 0;
290 for(i
=0; i
<sizeof(void*)*2 && z
[0]; i
++){
293 if( v
<0 ) return TCL_ERROR
;
296 if( *z
!=0 ) return TCL_ERROR
;
297 if( sizeof(n
)==sizeof(*pp
) ){
298 memcpy(pp
, &n
, sizeof(n
));
299 }else if( sizeof(u
)==sizeof(*pp
) ){
301 memcpy(pp
, &u
, sizeof(u
));
309 ** Usage: sqlite3_malloc NBYTES
311 ** Raw test interface for sqlite3_malloc().
313 static int SQLITE_TCLAPI
test_malloc(
317 Tcl_Obj
*CONST objv
[]
323 Tcl_WrongNumArgs(interp
, 1, objv
, "NBYTES");
326 if( Tcl_GetIntFromObj(interp
, objv
[1], &nByte
) ) return TCL_ERROR
;
327 p
= sqlite3_malloc((unsigned)nByte
);
328 pointerToText(p
, zOut
);
329 Tcl_AppendResult(interp
, zOut
, NULL
);
334 ** Usage: sqlite3_realloc PRIOR NBYTES
336 ** Raw test interface for sqlite3_realloc().
338 static int SQLITE_TCLAPI
test_realloc(
342 Tcl_Obj
*CONST objv
[]
348 Tcl_WrongNumArgs(interp
, 1, objv
, "PRIOR NBYTES");
351 if( Tcl_GetIntFromObj(interp
, objv
[2], &nByte
) ) return TCL_ERROR
;
352 if( textToPointer(Tcl_GetString(objv
[1]), &pPrior
) ){
353 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
356 p
= sqlite3_realloc(pPrior
, (unsigned)nByte
);
357 pointerToText(p
, zOut
);
358 Tcl_AppendResult(interp
, zOut
, NULL
);
363 ** Usage: sqlite3_free PRIOR
365 ** Raw test interface for sqlite3_free().
367 static int SQLITE_TCLAPI
test_free(
371 Tcl_Obj
*CONST objv
[]
375 Tcl_WrongNumArgs(interp
, 1, objv
, "PRIOR");
378 if( textToPointer(Tcl_GetString(objv
[1]), &pPrior
) ){
379 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
382 sqlite3_free(pPrior
);
387 ** These routines are in test_hexio.c
389 int sqlite3TestHexToBin(const char *, int, char *);
390 int sqlite3TestBinToHex(char*,int);
393 ** Usage: memset ADDRESS SIZE HEX
395 ** Set a chunk of memory (obtained from malloc, probably) to a
396 ** specified hex pattern.
398 static int SQLITE_TCLAPI
test_memset(
402 Tcl_Obj
*CONST objv
[]
411 Tcl_WrongNumArgs(interp
, 1, objv
, "ADDRESS SIZE HEX");
414 if( textToPointer(Tcl_GetString(objv
[1]), &p
) ){
415 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
418 if( Tcl_GetIntFromObj(interp
, objv
[2], &size
) ){
422 Tcl_AppendResult(interp
, "size must be positive", (char*)0);
425 zHex
= Tcl_GetStringFromObj(objv
[3], &n
);
426 if( n
>sizeof(zBin
)*2 ) n
= sizeof(zBin
)*2;
427 n
= sqlite3TestHexToBin(zHex
, n
, zBin
);
429 Tcl_AppendResult(interp
, "no data", (char*)0);
433 for(i
=0; i
<size
; i
++){
440 ** Usage: memget ADDRESS SIZE
442 ** Return memory as hexadecimal text.
444 static int SQLITE_TCLAPI
test_memget(
448 Tcl_Obj
*CONST objv
[]
456 Tcl_WrongNumArgs(interp
, 1, objv
, "ADDRESS SIZE");
459 if( textToPointer(Tcl_GetString(objv
[1]), &p
) ){
460 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
463 if( Tcl_GetIntFromObj(interp
, objv
[2], &size
) ){
467 Tcl_AppendResult(interp
, "size must be positive", (char*)0);
472 if( size
>(sizeof(zHex
)-1)/2 ){
473 n
= (sizeof(zHex
)-1)/2;
477 memcpy(zHex
, zBin
, n
);
480 sqlite3TestBinToHex(zHex
, n
);
481 Tcl_AppendResult(interp
, zHex
, (char*)0);
487 ** Usage: sqlite3_memory_used
489 ** Raw test interface for sqlite3_memory_used().
491 static int SQLITE_TCLAPI
test_memory_used(
495 Tcl_Obj
*CONST objv
[]
497 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(sqlite3_memory_used()));
502 ** Usage: sqlite3_memory_highwater ?RESETFLAG?
504 ** Raw test interface for sqlite3_memory_highwater().
506 static int SQLITE_TCLAPI
test_memory_highwater(
510 Tcl_Obj
*CONST objv
[]
513 if( objc
!=1 && objc
!=2 ){
514 Tcl_WrongNumArgs(interp
, 1, objv
, "?RESET?");
518 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &resetFlag
) ) return TCL_ERROR
;
520 Tcl_SetObjResult(interp
,
521 Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag
)));
526 ** Usage: sqlite3_memdebug_backtrace DEPTH
528 ** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined
529 ** then this routine is a no-op.
531 static int SQLITE_TCLAPI
test_memdebug_backtrace(
535 Tcl_Obj
*CONST objv
[]
539 Tcl_WrongNumArgs(interp
, 1, objv
, "DEPT");
542 if( Tcl_GetIntFromObj(interp
, objv
[1], &depth
) ) return TCL_ERROR
;
543 #ifdef SQLITE_MEMDEBUG
545 extern void sqlite3MemdebugBacktrace(int);
546 sqlite3MemdebugBacktrace(depth
);
553 ** Usage: sqlite3_memdebug_dump FILENAME
555 ** Write a summary of unfreed memory to FILENAME.
557 static int SQLITE_TCLAPI
test_memdebug_dump(
561 Tcl_Obj
*CONST objv
[]
564 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME");
567 #if defined(SQLITE_MEMDEBUG) || defined(SQLITE_MEMORY_SIZE) \
568 || defined(SQLITE_POW2_MEMORY_SIZE)
570 extern void sqlite3MemdebugDump(const char*);
571 sqlite3MemdebugDump(Tcl_GetString(objv
[1]));
578 ** Usage: sqlite3_memdebug_malloc_count
580 ** Return the total number of times malloc() has been called.
582 static int SQLITE_TCLAPI
test_memdebug_malloc_count(
586 Tcl_Obj
*CONST objv
[]
590 Tcl_WrongNumArgs(interp
, 1, objv
, "");
593 #if defined(SQLITE_MEMDEBUG)
595 extern int sqlite3MemdebugMallocCount();
596 nMalloc
= sqlite3MemdebugMallocCount();
599 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nMalloc
));
605 ** Usage: sqlite3_memdebug_fail COUNTER ?OPTIONS?
607 ** where options are:
610 ** -benigncnt <varname>
612 ** Arrange for a simulated malloc() failure after COUNTER successes.
613 ** If a repeat count is specified, the fault is repeated that many
616 ** Each call to this routine overrides the prior counter value.
617 ** This routine returns the number of simulated failures that have
618 ** happened since the previous call to this routine.
620 ** To disable simulated failures, use a COUNTER of -1.
622 static int SQLITE_TCLAPI
test_memdebug_fail(
626 Tcl_Obj
*CONST objv
[]
631 Tcl_Obj
*pBenignCnt
= 0;
636 Tcl_WrongNumArgs(interp
, 1, objv
, "COUNTER ?OPTIONS?");
639 if( Tcl_GetIntFromObj(interp
, objv
[1], &iFail
) ) return TCL_ERROR
;
641 for(ii
=2; ii
<objc
; ii
+=2){
643 char *zOption
= Tcl_GetStringFromObj(objv
[ii
], &nOption
);
646 if( nOption
>1 && strncmp(zOption
, "-repeat", nOption
)==0 ){
648 zErr
= "option requires an argument: ";
650 if( Tcl_GetIntFromObj(interp
, objv
[ii
+1], &nRepeat
) ){
654 }else if( nOption
>1 && strncmp(zOption
, "-benigncnt", nOption
)==0 ){
656 zErr
= "option requires an argument: ";
658 pBenignCnt
= objv
[ii
+1];
661 zErr
= "unknown option: ";
665 Tcl_AppendResult(interp
, zErr
, zOption
, 0);
670 nBenign
= faultsimBenignFailures();
671 nFail
= faultsimFailures();
672 faultsimConfig(iFail
, nRepeat
);
675 Tcl_ObjSetVar2(interp
, pBenignCnt
, 0, Tcl_NewIntObj(nBenign
), 0);
677 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nFail
));
682 ** Usage: sqlite3_memdebug_pending
684 ** Return the number of malloc() calls that will succeed before a
685 ** simulated failure occurs. A negative return value indicates that
686 ** no malloc() failure is scheduled.
688 static int SQLITE_TCLAPI
test_memdebug_pending(
692 Tcl_Obj
*CONST objv
[]
696 Tcl_WrongNumArgs(interp
, 1, objv
, "");
699 nPending
= faultsimPending();
700 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nPending
));
705 ** The following global variable keeps track of the number of tests
706 ** that have run. This variable is only useful when running in the
709 static int sqlite3_memdebug_title_count
= 0;
712 ** Usage: sqlite3_memdebug_settitle TITLE
714 ** Set a title string stored with each allocation. The TITLE is
715 ** typically the name of the test that was running when the
716 ** allocation occurred. The TITLE is stored with the allocation
717 ** and can be used to figure out which tests are leaking memory.
719 ** Each title overwrite the previous.
721 static int SQLITE_TCLAPI
test_memdebug_settitle(
725 Tcl_Obj
*CONST objv
[]
727 sqlite3_memdebug_title_count
++;
729 Tcl_WrongNumArgs(interp
, 1, objv
, "TITLE");
732 #ifdef SQLITE_MEMDEBUG
735 extern int sqlite3MemdebugSettitle(const char*);
736 zTitle
= Tcl_GetString(objv
[1]);
737 sqlite3MemdebugSettitle(zTitle
);
743 #define MALLOC_LOG_FRAMES 10
744 #define MALLOC_LOG_KEYINTS ( \
745 10 * ((sizeof(int)>=sizeof(void*)) ? 1 : sizeof(void*)/sizeof(int)) \
747 static Tcl_HashTable aMallocLog
;
748 static int mallocLogEnabled
= 0;
750 typedef struct MallocLog MallocLog
;
756 #ifdef SQLITE_MEMDEBUG
757 static void test_memdebug_callback(int nByte
, int nFrame
, void **aFrame
){
758 if( mallocLogEnabled
){
760 Tcl_HashEntry
*pEntry
;
763 int aKey
[MALLOC_LOG_KEYINTS
];
764 unsigned int nKey
= sizeof(int)*MALLOC_LOG_KEYINTS
;
766 memset(aKey
, 0, nKey
);
767 if( (sizeof(void*)*nFrame
)<nKey
){
768 nKey
= nFrame
*sizeof(void*);
770 memcpy(aKey
, aFrame
, nKey
);
772 pEntry
= Tcl_CreateHashEntry(&aMallocLog
, (const char *)aKey
, &isNew
);
774 pLog
= (MallocLog
*)Tcl_Alloc(sizeof(MallocLog
));
775 memset(pLog
, 0, sizeof(MallocLog
));
776 Tcl_SetHashValue(pEntry
, (ClientData
)pLog
);
778 pLog
= (MallocLog
*)Tcl_GetHashValue(pEntry
);
782 pLog
->nByte
+= nByte
;
785 #endif /* SQLITE_MEMDEBUG */
787 static void test_memdebug_log_clear(void){
788 Tcl_HashSearch search
;
789 Tcl_HashEntry
*pEntry
;
791 pEntry
=Tcl_FirstHashEntry(&aMallocLog
, &search
);
793 pEntry
=Tcl_NextHashEntry(&search
)
795 MallocLog
*pLog
= (MallocLog
*)Tcl_GetHashValue(pEntry
);
796 Tcl_Free((char *)pLog
);
798 Tcl_DeleteHashTable(&aMallocLog
);
799 Tcl_InitHashTable(&aMallocLog
, MALLOC_LOG_KEYINTS
);
802 static int SQLITE_TCLAPI
test_memdebug_log(
806 Tcl_Obj
*CONST objv
[]
808 static int isInit
= 0;
811 static const char *MB_strs
[] = { "start", "stop", "dump", "clear", "sync" };
813 MB_LOG_START
, MB_LOG_STOP
, MB_LOG_DUMP
, MB_LOG_CLEAR
, MB_LOG_SYNC
817 #ifdef SQLITE_MEMDEBUG
818 extern void sqlite3MemdebugBacktraceCallback(
819 void (*xBacktrace
)(int, int, void **));
820 sqlite3MemdebugBacktraceCallback(test_memdebug_callback
);
822 Tcl_InitHashTable(&aMallocLog
, MALLOC_LOG_KEYINTS
);
827 Tcl_WrongNumArgs(interp
, 1, objv
, "SUB-COMMAND ...");
829 if( Tcl_GetIndexFromObj(interp
, objv
[1], MB_strs
, "sub-command", 0, &iSub
) ){
833 switch( (enum MB_enum
)iSub
){
835 mallocLogEnabled
= 1;
838 mallocLogEnabled
= 0;
841 Tcl_HashSearch search
;
842 Tcl_HashEntry
*pEntry
;
843 Tcl_Obj
*pRet
= Tcl_NewObj();
845 assert(sizeof(Tcl_WideInt
)>=sizeof(void*));
848 pEntry
=Tcl_FirstHashEntry(&aMallocLog
, &search
);
850 pEntry
=Tcl_NextHashEntry(&search
)
852 Tcl_Obj
*apElem
[MALLOC_LOG_FRAMES
+2];
853 MallocLog
*pLog
= (MallocLog
*)Tcl_GetHashValue(pEntry
);
854 Tcl_WideInt
*aKey
= (Tcl_WideInt
*)Tcl_GetHashKey(&aMallocLog
, pEntry
);
857 apElem
[0] = Tcl_NewIntObj(pLog
->nCall
);
858 apElem
[1] = Tcl_NewIntObj(pLog
->nByte
);
859 for(ii
=0; ii
<MALLOC_LOG_FRAMES
; ii
++){
860 apElem
[ii
+2] = Tcl_NewWideIntObj(aKey
[ii
]);
863 Tcl_ListObjAppendElement(interp
, pRet
,
864 Tcl_NewListObj(MALLOC_LOG_FRAMES
+2, apElem
)
868 Tcl_SetObjResult(interp
, pRet
);
872 test_memdebug_log_clear();
877 #ifdef SQLITE_MEMDEBUG
878 extern void sqlite3MemdebugSync();
879 test_memdebug_log_clear();
880 mallocLogEnabled
= 1;
881 sqlite3MemdebugSync();
891 ** Usage: sqlite3_config_pagecache SIZE N
893 ** Set the page-cache memory buffer using SQLITE_CONFIG_PAGECACHE.
894 ** The buffer is static and is of limited size. N might be
895 ** adjusted downward as needed to accommodate the requested size.
896 ** The revised value of N is returned.
898 ** A negative SIZE causes the buffer pointer to be NULL.
900 static int SQLITE_TCLAPI
test_config_pagecache(
904 Tcl_Obj
*CONST objv
[]
908 static char *buf
= 0;
910 Tcl_WrongNumArgs(interp
, 1, objv
, "SIZE N");
913 if( Tcl_GetIntFromObj(interp
, objv
[1], &sz
) ) return TCL_ERROR
;
914 if( Tcl_GetIntFromObj(interp
, objv
[2], &N
) ) return TCL_ERROR
;
918 /* Set the return value */
920 Tcl_ListObjAppendElement(0, pRes
, Tcl_NewIntObj(sqlite3GlobalConfig
.szPage
));
921 Tcl_ListObjAppendElement(0, pRes
, Tcl_NewIntObj(sqlite3GlobalConfig
.nPage
));
922 Tcl_SetObjResult(interp
, pRes
);
925 sqlite3_config(SQLITE_CONFIG_PAGECACHE
, (void*)0, 0, 0);
927 buf
= malloc( sz
*N
);
928 sqlite3_config(SQLITE_CONFIG_PAGECACHE
, buf
, sz
, N
);
934 ** Usage: sqlite3_config_alt_pcache INSTALL_FLAG DISCARD_CHANCE PRNG_SEED
936 ** Set up the alternative test page cache. Install if INSTALL_FLAG is
937 ** true and uninstall (reverting to the default page cache) if INSTALL_FLAG
938 ** is false. DISCARD_CHANGE is an integer between 0 and 100 inclusive
939 ** which determines the chance of discarding a page when unpinned. 100
940 ** is certainty. 0 is never. PRNG_SEED is the pseudo-random number generator
943 static int SQLITE_TCLAPI
test_alt_pcache(
947 Tcl_Obj
*CONST objv
[]
950 int discardChance
= 0;
953 extern void installTestPCache(int,unsigned,unsigned,unsigned);
954 if( objc
<2 || objc
>5 ){
955 Tcl_WrongNumArgs(interp
, 1, objv
,
956 "INSTALLFLAG DISCARDCHANCE PRNGSEEED HIGHSTRESS");
959 if( Tcl_GetIntFromObj(interp
, objv
[1], &installFlag
) ) return TCL_ERROR
;
960 if( objc
>=3 && Tcl_GetIntFromObj(interp
, objv
[2], &discardChance
) ){
963 if( objc
>=4 && Tcl_GetIntFromObj(interp
, objv
[3], &prngSeed
) ){
966 if( objc
>=5 && Tcl_GetIntFromObj(interp
, objv
[4], &highStress
) ){
969 if( discardChance
<0 || discardChance
>100 ){
970 Tcl_AppendResult(interp
, "discard-chance should be between 0 and 100",
974 installTestPCache(installFlag
, (unsigned)discardChance
, (unsigned)prngSeed
,
975 (unsigned)highStress
);
980 ** Usage: sqlite3_config_memstatus BOOLEAN
982 ** Enable or disable memory status reporting using SQLITE_CONFIG_MEMSTATUS.
984 static int SQLITE_TCLAPI
test_config_memstatus(
988 Tcl_Obj
*CONST objv
[]
992 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOLEAN");
995 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &enable
) ) return TCL_ERROR
;
996 rc
= sqlite3_config(SQLITE_CONFIG_MEMSTATUS
, enable
);
997 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
1002 ** Usage: sqlite3_config_lookaside SIZE COUNT
1005 static int SQLITE_TCLAPI
test_config_lookaside(
1009 Tcl_Obj
*CONST objv
[]
1014 Tcl_WrongNumArgs(interp
, 1, objv
, "SIZE COUNT");
1017 if( Tcl_GetIntFromObj(interp
, objv
[1], &sz
) ) return TCL_ERROR
;
1018 if( Tcl_GetIntFromObj(interp
, objv
[2], &cnt
) ) return TCL_ERROR
;
1019 pRet
= Tcl_NewObj();
1020 Tcl_ListObjAppendElement(
1021 interp
, pRet
, Tcl_NewIntObj(sqlite3GlobalConfig
.szLookaside
)
1023 Tcl_ListObjAppendElement(
1024 interp
, pRet
, Tcl_NewIntObj(sqlite3GlobalConfig
.nLookaside
)
1026 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, sz
, cnt
);
1027 Tcl_SetObjResult(interp
, pRet
);
1033 ** Usage: sqlite3_db_config_lookaside CONNECTION BUFID SIZE COUNT
1035 ** There are two static buffers with BUFID 1 and 2. Each static buffer
1036 ** is 10KB in size. A BUFID of 0 indicates that the buffer should be NULL
1037 ** which will cause sqlite3_db_config() to allocate space on its own.
1039 static int SQLITE_TCLAPI
test_db_config_lookaside(
1043 Tcl_Obj
*CONST objv
[]
1049 static char azBuf
[2][10000];
1050 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
1052 Tcl_WrongNumArgs(interp
, 1, objv
, "BUFID SIZE COUNT");
1055 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1056 if( Tcl_GetIntFromObj(interp
, objv
[2], &bufid
) ) return TCL_ERROR
;
1057 if( Tcl_GetIntFromObj(interp
, objv
[3], &sz
) ) return TCL_ERROR
;
1058 if( Tcl_GetIntFromObj(interp
, objv
[4], &cnt
) ) return TCL_ERROR
;
1060 rc
= sqlite3_db_config(db
, SQLITE_DBCONFIG_LOOKASIDE
, (void*)0, sz
, cnt
);
1061 }else if( bufid
>=1 && bufid
<=2 && sz
*cnt
<=sizeof(azBuf
[0]) ){
1062 rc
= sqlite3_db_config(db
, SQLITE_DBCONFIG_LOOKASIDE
, azBuf
[bufid
], sz
,cnt
);
1064 Tcl_AppendResult(interp
, "illegal arguments - see documentation", (char*)0);
1067 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
1072 ** Usage: sqlite3_config_heap NBYTE NMINALLOC
1074 static int SQLITE_TCLAPI
test_config_heap(
1078 Tcl_Obj
*CONST objv
[]
1080 static char *zBuf
; /* Use this memory */
1081 int nByte
; /* Size of buffer to pass to sqlite3_config() */
1082 int nMinAlloc
; /* Size of minimum allocation */
1083 int rc
; /* Return code of sqlite3_config() */
1085 Tcl_Obj
* CONST
*aArg
= &objv
[1];
1089 Tcl_WrongNumArgs(interp
, 1, objv
, "NBYTE NMINALLOC");
1092 if( Tcl_GetIntFromObj(interp
, aArg
[0], &nByte
) ) return TCL_ERROR
;
1093 if( Tcl_GetIntFromObj(interp
, aArg
[1], &nMinAlloc
) ) return TCL_ERROR
;
1098 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, (void*)0, 0, 0);
1100 zBuf
= realloc(zBuf
, nByte
);
1101 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, zBuf
, nByte
, nMinAlloc
);
1104 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1109 ** Usage: sqlite3_config_heap_size NBYTE
1111 static int SQLITE_TCLAPI
test_config_heap_size(
1115 Tcl_Obj
*CONST objv
[]
1117 int nByte
; /* Size to pass to sqlite3_config() */
1118 int rc
; /* Return code of sqlite3_config() */
1120 Tcl_Obj
* CONST
*aArg
= &objv
[1];
1124 Tcl_WrongNumArgs(interp
, 1, objv
, "NBYTE");
1127 if( Tcl_GetIntFromObj(interp
, aArg
[0], &nByte
) ) return TCL_ERROR
;
1129 rc
= sqlite3_config(SQLITE_CONFIG_WIN32_HEAPSIZE
, nByte
);
1131 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1136 ** Usage: sqlite3_config_error [DB]
1138 ** Invoke sqlite3_config() or sqlite3_db_config() with invalid
1139 ** opcodes and verify that they return errors.
1141 static int SQLITE_TCLAPI
test_config_error(
1145 Tcl_Obj
*CONST objv
[]
1148 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
1150 if( objc
!=2 && objc
!=1 ){
1151 Tcl_WrongNumArgs(interp
, 1, objv
, "[DB]");
1155 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1156 if( sqlite3_db_config(db
, 99999)!=SQLITE_ERROR
){
1157 Tcl_AppendResult(interp
,
1158 "sqlite3_db_config(db, 99999) does not return SQLITE_ERROR",
1163 if( sqlite3_config(99999)!=SQLITE_ERROR
){
1164 Tcl_AppendResult(interp
,
1165 "sqlite3_config(99999) does not return SQLITE_ERROR",
1174 ** Usage: sqlite3_config_uri BOOLEAN
1176 ** Enables or disables interpretation of URI parameters by default using
1177 ** SQLITE_CONFIG_URI.
1179 static int SQLITE_TCLAPI
test_config_uri(
1183 Tcl_Obj
*CONST objv
[]
1189 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOL");
1192 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &bOpenUri
) ){
1196 rc
= sqlite3_config(SQLITE_CONFIG_URI
, bOpenUri
);
1197 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1203 ** Usage: sqlite3_config_cis BOOLEAN
1205 ** Enables or disables the use of the covering-index scan optimization.
1206 ** SQLITE_CONFIG_COVERING_INDEX_SCAN.
1208 static int SQLITE_TCLAPI
test_config_cis(
1212 Tcl_Obj
*CONST objv
[]
1218 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOL");
1221 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &bUseCis
) ){
1225 rc
= sqlite3_config(SQLITE_CONFIG_COVERING_INDEX_SCAN
, bUseCis
);
1226 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1232 ** Usage: sqlite3_config_pmasz INTEGER
1234 ** Set the minimum PMA size.
1236 static int SQLITE_TCLAPI
test_config_pmasz(
1240 Tcl_Obj
*CONST objv
[]
1246 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOL");
1249 if( Tcl_GetIntFromObj(interp
, objv
[1], &iPmaSz
) ){
1253 rc
= sqlite3_config(SQLITE_CONFIG_PMASZ
, iPmaSz
);
1254 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1261 ** Usage: sqlite3_dump_memsys3 FILENAME
1262 ** sqlite3_dump_memsys5 FILENAME
1264 ** Write a summary of unfreed memsys3 allocations to FILENAME.
1266 static int SQLITE_TCLAPI
test_dump_memsys3(
1270 Tcl_Obj
*CONST objv
[]
1273 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME");
1277 switch( SQLITE_PTR_TO_INT(clientData
) ){
1279 #ifdef SQLITE_ENABLE_MEMSYS3
1280 extern void sqlite3Memsys3Dump(const char*);
1281 sqlite3Memsys3Dump(Tcl_GetString(objv
[1]));
1286 #ifdef SQLITE_ENABLE_MEMSYS5
1287 extern void sqlite3Memsys5Dump(const char*);
1288 sqlite3Memsys5Dump(Tcl_GetString(objv
[1]));
1297 ** Usage: sqlite3_status OPCODE RESETFLAG
1299 ** Return a list of three elements which are the sqlite3_status() return
1300 ** code, the current value, and the high-water mark value.
1302 static int SQLITE_TCLAPI
test_status(
1306 Tcl_Obj
*CONST objv
[]
1308 int rc
, iValue
, mxValue
;
1309 int i
, op
= 0, resetFlag
;
1310 const char *zOpName
;
1311 static const struct {
1315 { "SQLITE_STATUS_MEMORY_USED", SQLITE_STATUS_MEMORY_USED
},
1316 { "SQLITE_STATUS_MALLOC_SIZE", SQLITE_STATUS_MALLOC_SIZE
},
1317 { "SQLITE_STATUS_PAGECACHE_USED", SQLITE_STATUS_PAGECACHE_USED
},
1318 { "SQLITE_STATUS_PAGECACHE_OVERFLOW", SQLITE_STATUS_PAGECACHE_OVERFLOW
},
1319 { "SQLITE_STATUS_PAGECACHE_SIZE", SQLITE_STATUS_PAGECACHE_SIZE
},
1320 { "SQLITE_STATUS_SCRATCH_USED", SQLITE_STATUS_SCRATCH_USED
},
1321 { "SQLITE_STATUS_SCRATCH_OVERFLOW", SQLITE_STATUS_SCRATCH_OVERFLOW
},
1322 { "SQLITE_STATUS_SCRATCH_SIZE", SQLITE_STATUS_SCRATCH_SIZE
},
1323 { "SQLITE_STATUS_PARSER_STACK", SQLITE_STATUS_PARSER_STACK
},
1324 { "SQLITE_STATUS_MALLOC_COUNT", SQLITE_STATUS_MALLOC_COUNT
},
1328 Tcl_WrongNumArgs(interp
, 1, objv
, "PARAMETER RESETFLAG");
1331 zOpName
= Tcl_GetString(objv
[1]);
1332 for(i
=0; i
<ArraySize(aOp
); i
++){
1333 if( strcmp(aOp
[i
].zName
, zOpName
)==0 ){
1338 if( i
>=ArraySize(aOp
) ){
1339 if( Tcl_GetIntFromObj(interp
, objv
[1], &op
) ) return TCL_ERROR
;
1341 if( Tcl_GetBooleanFromObj(interp
, objv
[2], &resetFlag
) ) return TCL_ERROR
;
1344 rc
= sqlite3_status(op
, &iValue
, &mxValue
, resetFlag
);
1345 pResult
= Tcl_NewObj();
1346 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(rc
));
1347 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(iValue
));
1348 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(mxValue
));
1349 Tcl_SetObjResult(interp
, pResult
);
1354 ** Usage: sqlite3_db_status DATABASE OPCODE RESETFLAG
1356 ** Return a list of three elements which are the sqlite3_db_status() return
1357 ** code, the current value, and the high-water mark value.
1359 static int SQLITE_TCLAPI
test_db_status(
1363 Tcl_Obj
*CONST objv
[]
1365 int rc
, iValue
, mxValue
;
1366 int i
, op
= 0, resetFlag
;
1367 const char *zOpName
;
1369 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
1370 static const struct {
1374 { "LOOKASIDE_USED", SQLITE_DBSTATUS_LOOKASIDE_USED
},
1375 { "CACHE_USED", SQLITE_DBSTATUS_CACHE_USED
},
1376 { "SCHEMA_USED", SQLITE_DBSTATUS_SCHEMA_USED
},
1377 { "STMT_USED", SQLITE_DBSTATUS_STMT_USED
},
1378 { "LOOKASIDE_HIT", SQLITE_DBSTATUS_LOOKASIDE_HIT
},
1379 { "LOOKASIDE_MISS_SIZE", SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
},
1380 { "LOOKASIDE_MISS_FULL", SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
},
1381 { "CACHE_HIT", SQLITE_DBSTATUS_CACHE_HIT
},
1382 { "CACHE_MISS", SQLITE_DBSTATUS_CACHE_MISS
},
1383 { "CACHE_WRITE", SQLITE_DBSTATUS_CACHE_WRITE
},
1384 { "DEFERRED_FKS", SQLITE_DBSTATUS_DEFERRED_FKS
},
1385 { "CACHE_USED_SHARED", SQLITE_DBSTATUS_CACHE_USED_SHARED
},
1389 Tcl_WrongNumArgs(interp
, 1, objv
, "DB PARAMETER RESETFLAG");
1392 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1393 zOpName
= Tcl_GetString(objv
[2]);
1394 if( memcmp(zOpName
, "SQLITE_", 7)==0 ) zOpName
+= 7;
1395 if( memcmp(zOpName
, "DBSTATUS_", 9)==0 ) zOpName
+= 9;
1396 for(i
=0; i
<ArraySize(aOp
); i
++){
1397 if( strcmp(aOp
[i
].zName
, zOpName
)==0 ){
1402 if( i
>=ArraySize(aOp
) ){
1403 if( Tcl_GetIntFromObj(interp
, objv
[2], &op
) ) return TCL_ERROR
;
1405 if( Tcl_GetBooleanFromObj(interp
, objv
[3], &resetFlag
) ) return TCL_ERROR
;
1408 rc
= sqlite3_db_status(db
, op
, &iValue
, &mxValue
, resetFlag
);
1409 pResult
= Tcl_NewObj();
1410 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(rc
));
1411 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(iValue
));
1412 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(mxValue
));
1413 Tcl_SetObjResult(interp
, pResult
);
1418 ** install_malloc_faultsim BOOLEAN
1420 static int SQLITE_TCLAPI
test_install_malloc_faultsim(
1424 Tcl_Obj
*CONST objv
[]
1430 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOLEAN");
1433 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[1], &isInstall
) ){
1436 rc
= faultsimInstall(isInstall
);
1437 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1442 ** sqlite3_install_memsys3
1444 static int SQLITE_TCLAPI
test_install_memsys3(
1448 Tcl_Obj
*CONST objv
[]
1450 int rc
= SQLITE_MISUSE
;
1451 #ifdef SQLITE_ENABLE_MEMSYS3
1452 const sqlite3_mem_methods
*sqlite3MemGetMemsys3(void);
1453 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, sqlite3MemGetMemsys3());
1455 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1459 static int SQLITE_TCLAPI
test_vfs_oom_test(
1463 Tcl_Obj
*CONST objv
[]
1465 extern int sqlite3_memdebug_vfs_oom_test
;
1467 Tcl_WrongNumArgs(interp
, 1, objv
, "?INTEGER?");
1469 }else if( objc
==2 ){
1471 if( Tcl_GetIntFromObj(interp
, objv
[1], &iNew
) ) return TCL_ERROR
;
1472 sqlite3_memdebug_vfs_oom_test
= iNew
;
1474 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_memdebug_vfs_oom_test
));
1479 ** Register commands with the TCL interpreter.
1481 int Sqlitetest_malloc_Init(Tcl_Interp
*interp
){
1484 Tcl_ObjCmdProc
*xProc
;
1487 { "sqlite3_malloc", test_malloc
,0 },
1488 { "sqlite3_realloc", test_realloc
,0 },
1489 { "sqlite3_free", test_free
,0 },
1490 { "memset", test_memset
,0 },
1491 { "memget", test_memget
,0 },
1492 { "sqlite3_memory_used", test_memory_used
,0 },
1493 { "sqlite3_memory_highwater", test_memory_highwater
,0 },
1494 { "sqlite3_memdebug_backtrace", test_memdebug_backtrace
,0 },
1495 { "sqlite3_memdebug_dump", test_memdebug_dump
,0 },
1496 { "sqlite3_memdebug_fail", test_memdebug_fail
,0 },
1497 { "sqlite3_memdebug_pending", test_memdebug_pending
,0 },
1498 { "sqlite3_memdebug_settitle", test_memdebug_settitle
,0 },
1499 { "sqlite3_memdebug_malloc_count", test_memdebug_malloc_count
,0 },
1500 { "sqlite3_memdebug_log", test_memdebug_log
,0 },
1501 { "sqlite3_config_pagecache", test_config_pagecache
,0 },
1502 { "sqlite3_config_alt_pcache", test_alt_pcache
,0 },
1503 { "sqlite3_status", test_status
,0 },
1504 { "sqlite3_db_status", test_db_status
,0 },
1505 { "install_malloc_faultsim", test_install_malloc_faultsim
,0 },
1506 { "sqlite3_config_heap", test_config_heap
,0 },
1507 { "sqlite3_config_heap_size", test_config_heap_size
,0 },
1508 { "sqlite3_config_memstatus", test_config_memstatus
,0 },
1509 { "sqlite3_config_lookaside", test_config_lookaside
,0 },
1510 { "sqlite3_config_error", test_config_error
,0 },
1511 { "sqlite3_config_uri", test_config_uri
,0 },
1512 { "sqlite3_config_cis", test_config_cis
,0 },
1513 { "sqlite3_config_pmasz", test_config_pmasz
,0 },
1514 { "sqlite3_db_config_lookaside",test_db_config_lookaside
,0 },
1515 { "sqlite3_dump_memsys3", test_dump_memsys3
,3 },
1516 { "sqlite3_dump_memsys5", test_dump_memsys3
,5 },
1517 { "sqlite3_install_memsys3", test_install_memsys3
,0 },
1518 { "sqlite3_memdebug_vfs_oom_test", test_vfs_oom_test
,0 },
1521 for(i
=0; i
<sizeof(aObjCmd
)/sizeof(aObjCmd
[0]); i
++){
1522 ClientData c
= (ClientData
)SQLITE_INT_TO_PTR(aObjCmd
[i
].clientData
);
1523 Tcl_CreateObjCommand(interp
, aObjCmd
[i
].zName
, aObjCmd
[i
].xProc
, c
, 0);