xref: /optee_os/lib/libutee/trace_ext.c (revision 5a913ee74d3c71af2a2860ce8a4e7aeab2916f9b)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <string_ext.h>
10 #include <trace.h>
11 #include <util.h>
12 #include <utee_syscalls.h>
13 
14 #if TRACE_LEVEL > 0
15 
16 void trace_ext_puts(const char *str)
17 {
18 	utee_log(str, strlen(str));
19 }
20 
21 int trace_ext_get_thread_id(void)
22 {
23 	return -1;
24 }
25 
26 /*
27  * printf and puts - stdio printf support
28  *
29  * 'printf()' and 'puts()' traces have the 'info' trace level.
30  */
31 int printf(const char *fmt, ...)
32 {
33 	char to_format[MAX_PRINT_SIZE];
34 	va_list ap;
35 	int s;
36 
37 	if (trace_get_level() < TRACE_PRINTF_LEVEL)
38 		return 0;
39 
40 	va_start(ap, fmt);
41 	s = vsnprintf(to_format, sizeof(to_format), fmt, ap);
42 	va_end(ap);
43 
44 	if (s < 0)
45 		return s;
46 
47 	trace_ext_puts(to_format);
48 
49 	return s;
50 }
51 
52 int puts(const char *str)
53 {
54 	if (trace_get_level() >= TRACE_PRINTF_LEVEL) {
55 		trace_ext_puts(str);
56 		trace_ext_puts("\n");
57 	}
58 	return 1;
59 }
60 
61 int putchar(int c)
62 {
63 	char str[2] = { (char)c, '\0' };
64 
65 	if (trace_get_level() >= TRACE_PRINTF_LEVEL)
66 		trace_ext_puts(str);
67 	/*
68 	 * From the putchar() man page:
69 	 * "fputc(), putc() and putchar() return the character written as an
70 	 * unsigned char cast to an int or EOF on error."
71 	 */
72 	return (int)(unsigned char)c;
73 }
74 
75 #else
76 
77 int printf(const char *fmt __unused, ...)
78 {
79 	return 0;
80 }
81 
82 int puts(const char *str __unused)
83 {
84 	return 0;
85 }
86 
87 int putchar(int c)
88 {
89 	return (int)(unsigned char)c;
90 }
91 
92 #endif
93