From: drh Date: Sat, 29 Feb 2020 15:53:48 +0000 (+0000) Subject: In the CLI, add the ".oom" command for debugging builds. X-Git-Tag: version-3.32.0~140 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4a3a3eb3d75af89b9da530f63ee31c3b2e56b189;p=thirdparty%2Fsqlite.git In the CLI, add the ".oom" command for debugging builds. FossilOrigin-Name: 9c3136a722715952d155aae55cbc6d1fb921c6940d8e7d3e32fcba000f6ac1ed --- diff --git a/manifest b/manifest index 58b1cc67a2..d4d88064f1 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C The\sRTREE\sextension\sbehaves\shas\sif\sdata\scolumns\shave\stype\sREAL,\sso\swe\nshould\sactually\sdeclare\sthem\sas\sREAL\sso\sthat\sautomatic\sindexes\shandle\nthem\scorrectly.\s\sTicket\s[e63b4d1a65546532] -D 2020-02-28T16:04:28.158 +C In\sthe\sCLI,\sadd\sthe\s".oom"\scommand\sfor\sdebugging\sbuilds. +D 2020-02-29T15:53:48.614 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -532,7 +532,7 @@ F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 38e3a5636f5bdc92e3683e4cafbba6418c0aa15e0d89ca5b28bd0b621dbb80bf F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 F src/select.c c94eec317c8ba929bc228392eb3cac8124f2d0fbe3fc1bddecb44dfc7057bc78 -F src/shell.c.in c2e20c43a44fb5588a6c27ce60589538fbf4794fd7686f5b2598eca22eaae1fa +F src/shell.c.in 3897f3f7302914da1f6df3a2a09ac4aafa14a571d7d18c51500cfb2ff04f05eb F src/sqlite.h.in 802957feeb249ede54f8dfe99b72aa19e70a0b7737969c46e625dc2f9f2d42b0 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h 9c5269260409eb3275324ccace6a13a96f4ad330c708415f70ca6097901ff4ee @@ -1860,7 +1860,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P f71a13d072398c9fc3556f42d75159cc2d0edc2c42f6c47f64503a7fbbca6e37 -R e584e926a3f43231aaab0b4293c722be +P 85a9b6a92fd5805d5936f02d555af395441607b9eb5f4dae63560b5e65663b00 +R 2cdefa0603e77b0eb4b0ccaa79df3caf U drh -Z 64c3887bc4bd00b14ac4e9288ce5788a +Z 076cff7e0a1612f30cfec18b6a7cf8ce diff --git a/manifest.uuid b/manifest.uuid index 72796f77e7..e16eea23a4 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -85a9b6a92fd5805d5936f02d555af395441607b9eb5f4dae63560b5e65663b00 \ No newline at end of file +9c3136a722715952d155aae55cbc6d1fb921c6940d8e7d3e32fcba000f6ac1ed \ No newline at end of file diff --git a/src/shell.c.in b/src/shell.c.in index 0532f046ce..149ba7ee1d 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -397,6 +397,15 @@ static sqlite3 *globalDb = 0; */ static volatile int seenInterrupt = 0; +#ifdef SQLITE_DEBUG +/* +** Out-of-memory simulator variables +*/ +static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */ +static unsigned int oomRepeat = 0; /* Number of OOMs in a row */ +static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */ +#endif /* SQLITE_DEBUG */ + /* ** This is the name of our program. It is set in main(), used ** in a number of other places, mostly for error messages. @@ -448,6 +457,49 @@ static void shell_out_of_memory(void){ exit(1); } +#ifdef SQLITE_DEBUG +/* This routine is called when a simulated OOM occurs. It is broken +** out as a separate routine to make it easy to set a breakpoint on +** the OOM +*/ +void shellOomFault(void){ + if( oomRepeat>0 ){ + oomRepeat--; + }else{ + oomCounter--; + } +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* This routine is a replacement malloc() that is used to simulate +** Out-Of-Memory (OOM) errors for testing purposes. +*/ +static void *oomMalloc(int nByte){ + if( oomCounter ){ + if( oomCounter==1 ){ + shellOomFault(); + return 0; + }else{ + oomCounter--; + } + } + return defaultMalloc(nByte); +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* Register the OOM simulator. This must occur before any memory +** allocations */ +static void registerOomSimulator(void){ + sqlite3_mem_methods mem; + sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); + defaultMalloc = mem.xMalloc; + mem.xMalloc = oomMalloc; + sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); +} +#endif + /* ** Write I/O traces to the following stream. */ @@ -3558,6 +3610,9 @@ static const char *(azHelp[]) = { " Other options:", " -e Invoke system text editor", " -x Open in a spreadsheet", +#ifdef SQLITE_DEBUG + ".oom [--repeat M] [N] Simulate an OOM error on the N-th allocation", +#endif ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", " Options:", " --append Use appendvfs to append database to the end of FILE", @@ -8039,6 +8094,34 @@ static int do_meta_command(char *zLine, ShellState *p){ } }else +#ifdef SQLITE_DEBUG + if( c=='o' && strcmp(azArg[0],"oom")==0 ){ + int i; + for(i=1; iout, "missing argument on \"%s\"\n", azArg[i]); + rc = 1; + }else{ + oomRepeat = (int)integerValue(azArg[++i]); + } + }else if( IsDigit(z[0]) ){ + oomCounter = (int)integerValue(azArg[i]); + }else{ + raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]); + raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n"); + rc = 1; + } + } + if( rc==0 ){ + raw_printf(p->out, "oomCounter = %d\n", oomCounter); + raw_printf(p->out, "oomRepeat = %d\n", oomRepeat); + } + }else +#endif /* SQLITE_DEBUG */ + if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ char *zNewFilename; /* Name of the database file to open */ int iName = 1; /* Index in azArg[] of the filename */ @@ -10122,6 +10205,10 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ stdin_is_interactive = isatty(0); stdout_is_console = isatty(1); +#ifdef SQLITE_DEBUG + registerOomSimulator(); +#endif + #if !defined(_WIN32_WCE) if( getenv("SQLITE_DEBUG_BREAK") ){ if( isatty(0) && isatty(2) ){