summaryrefslogtreecommitdiffstats
path: root/src/path.c
blob: a745838bdb24b4275be32774b1f0bb383ca55200 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Copyright 2018 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <limits.h>

#include <libpdbg.h>

#include "path.h"
#include "util.h"

#define MAX_PATH_COMP_LEN	32
#define MAX_PATH_COMPONENTS	16
#define MAX_PATH_LEN		(MAX_PATH_COMP_LEN * MAX_PATH_COMPONENTS)

/* This is max(MAX_PROCESSORS, MAX_CHIPS, MAX_THREADS) */
#define MAX_PATH_INDEX		64

struct path_pattern {
	char prefix[MAX_PATH_COMP_LEN];
	int index[MAX_PATH_INDEX];
	bool match_full;
	bool match_index;
};

/* Start with max threads, rather than defining arbitrary number */
#define MAX_TARGETS	(64 * 24 * 8)

static struct pdbg_target *path_target[MAX_TARGETS];
static unsigned int path_target_count;

static void safe_strcpy(char *dest, size_t n, const char *src)
{
	assert(strlen(src) + 1 <= n);

	strcpy(dest, src);
}

/*
 * Parse string components of following forms:
 *	pib0
 *	core[1-3,11-13]
 *	thread*
 *	adu@123000
 */
static bool path_pattern_parse(const char *arg, struct path_pattern *pat)
{
	char tmp[strlen(arg)+1];
	char *tok;
	bool ok;

	safe_strcpy(tmp, sizeof(tmp), arg);

	memset(pat, 0, sizeof(*pat));

	if (strchr(tmp, '@')) {
		safe_strcpy(pat->prefix, sizeof(pat->prefix), tmp);
		pat->match_full = true;

	} else if (strchr(tmp, '*')) {
		tok = strtok(tmp, "*");
		if (!tok)
			safe_strcpy(pat->prefix, sizeof(pat->prefix), "all");
		else
			safe_strcpy(pat->prefix, sizeof(pat->prefix), tok);

	} else if (strchr(tmp, '[')) {
		tok = strtok(tmp, "[");
		if (tok == NULL) {
			fprintf(stderr, "Invalid pattern '%s'\n", arg);
			return false;
		}
		safe_strcpy(pat->prefix, sizeof(pat->prefix), tok);

		tok = strtok(NULL, "]");
		if (!tok) {
			fprintf(stderr, "Invalid pattern '%s'\n", arg);
			return false;
		}

		ok = parse_list(tok, MAX_PATH_INDEX, pat->index, NULL);
		if (!ok)
			return false;

		pat->match_index = true;

	} else {
		size_t n = strlen(tmp) - 1;
		while (n >= 0 && isdigit(tmp[n]))
			n--;
		n++;

		if (n != strlen(tmp)) {
			int index;

			index = atoi(&tmp[n]);
			if (index < 0 || index >= MAX_PATH_INDEX) {
				fprintf(stderr, "Invalid index '%s'\n", &tmp[n]);
				return false;
			}
			tmp[n] = '\0';
			pat->index[index] = 1;
			pat->match_index = true;
		}

		safe_strcpy(pat->prefix, sizeof(pat->prefix), tmp);
	}

	if (!pat->prefix)
		return false;

	return true;
}

static int path_pattern_split(const char *arg, struct path_pattern *pats)
{
	char arg_copy[MAX_PATH_LEN];
	char *tmp, *tok, *saveptr = NULL;
	int n;
	bool ok;

	safe_strcpy(arg_copy, sizeof(arg_copy), arg);

	tmp = arg_copy;
	n = 0;
	while ((tok = strtok_r(tmp, "/", &saveptr))) {
		size_t len = strlen(tok);

		if (len == 0)
			continue;

		if (len >= MAX_PATH_LEN) {
			fprintf(stderr, "Pattern component '%s' too long\n", tok);
			return -1;
		}

		ok = path_pattern_parse(tok, &pats[n]);
		if (!ok)
			return -1;

		n++;
		assert(n <= MAX_PATH_COMPONENTS);

		tmp = NULL;
	}

	return n;
}

static int path_target_find(struct pdbg_target *prev)
{
	int i;

	if (!prev)
		return -1;

	for (i=0; i<path_target_count; i++) {
		if (path_target[i] == prev)
			return i;
	}

	return -1;
}

struct pdbg_target *path_target_find_next(const char *klass, int index)
{
	struct pdbg_target *target;
	int i;

	for (i=index+1; i<path_target_count; i++) {
		target = path_target[i];
		if (klass) {
			const char *classname = pdbg_target_class_name(target);

			if (!strcmp(klass, classname))
				return target;
		} else {
			return target;
		}
	}

	return NULL;
}

bool path_target_add(struct pdbg_target *target)
{
	int index;

	index = path_target_find(target);
	if (index >= 0)
		return true;

	if (path_target_count == MAX_TARGETS)
		return false;

	path_target[path_target_count] = target;
	path_target_count++;
	return true;
}

static void path_pattern_match(struct pdbg_target *target,
			       struct path_pattern *pats,
			       int max_levels,
			       int level)
{
	struct pdbg_target *child;
	char comp_name[MAX_PATH_COMP_LEN];
	const char *classname;
	char *tok;
	int next = level;
	bool found = false;

	if (target == pdbg_target_root()) {
		goto end;
	}

	if (!strcmp("all", pats[level].prefix)) {
		if (!path_target_add(target))
			return;
		goto end;
	}

	classname = pdbg_target_class_name(target);
	if (!classname)
		goto end;

	if (pats[level].match_full) {
		const char *dn_name = pdbg_target_dn_name(target);

		safe_strcpy(comp_name, sizeof(comp_name), dn_name);
	} else {
		safe_strcpy(comp_name, sizeof(comp_name), classname);
	}
	tok = comp_name;

	if (!strcmp(tok, pats[level].prefix)) {
		found = true;

		if (pats[level].match_index) {
			int index = pdbg_target_index(target);

			if (pats[level].index[index] != 1)
				found = false;
		}
	}

	if (found) {
		if (level == max_levels-1) {
			path_target_add(target);
			return;
		} else {
			next = level + 1;
		}
	}

end:
	pdbg_for_each_child_target(target, child) {
		path_pattern_match(child, pats, max_levels, next);
	}
}

bool path_target_parse(const char **arg, int arg_count)
{
	struct path_pattern pats[MAX_PATH_COMPONENTS];
	int i, n;

	for (i=0; i<arg_count; i++) {
		n = path_pattern_split(arg[i], pats);
		if (n <= 0)
			return false;

		path_pattern_match(pdbg_target_root(), pats, n, 0);
	}

	return true;
}

bool path_target_present(void)
{
	return (path_target_count > 0);
}

bool path_target_selected(struct pdbg_target *target)
{
	int index;

	index = path_target_find(target);
	if (index >= 0)
		return true;

	return false;
}

void path_target_dump(void)
{
	struct pdbg_target *target;
	char *path;

	for_each_path_target(target) {
		path = pdbg_target_path(target);
		assert(path);
		printf("%s\n", path);
		free(path);
	}
}

struct pdbg_target *path_target_next(struct pdbg_target *prev)
{
	int index;

	index = path_target_find(prev);
	return path_target_find_next(NULL, index);
}

struct pdbg_target *path_target_next_class(const char *klass,
					   struct pdbg_target *prev)
{
	int index;

	index = path_target_find(prev);
	return path_target_find_next(klass, index);
}
OpenPOWER on IntegriCloud