drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2007 August 15 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains code used to implement test interfaces to the |
| 14 | ** memory allocation subsystem. |
| 15 | ** |
danielk1977 | ab7bee8 | 2008-10-15 11:43:55 +0000 | [diff] [blame^] | 16 | ** $Id: test_malloc.c,v 1.49 2008/10/15 11:43:55 danielk1977 Exp $ |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "tcl.h" |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <assert.h> |
| 23 | |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 24 | /* |
| 25 | ** This structure is used to encapsulate the global state variables used |
| 26 | ** by malloc() fault simulation. |
| 27 | */ |
| 28 | static struct MemFault { |
| 29 | int iCountdown; /* Number of pending successes before a failure */ |
| 30 | int nRepeat; /* Number of times to repeat the failure */ |
| 31 | int nBenign; /* Number of benign failures seen since last config */ |
| 32 | int nFail; /* Number of failures seen since last config */ |
| 33 | u8 enable; /* True if enabled */ |
| 34 | int isInstalled; /* True if the fault simulation layer is installed */ |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 35 | int isBenignMode; /* True if malloc failures are considered benign */ |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 36 | sqlite3_mem_methods m; /* 'Real' malloc implementation */ |
| 37 | } memfault; |
| 38 | |
| 39 | /* |
| 40 | ** This routine exists as a place to set a breakpoint that will |
| 41 | ** fire on any simulated malloc() failure. |
| 42 | */ |
| 43 | static void sqlite3Fault(void){ |
| 44 | static int cnt = 0; |
| 45 | cnt++; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | ** Check to see if a fault should be simulated. Return true to simulate |
| 50 | ** the fault. Return false if the fault should not be simulated. |
| 51 | */ |
| 52 | static int faultsimStep(){ |
| 53 | if( likely(!memfault.enable) ){ |
| 54 | return 0; |
| 55 | } |
| 56 | if( memfault.iCountdown>0 ){ |
| 57 | memfault.iCountdown--; |
| 58 | return 0; |
| 59 | } |
| 60 | sqlite3Fault(); |
| 61 | memfault.nFail++; |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 62 | if( memfault.isBenignMode>0 ){ |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 63 | memfault.nBenign++; |
| 64 | } |
| 65 | memfault.nRepeat--; |
| 66 | if( memfault.nRepeat<=0 ){ |
| 67 | memfault.enable = 0; |
| 68 | } |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | ** A version of sqlite3_mem_methods.xMalloc() that includes fault simulation |
| 74 | ** logic. |
| 75 | */ |
| 76 | static void *faultsimMalloc(int n){ |
| 77 | void *p = 0; |
| 78 | if( !faultsimStep() ){ |
| 79 | p = memfault.m.xMalloc(n); |
| 80 | } |
| 81 | return p; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | ** A version of sqlite3_mem_methods.xRealloc() that includes fault simulation |
| 87 | ** logic. |
| 88 | */ |
| 89 | static void *faultsimRealloc(void *pOld, int n){ |
| 90 | void *p = 0; |
| 91 | if( !faultsimStep() ){ |
| 92 | p = memfault.m.xRealloc(pOld, n); |
| 93 | } |
| 94 | return p; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | ** The following method calls are passed directly through to the underlying |
| 99 | ** malloc system: |
| 100 | ** |
| 101 | ** xFree |
| 102 | ** xSize |
| 103 | ** xRoundup |
| 104 | ** xInit |
| 105 | ** xShutdown |
| 106 | */ |
| 107 | static void faultsimFree(void *p){ |
| 108 | memfault.m.xFree(p); |
| 109 | } |
| 110 | static int faultsimSize(void *p){ |
| 111 | return memfault.m.xSize(p); |
| 112 | } |
| 113 | static int faultsimRoundup(int n){ |
| 114 | return memfault.m.xRoundup(n); |
| 115 | } |
| 116 | static int faultsimInit(void *p){ |
| 117 | return memfault.m.xInit(memfault.m.pAppData); |
| 118 | } |
| 119 | static void faultsimShutdown(void *p){ |
| 120 | memfault.m.xShutdown(memfault.m.pAppData); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | ** This routine configures the malloc failure simulation. After |
| 125 | ** calling this routine, the next nDelay mallocs will succeed, followed |
| 126 | ** by a block of nRepeat failures, after which malloc() calls will begin |
| 127 | ** to succeed again. |
| 128 | */ |
| 129 | static void faultsimConfig(int nDelay, int nRepeat){ |
| 130 | memfault.iCountdown = nDelay; |
| 131 | memfault.nRepeat = nRepeat; |
| 132 | memfault.nBenign = 0; |
| 133 | memfault.nFail = 0; |
| 134 | memfault.enable = nDelay>=0; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | ** Return the number of faults (both hard and benign faults) that have |
| 139 | ** occurred since the injector was last configured. |
| 140 | */ |
| 141 | static int faultsimFailures(void){ |
| 142 | return memfault.nFail; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | ** Return the number of benign faults that have occurred since the |
| 147 | ** injector was last configured. |
| 148 | */ |
| 149 | static int faultsimBenignFailures(void){ |
| 150 | return memfault.nBenign; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | ** Return the number of successes that will occur before the next failure. |
| 155 | ** If no failures are scheduled, return -1. |
| 156 | */ |
| 157 | static int faultsimPending(void){ |
| 158 | if( memfault.enable ){ |
| 159 | return memfault.iCountdown; |
| 160 | }else{ |
| 161 | return -1; |
| 162 | } |
| 163 | } |
| 164 | |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 165 | |
| 166 | static void faultsimBeginBenign(void){ |
| 167 | memfault.isBenignMode++; |
| 168 | } |
| 169 | static void faultsimEndBenign(void){ |
| 170 | memfault.isBenignMode--; |
| 171 | } |
| 172 | |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 173 | /* |
| 174 | ** Add or remove the fault-simulation layer using sqlite3_config(). If |
| 175 | ** the argument is non-zero, the |
| 176 | */ |
| 177 | static int faultsimInstall(int install){ |
| 178 | static struct sqlite3_mem_methods m = { |
| 179 | faultsimMalloc, /* xMalloc */ |
| 180 | faultsimFree, /* xFree */ |
| 181 | faultsimRealloc, /* xRealloc */ |
| 182 | faultsimSize, /* xSize */ |
| 183 | faultsimRoundup, /* xRoundup */ |
| 184 | faultsimInit, /* xInit */ |
| 185 | faultsimShutdown, /* xShutdown */ |
| 186 | 0 /* pAppData */ |
| 187 | }; |
| 188 | int rc; |
| 189 | |
| 190 | install = (install ? 1 : 0); |
| 191 | assert(memfault.isInstalled==1 || memfault.isInstalled==0); |
| 192 | |
| 193 | if( install==memfault.isInstalled ){ |
| 194 | return SQLITE_ERROR; |
| 195 | } |
| 196 | |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 197 | if( install ){ |
| 198 | rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memfault.m); |
| 199 | assert(memfault.m.xMalloc); |
| 200 | if( rc==SQLITE_OK ){ |
| 201 | rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &m); |
| 202 | } |
| 203 | sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, |
| 204 | faultsimBeginBenign, faultsimEndBenign |
| 205 | ); |
| 206 | }else{ |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 207 | sqlite3_mem_methods m; |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 208 | assert(memfault.m.xMalloc); |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 209 | |
| 210 | /* One should be able to reset the default memory allocator by storing |
| 211 | ** a zeroed allocator then calling GETMALLOC. */ |
| 212 | memset(&m, 0, sizeof(m)); |
| 213 | sqlite3_config(SQLITE_CONFIG_MALLOC, &m); |
| 214 | sqlite3_config(SQLITE_CONFIG_GETMALLOC, &m); |
| 215 | assert( memcmp(&m, &memfault.m, sizeof(m))==0 ); |
| 216 | |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 217 | rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memfault.m); |
| 218 | sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, 0, 0); |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if( rc==SQLITE_OK ){ |
| 222 | memfault.isInstalled = 1; |
| 223 | } |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | #ifdef SQLITE_TEST |
| 228 | |
| 229 | /* |
| 230 | ** This function is implemented in test1.c. Returns a pointer to a static |
| 231 | ** buffer containing the symbolic SQLite error code that corresponds to |
| 232 | ** the least-significant 8-bits of the integer passed as an argument. |
| 233 | ** For example: |
| 234 | ** |
| 235 | ** sqlite3TestErrorName(1) -> "SQLITE_ERROR" |
| 236 | */ |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 237 | const char *sqlite3TestErrorName(int); |
| 238 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 239 | /* |
| 240 | ** Transform pointers to text and back again |
| 241 | */ |
| 242 | static void pointerToText(void *p, char *z){ |
| 243 | static const char zHex[] = "0123456789abcdef"; |
| 244 | int i, k; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 245 | unsigned int u; |
| 246 | sqlite3_uint64 n; |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 247 | if( p==0 ){ |
| 248 | strcpy(z, "0"); |
| 249 | return; |
| 250 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 251 | if( sizeof(n)==sizeof(p) ){ |
| 252 | memcpy(&n, &p, sizeof(p)); |
| 253 | }else if( sizeof(u)==sizeof(p) ){ |
| 254 | memcpy(&u, &p, sizeof(u)); |
| 255 | n = u; |
| 256 | }else{ |
| 257 | assert( 0 ); |
| 258 | } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 259 | for(i=0, k=sizeof(p)*2-1; i<sizeof(p)*2; i++, k--){ |
| 260 | z[k] = zHex[n&0xf]; |
| 261 | n >>= 4; |
| 262 | } |
| 263 | z[sizeof(p)*2] = 0; |
| 264 | } |
| 265 | static int hexToInt(int h){ |
| 266 | if( h>='0' && h<='9' ){ |
| 267 | return h - '0'; |
| 268 | }else if( h>='a' && h<='f' ){ |
| 269 | return h - 'a' + 10; |
| 270 | }else{ |
| 271 | return -1; |
| 272 | } |
| 273 | } |
| 274 | static int textToPointer(const char *z, void **pp){ |
| 275 | sqlite3_uint64 n = 0; |
| 276 | int i; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 277 | unsigned int u; |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 278 | for(i=0; i<sizeof(void*)*2 && z[0]; i++){ |
| 279 | int v; |
| 280 | v = hexToInt(*z++); |
| 281 | if( v<0 ) return TCL_ERROR; |
| 282 | n = n*16 + v; |
| 283 | } |
| 284 | if( *z!=0 ) return TCL_ERROR; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 285 | if( sizeof(n)==sizeof(*pp) ){ |
| 286 | memcpy(pp, &n, sizeof(n)); |
| 287 | }else if( sizeof(u)==sizeof(*pp) ){ |
| 288 | u = (unsigned int)n; |
| 289 | memcpy(pp, &u, sizeof(u)); |
| 290 | }else{ |
| 291 | assert( 0 ); |
| 292 | } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 293 | return TCL_OK; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | ** Usage: sqlite3_malloc NBYTES |
| 298 | ** |
| 299 | ** Raw test interface for sqlite3_malloc(). |
| 300 | */ |
| 301 | static int test_malloc( |
| 302 | void * clientData, |
| 303 | Tcl_Interp *interp, |
| 304 | int objc, |
| 305 | Tcl_Obj *CONST objv[] |
| 306 | ){ |
| 307 | int nByte; |
| 308 | void *p; |
| 309 | char zOut[100]; |
| 310 | if( objc!=2 ){ |
| 311 | Tcl_WrongNumArgs(interp, 1, objv, "NBYTES"); |
| 312 | return TCL_ERROR; |
| 313 | } |
| 314 | if( Tcl_GetIntFromObj(interp, objv[1], &nByte) ) return TCL_ERROR; |
| 315 | p = sqlite3_malloc((unsigned)nByte); |
| 316 | pointerToText(p, zOut); |
| 317 | Tcl_AppendResult(interp, zOut, NULL); |
| 318 | return TCL_OK; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | ** Usage: sqlite3_realloc PRIOR NBYTES |
| 323 | ** |
| 324 | ** Raw test interface for sqlite3_realloc(). |
| 325 | */ |
| 326 | static int test_realloc( |
| 327 | void * clientData, |
| 328 | Tcl_Interp *interp, |
| 329 | int objc, |
| 330 | Tcl_Obj *CONST objv[] |
| 331 | ){ |
| 332 | int nByte; |
| 333 | void *pPrior, *p; |
| 334 | char zOut[100]; |
| 335 | if( objc!=3 ){ |
| 336 | Tcl_WrongNumArgs(interp, 1, objv, "PRIOR NBYTES"); |
| 337 | return TCL_ERROR; |
| 338 | } |
| 339 | if( Tcl_GetIntFromObj(interp, objv[2], &nByte) ) return TCL_ERROR; |
| 340 | if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){ |
| 341 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 342 | return TCL_ERROR; |
| 343 | } |
| 344 | p = sqlite3_realloc(pPrior, (unsigned)nByte); |
| 345 | pointerToText(p, zOut); |
| 346 | Tcl_AppendResult(interp, zOut, NULL); |
| 347 | return TCL_OK; |
| 348 | } |
| 349 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 350 | /* |
| 351 | ** Usage: sqlite3_free PRIOR |
| 352 | ** |
| 353 | ** Raw test interface for sqlite3_free(). |
| 354 | */ |
| 355 | static int test_free( |
| 356 | void * clientData, |
| 357 | Tcl_Interp *interp, |
| 358 | int objc, |
| 359 | Tcl_Obj *CONST objv[] |
| 360 | ){ |
| 361 | void *pPrior; |
| 362 | if( objc!=2 ){ |
| 363 | Tcl_WrongNumArgs(interp, 1, objv, "PRIOR"); |
| 364 | return TCL_ERROR; |
| 365 | } |
| 366 | if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){ |
| 367 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 368 | return TCL_ERROR; |
| 369 | } |
| 370 | sqlite3_free(pPrior); |
| 371 | return TCL_OK; |
| 372 | } |
| 373 | |
| 374 | /* |
drh | 9c7a60d | 2007-10-19 17:47:24 +0000 | [diff] [blame] | 375 | ** These routines are in test_hexio.c |
| 376 | */ |
| 377 | int sqlite3TestHexToBin(const char *, int, char *); |
| 378 | int sqlite3TestBinToHex(char*,int); |
| 379 | |
| 380 | /* |
| 381 | ** Usage: memset ADDRESS SIZE HEX |
| 382 | ** |
| 383 | ** Set a chunk of memory (obtained from malloc, probably) to a |
| 384 | ** specified hex pattern. |
| 385 | */ |
| 386 | static int test_memset( |
| 387 | void * clientData, |
| 388 | Tcl_Interp *interp, |
| 389 | int objc, |
| 390 | Tcl_Obj *CONST objv[] |
| 391 | ){ |
| 392 | void *p; |
| 393 | int size, n, i; |
| 394 | char *zHex; |
| 395 | char *zOut; |
| 396 | char zBin[100]; |
| 397 | |
| 398 | if( objc!=4 ){ |
| 399 | Tcl_WrongNumArgs(interp, 1, objv, "ADDRESS SIZE HEX"); |
| 400 | return TCL_ERROR; |
| 401 | } |
| 402 | if( textToPointer(Tcl_GetString(objv[1]), &p) ){ |
| 403 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 404 | return TCL_ERROR; |
| 405 | } |
| 406 | if( Tcl_GetIntFromObj(interp, objv[2], &size) ){ |
| 407 | return TCL_ERROR; |
| 408 | } |
| 409 | if( size<=0 ){ |
| 410 | Tcl_AppendResult(interp, "size must be positive", (char*)0); |
| 411 | return TCL_ERROR; |
| 412 | } |
| 413 | zHex = Tcl_GetStringFromObj(objv[3], &n); |
| 414 | if( n>sizeof(zBin)*2 ) n = sizeof(zBin)*2; |
| 415 | n = sqlite3TestHexToBin(zHex, n, zBin); |
| 416 | if( n==0 ){ |
| 417 | Tcl_AppendResult(interp, "no data", (char*)0); |
| 418 | return TCL_ERROR; |
| 419 | } |
| 420 | zOut = p; |
| 421 | for(i=0; i<size; i++){ |
| 422 | zOut[i] = zBin[i%n]; |
| 423 | } |
| 424 | return TCL_OK; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | ** Usage: memget ADDRESS SIZE |
| 429 | ** |
| 430 | ** Return memory as hexadecimal text. |
| 431 | */ |
| 432 | static int test_memget( |
| 433 | void * clientData, |
| 434 | Tcl_Interp *interp, |
| 435 | int objc, |
| 436 | Tcl_Obj *CONST objv[] |
| 437 | ){ |
| 438 | void *p; |
| 439 | int size, n; |
| 440 | char *zBin; |
| 441 | char zHex[100]; |
| 442 | |
| 443 | if( objc!=3 ){ |
| 444 | Tcl_WrongNumArgs(interp, 1, objv, "ADDRESS SIZE"); |
| 445 | return TCL_ERROR; |
| 446 | } |
| 447 | if( textToPointer(Tcl_GetString(objv[1]), &p) ){ |
| 448 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 449 | return TCL_ERROR; |
| 450 | } |
| 451 | if( Tcl_GetIntFromObj(interp, objv[2], &size) ){ |
| 452 | return TCL_ERROR; |
| 453 | } |
| 454 | if( size<=0 ){ |
| 455 | Tcl_AppendResult(interp, "size must be positive", (char*)0); |
| 456 | return TCL_ERROR; |
| 457 | } |
| 458 | zBin = p; |
| 459 | while( size>0 ){ |
| 460 | if( size>(sizeof(zHex)-1)/2 ){ |
| 461 | n = (sizeof(zHex)-1)/2; |
| 462 | }else{ |
| 463 | n = size; |
| 464 | } |
| 465 | memcpy(zHex, zBin, n); |
| 466 | zBin += n; |
| 467 | size -= n; |
| 468 | sqlite3TestBinToHex(zHex, n); |
| 469 | Tcl_AppendResult(interp, zHex, (char*)0); |
| 470 | } |
| 471 | return TCL_OK; |
| 472 | } |
| 473 | |
| 474 | /* |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 475 | ** Usage: sqlite3_memory_used |
| 476 | ** |
| 477 | ** Raw test interface for sqlite3_memory_used(). |
| 478 | */ |
| 479 | static int test_memory_used( |
| 480 | void * clientData, |
| 481 | Tcl_Interp *interp, |
| 482 | int objc, |
| 483 | Tcl_Obj *CONST objv[] |
| 484 | ){ |
| 485 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_memory_used())); |
| 486 | return TCL_OK; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | ** Usage: sqlite3_memory_highwater ?RESETFLAG? |
| 491 | ** |
| 492 | ** Raw test interface for sqlite3_memory_highwater(). |
| 493 | */ |
| 494 | static int test_memory_highwater( |
| 495 | void * clientData, |
| 496 | Tcl_Interp *interp, |
| 497 | int objc, |
| 498 | Tcl_Obj *CONST objv[] |
| 499 | ){ |
| 500 | int resetFlag = 0; |
| 501 | if( objc!=1 && objc!=2 ){ |
| 502 | Tcl_WrongNumArgs(interp, 1, objv, "?RESET?"); |
| 503 | return TCL_ERROR; |
| 504 | } |
| 505 | if( objc==2 ){ |
| 506 | if( Tcl_GetBooleanFromObj(interp, objv[1], &resetFlag) ) return TCL_ERROR; |
| 507 | } |
| 508 | Tcl_SetObjResult(interp, |
| 509 | Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag))); |
| 510 | return TCL_OK; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | ** Usage: sqlite3_memdebug_backtrace DEPTH |
| 515 | ** |
| 516 | ** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined |
| 517 | ** then this routine is a no-op. |
| 518 | */ |
| 519 | static int test_memdebug_backtrace( |
| 520 | void * clientData, |
| 521 | Tcl_Interp *interp, |
| 522 | int objc, |
| 523 | Tcl_Obj *CONST objv[] |
| 524 | ){ |
| 525 | int depth; |
| 526 | if( objc!=2 ){ |
| 527 | Tcl_WrongNumArgs(interp, 1, objv, "DEPT"); |
| 528 | return TCL_ERROR; |
| 529 | } |
| 530 | if( Tcl_GetIntFromObj(interp, objv[1], &depth) ) return TCL_ERROR; |
| 531 | #ifdef SQLITE_MEMDEBUG |
| 532 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 533 | extern void sqlite3MemdebugBacktrace(int); |
| 534 | sqlite3MemdebugBacktrace(depth); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 535 | } |
| 536 | #endif |
| 537 | return TCL_OK; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | ** Usage: sqlite3_memdebug_dump FILENAME |
| 542 | ** |
| 543 | ** Write a summary of unfreed memory to FILENAME. |
| 544 | */ |
| 545 | static int test_memdebug_dump( |
| 546 | void * clientData, |
| 547 | Tcl_Interp *interp, |
| 548 | int objc, |
| 549 | Tcl_Obj *CONST objv[] |
| 550 | ){ |
| 551 | if( objc!=2 ){ |
| 552 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); |
| 553 | return TCL_ERROR; |
| 554 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 555 | #if defined(SQLITE_MEMDEBUG) || defined(SQLITE_MEMORY_SIZE) \ |
| 556 | || defined(SQLITE_POW2_MEMORY_SIZE) |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 557 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 558 | extern void sqlite3MemdebugDump(const char*); |
| 559 | sqlite3MemdebugDump(Tcl_GetString(objv[1])); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 560 | } |
| 561 | #endif |
| 562 | return TCL_OK; |
| 563 | } |
| 564 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 565 | /* |
| 566 | ** Usage: sqlite3_memdebug_malloc_count |
| 567 | ** |
| 568 | ** Return the total number of times malloc() has been called. |
| 569 | */ |
| 570 | static int test_memdebug_malloc_count( |
| 571 | void * clientData, |
| 572 | Tcl_Interp *interp, |
| 573 | int objc, |
| 574 | Tcl_Obj *CONST objv[] |
| 575 | ){ |
| 576 | int nMalloc = -1; |
| 577 | if( objc!=1 ){ |
| 578 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 579 | return TCL_ERROR; |
| 580 | } |
| 581 | #if defined(SQLITE_MEMDEBUG) |
| 582 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 583 | extern int sqlite3MemdebugMallocCount(); |
| 584 | nMalloc = sqlite3MemdebugMallocCount(); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 585 | } |
| 586 | #endif |
| 587 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nMalloc)); |
| 588 | return TCL_OK; |
| 589 | } |
| 590 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 591 | |
| 592 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 593 | ** Usage: sqlite3_memdebug_fail COUNTER ?OPTIONS? |
| 594 | ** |
| 595 | ** where options are: |
| 596 | ** |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 597 | ** -repeat <count> |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 598 | ** -benigncnt <varname> |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 599 | ** |
| 600 | ** Arrange for a simulated malloc() failure after COUNTER successes. |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 601 | ** If a repeat count is specified, the fault is repeated that many |
| 602 | ** times. |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 603 | ** |
| 604 | ** Each call to this routine overrides the prior counter value. |
| 605 | ** This routine returns the number of simulated failures that have |
| 606 | ** happened since the previous call to this routine. |
| 607 | ** |
| 608 | ** To disable simulated failures, use a COUNTER of -1. |
| 609 | */ |
| 610 | static int test_memdebug_fail( |
| 611 | void * clientData, |
| 612 | Tcl_Interp *interp, |
| 613 | int objc, |
| 614 | Tcl_Obj *CONST objv[] |
| 615 | ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 616 | int ii; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 617 | int iFail; |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 618 | int nRepeat = 1; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 619 | Tcl_Obj *pBenignCnt = 0; |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 620 | int nBenign; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 621 | int nFail = 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 622 | |
| 623 | if( objc<2 ){ |
| 624 | Tcl_WrongNumArgs(interp, 1, objv, "COUNTER ?OPTIONS?"); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 625 | return TCL_ERROR; |
| 626 | } |
| 627 | if( Tcl_GetIntFromObj(interp, objv[1], &iFail) ) return TCL_ERROR; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 628 | |
| 629 | for(ii=2; ii<objc; ii+=2){ |
| 630 | int nOption; |
| 631 | char *zOption = Tcl_GetStringFromObj(objv[ii], &nOption); |
| 632 | char *zErr = 0; |
| 633 | |
| 634 | if( nOption>1 && strncmp(zOption, "-repeat", nOption)==0 ){ |
| 635 | if( ii==(objc-1) ){ |
| 636 | zErr = "option requires an argument: "; |
| 637 | }else{ |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 638 | if( Tcl_GetIntFromObj(interp, objv[ii+1], &nRepeat) ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 639 | return TCL_ERROR; |
| 640 | } |
| 641 | } |
| 642 | }else if( nOption>1 && strncmp(zOption, "-benigncnt", nOption)==0 ){ |
| 643 | if( ii==(objc-1) ){ |
| 644 | zErr = "option requires an argument: "; |
| 645 | }else{ |
| 646 | pBenignCnt = objv[ii+1]; |
| 647 | } |
| 648 | }else{ |
| 649 | zErr = "unknown option: "; |
| 650 | } |
| 651 | |
| 652 | if( zErr ){ |
| 653 | Tcl_AppendResult(interp, zErr, zOption, 0); |
| 654 | return TCL_ERROR; |
| 655 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 656 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 657 | |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 658 | nBenign = faultsimBenignFailures(); |
| 659 | nFail = faultsimFailures(); |
| 660 | faultsimConfig(iFail, nRepeat); |
| 661 | |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 662 | if( pBenignCnt ){ |
| 663 | Tcl_ObjSetVar2(interp, pBenignCnt, 0, Tcl_NewIntObj(nBenign), 0); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 664 | } |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 665 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nFail)); |
| 666 | return TCL_OK; |
| 667 | } |
| 668 | |
danielk1977 | cd03724 | 2007-08-30 15:46:06 +0000 | [diff] [blame] | 669 | /* |
| 670 | ** Usage: sqlite3_memdebug_pending |
| 671 | ** |
| 672 | ** Return the number of malloc() calls that will succeed before a |
| 673 | ** simulated failure occurs. A negative return value indicates that |
| 674 | ** no malloc() failure is scheduled. |
| 675 | */ |
| 676 | static int test_memdebug_pending( |
| 677 | void * clientData, |
| 678 | Tcl_Interp *interp, |
| 679 | int objc, |
| 680 | Tcl_Obj *CONST objv[] |
| 681 | ){ |
drh | 5efaf07 | 2008-03-18 00:07:10 +0000 | [diff] [blame] | 682 | int nPending; |
danielk1977 | cd03724 | 2007-08-30 15:46:06 +0000 | [diff] [blame] | 683 | if( objc!=1 ){ |
| 684 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 685 | return TCL_ERROR; |
| 686 | } |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 687 | nPending = faultsimPending(); |
drh | 5efaf07 | 2008-03-18 00:07:10 +0000 | [diff] [blame] | 688 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending)); |
danielk1977 | cd03724 | 2007-08-30 15:46:06 +0000 | [diff] [blame] | 689 | return TCL_OK; |
| 690 | } |
| 691 | |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 692 | |
| 693 | /* |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 694 | ** Usage: sqlite3_memdebug_settitle TITLE |
| 695 | ** |
| 696 | ** Set a title string stored with each allocation. The TITLE is |
| 697 | ** typically the name of the test that was running when the |
| 698 | ** allocation occurred. The TITLE is stored with the allocation |
| 699 | ** and can be used to figure out which tests are leaking memory. |
| 700 | ** |
| 701 | ** Each title overwrite the previous. |
| 702 | */ |
| 703 | static int test_memdebug_settitle( |
| 704 | void * clientData, |
| 705 | Tcl_Interp *interp, |
| 706 | int objc, |
| 707 | Tcl_Obj *CONST objv[] |
| 708 | ){ |
| 709 | const char *zTitle; |
| 710 | if( objc!=2 ){ |
| 711 | Tcl_WrongNumArgs(interp, 1, objv, "TITLE"); |
| 712 | return TCL_ERROR; |
| 713 | } |
| 714 | zTitle = Tcl_GetString(objv[1]); |
| 715 | #ifdef SQLITE_MEMDEBUG |
| 716 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 717 | extern int sqlite3MemdebugSettitle(const char*); |
| 718 | sqlite3MemdebugSettitle(zTitle); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 719 | } |
| 720 | #endif |
| 721 | return TCL_OK; |
| 722 | } |
| 723 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 724 | #define MALLOC_LOG_FRAMES 10 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 725 | static Tcl_HashTable aMallocLog; |
| 726 | static int mallocLogEnabled = 0; |
| 727 | |
| 728 | typedef struct MallocLog MallocLog; |
| 729 | struct MallocLog { |
| 730 | int nCall; |
| 731 | int nByte; |
| 732 | }; |
| 733 | |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 734 | #ifdef SQLITE_MEMDEBUG |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 735 | static void test_memdebug_callback(int nByte, int nFrame, void **aFrame){ |
| 736 | if( mallocLogEnabled ){ |
| 737 | MallocLog *pLog; |
| 738 | Tcl_HashEntry *pEntry; |
| 739 | int isNew; |
| 740 | |
| 741 | int aKey[MALLOC_LOG_FRAMES]; |
| 742 | int nKey = sizeof(int)*MALLOC_LOG_FRAMES; |
| 743 | |
| 744 | memset(aKey, 0, nKey); |
| 745 | if( (sizeof(void*)*nFrame)<nKey ){ |
| 746 | nKey = nFrame*sizeof(void*); |
| 747 | } |
| 748 | memcpy(aKey, aFrame, nKey); |
| 749 | |
| 750 | pEntry = Tcl_CreateHashEntry(&aMallocLog, (const char *)aKey, &isNew); |
| 751 | if( isNew ){ |
| 752 | pLog = (MallocLog *)Tcl_Alloc(sizeof(MallocLog)); |
| 753 | memset(pLog, 0, sizeof(MallocLog)); |
| 754 | Tcl_SetHashValue(pEntry, (ClientData)pLog); |
| 755 | }else{ |
| 756 | pLog = (MallocLog *)Tcl_GetHashValue(pEntry); |
| 757 | } |
| 758 | |
| 759 | pLog->nCall++; |
| 760 | pLog->nByte += nByte; |
| 761 | } |
| 762 | } |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 763 | #endif /* SQLITE_MEMDEBUG */ |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 764 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 765 | static void test_memdebug_log_clear(){ |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 766 | Tcl_HashSearch search; |
| 767 | Tcl_HashEntry *pEntry; |
| 768 | for( |
| 769 | pEntry=Tcl_FirstHashEntry(&aMallocLog, &search); |
| 770 | pEntry; |
| 771 | pEntry=Tcl_NextHashEntry(&search) |
| 772 | ){ |
| 773 | MallocLog *pLog = (MallocLog *)Tcl_GetHashValue(pEntry); |
| 774 | Tcl_Free((char *)pLog); |
| 775 | } |
| 776 | Tcl_DeleteHashTable(&aMallocLog); |
| 777 | Tcl_InitHashTable(&aMallocLog, MALLOC_LOG_FRAMES); |
| 778 | } |
| 779 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 780 | static int test_memdebug_log( |
| 781 | void * clientData, |
| 782 | Tcl_Interp *interp, |
| 783 | int objc, |
| 784 | Tcl_Obj *CONST objv[] |
| 785 | ){ |
| 786 | static int isInit = 0; |
| 787 | int iSub; |
| 788 | |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 789 | static const char *MB_strs[] = { "start", "stop", "dump", "clear", "sync" }; |
| 790 | enum MB_enum { |
| 791 | MB_LOG_START, MB_LOG_STOP, MB_LOG_DUMP, MB_LOG_CLEAR, MB_LOG_SYNC |
| 792 | }; |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 793 | |
| 794 | if( !isInit ){ |
| 795 | #ifdef SQLITE_MEMDEBUG |
| 796 | extern void sqlite3MemdebugBacktraceCallback( |
| 797 | void (*xBacktrace)(int, int, void **)); |
| 798 | sqlite3MemdebugBacktraceCallback(test_memdebug_callback); |
| 799 | #endif |
| 800 | Tcl_InitHashTable(&aMallocLog, MALLOC_LOG_FRAMES); |
| 801 | isInit = 1; |
| 802 | } |
| 803 | |
| 804 | if( objc<2 ){ |
| 805 | Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ..."); |
| 806 | } |
| 807 | if( Tcl_GetIndexFromObj(interp, objv[1], MB_strs, "sub-command", 0, &iSub) ){ |
| 808 | return TCL_ERROR; |
| 809 | } |
| 810 | |
| 811 | switch( (enum MB_enum)iSub ){ |
| 812 | case MB_LOG_START: |
| 813 | mallocLogEnabled = 1; |
| 814 | break; |
| 815 | case MB_LOG_STOP: |
| 816 | mallocLogEnabled = 0; |
| 817 | break; |
| 818 | case MB_LOG_DUMP: { |
| 819 | Tcl_HashSearch search; |
| 820 | Tcl_HashEntry *pEntry; |
| 821 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 822 | |
| 823 | assert(sizeof(int)==sizeof(void*)); |
| 824 | |
| 825 | for( |
| 826 | pEntry=Tcl_FirstHashEntry(&aMallocLog, &search); |
| 827 | pEntry; |
| 828 | pEntry=Tcl_NextHashEntry(&search) |
| 829 | ){ |
| 830 | Tcl_Obj *apElem[MALLOC_LOG_FRAMES+2]; |
| 831 | MallocLog *pLog = (MallocLog *)Tcl_GetHashValue(pEntry); |
| 832 | int *aKey = (int *)Tcl_GetHashKey(&aMallocLog, pEntry); |
| 833 | int ii; |
| 834 | |
| 835 | apElem[0] = Tcl_NewIntObj(pLog->nCall); |
| 836 | apElem[1] = Tcl_NewIntObj(pLog->nByte); |
| 837 | for(ii=0; ii<MALLOC_LOG_FRAMES; ii++){ |
| 838 | apElem[ii+2] = Tcl_NewIntObj(aKey[ii]); |
| 839 | } |
| 840 | |
| 841 | Tcl_ListObjAppendElement(interp, pRet, |
| 842 | Tcl_NewListObj(MALLOC_LOG_FRAMES+2, apElem) |
| 843 | ); |
| 844 | } |
| 845 | |
| 846 | Tcl_SetObjResult(interp, pRet); |
| 847 | break; |
| 848 | } |
| 849 | case MB_LOG_CLEAR: { |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 850 | test_memdebug_log_clear(); |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | case MB_LOG_SYNC: { |
drh | b940492 | 2008-03-28 12:53:38 +0000 | [diff] [blame] | 855 | #ifdef SQLITE_MEMDEBUG |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 856 | extern void sqlite3MemdebugSync(); |
| 857 | test_memdebug_log_clear(); |
| 858 | mallocLogEnabled = 1; |
| 859 | sqlite3MemdebugSync(); |
drh | b940492 | 2008-03-28 12:53:38 +0000 | [diff] [blame] | 860 | #endif |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 861 | break; |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
| 865 | return TCL_OK; |
| 866 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 867 | |
| 868 | /* |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 869 | ** Usage: sqlite3_config_scratch SIZE N |
| 870 | ** |
| 871 | ** Set the scratch memory buffer using SQLITE_CONFIG_SCRATCH. |
| 872 | ** The buffer is static and is of limited size. N might be |
| 873 | ** adjusted downward as needed to accomodate the requested size. |
| 874 | ** The revised value of N is returned. |
| 875 | ** |
| 876 | ** A negative SIZE causes the buffer pointer to be NULL. |
| 877 | */ |
| 878 | static int test_config_scratch( |
| 879 | void * clientData, |
| 880 | Tcl_Interp *interp, |
| 881 | int objc, |
| 882 | Tcl_Obj *CONST objv[] |
| 883 | ){ |
| 884 | int sz, N, rc; |
| 885 | Tcl_Obj *pResult; |
drh | 5f4bcf1 | 2008-07-29 14:29:06 +0000 | [diff] [blame] | 886 | static char *buf = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 887 | if( objc!=3 ){ |
| 888 | Tcl_WrongNumArgs(interp, 1, objv, "SIZE N"); |
| 889 | return TCL_ERROR; |
| 890 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 891 | if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR; |
| 892 | if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR; |
drh | 5f4bcf1 | 2008-07-29 14:29:06 +0000 | [diff] [blame] | 893 | free(buf); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 894 | if( sz<0 ){ |
drh | 5f4bcf1 | 2008-07-29 14:29:06 +0000 | [diff] [blame] | 895 | buf = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 896 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, 0, 0, 0); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 897 | }else{ |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 898 | buf = malloc( sz*N + 1 ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 899 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, buf, sz, N); |
| 900 | } |
| 901 | pResult = Tcl_NewObj(); |
| 902 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 903 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N)); |
| 904 | Tcl_SetObjResult(interp, pResult); |
| 905 | return TCL_OK; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | ** Usage: sqlite3_config_pagecache SIZE N |
| 910 | ** |
| 911 | ** Set the page-cache memory buffer using SQLITE_CONFIG_PAGECACHE. |
| 912 | ** The buffer is static and is of limited size. N might be |
| 913 | ** adjusted downward as needed to accomodate the requested size. |
| 914 | ** The revised value of N is returned. |
| 915 | ** |
| 916 | ** A negative SIZE causes the buffer pointer to be NULL. |
| 917 | */ |
| 918 | static int test_config_pagecache( |
| 919 | void * clientData, |
| 920 | Tcl_Interp *interp, |
| 921 | int objc, |
| 922 | Tcl_Obj *CONST objv[] |
| 923 | ){ |
| 924 | int sz, N, rc; |
| 925 | Tcl_Obj *pResult; |
drh | 5f4bcf1 | 2008-07-29 14:29:06 +0000 | [diff] [blame] | 926 | static char *buf = 0; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 927 | if( objc!=3 ){ |
| 928 | Tcl_WrongNumArgs(interp, 1, objv, "SIZE N"); |
| 929 | return TCL_ERROR; |
| 930 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 931 | if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR; |
| 932 | if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR; |
drh | 5f4bcf1 | 2008-07-29 14:29:06 +0000 | [diff] [blame] | 933 | free(buf); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 934 | if( sz<0 ){ |
drh | 5f4bcf1 | 2008-07-29 14:29:06 +0000 | [diff] [blame] | 935 | buf = 0; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 936 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, 0, 0, 0); |
| 937 | }else{ |
drh | 0a60a38 | 2008-07-31 17:16:05 +0000 | [diff] [blame] | 938 | buf = malloc( sz*N ); |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 939 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, buf, sz, N); |
| 940 | } |
| 941 | pResult = Tcl_NewObj(); |
| 942 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 943 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N)); |
| 944 | Tcl_SetObjResult(interp, pResult); |
| 945 | return TCL_OK; |
| 946 | } |
| 947 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 948 | /* |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 949 | ** Usage: sqlite3_config_memstatus BOOLEAN |
| 950 | ** |
| 951 | ** Enable or disable memory status reporting using SQLITE_CONFIG_MEMSTATUS. |
| 952 | */ |
| 953 | static int test_config_memstatus( |
| 954 | void * clientData, |
| 955 | Tcl_Interp *interp, |
| 956 | int objc, |
| 957 | Tcl_Obj *CONST objv[] |
| 958 | ){ |
| 959 | int enable, rc; |
| 960 | if( objc!=2 ){ |
| 961 | Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); |
| 962 | return TCL_ERROR; |
| 963 | } |
| 964 | if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ) return TCL_ERROR; |
| 965 | rc = sqlite3_config(SQLITE_CONFIG_MEMSTATUS, enable); |
| 966 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 967 | return TCL_OK; |
| 968 | } |
| 969 | |
| 970 | /* |
danielk1977 | 2d34081 | 2008-07-24 08:20:40 +0000 | [diff] [blame] | 971 | ** Usage: sqlite3_config_chunkalloc |
| 972 | ** |
| 973 | */ |
| 974 | static int test_config_chunkalloc( |
| 975 | void * clientData, |
| 976 | Tcl_Interp *interp, |
| 977 | int objc, |
| 978 | Tcl_Obj *CONST objv[] |
| 979 | ){ |
| 980 | int rc; |
danielk1977 | 31fab4f | 2008-07-25 08:48:59 +0000 | [diff] [blame] | 981 | int nThreshold; |
| 982 | if( objc!=2 ){ |
| 983 | Tcl_WrongNumArgs(interp, 1, objv, "THRESHOLD"); |
danielk1977 | 2d34081 | 2008-07-24 08:20:40 +0000 | [diff] [blame] | 984 | return TCL_ERROR; |
| 985 | } |
danielk1977 | 31fab4f | 2008-07-25 08:48:59 +0000 | [diff] [blame] | 986 | if( Tcl_GetIntFromObj(interp, objv[1], &nThreshold) ) return TCL_ERROR; |
| 987 | rc = sqlite3_config(SQLITE_CONFIG_CHUNKALLOC, nThreshold); |
danielk1977 | 2d34081 | 2008-07-24 08:20:40 +0000 | [diff] [blame] | 988 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 989 | return TCL_OK; |
| 990 | } |
| 991 | |
| 992 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 993 | ** Usage: sqlite3_config_lookaside SIZE COUNT |
| 994 | ** |
| 995 | */ |
| 996 | static int test_config_lookaside( |
| 997 | void * clientData, |
| 998 | Tcl_Interp *interp, |
| 999 | int objc, |
| 1000 | Tcl_Obj *CONST objv[] |
| 1001 | ){ |
| 1002 | int rc; |
| 1003 | int sz, cnt; |
danielk1977 | ab7bee8 | 2008-10-15 11:43:55 +0000 | [diff] [blame^] | 1004 | Tcl_Obj *pRet; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1005 | if( objc!=3 ){ |
| 1006 | Tcl_WrongNumArgs(interp, 1, objv, "SIZE COUNT"); |
| 1007 | return TCL_ERROR; |
| 1008 | } |
| 1009 | if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR; |
| 1010 | if( Tcl_GetIntFromObj(interp, objv[2], &cnt) ) return TCL_ERROR; |
danielk1977 | ab7bee8 | 2008-10-15 11:43:55 +0000 | [diff] [blame^] | 1011 | pRet = Tcl_NewObj(); |
| 1012 | Tcl_ListObjAppendElement( |
| 1013 | interp, pRet, Tcl_NewIntObj(sqlite3GlobalConfig.szLookaside) |
| 1014 | ); |
| 1015 | Tcl_ListObjAppendElement( |
| 1016 | interp, pRet, Tcl_NewIntObj(sqlite3GlobalConfig.nLookaside) |
| 1017 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1018 | rc = sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, cnt); |
danielk1977 | ab7bee8 | 2008-10-15 11:43:55 +0000 | [diff] [blame^] | 1019 | Tcl_SetObjResult(interp, pRet); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1020 | return TCL_OK; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | /* |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 1025 | ** Usage: sqlite3_db_config_lookaside CONNECTION BUFID SIZE COUNT |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1026 | ** |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 1027 | ** There are two static buffers with BUFID 1 and 2. Each static buffer |
| 1028 | ** is 10KB in size. A BUFID of 0 indicates that the buffer should be NULL |
| 1029 | ** which will cause sqlite3_db_config() to allocate space on its own. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1030 | */ |
| 1031 | static int test_db_config_lookaside( |
| 1032 | void * clientData, |
| 1033 | Tcl_Interp *interp, |
| 1034 | int objc, |
| 1035 | Tcl_Obj *CONST objv[] |
| 1036 | ){ |
| 1037 | int rc; |
| 1038 | int sz, cnt; |
| 1039 | sqlite3 *db; |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 1040 | int bufid; |
| 1041 | static char azBuf[2][10000]; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1042 | int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 1043 | if( objc!=5 ){ |
| 1044 | Tcl_WrongNumArgs(interp, 1, objv, "BUFID SIZE COUNT"); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1045 | return TCL_ERROR; |
| 1046 | } |
| 1047 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 1048 | if( Tcl_GetIntFromObj(interp, objv[2], &bufid) ) return TCL_ERROR; |
| 1049 | if( Tcl_GetIntFromObj(interp, objv[3], &sz) ) return TCL_ERROR; |
| 1050 | if( Tcl_GetIntFromObj(interp, objv[4], &cnt) ) return TCL_ERROR; |
| 1051 | if( bufid==0 ){ |
| 1052 | rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, 0, sz, cnt); |
| 1053 | }else if( bufid>=1 && bufid<=2 && sz*cnt<=sizeof(azBuf[0]) ){ |
| 1054 | rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, azBuf[bufid], sz,cnt); |
| 1055 | }else{ |
| 1056 | Tcl_AppendResult(interp, "illegal arguments - see documentation", (char*)0); |
| 1057 | return TCL_ERROR; |
| 1058 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1059 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 1060 | return TCL_OK; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1064 | ** Usage: |
| 1065 | ** |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 1066 | ** sqlite3_config_heap NBYTE NMINALLOC |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1067 | */ |
| 1068 | static int test_config_heap( |
| 1069 | void * clientData, |
| 1070 | Tcl_Interp *interp, |
| 1071 | int objc, |
| 1072 | Tcl_Obj *CONST objv[] |
| 1073 | ){ |
drh | 7830cd4 | 2008-07-16 12:25:32 +0000 | [diff] [blame] | 1074 | static char *zBuf; /* Use this memory */ |
| 1075 | static int szBuf; /* Bytes allocated for zBuf */ |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1076 | int nByte; /* Size of buffer to pass to sqlite3_config() */ |
| 1077 | int nMinAlloc; /* Size of minimum allocation */ |
| 1078 | int rc; /* Return code of sqlite3_config() */ |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1079 | |
| 1080 | Tcl_Obj * CONST *aArg = &objv[1]; |
| 1081 | int nArg = objc-1; |
| 1082 | |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1083 | if( nArg!=2 ){ |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 1084 | Tcl_WrongNumArgs(interp, 1, objv, "NBYTE NMINALLOC"); |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1085 | return TCL_ERROR; |
| 1086 | } |
| 1087 | if( Tcl_GetIntFromObj(interp, aArg[0], &nByte) ) return TCL_ERROR; |
| 1088 | if( Tcl_GetIntFromObj(interp, aArg[1], &nMinAlloc) ) return TCL_ERROR; |
| 1089 | |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1090 | if( nByte==0 ){ |
drh | 7830cd4 | 2008-07-16 12:25:32 +0000 | [diff] [blame] | 1091 | free( zBuf ); |
| 1092 | zBuf = 0; |
| 1093 | szBuf = 0; |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 1094 | rc = sqlite3_config(SQLITE_CONFIG_HEAP, (void*)0, 0, 0); |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1095 | }else{ |
drh | 7830cd4 | 2008-07-16 12:25:32 +0000 | [diff] [blame] | 1096 | zBuf = realloc(zBuf, nByte); |
| 1097 | szBuf = nByte; |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1098 | rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc); |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1099 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 1100 | |
| 1101 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1102 | return TCL_OK; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 1106 | ** tclcmd: sqlite3_config_error [DB] |
| 1107 | ** |
| 1108 | ** Invoke sqlite3_config() or sqlite3_db_config() with invalid |
| 1109 | ** opcodes and verify that they return errors. |
| 1110 | */ |
| 1111 | static int test_config_error( |
| 1112 | void * clientData, |
| 1113 | Tcl_Interp *interp, |
| 1114 | int objc, |
| 1115 | Tcl_Obj *CONST objv[] |
| 1116 | ){ |
| 1117 | sqlite3 *db; |
| 1118 | int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 1119 | |
| 1120 | if( objc!=2 && objc!=1 ){ |
| 1121 | Tcl_WrongNumArgs(interp, 1, objv, "[DB]"); |
| 1122 | return TCL_ERROR; |
| 1123 | } |
| 1124 | if( objc==2 ){ |
| 1125 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1126 | if( sqlite3_db_config(db, 99999)!=SQLITE_ERROR ){ |
| 1127 | Tcl_AppendResult(interp, |
| 1128 | "sqlite3_db_config(db, 99999) does not return SQLITE_ERROR", |
| 1129 | (char*)0); |
| 1130 | return TCL_ERROR; |
| 1131 | } |
| 1132 | }else{ |
| 1133 | if( sqlite3_config(99999)!=SQLITE_ERROR ){ |
| 1134 | Tcl_AppendResult(interp, |
| 1135 | "sqlite3_config(99999) does not return SQLITE_ERROR", |
| 1136 | (char*)0); |
| 1137 | return TCL_ERROR; |
| 1138 | } |
| 1139 | } |
| 1140 | return TCL_OK; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1144 | ** Usage: |
| 1145 | ** |
| 1146 | ** sqlite3_dump_memsys3 FILENAME |
| 1147 | ** sqlite3_dump_memsys5 FILENAME |
danielk1977 | 32155ef | 2008-06-25 10:34:34 +0000 | [diff] [blame] | 1148 | ** |
| 1149 | ** Write a summary of unfreed memsys3 allocations to FILENAME. |
| 1150 | */ |
| 1151 | static int test_dump_memsys3( |
| 1152 | void * clientData, |
| 1153 | Tcl_Interp *interp, |
| 1154 | int objc, |
| 1155 | Tcl_Obj *CONST objv[] |
| 1156 | ){ |
| 1157 | if( objc!=2 ){ |
| 1158 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); |
| 1159 | return TCL_ERROR; |
| 1160 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1161 | |
| 1162 | switch( (int)clientData ){ |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1163 | case 3: { |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1164 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 1165 | extern void sqlite3Memsys3Dump(const char*); |
| 1166 | sqlite3Memsys3Dump(Tcl_GetString(objv[1])); |
| 1167 | break; |
danielk1977 | 32155ef | 2008-06-25 10:34:34 +0000 | [diff] [blame] | 1168 | #endif |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1169 | } |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1170 | case 5: { |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1171 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 1172 | extern void sqlite3Memsys5Dump(const char*); |
| 1173 | sqlite3Memsys5Dump(Tcl_GetString(objv[1])); |
| 1174 | break; |
| 1175 | #endif |
| 1176 | } |
| 1177 | } |
danielk1977 | 32155ef | 2008-06-25 10:34:34 +0000 | [diff] [blame] | 1178 | return TCL_OK; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1182 | ** Usage: sqlite3_status OPCODE RESETFLAG |
| 1183 | ** |
| 1184 | ** Return a list of three elements which are the sqlite3_status() return |
| 1185 | ** code, the current value, and the high-water mark value. |
| 1186 | */ |
| 1187 | static int test_status( |
| 1188 | void * clientData, |
| 1189 | Tcl_Interp *interp, |
| 1190 | int objc, |
| 1191 | Tcl_Obj *CONST objv[] |
| 1192 | ){ |
| 1193 | int rc, iValue, mxValue; |
| 1194 | int i, op, resetFlag; |
| 1195 | const char *zOpName; |
| 1196 | static const struct { |
| 1197 | const char *zName; |
| 1198 | int op; |
| 1199 | } aOp[] = { |
| 1200 | { "SQLITE_STATUS_MEMORY_USED", SQLITE_STATUS_MEMORY_USED }, |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1201 | { "SQLITE_STATUS_MALLOC_SIZE", SQLITE_STATUS_MALLOC_SIZE }, |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1202 | { "SQLITE_STATUS_PAGECACHE_USED", SQLITE_STATUS_PAGECACHE_USED }, |
| 1203 | { "SQLITE_STATUS_PAGECACHE_OVERFLOW", SQLITE_STATUS_PAGECACHE_OVERFLOW }, |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1204 | { "SQLITE_STATUS_PAGECACHE_SIZE", SQLITE_STATUS_PAGECACHE_SIZE }, |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1205 | { "SQLITE_STATUS_SCRATCH_USED", SQLITE_STATUS_SCRATCH_USED }, |
| 1206 | { "SQLITE_STATUS_SCRATCH_OVERFLOW", SQLITE_STATUS_SCRATCH_OVERFLOW }, |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1207 | { "SQLITE_STATUS_SCRATCH_SIZE", SQLITE_STATUS_SCRATCH_SIZE }, |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 1208 | { "SQLITE_STATUS_PARSER_STACK", SQLITE_STATUS_PARSER_STACK }, |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1209 | }; |
| 1210 | Tcl_Obj *pResult; |
| 1211 | if( objc!=3 ){ |
| 1212 | Tcl_WrongNumArgs(interp, 1, objv, "PARAMETER RESETFLAG"); |
| 1213 | return TCL_ERROR; |
| 1214 | } |
| 1215 | zOpName = Tcl_GetString(objv[1]); |
| 1216 | for(i=0; i<ArraySize(aOp); i++){ |
| 1217 | if( strcmp(aOp[i].zName, zOpName)==0 ){ |
| 1218 | op = aOp[i].op; |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | if( i>=ArraySize(aOp) ){ |
| 1223 | if( Tcl_GetIntFromObj(interp, objv[1], &op) ) return TCL_ERROR; |
| 1224 | } |
| 1225 | if( Tcl_GetBooleanFromObj(interp, objv[2], &resetFlag) ) return TCL_ERROR; |
drh | af005fb | 2008-07-09 16:51:51 +0000 | [diff] [blame] | 1226 | iValue = 0; |
| 1227 | mxValue = 0; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1228 | rc = sqlite3_status(op, &iValue, &mxValue, resetFlag); |
| 1229 | pResult = Tcl_NewObj(); |
| 1230 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 1231 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(iValue)); |
| 1232 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(mxValue)); |
| 1233 | Tcl_SetObjResult(interp, pResult); |
| 1234 | return TCL_OK; |
| 1235 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1238 | ** Usage: sqlite3_db_status DATABASE OPCODE RESETFLAG |
| 1239 | ** |
| 1240 | ** Return a list of three elements which are the sqlite3_db_status() return |
| 1241 | ** code, the current value, and the high-water mark value. |
| 1242 | */ |
| 1243 | static int test_db_status( |
| 1244 | void * clientData, |
| 1245 | Tcl_Interp *interp, |
| 1246 | int objc, |
| 1247 | Tcl_Obj *CONST objv[] |
| 1248 | ){ |
| 1249 | int rc, iValue, mxValue; |
| 1250 | int i, op, resetFlag; |
| 1251 | const char *zOpName; |
| 1252 | sqlite3 *db; |
| 1253 | int getDbPointer(Tcl_Interp*, const char*, sqlite3**); |
| 1254 | static const struct { |
| 1255 | const char *zName; |
| 1256 | int op; |
| 1257 | } aOp[] = { |
| 1258 | { "SQLITE_DBSTATUS_LOOKASIDE_USED", SQLITE_DBSTATUS_LOOKASIDE_USED }, |
| 1259 | }; |
| 1260 | Tcl_Obj *pResult; |
| 1261 | if( objc!=4 ){ |
| 1262 | Tcl_WrongNumArgs(interp, 1, objv, "PARAMETER RESETFLAG"); |
| 1263 | return TCL_ERROR; |
| 1264 | } |
| 1265 | if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; |
| 1266 | zOpName = Tcl_GetString(objv[2]); |
| 1267 | for(i=0; i<ArraySize(aOp); i++){ |
| 1268 | if( strcmp(aOp[i].zName, zOpName)==0 ){ |
| 1269 | op = aOp[i].op; |
| 1270 | break; |
| 1271 | } |
| 1272 | } |
| 1273 | if( i>=ArraySize(aOp) ){ |
| 1274 | if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR; |
| 1275 | } |
| 1276 | if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR; |
| 1277 | iValue = 0; |
| 1278 | mxValue = 0; |
| 1279 | rc = sqlite3_db_status(db, op, &iValue, &mxValue, resetFlag); |
| 1280 | pResult = Tcl_NewObj(); |
| 1281 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 1282 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(iValue)); |
| 1283 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(mxValue)); |
| 1284 | Tcl_SetObjResult(interp, pResult); |
| 1285 | return TCL_OK; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 1289 | ** install_malloc_faultsim BOOLEAN |
| 1290 | */ |
| 1291 | static int test_install_malloc_faultsim( |
| 1292 | void * clientData, |
| 1293 | Tcl_Interp *interp, |
| 1294 | int objc, |
| 1295 | Tcl_Obj *CONST objv[] |
| 1296 | ){ |
| 1297 | int rc; |
| 1298 | int isInstall; |
| 1299 | |
| 1300 | if( objc!=2 ){ |
| 1301 | Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); |
| 1302 | return TCL_ERROR; |
| 1303 | } |
| 1304 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[1], &isInstall) ){ |
| 1305 | return TCL_ERROR; |
| 1306 | } |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 1307 | rc = faultsimInstall(isInstall); |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 1308 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1309 | return TCL_OK; |
| 1310 | } |
| 1311 | |
| 1312 | /* |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1313 | ** Register commands with the TCL interpreter. |
| 1314 | */ |
| 1315 | int Sqlitetest_malloc_Init(Tcl_Interp *interp){ |
| 1316 | static struct { |
| 1317 | char *zName; |
| 1318 | Tcl_ObjCmdProc *xProc; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1319 | int clientData; |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1320 | } aObjCmd[] = { |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 1321 | { "sqlite3_malloc", test_malloc ,0 }, |
| 1322 | { "sqlite3_realloc", test_realloc ,0 }, |
| 1323 | { "sqlite3_free", test_free ,0 }, |
| 1324 | { "memset", test_memset ,0 }, |
| 1325 | { "memget", test_memget ,0 }, |
| 1326 | { "sqlite3_memory_used", test_memory_used ,0 }, |
| 1327 | { "sqlite3_memory_highwater", test_memory_highwater ,0 }, |
| 1328 | { "sqlite3_memdebug_backtrace", test_memdebug_backtrace ,0 }, |
| 1329 | { "sqlite3_memdebug_dump", test_memdebug_dump ,0 }, |
| 1330 | { "sqlite3_memdebug_fail", test_memdebug_fail ,0 }, |
| 1331 | { "sqlite3_memdebug_pending", test_memdebug_pending ,0 }, |
| 1332 | { "sqlite3_memdebug_settitle", test_memdebug_settitle ,0 }, |
| 1333 | { "sqlite3_memdebug_malloc_count", test_memdebug_malloc_count ,0 }, |
| 1334 | { "sqlite3_memdebug_log", test_memdebug_log ,0 }, |
| 1335 | { "sqlite3_config_scratch", test_config_scratch ,0 }, |
| 1336 | { "sqlite3_config_pagecache", test_config_pagecache ,0 }, |
| 1337 | { "sqlite3_status", test_status ,0 }, |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1338 | { "sqlite3_db_status", test_db_status ,0 }, |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 1339 | { "install_malloc_faultsim", test_install_malloc_faultsim ,0 }, |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1340 | { "sqlite3_config_heap", test_config_heap ,0 }, |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 1341 | { "sqlite3_config_memstatus", test_config_memstatus ,0 }, |
danielk1977 | 2d34081 | 2008-07-24 08:20:40 +0000 | [diff] [blame] | 1342 | { "sqlite3_config_chunkalloc", test_config_chunkalloc ,0 }, |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1343 | { "sqlite3_config_lookaside", test_config_lookaside ,0 }, |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 1344 | { "sqlite3_config_error", test_config_error ,0 }, |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1345 | { "sqlite3_db_config_lookaside",test_db_config_lookaside ,0 }, |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame] | 1346 | { "sqlite3_dump_memsys3", test_dump_memsys3 ,3 }, |
| 1347 | { "sqlite3_dump_memsys5", test_dump_memsys3 ,5 } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1348 | }; |
| 1349 | int i; |
| 1350 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1351 | ClientData c = (ClientData)aObjCmd[i].clientData; |
| 1352 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, c, 0); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1353 | } |
| 1354 | return TCL_OK; |
| 1355 | } |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 1356 | #endif |