Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*	$NetBSD: db_sym.c,v 1.65 2017/11/03 09:59:07 maxv Exp $	*/

/*
 * Mach Operating System
 * Copyright (c) 1991,1990 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: db_sym.c,v 1.65 2017/11/03 09:59:07 maxv Exp $");

#ifdef _KERNEL_OPT
#include "opt_ddbparam.h"
#endif

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/ksyms.h>

#include <ddb/ddb.h>

static void		db_symsplit(char *, char **, char **);


#ifndef _KERNEL
#define	TBLNAME	"netbsd"

#define use_ksyms 0

const db_symformat_t *db_symformat;
static db_forall_func_t db_sift;
extern db_symformat_t db_symformat_aout;
extern db_symformat_t db_symformat_elf;
#endif


/*
 * Initialize the kernel debugger by initializing the master symbol
 * table.  Note that if initializing the master symbol table fails,
 * no other symbol tables can be loaded.
 */
void
ddb_init(int symsize, void *vss, void *vse)
{
#ifdef _KERNEL
	ksyms_addsyms_elf(symsize, vss, vse);	/* Will complain if necessary */
#else	/* _KERNEL */
	db_symformat = &db_symformat_elf;
	if ((*db_symformat->sym_init)(symsize, vss, vse, TBLNAME) != true)
		printf("sym_init failed");
#endif	/* _KERNEL */
}

bool
db_eqname(const char *src, const char *dst, int c)
{

	if (!strcmp(src, dst))
		return (true);
	if (src[0] == c)
		return (!strcmp(src+1,dst));
	return (false);
}

bool
db_value_of_name(const char *name, db_expr_t *valuep)
{
	char symbol[128];
	char *mod, *sym;
#ifdef _KERNEL
	unsigned long uval;
	long val;
#endif

#ifndef _KERNEL
	if (!use_ksyms) {
		db_sym_t ssym;

		/*
		 * Cannot load symtabs in a.out kernels, so the ':'
		 * style of selecting modules is irrelevant.
		 */
		ssym = (*db_symformat->sym_lookup)(NULL, name);
		if (ssym == DB_SYM_NULL)
			return (false);
		db_symbol_values(ssym, &name, valuep);
		return (true);
	}
#endif

	(void)strlcpy(symbol, name, sizeof(symbol));
	db_symsplit(symbol, &mod, &sym);
#ifdef _KERNEL
	if (ksyms_getval_unlocked(mod, sym, NULL, &uval, KSYMS_EXTERN) == 0) {
		val = (long) uval;
		*valuep = (db_expr_t)val;
		return true;
	}
	if (ksyms_getval_unlocked(mod, sym, NULL, &uval, KSYMS_ANY) == 0) {
		val = (long) uval;
		*valuep = (db_expr_t)val;
		return true;
	}
#endif
	return false;
}

#ifndef _KERNEL
/* Private structure for passing args to db_sift() from db_sifting(). */
struct db_sift_args {
	char	*symstr;
	int	mode;
};

/*
 * Does the work of db_sifting(), called once for each
 * symbol via db_forall(), prints out symbols matching
 * criteria.
 */
static void
db_sift(db_symtab_t *stab, db_sym_t sym, char *name,
    char *suffix, int prefix, void *arg)
{
	char c, sc;
	char *find, *p;
	size_t len;
	struct db_sift_args *dsa;

	dsa = (struct db_sift_args*)arg;

	find = dsa->symstr;	/* String we're looking for. */
	p = name;		/* String we're searching within. */

	/* Matching algorithm cribbed from strstr(), which is not
	   in the kernel. */
	if ((c = *find++) != 0) {
		len = strlen(find);
		do {
			do {
				if ((sc = *p++) == 0)
					return;
			} while (sc != c);
		} while (strncmp(p, find, len) != 0);
	}
	if (dsa->mode=='F')	/* ala ls -F */
		db_printf("%s%s ", name, suffix);
	else
		db_printf("%s ", name);
}
#endif

