blob: 5022b009e6bf7772a1d6eba7189f2fde67e988e3 [file] [log] [blame]
drh27338e62013-04-06 00:19:37 +00001/*
2** 2013-04-05
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 is a program used for testing SQLite, and specifically for testing
14** the ability of independent processes to access the same SQLite database
15** concurrently.
16**
17** Compile this program as follows:
18**
19** gcc -g -c -Wall sqlite3.c $(OPTS)
20** gcc -g -o mptest mptest.c sqlite3.o $(LIBS)
21**
22** Recommended options:
23**
24** -DHAVE_USLEEP
25** -DSQLITE_NO_SYNC
26** -DSQLITE_THREADSAFE=0
27** -DSQLITE_OMIT_LOAD_EXTENSION
28**
29** Run like this:
30**
31** ./mptest $database $script
32**
33** where $database is the database to use for testing and $script is a
34** test script.
35*/
36#include "sqlite3.h"
37#include <stdio.h>
drhbc94dbb2013-04-08 14:28:33 +000038#if defined(_WIN32)
mistachkin08d41892013-04-11 00:09:44 +000039# define WIN32_LEAN_AND_MEAN
40# include <windows.h>
drhbc94dbb2013-04-08 14:28:33 +000041#else
42# include <unistd.h>
43#endif
drhcd423522016-02-12 17:27:32 +000044#include <errno.h>
drh27338e62013-04-06 00:19:37 +000045#include <stdlib.h>
46#include <string.h>
47#include <assert.h>
48#include <ctype.h>
49
drhc56fac72015-10-29 13:48:15 +000050#define ISSPACE(X) isspace((unsigned char)(X))
51#define ISDIGIT(X) isdigit((unsigned char)(X))
52
mistachkin08d41892013-04-11 00:09:44 +000053/* The suffix to append to the child command lines, if any */
54#if defined(_WIN32)
mistachkinfdd72c92013-04-11 21:13:10 +000055# define GETPID (int)GetCurrentProcessId
mistachkin08d41892013-04-11 00:09:44 +000056#else
mistachkinfdd72c92013-04-11 21:13:10 +000057# define GETPID getpid
mistachkin08d41892013-04-11 00:09:44 +000058#endif
59
mistachkin25a72de2015-03-31 17:58:13 +000060/* The directory separator character(s) */
61#if defined(_WIN32)
62# define isDirSep(c) (((c) == '/') || ((c) == '\\'))
63#else
64# define isDirSep(c) ((c) == '/')
65#endif
66
drh841810c2013-04-08 13:59:11 +000067/* Mark a parameter as unused to suppress compiler warnings */
68#define UNUSED_PARAMETER(x) (void)x
69
drh27338e62013-04-06 00:19:37 +000070/* Global data
71*/
72static struct Global {
73 char *argv0; /* Name of the executable */
74 const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */
75 char *zDbFile; /* Name of the database */
76 sqlite3 *db; /* Open connection to database */
77 char *zErrLog; /* Filename for error log */
78 FILE *pErrLog; /* Where to write errors */
79 char *zLog; /* Name of output log file */
80 FILE *pLog; /* Where to write log messages */
drhe3be8c82013-04-11 11:53:45 +000081 char zName[32]; /* Symbolic name of this process */
drh27338e62013-04-06 00:19:37 +000082 int taskId; /* Task ID. 0 means supervisor. */
83 int iTrace; /* Tracing level */
84 int bSqlTrace; /* True to trace SQL commands */
drhbc082812013-04-18 15:11:03 +000085 int bIgnoreSqlErrors; /* Ignore errors in SQL statements */
drh27338e62013-04-06 00:19:37 +000086 int nError; /* Number of errors */
drh3f5bc382013-04-06 13:09:11 +000087 int nTest; /* Number of --match operators */
88 int iTimeout; /* Milliseconds until a busy timeout */
drhbc94dbb2013-04-08 14:28:33 +000089 int bSync; /* Call fsync() */
drh27338e62013-04-06 00:19:37 +000090} g;
91
drh3f5bc382013-04-06 13:09:11 +000092/* Default timeout */
93#define DEFAULT_TIMEOUT 10000
94
drh27338e62013-04-06 00:19:37 +000095/*
96** Print a message adding zPrefix[] to the beginning of every line.
97*/
98static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){
99 while( zMsg && zMsg[0] ){
100 int i;
101 for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){}
102 fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg);
103 zMsg += i;
104 while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++;
105 }
106}
107
108/*
109** Compare two pointers to strings, where the pointers might be NULL.
110*/
111static int safe_strcmp(const char *a, const char *b){
112 if( a==b ) return 0;
113 if( a==0 ) return -1;
114 if( b==0 ) return 1;
115 return strcmp(a,b);
116}
117
118/*
119** Return TRUE if string z[] matches glob pattern zGlob[].
120** Return FALSE if the pattern does not match.
121**
122** Globbing rules:
123**
124** '*' Matches any sequence of zero or more characters.
125**
126** '?' Matches exactly one character.
127**
128** [...] Matches one character from the enclosed list of
129** characters.
130**
131** [^...] Matches one character not in the enclosed list.
132**
133** '#' Matches any sequence of one or more digits with an
134** optional + or - sign in front
135*/
136int strglob(const char *zGlob, const char *z){
137 int c, c2;
138 int invert;
139 int seen;
140
141 while( (c = (*(zGlob++)))!=0 ){
142 if( c=='*' ){
143 while( (c=(*(zGlob++))) == '*' || c=='?' ){
144 if( c=='?' && (*(z++))==0 ) return 0;
145 }
146 if( c==0 ){
147 return 1;
148 }else if( c=='[' ){
149 while( *z && strglob(zGlob-1,z) ){
150 z++;
151 }
152 return (*z)!=0;
153 }
154 while( (c2 = (*(z++)))!=0 ){
155 while( c2!=c ){
156 c2 = *(z++);
157 if( c2==0 ) return 0;
158 }
159 if( strglob(zGlob,z) ) return 1;
160 }
161 return 0;
162 }else if( c=='?' ){
163 if( (*(z++))==0 ) return 0;
164 }else if( c=='[' ){
165 int prior_c = 0;
166 seen = 0;
167 invert = 0;
168 c = *(z++);
169 if( c==0 ) return 0;
170 c2 = *(zGlob++);
171 if( c2=='^' ){
172 invert = 1;
173 c2 = *(zGlob++);
174 }
175 if( c2==']' ){
176 if( c==']' ) seen = 1;
177 c2 = *(zGlob++);
178 }
179 while( c2 && c2!=']' ){
180 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
181 c2 = *(zGlob++);
182 if( c>=prior_c && c<=c2 ) seen = 1;
183 prior_c = 0;
184 }else{
185 if( c==c2 ){
186 seen = 1;
187 }
188 prior_c = c2;
189 }
190 c2 = *(zGlob++);
191 }
192 if( c2==0 || (seen ^ invert)==0 ) return 0;
193 }else if( c=='#' ){
drhc56fac72015-10-29 13:48:15 +0000194 if( (z[0]=='-' || z[0]=='+') && ISDIGIT(z[1]) ) z++;
195 if( !ISDIGIT(z[0]) ) return 0;
drh27338e62013-04-06 00:19:37 +0000196 z++;
drhc56fac72015-10-29 13:48:15 +0000197 while( ISDIGIT(z[0]) ){ z++; }
drh27338e62013-04-06 00:19:37 +0000198 }else{
199 if( c!=(*(z++)) ) return 0;
200 }
201 }
202 return *z==0;
203}
204
205/*
206** Close output stream pOut if it is not stdout or stderr
207*/
208static void maybeClose(FILE *pOut){
209 if( pOut!=stdout && pOut!=stderr ) fclose(pOut);
210}
211
212/*
213** Print an error message
214*/
215static void errorMessage(const char *zFormat, ...){
216 va_list ap;
217 char *zMsg;
218 char zPrefix[30];
219 va_start(ap, zFormat);
220 zMsg = sqlite3_vmprintf(zFormat, ap);
221 va_end(ap);
222 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName);
223 if( g.pLog ){
224 printWithPrefix(g.pLog, zPrefix, zMsg);
225 fflush(g.pLog);
226 }
227 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
228 printWithPrefix(g.pErrLog, zPrefix, zMsg);
229 fflush(g.pErrLog);
230 }
231 sqlite3_free(zMsg);
232 g.nError++;
233}
234
235/* Forward declaration */
236static int trySql(const char*, ...);
237
238/*
239** Print an error message and then quit.
240*/
241static void fatalError(const char *zFormat, ...){
242 va_list ap;
243 char *zMsg;
244 char zPrefix[30];
245 va_start(ap, zFormat);
246 zMsg = sqlite3_vmprintf(zFormat, ap);
247 va_end(ap);
248 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName);
249 if( g.pLog ){
250 printWithPrefix(g.pLog, zPrefix, zMsg);
251 fflush(g.pLog);
252 maybeClose(g.pLog);
253 }
254 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
255 printWithPrefix(g.pErrLog, zPrefix, zMsg);
256 fflush(g.pErrLog);
257 maybeClose(g.pErrLog);
258 }
259 sqlite3_free(zMsg);
260 if( g.db ){
261 int nTry = 0;
drh3f5bc382013-04-06 13:09:11 +0000262 g.iTimeout = 0;
263 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
264 && (nTry++)<100 ){
drh27338e62013-04-06 00:19:37 +0000265 sqlite3_sleep(10);
266 }
267 }
268 sqlite3_close(g.db);
269 exit(1);
270}
271
272
273/*
274** Print a log message
275*/
276static void logMessage(const char *zFormat, ...){
277 va_list ap;
278 char *zMsg;
279 char zPrefix[30];
280 va_start(ap, zFormat);
281 zMsg = sqlite3_vmprintf(zFormat, ap);
282 va_end(ap);
283 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName);
284 if( g.pLog ){
285 printWithPrefix(g.pLog, zPrefix, zMsg);
286 fflush(g.pLog);
287 }
288 sqlite3_free(zMsg);
289}
290
291/*
292** Return the length of a string omitting trailing whitespace
293*/
294static int clipLength(const char *z){
295 int n = (int)strlen(z);
drhc56fac72015-10-29 13:48:15 +0000296 while( n>0 && ISSPACE(z[n-1]) ){ n--; }
drh27338e62013-04-06 00:19:37 +0000297 return n;
298}
299
300/*
drh1bf44c72013-04-08 13:48:29 +0000301** Auxiliary SQL function to return the name of the VFS
302*/
303static void vfsNameFunc(
304 sqlite3_context *context,
305 int argc,
306 sqlite3_value **argv
307){
308 sqlite3 *db = sqlite3_context_db_handle(context);
309 char *zVfs = 0;
drh841810c2013-04-08 13:59:11 +0000310 UNUSED_PARAMETER(argc);
311 UNUSED_PARAMETER(argv);
drh1bf44c72013-04-08 13:48:29 +0000312 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs);
313 if( zVfs ){
314 sqlite3_result_text(context, zVfs, -1, sqlite3_free);
315 }
316}
317
318/*
drh3f5bc382013-04-06 13:09:11 +0000319** Busy handler with a g.iTimeout-millisecond timeout
320*/
321static int busyHandler(void *pCD, int count){
drh841810c2013-04-08 13:59:11 +0000322 UNUSED_PARAMETER(pCD);
drh3f5bc382013-04-06 13:09:11 +0000323 if( count*10>g.iTimeout ){
324 if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout);
325 return 0;
326 }
327 sqlite3_sleep(10);
328 return 1;
329}
330
331/*
drh27338e62013-04-06 00:19:37 +0000332** SQL Trace callback
333*/
334static void sqlTraceCallback(void *NotUsed1, const char *zSql){
drh841810c2013-04-08 13:59:11 +0000335 UNUSED_PARAMETER(NotUsed1);
drh27338e62013-04-06 00:19:37 +0000336 logMessage("[%.*s]", clipLength(zSql), zSql);
337}
338
339/*
drh1790bb32013-04-06 14:30:29 +0000340** SQL error log callback
341*/
342static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){
drh841810c2013-04-08 13:59:11 +0000343 UNUSED_PARAMETER(pArg);
drhbc082812013-04-18 15:11:03 +0000344 if( iErrCode==SQLITE_ERROR && g.bIgnoreSqlErrors ) return;
drh1790bb32013-04-06 14:30:29 +0000345 if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return;
drhe5ebd222013-04-08 15:36:51 +0000346 if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return;
drhe3be8c82013-04-11 11:53:45 +0000347 if( (iErrCode&0xff)==SQLITE_NOTICE ){
drhab755ac2013-04-09 18:36:36 +0000348 logMessage("(info) %s", zMsg);
349 }else{
350 errorMessage("(errcode=%d) %s", iErrCode, zMsg);
351 }
drh1790bb32013-04-06 14:30:29 +0000352}
353
354/*
drh27338e62013-04-06 00:19:37 +0000355** Prepare an SQL statement. Issue a fatal error if unable.
356*/
357static sqlite3_stmt *prepareSql(const char *zFormat, ...){
358 va_list ap;
359 char *zSql;
360 int rc;
361 sqlite3_stmt *pStmt = 0;
362 va_start(ap, zFormat);
363 zSql = sqlite3_vmprintf(zFormat, ap);
364 va_end(ap);
365 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
366 if( rc!=SQLITE_OK ){
367 sqlite3_finalize(pStmt);
368 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
369 }
370 sqlite3_free(zSql);
371 return pStmt;
372}
373
374/*
375** Run arbitrary SQL. Issue a fatal error on failure.
376*/
377static void runSql(const char *zFormat, ...){
378 va_list ap;
379 char *zSql;
380 int rc;
381 va_start(ap, zFormat);
382 zSql = sqlite3_vmprintf(zFormat, ap);
383 va_end(ap);
384 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
385 if( rc!=SQLITE_OK ){
386 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
387 }
388 sqlite3_free(zSql);
389}
390
391/*
392** Try to run arbitrary SQL. Return success code.
393*/
394static int trySql(const char *zFormat, ...){
395 va_list ap;
396 char *zSql;
397 int rc;
398 va_start(ap, zFormat);
399 zSql = sqlite3_vmprintf(zFormat, ap);
400 va_end(ap);
401 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
402 sqlite3_free(zSql);
403 return rc;
404}
405
406/* Structure for holding an arbitrary length string
407*/
408typedef struct String String;
409struct String {
410 char *z; /* the string */
411 int n; /* Slots of z[] used */
412 int nAlloc; /* Slots of z[] allocated */
413};
414
415/* Free a string */
416static void stringFree(String *p){
417 if( p->z ) sqlite3_free(p->z);
418 memset(p, 0, sizeof(*p));
419}
420
421/* Append n bytes of text to a string. If n<0 append the entire string. */
422static void stringAppend(String *p, const char *z, int n){
423 if( n<0 ) n = (int)strlen(z);
424 if( p->n+n>=p->nAlloc ){
425 int nAlloc = p->nAlloc*2 + n + 100;
mistachkin8ccdef62015-12-16 22:06:52 +0000426 char *zNew = sqlite3_realloc(p->z, nAlloc);
427 if( zNew==0 ) fatalError("out of memory");
428 p->z = zNew;
drh27338e62013-04-06 00:19:37 +0000429 p->nAlloc = nAlloc;
430 }
431 memcpy(p->z+p->n, z, n);
432 p->n += n;
433 p->z[p->n] = 0;
434}
435
436/* Reset a string to an empty string */
437static void stringReset(String *p){
438 if( p->z==0 ) stringAppend(p, " ", 1);
439 p->n = 0;
440 p->z[0] = 0;
441}
442
443/* Append a new token onto the end of the string */
444static void stringAppendTerm(String *p, const char *z){
445 int i;
446 if( p->n ) stringAppend(p, " ", 1);
447 if( z==0 ){
448 stringAppend(p, "nil", 3);
449 return;
450 }
drhc56fac72015-10-29 13:48:15 +0000451 for(i=0; z[i] && !ISSPACE(z[i]); i++){}
drh27338e62013-04-06 00:19:37 +0000452 if( i>0 && z[i]==0 ){
453 stringAppend(p, z, i);
454 return;
455 }
456 stringAppend(p, "'", 1);
457 while( z[0] ){
458 for(i=0; z[i] && z[i]!='\''; i++){}
459 if( z[i] ){
460 stringAppend(p, z, i+1);
461 stringAppend(p, "'", 1);
462 z += i+1;
463 }else{
464 stringAppend(p, z, i);
465 break;
466 }
467 }
468 stringAppend(p, "'", 1);
469}
470
471/*
472** Callback function for evalSql()
473*/
474static int evalCallback(void *pCData, int argc, char **argv, char **azCol){
475 String *p = (String*)pCData;
476 int i;
drh841810c2013-04-08 13:59:11 +0000477 UNUSED_PARAMETER(azCol);
drh27338e62013-04-06 00:19:37 +0000478 for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]);
479 return 0;
480}
481
482/*
483** Run arbitrary SQL and record the results in an output string
484** given by the first parameter.
485*/
486static int evalSql(String *p, const char *zFormat, ...){
487 va_list ap;
488 char *zSql;
489 int rc;
490 char *zErrMsg = 0;
491 va_start(ap, zFormat);
492 zSql = sqlite3_vmprintf(zFormat, ap);
493 va_end(ap);
drh3f5bc382013-04-06 13:09:11 +0000494 assert( g.iTimeout>0 );
drh27338e62013-04-06 00:19:37 +0000495 rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg);
496 sqlite3_free(zSql);
497 if( rc ){
498 char zErr[30];
499 sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc);
500 stringAppendTerm(p, zErr);
501 if( zErrMsg ){
502 stringAppendTerm(p, zErrMsg);
503 sqlite3_free(zErrMsg);
504 }
505 }
506 return rc;
507}
508
509/*
drh1bf44c72013-04-08 13:48:29 +0000510** Auxiliary SQL function to recursively evaluate SQL.
511*/
512static void evalFunc(
513 sqlite3_context *context,
514 int argc,
515 sqlite3_value **argv
516){
517 sqlite3 *db = sqlite3_context_db_handle(context);
518 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
519 String res;
520 char *zErrMsg = 0;
521 int rc;
drh841810c2013-04-08 13:59:11 +0000522 UNUSED_PARAMETER(argc);
drh1bf44c72013-04-08 13:48:29 +0000523 memset(&res, 0, sizeof(res));
524 rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg);
525 if( zErrMsg ){
526 sqlite3_result_error(context, zErrMsg, -1);
527 sqlite3_free(zErrMsg);
528 }else if( rc ){
529 sqlite3_result_error_code(context, rc);
530 }else{
531 sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT);
532 }
533 stringFree(&res);
534}
535
536/*
drh27338e62013-04-06 00:19:37 +0000537** Look up the next task for client iClient in the database.
538** Return the task script and the task number and mark that
539** task as being under way.
540*/
541static int startScript(
542 int iClient, /* The client number */
543 char **pzScript, /* Write task script here */
drh4c5298f2013-04-10 12:01:21 +0000544 int *pTaskId, /* Write task number here */
545 char **pzTaskName /* Name of the task */
drh27338e62013-04-06 00:19:37 +0000546){
547 sqlite3_stmt *pStmt = 0;
548 int taskId;
549 int rc;
drh3f5bc382013-04-06 13:09:11 +0000550 int totalTime = 0;
drh27338e62013-04-06 00:19:37 +0000551
552 *pzScript = 0;
drh3f5bc382013-04-06 13:09:11 +0000553 g.iTimeout = 0;
drh27338e62013-04-06 00:19:37 +0000554 while(1){
drhf90e50f2013-04-08 19:13:48 +0000555 rc = trySql("BEGIN IMMEDIATE");
drh27338e62013-04-06 00:19:37 +0000556 if( rc==SQLITE_BUSY ){
557 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +0000558 totalTime += 10;
drh27338e62013-04-06 00:19:37 +0000559 continue;
560 }
561 if( rc!=SQLITE_OK ){
drh6adab7a2013-04-08 18:58:00 +0000562 fatalError("in startScript: %s", sqlite3_errmsg(g.db));
drh27338e62013-04-06 00:19:37 +0000563 }
drh3f5bc382013-04-06 13:09:11 +0000564 if( g.nError || g.nTest ){
565 runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d",
566 g.nError, g.nTest);
drh27338e62013-04-06 00:19:37 +0000567 g.nError = 0;
drh3f5bc382013-04-06 13:09:11 +0000568 g.nTest = 0;
569 }
570 pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient);
571 rc = sqlite3_step(pStmt);
572 sqlite3_finalize(pStmt);
573 if( rc==SQLITE_ROW ){
574 runSql("DELETE FROM client WHERE id=%d", iClient);
drh3f5bc382013-04-06 13:09:11 +0000575 g.iTimeout = DEFAULT_TIMEOUT;
drhf90e50f2013-04-08 19:13:48 +0000576 runSql("COMMIT TRANSACTION;");
drh3f5bc382013-04-06 13:09:11 +0000577 return SQLITE_DONE;
drh27338e62013-04-06 00:19:37 +0000578 }
579 pStmt = prepareSql(
drh4c5298f2013-04-10 12:01:21 +0000580 "SELECT script, id, name FROM task"
drh27338e62013-04-06 00:19:37 +0000581 " WHERE client=%d AND starttime IS NULL"
582 " ORDER BY id LIMIT 1", iClient);
583 rc = sqlite3_step(pStmt);
584 if( rc==SQLITE_ROW ){
585 int n = sqlite3_column_bytes(pStmt, 0);
drhf90e50f2013-04-08 19:13:48 +0000586 *pzScript = sqlite3_malloc(n+1);
drh27338e62013-04-06 00:19:37 +0000587 strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0));
588 *pTaskId = taskId = sqlite3_column_int(pStmt, 1);
drh4c5298f2013-04-10 12:01:21 +0000589 *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2));
drh27338e62013-04-06 00:19:37 +0000590 sqlite3_finalize(pStmt);
591 runSql("UPDATE task"
592 " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
593 " WHERE id=%d;", taskId);
drh3f5bc382013-04-06 13:09:11 +0000594 g.iTimeout = DEFAULT_TIMEOUT;
drhf90e50f2013-04-08 19:13:48 +0000595 runSql("COMMIT TRANSACTION;");
drh27338e62013-04-06 00:19:37 +0000596 return SQLITE_OK;
597 }
598 sqlite3_finalize(pStmt);
599 if( rc==SQLITE_DONE ){
drh3f5bc382013-04-06 13:09:11 +0000600 if( totalTime>30000 ){
601 errorMessage("Waited over 30 seconds with no work. Giving up.");
602 runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient);
603 sqlite3_close(g.db);
604 exit(1);
605 }
drhf90e50f2013-04-08 19:13:48 +0000606 while( trySql("COMMIT")==SQLITE_BUSY ){
607 sqlite3_sleep(10);
608 totalTime += 10;
609 }
drh27338e62013-04-06 00:19:37 +0000610 sqlite3_sleep(100);
drh3f5bc382013-04-06 13:09:11 +0000611 totalTime += 100;
drh27338e62013-04-06 00:19:37 +0000612 continue;
613 }
614 fatalError("%s", sqlite3_errmsg(g.db));
615 }
drh3f5bc382013-04-06 13:09:11 +0000616 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +0000617}
618
619/*
drh3f5bc382013-04-06 13:09:11 +0000620** Mark a script as having finished. Remove the CLIENT table entry
621** if bShutdown is true.
drh27338e62013-04-06 00:19:37 +0000622*/
drh3f5bc382013-04-06 13:09:11 +0000623static int finishScript(int iClient, int taskId, int bShutdown){
624 runSql("UPDATE task"
625 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
626 " WHERE id=%d;", taskId);
627 if( bShutdown ){
628 runSql("DELETE FROM client WHERE id=%d", iClient);
drh27338e62013-04-06 00:19:37 +0000629 }
drh3f5bc382013-04-06 13:09:11 +0000630 return SQLITE_OK;
631}
632
633/*
634** Start up a client process for iClient, if it is not already
635** running. If the client is already running, then this routine
636** is a no-op.
637*/
638static void startClient(int iClient){
639 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient);
640 if( sqlite3_changes(g.db) ){
641 char *zSys;
drhbc94dbb2013-04-08 14:28:33 +0000642 int rc;
drh739ee7f2013-04-12 01:04:36 +0000643 zSys = sqlite3_mprintf("%s \"%s\" --client %d --trace %d",
644 g.argv0, g.zDbFile, iClient, g.iTrace);
645 if( g.bSqlTrace ){
646 zSys = sqlite3_mprintf("%z --sqltrace", zSys);
647 }
648 if( g.bSync ){
649 zSys = sqlite3_mprintf("%z --sync", zSys);
650 }
651 if( g.zVfs ){
652 zSys = sqlite3_mprintf("%z --vfs \"%s\"", zSys, g.zVfs);
653 }
654 if( g.iTrace>=2 ) logMessage("system('%q')", zSys);
mistachkin08d41892013-04-11 00:09:44 +0000655#if !defined(_WIN32)
drh739ee7f2013-04-12 01:04:36 +0000656 zSys = sqlite3_mprintf("%z &", zSys);
drhbc94dbb2013-04-08 14:28:33 +0000657 rc = system(zSys);
658 if( rc ) errorMessage("system() fails with error code %d", rc);
mistachkin08d41892013-04-11 00:09:44 +0000659#else
660 {
661 STARTUPINFOA startupInfo;
662 PROCESS_INFORMATION processInfo;
663 memset(&startupInfo, 0, sizeof(startupInfo));
664 startupInfo.cb = sizeof(startupInfo);
665 memset(&processInfo, 0, sizeof(processInfo));
666 rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL,
667 &startupInfo, &processInfo);
668 if( rc ){
669 CloseHandle(processInfo.hThread);
670 CloseHandle(processInfo.hProcess);
671 }else{
672 errorMessage("CreateProcessA() fails with error code %lu",
673 GetLastError());
674 }
drhf012ae02013-04-06 14:04:22 +0000675 }
drhf012ae02013-04-06 14:04:22 +0000676#endif
mistachkin08d41892013-04-11 00:09:44 +0000677 sqlite3_free(zSys);
drh3f5bc382013-04-06 13:09:11 +0000678 }
drh27338e62013-04-06 00:19:37 +0000679}
680
681/*
682** Read the entire content of a file into memory
683*/
684static char *readFile(const char *zFilename){
685 FILE *in = fopen(zFilename, "rb");
686 long sz;
687 char *z;
688 if( in==0 ){
689 fatalError("cannot open \"%s\" for reading", zFilename);
690 }
691 fseek(in, 0, SEEK_END);
692 sz = ftell(in);
693 rewind(in);
694 z = sqlite3_malloc( sz+1 );
695 sz = (long)fread(z, 1, sz, in);
696 z[sz] = 0;
697 fclose(in);
698 return z;
699}
700
701/*
702** Return the length of the next token.
703*/
704static int tokenLength(const char *z, int *pnLine){
705 int n = 0;
drhc56fac72015-10-29 13:48:15 +0000706 if( ISSPACE(z[0]) || (z[0]=='/' && z[1]=='*') ){
drh27338e62013-04-06 00:19:37 +0000707 int inC = 0;
708 int c;
709 if( z[0]=='/' ){
710 inC = 1;
711 n = 2;
712 }
713 while( (c = z[n++])!=0 ){
714 if( c=='\n' ) (*pnLine)++;
drhc56fac72015-10-29 13:48:15 +0000715 if( ISSPACE(c) ) continue;
drh27338e62013-04-06 00:19:37 +0000716 if( inC && c=='*' && z[n]=='/' ){
717 n++;
718 inC = 0;
719 }else if( !inC && c=='/' && z[n]=='*' ){
720 n++;
721 inC = 1;
722 }else if( !inC ){
723 break;
724 }
725 }
726 n--;
727 }else if( z[0]=='-' && z[1]=='-' ){
728 for(n=2; z[n] && z[n]!='\n'; n++){}
729 if( z[n] ){ (*pnLine)++; n++; }
730 }else if( z[0]=='"' || z[0]=='\'' ){
731 int delim = z[0];
732 for(n=1; z[n]; n++){
733 if( z[n]=='\n' ) (*pnLine)++;
734 if( z[n]==delim ){
735 n++;
736 if( z[n+1]!=delim ) break;
737 }
738 }
739 }else{
740 int c;
drhc56fac72015-10-29 13:48:15 +0000741 for(n=1; (c = z[n])!=0 && !ISSPACE(c) && c!='"' && c!='\'' && c!=';'; n++){}
drh27338e62013-04-06 00:19:37 +0000742 }
743 return n;
744}
745
746/*
747** Copy a single token into a string buffer.
748*/
749static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
750 int i;
751 if( nIn<=0 ){
752 zOut[0] = 0;
753 return 0;
754 }
drhc56fac72015-10-29 13:48:15 +0000755 for(i=0; i<nIn && i<nOut-1 && !ISSPACE(zIn[i]); i++){ zOut[i] = zIn[i]; }
drh27338e62013-04-06 00:19:37 +0000756 zOut[i] = 0;
757 return i;
758}
759
760/*
drh7dfe8e22013-04-08 13:13:43 +0000761** Find the number of characters up to the start of the next "--end" token.
drh27338e62013-04-06 00:19:37 +0000762*/
763static int findEnd(const char *z, int *pnLine){
764 int n = 0;
drhc56fac72015-10-29 13:48:15 +0000765 while( z[n] && (strncmp(z+n,"--end",5) || !ISSPACE(z[n+5])) ){
drh27338e62013-04-06 00:19:37 +0000766 n += tokenLength(z+n, pnLine);
767 }
768 return n;
769}
770
771/*
drh7dfe8e22013-04-08 13:13:43 +0000772** Find the number of characters up to the first character past the
773** of the next "--endif" or "--else" token. Nested --if commands are
774** also skipped.
775*/
776static int findEndif(const char *z, int stopAtElse, int *pnLine){
777 int n = 0;
778 while( z[n] ){
779 int len = tokenLength(z+n, pnLine);
drhc56fac72015-10-29 13:48:15 +0000780 if( (strncmp(z+n,"--endif",7)==0 && ISSPACE(z[n+7]))
781 || (stopAtElse && strncmp(z+n,"--else",6)==0 && ISSPACE(z[n+6]))
drh7dfe8e22013-04-08 13:13:43 +0000782 ){
783 return n+len;
784 }
drhc56fac72015-10-29 13:48:15 +0000785 if( strncmp(z+n,"--if",4)==0 && ISSPACE(z[n+4]) ){
drh7dfe8e22013-04-08 13:13:43 +0000786 int skip = findEndif(z+n+len, 0, pnLine);
787 n += skip + len;
788 }else{
789 n += len;
790 }
791 }
792 return n;
793}
794
795/*
drh27338e62013-04-06 00:19:37 +0000796** Wait for a client process to complete all its tasks
797*/
798static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){
799 sqlite3_stmt *pStmt;
800 int rc;
801 if( iClient>0 ){
802 pStmt = prepareSql(
drh3f5bc382013-04-06 13:09:11 +0000803 "SELECT 1 FROM task"
804 " WHERE client=%d"
805 " AND client IN (SELECT id FROM client)"
806 " AND endtime IS NULL",
drh27338e62013-04-06 00:19:37 +0000807 iClient);
808 }else{
809 pStmt = prepareSql(
drh3f5bc382013-04-06 13:09:11 +0000810 "SELECT 1 FROM task"
811 " WHERE client IN (SELECT id FROM client)"
812 " AND endtime IS NULL");
drh27338e62013-04-06 00:19:37 +0000813 }
drh3f5bc382013-04-06 13:09:11 +0000814 g.iTimeout = 0;
drh27338e62013-04-06 00:19:37 +0000815 while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW)
816 && iTimeout>0
817 ){
818 sqlite3_reset(pStmt);
819 sqlite3_sleep(50);
820 iTimeout -= 50;
821 }
822 sqlite3_finalize(pStmt);
drh3f5bc382013-04-06 13:09:11 +0000823 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +0000824 if( rc!=SQLITE_DONE ){
825 if( zErrPrefix==0 ) zErrPrefix = "";
826 if( iClient>0 ){
827 errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient);
828 }else{
829 errorMessage("%stimeout waiting for all clients", zErrPrefix);
830 }
831 }
832}
833
drh4c5298f2013-04-10 12:01:21 +0000834/* Return a pointer to the tail of a filename
835*/
836static char *filenameTail(char *z){
837 int i, j;
mistachkin25a72de2015-03-31 17:58:13 +0000838 for(i=j=0; z[i]; i++) if( isDirSep(z[i]) ) j = i+1;
drh4c5298f2013-04-10 12:01:21 +0000839 return z+j;
840}
841
drhbc082812013-04-18 15:11:03 +0000842/*
843** Interpret zArg as a boolean value. Return either 0 or 1.
844*/
845static int booleanValue(char *zArg){
846 int i;
847 if( zArg==0 ) return 0;
848 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
849 if( i>0 && zArg[i]==0 ) return atoi(zArg);
850 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
851 return 1;
852 }
853 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
854 return 0;
855 }
856 errorMessage("unknown boolean: [%s]", zArg);
857 return 0;
858}
859
860
861/* This routine exists as a convenient place to set a debugger
862** breakpoint.
863*/
864static void test_breakpoint(void){ static volatile int cnt = 0; cnt++; }
865
drh27338e62013-04-06 00:19:37 +0000866/* Maximum number of arguments to a --command */
drh7dfe8e22013-04-08 13:13:43 +0000867#define MX_ARG 2
drh27338e62013-04-06 00:19:37 +0000868
869/*
870** Run a script.
871*/
872static void runScript(
873 int iClient, /* The client number, or 0 for the master */
874 int taskId, /* The task ID for clients. 0 for master */
875 char *zScript, /* Text of the script */
876 char *zFilename /* File from which script was read. */
877){
878 int lineno = 1;
879 int prevLine = 1;
880 int ii = 0;
881 int iBegin = 0;
882 int n, c, j;
drh27338e62013-04-06 00:19:37 +0000883 int len;
884 int nArg;
885 String sResult;
886 char zCmd[30];
887 char zError[1000];
888 char azArg[MX_ARG][100];
drh27338e62013-04-06 00:19:37 +0000889
drh27338e62013-04-06 00:19:37 +0000890 memset(&sResult, 0, sizeof(sResult));
891 stringReset(&sResult);
892 while( (c = zScript[ii])!=0 ){
893 prevLine = lineno;
894 len = tokenLength(zScript+ii, &lineno);
drhc56fac72015-10-29 13:48:15 +0000895 if( ISSPACE(c) || (c=='/' && zScript[ii+1]=='*') ){
drh27338e62013-04-06 00:19:37 +0000896 ii += len;
897 continue;
898 }
899 if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){
900 ii += len;
901 continue;
902 }
903
904 /* Run any prior SQL before processing the new --command */
905 if( ii>iBegin ){
906 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
907 evalSql(&sResult, zSql);
908 sqlite3_free(zSql);
909 iBegin = ii + len;
910 }
911
912 /* Parse the --command */
913 if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii);
914 n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd));
915 for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){
drhc56fac72015-10-29 13:48:15 +0000916 while( n<len-2 && ISSPACE(zScript[ii+2+n]) ){ n++; }
drh27338e62013-04-06 00:19:37 +0000917 if( n>=len-2 ) break;
918 n += extractToken(zScript+ii+2+n, len-2-n,
919 azArg[nArg], sizeof(azArg[nArg]));
920 }
921 for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0;
922
923 /*
924 ** --sleep N
925 **
926 ** Pause for N milliseconds
927 */
928 if( strcmp(zCmd, "sleep")==0 ){
929 sqlite3_sleep(atoi(azArg[0]));
930 }else
931
932 /*
933 ** --exit N
934 **
935 ** Exit this process. If N>0 then exit without shutting down
936 ** SQLite. (In other words, simulate a crash.)
937 */
drh87f9caa2013-04-17 18:56:16 +0000938 if( strcmp(zCmd, "exit")==0 ){
drh27338e62013-04-06 00:19:37 +0000939 int rc = atoi(azArg[0]);
drh3f5bc382013-04-06 13:09:11 +0000940 finishScript(iClient, taskId, 1);
drh27338e62013-04-06 00:19:37 +0000941 if( rc==0 ) sqlite3_close(g.db);
942 exit(rc);
943 }else
944
945 /*
drh87f9caa2013-04-17 18:56:16 +0000946 ** --testcase NAME
947 **
drh93c8c452013-04-18 20:33:41 +0000948 ** Begin a new test case. Announce in the log that the test case
949 ** has begun.
drh87f9caa2013-04-17 18:56:16 +0000950 */
951 if( strcmp(zCmd, "testcase")==0 ){
952 if( g.iTrace==1 ) logMessage("%.*s", len - 1, zScript+ii);
953 stringReset(&sResult);
954 }else
955
956 /*
drh6adab7a2013-04-08 18:58:00 +0000957 ** --finish
958 **
959 ** Mark the current task as having finished, even if it is not.
960 ** This can be used in conjunction with --exit to simulate a crash.
961 */
962 if( strcmp(zCmd, "finish")==0 && iClient>0 ){
963 finishScript(iClient, taskId, 1);
964 }else
965
966 /*
drh7dfe8e22013-04-08 13:13:43 +0000967 ** --reset
drh27338e62013-04-06 00:19:37 +0000968 **
969 ** Reset accumulated results back to an empty string
970 */
971 if( strcmp(zCmd, "reset")==0 ){
972 stringReset(&sResult);
973 }else
974
975 /*
976 ** --match ANSWER...
977 **
978 ** Check to see if output matches ANSWER. Report an error if not.
979 */
980 if( strcmp(zCmd, "match")==0 ){
981 int jj;
982 char *zAns = zScript+ii;
drhc56fac72015-10-29 13:48:15 +0000983 for(jj=7; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
drh27338e62013-04-06 00:19:37 +0000984 zAns += jj;
drh44fddca2013-04-17 19:42:17 +0000985 if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){
drh27338e62013-04-06 00:19:37 +0000986 errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]",
987 prevLine, zFilename, len-jj-1, zAns, sResult.z);
988 }
drh3f5bc382013-04-06 13:09:11 +0000989 g.nTest++;
drh27338e62013-04-06 00:19:37 +0000990 stringReset(&sResult);
991 }else
992
993 /*
drh87f9caa2013-04-17 18:56:16 +0000994 ** --glob ANSWER...
995 ** --notglob ANSWER....
996 **
997 ** Check to see if output does or does not match the glob pattern
998 ** ANSWER.
999 */
1000 if( strcmp(zCmd, "glob")==0 || strcmp(zCmd, "notglob")==0 ){
1001 int jj;
1002 char *zAns = zScript+ii;
1003 char *zCopy;
1004 int isGlob = (zCmd[0]=='g');
drhc56fac72015-10-29 13:48:15 +00001005 for(jj=9-3*isGlob; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
drh87f9caa2013-04-17 18:56:16 +00001006 zAns += jj;
1007 zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns);
1008 if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){
1009 errorMessage("line %d of %s:\nExpected [%s]\n Got [%s]",
1010 prevLine, zFilename, zCopy, sResult.z);
1011 }
1012 sqlite3_free(zCopy);
1013 g.nTest++;
1014 stringReset(&sResult);
1015 }else
1016
1017 /*
drh1bf44c72013-04-08 13:48:29 +00001018 ** --output
1019 **
1020 ** Output the result of the previous SQL.
1021 */
1022 if( strcmp(zCmd, "output")==0 ){
1023 logMessage("%s", sResult.z);
1024 }else
1025
1026 /*
drh27338e62013-04-06 00:19:37 +00001027 ** --source FILENAME
1028 **
1029 ** Run a subscript from a separate file.
1030 */
1031 if( strcmp(zCmd, "source")==0 ){
drhe348fc72013-04-06 18:35:07 +00001032 char *zNewFile, *zNewScript;
1033 char *zToDel = 0;
1034 zNewFile = azArg[0];
mistachkin25a72de2015-03-31 17:58:13 +00001035 if( !isDirSep(zNewFile[0]) ){
drhe348fc72013-04-06 18:35:07 +00001036 int k;
mistachkin25a72de2015-03-31 17:58:13 +00001037 for(k=(int)strlen(zFilename)-1; k>=0 && !isDirSep(zFilename[k]); k--){}
drhe348fc72013-04-06 18:35:07 +00001038 if( k>0 ){
1039 zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile);
1040 }
1041 }
1042 zNewScript = readFile(zNewFile);
drh27338e62013-04-06 00:19:37 +00001043 if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile);
1044 runScript(0, 0, zNewScript, zNewFile);
1045 sqlite3_free(zNewScript);
1046 if( g.iTrace ) logMessage("end script [%s]\n", zNewFile);
drhbc94dbb2013-04-08 14:28:33 +00001047 sqlite3_free(zToDel);
drh27338e62013-04-06 00:19:37 +00001048 }else
1049
1050 /*
1051 ** --print MESSAGE....
1052 **
1053 ** Output the remainder of the line to the log file
1054 */
1055 if( strcmp(zCmd, "print")==0 ){
1056 int jj;
drhc56fac72015-10-29 13:48:15 +00001057 for(jj=7; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
drh27338e62013-04-06 00:19:37 +00001058 logMessage("%.*s", len-jj, zScript+ii+jj);
1059 }else
1060
1061 /*
drh7dfe8e22013-04-08 13:13:43 +00001062 ** --if EXPR
1063 **
1064 ** Skip forward to the next matching --endif or --else if EXPR is false.
1065 */
1066 if( strcmp(zCmd, "if")==0 ){
1067 int jj, rc;
1068 sqlite3_stmt *pStmt;
drhc56fac72015-10-29 13:48:15 +00001069 for(jj=4; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
drh7dfe8e22013-04-08 13:13:43 +00001070 pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj);
1071 rc = sqlite3_step(pStmt);
1072 if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){
1073 ii += findEndif(zScript+ii+len, 1, &lineno);
1074 }
1075 sqlite3_finalize(pStmt);
1076 }else
1077
1078 /*
1079 ** --else
1080 **
1081 ** This command can only be encountered if currently inside an --if that
1082 ** is true. Skip forward to the next matching --endif.
1083 */
1084 if( strcmp(zCmd, "else")==0 ){
1085 ii += findEndif(zScript+ii+len, 0, &lineno);
1086 }else
1087
1088 /*
1089 ** --endif
1090 **
1091 ** This command can only be encountered if currently inside an --if that
1092 ** is true or an --else of a false if. This is a no-op.
1093 */
1094 if( strcmp(zCmd, "endif")==0 ){
1095 /* no-op */
1096 }else
1097
1098 /*
drh27338e62013-04-06 00:19:37 +00001099 ** --start CLIENT
1100 **
1101 ** Start up the given client.
1102 */
drh7dfe8e22013-04-08 13:13:43 +00001103 if( strcmp(zCmd, "start")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001104 int iNewClient = atoi(azArg[0]);
drh3f5bc382013-04-06 13:09:11 +00001105 if( iNewClient>0 ){
1106 startClient(iNewClient);
drh27338e62013-04-06 00:19:37 +00001107 }
drh27338e62013-04-06 00:19:37 +00001108 }else
1109
1110 /*
1111 ** --wait CLIENT TIMEOUT
1112 **
1113 ** Wait until all tasks complete for the given client. If CLIENT is
1114 ** "all" then wait for all clients to complete. Wait no longer than
1115 ** TIMEOUT milliseconds (default 10,000)
1116 */
drh7dfe8e22013-04-08 13:13:43 +00001117 if( strcmp(zCmd, "wait")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001118 int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000;
1119 sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n",
1120 prevLine, zFilename);
1121 waitForClient(atoi(azArg[0]), iTimeout, zError);
1122 }else
1123
1124 /*
1125 ** --task CLIENT
1126 ** <task-content-here>
1127 ** --end
1128 **
drh3f5bc382013-04-06 13:09:11 +00001129 ** Assign work to a client. Start the client if it is not running
1130 ** already.
drh27338e62013-04-06 00:19:37 +00001131 */
drh7dfe8e22013-04-08 13:13:43 +00001132 if( strcmp(zCmd, "task")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001133 int iTarget = atoi(azArg[0]);
1134 int iEnd;
1135 char *zTask;
drh4c5298f2013-04-10 12:01:21 +00001136 char *zTName;
drh27338e62013-04-06 00:19:37 +00001137 iEnd = findEnd(zScript+ii+len, &lineno);
drh3f5bc382013-04-06 13:09:11 +00001138 if( iTarget<0 ){
1139 errorMessage("line %d of %s: bad client number: %d",
drh27338e62013-04-06 00:19:37 +00001140 prevLine, zFilename, iTarget);
drh3f5bc382013-04-06 13:09:11 +00001141 }else{
1142 zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len);
drh4c5298f2013-04-10 12:01:21 +00001143 if( nArg>1 ){
1144 zTName = sqlite3_mprintf("%s", azArg[1]);
1145 }else{
1146 zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine);
1147 }
drh3f5bc382013-04-06 13:09:11 +00001148 startClient(iTarget);
drh4c5298f2013-04-10 12:01:21 +00001149 runSql("INSERT INTO task(client,script,name)"
1150 " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName);
drh3f5bc382013-04-06 13:09:11 +00001151 sqlite3_free(zTask);
drh4c5298f2013-04-10 12:01:21 +00001152 sqlite3_free(zTName);
drh27338e62013-04-06 00:19:37 +00001153 }
drh27338e62013-04-06 00:19:37 +00001154 iEnd += tokenLength(zScript+ii+len+iEnd, &lineno);
1155 len += iEnd;
1156 iBegin = ii+len;
1157 }else
1158
drhbc082812013-04-18 15:11:03 +00001159 /*
1160 ** --breakpoint
1161 **
1162 ** This command calls "test_breakpoint()" which is a routine provided
1163 ** as a convenient place to set a debugger breakpoint.
1164 */
1165 if( strcmp(zCmd, "breakpoint")==0 ){
1166 test_breakpoint();
1167 }else
1168
1169 /*
1170 ** --show-sql-errors BOOLEAN
1171 **
1172 ** Turn display of SQL errors on and off.
1173 */
1174 if( strcmp(zCmd, "show-sql-errors")==0 ){
1175 g.bIgnoreSqlErrors = nArg>=1 ? !booleanValue(azArg[0]) : 1;
1176 }else
1177
1178
drh27338e62013-04-06 00:19:37 +00001179 /* error */{
1180 errorMessage("line %d of %s: unknown command --%s",
1181 prevLine, zFilename, zCmd);
1182 }
1183 ii += len;
1184 }
1185 if( iBegin<ii ){
1186 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
1187 runSql(zSql);
1188 sqlite3_free(zSql);
1189 }
1190 stringFree(&sResult);
1191}
1192
1193/*
1194** Look for a command-line option. If present, return a pointer.
1195** Return NULL if missing.
1196**
1197** hasArg==0 means the option is a flag. It is either present or not.
1198** hasArg==1 means the option has an argument. Return a pointer to the
1199** argument.
1200*/
1201static char *findOption(
1202 char **azArg,
1203 int *pnArg,
1204 const char *zOption,
1205 int hasArg
1206){
1207 int i, j;
1208 char *zReturn = 0;
1209 int nArg = *pnArg;
1210
1211 assert( hasArg==0 || hasArg==1 );
1212 for(i=0; i<nArg; i++){
1213 const char *z;
1214 if( i+hasArg >= nArg ) break;
1215 z = azArg[i];
1216 if( z[0]!='-' ) continue;
1217 z++;
1218 if( z[0]=='-' ){
1219 if( z[1]==0 ) break;
1220 z++;
1221 }
1222 if( strcmp(z,zOption)==0 ){
1223 if( hasArg && i==nArg-1 ){
1224 fatalError("command-line option \"--%s\" requires an argument", z);
1225 }
1226 if( hasArg ){
1227 zReturn = azArg[i+1];
1228 }else{
1229 zReturn = azArg[i];
1230 }
1231 j = i+1+(hasArg!=0);
1232 while( j<nArg ) azArg[i++] = azArg[j++];
1233 *pnArg = i;
1234 return zReturn;
1235 }
1236 }
1237 return zReturn;
1238}
1239
1240/* Print a usage message for the program and exit */
1241static void usage(const char *argv0){
1242 int i;
1243 const char *zTail = argv0;
1244 for(i=0; argv0[i]; i++){
mistachkin25a72de2015-03-31 17:58:13 +00001245 if( isDirSep(argv0[i]) ) zTail = argv0+i+1;
drh27338e62013-04-06 00:19:37 +00001246 }
1247 fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail);
drhcd423522016-02-12 17:27:32 +00001248 fprintf(stderr,
1249 "Options:\n"
1250 " --errlog FILENAME Write errors to FILENAME\n"
1251 " --journalmode MODE Use MODE as the journal_mode\n"
1252 " --log FILENAME Log messages to FILENAME\n"
1253 " --quiet Suppress unnecessary output\n"
1254 " --vfs NAME Use NAME as the VFS\n"
1255 " --repeat N Repeat the test N times\n"
1256 " --sqltrace Enable SQL tracing\n"
1257 " --sync Enable synchronous disk writes\n"
1258 " --timeout MILLISEC Busy timeout is MILLISEC\n"
1259 " --trace BOOLEAN Enable or disable tracing\n"
1260 );
drh27338e62013-04-06 00:19:37 +00001261 exit(1);
1262}
1263
1264/* Report on unrecognized arguments */
1265static void unrecognizedArguments(
1266 const char *argv0,
1267 int nArg,
1268 char **azArg
1269){
1270 int i;
1271 fprintf(stderr,"%s: unrecognized arguments:", argv0);
1272 for(i=0; i<nArg; i++){
1273 fprintf(stderr," %s", azArg[i]);
1274 }
1275 fprintf(stderr,"\n");
1276 exit(1);
1277}
1278
mistachkin44723ce2015-03-21 02:22:37 +00001279int SQLITE_CDECL main(int argc, char **argv){
drh27338e62013-04-06 00:19:37 +00001280 const char *zClient;
1281 int iClient;
drhe348fc72013-04-06 18:35:07 +00001282 int n, i;
drh27338e62013-04-06 00:19:37 +00001283 int openFlags = SQLITE_OPEN_READWRITE;
1284 int rc;
1285 char *zScript;
1286 int taskId;
1287 const char *zTrace;
drhe348fc72013-04-06 18:35:07 +00001288 const char *zCOption;
drhcc285c52015-03-11 14:34:38 +00001289 const char *zJMode;
1290 const char *zNRep;
1291 int nRep = 1, iRep;
drhcd423522016-02-12 17:27:32 +00001292 int iTmout = 0; /* Default: no timeout */
1293 const char *zTmout;
drh27338e62013-04-06 00:19:37 +00001294
1295 g.argv0 = argv[0];
1296 g.iTrace = 1;
1297 if( argc<2 ) usage(argv[0]);
1298 g.zDbFile = argv[1];
drhe348fc72013-04-06 18:35:07 +00001299 if( strglob("*.test", g.zDbFile) ) usage(argv[0]);
1300 if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){
1301 fprintf(stderr, "SQLite library and header mismatch\n"
1302 "Library: %s\n"
1303 "Header: %s\n",
1304 sqlite3_sourceid(), SQLITE_SOURCE_ID);
1305 exit(1);
1306 }
drh27338e62013-04-06 00:19:37 +00001307 n = argc-2;
drhe3be8c82013-04-11 11:53:45 +00001308 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID());
drhcc285c52015-03-11 14:34:38 +00001309 zJMode = findOption(argv+2, &n, "journalmode", 1);
1310 zNRep = findOption(argv+2, &n, "repeat", 1);
1311 if( zNRep ) nRep = atoi(zNRep);
1312 if( nRep<1 ) nRep = 1;
drh27338e62013-04-06 00:19:37 +00001313 g.zVfs = findOption(argv+2, &n, "vfs", 1);
1314 zClient = findOption(argv+2, &n, "client", 1);
1315 g.zErrLog = findOption(argv+2, &n, "errlog", 1);
1316 g.zLog = findOption(argv+2, &n, "log", 1);
1317 zTrace = findOption(argv+2, &n, "trace", 1);
1318 if( zTrace ) g.iTrace = atoi(zTrace);
1319 if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0;
drhcd423522016-02-12 17:27:32 +00001320 zTmout = findOption(argv+2, &n, "timeout", 1);
1321 if( zTmout ) iTmout = atoi(zTmout);
drh27338e62013-04-06 00:19:37 +00001322 g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0;
drhbc94dbb2013-04-08 14:28:33 +00001323 g.bSync = findOption(argv+2, &n, "sync", 0)!=0;
drh27338e62013-04-06 00:19:37 +00001324 if( g.zErrLog ){
1325 g.pErrLog = fopen(g.zErrLog, "a");
1326 }else{
1327 g.pErrLog = stderr;
1328 }
1329 if( g.zLog ){
1330 g.pLog = fopen(g.zLog, "a");
1331 }else{
1332 g.pLog = stdout;
1333 }
drhe3be8c82013-04-11 11:53:45 +00001334
drh1790bb32013-04-06 14:30:29 +00001335 sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0);
drh27338e62013-04-06 00:19:37 +00001336 if( zClient ){
1337 iClient = atoi(zClient);
1338 if( iClient<1 ) fatalError("illegal client number: %d\n", iClient);
drhe3be8c82013-04-11 11:53:45 +00001339 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d",
1340 GETPID(), iClient);
drh27338e62013-04-06 00:19:37 +00001341 }else{
drhcd423522016-02-12 17:27:32 +00001342 int nTry = 0;
drhe348fc72013-04-06 18:35:07 +00001343 if( g.iTrace>0 ){
drh8773b852015-03-31 14:18:29 +00001344 printf("BEGIN: %s", argv[0]);
1345 for(i=1; i<argc; i++) printf(" %s", argv[i]);
1346 printf("\n");
drhe348fc72013-04-06 18:35:07 +00001347 printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
1348 for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
1349 printf("-DSQLITE_%s\n", zCOption);
1350 }
1351 fflush(stdout);
1352 }
drh27338e62013-04-06 00:19:37 +00001353 iClient = 0;
drhcd423522016-02-12 17:27:32 +00001354 do{
1355 if( (nTry%5)==4 ) printf("... %strying to unlink '%s'\n",
1356 nTry>5 ? "still " : "", g.zDbFile);
1357 rc = unlink(g.zDbFile);
1358 if( rc && errno==ENOENT ) rc = 0;
1359 }while( rc!=0 && (++nTry)<60 && sqlite3_sleep(1000)>0 );
1360 if( rc!=0 ){
1361 fatalError("unable to unlink '%s' after %d attempts\n",
1362 g.zDbFile, nTry);
1363 }
drh27338e62013-04-06 00:19:37 +00001364 openFlags |= SQLITE_OPEN_CREATE;
1365 }
1366 rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
1367 if( rc ) fatalError("cannot open [%s]", g.zDbFile);
drhcd423522016-02-12 17:27:32 +00001368 if( iTmout>0 ) sqlite3_busy_timeout(g.db, iTmout);
1369
drh4c451962015-03-31 18:05:49 +00001370 if( zJMode ){
1371#if defined(_WIN32)
1372 if( sqlite3_stricmp(zJMode,"persist")==0
1373 || sqlite3_stricmp(zJMode,"truncate")==0
1374 ){
1375 printf("Changing journal mode to DELETE from %s", zJMode);
1376 zJMode = "DELETE";
1377 }
1378#endif
1379 runSql("PRAGMA journal_mode=%Q;", zJMode);
1380 }
drh2c32ed72015-03-31 18:18:53 +00001381 if( !g.bSync ) trySql("PRAGMA synchronous=OFF");
drh87f9caa2013-04-17 18:56:16 +00001382 sqlite3_enable_load_extension(g.db, 1);
drh3f5bc382013-04-06 13:09:11 +00001383 sqlite3_busy_handler(g.db, busyHandler, 0);
drh1bf44c72013-04-08 13:48:29 +00001384 sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
1385 vfsNameFunc, 0, 0);
1386 sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0,
1387 evalFunc, 0, 0);
drh3f5bc382013-04-06 13:09:11 +00001388 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +00001389 if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0);
1390 if( iClient>0 ){
1391 if( n>0 ) unrecognizedArguments(argv[0], n, argv+2);
1392 if( g.iTrace ) logMessage("start-client");
1393 while(1){
drh4c5298f2013-04-10 12:01:21 +00001394 char *zTaskName = 0;
1395 rc = startScript(iClient, &zScript, &taskId, &zTaskName);
drh27338e62013-04-06 00:19:37 +00001396 if( rc==SQLITE_DONE ) break;
drh4c5298f2013-04-10 12:01:21 +00001397 if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId);
drh27338e62013-04-06 00:19:37 +00001398 runScript(iClient, taskId, zScript, zTaskName);
drh4c5298f2013-04-10 12:01:21 +00001399 if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId);
drh3f5bc382013-04-06 13:09:11 +00001400 finishScript(iClient, taskId, 0);
drh4c5298f2013-04-10 12:01:21 +00001401 sqlite3_free(zTaskName);
drh27338e62013-04-06 00:19:37 +00001402 sqlite3_sleep(10);
1403 }
1404 if( g.iTrace ) logMessage("end-client");
1405 }else{
1406 sqlite3_stmt *pStmt;
drh3f5bc382013-04-06 13:09:11 +00001407 int iTimeout;
drh27338e62013-04-06 00:19:37 +00001408 if( n==0 ){
1409 fatalError("missing script filename");
1410 }
1411 if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
1412 runSql(
drhcc285c52015-03-11 14:34:38 +00001413 "DROP TABLE IF EXISTS task;\n"
1414 "DROP TABLE IF EXISTS counters;\n"
1415 "DROP TABLE IF EXISTS client;\n"
drh27338e62013-04-06 00:19:37 +00001416 "CREATE TABLE task(\n"
1417 " id INTEGER PRIMARY KEY,\n"
drh4c5298f2013-04-10 12:01:21 +00001418 " name TEXT,\n"
drh27338e62013-04-06 00:19:37 +00001419 " client INTEGER,\n"
1420 " starttime DATE,\n"
1421 " endtime DATE,\n"
1422 " script TEXT\n"
1423 ");"
drh3f5bc382013-04-06 13:09:11 +00001424 "CREATE INDEX task_i1 ON task(client, starttime);\n"
1425 "CREATE INDEX task_i2 ON task(client, endtime);\n"
1426 "CREATE TABLE counters(nError,nTest);\n"
1427 "INSERT INTO counters VALUES(0,0);\n"
1428 "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n"
drh27338e62013-04-06 00:19:37 +00001429 );
1430 zScript = readFile(argv[2]);
drhcc285c52015-03-11 14:34:38 +00001431 for(iRep=1; iRep<=nRep; iRep++){
1432 if( g.iTrace ) logMessage("begin script [%s] cycle %d\n", argv[2], iRep);
1433 runScript(0, 0, zScript, argv[2]);
1434 if( g.iTrace ) logMessage("end script [%s] cycle %d\n", argv[2], iRep);
1435 }
drh27338e62013-04-06 00:19:37 +00001436 sqlite3_free(zScript);
drh27338e62013-04-06 00:19:37 +00001437 waitForClient(0, 2000, "during shutdown...\n");
drh3f5bc382013-04-06 13:09:11 +00001438 trySql("UPDATE client SET wantHalt=1");
1439 sqlite3_sleep(10);
1440 g.iTimeout = 0;
1441 iTimeout = 1000;
1442 while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1443 || rc==SQLITE_ROW) && iTimeout>0 ){
drh27338e62013-04-06 00:19:37 +00001444 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +00001445 iTimeout -= 10;
drh27338e62013-04-06 00:19:37 +00001446 }
1447 sqlite3_sleep(100);
drh3f5bc382013-04-06 13:09:11 +00001448 pStmt = prepareSql("SELECT nError, nTest FROM counters");
1449 iTimeout = 1000;
1450 while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){
drh27338e62013-04-06 00:19:37 +00001451 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +00001452 iTimeout -= 10;
drh27338e62013-04-06 00:19:37 +00001453 }
1454 if( rc==SQLITE_ROW ){
1455 g.nError += sqlite3_column_int(pStmt, 0);
drh3f5bc382013-04-06 13:09:11 +00001456 g.nTest += sqlite3_column_int(pStmt, 1);
drh27338e62013-04-06 00:19:37 +00001457 }
1458 sqlite3_finalize(pStmt);
1459 }
drhcc285c52015-03-11 14:34:38 +00001460 sqlite3_close(g.db);
drh27338e62013-04-06 00:19:37 +00001461 maybeClose(g.pLog);
1462 maybeClose(g.pErrLog);
1463 if( iClient==0 ){
drhbd41d562014-12-30 20:40:32 +00001464 printf("Summary: %d errors out of %d tests\n", g.nError, g.nTest);
drh8773b852015-03-31 14:18:29 +00001465 printf("END: %s", argv[0]);
1466 for(i=1; i<argc; i++) printf(" %s", argv[i]);
1467 printf("\n");
drh27338e62013-04-06 00:19:37 +00001468 }
1469 return g.nError>0;
1470}