xref: /optee_os/lib/libutee/trace_ext.c (revision 8e81e2f5366a971afdd2ac47fb8529d1def5feb0)
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  * printf and puts - stdio printf support
50  *
51  * 'printf()' and 'puts()' traces have the 'info' trace level.
52  */
53 int printf(const char *fmt, ...)
54 {
55 	char to_format[MAX_PRINT_SIZE];
56 	va_list ap;
57 	int s;
58 
59 	if (trace_get_level() < TRACE_PRINTF_LEVEL)
60 		return 0;
61 
62 	va_start(ap, fmt);
63 	s = vsnprintf(to_format, sizeof(to_format), fmt, ap);
64 	va_end(ap);
65 
66 	if (s < 0)
67 		return s;
68 
69 	trace_ext_puts(to_format);
70 
71 	return s;
72 }
73 
74 int puts(const char *str)
75 {
76 	if (trace_get_level() >= TRACE_PRINTF_LEVEL) {
77 		trace_ext_puts(str);
78 		trace_ext_puts("\n");
79 	}
80 	return 1;
81 }
82 
83 int putchar(int c)
84 {
85 	char str[2] = { (char)c, '\0' };
86 
87 	if (trace_get_level() >= TRACE_PRINTF_LEVEL)
88 		trace_ext_puts(str);
89 	/*
90 	 * From the putchar() man page:
91 	 * "fputc(), putc() and putchar() return the character written as an
92 	 * unsigned char cast to an int or EOF on error."
93 	 */
94 	return (int)(unsigned char)c;
95 }
96 
97 #else
98 
99 int printf(const char *fmt __unused, ...)
100 {
101 	return 0;
102 }
103 
104 int puts(const char *str __unused)
105 {
106 	return 0;
107 }
108 
109 int putchar(int c)
110 {
111 	return (int)(unsigned char)c;
112 }
113 
114 #endif
115