handle memory-related debug / trace logging separately

This commit is contained in:
Stephen Lombardo 2019-10-04 16:49:18 -04:00
parent 6936ef0d11
commit 4ce256966e
2 changed files with 22 additions and 20 deletions

View File

@ -115,16 +115,6 @@ void sqlite3pager_reset(Pager *pPager);
#include <android/log.h>
#endif
#ifdef CODEC_DEBUG_MUTEX
#ifdef __ANDROID__
#define CODEC_TRACE_MUTEX(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
#else
#define CODEC_TRACE_MUTEX(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#endif
#else
#define CODEC_TRACE_MUTEX(...)
#endif
#ifdef CODEC_DEBUG
#ifdef __ANDROID__
#define CODEC_TRACE(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
@ -135,6 +125,18 @@ void sqlite3pager_reset(Pager *pPager);
#define CODEC_TRACE(...)
#endif
#ifdef CODEC_DEBUG_MUTEX
#define CODEC_TRACE_MUTEX(...) CODEC_TRACE(__VA_ARGS__)
#else
#define CODEC_TRACE_MUTEX(...)
#endif
#ifdef CODEC_DEBUG_MEMORY
#define CODEC_TRACE_MEMORY(...) CODEC_TRACE(__VA_ARGS__)
#else
#define CODEC_TRACE_MEMORY(...)
#endif
#ifdef CODEC_DEBUG_PAGEDATA
#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \
{ \

View File

@ -112,7 +112,7 @@ static void sqlcipher_mem_shutdown(void *pAppData) {
static void *sqlcipher_mem_malloc(int n) {
void *ptr = default_mem_methods.xMalloc(n);
if(mem_security_on) {
CODEC_TRACE("sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)\n", ptr, n);
CODEC_TRACE_MEMORY("sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)\n", ptr, n);
sqlcipher_mlock(ptr, n);
if(!mem_security_activated) mem_security_activated = 1;
}
@ -125,7 +125,7 @@ static void sqlcipher_mem_free(void *p) {
int sz;
if(mem_security_on) {
sz = sqlcipher_mem_size(p);
CODEC_TRACE("sqlcipher_mem_free: calling sqlcipher_memset(%p,0,%d) and sqlcipher_munlock(%p, %d) \n", p, sz, p, sz);
CODEC_TRACE_MEMORY("sqlcipher_mem_free: calling sqlcipher_memset(%p,0,%d) and sqlcipher_munlock(%p, %d) \n", p, sz, p, sz);
sqlcipher_memset(p, 0, sz);
sqlcipher_munlock(p, sz);
if(!mem_security_activated) mem_security_activated = 1;
@ -281,7 +281,7 @@ void* sqlcipher_memset(void *v, unsigned char value, int len) {
if (v == NULL) return v;
CODEC_TRACE("sqlcipher_memset: setting %p[0-%d]=%d)\n", a, len, value);
CODEC_TRACE_MEMORY("sqlcipher_memset: setting %p[0-%d]=%d)\n", a, len, value);
for(i = 0; i < len; i++) {
a[i] = value;
}
@ -325,10 +325,10 @@ void sqlcipher_mlock(void *ptr, int sz) {
if(ptr == NULL || sz == 0) return;
CODEC_TRACE("sqlcipher_mem_lock: calling mlock(%p,%lu); _SC_PAGESIZE=%lu\n", ptr - offset, sz + offset, pagesize);
CODEC_TRACE_MEMORY("sqlcipher_mem_lock: calling mlock(%p,%lu); _SC_PAGESIZE=%lu\n", ptr - offset, sz + offset, pagesize);
rc = mlock(ptr - offset, sz + offset);
if(rc!=0) {
CODEC_TRACE("sqlcipher_mem_lock: mlock(%p,%lu) returned %d errno=%d\n", ptr - offset, sz + offset, rc, errno);
CODEC_TRACE_MEMORY("sqlcipher_mem_lock: mlock(%p,%lu) returned %d errno=%d\n", ptr - offset, sz + offset, rc, errno);
}
#elif defined(_WIN32)
#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
@ -352,10 +352,10 @@ void sqlcipher_munlock(void *ptr, int sz) {
if(ptr == NULL || sz == 0) return;
CODEC_TRACE("sqlcipher_mem_unlock: calling munlock(%p,%lu)\n", ptr - offset, sz + offset);
CODEC_TRACE_MEMORY("sqlcipher_mem_unlock: calling munlock(%p,%lu)\n", ptr - offset, sz + offset);
rc = munlock(ptr - offset, sz + offset);
if(rc!=0) {
CODEC_TRACE("sqlcipher_mem_unlock: munlock(%p,%lu) returned %d errno=%d\n", ptr - offset, sz + offset, rc, errno);
CODEC_TRACE_MEMORY("sqlcipher_mem_unlock: munlock(%p,%lu) returned %d errno=%d\n", ptr - offset, sz + offset, rc, errno);
}
#elif defined(_WIN32)
#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
@ -379,7 +379,7 @@ void sqlcipher_munlock(void *ptr, int sz) {
* memory segment so it can be paged
*/
void sqlcipher_free(void *ptr, int sz) {
CODEC_TRACE("sqlcipher_free: calling sqlcipher_memset(%p,0,%d)\n", ptr, sz);
CODEC_TRACE_MEMORY("sqlcipher_free: calling sqlcipher_memset(%p,0,%d)\n", ptr, sz);
sqlcipher_memset(ptr, 0, sz);
sqlcipher_munlock(ptr, sz);
sqlite3_free(ptr);
@ -392,9 +392,9 @@ void sqlcipher_free(void *ptr, int sz) {
*/
void* sqlcipher_malloc(int sz) {
void *ptr;
CODEC_TRACE("sqlcipher_malloc: calling sqlite3Malloc(%d)\n", sz);
CODEC_TRACE_MEMORY("sqlcipher_malloc: calling sqlite3Malloc(%d)\n", sz);
ptr = sqlite3Malloc(sz);
CODEC_TRACE("sqlcipher_malloc: calling sqlcipher_memset(%p,0,%d)\n", ptr, sz);
CODEC_TRACE_MEMORY("sqlcipher_malloc: calling sqlcipher_memset(%p,0,%d)\n", ptr, sz);
sqlcipher_memset(ptr, 0, sz);
sqlcipher_mlock(ptr, sz);
return ptr;