File: ksyms.c

package info (click to toggle)
kernel-source-2.2.19 2.2.19.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 92,116 kB
  • ctags: 276,892
  • sloc: ansic: 1,710,377; asm: 58,705; makefile: 10,198; sh: 2,398; perl: 907; tcl: 570; lisp: 218; cpp: 186; awk: 133; sed: 72
file content (294 lines) | stat: -rw-r--r-- 6,994 bytes parent folder | download | duplicates (8)
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
/*
	ksyms.c.

	Process ksyms for ksymoops.

	Copyright Keith Owens <kaos@ocs.com.au>.
	Released under the GNU Public Licence, Version 2.

	Fri Nov  6 10:38:42 EST 1998
	Version 0.6b
	Remove false warnings when comparing ksyms and lsmod.

	Tue Nov  3 02:31:01 EST 1998
	Version 0.6
	Read lsmod (/proc/modules).
	Move "Using_Version" copy to map.c.

	Wed Oct 28 13:47:23 EST 1998
	Version 0.4
	Split into separate sources.
 */

#include "ksymoops.h"
#include <malloc.h>
#include <string.h>

/* Scan one line from ksyms.  Split lines into the base symbols and the module
 * symbols.  Separate ss for base and each module.
 */
static void scan_ksyms_line(const char *line)
{
	int i;
	char **string = NULL;
	SYMBOL_SET *ssp;
	static char *prev_module = NULL;
	static regex_t     re_ksyms;
	static regmatch_t *re_ksyms_pmatch;
	static char const procname[] = "scan_ksyms_line";

	/* ksyms: address, symbol, optional module */
	re_compile(&re_ksyms,
		"^([0-9a-fA-F]{4,}) +([^ \t]+)([ \t]+\\[([^ ]+)\\])?$",
		REG_NEWLINE|REG_EXTENDED,
		&re_ksyms_pmatch);

	i = regexec(&re_ksyms, line,
		    re_ksyms.re_nsub+1, re_ksyms_pmatch, 0);
	if (debug > 3)
		fprintf(stderr, "DEBUG: %s regexec %d\n", procname, i);
	if (i)
		return;

	/* string [1] - address, [2] - symbol, [3] - white space+module,
	 * [4] - module.
	 */
	re_strings(&re_ksyms, line, re_ksyms_pmatch, &string);
	if (string[4]) {
		if (!prev_module || strcmp(prev_module, string[4])) {
			/* start of a new module in ksyms */
			++ss_ksyms_modules;
			ss_ksyms_module = realloc(ss_ksyms_module,
				ss_ksyms_modules*sizeof(*ss_ksyms_module));
			if (!ss_ksyms_module)
				malloc_error("realloc ss_ksyms_module");
			ssp = ss_ksyms_module+ss_ksyms_modules-1;
			ss_init(ssp, string[4]);
			prev_module = strdup(string[4]);
			if (!prev_module)
				malloc_error("strdup prev_module");
		}
		ssp = ss_ksyms_module+ss_ksyms_modules-1;
	}
	else
		ssp = &ss_ksyms_base;
	add_symbol(ssp, string[1], ' ', 1, string[2]);
	re_strings_free(&re_ksyms, &string);
}

/* Read the symbols from ksyms.  */
void read_ksyms(const char *ksyms)
{
	FILE *f;
	char *line = NULL;
	int i, size;
	static char const procname[] = "read_ksyms";

	if (!ksyms)
		return;
	ss_init(&ss_ksyms_base, "ksyms_base");
	if (debug)
		fprintf(stderr, "DEBUG: %s %s\n", procname, ksyms);

	if (!regular_file(ksyms, procname))
		return;

	if (!(f = fopen_local(ksyms, "r", procname)))
		return;

	while (fgets_local(&line, &size, f, procname))
		scan_ksyms_line(line);

	fclose_local(f, procname);
	free(line);

	for (i = 0; i < ss_ksyms_modules; ++i) {
		ss_sort_na(ss_ksyms_module+i);
		extract_Version(ss_ksyms_module+i);
	}
	if (ss_ksyms_base.used) {
		ss_sort_na(&ss_ksyms_base);
		extract_Version(&ss_ksyms_base);
	}
	else {
		fprintf(stderr,
			"Warning, no kernel symbols in ksyms, is %s a valid "
			"ksyms file?\n",
			ksyms);
		++warnings;
	}

	if (debug > 1) {
		for (i = 0; i < ss_ksyms_modules; ++i) {
			fprintf(stderr,
				"DEBUG: %s %s used %d out of %d entries\n",
				procname,
				ss_ksyms_module[i].source,
				ss_ksyms_module[i].used,
				ss_ksyms_module[i].alloc);
		}
		fprintf(stderr,
			"DEBUG: %s %s used %d out of %d entries\n",
			procname, ss_ksyms_base.source, ss_ksyms_base.used,
			ss_ksyms_base.alloc);
	}
}

