diff options
author | Eunji, Lee <eunjieji.lee@samsung.com> | 2016-07-04 19:06:12 +0900 |
---|---|---|
committer | Eunji, Lee <eunjieji.lee@samsung.com> | 2016-07-05 16:02:14 +0900 |
commit | 5f101213b21ee0493415cd6b37163370a7611684 (patch) | |
tree | 64951a8f9a153defe1c316aa28996fb0f16039d7 | |
parent | 921dd21333ed11d14e7d80728ebd96a6a23b4acc (diff) | |
download | ttrace-accepted/tizen/common/20160706.141733.tar.gz ttrace-accepted/tizen/common/20160706.141733.tar.bz2 ttrace-accepted/tizen/common/20160706.141733.zip |
- add protection codes for preventing atrace from double initailizationsubmit/tizen/20160705.082028accepted/tizen/wearable/20160705.101831accepted/tizen/tv/20160705.101809accepted/tizen/mobile/20160705.101854accepted/tizen/ivi/20160705.101738accepted/tizen/common/20160706.141733
- add protection codes for avoding tracing msg size overflow
Change-Id: I5415c94b5ed77c1fa805ff73d6d12fc10194085c
Signed-off-by: Eunji, Lee <eunjieji.lee@samsung.com>
-rwxr-xr-x | src/atrace/atrace.cpp | 16 | ||||
-rwxr-xr-x | src/ttrace.c | 77 |
2 files changed, 64 insertions, 29 deletions
diff --git a/src/atrace/atrace.cpp b/src/atrace/atrace.cpp index 05e1506..c80cf06 100755 --- a/src/atrace/atrace.cpp +++ b/src/atrace/atrace.cpp @@ -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); diff --git a/src/ttrace.c b/src/ttrace.c index e89fc41..c255194 100755 --- a/src/ttrace.c +++ b/src/ttrace.c @@ -52,6 +52,11 @@ #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, ...) |