/ Check-in [edea3bb7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Continuing improvements to error reporting and the sqlite3_log() routine.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:edea3bb740ddd096a46e00678b59d465bb1e2903
User & Date: drh 2010-02-23 20:11:57
References
2010-02-25
02:32
Merge in all of the logging enhancements. This is a cherrypick merge of the following check-ins: [103321e37a], [a8076aede3], [6d910245ad], [7c4cca6d1a], [edea3bb740], [1a6d4bb130], [a8c984c1d6], [69a493182f], and [1168763d2c]. check-in: 46f406b2 user: drh tags: branch-3.6.22
Context
2010-02-23
20:32
Merge the compile-time option introspection interfaces into the trunk. check-in: 9f429434 user: drh tags: trunk
20:11
Continuing improvements to error reporting and the sqlite3_log() routine. check-in: edea3bb7 user: drh tags: trunk
18:05
Fix a segfault that can occur if a malloc fails in ATTACH in shared-cache mode. check-in: e3713737 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/legacy.c.

    37     37     int rc = SQLITE_OK;         /* Return code */
    38     38     const char *zLeftover;      /* Tail of unprocessed SQL */
    39     39     sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
    40     40     char **azCols = 0;          /* Names of result columns */
    41     41     int nRetry = 0;             /* Number of retry attempts */
    42     42     int callbackIsInit;         /* True if callback data is initialized */
    43     43   
    44         -  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE;
           44  +  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
    45     45     if( zSql==0 ) zSql = "";
    46     46   
    47     47     sqlite3_mutex_enter(db->mutex);
    48     48     sqlite3Error(db, SQLITE_OK, 0);
    49     49     while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
    50     50       int nCol;
    51     51       char **azVals = 0;

Changes to src/main.c.

   253    253   */
   254    254   int sqlite3_config(int op, ...){
   255    255     va_list ap;
   256    256     int rc = SQLITE_OK;
   257    257   
   258    258     /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
   259    259     ** the SQLite library is in use. */
   260         -  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
          260  +  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
   261    261   
   262    262     va_start(ap, op);
   263    263     switch( op ){
   264    264   
   265    265       /* Mutex configuration options are only available in a threadsafe
   266    266       ** compile. 
   267    267       */
................................................................................
   597    597     HashElem *i;
   598    598     int j;
   599    599   
   600    600     if( !db ){
   601    601       return SQLITE_OK;
   602    602     }
   603    603     if( !sqlite3SafetyCheckSickOrOk(db) ){
   604         -    return SQLITE_MISUSE;
          604  +    return SQLITE_MISUSE_BKPT;
   605    605     }
   606    606     sqlite3_mutex_enter(db->mutex);
   607    607   
   608    608     sqlite3ResetInternalSchema(db, 0);
   609    609   
   610    610     /* If a transaction is open, the ResetInternalSchema() call above
   611    611     ** will not have called the xDisconnect() method on any virtual
................................................................................
   944    944     assert( sqlite3_mutex_held(db->mutex) );
   945    945     if( zFunctionName==0 ||
   946    946         (xFunc && (xFinal || xStep)) || 
   947    947         (!xFunc && (xFinal && !xStep)) ||
   948    948         (!xFunc && (!xFinal && xStep)) ||
   949    949         (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
   950    950         (255<(nName = sqlite3Strlen30( zFunctionName))) ){
   951         -    return SQLITE_MISUSE;
          951  +    return SQLITE_MISUSE_BKPT;
   952    952     }
   953    953     
   954    954   #ifndef SQLITE_OMIT_UTF16
   955    955     /* If SQLITE_UTF16 is specified as the encoding type, transform this
   956    956     ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   957    957     ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
   958    958     **
................................................................................
  1275   1275   */
  1276   1276   const char *sqlite3_errmsg(sqlite3 *db){
  1277   1277     const char *z;
  1278   1278     if( !db ){
  1279   1279       return sqlite3ErrStr(SQLITE_NOMEM);
  1280   1280     }
  1281   1281     if( !sqlite3SafetyCheckSickOrOk(db) ){
  1282         -    return sqlite3ErrStr(SQLITE_MISUSE);
         1282  +    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
  1283   1283     }
  1284   1284     sqlite3_mutex_enter(db->mutex);
  1285   1285     if( db->mallocFailed ){
  1286   1286       z = sqlite3ErrStr(SQLITE_NOMEM);
  1287   1287     }else{
  1288   1288       z = (char*)sqlite3_value_text(db->pErr);
  1289   1289       assert( !db->mallocFailed );
................................................................................
  1344   1344   
  1345   1345   /*
  1346   1346   ** Return the most recent error code generated by an SQLite routine. If NULL is
  1347   1347   ** passed to this function, we assume a malloc() failed during sqlite3_open().
  1348   1348   */
  1349   1349   int sqlite3_errcode(sqlite3 *db){
  1350   1350     if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  1351         -    return SQLITE_MISUSE;
         1351  +    return SQLITE_MISUSE_BKPT;
  1352   1352     }
  1353   1353     if( !db || db->mallocFailed ){
  1354   1354       return SQLITE_NOMEM;
  1355   1355     }
  1356   1356     return db->errCode & db->errMask;
  1357   1357   }
  1358   1358   int sqlite3_extended_errcode(sqlite3 *db){
  1359   1359     if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  1360         -    return SQLITE_MISUSE;
         1360  +    return SQLITE_MISUSE_BKPT;
  1361   1361     }
  1362   1362     if( !db || db->mallocFailed ){
  1363   1363       return SQLITE_NOMEM;
  1364   1364     }
  1365   1365     return db->errCode;
  1366   1366   }
  1367   1367   
................................................................................
  1391   1391     enc2 = enc;
  1392   1392     testcase( enc2==SQLITE_UTF16 );
  1393   1393     testcase( enc2==SQLITE_UTF16_ALIGNED );
  1394   1394     if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
  1395   1395       enc2 = SQLITE_UTF16NATIVE;
  1396   1396     }
  1397   1397     if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
  1398         -    return SQLITE_MISUSE;
         1398  +    return SQLITE_MISUSE_BKPT;
  1399   1399     }
  1400   1400   
  1401   1401     /* Check if this call is removing or replacing an existing collation 
  1402   1402     ** sequence. If so, and there are active VMs, return busy. If there
  1403   1403     ** are no active VMs, invalidate any pre-compiled statements.
  1404   1404     */
  1405   1405     pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);

