4
** The author disclaims copyright to this source code. In place of
5
** a legal notice, here is a blessing:
7
** May you do good and not evil.
8
** May you find forgiveness for yourself and forgive others.
9
** May you share freely, never taking more than you give.
11
*************************************************************************
12
** Code for testing the btree.c module in SQLite. This code
13
** is not included in the SQLite library. It is used for automated
14
** testing of the SQLite library.
16
** $Id: test_btree.c,v 1.3 2007/08/17 01:14:39 drh Exp $
22
** Print a disassembly of the given page on standard output. This routine
23
** is used for debugging and testing only.
25
static int btreePageDump(
26
BtShared *pBt, /* The Btree to be dumped */
27
int pgno, /* The page to be dumped */
28
int recursive, /* True to decend into child pages */
29
MemPage *pParent /* Parent page */
41
unsigned char payload[20];
43
rc = sqlite3BtreeGetPage(pBt, (Pgno)pgno, &pPage, 0);
44
isInit = pPage->isInit;
45
if( pPage->isInit==0 ){
46
sqlite3BtreeInitPage(pPage, pParent);
51
hdr = pPage->hdrOffset;
54
pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0;
55
pPage->zeroData = (c & PTF_ZERODATA)!=0;
56
pPage->leafData = (c & PTF_LEAFDATA)!=0;
57
pPage->leaf = (c & PTF_LEAF)!=0;
58
pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
59
nCell = get2byte(&data[hdr+3]);
60
sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno,
61
data[hdr], data[hdr+7],
62
(pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0);
63
assert( hdr == (pgno==1 ? 100 : 0) );
64
idx = hdr + 12 - pPage->leaf*4;
65
for(i=0; i<nCell; i++){
72
addr = get2byte(&data[idx + 2*i]);
74
sqlite3BtreeParseCellPtr(pPage, pCell, &info);
76
sqlite3_snprintf(sizeof(range),range,"%d..%d", addr, addr+sz-1);
80
child = get4byte(pCell);
83
if( !pPage->intKey ) sz += info.nKey;
84
if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
85
memcpy(payload, &pCell[info.nHeader], sz);
87
if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
91
"cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n",
92
i, range, child, info.nKey, info.nData, payload
96
sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8]));
100
idx = get2byte(&data[hdr+1]);
101
while( idx>0 && idx<pPage->pBt->usableSize ){
102
int sz = get2byte(&data[idx+2]);
103
sqlite3_snprintf(sizeof(range),range,"%d..%d", idx, idx+sz-1);
105
sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
106
i, range, sz, nFree);
107
idx = get2byte(&data[idx]);
111
sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx);
113
if( recursive && !pPage->leaf ){
114
for(i=0; i<nCell; i++){
115
unsigned char *pCell = sqlite3BtreeFindCell(pPage, i);
116
btreePageDump(pBt, get4byte(pCell), 1, pPage);
117
idx = get2byte(pCell);
119
btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
121
pPage->isInit = isInit;
122
sqlite3PagerUnref(pPage->pDbPage);
126
int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){
127
return btreePageDump(p->pBt, pgno, recursive, 0);
131
** Usage: sqlite3_shared_cache_report
133
** Return a list of file that are shared and the number of
134
** references to each file.
136
int sqlite3BtreeSharedCacheReport(
140
Tcl_Obj *CONST objv[]
142
#ifndef SQLITE_OMIT_SHARED_CACHE
143
extern BtShared *sqlite3SharedCacheList;
145
Tcl_Obj *pRet = Tcl_NewObj();
146
for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
147
const char *zFile = sqlite3PagerFilename(pBt->pPager);
148
Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
149
Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
151
Tcl_SetObjResult(interp, pRet);
157
** Print debugging information about all cursors to standard output.
159
void sqlite3BtreeCursorList(Btree *p){
161
BtShared *pBt = p->pBt;
162
for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
163
MemPage *pPage = pCur->pPage;
164
char *zMode = pCur->wrFlag ? "rw" : "ro";
165
sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
166
pCur, pCur->pgnoRoot, zMode,
167
pPage ? pPage->pgno : 0, pCur->idx,
168
(pCur->eState==CURSOR_VALID) ? "" : " eof"
175
** Fill aResult[] with information about the entry and page that the
176
** cursor is pointing to.
178
** aResult[0] = The page number
179
** aResult[1] = The entry number
180
** aResult[2] = Total number of entries on this page
181
** aResult[3] = Cell size (local payload + header)
182
** aResult[4] = Number of free bytes on this page
183
** aResult[5] = Number of free blocks on the page
184
** aResult[6] = Total payload size (local + overflow)
185
** aResult[7] = Header size in bytes
186
** aResult[8] = Local payload size
187
** aResult[9] = Parent page number
188
** aResult[10]= Page number of the first overflow page
190
** This routine is used for testing and debugging only.
192
int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
194
MemPage *pPage = pCur->pPage;
198
if( pCur->eState==CURSOR_REQUIRESEEK ){
199
rc = sqlite3BtreeRestoreOrClearCursorPosition(pCur);
205
assert( pPage->isInit );
206
sqlite3BtreeGetTempCursor(pCur, &tmpCur);
208
sqlite3BtreeMoveToParent(&tmpCur);
210
pPage = tmpCur.pPage;
211
aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
212
assert( aResult[0]==pPage->pgno );
213
aResult[1] = tmpCur.idx;
214
aResult[2] = pPage->nCell;
215
if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
216
sqlite3BtreeParseCell(tmpCur.pPage, tmpCur.idx, &tmpCur.info);
217
aResult[3] = tmpCur.info.nSize;
218
aResult[6] = tmpCur.info.nData;
219
aResult[7] = tmpCur.info.nHeader;
220
aResult[8] = tmpCur.info.nLocal;
227
aResult[4] = pPage->nFree;
229
idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
230
while( idx>0 && idx<pPage->pBt->usableSize ){
232
idx = get2byte(&pPage->aData[idx]);
235
if( pPage->pParent==0 || sqlite3BtreeIsRootPage(pPage) ){
238
aResult[9] = pPage->pParent->pgno;
240
if( tmpCur.info.iOverflow ){
241
aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
245
sqlite3BtreeReleaseTempCursor(&tmpCur);