summaryrefslogtreecommitdiff
path: root/src/pmdas/linux_proc/ksym.c
blob: 1604c841a80e5bac685c240098f7ec593d1b4d80 (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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 * Copyright (c) International Business Machines Corp., 2002
 * Copyright (c) 2003,2004 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/*
 * This code originally contributed by Mike Mason <mmlnx@us.ibm.com>
 * with hints from the procps and ksymoops projects.
 */

#include <ctype.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include "pmapi.h"
#include "impl.h"
#include "ksym.h"
#include "indom.h"

static struct ksym *ksym_a;
static size_t ksym_a_sz;

static int
find_index(__psint_t addr, int lo, int hi)
{
    int mid;

    if (lo > hi) {
	return -1;
    }

    mid = lo + ((hi - lo) / 2);
    if (addr == ksym_a[mid].addr || 
	(addr > ksym_a[mid].addr && addr < ksym_a[mid+1].addr)) {
	return mid;
    }

    if (addr > ksym_a[mid].addr)
	return find_index(addr, mid+1, hi);
    else
	return find_index(addr, lo, mid-1);
}

static char *
find_name_by_addr(__psint_t addr)
{
    int ix = -1;

    if (ksym_a)
	ix = find_index(addr, 0, ksym_a_sz - 1);
    if (ix < 0)
	return NULL;

    return ksym_a[ix].name;
}

static int
find_dup_name(int maxix, __psint_t addr, char *name)
{
    int i, res;

    for (i = 0; i < maxix; i++) {
	if (ksym_a[i].name) {
	    res = strcmp(ksym_a[i].name, name);
	    if (res > 0) 
		break;
	    if (res == 0) {
		if (addr == ksym_a[i].addr)
		    return KSYM_FOUND;
		else
		    return KSYM_FOUND_MISMATCH;
	    }
	}
    }

    return KSYM_NOT_FOUND;
}

/* Brute force linear search to determine if the kernel version
   in System.map matches the running kernel version and returns
   a tri-state result as follows:

   0 no match
   1 _end not found but version matched
   2 _end found and matched
 */
static int
validate_sysmap(FILE *fp, char *version, __psint_t end_addr)
{
    __psint_t addr;
    char type;
    int ret = 0;
    char kname[128];

    while (fscanf(fp, "%p %c %s", (void **)&addr, &type, kname) != EOF) {
	if (end_addr && strcmp(kname, "_end") == 0) {
	    ret = (end_addr == addr) ? 2 : 0;
	    break; /* no need to look any further */
	}
	if (strcmp(kname, version) == 0)
	    ret = 1;
    }

    return ret;
}

char *
wchan(__psint_t addr)
{
    static char zero;
    char *p = NULL;

    if (addr == 0) /* 0 address means not in kernel space */
	p = &zero;
    else if ((p = find_name_by_addr(addr))) {
	/* strip off "sys_" or leading "_"s if necessary */
	if (strncmp(p, "sys_", 4) == 0)
	    p += 4;
	while (*p == '_' && *p)
	    ++p;
    }

    return p;
}

static int
ksym_compare_addr(const void *e1, const void *e2)
{
    struct ksym *ks1 = (struct ksym *) e1;
    struct ksym *ks2 = (struct ksym *) e2;

    if (ks1->addr < ks2->addr)
	return -1;
    if (ks1->addr > ks2->addr)
	return 1;
    return 0;
}

static int
ksym_compare_name(const void *e1, const void *e2)
{
    struct ksym *ks1 = (struct ksym *) e1;
    struct ksym *ks2 = (struct ksym *) e2;

    return(strcmp(ks1->name, ks2->name));
}

static int
read_ksyms(__psint_t *end_addr)
{
    char	inbuf[256];
    char	*ip;
    char	*sp;
    char	*tp;
    char	*p;
    int		ix = 0;
    int		l = 0;
    int		len;
    int		err;
    FILE	*fp;
    struct ksym	*ksym_tmp;

    *end_addr = 0;
    if ((fp = proc_statsfile("/proc/ksyms", inbuf, sizeof(inbuf))) == NULL)
	return -oserror();

    while (fgets(inbuf, sizeof(inbuf), fp) != NULL) {
	l++;

	/*
	 * /proc/ksyms lines look like this on ia32 ...
	 *
	 * c8804060 __insmod_rtc_S.text_L4576	[rtc]
	 * c010a320 disable_irq_nosync
	 *
	 * else on ia64 ...
	 *
	 * a0000000003e0d28 debug	[arsess]
	 * e002100000891140 disable_irq_nosync
	 */

	if (strstr(inbuf, "\n") == NULL) {
	    fprintf(stderr, "read_ksyms: truncated /proc/ksyms line [%d]: %s\n", l-1, inbuf);
	    continue;
	}

	/* Increase array size, if necessary */
	if (ksym_a_sz < ix+1) {
	    if (ksym_a_sz > 0)
		ksym_a_sz += INCR_KSIZE;
	    else
		ksym_a_sz = INIT_KSIZE;
	    ksym_tmp = (struct ksym *)realloc(ksym_a, ksym_a_sz * sizeof(struct ksym));
	    if (ksym_tmp == NULL) {
		err = -oserror();
		free(ksym_a);
		fclose(fp);
		return err;
	    }
	    ksym_a = ksym_tmp;
	}

	ip = inbuf;
	/* parse over address */
	while (isxdigit((int)*ip)) ip++;

	if (!isspace((int)*ip) || ip-inbuf < 4) {
	    /* bad format line */
#if PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL2) {
		fprintf(stderr, "read_ksyms: bad addr? %c[%d] line=\"%s\"\n", *ip, (int)(ip-inbuf), inbuf);
	    }
#endif
	    continue;
	}

	sscanf(inbuf, "%p", (void **)&ksym_a[ix].addr);

	while (isblank((int)*ip)) ip++;

	/* next should be the symbol name */
	sp = ip++;
	while (!isblank((int)*ip) &&*ip != '\n') ip++;

	/* strip off GPLONLY_ prefix, if found */
	if (strncmp(sp, "GPLONLY_", 8) == 0)
	    sp += 8;

	/*
	 * strip off symbol version suffix, if found ... looking for
	 * trailing pattern of the form _R.*[0-9a-fA-F]{8,}
	 * - find rightmost _R, if any
	 */
	tp = sp;
    	while ((p = strstr(tp, "_R")) != NULL) tp = p+2;
	if (tp > sp) {
	    /*
	     * found _R, need the last 8 digits to be hex
	     */
	    if (ip - tp + 1 >= 8) {
		for (p = &ip[-8]; p < ip; p++) {
		    if (!isxdigit((int)*p)) {
			tp = sp;
			break;
		    }
		}
	    }
	    else {
		/* not enough characters for [0-9a-fA-f]{8,} at the end */
		tp = sp;
	    }
	}
	if (tp > sp)
	    /* need to strip the trailing _R.*[0-9a-fA-f]{8,} */
	    len = tp - sp - 2;
	else
	    len = ip - sp + 1;

	ksym_a[ix].name = strndup(sp, len);
	if (ksym_a[ix].name == NULL) {
	    err = -oserror();
	    fclose(fp);
	    return err;
	}
	ksym_a[ix].name[len-1] = '\0';

	if (*end_addr == 0 && strcmp(ksym_a[ix].name, "_end") == 0)
	    *end_addr = ksym_a[ix].addr;

	if (*ip == '\n')
	    /* nothing after the symbol name, so no module name */
	    goto next;

	while (isblank((int)*ip)) ip++;

	/* next expect module name */
	if (*ip != '[') {
#if PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL2) {
		fprintf(stderr, "read_ksyms: bad start module name %c[%d] != [ line=\"%s\"\n", *ip, (int)(ip-inbuf), inbuf);
	    }
#endif
	    free(ksym_a[ix].name);
	    continue;
	}

	sp = ++ip;
	while (!isblank((int)*ip) && *ip != ']') ip++;

	if (*ip != ']') {
#if PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL2) {
		fprintf(stderr, "read_ksyms: bad end module name %c[%d] != ] line=\"%s\"\n", *ip, (int)(ip-inbuf), inbuf);
	    }
#endif
	    free(ksym_a[ix].name);
	    continue;
	}

	ksym_a[ix].module = strndup(sp, ip - sp + 1);
	if (ksym_a[ix].module == NULL) {
	    err = -oserror();
	    fclose(fp);
	    free(ksym_a[ix].name);
	    return err;
	}
	ksym_a[ix].module[ip - sp] = '\0';

