Fix generating LSan report before app closing 39/165139/3
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Tue, 26 Dec 2017 16:22:58 +0000 (19:22 +0300)
committerDmitry Kovalenko <d.kovalenko@samsung.com>
Wed, 10 Jan 2018 12:44:20 +0000 (12:44 +0000)
Change-Id: Ia43813502d5a3ccfb261769ec27d3ede9274a566
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
helper/libdaprobe.c

index 968c2f6..605b8a7 100755 (executable)
@@ -324,6 +324,25 @@ pid_t _gettid()
        return gTid;
 }
 
+static void leak_check()
+{
+       static bool is_check = false;
+       static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+       real_pthread_mutex_lock(&mutex);
+       if (is_check) {
+               PRINTWRN("LSan leak checker has been already called");
+       } else {
+               is_check = true;
+
+               /*
+                * Called in critical section to guarantee the completion
+                * of the report generation
+                */
+               lsan_do_leak_check();
+       }
+       real_pthread_mutex_unlock(&mutex);
+}
 
 /* 0 - continue reading, 1 - break */
 static int msg_handler(log_t *log)
@@ -338,7 +357,7 @@ static int msg_handler(log_t *log)
                _process_target_bins_remove((char *)log->data, log->length);
        } else if (log->type == APP_MSG_STOP) {
                /* Do leaks check if LSan is enabled. */
-               lsan_do_leak_check();
+               leak_check();
 
                /* Send acknowlege message to manager */
                printLog(log, APP_MSG_STOP);
@@ -347,7 +366,7 @@ static int msg_handler(log_t *log)
                return 1;
        } else if (log->type == APP_MSG_STOP_WITHOUT_KILL) {
                /* Do leaks check if LSan is enabled. */
-               lsan_do_leak_check();
+               leak_check();
 
                /* Send acknowlege message to manager */
                printLog(log, APP_MSG_STOP);
@@ -552,9 +571,16 @@ void _init_(void)
 
 void __attribute__((constructor)) _init_probe()
 {
+       int ret;
+
        /* init library */
        _init_();
 
+       /* register LSan report callback for app closing case */
+       ret = atexit(leak_check);
+       if (ret)
+               PRINTERR("Cannot register leak_check(), ret=%d\n", ret);
+
        PRINTMSG("<-lib construnctor");
 }