Ranjani Sridharan | 4670402 | 2018-05-31 19:29:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, Intel Corporation |
| 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 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the Intel Corporation nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com> |
| 29 | * Liam Girdwood <liam.r.girdwood@linux.intel.com> |
| 30 | * Keyon Jie <yang.jie@linux.intel.com> |
| 31 | * Ranjani Sridharan <ranjani.sridharan@linux.intel.com> |
| 32 | */ |
| 33 | |
| 34 | #include <stdint.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include "host/common_test.h" |
| 38 | #include "host/trace.h" |
| 39 | |
| 40 | #define MAX_TRACE_CLASSES 255 |
| 41 | /* testbench trace definition */ |
| 42 | |
| 43 | /* enable trace by default in testbench */ |
| 44 | static int test_bench_trace = 1; |
| 45 | int num_trace_classes; |
| 46 | |
| 47 | /* set up trace class identifier table based on SOF trace header file */ |
Ranjani Sridharan | 7166305 | 2018-07-02 21:51:13 -0700 | [diff] [blame] | 48 | int setup_trace_table(void) |
Ranjani Sridharan | 4670402 | 2018-05-31 19:29:05 -0700 | [diff] [blame] | 49 | { |
| 50 | char buffer[2048]; |
| 51 | char *trace = "sof/trace.h"; |
| 52 | char *trace_filename = malloc(strlen(SOF_INC) + strlen(trace) + 2); |
| 53 | char *token; |
| 54 | int ret, i = 0; |
| 55 | size_t size; |
| 56 | FILE *fp; |
| 57 | |
| 58 | /* set up trace file name using include directory prefix */ |
| 59 | sprintf(trace_filename, "%s/%s", SOF_INC, trace); |
| 60 | |
| 61 | fp = fopen(trace_filename, "r"); |
| 62 | if (!fp) { |
| 63 | fprintf(stderr, "error: opening trace include file %s\n", |
| 64 | trace_filename); |
Ranjani Sridharan | 7166305 | 2018-07-02 21:51:13 -0700 | [diff] [blame] | 65 | return -EINVAL; |
Ranjani Sridharan | 4670402 | 2018-05-31 19:29:05 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* find number of trace classes defined */ |
| 69 | while (fgets(buffer, sizeof(buffer), fp)) { |
| 70 | char identifier[1024]; |
| 71 | int value = 0, shift = 0; |
| 72 | |
| 73 | ret = sscanf(buffer, "#define %s (%d << %d)", identifier, |
| 74 | &value, &shift); |
| 75 | if (ret == 3) { |
| 76 | /* if TRACE_CLASS definition */ |
| 77 | if (strstr(identifier, "TRACE_CLASS")) |
| 78 | i++; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | num_trace_classes = i; |
| 83 | |
| 84 | /* allocate memory for trace table */ |
| 85 | size = sizeof(struct trace_class_table); |
| 86 | trace_table = (struct trace_class_table *)malloc(size * |
| 87 | num_trace_classes); |
| 88 | |
| 89 | /* rewind file pointer */ |
| 90 | fseek(fp, 0, SEEK_SET); |
| 91 | |
| 92 | i = 0; |
| 93 | |
| 94 | /* read lines from header */ |
| 95 | while (fgets(buffer, sizeof(buffer), fp)) { |
| 96 | char identifier[1024]; |
| 97 | int value = 0, shift = 0; |
| 98 | |
| 99 | ret = sscanf(buffer, "#define %s (%d << %d)", identifier, |
| 100 | &value, &shift); |
| 101 | if (ret == 3) { |
| 102 | /* if TRACE_CLASS definition */ |
| 103 | if (strstr(identifier, "TRACE_CLASS")) { |
| 104 | /* extract subsystem name */ |
| 105 | token = strtok(identifier, "_"); |
| 106 | token = strtok(NULL, "_"); |
| 107 | token = strtok(NULL, "_"); |
| 108 | |
| 109 | /* add trace class entry */ |
| 110 | trace_table[i].trace_class = value; |
| 111 | trace_table[i].class_name = strdup(token); |
| 112 | i++; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | fclose(fp); |
| 117 | free(trace_filename); |
Ranjani Sridharan | 7166305 | 2018-07-02 21:51:13 -0700 | [diff] [blame] | 118 | return 0; |
Ranjani Sridharan | 4670402 | 2018-05-31 19:29:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void free_trace_table(void) |
| 122 | { |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < num_trace_classes; i++) |
| 126 | free(trace_table[i].class_name); |
| 127 | |
| 128 | free(trace_table); |
| 129 | } |
| 130 | |
| 131 | /* look up subsystem class name from table */ |
| 132 | static char *get_trace_class(uint32_t trace_class) |
| 133 | { |
| 134 | int i; |
| 135 | |
| 136 | /* look up trace class table and return subsystem name */ |
| 137 | for (i = 0; i < num_trace_classes; i++) { |
| 138 | if (trace_table[i].trace_class == trace_class) |
| 139 | return trace_table[i].class_name; |
| 140 | } |
| 141 | |
| 142 | return "value"; |
| 143 | } |
| 144 | |
| 145 | /* print trace event */ |
| 146 | void _trace_event(uint32_t event) |
| 147 | { |
| 148 | char a, b, c; |
| 149 | char *trace_class = NULL; |
| 150 | |
| 151 | if (test_bench_trace > 0) { |
| 152 | a = event & 0xff; |
| 153 | b = (event >> 8) & 0xff; |
| 154 | c = (event >> 16) & 0xff; |
| 155 | |
| 156 | /* look up subsystem from trace class table */ |
| 157 | trace_class = strdup(get_trace_class(event >> 24)); |
| 158 | |
| 159 | /* print trace event stderr*/ |
| 160 | if (strcmp(trace_class, "value") == 0) |
| 161 | fprintf(stderr, "Trace value %d\n", event); |
| 162 | else |
| 163 | fprintf(stderr, "Trace %s %c%c%c\n", trace_class, |
| 164 | c, b, a); |
| 165 | } |
| 166 | |
| 167 | free(trace_class); |
| 168 | } |
| 169 | |
| 170 | void _trace_event_mbox_atomic(uint32_t event) |
| 171 | { |
| 172 | _trace_event(event); |
| 173 | } |
| 174 | |
| 175 | /* enable trace in testbench */ |
| 176 | void tb_enable_trace(bool enable) |
| 177 | { |
| 178 | test_bench_trace = enable; |
| 179 | if (enable) |
| 180 | debug_print("trace print enabled\n"); |
| 181 | else |
| 182 | debug_print("trace print disabled\n"); |
| 183 | } |