sync with 2.4 branch (commit:1ae27085e86ce77d74922fc43a256a9e7108724e) 90/59390/1
authorEunji, Lee <eunjieji.lee@samsung.com>
Mon, 15 Feb 2016 08:32:43 +0000 (17:32 +0900)
committerEunji, Lee <eunjieji.lee@samsung.com>
Mon, 15 Feb 2016 08:32:43 +0000 (17:32 +0900)
Change-Id: Ie3cba9e4c69c848ec88b4cad2e64c9c0a8c977e4
Signed-off-by: Eunji, Lee <eunjieji.lee@samsung.com>
packaging/ttrace.spec
src/atrace/atrace.cpp
src/ttrace.c
ttrace.h.in

index b450a7272d03634e5d99d3a2d5c462075e82bc69..1dc025993c4ad11ec6b30e5e399d003e0b80d82e 100755 (executable)
@@ -46,7 +46,8 @@ T-trace library devel
 %build
 export CFLAGS="$CFLAGS -g -Wall -std=gnu99"
 export CXXFLAGS="$CXXFLAGS -std=c++0x -fPIE -pie"
-%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DLIBDIR=%{_libdir} -DINCLUDEDIR=%{_includedir}
+%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DLIBDIR=%{_libdir} -DINCLUDEDIR=%{_includedir} \
+      -DTTRACE_PROFILE=%{TTRACE_PROFILE} 
 make %{?jobs:-j%jobs}
 
 %install
index a31322fdc12aff879f7ecaae87e64bb960273212..146e99b3425f75c16f39483b98463eb7b713b4b2 100755 (executable)
@@ -65,12 +65,21 @@ const char* k_traceAppCmdlineProperty = "debug.atrace.app_cmdlines";
 typedef enum { OPT, REQ } requiredness  ;
 
 char str_error[256] = "";
+
 struct CommonNode {
        const char* path;
        const mode_t    perms;
 };