Changes to src/mem1.c.

    38     38     sqlite3_int64 *p;
    39     39     assert( nByte>0 );
    40     40     nByte = ROUND8(nByte);
    41     41     p = malloc( nByte+8 );
    42     42     if( p ){
    43     43       p[0] = nByte;
    44     44       p++;
           45  +  }else{
           46  +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
    45     47     }
    46     48     return (void *)p;
    47     49   }
    48     50   
    49     51   /*
    50     52   ** Like free() but works for allocations obtained from sqlite3MemMalloc()
    51     53   ** or sqlite3MemRealloc().
................................................................................
    56     58   */
    57     59   static void sqlite3MemFree(void *pPrior){
    58     60     sqlite3_int64 *p = (sqlite3_int64*)pPrior;
    59     61     assert( pPrior!=0 );
    60     62     p--;
    61     63     free(p);
    62     64   }
           65  +
           66  +/*
           67  +** Report the allocated size of a prior return from xMalloc()
           68  +** or xRealloc().
           69  +*/
           70  +static int sqlite3MemSize(void *pPrior){
           71  +  sqlite3_int64 *p;
           72  +  if( pPrior==0 ) return 0;
           73  +  p = (sqlite3_int64*)pPrior;
           74  +  p--;
           75  +  return (int)p[0];
           76  +}
    63     77   
    64     78   /*
    65     79   ** Like realloc().  Resize an allocation previously obtained from
    66     80   ** sqlite3MemMalloc().
    67     81   **
    68     82   ** For this low-level interface, we know that pPrior!=0.  Cases where
    69     83   ** pPrior==0 while have been intercepted by higher-level routine and
................................................................................
    76     90     assert( pPrior!=0 && nByte>0 );
    77     91     nByte = ROUND8(nByte);
    78     92     p--;
    79     93     p = realloc(p, nByte+8 );
    80     94     if( p ){
    81     95       p[0] = nByte;
    82     96       p++;
           97  +  }else{
           98  +    sqlite3_log(SQLITE_NOMEM,
           99  +      "failed memory resize %u to %u bytes",
          100  +      sqlite3MemSize(pPrior), nByte);
    83    101     }
    84    102     return (void*)p;
    85    103   }
    86    104   
    87         -/*
    88         -** Report the allocated size of a prior return from xMalloc()
    89         -** or xRealloc().
    90         -*/
    91         -static int sqlite3MemSize(void *pPrior){
    92         -  sqlite3_int64 *p;
    93         -  if( pPrior==0 ) return 0;
    94         -  p = (sqlite3_int64*)pPrior;
    95         -  p--;
    96         -  return (int)p[0];
    97         -}
    98         -
    99    105   /*
   100    106   ** Round up a request size to the next valid allocation size.
   101    107   */
   102    108   static int sqlite3MemRoundup(int n){
   103    109     return ROUND8(n);
   104    110   }
   105    111   

