xref: /optee_os/lib/libutee/trace_ext.c (revision 4de4bebcfbe3c5c1884a2ff2c66fa2aa5dede662)
1*4de4bebcSJens Wiklander /*
2*4de4bebcSJens Wiklander  * Copyright (c) 2014, STMicroelectronics International N.V.
3*4de4bebcSJens Wiklander  * All rights reserved.
4*4de4bebcSJens Wiklander  *
5*4de4bebcSJens Wiklander  * Redistribution and use in source and binary forms, with or without
6*4de4bebcSJens Wiklander  * modification, are permitted provided that the following conditions are met:
7*4de4bebcSJens Wiklander  *
8*4de4bebcSJens Wiklander  * 1. Redistributions of source code must retain the above copyright notice,
9*4de4bebcSJens Wiklander  * this list of conditions and the following disclaimer.
10*4de4bebcSJens Wiklander  *
11*4de4bebcSJens Wiklander  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*4de4bebcSJens Wiklander  * this list of conditions and the following disclaimer in the documentation
13*4de4bebcSJens Wiklander  * and/or other materials provided with the distribution.
14*4de4bebcSJens Wiklander  *
15*4de4bebcSJens Wiklander  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*4de4bebcSJens Wiklander  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*4de4bebcSJens Wiklander  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*4de4bebcSJens Wiklander  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19*4de4bebcSJens Wiklander  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*4de4bebcSJens Wiklander  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*4de4bebcSJens Wiklander  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*4de4bebcSJens Wiklander  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*4de4bebcSJens Wiklander  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*4de4bebcSJens Wiklander  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*4de4bebcSJens Wiklander  * POSSIBILITY OF SUCH DAMAGE.
26*4de4bebcSJens Wiklander  */
27*4de4bebcSJens Wiklander 
28*4de4bebcSJens Wiklander #include <stdio.h>
29*4de4bebcSJens Wiklander #include <stdarg.h>
30*4de4bebcSJens Wiklander #include <string.h>
31*4de4bebcSJens Wiklander #include <string_ext.h>
32*4de4bebcSJens Wiklander #include <trace.h>
33*4de4bebcSJens Wiklander #include <util.h>
34*4de4bebcSJens Wiklander #include <utee_syscalls.h>
35*4de4bebcSJens Wiklander 
36*4de4bebcSJens Wiklander #if CFG_TRACE_LEVEL > 0
37*4de4bebcSJens Wiklander 
38*4de4bebcSJens Wiklander const char trace_ext_prefix[] = "USER-TA";
39*4de4bebcSJens Wiklander 
40*4de4bebcSJens Wiklander void trace_ext_puts(bool sync __unused, const char *str)
41*4de4bebcSJens Wiklander {
42*4de4bebcSJens Wiklander 	utee_log(str, strlen(str));
43*4de4bebcSJens Wiklander }
44*4de4bebcSJens Wiklander 
45*4de4bebcSJens Wiklander int trace_ext_get_thread_id(void)
46*4de4bebcSJens Wiklander {
47*4de4bebcSJens Wiklander 	return -1;
48*4de4bebcSJens Wiklander }
49*4de4bebcSJens Wiklander 
50*4de4bebcSJens Wiklander 
51*4de4bebcSJens Wiklander /*
52*4de4bebcSJens Wiklander  * printf and puts - stdio printf support
53*4de4bebcSJens Wiklander  *
54*4de4bebcSJens Wiklander  * 'printf()' and 'puts()' traces have the 'info' trace level.
55*4de4bebcSJens Wiklander  * Traces are prefixed with string "[ta log] ".
56*4de4bebcSJens Wiklander  */
57*4de4bebcSJens Wiklander int printf(const char *fmt, ...)
58*4de4bebcSJens Wiklander {
59*4de4bebcSJens Wiklander 	char to_format[MAX_PRINT_SIZE];
60*4de4bebcSJens Wiklander 	static const char prefix[] = "[ta log] ";
61*4de4bebcSJens Wiklander 	static const char failed[] = "uta trace failed";
62*4de4bebcSJens Wiklander 	static const char trunc[] = "...\n";
63*4de4bebcSJens Wiklander 	va_list ap;
64*4de4bebcSJens Wiklander 	int s;
65*4de4bebcSJens Wiklander 
66*4de4bebcSJens Wiklander 	if (trace_get_level() <= TRACE_PRINTF_LEVEL)
67*4de4bebcSJens Wiklander 		return 0;
68*4de4bebcSJens Wiklander 
69*4de4bebcSJens Wiklander 	s = strlcpy(to_format, prefix, sizeof(to_format));
70*4de4bebcSJens Wiklander 	if ((unsigned int)s >= sizeof(to_format)) {
71*4de4bebcSJens Wiklander 		puts(failed);
72*4de4bebcSJens Wiklander 		return 0;
73*4de4bebcSJens Wiklander 	}
74*4de4bebcSJens Wiklander 
75*4de4bebcSJens Wiklander 	va_start(ap, fmt);
76*4de4bebcSJens Wiklander 	s = vsnprintf(to_format + s, sizeof(to_format) - s, fmt, ap);
77*4de4bebcSJens Wiklander 	va_end(ap);
78*4de4bebcSJens Wiklander 
79*4de4bebcSJens Wiklander 	if (s < 0) {
80*4de4bebcSJens Wiklander 		puts(failed);
81*4de4bebcSJens Wiklander 		return s;
82*4de4bebcSJens Wiklander 	}
83*4de4bebcSJens Wiklander 	if (((unsigned int)s >= (sizeof(to_format) - strlen(prefix)))) {
84*4de4bebcSJens Wiklander 		memcpy(&to_format[sizeof(to_format) - sizeof(trunc)], trunc,
85*4de4bebcSJens Wiklander 		       sizeof(trunc));
86*4de4bebcSJens Wiklander 		s = sizeof(to_format) - sizeof(prefix) - sizeof(trunc);
87*4de4bebcSJens Wiklander 	}
88*4de4bebcSJens Wiklander 
89*4de4bebcSJens Wiklander 	puts(to_format);
90*4de4bebcSJens Wiklander 
91*4de4bebcSJens Wiklander 	return s;
92*4de4bebcSJens Wiklander }
93*4de4bebcSJens Wiklander 
94*4de4bebcSJens Wiklander int puts(const char *str)
95*4de4bebcSJens Wiklander {
96*4de4bebcSJens Wiklander 	if (trace_get_level() > TRACE_PRINTF_LEVEL)
97*4de4bebcSJens Wiklander 		trace_ext_puts(false, str);
98*4de4bebcSJens Wiklander 	return 1;
99*4de4bebcSJens Wiklander }
100*4de4bebcSJens Wiklander 
101*4de4bebcSJens Wiklander 
102*4de4bebcSJens Wiklander 
103*4de4bebcSJens Wiklander #else
104*4de4bebcSJens Wiklander 
105*4de4bebcSJens Wiklander int printf(const char *fmt __unused, ...)
106*4de4bebcSJens Wiklander {
107*4de4bebcSJens Wiklander 	return 0;
108*4de4bebcSJens Wiklander }
109*4de4bebcSJens Wiklander 
110*4de4bebcSJens Wiklander int puts(const char *str __unused)
111*4de4bebcSJens Wiklander {
112*4de4bebcSJens Wiklander 	return 0;
113*4de4bebcSJens Wiklander }
114*4de4bebcSJens Wiklander #
115*4de4bebcSJens Wiklander 
116*4de4bebcSJens Wiklander #endif
117