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 CFG_TRACE_LEVEL > 0 37 38 const char trace_ext_prefix[] = "USER-TA"; 39 40 void trace_ext_puts(bool sync __unused, const char *str) 41 { 42 utee_log(str, strlen(str)); 43 } 44 45 int trace_ext_get_thread_id(void) 46 { 47 return -1; 48 } 49 50 51 /* 52 * printf and puts - stdio printf support 53 * 54 * 'printf()' and 'puts()' traces have the 'info' trace level. 55 * Traces are prefixed with string "[ta log] ". 56 */ 57 int printf(const char *fmt, ...) 58 { 59 char to_format[MAX_PRINT_SIZE]; 60 static const char prefix[] = "[ta log] "; 61 static const char failed[] = "uta trace failed"; 62 static const char trunc[] = "...\n"; 63 va_list ap; 64 int s; 65 66 if (trace_get_level() <= TRACE_PRINTF_LEVEL) 67 return 0; 68 69 s = strlcpy(to_format, prefix, sizeof(to_format)); 70 if ((unsigned int)s >= sizeof(to_format)) { 71 puts(failed); 72 return 0; 73 } 74 75 va_start(ap, fmt); 76 s = vsnprintf(to_format + s, sizeof(to_format) - s, fmt, ap); 77 va_end(ap); 78 79 if (s < 0) { 80 puts(failed); 81 return s; 82 } 83 if (((unsigned int)s >= (sizeof(to_format) - strlen(prefix)))) { 84 memcpy(&to_format[sizeof(to_format) - sizeof(trunc)], trunc, 85 sizeof(trunc)); 86 s = sizeof(to_format) - sizeof(prefix) - sizeof(trunc); 87 } 88 89 puts(to_format); 90 91 return s; 92 } 93 94 int puts(const char *str) 95 { 96 if (trace_get_level() > TRACE_PRINTF_LEVEL) 97 trace_ext_puts(false, str); 98 return 1; 99 } 100 101 102 103 #else 104 105 int printf(const char *fmt __unused, ...) 106 { 107 return 0; 108 } 109 110 int puts(const char *str __unused) 111 { 112 return 0; 113 } 114 # 115 116 #endif 117