Changes to src/mem5.c.

   264    264     for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
   265    265   
   266    266     /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
   267    267     ** block.  If not, then split a block of the next larger power of
   268    268     ** two in order to create a new free block of size iLogsize.
   269    269     */
   270    270     for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
   271         -  if( iBin>LOGMAX ) return 0;
          271  +  if( iBin>LOGMAX ){
          272  +    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
          273  +    return 0;
          274  +  }
   272    275     i = memsys5UnlinkFirst(iBin);
   273    276     while( iBin>iLogsize ){
   274    277       int newSize;
   275    278   
   276    279       iBin--;
   277    280       newSize = 1 << iBin;
   278    281       mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;

Changes to src/os_unix.c.

  1107   1107     if( pthread_equal(pFile->tid, hSelf) ){
  1108   1108       /* We are still in the same thread */
  1109   1109       OSTRACE1("No-transfer, same thread\n");
  1110   1110       return SQLITE_OK;
  1111   1111     }
  1112   1112     if( pFile->locktype!=NO_LOCK ){
  1113   1113       /* We cannot change ownership while we are holding a lock! */
  1114         -    return SQLITE_MISUSE;
         1114  +    return SQLITE_MISUSE_BKPT;
  1115   1115     }
  1116   1116     OSTRACE4("Transfer ownership of %d from %d to %d\n",
  1117   1117               pFile->h, pFile->tid, hSelf);
  1118   1118     pFile->tid = hSelf;
  1119   1119     if (pFile->pLock != NULL) {
  1120   1120       releaseLockInfo(pFile->pLock);
  1121   1121       rc = findLockInfo(pFile, &pFile->pLock, 0);
................................................................................
  1508   1508         pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1509   1509   
  1510   1510     assert( locktype<=SHARED_LOCK );
  1511   1511     if( pFile->locktype<=locktype ){
  1512   1512       return SQLITE_OK;
  1513   1513     }
  1514   1514     if( CHECK_THREADID(pFile) ){
  1515         -    return SQLITE_MISUSE;
         1515  +    return SQLITE_MISUSE_BKPT;
  1516   1516     }
  1517   1517     unixEnterMutex();
  1518   1518     h = pFile->h;
  1519   1519     pLock = pFile->pLock;
  1520   1520     assert( pLock->cnt!=0 );
  1521   1521     if( pFile->locktype>SHARED_LOCK ){
  1522   1522       assert( pLock->locktype==pFile->locktype );
................................................................................
  2731   2731              pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  2732   2732   
  2733   2733     assert( locktype<=SHARED_LOCK );
  2734   2734     if( pFile->locktype<=locktype ){
  2735   2735       return SQLITE_OK;
  2736   2736     }
  2737   2737     if( CHECK_THREADID(pFile) ){
  2738         -    return SQLITE_MISUSE;
         2738  +    return SQLITE_MISUSE_BKPT;
  2739   2739     }
  2740   2740     unixEnterMutex();
  2741   2741     pLock = pFile->pLock;
  2742   2742     assert( pLock->cnt!=0 );
  2743   2743     if( pFile->locktype>SHARED_LOCK ){
  2744   2744       assert( pLock->locktype==pFile->locktype );
  2745   2745       SimulateIOErrorBenign(1);

Changes to src/prepare.c.

   685    685     sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   686    686     const char **pzTail       /* OUT: End of parsed string */
   687    687   ){
   688    688     int rc;
   689    689     assert( ppStmt!=0 );
   690    690     *ppStmt = 0;
   691    691     if( !sqlite3SafetyCheckOk(db) ){
   692         -    return SQLITE_MISUSE;
          692  +    return SQLITE_MISUSE_BKPT;
   693    693     }
   694    694     sqlite3_mutex_enter(db->mutex);
   695    695     sqlite3BtreeEnterAll(db);
   696    696     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
   697    697     if( rc==SQLITE_SCHEMA ){
   698    698       sqlite3_finalize(*ppStmt);
   699    699       rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
................................................................................
   793    793     char *zSql8;
   794    794     const char *zTail8 = 0;
   795    795     int rc = SQLITE_OK;
   796    796   
   797    797     assert( ppStmt );
   798    798     *ppStmt = 0;
   799    799     if( !sqlite3SafetyCheckOk(db) ){
   800         -    return SQLITE_MISUSE;
          800  +    return SQLITE_MISUSE_BKPT;
   801    801     }
   802    802     sqlite3_mutex_enter(db->mutex);
   803    803     zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
   804    804     if( zSql8 ){
   805    805       rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
   806    806     }
   807    807   

Changes to src/status.c.

    79     79   ** This implementation assumes that reading or writing an aligned
    80     80   ** 32-bit integer is an atomic operation.  If that assumption is not true,
    81     81   ** then this routine is not threadsafe.
    82     82   */
    83     83   int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
    84     84     wsdStatInit;
    85     85     if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
    86         -    return SQLITE_MISUSE;
           86  +    return SQLITE_MISUSE_BKPT;
    87     87     }
    88     88     *pCurrent = wsdStat.nowValue[op];
    89     89     *pHighwater = wsdStat.mxValue[op];
    90     90     if( resetFlag ){
    91     91       wsdStat.mxValue[op] = wsdStat.nowValue[op];
    92     92     }
    93     93     return SQLITE_OK;

Changes to src/tokenize.c.

   476    476     }
   477    477     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
   478    478       sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
   479    479     }
   480    480     assert( pzErrMsg!=0 );
   481    481     if( pParse->zErrMsg ){
   482    482       *pzErrMsg = pParse->zErrMsg;
          483  +    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
   483    484       pParse->zErrMsg = 0;
   484    485       nErr++;
   485    486     }
   486    487     if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
   487    488       sqlite3VdbeDelete(pParse->pVdbe);
   488    489       pParse->pVdbe = 0;
   489    490     }

Changes to src/util.c.

   157    157     if( db->suppressErr ){
   158    158       sqlite3DbFree(db, zMsg);
   159    159     }else{
   160    160       pParse->nErr++;
   161    161       sqlite3DbFree(db, pParse->zErrMsg);
   162    162       pParse->zErrMsg = zMsg;
   163    163       pParse->rc = SQLITE_ERROR;
   164         -    sqlite3_log(SQLITE_ERROR, pParse->zErrMsg);
   165    164     }
   166    165   }
   167    166   
   168    167   /*
   169    168   ** Convert an SQL-style quoted string into a normal string by removing
   170    169   ** the quote characters.  The conversion is done in-place.  If the
   171    170   ** input does not begin with a quote character, then this routine
................................................................................
  1006   1005       }
  1007   1006       zBlob[i/2] = 0;
  1008   1007     }
  1009   1008     return zBlob;
  1010   1009   }
  1011   1010   #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
  1012   1011   
         1012  +/*
         1013  +** Log an error that is an API call on a connection pointer that should
         1014  +** not have been used.  The "type" of connection pointer is given as the
         1015  +** argument.  The zType is a word like "NULL" or "closed" or "invalid".
         1016  +*/
         1017  +static void logBadConnection(const char *zType){
         1018  +  sqlite3_log(SQLITE_MISUSE, 
         1019  +     "API call with %s database connection pointer",
         1020  +     zType
         1021  +  );
         1022  +}
  1013   1023   
  1014   1024   /*
  1015   1025   ** Check to make sure we have a valid db pointer.  This test is not
  1016   1026   ** foolproof but it does provide some measure of protection against
  1017   1027   ** misuse of the interface such as passing in db pointers that are
  1018   1028   ** NULL or which have been previously closed.  If this routine returns
  1019   1029   ** 1 it means that the db pointer is valid and 0 if it should not be
................................................................................
  1023   1033   ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
  1024   1034   ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
  1025   1035   ** open properly and is not fit for general use but which can be
  1026   1036   ** used as an argument to sqlite3_errmsg() or sqlite3_close().
  1027   1037   */
  1028   1038   int sqlite3SafetyCheckOk(sqlite3 *db){
  1029   1039     u32 magic;
  1030         -  if( db==0 ) return 0;
         1040  +  if( db==0 ){
         1041  +    logBadConnection("NULL");
         1042  +    return 0;
         1043  +  }
  1031   1044     magic = db->magic;
  1032   1045     if( magic!=SQLITE_MAGIC_OPEN ){
         1046  +    if( !sqlite3SafetyCheckSickOrOk(db) ){
         1047  +      logBadConnection("unopened");
         1048  +    }
  1033   1049       return 0;
  1034   1050     }else{
  1035   1051       return 1;
  1036   1052     }
  1037   1053   }
  1038   1054   int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
  1039   1055     u32 magic;
  1040   1056     magic = db->magic;
  1041   1057     if( magic!=SQLITE_MAGIC_SICK &&
  1042   1058         magic!=SQLITE_MAGIC_OPEN &&
  1043         -      magic!=SQLITE_MAGIC_BUSY ) return 0;
  1044         -  return 1;
         1059  +      magic!=SQLITE_MAGIC_BUSY ){
         1060  +    logBadConnection( magic==SQLITE_MAGIC_CLOSED ? "closed" : "invalid" );
         1061  +    return 0;
         1062  +  }else{
         1063  +    return 1;
         1064  +  }
  1045   1065   }

