xref: /optee_os/lib/libutee/trace_ext.c (revision 44202a48c5de6db267056151a60995d7ed171383)
14de4bebcSJens Wiklander /*
24de4bebcSJens Wiklander  * Copyright (c) 2014, STMicroelectronics International N.V.
34de4bebcSJens Wiklander  * All rights reserved.
44de4bebcSJens Wiklander  *
54de4bebcSJens Wiklander  * Redistribution and use in source and binary forms, with or without
64de4bebcSJens Wiklander  * modification, are permitted provided that the following conditions are met:
74de4bebcSJens Wiklander  *
84de4bebcSJens Wiklander  * 1. Redistributions of source code must retain the above copyright notice,
94de4bebcSJens Wiklander  * this list of conditions and the following disclaimer.
104de4bebcSJens Wiklander  *
114de4bebcSJens Wiklander  * 2. Redistributions in binary form must reproduce the above copyright notice,
124de4bebcSJens Wiklander  * this list of conditions and the following disclaimer in the documentation
134de4bebcSJens Wiklander  * and/or other materials provided with the distribution.
144de4bebcSJens Wiklander  *
154de4bebcSJens Wiklander  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
164de4bebcSJens Wiklander  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174de4bebcSJens Wiklander  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184de4bebcSJens Wiklander  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
194de4bebcSJens Wiklander  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
204de4bebcSJens Wiklander  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
214de4bebcSJens Wiklander  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
224de4bebcSJens Wiklander  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
234de4bebcSJens Wiklander  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
244de4bebcSJens Wiklander  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
254de4bebcSJens Wiklander  * POSSIBILITY OF SUCH DAMAGE.
264de4bebcSJens Wiklander  */
274de4bebcSJens Wiklander 
284de4bebcSJens Wiklander #include <stdio.h>
294de4bebcSJens Wiklander #include <stdarg.h>
304de4bebcSJens Wiklander #include <string.h>
314de4bebcSJens Wiklander #include <string_ext.h>
324de4bebcSJens Wiklander #include <trace.h>
334de4bebcSJens Wiklander #include <util.h>
344de4bebcSJens Wiklander #include <utee_syscalls.h>
354de4bebcSJens Wiklander 
364de4bebcSJens Wiklander #if CFG_TRACE_LEVEL > 0
374de4bebcSJens Wiklander 
384de4bebcSJens Wiklander const char trace_ext_prefix[] = "USER-TA";
394de4bebcSJens Wiklander 
404de4bebcSJens Wiklander void trace_ext_puts(bool sync __unused, const char *str)
414de4bebcSJens Wiklander {
424de4bebcSJens Wiklander 	utee_log(str, strlen(str));
434de4bebcSJens Wiklander }
444de4bebcSJens Wiklander 
454de4bebcSJens Wiklander int trace_ext_get_thread_id(void)
464de4bebcSJens Wiklander {
474de4bebcSJens Wiklander 	return -1;
484de4bebcSJens Wiklander }
494de4bebcSJens Wiklander 
504de4bebcSJens Wiklander 
514de4bebcSJens Wiklander /*
524de4bebcSJens Wiklander  * printf and puts - stdio printf support
534de4bebcSJens Wiklander  *
544de4bebcSJens Wiklander  * 'printf()' and 'puts()' traces have the 'info' trace level.
554de4bebcSJens Wiklander  * Traces are prefixed with string "[ta log] ".
564de4bebcSJens Wiklander  */
574de4bebcSJens Wiklander int printf(const char *fmt, ...)
584de4bebcSJens Wiklander {
594de4bebcSJens Wiklander 	char to_format[MAX_PRINT_SIZE];
604de4bebcSJens Wiklander 	static const char prefix[] = "[ta log] ";
614de4bebcSJens Wiklander 	static const char failed[] = "uta trace failed";
624de4bebcSJens Wiklander 	static const char trunc[] = "...\n";
634de4bebcSJens Wiklander 	va_list ap;
644de4bebcSJens Wiklander 	int s;
654de4bebcSJens Wiklander 
66*44202a48SPascal Brand 	if (trace_get_level() < TRACE_PRINTF_LEVEL)
674de4bebcSJens Wiklander 		return 0;
684de4bebcSJens Wiklander 
694de4bebcSJens Wiklander 	s = strlcpy(to_format, prefix, sizeof(to_format));
704de4bebcSJens Wiklander 	if ((unsigned int)s >= sizeof(to_format)) {
714de4bebcSJens Wiklander 		puts(failed);
724de4bebcSJens Wiklander 		return 0;
734de4bebcSJens Wiklander 	}
744de4bebcSJens Wiklander 
754de4bebcSJens Wiklander 	va_start(ap, fmt);
764de4bebcSJens Wiklander 	s = vsnprintf(to_format + s, sizeof(to_format) - s, fmt, ap);
774de4bebcSJens Wiklander 	va_end(ap);
784de4bebcSJens Wiklander 
794de4bebcSJens Wiklander 	if (s < 0) {
804de4bebcSJens Wiklander 		puts(failed);
814de4bebcSJens Wiklander 		return s;
824de4bebcSJens Wiklander 	}
834de4bebcSJens Wiklander 	if (((unsigned int)s >= (sizeof(to_format) - strlen(prefix)))) {
844de4bebcSJens Wiklander 		memcpy(&to_format[sizeof(to_format) - sizeof(trunc)], trunc,
854de4bebcSJens Wiklander 		       sizeof(trunc));
864de4bebcSJens Wiklander 		s = sizeof(to_format) - sizeof(prefix) - sizeof(trunc);
874de4bebcSJens Wiklander 	}
884de4bebcSJens Wiklander 
894de4bebcSJens Wiklander 	puts(to_format);
904de4bebcSJens Wiklander 
914de4bebcSJens Wiklander 	return s;
924de4bebcSJens Wiklander }
934de4bebcSJens Wiklander 
944de4bebcSJens Wiklander int puts(const char *str)
954de4bebcSJens Wiklander {
96*44202a48SPascal Brand 	if (trace_get_level() >= TRACE_PRINTF_LEVEL)
974de4bebcSJens Wiklander 		trace_ext_puts(false, str);
984de4bebcSJens Wiklander 	return 1;
994de4bebcSJens Wiklander }
1004de4bebcSJens Wiklander 
1014de4bebcSJens Wiklander 
1024de4bebcSJens Wiklander 
1034de4bebcSJens Wiklander #else
1044de4bebcSJens Wiklander 
1054de4bebcSJens Wiklander int printf(const char *fmt __unused, ...)
1064de4bebcSJens Wiklander {
1074de4bebcSJens Wiklander 	return 0;
1084de4bebcSJens Wiklander }
1094de4bebcSJens Wiklander 
1104de4bebcSJens Wiklander int puts(const char *str __unused)
1114de4bebcSJens Wiklander {
1124de4bebcSJens Wiklander 	return 0;
1134de4bebcSJens Wiklander }
1144de4bebcSJens Wiklander #
1154de4bebcSJens Wiklander 
1164de4bebcSJens Wiklander #endif
117