- add protection codes for preventing atrace from double initailization 31/78131/4 accepted/tizen/common/20160706.141733 accepted/tizen/ivi/20160705.101738 accepted/tizen/mobile/20160705.101854 accepted/tizen/tv/20160705.101809 accepted/tizen/wearable/20160705.101831 submit/tizen/20160705.082028
authorEunji, Lee <eunjieji.lee@samsung.com>
Mon, 4 Jul 2016 10:06:12 +0000 (19:06 +0900)
committerEunji, Lee <eunjieji.lee@samsung.com>
Tue, 5 Jul 2016 07:02:14 +0000 (16:02 +0900)
- add protection codes for avoding tracing msg size overflow

Change-Id: I5415c94b5ed77c1fa805ff73d6d12fc10194085c
Signed-off-by: Eunji, Lee <eunjieji.lee@samsung.com>
src/atrace/atrace.cpp
src/ttrace.c

index 05e1506..c80cf06 100755 (executable)
@@ -511,6 +511,11 @@ static bool setTagsProperty(uint64_t tags)
                size_t bufSize = DEF_GR_SIZE;
                char buf[DEF_GR_SIZE];
                int ret = 0;
+
+               if(fileExists(ENABLED_TAG_FILE)) {
+                       fprintf(stderr, "[Info] T-trace has been already initailized\n");
+                       return false; //atrace has been already initailized.
+               }
                ret = getgrnam_r(TTRACE_GROUP_NAME, &group_dev, buf, bufSize, &group_ptr);
 
                if (ret != 0 && ret != ERANGE)
@@ -543,7 +548,6 @@ static bool setTagsProperty(uint64_t tags)
                        if(ret == ERANGE) isInvalid = true;
                        free(dynbuf);
                }