/*
 * "Sift" for a partial symbol.
 * Named for the Sun OpenPROM command ("sifting").
 * If the symbol has a qualifier (e.g., ux:vm_map),
 * then only the specified symbol table will be searched;
 * otherwise, all symbol tables will be searched..
 *
 * "mode" is how-to-display, set from modifiers.
 */
void
db_sifting(char *symstr, int mode)
{
#ifdef _KERNEL
	char *mod, *sym;
#endif

#ifndef _KERNEL
	struct db_sift_args dsa;

	if (!use_ksyms) {
		dsa.symstr = symstr;
		dsa.mode = mode;
		(*db_symformat->sym_forall)(NULL, db_sift, &dsa);
		db_printf("\n");
		return;
	}
#endif

#ifdef _KERNEL
	db_symsplit(symstr, &mod, &sym);
	if (ksyms_sift(mod, sym, mode) == ENODEV)
		db_error("invalid symbol table name");
#endif
}

/*
 * Find the closest symbol to val, and return its name
 * and the difference between val and the symbol found.
 */
db_sym_t
db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
{
	unsigned int diff;
	db_sym_t ret = DB_SYM_NULL;
#ifdef _KERNEL
	unsigned long naddr;
	const char *mod;
	const char *sym;
#endif

#ifndef _KERNEL
	if (!use_ksyms) {
		db_expr_t newdiff;
		db_sym_t ssym;

		newdiff = diff = ~0;
		ssym = (*db_symformat->sym_search)
		    (NULL, val, strategy, &newdiff);
		if ((unsigned int) newdiff < diff) {
			diff = newdiff;
			ret = ssym;
		}
		*offp = diff;
		return ret;
	}
#endif

#ifdef _KERNEL
	if (ksyms_getname(&mod, &sym, (vaddr_t)val, strategy) == 0) {
		(void)ksyms_getval_unlocked(mod, sym, NULL, &naddr, KSYMS_ANY);
		diff = val - (db_addr_t)naddr;
		ret = (db_sym_t)naddr;
	} else
#endif
		diff = 0;
	*offp = diff;
	return ret;
}

/*
 * Return name and value of a symbol
 */
void
db_symbol_values(db_sym_t sym, const char **namep, db_expr_t *valuep)
{
#ifdef _KERNEL
	const char *mod;
#endif

	if (sym == DB_SYM_NULL) {
		*namep = 0;
		return;
	}

#ifndef _KERNEL
	if (!use_ksyms) {
		db_expr_t value;

		(*db_symformat->sym_value)(NULL, sym, namep, &value);
		if (valuep)
			*valuep = value;
		return;
	}
#endif

#ifdef _KERNEL
	if (ksyms_getname(&mod, namep, (vaddr_t)sym,
	    KSYMS_ANY|KSYMS_EXACT) == 0) {
		if (valuep)
			*valuep = sym;
	} else
#endif
		*namep = NULL;
}


/*
 * Print a the closest symbol to value
 *
 * After matching the symbol according to the given strategy
 * we print it in the name+offset format, provided the symbol's
 * value is close enough (eg smaller than db_maxoff).
 * We also attempt to print [filename:linenum] when applicable
 * (eg for procedure names).
 *
 * If we could not find a reasonable name+offset representation,
 * then we just print the value in hex.  Small values might get
 * bogus symbol associations, e.g. 3 might get some absolute
 * value like _INCLUDE_VERSION or something, therefore we do
 * not accept symbols whose value is zero (and use plain hex).
 */
unsigned int	db_maxoff = 0x100000;