Changes to src/vdbe.c.

   843    843       break;
   844    844     }
   845    845   
   846    846     p->rc = pOp->p1;
   847    847     p->errorAction = (u8)pOp->p2;
   848    848     p->pc = pc;
   849    849     if( pOp->p4.z ){
          850  +    assert( p->rc!=SQLITE_OK );
   850    851       sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
          852  +    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
   851    853     }
   852    854     rc = sqlite3VdbeHalt(p);
   853    855     assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
   854    856     if( rc==SQLITE_BUSY ){
   855    857       p->rc = rc = SQLITE_BUSY;
   856    858     }else{
   857    859       assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
................................................................................
  5693   5695   
  5694   5696     /* If we reach this point, it means that execution is finished with
  5695   5697     ** an error of some kind.
  5696   5698     */
  5697   5699   vdbe_error_halt:
  5698   5700     assert( rc );
  5699   5701     p->rc = rc;
         5702  +  sqlite3_log(rc, "prepared statement aborts at %d: [%s]", pc, p->zSql);
  5700   5703     sqlite3VdbeHalt(p);
  5701   5704     if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
  5702   5705     rc = SQLITE_ERROR;
  5703   5706     if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
  5704   5707   
  5705   5708     /* This is the only way out of this procedure.  We have to
  5706   5709     ** release the mutexes on btrees that were acquired at the
................................................................................
  5721   5724     */
  5722   5725   no_mem:
  5723   5726     db->mallocFailed = 1;
  5724   5727     sqlite3SetString(&p->zErrMsg, db, "out of memory");
  5725   5728     rc = SQLITE_NOMEM;
  5726   5729     goto vdbe_error_halt;
  5727   5730   
  5728         -  /* Jump to here for an SQLITE_MISUSE error.
  5729         -  */
  5730         -abort_due_to_misuse:
  5731         -  rc = SQLITE_MISUSE;
  5732         -  /* Fall thru into abort_due_to_error */
  5733         -
  5734   5731     /* Jump to here for any other kind of fatal error.  The "rc" variable
  5735   5732     ** should hold the error number.
  5736   5733     */
  5737   5734   abort_due_to_error:
  5738   5735     assert( p->zErrMsg==0 );
  5739   5736     if( db->mallocFailed ) rc = SQLITE_NOMEM;
  5740   5737     if( rc!=SQLITE_IOERR_NOMEM ){

Changes to src/vdbeapi.c.

    26     26   ** added or changed.
    27     27   */
    28     28   int sqlite3_expired(sqlite3_stmt *pStmt){
    29     29     Vdbe *p = (Vdbe*)pStmt;
    30     30     return p==0 || p->expired;
    31     31   }
    32     32   #endif
           33  +
           34  +/*
           35  +** Check on a Vdbe to make sure it has not been finalized.  Log
           36  +** an error and return true if it has been finalized (or is otherwise
           37  +** invalid).  Return false if it is ok.
           38  +*/
           39  +static int vdbeSafety(Vdbe *p){
           40  +  if( p->db==0 ){
           41  +    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
           42  +    return 1;
           43  +  }else{
           44  +    return 0;
           45  +  }
           46  +}
           47  +static int vdbeSafetyNotNull(Vdbe *p){
           48  +  if( p==0 ){
           49  +    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
           50  +    return 1;
           51  +  }else{
           52  +    return vdbeSafety(p);
           53  +  }
           54  +}
    33     55   
    34     56   /*
    35     57   ** The following routine destroys a virtual machine that is created by
    36     58   ** the sqlite3_compile() routine. The integer returned is an SQLITE_
    37     59   ** success/failure code that describes the result of executing the virtual
    38     60   ** machine.
    39     61   **
................................................................................
    46     68       rc = SQLITE_OK;
    47     69     }else{
    48     70       Vdbe *v = (Vdbe*)pStmt;
    49     71       sqlite3 *db = v->db;
    50     72   #if SQLITE_THREADSAFE
    51     73       sqlite3_mutex *mutex;
    52     74   #endif
    53         -    if( db==0 ) return SQLITE_MISUSE;
           75  +    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
    54     76   #if SQLITE_THREADSAFE
    55     77       mutex = v->db->mutex;
    56     78   #endif
    57     79       sqlite3_mutex_enter(mutex);
    58     80       rc = sqlite3VdbeFinalize(v);
    59     81       rc = sqlite3ApiExit(db, rc);
    60     82       sqlite3_mutex_leave(mutex);
................................................................................
   295    317   */
   296    318   static int sqlite3Step(Vdbe *p){
   297    319     sqlite3 *db;
   298    320     int rc;
   299    321   
   300    322     assert(p);
   301    323     if( p->magic!=VDBE_MAGIC_RUN ){
   302         -    return SQLITE_MISUSE;
          324  +    sqlite3_log(SQLITE_MISUSE, 
          325  +          "attempt to step a halted statement: [%s]", p->zSql);
          326  +    return SQLITE_MISUSE_BKPT;
   303    327     }
   304    328   
   305    329     /* Check that malloc() has not failed. If it has, return early. */
   306    330     db = p->db;
   307    331     if( db->mallocFailed ){
   308    332       p->rc = SQLITE_NOMEM;
   309    333       return SQLITE_NOMEM;
................................................................................
   390    414   
   391    415   /*
   392    416   ** This is the top-level implementation of sqlite3_step().  Call
   393    417   ** sqlite3Step() to do most of the work.  If a schema error occurs,
   394    418   ** call sqlite3Reprepare() and try again.
   395    419   */
   396    420   int sqlite3_step(sqlite3_stmt *pStmt){
   397         -  int rc = SQLITE_MISUSE;
          421  +  int rc = SQLITE_OK;
   398    422     Vdbe *v = (Vdbe*)pStmt;
   399         -  if( v && (v->db)!=0 ){
   400         -    int cnt = 0;
   401         -    sqlite3 *db = v->db;
   402         -    sqlite3_mutex_enter(db->mutex);
   403         -    while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   404         -           && cnt++ < 5
   405         -           && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
   406         -      sqlite3_reset(pStmt);
   407         -      v->expired = 0;
   408         -    }
   409         -    if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
   410         -      /* This case occurs after failing to recompile an sql statement. 
   411         -      ** The error message from the SQL compiler has already been loaded 
   412         -      ** into the database handle. This block copies the error message 
   413         -      ** from the database handle into the statement and sets the statement
   414         -      ** program counter to 0 to ensure that when the statement is 
   415         -      ** finalized or reset the parser error message is available via
   416         -      ** sqlite3_errmsg() and sqlite3_errcode().
   417         -      */
   418         -      const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
   419         -      sqlite3DbFree(db, v->zErrMsg);
   420         -      if( !db->mallocFailed ){
   421         -        v->zErrMsg = sqlite3DbStrDup(db, zErr);
   422         -      } else {
   423         -        v->zErrMsg = 0;
   424         -        v->rc = SQLITE_NOMEM;
   425         -      }
   426         -    }
   427         -    rc = sqlite3ApiExit(db, rc);
   428         -    sqlite3_mutex_leave(db->mutex);
   429         -  }
          423  +  int cnt = 0;
          424  +  sqlite3 *db;
          425  +  if( vdbeSafetyNotNull(v) ){
          426  +    return SQLITE_MISUSE_BKPT;
          427  +  }
          428  +  db = v->db;
          429  +  sqlite3_mutex_enter(db->mutex);
          430  +  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
          431  +         && cnt++ < 5
          432  +         && (rc = sqlite3Reprepare(v))==SQLITE_OK ){
          433  +    sqlite3_reset(pStmt);
          434  +    v->expired = 0;
          435  +  }
          436  +  if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
          437  +    /* This case occurs after failing to recompile an sql statement. 
          438  +    ** The error message from the SQL compiler has already been loaded 
          439  +    ** into the database handle. This block copies the error message 
          440  +    ** from the database handle into the statement and sets the statement
          441  +    ** program counter to 0 to ensure that when the statement is 
          442  +    ** finalized or reset the parser error message is available via
          443  +    ** sqlite3_errmsg() and sqlite3_errcode().
          444  +    */
          445  +    const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
          446  +    sqlite3DbFree(db, v->zErrMsg);
          447  +    if( !db->mallocFailed ){
          448  +      v->zErrMsg = sqlite3DbStrDup(db, zErr);
          449  +    } else {
          450  +      v->zErrMsg = 0;
          451  +      v->rc = SQLITE_NOMEM;
          452  +    }
          453  +  }
          454  +  rc = sqlite3ApiExit(db, rc);
          455  +  sqlite3_mutex_leave(db->mutex);
   430    456     return rc;
   431    457   }
   432    458   
   433    459   /*
   434    460   ** Extract the user data from a sqlite3_context structure and return a
   435    461   ** pointer to it.
   436    462   */
