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 368a1e7b89SJerome Forissier #if TRACE_LEVEL > 0 374de4bebcSJens Wiklander 38bfc6487bSJerome Forissier void trace_ext_puts(const char *str) 394de4bebcSJens Wiklander { 404de4bebcSJens Wiklander utee_log(str, strlen(str)); 414de4bebcSJens Wiklander } 424de4bebcSJens Wiklander 434de4bebcSJens Wiklander int trace_ext_get_thread_id(void) 444de4bebcSJens Wiklander { 454de4bebcSJens Wiklander return -1; 464de4bebcSJens Wiklander } 474de4bebcSJens Wiklander 484de4bebcSJens Wiklander 494de4bebcSJens Wiklander /* 504de4bebcSJens Wiklander * printf and puts - stdio printf support 514de4bebcSJens Wiklander * 524de4bebcSJens Wiklander * 'printf()' and 'puts()' traces have the 'info' trace level. 534de4bebcSJens Wiklander * Traces are prefixed with string "[ta log] ". 544de4bebcSJens Wiklander */ 554de4bebcSJens Wiklander int printf(const char *fmt, ...) 564de4bebcSJens Wiklander { 574de4bebcSJens Wiklander char to_format[MAX_PRINT_SIZE]; 584de4bebcSJens Wiklander static const char prefix[] = "[ta log] "; 594de4bebcSJens Wiklander static const char failed[] = "uta trace failed"; 604de4bebcSJens Wiklander static const char trunc[] = "...\n"; 614de4bebcSJens Wiklander va_list ap; 624de4bebcSJens Wiklander int s; 634de4bebcSJens Wiklander 6444202a48SPascal Brand if (trace_get_level() < TRACE_PRINTF_LEVEL) 654de4bebcSJens Wiklander return 0; 664de4bebcSJens Wiklander 674de4bebcSJens Wiklander s = strlcpy(to_format, prefix, sizeof(to_format)); 684de4bebcSJens Wiklander if ((unsigned int)s >= sizeof(to_format)) { 694de4bebcSJens Wiklander puts(failed); 704de4bebcSJens Wiklander return 0; 714de4bebcSJens Wiklander } 724de4bebcSJens Wiklander 734de4bebcSJens Wiklander va_start(ap, fmt); 744de4bebcSJens Wiklander s = vsnprintf(to_format + s, sizeof(to_format) - s, fmt, ap); 754de4bebcSJens Wiklander va_end(ap); 764de4bebcSJens Wiklander 774de4bebcSJens Wiklander if (s < 0) { 784de4bebcSJens Wiklander puts(failed); 794de4bebcSJens Wiklander return s; 804de4bebcSJens Wiklander } 814de4bebcSJens Wiklander if (((unsigned int)s >= (sizeof(to_format) - strlen(prefix)))) { 824de4bebcSJens Wiklander memcpy(&to_format[sizeof(to_format) - sizeof(trunc)], trunc, 834de4bebcSJens Wiklander sizeof(trunc)); 844de4bebcSJens Wiklander s = sizeof(to_format) - sizeof(prefix) - sizeof(trunc); 854de4bebcSJens Wiklander } 864de4bebcSJens Wiklander 874de4bebcSJens Wiklander puts(to_format); 884de4bebcSJens Wiklander 894de4bebcSJens Wiklander return s; 904de4bebcSJens Wiklander } 914de4bebcSJens Wiklander 924de4bebcSJens Wiklander int puts(const char *str) 934de4bebcSJens Wiklander { 9444202a48SPascal Brand if (trace_get_level() >= TRACE_PRINTF_LEVEL) 95bfc6487bSJerome Forissier trace_ext_puts(str); 964de4bebcSJens Wiklander return 1; 974de4bebcSJens Wiklander } 984de4bebcSJens Wiklander 99*dc454609SJerome Forissier int putchar(int c) 100*dc454609SJerome Forissier { 101*dc454609SJerome Forissier char str[2] = { (char)c, '\0' }; 102*dc454609SJerome Forissier 103*dc454609SJerome Forissier if (trace_get_level() >= TRACE_PRINTF_LEVEL) 104*dc454609SJerome Forissier trace_ext_puts(str); 105*dc454609SJerome Forissier /* 106*dc454609SJerome Forissier * From the putchar() man page: 107*dc454609SJerome Forissier * "fputc(), putc() and putchar() return the character written as an 108*dc454609SJerome Forissier * unsigned char cast to an int or EOF on error." 109*dc454609SJerome Forissier */ 110*dc454609SJerome Forissier return (int)(unsigned char)c; 111*dc454609SJerome Forissier } 112*dc454609SJerome Forissier 1134de4bebcSJens Wiklander #else 1144de4bebcSJens Wiklander 1154de4bebcSJens Wiklander int printf(const char *fmt __unused, ...) 1164de4bebcSJens Wiklander { 1174de4bebcSJens Wiklander return 0; 1184de4bebcSJens Wiklander } 1194de4bebcSJens Wiklander 1204de4bebcSJens Wiklander int puts(const char *str __unused) 1214de4bebcSJens Wiklander { 1224de4bebcSJens Wiklander return 0; 1234de4bebcSJens Wiklander } 1244de4bebcSJens Wiklander 125*dc454609SJerome Forissier int putchar(int c) 126*dc454609SJerome Forissier { 127*dc454609SJerome Forissier return (int)(unsigned char)c; 128*dc454609SJerome Forissier } 129*dc454609SJerome Forissier 1304de4bebcSJens Wiklander #endif 131