tizen: Add trace api for using ttrace
authorEunji, Lee <eunjieji.lee@samsung.com>
Wed, 11 Jan 2017 09:02:48 +0000 (18:02 +0900)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Tue, 31 Jan 2017 02:46:31 +0000 (02:46 +0000)
android: Add trace api for using ftrace

This patch includes implementation of alternative trace api set for android.
Since Atrace (android trace) api is available at android level 23,
we implement trace api by using ftrace directly.

Change-Id: I5370a190f340816934b77a83159c7c7ba9c8a17e
Signed-off-by: Eunji, Lee <eunjieji.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16467
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
12 files changed:
build_common/tizen/SConscript
resource/csdk/connectivity/build/tizen/Makefile
resource/csdk/connectivity/build/tizen/packaging/com.oic.ca.spec
resource/csdk/connectivity/build/tizen/scons/SConscript
resource/csdk/connectivity/src/camessagehandler.c
resource/csdk/logger/SConscript
resource/csdk/logger/include/trace.h [new file with mode: 0755]
resource/csdk/logger/src/trace.c [new file with mode: 0755]
resource/csdk/stack/samples/tizen/build/packaging/com.oic.ri.spec
resource/csdk/stack/samples/tizen/build/scons/SConscript
resource/csdk/stack/src/ocstack.c
tools/tizen/iotivity.spec

index 88e95dd..c3c4feb 100644 (file)
@@ -23,6 +23,6 @@ env.AppendUnique(CCFLAGS = ['-Wextra', '-fPIC'])
 env.AppendUnique(LIBS = ['dl', 'pthread'])
 
 env.AppendUnique(CCFLAGS = ['-D__TIZEN__', '-D_GNU_SOURCE', '-DTIZEN_DEBUG_ENABLE'])
-env.ParseConfig("pkg-config dlog --cflags --libs")
+env.ParseConfig("pkg-config dlog ttrace --cflags --libs")
 
 # Set arch flags : It will be handled by Tizen build system.
index 726faee..8df2869 100644 (file)
@@ -24,7 +24,7 @@ TINYDTLS_OBJ_DIR=./lib/extlibs/tinydtls/obj
 # TODO : Currently Hardcoded WIFI_ADAPTER. Change it to generic
 CFLAGS.debug = -DWITH_POSIX -D__TIZEN__ -Wall -std=c99 -DSLP_SDK_LOG -g -D_GNU_SOURCE -DTIZEN_DEBUG_ENABLE -DTB_LOG $(ADAPTER_MACRO)
 CFLAGS.release = -DWITH_POSIX -D__TIZEN__ -Wall -std=c99 -DSLP_SDK_LOG -D_GNU_SOURCE -DTIZEN_DEBUG_ENABLE -DTB_LOG $(ADAPTER_MACRO)
-COMPILEFLAG = `pkg-config --cflags --libs capi-network-wifi dlog capi-network-bluetooth glib-2.0`
+COMPILEFLAG = `pkg-config --cflags --libs capi-network-wifi dlog ttrace capi-network-bluetooth glib-2.0`
 
 # Include files will be copied into a single folder on gbsbuild.
 # NO NEED to mention the path explicitly.
index bc71ac8..6af4481 100644 (file)
@@ -30,6 +30,7 @@ Source0: http://mirrors.kernel.org/%{name}/%{version}/%{name}-%{version}.tar.gz
 %{!?WITH_TCP: %define WITH_TCP 0}
 
 BuildRequires: pkgconfig(dlog)
+BuildRequires: pkgconfig(ttrace)
 BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(capi-network-connection)
 BuildRequires: pkgconfig(capi-network-wifi)
index 88be35d..e05eb3b 100644 (file)
@@ -20,7 +20,7 @@ else:
 if env.get('LOGGING'):
     env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 
-env.ParseConfig("pkg-config --cflags --libs capi-network-wifi capi-network-connection dlog glib-2.0")
+env.ParseConfig("pkg-config --cflags --libs capi-network-wifi capi-network-connection dlog ttrace glib-2.0")
 if 'ALL' in transport:
                env.AppendUnique(CPPDEFINES = ['WIFI_ADAPTER', 'NO_ETHERNET_ADAPTER','EDR_ADAPTER','LE_ADAPTER'])
                print "CA Transport is ALL"
index 44869ee..81694f8 100644 (file)
@@ -28,6 +28,7 @@
 #include "caremotehandler.h"
 #include "caprotocolmessage.h"
 #include "logger.h"
+#include "trace.h"
 #ifndef WITH_UPSTREAM_LIBCOAP
 #include "coap/config.h"
 #endif
@@ -499,6 +500,8 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
     VERIFY_NON_NULL(data, TAG, "data");
     VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");
 