................................................................................
   892    918   ** the mutex is released if any kind of error occurs.
   893    919   **
   894    920   ** The error code stored in database p->db is overwritten with the return
   895    921   ** value in any case.
   896    922   */
   897    923   static int vdbeUnbind(Vdbe *p, int i){
   898    924     Mem *pVar;
   899         -  if( p==0 ) return SQLITE_MISUSE;
          925  +  if( vdbeSafetyNotNull(p) ){
          926  +    return SQLITE_MISUSE_BKPT;
          927  +  }
   900    928     sqlite3_mutex_enter(p->db->mutex);
   901    929     if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
   902    930       sqlite3Error(p->db, SQLITE_MISUSE, 0);
   903    931       sqlite3_mutex_leave(p->db->mutex);
   904         -    return SQLITE_MISUSE;
          932  +    sqlite3_log(SQLITE_MISUSE, 
          933  +        "bind on a busy prepared statement: [%s]", p->zSql);
          934  +    return SQLITE_MISUSE_BKPT;
   905    935     }
   906    936     if( i<1 || i>p->nVar ){
   907    937       sqlite3Error(p->db, SQLITE_RANGE, 0);
   908    938       sqlite3_mutex_leave(p->db->mutex);
   909    939       return SQLITE_RANGE;
   910    940     }
   911    941     i--;