+
+typedef enum {
+       TTRACE_TAG_IDX = 0,
+       DEBUG_FS_IDX,
+       TRACE_MARKER_IDX,
+       ESSENCE_NODE_IDX        
+} commonNodeIdx;
+
 static const CommonNode commonNodes[] = {
-       {       "/tmp/ttrace_tag",                                                              0666    },
+       {       ENABLED_TAG_FILE,               0664    },
        {       "/sys/kernel/debug",                                                    0755    },
        {       "/sys/kernel/debug/tracing/trace_marker",               0222    },
        {       "/sys/kernel/debug/tracing/trace_clock",                0666    },
@@ -208,6 +217,9 @@ static bool g_categoryEnables[NELEM(k_categories)] = {};
 static bool g_init_exec = false;
 static bool g_append_trace = false;
 static bool g_backup_trace = false;
+
+static struct group group_dev;
+static struct group* group_ptr;
 #endif
 
 /* Sys file paths */
@@ -259,28 +271,35 @@ static bool fileExists(const char* filename) {
 static bool fileIsWritable(const char* filename) {
     return access(filename, W_OK) != -1;
 }
+
+static bool setFilePermisstion (const char *path, const mode_t perms) {
+       //fprintf(stderr, "path: %s, perms: %d, gid: %d\n", path,perms, group_dev.gr_gid);
+       if (0 > chown(path, 0, group_dev.gr_gid)) return false;
+       if (0 > chmod(path, perms)) return false;
+       if (0 > smack_setlabel(path, "*", SMACK_LABEL_ACCESS)) return false;
+
+       return true;
+}
+
 static bool initSysfsPermission() {
-       struct group group_dev;
-       struct group* group_ptr;
-       char buf[128];
-       if(0 != getgrnam_r("developer", &group_dev, buf, sizeof(buf), &group_ptr))
-               return false;
-       for (int i = 0 ; i < NELEM(commonNodes); i++) {
+       for (int i = TTRACE_TAG_IDX + 1 ; i < NELEM(commonNodes); i++) {
                const CommonNode &node = commonNodes[i];
-               if (node.path != NULL) {
-                       if (fileExists(node.path)) {
-                               if (strcmp(node.path, "/sys/kernel/debug") == 0) {
-                                       chmod(node.path, node.perms);
-                               }
-                               else {
-                                       chown(node.path, 0, group_dev.gr_gid);
-                                       chmod(node.path, node.perms);
-                                       smack_setlabel(node.path, "*", SMACK_LABEL_ACCESS);
-                               }
+               printf("initsysfsperm: path- %s, perms- %d\n", node.path, node.perms);
+               if (fileExists(node.path)) {
+                       if (i == DEBUG_FS_IDX) {
+                               if(0 > chmod(node.path, node.perms))
+                                       return false;
+                       }
+                       else {
+                               if (!setFilePermisstion(node.path, node.perms))
+                                       return false;
                        }
                }
                else {
-                       return false;
+                       if(i < ESSENCE_NODE_IDX)
+                       {
+                               return false;
+                       }
                }
        }
     return true;
@@ -418,7 +437,6 @@ static bool clearTrace()
 static bool setTraceBufferSizeKB(int size)
 {
     char str[32] = "1";
-    int len;
     if (size < 1) {
         size = 1;
     }
@@ -477,48 +495,76 @@ static bool setTagsProperty(uint64_t tags)
 #ifdef DEVICE_TYPE_TIZEN
        uint64_t *sm_for_enabled_tag = NULL;
        int fd = -1;
-       if(g_init_exec) {
-               fd = open("/tmp/tmp_tag", O_CREAT | O_RDWR | O_CLOEXEC, 0666);          
+       char buf[128];
+       const CommonNode &tag_node = commonNodes[TTRACE_TAG_IDX];
 
-       if(fd < 0){
-               fprintf(stderr, "Fail to open enabled_tag file: %s(%d)\n", strerror_r(errno, str_error, sizeof(str_error)), errno);
+//atrace "--init_exec" mode
+       if(g_init_exec) {
+               if(0 != getgrnam_r("developer", &group_dev, buf, sizeof(buf), &group_ptr))
+                       return false;
+               
+               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);
+                       return false;
+               }
+               //set file permission, smack label to "/tmp/tmp_tag" and then change it's name to "/tmp/ttrace_tag"
+               if (!setFilePermisstion("/tmp/tmp_tag", tag_node.perms)) 
+               {
+                       fprintf(stderr, "error: setFilePermisstion 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)));
                        close(fd);
-               return false;
-       }
-       sm_for_enabled_tag = (uint64_t*)mmap(NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+                       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)));
-               close(fd);
-               return false;
-       }
+               if(sm_for_enabled_tag == MAP_FAILED) {
+                       fprintf(stderr, "error: mmap() failed(%s)\n", strerror_r(errno, str_error, sizeof(str_error)));
+                       close(fd);
+                       return false;
+               }
+
+               if(!initSysfsPermission()) {
+                       fprintf(stderr, "Fail to init sysfs permisions: %s(%d)\n", strerror_r(errno, str_error, sizeof(str_error)), errno);
+                       close(fd);
+                       return false;
+               }
 
                memset(sm_for_enabled_tag, 0, sizeof(uint64_t));
-               if(-1 == rename("/tmp/tmp_tag", ENABLED_TAG_FILE)) {
+               if(-1 == rename("/tmp/tmp_tag", tag_node.path)) {
                        fprintf(stderr, "Fail to rename enabled_tag file: %s(%d)\n", strerror_r(errno, str_error, sizeof(str_error)), errno);
                }
-               if(false == initSysfsPermission()) {
-                       fprintf(stderr, "Fail to init sysfs permisions: %s(%d)\n", strerror_r(errno, str_error, sizeof(str_error)), errno);
-               }
+
                if(fileExists(BOOTUP_TRACE)) {
                        FILE *ifile = NULL;
                        char bootup_cmd[128];
                        ifile = fopen(BOOTUP_TRACE, "r");
                        if (ifile == NULL) {
-                           munmap(sm_for_enabled_tag, sizeof(uint64_t));
-                           close(fd);
-                       return false;
-               }
-                       fgets(bootup_cmd, sizeof(bootup_cmd), ifile);
+                               munmap(sm_for_enabled_tag, sizeof(uint64_t));
+                               close(fd);
+                               return false;
+                       }
+                       if (fgets(bootup_cmd, sizeof(bootup_cmd), ifile) == NULL) {
+                               munmap(sm_for_enabled_tag, sizeof(uint64_t));
+                               close(fd);
+                               fclose(ifile);
+                               return false;
+                       }
                        fclose(ifile);
                        remove(BOOTUP_TRACE);
-                       system(bootup_cmd);
+                       if (0 > system(bootup_cmd)) {
+                               munmap(sm_for_enabled_tag, sizeof(uint64_t));
+                               close(fd);
+                               return false;
+                       }
                }
        }
+//atrace normal mode
        else {
                fd = open(ENABLED_TAG_FILE, O_RDWR | O_CLOEXEC, 0666);          
                if(fd < 0){
@@ -531,12 +577,11 @@ static bool setTagsProperty(uint64_t tags)
                        close(fd);
                        return false;
                }
-       *sm_for_enabled_tag = tags;
+               *sm_for_enabled_tag = tags;
        }
        // For debug
-       // fprintf(stderr, "Enabled TAGs: %u\n", (uint32_t)*sm_for_enabled_tag);
+       //fprintf(stderr, "Enabled TAGs: %u\n", (uint32_t)*sm_for_enabled_tag);
        //
-
        munmap(sm_for_enabled_tag, sizeof(uint64_t));
        close(fd);
 #else
@@ -645,6 +690,7 @@ static bool setKernelTraceFuncs(const char* funcs)
         ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
         ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
         ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
+        ok &= setKernelOptionEnable(k_funcgraphDurationPath, true);
 
         // Set the requested filter functions.
         ok &= truncateFile(k_ftraceFilterPath);
index e9277ae7e6d87c78fd158ff8c918ba0fa82a7491..12c953df5ee133512de496a9d7ad454b14c0bcab 100755 (executable)
@@ -39,6 +39,7 @@
 #define TTRACE_LOG(format, arg...)
 #endif
 
+#include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -62,27 +63,29 @@ static uint64_t traceInit()
 {
        uint64_t *sm_for_enabled_tag;
 
-       if(g_trace_handle_fd == FD_INITIAL_VALUE) {
-       g_trace_handle_fd = open(TRACE_FILE, O_WRONLY);
-       if (g_trace_handle_fd < 0) {
-               TTRACE_LOG("Fail to open trace file: %s(%d)", strerror(errno), errno);
-               /* in case ftrace debugfs is not mounted, ttrace does not call traceInit() anymore. */
-               if (errno == ENOENT)
-                       g_trace_handle_fd = TRACE_FILE_NOT_EXIST;
-
-               set_last_result(TRACE_ERROR_IO_ERROR);
-               return 0;
-               }
-       }
        if (cur_enabled_tag == ((void *)&dummy)) {
                g_enabled_tag_fd = open(ENABLED_TAG_FILE, O_RDONLY | O_CLOEXEC);
                if (g_enabled_tag_fd < 0) {
                        TTRACE_LOG("Fail to open enabled_tag file: %s(%d)", strerror(errno), errno);
                        if (errno == ENOENT)
                                g_enabled_tag_fd = TRACE_FILE_NOT_EXIST;
-
                        return 0;
                }
+
+               /* access trace_marker after ensuring tag file creation */
+               if(g_trace_handle_fd == FD_INITIAL_VALUE) {
+                       g_trace_handle_fd = open(TRACE_FILE, O_WRONLY);
+                       if (g_trace_handle_fd < 0) {
+                               TTRACE_LOG("Fail to open trace file: %s(%d)", strerror(errno), errno);
+                               /* in case ftrace debugfs is not mounted, ttrace does not call traceInit() anymore. */
+                               if (errno == ENOENT)
+                                       g_trace_handle_fd = TRACE_FILE_NOT_EXIST;
+
+                               set_last_result(TRACE_ERROR_IO_ERROR);
+                               return 0;
+                       }
+               }
+
                sm_for_enabled_tag = mmap(NULL, sizeof(uint64_t), PROT_READ, MAP_SHARED, g_enabled_tag_fd, 0);
                if (sm_for_enabled_tag == MAP_FAILED) {
                        TTRACE_LOG("error: mmap() failed(%s)\n", strerror(errno));
@@ -120,7 +123,7 @@ void traceBegin(uint64_t tag, const char *name, ...)
 {
        if (isTagEnabled(tag)) {
                char buf[MAX_LEN];
-               int len = 0;
+               int len = 0, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceBegin:: write >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
@@ -128,7 +131,9 @@ void traceBegin(uint64_t tag, const char *name, ...)
                len = snprintf(buf, MAX_LEN, "B|%d|", getpid());
                len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
                va_end(ap);
-               write(g_trace_handle_fd, buf, len);
+               ret = write(g_trace_handle_fd, buf, len);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceBegin.\n", len, ret, errno);
        }
 #ifdef TTRACE_DEBUG
        else
@@ -141,9 +146,12 @@ void traceBegin(uint64_t tag, const char *name, ...)
 void traceEnd(uint64_t tag)
 {
        if (isTagEnabled(tag)) {
+               int ret = 0;
                char end = 'E';
                TTRACE_LOG("traceEnd:: write>> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
-               write(g_trace_handle_fd, &end, 1);
+               ret = write(g_trace_handle_fd, &end, 1);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceEnd.\n", 1, ret, errno);
        }
 #ifdef TTRACE_DEBUG
        else
@@ -163,7 +171,7 @@ void traceAsyncBegin(uint64_t tag, int cookie, const char *name, ...)
 {
        if (isTagEnabled(tag)) {
                char buf[MAX_LEN];
-               int len = 0;
+               int len = 0, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceAsyncBegin:: write >> tag: %u tag_bit: %u cookie: %d", tag, *cur_enabled_tag, cookie);
@@ -172,7 +180,9 @@ void traceAsyncBegin(uint64_t tag, int cookie, const char *name, ...)
                len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
                len += snprintf(buf + len, MAX_LEN - len, "|%d", cookie);
                va_end(ap);
-               write(g_trace_handle_fd, buf, len);
+               ret = write(g_trace_handle_fd, buf, len);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceAsyncBegin.\n", len, ret, errno);
        }
 #ifdef TTRACE_DEBUG
        else
@@ -185,7 +195,7 @@ void traceAsyncEnd(uint64_t tag, int cookie, const char *name, ...)
 {
        if (isTagEnabled(tag)) {
                char buf[MAX_LEN];
-               int len = 0;
+               int len = 0, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceAsyncEnd:: write>> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
@@ -194,7 +204,9 @@ void traceAsyncEnd(uint64_t tag, int cookie, const char *name, ...)
                len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
                len += snprintf(buf + len, MAX_LEN - len, "|%d", cookie);
                va_end(ap);
-               write(g_trace_handle_fd, buf, len);
+               ret = write(g_trace_handle_fd, buf, len);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceAsyncEnd.\n", len, ret, errno);
        }
 #ifdef TTRACE_DEBUG
        else
@@ -213,7 +225,7 @@ void traceMark(uint64_t tag, const char *name, ...)
 {
        if (isTagEnabled(tag)) {
                char buf[MAX_LEN], end = 'E';
-               int len = 0;
+               int len = 0, ret = 0;
                va_list ap;
 
                TTRACE_LOG("traceMark:: write >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);
@@ -221,8 +233,12 @@ void traceMark(uint64_t tag, const char *name, ...)
                len = snprintf(buf, MAX_LEN, "B|%d|", getpid());
                len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
                va_end(ap);
-               write(g_trace_handle_fd, buf, len);
-               write(g_trace_handle_fd, &end, 1);
+               ret = write(g_trace_handle_fd, buf, len);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceMark.\n", len, ret, errno);
+               ret = write(g_trace_handle_fd, &end, 1);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceMark.\n", 1, ret, errno);
        }
 #ifdef TTRACE_DEBUG
        else
@@ -242,7 +258,7 @@ void traceCounter(uint64_t tag, int value, const char *name, ...)
 {
        if (isTagEnabled(tag)) {
                char buf[MAX_LEN];
-               int len = 0;
+               int len = 0, ret = 0;
                va_list ap;
 
                va_start(ap, name);
@@ -251,7 +267,9 @@ void traceCounter(uint64_t tag, int value, const char *name, ...)
                len += vsnprintf(buf + len, MAX_LEN - len, name, ap);
                len += snprintf(buf + len, MAX_LEN - len, "|%d", value);
                va_end(ap);
-               write(g_trace_handle_fd, buf, len);
+               ret = write(g_trace_handle_fd, buf, len);
+               if (ret < 0)
+                       fprintf(stderr, "error writing, len: %d, ret: %d, errno: %d at traceCounter.\n", len, ret, errno);
        }
 #ifdef TTRACE_DEBUG
        else
index f52b85b5fb01f104b4f97d182d21677c8b213dbb..cfd6890de661db3858403ed32e4911868006fb6c 100755 (executable)
 #ifndef _CDBG_TIZEN_TTRACE_H_
 #define _CDBG_TIZEN_TTRACE_H_
 
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
 #cmakedefine TTRACE_PROFILE_MOBILE
 #cmakedefine TTRACE_PROFILE_TV
 #cmakedefine TTRACE_PROFILE_WEARABLE
 
+#include <stdio.h>
 #include "stdint.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
 /* Define TTRACE_TAG */
 #define TTRACE_TAG_NEVER               0       // This tag is never enabled.
 #define TTRACE_TAG_ALWAYS              (1<<0)  // This tag is always enabled.