forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.c
More file actions
315 lines (280 loc) · 8.7 KB
/
filter.c
File metadata and controls
315 lines (280 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright(c) 2020 Intel Corporation. All rights reserved.
*
* Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
*/
#include <ipc/trace.h>
#include <smex/ldc.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <user/trace.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "convert.h"
#include "filter.h"
#include "misc.h"
#define COMPONENTS_SEPARATOR ','
#define COMPONENT_NAME_SCAN_STRING_LENGTH 32
/** map between log level given by user and enum value */
static const struct {
const char name[16];
int32_t log_level;
} log_level_dict[] = {
{"verbose", LOG_LEVEL_VERBOSE},
{"debug", LOG_LEVEL_DEBUG},
{"info", LOG_LEVEL_INFO},
{"warning", LOG_LEVEL_WARNING},
{"error", LOG_LEVEL_ERROR},
{"critical", LOG_LEVEL_CRITICAL},
{"v", LOG_LEVEL_VERBOSE},
{"d", LOG_LEVEL_DEBUG},
{"i", LOG_LEVEL_INFO},
{"w", LOG_LEVEL_WARNING},
{"e", LOG_LEVEL_ERROR},
{"c", LOG_LEVEL_CRITICAL},
};
struct filter_element {
struct list_item list;
int32_t uuid_id; /**< type id, or -1 when not important */
int32_t comp_id; /**< component id or -1 when not important */
int32_t pipe_id; /**< pipeline id or -1 when not important */
int32_t log_level; /**< new log level value */
};
/**
* Search for uuid entry with given component name in given uids dictionary
* @param name of uuid entry
* @return pointer to sof_uuid_entry with given name
*/
static struct sof_uuid_entry *get_uuid_by_name(const char *name)
{
const struct snd_sof_uids_header *uids_dict = global_config->uids_dict;
uintptr_t beg = (uintptr_t)uids_dict + uids_dict->data_offset;
uintptr_t end = beg + uids_dict->data_length;
struct sof_uuid_entry *ptr = (struct sof_uuid_entry *)beg;
while ((uintptr_t)ptr < end) {
if (strcmp(name, ptr->name) == 0)
return ptr;
++ptr;
}
return NULL;
}
/**
* Parse log level name (from log_level_dict) to enum value.
* Take care about possible whitespace at the begin.
* @param value_start pointer to the begin of range to search
* @return enum value for given log level, or -1 for invalid value
*/
static int filter_parse_log_level(const char *value_start)
{
int i;
for (i = 0; i < ARRAY_SIZE(log_level_dict); ++i) {
if (!strcmp(log_level_dict[i].name, value_start))
return log_level_dict[i].log_level;
}
return -1;
}
static char *filter_parse_component_name(char *input_str, struct filter_element *out)
{
static char scan_format_string[COMPONENT_NAME_SCAN_STRING_LENGTH] = "";
char comp_name[UUID_NAME_MAX_LEN];
struct sof_uuid_entry *uuid_entry;
int ret;
/* if component name is not specified, stay with default out->uuid_id value */
if (input_str[0] == '*')
return &input_str[1];
/*
* Take care about buffer overflows when dealing with input from
* user, so scan no more than UUID_NAME_MAX_LEN bytes to
* `comp_name` variable. Only once initialise scan_format_string.
*/
if (strlen(scan_format_string) == 0) {
ret = snprintf(scan_format_string, sizeof(scan_format_string),
"%%%d[^0-9* ]s", UUID_NAME_MAX_LEN);
if (ret <= 0)
return NULL;
}
ret = sscanf(input_str, scan_format_string, comp_name);
if (ret <= 0)
return NULL;
/* find component uuid key */
uuid_entry = get_uuid_by_name(comp_name);
if (!uuid_entry) {
log_err("unknown component name `%s`\n", comp_name);
return NULL;
}
out->uuid_id = get_uuid_key(uuid_entry);
return strstr(input_str, comp_name) + strlen(comp_name);
}
/**
* Parse component definition from input_str.
*
* Possible input_str formats:
* `name pipe_id.comp_id`
* `name pipe_id.*`
* `name *`
* `name`
* `* pipe_id.comp_id`
* `* pipe_id.*`
* `*`
* Whitespace is possible at the begin, end and after `name`.
* `name` must refer to values from given UUID dictionary,
* (so name comes from DECLARE_SOF_UUID macro usage)
* @param input_str formatted component definition
* @param out element where component definition should be saved
*/
static int filter_parse_component(char *input_str, struct filter_element *out)
{
char *instance_info;
int ret;
/* trim whitespaces, to easily check first and last char */
input_str = trim(input_str);
/* assign default values */
out->uuid_id = 0;
out->pipe_id = -1;
out->comp_id = -1;
/* parse component name and store pointer after component name, pointer to instance info */
instance_info = filter_parse_component_name(input_str, out);
if (!instance_info) {
log_err("component name parsing `%s`\n", input_str);
return -EINVAL;
}
/* if instance is not specified then stop parsing */
instance_info = ltrim(instance_info);
if (instance_info[0] == '\0' ||
(instance_info[0] == '*' && instance_info[1] == '\0')) {
return 0;
}
/* now parse last part: `number.x` where x is a number or `*` */
ret = sscanf(instance_info, "%d.%d", &out->pipe_id, &out->comp_id);
if (ret == 2)
return 0;
else if (ret != 1)
return -EINVAL;
/* pipeline id parsed but component id is not a number */
if (instance_info[strlen(instance_info) - 1] == '*')
return 0;
log_err("Use * to specify each component on particular pipeline\n");
return -EINVAL;
}
/**
* Convert argument from -F flag to sof_ipc_dma_trace_filter_elem struct values.
*
* Possible log_level - see filter_parse_log_level() documentation
* Possible component - list of components separated by `COMPONENTS_SEPARATOR`,
* for single component definition description look at
* filter_parse_component() documentation
*
* Examples:
* `debug="pipe1"` - set debug log level for components from pipeline1
* `d="pipe1, dai2.3"` - as above, but also for dai2.3
* `error="FIR*"` - for each FIR component set log level to error
*
* @param input_str log level settings in format `log_level=component`
* @param out_list output list with filter_element elements
*/
static int filter_parse_entry(char *input_str, struct list_item *out_list)
{
struct filter_element *filter;
char *comp_fmt_end;
int32_t log_level;
char *comp_fmt;
int ret;
/*
* split string on '=' char, left part describes log level,
* the right one is for component description.
*/
comp_fmt = strchr(input_str, '=');
if (!comp_fmt) {
log_err("unable to find `=` in `%s`\n", input_str);
return -EINVAL;
}
*comp_fmt = 0;
++comp_fmt;
/* find correct log level in given conf string - string before `=` */
log_level = filter_parse_log_level(input_str);
if (log_level < 0) {
log_err("unable to parse log level from `%s`\n", input_str);
return log_level;
}
/*
* now parse list of components name and optional instance identifier,
* split string on `COMPONENTS_SEPARATOR`
*/
while (comp_fmt) {
filter = malloc(sizeof(struct filter_element));
if (!filter) {
log_err("unable to malloc memory\n");
return -ENOMEM;
}
comp_fmt_end = strchr(comp_fmt, COMPONENTS_SEPARATOR);
if (comp_fmt_end)
*comp_fmt_end = '\0';
ret = filter_parse_component(comp_fmt, filter);
if (ret < 0) {
log_err("unable to parse component from `%s`\n", comp_fmt);
free(filter);
return ret;
}
filter->log_level = log_level;
list_item_append(&filter->list, out_list);
comp_fmt = comp_fmt_end ? comp_fmt_end + 1 : 0;
}
return 0;
}
/**
* Parse `input_str` content and send it to FW via debugFS.
*
* `input_str` contain single filter definition element per line.
* Each line is parsed by `filter_parse_entry`, and saved in list.
* List of `sof_ipc_dma_trace_filter_elem` is writend to debugFS,
* and then send as IPC to FW (this action is implemented in driver).
* Each line in debugFS represents single IPC message.
*/
int filter_update_firmware(void)
{
char *input_str = global_config->filter_config;
struct filter_element *filter;
struct list_item filter_list;
struct list_item *list_elem;
struct list_item *list_temp;
char *line_end;
FILE *out_fd = NULL;
int ret = 0;
list_init(&filter_list);
/* parse `input_str` line by line */
line_end = strchr(input_str, '\n');
while (line_end) {
line_end[0] = '\0';
ret = filter_parse_entry(input_str, &filter_list);
if (ret < 0)
goto err;
input_str = line_end + 1;
line_end = strchr(input_str, '\n');
}
/* write output to debugFS */
out_fd = fopen(FILTER_KERNEL_PATH, "w");
if (!out_fd) {
log_err("Unable to open out file '%s'\n", FILTER_KERNEL_PATH);
ret = -errno;
goto err;
}
list_for_item(list_elem, &filter_list) {
filter = container_of(list_elem, struct filter_element, list);
fprintf(out_fd, "%d %X %d %d;", filter->log_level,
filter->uuid_id, filter->pipe_id, filter->comp_id);
}
fprintf(out_fd, "\n");
err:
if (out_fd)
fclose(out_fd);
/* free each component from parsed element list */
list_for_item_safe(list_elem, list_temp, &filter_list) {
filter = container_of(list_elem, struct filter_element, list);
free(filter);
}
return ret;
}