/* Map each ksyms module entry to the corresponding object entry.  Tricky,
 * see the comments in the docs about needing a unique symbol in each
 * module.
 */
static void map_ksym_to_module(SYMBOL_SET *ss)
{
	int i, j, matches;
	char *name = NULL;

	for (i = 0; i < ss->used; ++i) {
		matches = 0;
		for (j = 0; j < ss_objects; ++j) {
			name = (ss->symbol)[i].name;
			if (find_symbol_name(ss_object+j, name, NULL)) {
				++matches;
				ss->related = ss_object+j;
			}
		}
		if (matches == 1)
			break;		/* unique symbol over all objects */
		ss->related = NULL;	/* keep looking */
	}
	if (!(ss->related)) {
		fprintf(stderr,
			"Warning: cannot match loaded module %s to any "
			"module object.  Trace may not be reliable.\n",
			ss->source);
		++warnings;
	}
	else if (debug)
		fprintf(stderr,
			"DEBUG: ksyms %s matches to %s based on unique "
			"symbol %s\n",
			ss->source, ss->related->source, name);
}

/* Map all ksyms module entries to their corresponding objects */
void map_ksyms_to_modules(void)
{
	int i;
	SYMBOL_SET *ss, *ssc;

	for (i = 0; i < ss_ksyms_modules; ++i) {
		ss = ss_ksyms_module+i;
		map_ksym_to_module(ss);
		if (ss->related) {
			ssc = adjust_object_offsets(ss);
			compare_maps(ss, ssc, 1);
		}
	}
}

/* Read the modules from lsmod.  */
void read_lsmod(const char *lsmod)
{
	FILE *f;
	char *line = NULL;
	int i, size;
	char **string = NULL;
	static regex_t     re_lsmod;
	static regmatch_t *re_lsmod_pmatch;
	static char const procname[] = "read_lsmod";

	if (!lsmod)
		return;
	ss_init(&ss_lsmod, "lsmod");
	if (debug)
		fprintf(stderr, "DEBUG: %s %s\n", procname, lsmod);

	if (!regular_file(lsmod, procname))
		return;

	if (!(f = fopen_local(lsmod, "r", procname)))
		return;

	/* lsmod: module, size, use count, optional used by */
	re_compile(&re_lsmod,
		"^"
		"[ \t]*([^ \t]+)"				/* 1 module */
		"[ \t]*([^ \t]+)"				/* 2 size */
		"[ \t]*([^ \t]+)"				/* 3 count */
		"[ \t]*(.*)"					/* 4 used by */
		"$",
		REG_NEWLINE|REG_EXTENDED,
		&re_lsmod_pmatch);

	while (fgets_local(&line, &size, f, procname)) {
		i = regexec(&re_lsmod, line,
			    re_lsmod.re_nsub+1, re_lsmod_pmatch, 0);
		if (debug > 3)
			fprintf(stderr, "DEBUG: %s regexec %d\n", procname, i);
		if (i)
			continue;
		re_strings(&re_lsmod, line, re_lsmod_pmatch, &string);
		add_symbol(&ss_lsmod, string[2], ' ', 1, string[1]);
	}

	fclose_local(f, procname);
	free(line);
	re_strings_free(&re_lsmod, &string);
	if (ss_lsmod.used)
		ss_sort_na(&ss_lsmod);
	else {
		fprintf(stderr,
			"Warning, no symbols in lsmod, is %s a valid "
			"lsmod file?\n",
			lsmod);
		++warnings;
	}

	if (debug > 1)
		fprintf(stderr,
			"DEBUG: %s %s used %d out of %d entries\n",
			procname, ss_lsmod.source, ss_lsmod.used,
			ss_lsmod.alloc);
}

/* Compare modules from ksyms against module list in lsmod and vice versa.
 * There is one ss_ for each ksyms module and a single ss_lsmod to cross
 * check.
 */
void compare_ksyms_lsmod(void)
{
	int i, j;
	SYMBOL_SET *ss;
	SYMBOL *s;
	static char const procname[] = "compare_ksyms_lsmod";

	if (!(ss_lsmod.used && ss_ksyms_modules))
		return;

	s = ss_lsmod.symbol;
	for (i = 0; i < ss_lsmod.used; ++i, ++s) {
		for (j = 0; j < ss_ksyms_modules; ++j) {
			ss = ss_ksyms_module+j;
			if (strcmp(s->name, ss->source) == 0)
				break;
		}
		if (j >= ss_ksyms_modules) {
			fprintf(stderr,
				"Warning in %s, module %s is in lsmod but not "
				"in ksyms, probably no symbols exported\n",
				procname, s->name);
			++warnings;
		}
	}

	for (i = 0; i < ss_ksyms_modules; ++i) {
		ss = ss_ksyms_module+i;
		if (!find_symbol_name(&ss_lsmod, ss->source, NULL)) {
			fprintf(stderr,
				"Error in %s, module %s is in ksyms but not "
				"in lsmod\n",
				procname, ss->source);
			++errors;
		}
	}
}