void
db_symstr(char *buf, size_t buflen, db_expr_t off, db_strategy_t strategy)
{
	const char  *name;
#ifdef _KERNEL
	const char *mod;
	unsigned long val;
#endif

#ifndef _KERNEL
	if (!use_ksyms) {
		db_expr_t	d;
		char 		*filename;
		db_expr_t	value;
		int 		linenum;
		db_sym_t	cursym;

		cursym = db_search_symbol(off, strategy, &d);
		db_symbol_values(cursym, &name, &value);
		if (name != NULL && ((unsigned int)d < db_maxoff) &&
		    value != 0) {
			strlcpy(buf, name, buflen);
			if (d) {
				strlcat(buf, "+", buflen);
				db_format_radix(buf + strlen(buf), 24, d, true);
			}
			if (strategy == DB_STGY_PROC) {
				if ((*db_symformat->sym_line_at_pc)
				    (NULL, cursym, &filename, &linenum, off)) {
					size_t len = strlen(buf);
					snprintf(buf + len, buflen - len,
					    " [%s:%d]", filename, linenum);
				}
			}
			return;
		}
		strlcpy(buf, db_num_to_str(off), buflen);
		return;
	}
#endif
#ifdef _KERNEL
	if (ksyms_getname(&mod, &name, (vaddr_t)off,
	    strategy|KSYMS_CLOSEST) == 0) {
		(void)ksyms_getval_unlocked(mod, name, NULL, &val, KSYMS_ANY);
		if (((off - val) < db_maxoff) && val) {
			snprintf(buf, buflen, "%s:%s", mod, name);
			if (off - val) {
				strlcat(buf, "+", buflen);
				db_format_radix(buf+strlen(buf),
				    24, off - val, true);
			}
#ifdef notyet
			if (strategy & KSYMS_PROC) {
				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
					snprintf(buf + strlen(buf),
					    buflen - strlen(buf),
					    " [%s:%d]", filename, linenum);
			}
#endif
			return;
		}
	}
	strlcpy(buf, db_num_to_str(off), buflen);
#endif
}

void
db_printsym(db_expr_t off, db_strategy_t strategy,
    void (*pr)(const char *, ...))
{
	const char  *name;
#ifdef _KERNEL
	const char *mod;
	unsigned long uval;
	long val;
#endif
#ifdef notyet
	char *filename;
	int  linenum;
#endif

#ifndef _KERNEL
	if (!use_ksyms) {
		db_expr_t	d;
		char 		*filename;
		db_expr_t	value;
		int 		linenum;
		db_sym_t	cursym;

		cursym = db_search_symbol(off, strategy, &d);
		db_symbol_values(cursym, &name, &value);
		if (name != NULL && ((unsigned int)d < db_maxoff) &&
		    value != 0) {
			(*pr)("%s", name);
			if (d) {
				char tbuf[24];

				db_format_radix(tbuf, 24, d, true);
				(*pr)("+%s", tbuf);
			}
			if (strategy == DB_STGY_PROC) {
				if ((*db_symformat->sym_line_at_pc)
				    (NULL, cursym, &filename, &linenum, off))
					(*pr)(" [%s:%d]", filename, linenum);
			}
			return;
		}
		(*pr)("%s", db_num_to_str(off));
		return;
	}
#endif
#ifdef _KERNEL
	if (ksyms_getname(&mod, &name, (vaddr_t)off,
	    strategy|KSYMS_CLOSEST) == 0) {
		(void)ksyms_getval_unlocked(mod, name, NULL, &uval, KSYMS_ANY);
		val = (long) uval;
		if (((off - val) < db_maxoff) && val) {
			(*pr)("%s:%s", mod, name);
			if (off - val) {
				char tbuf[24];

				db_format_radix(tbuf, 24, off - val, true);
				(*pr)("+%s", tbuf);
			}
#ifdef notyet
			if (strategy & KSYMS_PROC) {
				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
					(*pr)(" [%s:%d]", filename, linenum);
			}
#endif
			return;
		}
	}
#endif
	(*pr)("%s", db_num_to_str(off));
	return;
}

/*
 * Splits a string in the form "mod:sym" to two strings.
 */
static void
db_symsplit(char *str, char **mod, char **sym)
{
	char *cp;

	if ((cp = strchr(str, ':')) != NULL) {
		*cp++ = '\0';
		*mod = str;
		*sym = cp;
	} else {
		*mod = NULL;
		*sym = str;
	}
}

bool
db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
{
#ifndef _KERNEL
	if (!use_ksyms)
		return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
		    argnamep));
#endif
	return (false);
}