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 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame^] | 16 | ** $Id: test_malloc.c,v 1.33 2008/06/27 14:05:25 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{ |
| 207 | assert(memfault.m.xMalloc); |
| 208 | rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memfault.m); |
| 209 | sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, 0, 0); |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if( rc==SQLITE_OK ){ |
| 213 | memfault.isInstalled = 1; |
| 214 | } |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | #ifdef SQLITE_TEST |
| 219 | |
| 220 | /* |
| 221 | ** This function is implemented in test1.c. Returns a pointer to a static |
| 222 | ** buffer containing the symbolic SQLite error code that corresponds to |
| 223 | ** the least-significant 8-bits of the integer passed as an argument. |
| 224 | ** For example: |
| 225 | ** |
| 226 | ** sqlite3TestErrorName(1) -> "SQLITE_ERROR" |
| 227 | */ |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 228 | const char *sqlite3TestErrorName(int); |
| 229 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 230 | /* |
| 231 | ** Transform pointers to text and back again |
| 232 | */ |
| 233 | static void pointerToText(void *p, char *z){ |
| 234 | static const char zHex[] = "0123456789abcdef"; |
| 235 | int i, k; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 236 | unsigned int u; |
| 237 | sqlite3_uint64 n; |
| 238 | if( sizeof(n)==sizeof(p) ){ |
| 239 | memcpy(&n, &p, sizeof(p)); |
| 240 | }else if( sizeof(u)==sizeof(p) ){ |
| 241 | memcpy(&u, &p, sizeof(u)); |
| 242 | n = u; |
| 243 | }else{ |
| 244 | assert( 0 ); |
| 245 | } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 246 | for(i=0, k=sizeof(p)*2-1; i<sizeof(p)*2; i++, k--){ |
| 247 | z[k] = zHex[n&0xf]; |
| 248 | n >>= 4; |
| 249 | } |
| 250 | z[sizeof(p)*2] = 0; |
| 251 | } |
| 252 | static int hexToInt(int h){ |
| 253 | if( h>='0' && h<='9' ){ |
| 254 | return h - '0'; |
| 255 | }else if( h>='a' && h<='f' ){ |
| 256 | return h - 'a' + 10; |
| 257 | }else{ |
| 258 | return -1; |
| 259 | } |
| 260 | } |
| 261 | static int textToPointer(const char *z, void **pp){ |
| 262 | sqlite3_uint64 n = 0; |
| 263 | int i; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 264 | unsigned int u; |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 265 | for(i=0; i<sizeof(void*)*2 && z[0]; i++){ |
| 266 | int v; |
| 267 | v = hexToInt(*z++); |
| 268 | if( v<0 ) return TCL_ERROR; |
| 269 | n = n*16 + v; |
| 270 | } |
| 271 | if( *z!=0 ) return TCL_ERROR; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 272 | if( sizeof(n)==sizeof(*pp) ){ |
| 273 | memcpy(pp, &n, sizeof(n)); |
| 274 | }else if( sizeof(u)==sizeof(*pp) ){ |
| 275 | u = (unsigned int)n; |
| 276 | memcpy(pp, &u, sizeof(u)); |
| 277 | }else{ |
| 278 | assert( 0 ); |
| 279 | } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 280 | return TCL_OK; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | ** Usage: sqlite3_malloc NBYTES |
| 285 | ** |
| 286 | ** Raw test interface for sqlite3_malloc(). |
| 287 | */ |
| 288 | static int test_malloc( |
| 289 | void * clientData, |
| 290 | Tcl_Interp *interp, |
| 291 | int objc, |
| 292 | Tcl_Obj *CONST objv[] |
| 293 | ){ |
| 294 | int nByte; |
| 295 | void *p; |
| 296 | char zOut[100]; |
| 297 | if( objc!=2 ){ |
| 298 | Tcl_WrongNumArgs(interp, 1, objv, "NBYTES"); |
| 299 | return TCL_ERROR; |
| 300 | } |
| 301 | if( Tcl_GetIntFromObj(interp, objv[1], &nByte) ) return TCL_ERROR; |
| 302 | p = sqlite3_malloc((unsigned)nByte); |
| 303 | pointerToText(p, zOut); |
| 304 | Tcl_AppendResult(interp, zOut, NULL); |
| 305 | return TCL_OK; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | ** Usage: sqlite3_realloc PRIOR NBYTES |
| 310 | ** |
| 311 | ** Raw test interface for sqlite3_realloc(). |
| 312 | */ |
| 313 | static int test_realloc( |
| 314 | void * clientData, |
| 315 | Tcl_Interp *interp, |
| 316 | int objc, |
| 317 | Tcl_Obj *CONST objv[] |
| 318 | ){ |
| 319 | int nByte; |
| 320 | void *pPrior, *p; |
| 321 | char zOut[100]; |
| 322 | if( objc!=3 ){ |
| 323 | Tcl_WrongNumArgs(interp, 1, objv, "PRIOR NBYTES"); |
| 324 | return TCL_ERROR; |
| 325 | } |
| 326 | if( Tcl_GetIntFromObj(interp, objv[2], &nByte) ) return TCL_ERROR; |
| 327 | if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){ |
| 328 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 329 | return TCL_ERROR; |
| 330 | } |
| 331 | p = sqlite3_realloc(pPrior, (unsigned)nByte); |
| 332 | pointerToText(p, zOut); |
| 333 | Tcl_AppendResult(interp, zOut, NULL); |
| 334 | return TCL_OK; |
| 335 | } |
| 336 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 337 | /* |
| 338 | ** Usage: sqlite3_free PRIOR |
| 339 | ** |
| 340 | ** Raw test interface for sqlite3_free(). |
| 341 | */ |
| 342 | static int test_free( |
| 343 | void * clientData, |
| 344 | Tcl_Interp *interp, |
| 345 | int objc, |
| 346 | Tcl_Obj *CONST objv[] |
| 347 | ){ |
| 348 | void *pPrior; |
| 349 | if( objc!=2 ){ |
| 350 | Tcl_WrongNumArgs(interp, 1, objv, "PRIOR"); |
| 351 | return TCL_ERROR; |
| 352 | } |
| 353 | if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){ |
| 354 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 355 | return TCL_ERROR; |
| 356 | } |
| 357 | sqlite3_free(pPrior); |
| 358 | return TCL_OK; |
| 359 | } |
| 360 | |
| 361 | /* |
drh | 9c7a60d | 2007-10-19 17:47:24 +0000 | [diff] [blame] | 362 | ** These routines are in test_hexio.c |
| 363 | */ |
| 364 | int sqlite3TestHexToBin(const char *, int, char *); |
| 365 | int sqlite3TestBinToHex(char*,int); |
| 366 | |
| 367 | /* |
| 368 | ** Usage: memset ADDRESS SIZE HEX |
| 369 | ** |
| 370 | ** Set a chunk of memory (obtained from malloc, probably) to a |
| 371 | ** specified hex pattern. |
| 372 | */ |
| 373 | static int test_memset( |
| 374 | void * clientData, |
| 375 | Tcl_Interp *interp, |
| 376 | int objc, |
| 377 | Tcl_Obj *CONST objv[] |
| 378 | ){ |
| 379 | void *p; |
| 380 | int size, n, i; |
| 381 | char *zHex; |
| 382 | char *zOut; |
| 383 | char zBin[100]; |
| 384 | |
| 385 | if( objc!=4 ){ |
| 386 | Tcl_WrongNumArgs(interp, 1, objv, "ADDRESS SIZE HEX"); |
| 387 | return TCL_ERROR; |
| 388 | } |
| 389 | if( textToPointer(Tcl_GetString(objv[1]), &p) ){ |
| 390 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 391 | return TCL_ERROR; |
| 392 | } |
| 393 | if( Tcl_GetIntFromObj(interp, objv[2], &size) ){ |
| 394 | return TCL_ERROR; |
| 395 | } |
| 396 | if( size<=0 ){ |
| 397 | Tcl_AppendResult(interp, "size must be positive", (char*)0); |
| 398 | return TCL_ERROR; |
| 399 | } |
| 400 | zHex = Tcl_GetStringFromObj(objv[3], &n); |
| 401 | if( n>sizeof(zBin)*2 ) n = sizeof(zBin)*2; |
| 402 | n = sqlite3TestHexToBin(zHex, n, zBin); |
| 403 | if( n==0 ){ |
| 404 | Tcl_AppendResult(interp, "no data", (char*)0); |
| 405 | return TCL_ERROR; |
| 406 | } |
| 407 | zOut = p; |
| 408 | for(i=0; i<size; i++){ |
| 409 | zOut[i] = zBin[i%n]; |
| 410 | } |
| 411 | return TCL_OK; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | ** Usage: memget ADDRESS SIZE |
| 416 | ** |
| 417 | ** Return memory as hexadecimal text. |
| 418 | */ |
| 419 | static int test_memget( |
| 420 | void * clientData, |
| 421 | Tcl_Interp *interp, |
| 422 | int objc, |
| 423 | Tcl_Obj *CONST objv[] |
| 424 | ){ |
| 425 | void *p; |
| 426 | int size, n; |
| 427 | char *zBin; |
| 428 | char zHex[100]; |
| 429 | |
| 430 | if( objc!=3 ){ |
| 431 | Tcl_WrongNumArgs(interp, 1, objv, "ADDRESS SIZE"); |
| 432 | return TCL_ERROR; |
| 433 | } |
| 434 | if( textToPointer(Tcl_GetString(objv[1]), &p) ){ |
| 435 | Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0); |
| 436 | return TCL_ERROR; |
| 437 | } |
| 438 | if( Tcl_GetIntFromObj(interp, objv[2], &size) ){ |
| 439 | return TCL_ERROR; |
| 440 | } |
| 441 | if( size<=0 ){ |
| 442 | Tcl_AppendResult(interp, "size must be positive", (char*)0); |
| 443 | return TCL_ERROR; |
| 444 | } |
| 445 | zBin = p; |
| 446 | while( size>0 ){ |
| 447 | if( size>(sizeof(zHex)-1)/2 ){ |
| 448 | n = (sizeof(zHex)-1)/2; |
| 449 | }else{ |
| 450 | n = size; |
| 451 | } |
| 452 | memcpy(zHex, zBin, n); |
| 453 | zBin += n; |
| 454 | size -= n; |
| 455 | sqlite3TestBinToHex(zHex, n); |
| 456 | Tcl_AppendResult(interp, zHex, (char*)0); |
| 457 | } |
| 458 | return TCL_OK; |
| 459 | } |
| 460 | |
| 461 | /* |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 462 | ** Usage: sqlite3_memory_used |
| 463 | ** |
| 464 | ** Raw test interface for sqlite3_memory_used(). |
| 465 | */ |
| 466 | static int test_memory_used( |
| 467 | void * clientData, |
| 468 | Tcl_Interp *interp, |
| 469 | int objc, |
| 470 | Tcl_Obj *CONST objv[] |
| 471 | ){ |
| 472 | Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_memory_used())); |
| 473 | return TCL_OK; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | ** Usage: sqlite3_memory_highwater ?RESETFLAG? |
| 478 | ** |
| 479 | ** Raw test interface for sqlite3_memory_highwater(). |
| 480 | */ |
| 481 | static int test_memory_highwater( |
| 482 | void * clientData, |
| 483 | Tcl_Interp *interp, |
| 484 | int objc, |
| 485 | Tcl_Obj *CONST objv[] |
| 486 | ){ |
| 487 | int resetFlag = 0; |
| 488 | if( objc!=1 && objc!=2 ){ |
| 489 | Tcl_WrongNumArgs(interp, 1, objv, "?RESET?"); |
| 490 | return TCL_ERROR; |
| 491 | } |
| 492 | if( objc==2 ){ |
| 493 | if( Tcl_GetBooleanFromObj(interp, objv[1], &resetFlag) ) return TCL_ERROR; |
| 494 | } |
| 495 | Tcl_SetObjResult(interp, |
| 496 | Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag))); |
| 497 | return TCL_OK; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | ** Usage: sqlite3_memdebug_backtrace DEPTH |
| 502 | ** |
| 503 | ** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined |
| 504 | ** then this routine is a no-op. |
| 505 | */ |
| 506 | static int test_memdebug_backtrace( |
| 507 | void * clientData, |
| 508 | Tcl_Interp *interp, |
| 509 | int objc, |
| 510 | Tcl_Obj *CONST objv[] |
| 511 | ){ |
| 512 | int depth; |
| 513 | if( objc!=2 ){ |
| 514 | Tcl_WrongNumArgs(interp, 1, objv, "DEPT"); |
| 515 | return TCL_ERROR; |
| 516 | } |
| 517 | if( Tcl_GetIntFromObj(interp, objv[1], &depth) ) return TCL_ERROR; |
| 518 | #ifdef SQLITE_MEMDEBUG |
| 519 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 520 | extern void sqlite3MemdebugBacktrace(int); |
| 521 | sqlite3MemdebugBacktrace(depth); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 522 | } |
| 523 | #endif |
| 524 | return TCL_OK; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | ** Usage: sqlite3_memdebug_dump FILENAME |
| 529 | ** |
| 530 | ** Write a summary of unfreed memory to FILENAME. |
| 531 | */ |
| 532 | static int test_memdebug_dump( |
| 533 | void * clientData, |
| 534 | Tcl_Interp *interp, |
| 535 | int objc, |
| 536 | Tcl_Obj *CONST objv[] |
| 537 | ){ |
| 538 | if( objc!=2 ){ |
| 539 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); |
| 540 | return TCL_ERROR; |
| 541 | } |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 542 | #if defined(SQLITE_MEMDEBUG) || defined(SQLITE_MEMORY_SIZE) \ |
| 543 | || defined(SQLITE_POW2_MEMORY_SIZE) |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 544 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 545 | extern void sqlite3MemdebugDump(const char*); |
| 546 | sqlite3MemdebugDump(Tcl_GetString(objv[1])); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 547 | } |
| 548 | #endif |
| 549 | return TCL_OK; |
| 550 | } |
| 551 | |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 552 | /* |
| 553 | ** Usage: sqlite3_memdebug_malloc_count |
| 554 | ** |
| 555 | ** Return the total number of times malloc() has been called. |
| 556 | */ |
| 557 | static int test_memdebug_malloc_count( |
| 558 | void * clientData, |
| 559 | Tcl_Interp *interp, |
| 560 | int objc, |
| 561 | Tcl_Obj *CONST objv[] |
| 562 | ){ |
| 563 | int nMalloc = -1; |
| 564 | if( objc!=1 ){ |
| 565 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 566 | return TCL_ERROR; |
| 567 | } |
| 568 | #if defined(SQLITE_MEMDEBUG) |
| 569 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 570 | extern int sqlite3MemdebugMallocCount(); |
| 571 | nMalloc = sqlite3MemdebugMallocCount(); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 572 | } |
| 573 | #endif |
| 574 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nMalloc)); |
| 575 | return TCL_OK; |
| 576 | } |
| 577 | |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 578 | |
| 579 | /* |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 580 | ** Usage: sqlite3_memdebug_fail COUNTER ?OPTIONS? |
| 581 | ** |
| 582 | ** where options are: |
| 583 | ** |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 584 | ** -repeat <count> |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 585 | ** -benigncnt <varname> |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 586 | ** |
| 587 | ** Arrange for a simulated malloc() failure after COUNTER successes. |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 588 | ** If a repeat count is specified, the fault is repeated that many |
| 589 | ** times. |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 590 | ** |
| 591 | ** Each call to this routine overrides the prior counter value. |
| 592 | ** This routine returns the number of simulated failures that have |
| 593 | ** happened since the previous call to this routine. |
| 594 | ** |
| 595 | ** To disable simulated failures, use a COUNTER of -1. |
| 596 | */ |
| 597 | static int test_memdebug_fail( |
| 598 | void * clientData, |
| 599 | Tcl_Interp *interp, |
| 600 | int objc, |
| 601 | Tcl_Obj *CONST objv[] |
| 602 | ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 603 | int ii; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 604 | int iFail; |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 605 | int nRepeat = 1; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 606 | Tcl_Obj *pBenignCnt = 0; |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 607 | int nBenign; |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 608 | int nFail = 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 609 | |
| 610 | if( objc<2 ){ |
| 611 | Tcl_WrongNumArgs(interp, 1, objv, "COUNTER ?OPTIONS?"); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 612 | return TCL_ERROR; |
| 613 | } |
| 614 | if( Tcl_GetIntFromObj(interp, objv[1], &iFail) ) return TCL_ERROR; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 615 | |
| 616 | for(ii=2; ii<objc; ii+=2){ |
| 617 | int nOption; |
| 618 | char *zOption = Tcl_GetStringFromObj(objv[ii], &nOption); |
| 619 | char *zErr = 0; |
| 620 | |
| 621 | if( nOption>1 && strncmp(zOption, "-repeat", nOption)==0 ){ |
| 622 | if( ii==(objc-1) ){ |
| 623 | zErr = "option requires an argument: "; |
| 624 | }else{ |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 625 | if( Tcl_GetIntFromObj(interp, objv[ii+1], &nRepeat) ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 626 | return TCL_ERROR; |
| 627 | } |
| 628 | } |
| 629 | }else if( nOption>1 && strncmp(zOption, "-benigncnt", nOption)==0 ){ |
| 630 | if( ii==(objc-1) ){ |
| 631 | zErr = "option requires an argument: "; |
| 632 | }else{ |
| 633 | pBenignCnt = objv[ii+1]; |
| 634 | } |
| 635 | }else{ |
| 636 | zErr = "unknown option: "; |
| 637 | } |
| 638 | |
| 639 | if( zErr ){ |
| 640 | Tcl_AppendResult(interp, zErr, zOption, 0); |
| 641 | return TCL_ERROR; |
| 642 | } |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 643 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 644 | |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 645 | nBenign = faultsimBenignFailures(); |
| 646 | nFail = faultsimFailures(); |
| 647 | faultsimConfig(iFail, nRepeat); |
| 648 | |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 649 | if( pBenignCnt ){ |
| 650 | Tcl_ObjSetVar2(interp, pBenignCnt, 0, Tcl_NewIntObj(nBenign), 0); |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 651 | } |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 652 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nFail)); |
| 653 | return TCL_OK; |
| 654 | } |
| 655 | |
danielk1977 | cd03724 | 2007-08-30 15:46:06 +0000 | [diff] [blame] | 656 | /* |
| 657 | ** Usage: sqlite3_memdebug_pending |
| 658 | ** |
| 659 | ** Return the number of malloc() calls that will succeed before a |
| 660 | ** simulated failure occurs. A negative return value indicates that |
| 661 | ** no malloc() failure is scheduled. |
| 662 | */ |
| 663 | static int test_memdebug_pending( |
| 664 | void * clientData, |
| 665 | Tcl_Interp *interp, |
| 666 | int objc, |
| 667 | Tcl_Obj *CONST objv[] |
| 668 | ){ |
drh | 5efaf07 | 2008-03-18 00:07:10 +0000 | [diff] [blame] | 669 | int nPending; |
danielk1977 | cd03724 | 2007-08-30 15:46:06 +0000 | [diff] [blame] | 670 | if( objc!=1 ){ |
| 671 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 672 | return TCL_ERROR; |
| 673 | } |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 674 | nPending = faultsimPending(); |
drh | 5efaf07 | 2008-03-18 00:07:10 +0000 | [diff] [blame] | 675 | Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending)); |
danielk1977 | cd03724 | 2007-08-30 15:46:06 +0000 | [diff] [blame] | 676 | return TCL_OK; |
| 677 | } |
| 678 | |
drh | 0e6f154 | 2007-08-15 20:41:28 +0000 | [diff] [blame] | 679 | |
| 680 | /* |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 681 | ** Usage: sqlite3_memdebug_settitle TITLE |
| 682 | ** |
| 683 | ** Set a title string stored with each allocation. The TITLE is |
| 684 | ** typically the name of the test that was running when the |
| 685 | ** allocation occurred. The TITLE is stored with the allocation |
| 686 | ** and can be used to figure out which tests are leaking memory. |
| 687 | ** |
| 688 | ** Each title overwrite the previous. |
| 689 | */ |
| 690 | static int test_memdebug_settitle( |
| 691 | void * clientData, |
| 692 | Tcl_Interp *interp, |
| 693 | int objc, |
| 694 | Tcl_Obj *CONST objv[] |
| 695 | ){ |
| 696 | const char *zTitle; |
| 697 | if( objc!=2 ){ |
| 698 | Tcl_WrongNumArgs(interp, 1, objv, "TITLE"); |
| 699 | return TCL_ERROR; |
| 700 | } |
| 701 | zTitle = Tcl_GetString(objv[1]); |
| 702 | #ifdef SQLITE_MEMDEBUG |
| 703 | { |
drh | 49e4fd7 | 2008-02-19 15:15:15 +0000 | [diff] [blame] | 704 | extern int sqlite3MemdebugSettitle(const char*); |
| 705 | sqlite3MemdebugSettitle(zTitle); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 706 | } |
| 707 | #endif |
| 708 | return TCL_OK; |
| 709 | } |
| 710 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 711 | #define MALLOC_LOG_FRAMES 10 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 712 | static Tcl_HashTable aMallocLog; |
| 713 | static int mallocLogEnabled = 0; |
| 714 | |
| 715 | typedef struct MallocLog MallocLog; |
| 716 | struct MallocLog { |
| 717 | int nCall; |
| 718 | int nByte; |
| 719 | }; |
| 720 | |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 721 | #ifdef SQLITE_MEMDEBUG |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 722 | static void test_memdebug_callback(int nByte, int nFrame, void **aFrame){ |
| 723 | if( mallocLogEnabled ){ |
| 724 | MallocLog *pLog; |
| 725 | Tcl_HashEntry *pEntry; |
| 726 | int isNew; |
| 727 | |
| 728 | int aKey[MALLOC_LOG_FRAMES]; |
| 729 | int nKey = sizeof(int)*MALLOC_LOG_FRAMES; |
| 730 | |
| 731 | memset(aKey, 0, nKey); |
| 732 | if( (sizeof(void*)*nFrame)<nKey ){ |
| 733 | nKey = nFrame*sizeof(void*); |
| 734 | } |
| 735 | memcpy(aKey, aFrame, nKey); |
| 736 | |
| 737 | pEntry = Tcl_CreateHashEntry(&aMallocLog, (const char *)aKey, &isNew); |
| 738 | if( isNew ){ |
| 739 | pLog = (MallocLog *)Tcl_Alloc(sizeof(MallocLog)); |
| 740 | memset(pLog, 0, sizeof(MallocLog)); |
| 741 | Tcl_SetHashValue(pEntry, (ClientData)pLog); |
| 742 | }else{ |
| 743 | pLog = (MallocLog *)Tcl_GetHashValue(pEntry); |
| 744 | } |
| 745 | |
| 746 | pLog->nCall++; |
| 747 | pLog->nByte += nByte; |
| 748 | } |
| 749 | } |
shane | afdd23a | 2008-05-29 02:57:47 +0000 | [diff] [blame] | 750 | #endif /* SQLITE_MEMDEBUG */ |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 751 | |
danielk1977 | 5f09613 | 2008-03-28 15:44:09 +0000 | [diff] [blame] | 752 | static void test_memdebug_log_clear(){ |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 753 | Tcl_HashSearch search; |
| 754 | Tcl_HashEntry *pEntry; |
| 755 | for( |
| 756 | pEntry=Tcl_FirstHashEntry(&aMallocLog, &search); |
| 757 | pEntry; |
| 758 | pEntry=Tcl_NextHashEntry(&search) |
| 759 | ){ |
| 760 | MallocLog *pLog = (MallocLog *)Tcl_GetHashValue(pEntry); |
| 761 | Tcl_Free((char *)pLog); |
| 762 | } |
| 763 | Tcl_DeleteHashTable(&aMallocLog); |
| 764 | Tcl_InitHashTable(&aMallocLog, MALLOC_LOG_FRAMES); |
| 765 | } |
| 766 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 767 | static int test_memdebug_log( |
| 768 | void * clientData, |
| 769 | Tcl_Interp *interp, |
| 770 | int objc, |
| 771 | Tcl_Obj *CONST objv[] |
| 772 | ){ |
| 773 | static int isInit = 0; |
| 774 | int iSub; |
| 775 | |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 776 | static const char *MB_strs[] = { "start", "stop", "dump", "clear", "sync" }; |
| 777 | enum MB_enum { |
| 778 | MB_LOG_START, MB_LOG_STOP, MB_LOG_DUMP, MB_LOG_CLEAR, MB_LOG_SYNC |
| 779 | }; |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 780 | |
| 781 | if( !isInit ){ |
| 782 | #ifdef SQLITE_MEMDEBUG |
| 783 | extern void sqlite3MemdebugBacktraceCallback( |
| 784 | void (*xBacktrace)(int, int, void **)); |
| 785 | sqlite3MemdebugBacktraceCallback(test_memdebug_callback); |
| 786 | #endif |
| 787 | Tcl_InitHashTable(&aMallocLog, MALLOC_LOG_FRAMES); |
| 788 | isInit = 1; |
| 789 | } |
| 790 | |
| 791 | if( objc<2 ){ |
| 792 | Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ..."); |
| 793 | } |
| 794 | if( Tcl_GetIndexFromObj(interp, objv[1], MB_strs, "sub-command", 0, &iSub) ){ |
| 795 | return TCL_ERROR; |
| 796 | } |
| 797 | |
| 798 | switch( (enum MB_enum)iSub ){ |
| 799 | case MB_LOG_START: |
| 800 | mallocLogEnabled = 1; |
| 801 | break; |
| 802 | case MB_LOG_STOP: |
| 803 | mallocLogEnabled = 0; |
| 804 | break; |
| 805 | case MB_LOG_DUMP: { |
| 806 | Tcl_HashSearch search; |
| 807 | Tcl_HashEntry *pEntry; |
| 808 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 809 | |
| 810 | assert(sizeof(int)==sizeof(void*)); |
| 811 | |
| 812 | for( |
| 813 | pEntry=Tcl_FirstHashEntry(&aMallocLog, &search); |
| 814 | pEntry; |
| 815 | pEntry=Tcl_NextHashEntry(&search) |
| 816 | ){ |
| 817 | Tcl_Obj *apElem[MALLOC_LOG_FRAMES+2]; |
| 818 | MallocLog *pLog = (MallocLog *)Tcl_GetHashValue(pEntry); |
| 819 | int *aKey = (int *)Tcl_GetHashKey(&aMallocLog, pEntry); |
| 820 | int ii; |
| 821 | |
| 822 | apElem[0] = Tcl_NewIntObj(pLog->nCall); |
| 823 | apElem[1] = Tcl_NewIntObj(pLog->nByte); |
| 824 | for(ii=0; ii<MALLOC_LOG_FRAMES; ii++){ |
| 825 | apElem[ii+2] = Tcl_NewIntObj(aKey[ii]); |
| 826 | } |
| 827 | |
| 828 | Tcl_ListObjAppendElement(interp, pRet, |
| 829 | Tcl_NewListObj(MALLOC_LOG_FRAMES+2, apElem) |
| 830 | ); |
| 831 | } |
| 832 | |
| 833 | Tcl_SetObjResult(interp, pRet); |
| 834 | break; |
| 835 | } |
| 836 | case MB_LOG_CLEAR: { |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 837 | test_memdebug_log_clear(); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | case MB_LOG_SYNC: { |
drh | b940492 | 2008-03-28 12:53:38 +0000 | [diff] [blame] | 842 | #ifdef SQLITE_MEMDEBUG |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 843 | extern void sqlite3MemdebugSync(); |
| 844 | test_memdebug_log_clear(); |
| 845 | mallocLogEnabled = 1; |
| 846 | sqlite3MemdebugSync(); |
drh | b940492 | 2008-03-28 12:53:38 +0000 | [diff] [blame] | 847 | #endif |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 848 | break; |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | return TCL_OK; |
| 853 | } |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 854 | |
| 855 | /* |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 856 | ** Usage: sqlite3_config_scratch SIZE N |
| 857 | ** |
| 858 | ** Set the scratch memory buffer using SQLITE_CONFIG_SCRATCH. |
| 859 | ** The buffer is static and is of limited size. N might be |
| 860 | ** adjusted downward as needed to accomodate the requested size. |
| 861 | ** The revised value of N is returned. |
| 862 | ** |
| 863 | ** A negative SIZE causes the buffer pointer to be NULL. |
| 864 | */ |
| 865 | static int test_config_scratch( |
| 866 | void * clientData, |
| 867 | Tcl_Interp *interp, |
| 868 | int objc, |
| 869 | Tcl_Obj *CONST objv[] |
| 870 | ){ |
| 871 | int sz, N, rc; |
| 872 | Tcl_Obj *pResult; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 873 | static char buf[30000]; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 874 | if( objc!=3 ){ |
| 875 | Tcl_WrongNumArgs(interp, 1, objv, "SIZE N"); |
| 876 | return TCL_ERROR; |
| 877 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 878 | if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR; |
| 879 | if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 880 | if( sz<0 ){ |
| 881 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, 0, 0, 0); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 882 | }else{ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 883 | int mx = sizeof(buf)/(sz+4); |
| 884 | if( N>mx ) N = mx; |
| 885 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, buf, sz, N); |
| 886 | } |
| 887 | pResult = Tcl_NewObj(); |
| 888 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 889 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N)); |
| 890 | Tcl_SetObjResult(interp, pResult); |
| 891 | return TCL_OK; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | ** Usage: sqlite3_config_pagecache SIZE N |
| 896 | ** |
| 897 | ** Set the page-cache memory buffer using SQLITE_CONFIG_PAGECACHE. |
| 898 | ** The buffer is static and is of limited size. N might be |
| 899 | ** adjusted downward as needed to accomodate the requested size. |
| 900 | ** The revised value of N is returned. |
| 901 | ** |
| 902 | ** A negative SIZE causes the buffer pointer to be NULL. |
| 903 | */ |
| 904 | static int test_config_pagecache( |
| 905 | void * clientData, |
| 906 | Tcl_Interp *interp, |
| 907 | int objc, |
| 908 | Tcl_Obj *CONST objv[] |
| 909 | ){ |
| 910 | int sz, N, rc; |
| 911 | Tcl_Obj *pResult; |
| 912 | static char buf[100000]; |
| 913 | if( objc!=3 ){ |
| 914 | Tcl_WrongNumArgs(interp, 1, objv, "SIZE N"); |
| 915 | return TCL_ERROR; |
| 916 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 917 | if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR; |
| 918 | if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR; |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 919 | if( sz<0 ){ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 920 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, 0, 0, 0); |
| 921 | }else{ |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 922 | int mx = sizeof(buf)/(sz+4); |
| 923 | if( N>mx ) N = mx; |
| 924 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, buf, sz, N); |
| 925 | } |
| 926 | pResult = Tcl_NewObj(); |
| 927 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 928 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N)); |
| 929 | Tcl_SetObjResult(interp, pResult); |
| 930 | return TCL_OK; |
| 931 | } |
| 932 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 933 | /* |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 934 | ** Usage: |
| 935 | ** |
| 936 | ** sqlite3_config_heap ?-memsys3? NBYTE NMINALLOC |
| 937 | */ |
| 938 | static int test_config_heap( |
| 939 | void * clientData, |
| 940 | Tcl_Interp *interp, |
| 941 | int objc, |
| 942 | Tcl_Obj *CONST objv[] |
| 943 | ){ |
| 944 | static char zBuf[1048576]; |
| 945 | int nByte; /* Size of buffer to pass to sqlite3_config() */ |
| 946 | int nMinAlloc; /* Size of minimum allocation */ |
| 947 | int rc; /* Return code of sqlite3_config() */ |
| 948 | int isMemsys3 = 0; /* True if the -memsys3 switch is present */ |
| 949 | |
| 950 | Tcl_Obj * CONST *aArg = &objv[1]; |
| 951 | int nArg = objc-1; |
| 952 | |
| 953 | if( nArg>0 && 0==strcmp("-memsys3", Tcl_GetString(aArg[0])) ){ |
| 954 | nArg--; |
| 955 | aArg++; |
| 956 | isMemsys3 = 1; |
| 957 | } |
| 958 | if( nArg!=2 ){ |
| 959 | Tcl_WrongNumArgs(interp, 1, objv, "?-memsys3? NBYTE NMINALLOC"); |
| 960 | return TCL_ERROR; |
| 961 | } |
| 962 | if( Tcl_GetIntFromObj(interp, aArg[0], &nByte) ) return TCL_ERROR; |
| 963 | if( Tcl_GetIntFromObj(interp, aArg[1], &nMinAlloc) ) return TCL_ERROR; |
| 964 | |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame^] | 965 | if( nByte==0 ){ |
| 966 | sqlite3_mem_methods m; |
| 967 | memset(&m, 0, sizeof(sqlite3_mem_methods)); |
| 968 | rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &m); |
| 969 | }else{ |
| 970 | if( nByte>sizeof(zBuf) ){ |
| 971 | nByte = sizeof(zBuf); |
| 972 | } |
| 973 | rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc); |
| 974 | if( isMemsys3 && rc==SQLITE_OK ){ |
| 975 | rc = sqlite3_config(SQLITE_CONFIG_MEMSYS3); |
| 976 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 977 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 978 | |
| 979 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 980 | return TCL_OK; |
| 981 | } |
| 982 | |
| 983 | /* |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 984 | ** Usage: |
| 985 | ** |
| 986 | ** sqlite3_dump_memsys3 FILENAME |
| 987 | ** sqlite3_dump_memsys5 FILENAME |
danielk1977 | 32155ef | 2008-06-25 10:34:34 +0000 | [diff] [blame] | 988 | ** |
| 989 | ** Write a summary of unfreed memsys3 allocations to FILENAME. |
| 990 | */ |
| 991 | static int test_dump_memsys3( |
| 992 | void * clientData, |
| 993 | Tcl_Interp *interp, |
| 994 | int objc, |
| 995 | Tcl_Obj *CONST objv[] |
| 996 | ){ |
| 997 | if( objc!=2 ){ |
| 998 | Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); |
| 999 | return TCL_ERROR; |
| 1000 | } |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1001 | |
| 1002 | switch( (int)clientData ){ |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame^] | 1003 | case 3: { |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1004 | #ifdef SQLITE_ENABLE_MEMSYS3 |
| 1005 | extern void sqlite3Memsys3Dump(const char*); |
| 1006 | sqlite3Memsys3Dump(Tcl_GetString(objv[1])); |
| 1007 | break; |
danielk1977 | 32155ef | 2008-06-25 10:34:34 +0000 | [diff] [blame] | 1008 | #endif |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1009 | } |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame^] | 1010 | case 5: { |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1011 | #ifdef SQLITE_ENABLE_MEMSYS5 |
| 1012 | extern void sqlite3Memsys5Dump(const char*); |
| 1013 | sqlite3Memsys5Dump(Tcl_GetString(objv[1])); |
| 1014 | break; |
| 1015 | #endif |
| 1016 | } |
| 1017 | } |
danielk1977 | 32155ef | 2008-06-25 10:34:34 +0000 | [diff] [blame] | 1018 | return TCL_OK; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1022 | ** Usage: sqlite3_status OPCODE RESETFLAG |
| 1023 | ** |
| 1024 | ** Return a list of three elements which are the sqlite3_status() return |
| 1025 | ** code, the current value, and the high-water mark value. |
| 1026 | */ |
| 1027 | static int test_status( |
| 1028 | void * clientData, |
| 1029 | Tcl_Interp *interp, |
| 1030 | int objc, |
| 1031 | Tcl_Obj *CONST objv[] |
| 1032 | ){ |
| 1033 | int rc, iValue, mxValue; |
| 1034 | int i, op, resetFlag; |
| 1035 | const char *zOpName; |
| 1036 | static const struct { |
| 1037 | const char *zName; |
| 1038 | int op; |
| 1039 | } aOp[] = { |
| 1040 | { "SQLITE_STATUS_MEMORY_USED", SQLITE_STATUS_MEMORY_USED }, |
| 1041 | { "SQLITE_STATUS_PAGECACHE_USED", SQLITE_STATUS_PAGECACHE_USED }, |
| 1042 | { "SQLITE_STATUS_PAGECACHE_OVERFLOW", SQLITE_STATUS_PAGECACHE_OVERFLOW }, |
| 1043 | { "SQLITE_STATUS_SCRATCH_USED", SQLITE_STATUS_SCRATCH_USED }, |
| 1044 | { "SQLITE_STATUS_SCRATCH_OVERFLOW", SQLITE_STATUS_SCRATCH_OVERFLOW }, |
| 1045 | { "SQLITE_STATUS_MALLOC_SIZE", SQLITE_STATUS_MALLOC_SIZE }, |
| 1046 | }; |
| 1047 | Tcl_Obj *pResult; |
| 1048 | if( objc!=3 ){ |
| 1049 | Tcl_WrongNumArgs(interp, 1, objv, "PARAMETER RESETFLAG"); |
| 1050 | return TCL_ERROR; |
| 1051 | } |
| 1052 | zOpName = Tcl_GetString(objv[1]); |
| 1053 | for(i=0; i<ArraySize(aOp); i++){ |
| 1054 | if( strcmp(aOp[i].zName, zOpName)==0 ){ |
| 1055 | op = aOp[i].op; |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
| 1059 | if( i>=ArraySize(aOp) ){ |
| 1060 | if( Tcl_GetIntFromObj(interp, objv[1], &op) ) return TCL_ERROR; |
| 1061 | } |
| 1062 | if( Tcl_GetBooleanFromObj(interp, objv[2], &resetFlag) ) return TCL_ERROR; |
| 1063 | rc = sqlite3_status(op, &iValue, &mxValue, resetFlag); |
| 1064 | pResult = Tcl_NewObj(); |
| 1065 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); |
| 1066 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(iValue)); |
| 1067 | Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(mxValue)); |
| 1068 | Tcl_SetObjResult(interp, pResult); |
| 1069 | return TCL_OK; |
| 1070 | } |
drh | 9ac3fe9 | 2008-06-18 18:12:04 +0000 | [diff] [blame] | 1071 | |
| 1072 | /* |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 1073 | ** install_malloc_faultsim BOOLEAN |
| 1074 | */ |
| 1075 | static int test_install_malloc_faultsim( |
| 1076 | void * clientData, |
| 1077 | Tcl_Interp *interp, |
| 1078 | int objc, |
| 1079 | Tcl_Obj *CONST objv[] |
| 1080 | ){ |
| 1081 | int rc; |
| 1082 | int isInstall; |
| 1083 | |
| 1084 | if( objc!=2 ){ |
| 1085 | Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); |
| 1086 | return TCL_ERROR; |
| 1087 | } |
| 1088 | if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[1], &isInstall) ){ |
| 1089 | return TCL_ERROR; |
| 1090 | } |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 1091 | rc = faultsimInstall(isInstall); |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 1092 | Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); |
| 1093 | return TCL_OK; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1097 | ** Register commands with the TCL interpreter. |
| 1098 | */ |
| 1099 | int Sqlitetest_malloc_Init(Tcl_Interp *interp){ |
| 1100 | static struct { |
| 1101 | char *zName; |
| 1102 | Tcl_ObjCmdProc *xProc; |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1103 | int clientData; |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1104 | } aObjCmd[] = { |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1105 | { "sqlite3_malloc", test_malloc ,0. }, |
| 1106 | { "sqlite3_realloc", test_realloc ,0. }, |
| 1107 | { "sqlite3_free", test_free ,0. }, |
| 1108 | { "memset", test_memset ,0. }, |
| 1109 | { "memget", test_memget ,0. }, |
| 1110 | { "sqlite3_memory_used", test_memory_used ,0. }, |
| 1111 | { "sqlite3_memory_highwater", test_memory_highwater ,0. }, |
| 1112 | { "sqlite3_memdebug_backtrace", test_memdebug_backtrace ,0. }, |
| 1113 | { "sqlite3_memdebug_dump", test_memdebug_dump ,0. }, |
| 1114 | { "sqlite3_memdebug_fail", test_memdebug_fail ,0. }, |
| 1115 | { "sqlite3_memdebug_pending", test_memdebug_pending ,0. }, |
| 1116 | { "sqlite3_memdebug_settitle", test_memdebug_settitle ,0. }, |
| 1117 | { "sqlite3_memdebug_malloc_count", test_memdebug_malloc_count ,0. }, |
| 1118 | { "sqlite3_memdebug_log", test_memdebug_log ,0. }, |
| 1119 | { "sqlite3_config_scratch", test_config_scratch ,0. }, |
| 1120 | { "sqlite3_config_pagecache", test_config_pagecache ,0. }, |
| 1121 | { "sqlite3_status", test_status ,0. }, |
| 1122 | { "install_malloc_faultsim", test_install_malloc_faultsim ,0. }, |
danielk1977 | 0d84e5b | 2008-06-27 14:05:24 +0000 | [diff] [blame^] | 1123 | { "sqlite3_config_heap", test_config_heap ,0 }, |
| 1124 | { "sqlite3_dump_memsys3", test_dump_memsys3 ,3 }, |
| 1125 | { "sqlite3_dump_memsys5", test_dump_memsys3 ,5 } |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1126 | }; |
| 1127 | int i; |
| 1128 | for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
danielk1977 | c66c0e1 | 2008-06-25 14:26:07 +0000 | [diff] [blame] | 1129 | ClientData c = (ClientData)aObjCmd[i].clientData; |
| 1130 | Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, c, 0); |
drh | 2f999a6 | 2007-08-15 19:16:43 +0000 | [diff] [blame] | 1131 | } |
| 1132 | return TCL_OK; |
| 1133 | } |
danielk1977 | ef05f2d | 2008-06-20 11:05:37 +0000 | [diff] [blame] | 1134 | #endif |