replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / logger / src / trace.c
1 //******************************************************************
2 //
3 // Copyright 2017 Samsung Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include <stdio.h>
21 #include "iotivity_config.h"
22 #include "trace.h"
23
24 #if (defined(__ANDROID__)) || (defined(__TIZEN__) && defined(OIC_SUPPORT_TIZEN_TRACE))
25
26 #define MAX_BUFFER_SIZE 8
27 #define MAX_LINE_LEN ((MAX_BUFFER_SIZE) * 2) + 1
28
29 void oic_trace_buffer(const char *name, const uint8_t * buffer, size_t bufferSize)
30 {
31     if (!name || !buffer || (0 == bufferSize))
32     {
33         return;
34     }
35
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;
39     int writtenSize = 0;
40     char* buf = &lineBuffer[0];
41
42     for (size_t i = 0; i < count; i++)
43     {
44         writtenSize = snprintf(buf, remainSize, "%02x", buffer[i]);
45         if (2 != writtenSize)
46         {
47             break;
48         }
49         buf += writtenSize;
50         remainSize -= 2;
51     }
52
53     OIC_TRACE_BEGIN(%s:%s, name, lineBuffer);
54     OIC_TRACE_END();
55 }
56
57 #endif
58
59 #ifndef __TIZEN__
60
61 #include "logger.h"
62
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66 #ifdef HAVE_FCNTL_H
67 #include <fcntl.h>
68 #endif
69 #ifdef HAVE_STRING_H
70 #include <string.h>
71 #elif defined(HAVE_STRINGS_H)
72 #include <strings.h>
73 #endif
74
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))
83
84 #define TAG "OIC_TRACER"
85
86 #ifdef __ANDROID__
87 /*
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
91 */
92 int g_trace_marker_hd = FD_INITIAL_VALUE;
93
94 int oic_trace_init()
95 {
96     OIC_LOG(INFO, TAG, "entering oic_trace_init");
97     int mounts = -1;
98     char buf[MAX_BUF_SIZE] = {0};
99     ssize_t buflen = -1;
100     char *line = NULL, *tmp1 = NULL, *path = NULL;
101
102     if(g_trace_marker_hd == FD_INITIAL_VALUE)
103     {
104         mounts = open("/proc/mounts", O_RDONLY);
105         if (mounts < 0)
106         {
107             OIC_LOG(INFO, TAG, "no /proc/mounts");
108             return -1;
109         }
110
111         buflen = read(mounts, buf, sizeof(buf) - 1);
112         close(mounts);
113
114         if (buflen < 0)
115         {
116             OIC_LOG(INFO, TAG, "failed to read /proc/mounts");
117             return -1;
118         }
119
120         line = strtok_r(buf, "\n", &tmp1);
121         while (line)
122         {
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);
128
129             if ((fstype != NULL) && (strcmp(fstype, "debugfs") == 0))
130             {
131                 path = tmp_path;
132                 break;
133             }
134             line = strtok_r(NULL, "\n", &tmp1);
135         }
136
137         if (NULL == path)
138         {
139             OIC_LOG(INFO, TAG,  "debugfs mountpoint not found");
140             return -1;
141         }
142
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)
146         {
147             OIC_LOG_V(INFO, TAG, "failed to open trace_marker file: %s (%d)",
148                       strerror(errno), errno);
149             return -1;
150         }
151     }
152     OIC_LOG_V(INFO, TAG,  "exit oic_trace_init with: %d", g_trace_marker_hd);
153     return g_trace_marker_hd;
154 }
155
156 void oic_trace_begin(const char *name, ...)
157 {
158     if (g_trace_marker_hd == FD_INITIAL_VALUE)
159     {
160         oic_trace_init();
161     }
162
163     if (g_trace_marker_hd > 0)
164     {
165         char buf[MAX_TRACE_LEN] = {0};
166         int len = MAX_HEAD_LEN, ret = 0;
167         va_list ap;
168
169         va_start(ap, name);
170         snprintf(buf, MAX_TRACE_LEN, "B|%5d|", getpid());
171         len += vsnprintf(buf + MAX_HEAD_LEN, POS_LABEL_ST, name, ap);
172         va_end(ap);
173
174         if (len > MAX_TRACE_LEN)
175         {
176             len = MAX_TRACE_LEN - 1;
177         }
178
179         ret = write(g_trace_marker_hd, buf, len);
180
181         if (ret < 0)
182         {
183             OIC_LOG_V(INFO, TAG, "error writing, len: %d, ret: %d, errno: %d at oic_trace_begin",
184                       len, ret, errno);
185         }
186     }
187     else
188     {
189         OIC_LOG_V(INFO, TAG, "oic_trace_begin: invalid fd: %d", g_trace_marker_hd);
190     }
191 }
192
193 void oic_trace_end()
194 {
195     if (FD_INITIAL_VALUE == g_trace_marker_hd)
196     {
197         oic_trace_init();
198     }
199
200     if (g_trace_marker_hd > 0)
201     {
202         int ret = 0, len = 1;
203         char end = 'E';
204
205         ret = write(g_trace_marker_hd, &end, len);
206         if (ret < 0)
207         {
208             OIC_LOG_V(INFO, TAG, "error writing, len: %d, ret: %d, errno: %d at oic_trace_end",
209                       len, ret, errno);
210         }
211     }
212     else
213     {
214         OIC_LOG_V(INFO, TAG, "oic_trace_end: invalid fd: %d", g_trace_marker_hd);
215     }
216 }
217
218 #elif defined ARDUINO
219 /* TODO: Trace api for ARDUINO and others will be implemented */
220 #endif //ARDUINO
221
222 #endif // #ifndef __TIZEN__