1 //******************************************************************
3 // Copyright 2017 Samsung Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "iotivity_config.h"
24 #if (defined(__ANDROID__)) || (defined(__TIZEN__) && defined(OIC_SUPPORT_TIZEN_TRACE))
26 #define MAX_BUFFER_SIZE 8
27 #define MAX_LINE_LEN ((MAX_BUFFER_SIZE) * 2) + 1
29 void oic_trace_buffer(const char *name, const uint8_t * buffer, size_t bufferSize)
31 if (!name || !buffer || (0 == bufferSize))
36 char lineBuffer[MAX_LINE_LEN] = {0};
37 size_t count = (MAX_BUFFER_SIZE > bufferSize) ? bufferSize : MAX_BUFFER_SIZE;
38 size_t remainSize = MAX_LINE_LEN;
40 char* buf = &lineBuffer[0];
42 for (size_t i = 0; i < count; i++)
44 writtenSize = snprintf(buf, remainSize, "%02x", buffer[i]);
53 OIC_TRACE_BEGIN(%s:%s, name, lineBuffer);
71 #elif defined(HAVE_STRINGS_H)
75 #define FD_INITIAL_VALUE -1
76 #define FD_NOT_EXIST -2
77 #define MAX_BUF_SIZE 4096
78 #define MAX_TRACE_LEN 524
79 #define MAX_HEAD_LEN 8
80 #define MAX_TAIL_LEN 13
81 #define POS_LABEL_ST ((MAX_TRACE_LEN - MAX_HEAD_LEN))
82 #define POS_LABEL_ED ((MAX_TRACE_LEN - MAX_TAIL_LEN))
84 #define TAG "OIC_TRACER"
88 * Currently android api level 21 is used for building iotivity project.
89 * Since Atrace (aka. android trace) API has been provided by NDK above android api level 23,
90 * we use ftrace directly as workaround to Atrace API until android build level is upgraded
92 int g_trace_marker_hd = FD_INITIAL_VALUE;
96 OIC_LOG(INFO, TAG, "entering oic_trace_init");
98 char buf[MAX_BUF_SIZE] = {0};
100 char *line = NULL, *tmp1 = NULL, *path = NULL;
102 if(g_trace_marker_hd == FD_INITIAL_VALUE)
104 mounts = open("/proc/mounts", O_RDONLY);
107 OIC_LOG(INFO, TAG, "no /proc/mounts");
111 buflen = read(mounts, buf, sizeof(buf) - 1);
116 OIC_LOG(INFO, TAG, "failed to read /proc/mounts");
120 line = strtok_r(buf, "\n", &tmp1);
123 char *tmp2 = NULL, *tmp_path = NULL, *fstype = NULL;
124 /* "<dev> <mountpoint> <fs type> ..." */
125 strtok_r(line, " ", &tmp2);
126 tmp_path = strtok_r(NULL, " ", &tmp2);
127 fstype = strtok_r(NULL, " ", &tmp2);
129 if ((fstype != NULL) && (strcmp(fstype, "debugfs") == 0))
134 line = strtok_r(NULL, "\n", &tmp1);
139 OIC_LOG(INFO, TAG, "debugfs mountpoint not found");
143 snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
144 g_trace_marker_hd = open(buf, O_WRONLY);
145 if (g_trace_marker_hd < 0)
147 OIC_LOG_V(INFO, TAG, "failed to open trace_marker file: %s (%d)",
148 strerror(errno), errno);
152 OIC_LOG_V(INFO, TAG, "exit oic_trace_init with: %d", g_trace_marker_hd);
153 return g_trace_marker_hd;
156 void oic_trace_begin(const char *name, ...)
158 if (g_trace_marker_hd == FD_INITIAL_VALUE)
163 if (g_trace_marker_hd > 0)
165 char buf[MAX_TRACE_LEN] = {0};
166 int len = MAX_HEAD_LEN, ret = 0;
170 snprintf(buf, MAX_TRACE_LEN, "B|%5d|", getpid());
171 len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
174 if (len > MAX_TRACE_LEN)
176 len = MAX_TRACE_LEN - 1;
179 ret = write(g_trace_marker_hd, buf, len);
183 OIC_LOG_V(INFO, TAG, "error writing, len: %d, ret: %d, errno: %d at oic_trace_begin",
189 OIC_LOG_V(INFO, TAG, "oic_trace_begin: invalid fd: %d", g_trace_marker_hd);
195 if (FD_INITIAL_VALUE == g_trace_marker_hd)
200 if (g_trace_marker_hd > 0)
202 int ret = 0, len = 1;
205 ret = write(g_trace_marker_hd, &end, len);
208 OIC_LOG_V(INFO, TAG, "error writing, len: %d, ret: %d, errno: %d at oic_trace_end",
214 OIC_LOG_V(INFO, TAG, "oic_trace_end: invalid fd: %d", g_trace_marker_hd);
218 #elif defined ARDUINO
219 /* TODO: Trace api for ARDUINO and others will be implemented */
222 #endif // #ifndef __TIZEN__