1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
** 2007 August 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains code used to implement test interfaces to the
** memory allocation subsystem.
**
** $Id: test_malloc.c,v 1.8 2007/09/03 07:31:10 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*
** Transform pointers to text and back again
*/
static void pointerToText(void *p, char *z){
static const char zHex[] = "0123456789abcdef";
int i, k;
unsigned int u;
sqlite3_uint64 n;
if( sizeof(n)==sizeof(p) ){
memcpy(&n, &p, sizeof(p));
}else if( sizeof(u)==sizeof(p) ){
memcpy(&u, &p, sizeof(u));
n = u;
}else{
assert( 0 );
}
for(i=0, k=sizeof(p)*2-1; i<sizeof(p)*2; i++, k--){
z[k] = zHex[n&0xf];
n >>= 4;
}
z[sizeof(p)*2] = 0;
}
static int hexToInt(int h){
if( h>='0' && h<='9' ){
return h - '0';
}else if( h>='a' && h<='f' ){
return h - 'a' + 10;
}else{
return -1;
}
}
static int textToPointer(const char *z, void **pp){
sqlite3_uint64 n = 0;
int i;
unsigned int u;
for(i=0; i<sizeof(void*)*2 && z[0]; i++){
int v;
v = hexToInt(*z++);
if( v<0 ) return TCL_ERROR;
n = n*16 + v;
}
if( *z!=0 ) return TCL_ERROR;
if( sizeof(n)==sizeof(*pp) ){
memcpy(pp, &n, sizeof(n));
}else if( sizeof(u)==sizeof(*pp) ){
u = (unsigned int)n;
memcpy(pp, &u, sizeof(u));
}else{
assert( 0 );
}
return TCL_OK;
}
/*
** Usage: sqlite3_malloc NBYTES
**
** Raw test interface for sqlite3_malloc().
*/
static int test_malloc(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int nByte;
void *p;
char zOut[100];
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "NBYTES");
return TCL_ERROR;
}
if( Tcl_GetIntFromObj(interp, objv[1], &nByte) ) return TCL_ERROR;
p = sqlite3_malloc((unsigned)nByte);
pointerToText(p, zOut);
Tcl_AppendResult(interp, zOut, NULL);
return TCL_OK;
}
/*
** Usage: sqlite3_realloc PRIOR NBYTES
**
** Raw test interface for sqlite3_realloc().
*/
static int test_realloc(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int nByte;
void *pPrior, *p;
char zOut[100];
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "PRIOR NBYTES");
return TCL_ERROR;
}
if( Tcl_GetIntFromObj(interp, objv[2], &nByte) ) return TCL_ERROR;
if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){
Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0);
return TCL_ERROR;
}
p = sqlite3_realloc(pPrior, (unsigned)nByte);
pointerToText(p, zOut);
Tcl_AppendResult(interp, zOut, NULL);
return TCL_OK;
}
/*
** Usage: sqlite3_free PRIOR
**
** Raw test interface for sqlite3_free().
*/
static int test_free(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
void *pPrior;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "PRIOR");
return TCL_ERROR;
}
if( textToPointer(Tcl_GetString(objv[1]), &pPrior) ){
Tcl_AppendResult(interp, "bad pointer: ", Tcl_GetString(objv[1]), (char*)0);
return TCL_ERROR;
}
sqlite3_free(pPrior);
return TCL_OK;
}
/*
** Usage: sqlite3_memory_used
**
** Raw test interface for sqlite3_memory_used().
*/
static int test_memory_used(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_memory_used()));
return TCL_OK;
}
/*
** Usage: sqlite3_memory_highwater ?RESETFLAG?
**
** Raw test interface for sqlite3_memory_highwater().
*/
static int test_memory_highwater(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int resetFlag = 0;
if( objc!=1 && objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "?RESET?");
return TCL_ERROR;
}
if( objc==2 ){
if( Tcl_GetBooleanFromObj(interp, objv[1], &resetFlag) ) return TCL_ERROR;
}
Tcl_SetObjResult(interp,
Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag)));
return TCL_OK;
}
/*
** Usage: sqlite3_memdebug_backtrace DEPTH
**
** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined
** then this routine is a no-op.
*/
static int test_memdebug_backtrace(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int depth;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "DEPT");
return TCL_ERROR;
}
if( Tcl_GetIntFromObj(interp, objv[1], &depth) ) return TCL_ERROR;
#ifdef SQLITE_MEMDEBUG
{
extern void sqlite3_memdebug_backtrace(int);
sqlite3_memdebug_backtrace(depth);
}
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_memdebug_dump FILENAME
**
** Write a summary of unfreed memory to FILENAME.
*/
static int test_memdebug_dump(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
return TCL_ERROR;
}
#ifdef SQLITE_MEMDEBUG
{
extern void sqlite3_memdebug_dump(const char*);
sqlite3_memdebug_dump(Tcl_GetString(objv[1]));
}
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_memdebug_fail COUNTER ?OPTIONS?
**
** where options are:
**
** -repeat <boolean>
** -benigncnt <varname>
**
** Arrange for a simulated malloc() failure after COUNTER successes.
** If REPEAT is 1 then all subsequent malloc()s fail. If REPEAT is
** 0 then only a single failure occurs.
**
** Each call to this routine overrides the prior counter value.
** This routine returns the number of simulated failures that have
** happened since the previous call to this routine.
**
** To disable simulated failures, use a COUNTER of -1.
*/
static int test_memdebug_fail(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
int ii;
int iFail;
int iRepeat = -1;
Tcl_Obj *pBenignCnt = 0;
int nFail = 0;
if( objc<2 ){
Tcl_WrongNumArgs(interp, 1, objv, "COUNTER ?OPTIONS?");
return TCL_ERROR;
}
if( Tcl_GetIntFromObj(interp, objv[1], &iFail) ) return TCL_ERROR;
for(ii=2; ii<objc; ii+=2){
int nOption;
char *zOption = Tcl_GetStringFromObj(objv[ii], &nOption);
char *zErr = 0;
if( nOption>1 && strncmp(zOption, "-repeat", nOption)==0 ){
if( ii==(objc-1) ){
zErr = "option requires an argument: ";
}else{
if( Tcl_GetIntFromObj(interp, objv[ii+1], &iRepeat) ){
return TCL_ERROR;
}
}
}else if( nOption>1 && strncmp(zOption, "-benigncnt", nOption)==0 ){
if( ii==(objc-1) ){
zErr = "option requires an argument: ";
}else{
pBenignCnt = objv[ii+1];
}
}else{
zErr = "unknown option: ";
}
if( zErr ){
Tcl_AppendResult(interp, zErr, zOption, 0);
return TCL_ERROR;
}
}
#ifdef SQLITE_MEMDEBUG
{
extern int sqlite3_memdebug_fail(int,int,int*);
int iBenignCnt;
nFail = sqlite3_memdebug_fail(iFail, iRepeat, &iBenignCnt);
if( pBenignCnt ){
Tcl_ObjSetVar2(interp, pBenignCnt, 0, Tcl_NewIntObj(iBenignCnt), 0);
}
}
#endif
Tcl_SetObjResult(interp, Tcl_NewIntObj(nFail));
return TCL_OK;
}
/*
** Usage: sqlite3_memdebug_pending
**
** Return the number of malloc() calls that will succeed before a
** simulated failure occurs. A negative return value indicates that
** no malloc() failure is scheduled.
*/
static int test_memdebug_pending(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
#ifdef SQLITE_MEMDEBUG
{
extern int sqlite3_memdebug_pending();
Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_memdebug_pending()));
}
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_memdebug_settitle TITLE
**
** Set a title string stored with each allocation. The TITLE is
** typically the name of the test that was running when the
** allocation occurred. The TITLE is stored with the allocation
** and can be used to figure out which tests are leaking memory.
**
** Each title overwrite the previous.
*/
static int test_memdebug_settitle(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
const char *zTitle;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "TITLE");
return TCL_ERROR;
}
zTitle = Tcl_GetString(objv[1]);
#ifdef SQLITE_MEMDEBUG
{
extern int sqlite3_memdebug_settitle(const char*);
sqlite3_memdebug_settitle(zTitle);
}
#endif
return TCL_OK;
}
/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest_malloc_Init(Tcl_Interp *interp){
static struct {
char *zName;
Tcl_ObjCmdProc *xProc;
} aObjCmd[] = {
{ "sqlite3_malloc", test_malloc },
{ "sqlite3_realloc", test_realloc },
{ "sqlite3_free", test_free },
{ "sqlite3_memory_used", test_memory_used },
{ "sqlite3_memory_highwater", test_memory_highwater },
{ "sqlite3_memdebug_backtrace", test_memdebug_backtrace },
{ "sqlite3_memdebug_dump", test_memdebug_dump },
{ "sqlite3_memdebug_fail", test_memdebug_fail },
{ "sqlite3_memdebug_pending", test_memdebug_pending },
{ "sqlite3_memdebug_settitle", test_memdebug_settitle },
};
int i;
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
}
return TCL_OK;
}
|