next:
	ix++;
    }

    /* release unused ksym array entries */
    if (ix) {
	ksym_tmp = (struct ksym *)realloc(ksym_a, ix * sizeof(struct ksym));
	if (ksym_tmp == NULL) {
	    free(ksym_a);
	    fclose(fp);
	    return -oserror();
	}
	ksym_a = ksym_tmp;
    }

    ksym_a_sz = ix;

    qsort(ksym_a, ksym_a_sz, sizeof(struct ksym), ksym_compare_name);

    fclose(fp);

#if PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2) {
	fprintf(stderr, "symbols from ksyms ...\n");
	for (ix = 0; ix < ksym_a_sz; ix++) {
	    fprintf(stderr, "ksym[%d] " PRINTF_P_PFX "%p %s", ix, (void *)ksym_a[ix].addr, ksym_a[ix].name);
	    if (ksym_a[ix].module != NULL) fprintf(stderr, " [%s]", ksym_a[ix].module);
	    fprintf(stderr, "\n");
	}
    }
#endif

    return ksym_a_sz;
}

static int
read_sysmap(const char *release, __psint_t end_addr)
{
    char	inbuf[256], path[MAXPATHLEN], **fmt;
    struct ksym	*ksym_tmp;
    __psint_t	addr;
    int		ix, res, e;
    int		l = 0;
    char	*ip;
    char	*sp;
    int		major, minor, patch;
    FILE	*fp;
    char	*bestpath = NULL;
    int		ksym_mismatch_count;
    char *sysmap_paths[] = {	/* Paths to check for System.map file */
	"%s/boot/System.map-%s",
	"%s/boot/System.map",
	"%s/lib/modules/%s/System.map",
	"%s/usr/src/linux/System.map",
	"%s/System.map",
	NULL
    };

    /* Create version symbol name to look for in System.map */
    if (sscanf(release, "%d.%d.%d", &major, &minor, &patch) < 3 )
	return -1;
    sprintf(inbuf, "Version_%u", KERNEL_VERSION(major, minor, patch));

    /*
     * Walk through System.map path list looking for one that matches
     * either _end from /proc/ksyms or the uts version.
     */
    for (fmt = sysmap_paths; *fmt; fmt++) {
	snprintf(path, MAXPATHLEN, *fmt, proc_statspath, release);
	if ((fp = fopen(path, "r"))) {
	    if ((e = validate_sysmap(fp, inbuf, end_addr)) != 0) {
		if (e == 2) {
		    /* matched _end, so this is the right System.map */
		    if (bestpath)
		    	free(bestpath);
		    bestpath = strdup(path);
		}
		else
		if (e == 1 && !bestpath)
		    bestpath = strdup(path);
	    }
	    fclose(fp);
	    if (e == 2) {
		/* _end matched => don't look any further */
	    	break;
	    }
	}
    }

    if (bestpath)
	fprintf(stderr, "NOTICE: using \"%s\" for kernel symbols map.\n", bestpath);
    else {
	/* Didn't find a valid System.map */
	fprintf(stderr, "Warning: Valid System.map file not found!\n");
	fprintf(stderr, "Warning: proc.psinfo.wchan_s symbol names cannot be derived!\n");
	fprintf(stderr, "Warning: Addresses will be returned for proc.psinfo.wchan_s instead!\n");
	/* Free symbol array */
	for (ix = 0; ix < ksym_a_sz; ix++) {
		if (ksym_a[ix].name)
			free(ksym_a[ix].name);
		if (ksym_a[ix].module)
			free(ksym_a[ix].module);
	}
	free(ksym_a);
	ksym_a = NULL;
	ksym_a_sz = 0;
	return -1;
    }

    /* scan the System map */
    if ((fp = proc_statsfile(bestpath, path, sizeof(path))) == NULL)
    	return -oserror();

    ix = ksym_a_sz;

    /* Read each line in System.map */
    ksym_mismatch_count = 0;
    while (fgets(inbuf, sizeof(inbuf), fp) != NULL) {
	/*
	 * System.map lines look like this on ia32 ...
	 *
	 * c010a320 T disable_irq_nosync
	 *
	 * else on ia64 ...
	 *
	 * e002000000014c80 T disable_irq_nosync
	 */

	if (strstr(inbuf, "\n") == NULL) {
	    fprintf(stderr, "read_sysmap: truncated System.map line [%d]: %s\n", l-1, inbuf);
	    continue;
	}

	/* Increase array size, if necessary */
	if (ksym_a_sz < ix+1) {
	    ksym_a_sz += INCR_KSIZE;
	    ksym_tmp = (struct ksym *)realloc(ksym_a, ksym_a_sz * sizeof(struct ksym));
	    if (ksym_tmp == NULL) {
		free(ksym_a);
		goto fail;
	    }
	    ksym_a = ksym_tmp;
	}

	ip = inbuf;
	/* parse over address */
	while (isxdigit((int)*ip)) ip++;

	if (!isspace((int)*ip) || ip-inbuf < 4) {
	    /* bad format line */
#if PCP_DEBUG
	    if (pmDebug & DBG_TRACE_APPL2) {
		fprintf(stderr, "read_sysmap: bad addr? %c[%d] line=\"%s\"\n", *ip, (int)(ip-inbuf), inbuf);
	    }
#endif
	    continue;
	}

	sscanf(inbuf, "%p", (void **)&addr);

	while (isblank((int)*ip)) ip++;

	/* Only interested in symbol types that map to code addresses,
	 * so: t, T, W or A
	 */
	if (*ip != 't' && *ip != 'T' && *ip != 'W' && *ip != 'A')
	    continue;

	ip++;
	while (isblank((int)*ip)) ip++;

	/* next should be the symbol name */
	sp = ip++;
	while (!isblank((int)*ip) && *ip != '\n') ip++;
	*ip = '\0';

	/* Determine if symbol is already in ksym array.
	   If so, make sure the addresses match. */
	res = find_dup_name(ix - 1, addr, sp);
	if (res == KSYM_NOT_FOUND) { /* add it */
	    ksym_a[ix].name = strdup(sp);
	    if (ksym_a[ix].name == NULL)
		goto fail;
	    ksym_a[ix].addr = addr;
	    ix++;
	}
	else if (res == KSYM_FOUND_MISMATCH) {
	    if (ksym_mismatch_count++ < KSYM_MISMATCH_MAX_ALLOWED) {
		/*
		 * ia64 function pointer descriptors make this validation
		 * next to useless. So only report the first 
		 * KSYM_MISMATCH_MAX_ALLOWED mismatches found.
		 */
		fprintf(stderr, "Warning: mismatch for \"%s\" between System.map"
				" and /proc/ksyms.\n", sp);
	    }
	}
    }

    if (ksym_mismatch_count > KSYM_MISMATCH_MAX_ALLOWED) {
	fprintf(stderr, "Warning: only reported first %d out of %d mismatches "
			"between System.map and /proc/ksyms.\n",
	    KSYM_MISMATCH_MAX_ALLOWED, ksym_mismatch_count);
    }

    /* release unused ksym array entries */
    ksym_tmp = (struct ksym *)realloc(ksym_a, ix * sizeof(struct ksym));
    if (ksym_tmp == NULL) {
	free(ksym_a);
	goto fail;
    }
    ksym_a = ksym_tmp;
    ksym_a_sz = ix;

    qsort(ksym_a, ksym_a_sz, sizeof(struct ksym), ksym_compare_addr);

#if PCP_DEBUG
    if (pmDebug & DBG_TRACE_APPL2) {
	fprintf(stderr, "symbols from ksyms + sysmap ...\n");
	for (ix = 0; ix < ksym_a_sz; ix++) {
	    fprintf(stderr, "ksym[%d] " PRINTF_P_PFX "%p %s", ix, (void *)ksym_a[ix].addr, ksym_a[ix].name);
	    if (ksym_a[ix].module != NULL) fprintf(stderr, " [%s]", ksym_a[ix].module);
	    fprintf(stderr, "\n");
	}
    }
#endif

    fclose(fp);

    return ksym_a_sz;

fail:
    e = -oserror();
    if (fp)
	fclose(fp);
    return e;
}

void
read_ksym_sources(const char *release) 
{
    __psint_t end_addr;

    if (read_ksyms(&end_addr) > 0)	/* read /proc/ksyms first */
	read_sysmap(release, end_addr);	/* then System.map  */
}