+    OIC_TRACE_BEGIN(%s:CAProcessSendData, TAG);
+
     CAResult_t res = CA_STATUS_FAILED;
 
     CASendDataType_t type = data->type;
@@ -545,6 +548,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
         else
         {
             OIC_LOG(DEBUG, TAG, "request info, response info is empty");
+            OIC_TRACE_END();
             return CA_STATUS_INVALID_PARAM;
         }
 
@@ -566,6 +570,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
                         CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
                         coap_delete_list(options);
                         coap_delete_pdu(pdu);
+                        OIC_TRACE_END();
                         return res;
                     }
                 }
@@ -581,6 +586,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
                 CAErrorHandler(data->remoteEndpoint, pdu->transport_hdr, pdu->length, res);
                 coap_delete_list(options);
                 coap_delete_pdu(pdu);
+                OIC_TRACE_END();
                 return res;
             }
 
@@ -606,6 +612,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
                     OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
                     coap_delete_list(options);
                     coap_delete_pdu(pdu);
+                    OIC_TRACE_END();
                     return res;
                 }
             }
@@ -617,6 +624,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
         {
             OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
             CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
+            OIC_TRACE_END();
             return CA_SEND_FAILED;
         }
     }
@@ -654,7 +662,7 @@ static CAResult_t CAProcessSendData(const CAData_t *data)
         CAProcessMulticastData(data);
 #endif
     }
-
+    OIC_TRACE_END();
     return CA_STATUS_OK;
 }
 
@@ -737,9 +745,12 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
     VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
     VERIFY_NON_NULL_VOID(data, TAG, "data");
 
+    OIC_TRACE_BEGIN(%s:CAReceivedPacketCallback, TAG);
+
     if (0 == dataLen)
     {
         OIC_LOG(ERROR, TAG, "dataLen is zero");
+        OIC_TRACE_END();
         return;
     }
 
@@ -841,6 +852,8 @@ static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
 exit:
     OIC_LOG(DEBUG, TAG, "received pdu data :");
     OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);
+
+    OIC_TRACE_END();
 }
 
 void CAHandleRequestResponseCallbacks()
index b81fb7e..e3e4c12 100644 (file)
@@ -28,7 +28,7 @@ if env.get('TARGET_OS') in ['arduino']:
        Command("./src/logger.cpp", "./src/logger.c", Copy("$TARGET", "$SOURCE"))
        logger_src = ['./src/logger.cpp']
 else:
-       logger_src = ['./src/logger.c']
+       logger_src = ['./src/logger.c', './src/trace.c']
 
 loggerlib = local_env.StaticLibrary('logger', logger_src)
 local_env.InstallTarget(loggerlib, 'logger')
