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