-
                fd = open("/tmp/tmp_tag", O_CREAT | O_RDWR | O_CLOEXEC, 0666);                          
                if(fd < 0){
                        fprintf(stderr, "Fail to open enabled_tag file: %s(%d)\n", strerror_r(errno, str_error, sizeof(str_error)), errno);
@@ -552,20 +556,20 @@ static bool setTagsProperty(uint64_t tags)
                //set file permission, smack label to "/tmp/tmp_tag" and then change it's name to "/tmp/ttrace_tag"
                if (!setFilePermission("/tmp/tmp_tag", tag_node.perms))
                {
-                       fprintf(stderr, "error: setFilePermission failed(%s): /tmp/tmp_tag\n", strerror_r(errno, str_error, sizeof(str_error)));
+                       fprintf(stderr, "setFilePermission failed(%s): /tmp/tmp_tag\n", strerror_r(errno, str_error, sizeof(str_error)));
                        close(fd);
                        return false;
                }
 
                if (ftruncate(fd, sizeof(uint64_t)) < 0) {
-                       fprintf(stderr, "error: ftruncate() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
+                       fprintf(stderr, "ftruncate() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
                        close(fd);
                        return false;
                }
                sm_for_enabled_tag = (uint64_t*)mmap(NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 
                if(sm_for_enabled_tag == MAP_FAILED) {
-                       fprintf(stderr, "error: mmap() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
+                       fprintf(stderr, "mmap() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
                        close(fd);
                        return false;
                }
@@ -616,7 +620,7 @@ static bool setTagsProperty(uint64_t tags)
                }
                sm_for_enabled_tag = (uint64_t*)mmap(NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
                if(sm_for_enabled_tag == MAP_FAILED) {
-                       fprintf(stderr, "error: mmap() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
+                       fprintf(stderr, "mmap() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
                        close(fd);
                        return false;
                }
@@ -1198,7 +1202,7 @@ int main(int argc, char **argv)
                     listSupportedCategories();
                     exit(0);
                 } else if (!strcmp(long_options[option_index].name, "init_exec")) {
-                    fprintf(stderr, "init_exec\n");
+                    fprintf(stderr, "[Info] Initailize T-trace\n");
                     g_init_exec = true;
                     setTagsProperty(0);
                     exit(0);
index e89fc41..c255194 100755 (executable)
 
 #define MAX_TRACE_LEN 1024
 #define MAX_LEN 512
+#define MAX_TAIL_LEN 13
+#define MAX_HEAD_LEN 8
+#define POS_LABEL_ST (MAX_LEN - MAX_HEAD_LEN)
+#define POS_LABEL_ED (MAX_LEN - MAX_TAIL_LEN)
+
 #define FD_INITIAL_VALUE -1
 #define TRACE_FILE_NOT_EXIST -2
 
@@ -131,15 +136,17 @@ void traceBegin(uint64_t tag, const char *name, ...)
 {
        if (isTagEnabled(tag) || g_extension_state == EXT_ACTIVATED) { 
                char buf[MAX_LEN];
-               int len = 0, ret = 0;
+               int len = MAX_HEAD_LEN, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceBegin:: write >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
 
                va_start(ap, name);
-               len = snprintf(buf, MAX_LEN, "B|%d|", getpid());
-               len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
+               snprintf(buf, MAX_LEN, "B|%5d|", getpid());
+               len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
                va_end(ap);
+               
+               if (len > MAX_LEN) len = MAX_LEN - 1;
 
                if (g_extension_state == EXT_ACTIVATED) ttrace_extension_write(buf, len);
                else            ret = write(g_trace_handle_fd, buf, len);
@@ -181,20 +188,27 @@ void traceEnd(uint64_t tag)
  * - cookie: an unique identifier for distinguishing simultaneous events.
  * The name and cookie used to begin an event must be used to end it.
  */
-void traceAsyncBegin(uint64_t tag, int cookie, const char *name, ...)
+void traceAsyncBegin(uint64_t tag, int32_t cookie, const char *name, ...)
 {
        if (isTagEnabled(tag) || g_extension_state == EXT_ACTIVATED) { 
                char buf[MAX_LEN];
-               int len = 0, ret = 0;
+               int len = MAX_HEAD_LEN, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceAsyncBegin:: write >> tag: %u tag_bit: %u cookie: %d", tag, *cur_enabled_tag, cookie);
                va_start(ap, name);
-               len = snprintf(buf, MAX_LEN, "S|%d|", getpid());
-               len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
-               len += snprintf(buf + len, MAX_LEN - len, "|%d", cookie);
+               snprintf(buf, MAX_LEN, "S|%5d|", getpid());
+               len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
                va_end(ap);
 
+               if (len + MAX_TAIL_LEN < MAX_LEN) {
+                       len += snprintf(buf + len, MAX_LEN - len, "|%d", cookie);
+               }
+               else {
+                       snprintf(buf + POS_LABEL_ED, MAX_TAIL_LEN, "|%d", cookie);
+                       len = MAX_LEN - 1;
+               }
+
                if (g_extension_state == EXT_ACTIVATED) ttrace_extension_write(buf, len);
                else    ret = write(g_trace_handle_fd, buf, len);
 
@@ -208,20 +222,28 @@ void traceAsyncBegin(uint64_t tag, int cookie, const char *name, ...)
 #endif
 }
 
-void traceAsyncEnd(uint64_t tag, int cookie, const char *name, ...)
+void traceAsyncEnd(uint64_t tag, int32_t cookie, const char *name, ...)
 {
        if (isTagEnabled(tag) || g_extension_state == EXT_ACTIVATED) { 
                char buf[MAX_LEN];
-               int len = 0, ret = 0;
+               int len = MAX_HEAD_LEN, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceAsyncEnd:: write>> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
                va_start(ap, name);
-               len = snprintf(buf, MAX_LEN, "F|%d|", getpid());
-               len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
-               len += snprintf(buf + len, MAX_LEN - len, "|%d", cookie);
+               snprintf(buf, MAX_LEN, "F|%5d|", getpid());
+               len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
                va_end(ap);
 
+               if (len + MAX_TAIL_LEN < MAX_LEN) {
+                       len += snprintf(buf + len, MAX_LEN - len, "|%d", cookie);
+               }
+               else {
+                       snprintf(buf + POS_LABEL_ED, MAX_TAIL_LEN, "|%d", cookie);
+                       len = MAX_LEN - 1;
+               }
+
+
                if (g_extension_state == EXT_ACTIVATED) ttrace_extension_write(buf, len);
                else    ret = write(g_trace_handle_fd, buf, len);
 
@@ -245,15 +267,17 @@ void traceMark(uint64_t tag, const char *name, ...)
 {
        if (isTagEnabled(tag) || g_extension_state == EXT_ACTIVATED) { 
                char buf[MAX_LEN], end = 'E';
-               int len = 0, ret = 0;
+               int len = MAX_HEAD_LEN, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceMark:: write >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
                va_start(ap, name);
-               len = snprintf(buf, MAX_LEN, "B|%d|", getpid());
-               len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
+               snprintf(buf, MAX_LEN, "B|%5d|", getpid());
+               len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
                va_end(ap);
 
+               if (len > MAX_LEN) len = MAX_LEN - 1;
+
                if (g_extension_state == EXT_ACTIVATED) ttrace_extension_write(buf, len);
                else    ret = write(g_trace_handle_fd, buf, len);
                
@@ -280,20 +304,27 @@ void traceMark(uint64_t tag, const char *name, ...)
  * - name: the event name
  * - value: the value tracing
  */
-void traceCounter(uint64_t tag, int value, const char *name, ...)
+void traceCounter(uint64_t tag, int32_t value, const char *name, ...)
 {
        if (isTagEnabled(tag) || g_extension_state == EXT_ACTIVATED) { 
                char buf[MAX_LEN];
-               int len = 0, ret = 0;
+               int len = MAX_HEAD_LEN, ret = 0;
                va_list ap;
 
                va_start(ap, name);
 
-               len = snprintf(buf, MAX_LEN, "C|%d|", getpid());
-               len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
-               len += snprintf(buf + len, MAX_LEN - len, "|%d", value);
+               snprintf(buf, MAX_LEN, "C|%5d|", getpid());
+               len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
                va_end(ap);
 
+               if (len + MAX_TAIL_LEN < MAX_LEN) {
+                       len += snprintf(buf + len, MAX_LEN - len, "|%d", value);
+               }
+               else {
+                       snprintf(buf + POS_LABEL_ED, MAX_TAIL_LEN, "|%d", value);
+                       len = MAX_LEN - 1;
+               }
+
                if (g_extension_state == EXT_ACTIVATED) ttrace_extension_write(buf, len);
                else    ret = write(g_trace_handle_fd, buf, len);
 
@@ -314,10 +345,10 @@ void traceBegin(uint64_t tag, const char *name, ...)
 void traceEnd(uint64_t tag)
 {; }
 
-void traceAsyncBegin(uint64_t tag, int cookie, const char *name, ...)
+void traceAsyncBegin(uint64_t tag, int32_t cookie, const char *name, ...)
 {; }
 
-void traceAsyncEnd(uint64_t tag, int cookie, const char *name, ...)
+void traceAsyncEnd(uint64_t tag, int32_t cookie, const char *name, ...)
 {; }
 
 void traceMark(uint64_t tag, const char *name, ...)