summaryrefslogtreecommitdiff
path: root/src/ttrace.c
blob: 7b62bb77eaf88e733387bd93f966abfd05cbdc8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * =============================================================================
 *
 * SLP
 * Copyright (c) 2012 Samsung Electronics, Inc.
 * All rights reserved.
 *
 * This software is a confidential and proprietary information
 * of Samsung Electronics, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Samsung Electronics.
 *
 * @file:  ttrace.c
 * @brief:  ttrace
 * @author:  @samsung.com
 * @created:  Thursday 27 September 2012 08:05:23  KST
 * @compiler:  gcc
 * @company:  Samsung
 * @version:  0.1
 * @revision:  none
 *
 * =============================================================================
 */
#include "ttrace.h"
#include "trace.h"
#include "dlog.h"

#define ENABLE_TTRACE

#ifdef ENABLE_TTRACE
/* In "TTRACE_DEBUG" mode, dlog will print T-trace debug log */
#define TTRACE_DEBUG
#undef TTRACE_DEBUG

#ifdef TTRACE_DEBUG
#define TTRACE_LOG(format, arg...)	dlog_print(3, "TTRACE", format, ##arg)
#else
#define TTRACE_LOG(format, arg...)
#endif

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/mman.h>

#define TRACE_FILE "/sys/kernel/debug/tracing/trace_marker"

#define MAX_TRACE_LEN 1024
#define MAX_LEN 512
#define FD_INITIAL_VALUE -1
#define TRACE_FILE_NOT_EXIST -2

int g_trace_handle_fd = FD_INITIAL_VALUE;
int g_enabled_tag_fd = FD_INITIAL_VALUE;

static uint64_t dummy = 0;
uint64_t *cur_enabled_tag = (void *)&dummy;

static uint64_t traceInit()
{
	uint64_t *sm_for_enabled_tag;

	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));
			return 0;
		}
		cur_enabled_tag = sm_for_enabled_tag;
		TTRACE_LOG("cur_enabled_tag: %u %p", *cur_enabled_tag, cur_enabled_tag);

		return *cur_enabled_tag;
	} else {
		TTRACE_LOG("traceInit: %u", *cur_enabled_tag);
		return *cur_enabled_tag;
	}
}

static inline uint64_t isTagEnabled(uint64_t cur_tag)
{
	if (g_trace_handle_fd == TRACE_FILE_NOT_EXIST)
		return 0;
	/* if no tag is enabled, trace all tags. */
	cur_tag |= TTRACE_TAG_ALWAYS;

	if (g_trace_handle_fd < 0 || cur_enabled_tag == ((void *)&dummy))
		return (cur_tag & traceInit());

	return (cur_tag & *cur_enabled_tag);
}

/*
 * Tracing API for synchronous events: traceBegin()/traceEnd()
 * - tag: the tracing tag
 * - name: the event name
 */
void traceBegin(uint64_t tag, const char *name, ...)
{
	if (isTagEnabled(tag)) {
		char buf[MAX_LEN];
		int len = 0, 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);
		va_end(ap);
		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
		TTRACE_LOG("traceBegin:: disabled tag >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);

#endif

}

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);
		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
		TTRACE_LOG("traceEnd:: disabled tag >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);

#endif
}

/*
 * Tracing API for asynchronous events: traceAsyncBegin()/traceAsyncEnd()
 * - tag: the tracing tag
 * - name: the event name
 * - 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, ...)
{
	if (isTagEnabled(tag)) {
		char buf[MAX_LEN];
		int len = 0, 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);
		va_end(ap);
		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
		TTRACE_LOG("traceAsyncBegin:: disabled tag >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);

#endif
}

void traceAsyncEnd(uint64_t tag, int cookie, const char *name, ...)
{
	if (isTagEnabled(tag)) {
		char buf[MAX_LEN];
		int len = 0, 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);
		va_end(ap);
		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
		TTRACE_LOG("traceAsyncEnd:: disabled tag >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);

#endif
}

/*
 * Tracing API for marking occurrences of trace event: traceMark
 * - tag: the tracing tag
 * - name: the event name
 */
/* LCOV_EXCL_START */
void traceMark(uint64_t tag, const char *name, ...)
{
	if (isTagEnabled(tag)) {
		char buf[MAX_LEN], end = 'E';
		int len = 0, 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);
		va_end(ap);
		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
		TTRACE_LOG("traceMark:: disabled tag >> tag: %u tag_bit: %u", tag, *cur_enabled_tag);

#endif
}

/* LCOV_EXCL_STOP */
/*
 * Tracing API for tracing change of integer counter value: traceCounter
 * - tag: the tracing tag
 * - name: the event name
 * - value: the value tracing
 */
void traceCounter(uint64_t tag, int value, const char *name, ...)
{
	if (isTagEnabled(tag)) {
		char buf[MAX_LEN];
		int len = 0, 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);
		va_end(ap);
		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
		TTRACE_LOG("traceCounter:: disabled tag");

#endif
}

#else
void traceBegin(uint64_t tag, const char *name, ...)
{; }

void traceEnd(uint64_t tag)
{; }

void traceAsyncBegin(uint64_t tag, int cookie, const char *name, ...)
{; }

void traceAsyncEnd(uint64_t tag, int cookie, const char *name, ...)
{; }

void traceMark(uint64_t tag, const char *name, ...)
{; }

void traceCounter(uint64_t tag, int value, const char *name, ...)
{; }
#endif