Changes to src/vdbeblob.c.

   314    314     int (*xCall)(BtCursor*, u32, u32, void*)
   315    315   ){
   316    316     int rc;
   317    317     Incrblob *p = (Incrblob *)pBlob;
   318    318     Vdbe *v;
   319    319     sqlite3 *db;
   320    320   
   321         -  if( p==0 ) return SQLITE_MISUSE;
          321  +  if( p==0 ) return SQLITE_MISUSE_BKPT;
   322    322     db = p->db;
   323    323     sqlite3_mutex_enter(db->mutex);
   324    324     v = (Vdbe*)p->pStmt;
   325    325   
   326    326     if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
   327    327       /* Request is out of range. Return a transient error. */
   328    328       rc = SQLITE_ERROR;

Changes to src/vtab.c.

   643    643     char *zErr = 0;
   644    644   
   645    645     sqlite3_mutex_enter(db->mutex);
   646    646     pTab = db->pVTab;
   647    647     if( !pTab ){
   648    648       sqlite3Error(db, SQLITE_MISUSE, 0);
   649    649       sqlite3_mutex_leave(db->mutex);
   650         -    return SQLITE_MISUSE;
          650  +    return SQLITE_MISUSE_BKPT;
   651    651     }
   652    652     assert( (pTab->tabFlags & TF_Virtual)!=0 );
   653    653   
   654    654     pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   655    655     if( pParse==0 ){
   656    656       rc = SQLITE_NOMEM;
   657    657     }else{