diff --git a/resource/csdk/logger/include/trace.h b/resource/csdk/logger/include/trace.h
new file mode 100755 (executable)
index 0000000..1083ecf
--- /dev/null
@@ -0,0 +1,64 @@
+//******************************************************************
+//
+// Copyright 2017 Samsung Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef TRACE_H_
+#define TRACE_H_
+
+#ifdef __ANDROID__
+#include "logger.h"
+#elif defined(__TIZEN__)
+#include <ttrace.h>
+#elif defined(ARDUINO)
+#endif
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+#ifdef __ANDROID__
+
+#define OIC_TRACE_BEGIN(MSG, ...) \
+        oic_trace_begin("OIC:"#MSG, ##__VA_ARGS__)
+#define OIC_TRACE_END() \
+        oic_trace_end()
+
+#elif defined(__TIZEN__)
+/* trace macro for Tizen. this will call ttrace api internally*/
+#define OIC_TRACE_BEGIN(MSG, ...) \
+        traceBegin(TTRACE_TAG_APP, "IOTIVITY:"#MSG, ##__VA_ARGS__)
+#define OIC_TRACE_END() \
+        traceEnd(TTRACE_TAG_APP)
+
+#elif defined(ARDUINO)
+/* trace macro for Arduino. currently this will call nothing*/
+#define OIC_TRACE_BEGIN(MSG, ...)
+#define OIC_TRACE_END()
+
+#else
+#define OIC_TRACE_BEGIN(MSG, ...)
+#define OIC_TRACE_END()
+
+#endif //endif
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif /* TRACE_H_ */
diff --git a/resource/csdk/logger/src/trace.c b/resource/csdk/logger/src/trace.c
new file mode 100755 (executable)
index 0000000..47a626f
--- /dev/null
@@ -0,0 +1,200 @@
+//******************************************************************
+//
+// Copyright 2017 Samsung Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+// Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
+// causes header files to expose definitions
+// corresponding to the POSIX.1b, Real-time extensions
+// (IEEE Std 1003.1b-1993) specification
+//
+// For this specific file, see use of clock_gettime,
+// Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
+// and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
+
+#include "iotivity_config.h"
+
+// Pull in _POSIX_TIMERS feature test macro to check for
+// clock_gettime() support.
+#ifndef __TIZEN__
+
+#include "trace.h"
+#include "logger.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#elif defined(HAVE_STRINGS_H)
+#include <strings.h>
+#endif
+
+#define FD_INITIAL_VALUE  -1
+#define FD_NOT_EXIST    -2
+#define MAX_BUF_SIZE    4096
+#define MAX_TRACE_LEN   524
+#define MAX_HEAD_LEN    8
+#define MAX_TAIL_LEN    13
+#define POS_LABEL_ST    ((MAX_TRACE_LEN - MAX_HEAD_LEN))
+#define POS_LABEL_ED    ((MAX_TRACE_LEN - MAX_TAIL_LEN))
+
+#define TAG "OIC_TRACER"
+
+#ifdef __ANDROID__
+/*
+* Currently android api level 21 is used for building iotivity project.
+* Since Atrace (aka. android trace) API has been provided by NDK above android api level 23,
+* we use ftrace directly as workaround to Atrace API until android build level is upgraded
+*/
+int g_trace_marker_hd=FD_INITIAL_VALUE;
+
+int oic_trace_init()
+{
+    OIC_LOG(INFO, TAG, "entering oic_trace_init");
+    int mounts;
+    char buf[MAX_BUF_SIZE] = {0};
+    ssize_t buflen;
+    char *line, *tmp1, *path = NULL;
+
+    if(g_trace_marker_hd == FD_INITIAL_VALUE)
+    {
+        mounts = open("/proc/mounts", O_RDONLY);
+        if (mounts < 0)
+        {
+            OIC_LOG(INFO, TAG, "no /proc/mounts");
+            return -1;
+        }
+
+        buflen = read(mounts, buf, sizeof(buf) - 1);
+        close(mounts);
+
+        if (buflen < 0)
+        {
+            OIC_LOG(INFO, TAG, "failed to read /proc/mounts");
+            return -1;
+        }
+
+        line = strtok_r(buf, "\n", &tmp1);
+        while (line)
+        {
+            char *tmp2, *tmp_path, *fstype;
+            /* "<dev> <mountpoint> <fs type> ..." */
+            strtok_r(line, " ", &tmp2);
+            tmp_path = strtok_r(NULL, " ", &tmp2);
+            fstype = strtok_r(NULL, " ", &tmp2);
+
+            if (strcmp(fstype, "debugfs") == 0)
+            {
+                path = tmp_path;
+                break;
+            }
+            line = strtok_r(NULL, "\n", &tmp1);
+        }
+
+        if (NULL == path)
+        {
+            OIC_LOG(INFO, TAG,  "debugfs mountpoint not found");
+            return -1;
+        }
+
+        snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
+        g_trace_marker_hd = open(buf, O_WRONLY);
+        if (g_trace_marker_hd < 0)
+        {
+            OIC_LOG_V(INFO, TAG, "failed to open trace_marker file: %s (%d)", strerror(errno), errno);
+            return -1;
+        }
+    }
+    OIC_LOG_V(INFO, TAG,  "exit oic_trace_init with: %d", g_trace_marker_hd);
+    return 0;
+}
+
+void oic_trace_begin(const char *name, ...)
+{
+    if (g_trace_marker_hd == FD_INITIAL_VALUE)
+    {
+        oic_trace_init();
+    }
+
+    if (g_trace_marker_hd > 0)
+    {
+        char buf[MAX_TRACE_LEN]={0};
+        int len = MAX_HEAD_LEN, ret = 0;
+        va_list ap;
+
+        va_start(ap, name);
+        snprintf(buf, MAX_TRACE_LEN, "B|%5d|", getpid());
+        len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
+        va_end(ap);
+
+        if (len > MAX_TRACE_LEN)
+        {
+            len = MAX_TRACE_LEN - 1;
+        }
+
+        ret = write(g_trace_marker_hd, buf, len);
+
+        if (ret < 0)
+        {
+            OIC_LOG_V(INFO, TAG, "error writing, len: %d, ret: %d, errno: %d at oic_trace_begin",
+                      len, ret, errno);
+        }
+    }
+    else
+    {
+        OIC_LOG_V(INFO, TAG, "oic_trace_begin: invalid fd: %d", g_trace_marker_hd);
+    }
+}
+
+void oic_trace_end()
+{
+    if (FD_INITIAL_VALUE == g_trace_marker_hd)
+    {
+        oic_trace_init();
+    }
+
+    if (g_trace_marker_hd > 0)
+    {
+        int ret = 0, len = 1;
+        char end = 'E';
+
+        ret = write(g_trace_marker_hd, &end, len);
+        if (ret < 0)
+        {
+            OIC_LOG_V(INFO, TAG, "error writing, len: %d, ret: %d, errno: %d at oic_trace_end",
+                      len, ret, errno);
+        }
+    }
+    else
+    {
+        OIC_LOG_V(INFO, TAG, "oic_trace_end: invalid fd: %d", g_trace_marker_hd);
+    }
+}
+
+#elif defined ARDUINO
+/* TODO: Trace api for ARDUINO and others will be implemented */
+#endif //ARDUINO
+
+#endif // #ifndef __TIZEN__
index 4cd6d9a..9ae9c61 100644 (file)
@@ -32,6 +32,7 @@ Source0: http://mirrors.kernel.org/%{name}/%{version}/%{name}-%{version}.tar.gz
 %{!?WITH_TCP: %define WITH_TCP 0}
 
 BuildRequires: pkgconfig(dlog)
+BuildRequires: pkgconfig(ttrace)
 BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(capi-network-connection)
 BuildRequires: pkgconfig(capi-network-wifi)
index 24750a6..a65c554 100644 (file)
@@ -20,7 +20,7 @@ else:
 if env.get('LOGGING'):
     env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 
-env.ParseConfig("pkg-config --cflags --libs capi-network-wifi capi-network-connection dlog glib-2.0")
+env.ParseConfig("pkg-config --cflags --libs capi-network-wifi capi-network-connection dlog ttrace glib-2.0")
 if 'ALL' in transport:
                env.AppendUnique(CPPDEFINES = ['WIFI_ADAPTER', 'NO_ETHERNET_ADAPTER','EDR_ADAPTER','LE_ADAPTER'])
                print "CA Transport is ALL"
index 08ca676..560c9eb 100644 (file)
@@ -50,6 +50,7 @@
 #include "oic_malloc.h"
 #include "oic_string.h"
 #include "logger.h"
+#include "trace.h"
 #include "ocserverrequest.h"
 #include "secureresourcemanager.h"
 #include "psinterface.h"
@@ -1649,7 +1650,7 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res
     VERIFY_NON_NULL_NR(responseInfo, FATAL);
 
     OIC_LOG(INFO, TAG, "Enter HandleCAResponses");
-
+    OIC_TRACE_BEGIN(%s:HandleCAResponses, TAG);
 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
 #ifdef ROUTING_GATEWAY
     bool needRIHandling = false;
@@ -1664,6 +1665,7 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res
     if(ret != OC_STACK_OK || !needRIHandling)
     {
         OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
+        OIC_TRACE_END();
         return;
     }
 #endif
@@ -1681,6 +1683,7 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res
     OCHandleResponse(endPoint, responseInfo);
 
     OIC_LOG(INFO, TAG, "Exit HandleCAResponses");
+    OIC_TRACE_END();
 }
 
 /*
@@ -1693,6 +1696,7 @@ void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *er
     VERIFY_NON_NULL_NR(errorInfo, FATAL);
 
     OIC_LOG(INFO, TAG, "Enter HandleCAErrorResponse");
+    OIC_TRACE_BEGIN(%s:HandleCAErrorResponse, TAG);
 
     ClientCB *cbNode = GetClientCB(errorInfo->info.token,
                                    errorInfo->info.tokenLength, NULL, NULL);
@@ -1734,6 +1738,7 @@ void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *er
     }
 
     OIC_LOG(INFO, TAG, "Exit HandleCAErrorResponse");
+    OIC_TRACE_END();
 }
 
 /*
@@ -2130,15 +2135,18 @@ void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
 void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
 {
     OIC_LOG(INFO, TAG, "Enter HandleCARequests");
+    OIC_TRACE_BEGIN(%s:HandleCARequests, TAG);
     if (!endPoint)
     {
         OIC_LOG(ERROR, TAG, "endPoint is NULL");
+        OIC_TRACE_END();
         return;
     }
 
     if (!requestInfo)
     {
         OIC_LOG(ERROR, TAG, "requestInfo is NULL");
+        OIC_TRACE_END();
         return;
     }
 
@@ -2158,6 +2166,7 @@ void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
     if (OC_STACK_OK != ret || !needRIHandling)
     {
         OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
+        OIC_TRACE_END();
         return;
     }
 #endif
@@ -2193,6 +2202,7 @@ void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
         OCHandleRequests(endPoint, requestInfo);
     }
     OIC_LOG(INFO, TAG, "Exit HandleCARequests");
+    OIC_TRACE_END();
 }
 
 //-----------------------------------------------------------------------------
index e8a9c26..dd2c376 100644 (file)
@@ -97,6 +97,7 @@ BuildRequires:  pkgconfig(sqlite3)
 %if "%{TARGET_OS}" == "tizen"
 BuildRequires:  gettext-tools
 BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(ttrace)
 BuildRequires:  pkgconfig(capi-network-connection)
 BuildRequires:  pkgconfig(capi-network-wifi)
 BuildRequires:  pkgconfig(capi-network-bluetooth) >= 0.1.52