提交 32abecfb 编写于 作者: weixin_48148422's avatar weixin_48148422

make 'taos_dump_memory_leak' public for java

上级 a5bfb0d6
...@@ -17,6 +17,14 @@ extern "C" { ...@@ -17,6 +17,14 @@ extern "C" {
JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp
(JNIEnv *, jclass, jstring); (JNIEnv *, jclass, jstring);
/*
* Class: com_taosdata_jdbc_TSDBJNIConnector
* Method:
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_dumpMemoryLeakImp
(JNIEnv *, jclass);
/* /*
* Class: com_taosdata_jdbc_TSDBJNIConnector * Class: com_taosdata_jdbc_TSDBJNIConnector
* Method: initImp * Method: initImp
......
...@@ -114,15 +114,20 @@ void jniGetGlobalMethod(JNIEnv *env) { ...@@ -114,15 +114,20 @@ void jniGetGlobalMethod(JNIEnv *env) {
JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp(JNIEnv *env, jobject jobj, jstring jPath) { JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_detectMemoryLeakImp(JNIEnv *env, jobject jobj, jstring jPath) {
if (jPath != NULL) { if (jPath != NULL) {
const char *path = (*env)->GetStringUTFChars(env, jPath, NULL); const char *path = (*env)->GetStringUTFChars(env, jPath, NULL);
taos_dump_memory_leak_at_exit(path); taos_detect_memory_leak(path);
(*env)->ReleaseStringUTFChars(env, jPath, path); (*env)->ReleaseStringUTFChars(env, jPath, path);
} else { } else {
taos_dump_memory_leak_at_exit(NULL); taos_detect_memory_leak(NULL);
} }
jniGetGlobalMethod(env); jniGetGlobalMethod(env);
} }
JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_dumpMemoryLeakImp(JNIEnv *env, jobject jobj) {
taos_dump_memory_leak();
jniGetGlobalMethod(env);
}
JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp(JNIEnv *env, jobject jobj, jstring jconfigDir) { JNIEXPORT void JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_initImp(JNIEnv *env, jobject jobj, jstring jconfigDir) {
if (jconfigDir != NULL) { if (jconfigDir != NULL) {
const char *confDir = (*env)->GetStringUTFChars(env, jconfigDir, NULL); const char *confDir = (*env)->GetStringUTFChars(env, jconfigDir, NULL);
......
...@@ -187,7 +187,8 @@ static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, unsigned int inLen, cha ...@@ -187,7 +187,8 @@ static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, unsigned int inLen, cha
char *taosIpStr(uint32_t ipInt); char *taosIpStr(uint32_t ipInt);
extern void taos_dump_memory_leak_at_exit(const char* path); extern void taos_detect_memory_leak(const char* path);
extern void taos_dump_memory_leak();
#if TAOS_MEM_CHECK == 1 #if TAOS_MEM_CHECK == 1
......
...@@ -64,9 +64,9 @@ int main(int argc, char *argv[]) { ...@@ -64,9 +64,9 @@ int main(int argc, char *argv[]) {
#if TAOS_MEM_CHECK == 2 #if TAOS_MEM_CHECK == 2
} else if (strcmp(argv[i], "--check-mem-leak") == 0) { } else if (strcmp(argv[i], "--check-mem-leak") == 0) {
if ((i < argc - 1) && (argv[i+1][0] != '-')) { if ((i < argc - 1) && (argv[i+1][0] != '-')) {
taos_dump_memory_leak_at_exit(argv[++i]); taos_detect_memory_leak(argv[++i]);
} else { } else {
taos_dump_memory_leak_at_exit(NULL); taos_detect_memory_leak(NULL);
} }
#endif #endif
} }
......
...@@ -261,11 +261,15 @@ ssize_t taos_getline(char **lineptr, size_t *n, FILE *stream, const char* file, ...@@ -261,11 +261,15 @@ ssize_t taos_getline(char **lineptr, size_t *n, FILE *stream, const char* file,
return size; return size;
} }
static void dump_memory_leak() { void taos_dump_memory_leak() {
const char* hex = "0123456789ABCDEF"; const char* hex = "0123456789ABCDEF";
const char* fmt = ":%d: addr=0x%p, size=%d, content(first 16 bytes)='"; const char* fmt = ":%d: addr=0x%p, size=%d, content(first 16 bytes)='";
size_t numOfBlk = 0, totalSize = 0; size_t numOfBlk = 0, totalSize = 0;
if (fpMemLeak == NULL) {
return;
}
fputs("memory blocks allocated but not freed before exit:\n\n", fpMemLeak); fputs("memory blocks allocated but not freed before exit:\n\n", fpMemLeak);
while (atomic_val_compare_exchange_ptr(&lock, 0, 1) != 0); while (atomic_val_compare_exchange_ptr(&lock, 0, 1) != 0);
...@@ -303,13 +307,13 @@ static void dump_memory_leak() { ...@@ -303,13 +307,13 @@ static void dump_memory_leak() {
static void dump_memory_leak_at_sig(int sig) { static void dump_memory_leak_at_sig(int sig) {
fprintf(fpMemLeak, "signal %d received, exiting...\n", sig); fprintf(fpMemLeak, "signal %d received, exiting...\n", sig);
dump_memory_leak(); taos_dump_memory_leak();
struct sigaction act = {0}; struct sigaction act = {0};
act.sa_handler = SIG_DFL; act.sa_handler = SIG_DFL;
sigaction(sig, &act, NULL); sigaction(sig, &act, NULL);
} }
void taos_dump_memory_leak_at_exit(const char* path) { void taos_detect_memory_leak(const char* path) {
if (fpMemLeak != NULL) { if (fpMemLeak != NULL) {
printf("memory leak detection already enabled.\n"); printf("memory leak detection already enabled.\n");
return; return;
...@@ -322,7 +326,7 @@ void taos_dump_memory_leak_at_exit(const char* path) { ...@@ -322,7 +326,7 @@ void taos_dump_memory_leak_at_exit(const char* path) {
return; return;
} }
atexit(dump_memory_leak); atexit(taos_dump_memory_leak);
struct sigaction act = {0}; struct sigaction act = {0};
act.sa_handler = dump_memory_leak_at_sig; act.sa_handler = dump_memory_leak_at_sig;
...@@ -334,7 +338,11 @@ void taos_dump_memory_leak_at_exit(const char* path) { ...@@ -334,7 +338,11 @@ void taos_dump_memory_leak_at_exit(const char* path) {
#endif #endif
#if TAOS_MEM_CHECK != 2 #if TAOS_MEM_CHECK != 2
void taos_dump_memory_leak_at_exit(const char* path) { void taos_dump_memory_leak() {
printf("memory leak detection not enabled!") // do nothing
}
void taos_detect_memory_leak(const char* path) {
printf("memory leak detection not enabled, please set 'TAOS_MEM_CHECK' to 2.");
} }
#endif #endif
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册