File: va_server_v1.c

package info (click to toggle)
crash 7.0.8-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,348 kB
  • ctags: 13,100
  • sloc: ansic: 135,839; makefile: 495
file content (366 lines) | stat: -rwxr-xr-x 9,454 bytes parent folder | download | duplicates (2)
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
/* va_server_v1.c - kernel crash dump file translation library
 *
 * Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
 * Copyright (C) 2002, 2003, 2004, 2005 David Anderson
 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, 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.
 *
 * 11/12/99, Dave Winchell, Preserve V1 interface.
 */
#include <zlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "va_server.h"
#include <errno.h>
#include <sys/mman.h>
#include <signal.h>

struct map_hdr_v1 *vas_map_base_v1 = (struct map_hdr_v1 *)0;     /* base of tree */


#ifdef NOT_DEF
#define trunc_page(x)   ((void *)(((unsigned long)(x)) & ~((unsigned long)(page_size - 1))))
#define round_page(x)   trunc_page(((unsigned long)(x)) + ((unsigned long)(page_size - 1)))
#endif

extern u_long vas_base_va;
extern u_long vas_start_va;
u_long vas_end_va;

void find_data_v1(u_long va, u_long *buf, u_long *len, u_long *offset);
void load_data_v1(struct map_hdr_v1 *hdr, u_long index, u_long *buf, u_long *len);
struct map_hdr_v1 *find_header_v1(u_long va);
u_long vas_find_start_v1(void);
u_long vas_find_end_v1(void);
int read_maps_v1(char *crash_file);
int read_map_v1(int blk_pos);

extern int Page_Size;

extern FILE *vas_file_p;

extern void *malloc(size_t);


int va_server_init_v1(char *crash_file, u_long *start, u_long *end, u_long *stride)
{
	if(read_maps_v1(crash_file))
		return -1;

	vas_base_va = vas_start_va = vas_find_start_v1();
	vas_end_va = vas_find_end_v1();

	if(start)
		*start = vas_start_va;
	if(end)
		*end = vas_end_va;
	if(stride)
		*stride = vas_map_base_v1->va_per_entry;
	return 0;
}

int vas_lseek_v1(u_long position, int whence)
{
	if(whence != SEEK_SET)
		return -1;
	if(position > (vas_end_va - vas_start_va)) {
		printf("position 0x%lx beyond dump range of 0x%lx\n",
		       position, (vas_end_va - vas_start_va));
		return -1;
	}
	vas_base_va = vas_start_va + position;
	return 0;
}
size_t vas_read_v1(void *buf_in, size_t count)
{
	u_long len, offset, buf, va;
	u_long num, output, remaining;


	if(count > (vas_end_va - vas_base_va)) {
		printf("count 0x%lx greater than remaining dump of 0x%lx\n",
		       (ulong)count, (vas_end_va - vas_base_va));
		return -1;
	}
	va = vas_base_va;
	remaining = count;
	output = (u_long)buf_in;

	while(remaining) {
		find_data_v1(va, &buf, &len, &offset);
		num = (remaining > (len - offset)) ? (len - offset) : remaining;
		bcopy((const void *)(buf+offset), (void *)output, num);
		remaining -= num;
		va += num;
		output += num;
	}
	vas_base_va += count;
	return count;
}
size_t vas_write_v1(void *buf_in, size_t count)
{
	u_long len, offset, buf, va;

	if(count != sizeof(u_long)) {
		printf("count %d not %d\n", (int)count, (int)sizeof(u_long));
		return -1;
	}
	va = vas_base_va;
	find_data_v1(va, &buf, &len, &offset);
	*(u_long *)(buf+offset) = *(u_long *)buf_in;

	vas_base_va += count;
	return count;
}


void find_data_v1(u_long va, u_long *buf, u_long *len, u_long *offset)
{
	struct map_hdr_v1 *hdr;
	u_long index, off;

	hdr = find_header_v1(va);
	index = (va - hdr->start_va) / hdr->va_per_entry;
	off = (va - hdr->start_va) % hdr->va_per_entry;
	load_data_v1(hdr, index, buf, len);
	if(offset)
		*offset = off;
}
void vas_free_data_v1(u_long va)
{
	struct map_hdr_v1 *hdr;
	u_long index;

	hdr = find_header_v1(va);
	index = (va - hdr->start_va) / hdr->va_per_entry;

	if(hdr->map[index].exp_data) {
		free((void *)hdr->map[index].exp_data);
		hdr->map[index].exp_data = 0;
	}
}
void load_data_v1(struct map_hdr_v1 *hdr, u_long index, u_long *buf, u_long *len)
{
	char *compr_buf;
	char *exp_buf;
	int ret, items;
	uLongf destLen;

	if(hdr->map[index].exp_data)
		goto out;
	ret = fseek(vas_file_p, (long)((hdr->blk_offset + hdr->map[index].start_blk) * hdr->blk_size),
		    SEEK_SET);

	if(ret == -1) {
		printf("load_data: unable to fseek, errno = %d\n", ferror(vas_file_p));
		clean_exit(1);
	}
	compr_buf =  (char *)malloc(2*hdr->va_per_entry);
	if(!compr_buf) {
		printf("load_data: bad ret from malloc, errno = %d\n", ferror(vas_file_p));
		clean_exit(1);
	}
	items = fread((void *)compr_buf, sizeof(char), hdr->map[index].num_blks * hdr->blk_size, vas_file_p);
	if(items != hdr->map[index].num_blks * hdr->blk_size) {
		printf("unable to read blocks from errno = %d\n", ferror(vas_file_p));
		clean_exit(1);
	}
	hdr->map[index].exp_data = exp_buf =  (char *)malloc(hdr->va_per_entry);
	if(!exp_buf) {
		printf("load_data: bad ret from malloc, errno = %d\n", ferror(vas_file_p));
		clean_exit(1);
	}
	destLen = (uLongf)(2*hdr->va_per_entry);
	ret = uncompress((Bytef *)exp_buf, &destLen, (const Bytef *)compr_buf, (uLong)items);
	/*	if(destLen != hdr->va_per_entry) {
		 printf("uncompress error\n");
		 exit(1);
	}
	*/
	if(ret) {
		if(ret == Z_MEM_ERROR)
			printf("load_data, bad ret Z_MEM_ERROR from uncompress\n");
		else if(ret == Z_BUF_ERROR)
			printf("load_data, bad ret Z_BUF_ERROR from uncompress\n");
		else if(ret == Z_DATA_ERROR)
			printf("load_data, bad ret Z_DATA_ERROR from uncompress\n");
		else
			printf("load_data, bad ret %d from uncompress\n", ret);
		
		clean_exit(1);
	}
	free((void *)compr_buf);
  out:
	if(buf)
		*buf = (u_long)hdr->map[index].exp_data;
	if(len)
		*len = hdr->va_per_entry;
	return;
}

struct map_hdr_v1 *find_header_v1(u_long va)
{
	struct map_hdr_v1 *hdr;
	int found = 0;

	for(hdr = vas_map_base_v1; hdr; hdr = hdr->next)
		if((va >= hdr->start_va) && (va < hdr->end_va)) {
			found = 1;
			break;
		}
	if(found)
		return  hdr;
	else
		return (struct map_hdr_v1 *)0;
}
u_long vas_find_start_v1(void)
{
	struct map_hdr_v1 *hdr;
	u_long start;

	start = vas_map_base_v1->start_va;
	for(hdr = vas_map_base_v1; hdr; hdr = hdr->next)
		if(hdr->start_va < start)
			start = hdr->start_va;

	return start;
}
u_long vas_find_end_v1(void)
{
	struct map_hdr_v1 *hdr;
	u_long end;

	end = vas_map_base_v1->end_va;
	for(hdr = vas_map_base_v1; hdr; hdr = hdr->next)
		if(hdr->end_va > end)
			end = hdr->end_va;

	return end;
}
int read_maps_v1(char *crash_file)
{
	int *cur_entry_p;
	int ret, items, blk_pos;

	cur_entry_p = (int *)malloc(Page_Size);
	if(!cur_entry_p) {
		printf("read_maps: bad ret from malloc, errno = %d\n", ferror(vas_file_p));
		clean_exit(1);
	}
	bzero((void *)cur_entry_p, Page_Size);

	vas_file_p = fopen(crash_file, "r");
	if(vas_file_p == (FILE *)0) {
		printf("read_maps: bad ret from fopen for %s: %s\n", crash_file, strerror(errno));
		return -1;
	}
	ret = fseek(vas_file_p, (long)0, SEEK_SET);
	if(ret == -1) {
		printf("read_maps: unable to fseek in  %s, errno = %d\n", crash_file, ferror(vas_file_p));
		return -1;
	}	
	items = fread((void *)cur_entry_p, 1, Page_Size, vas_file_p);
	if(items != Page_Size) {
		printf("read_maps: unable to read header from %s, errno = %d\n", crash_file, ferror(vas_file_p));
		return -1;
	}
	ret = -1;
	while ((blk_pos = *cur_entry_p++)) {
		if (read_map_v1(blk_pos))
			return -1;
		ret = 0;
	}

	return ret;
}




int read_map_v1(int blk_pos)
{
	struct crash_map_hdr_v1 *disk_hdr;
	int ret, items;
	struct map_hdr_v1 *hdr, *hdr1;
	extern int console(char *, ...);

	hdr = (struct map_hdr_v1 *)malloc(sizeof(struct map_hdr_v1));
	if(!hdr) {
		printf("read_map: unable to malloc mem\n");
		return -1;
	}
	bzero((void *)hdr, sizeof(struct map_hdr_v1));
	disk_hdr = (struct crash_map_hdr_v1 *)malloc(Page_Size);
	ret = fseek(vas_file_p, (long)(blk_pos*Page_Size), SEEK_SET);
	if(ret == -1) {
		console("va_server: unable to fseek, err = %d\n", ferror(vas_file_p));
		free(disk_hdr);
		return -1;
	}
	items = fread((void *)disk_hdr, 1, Page_Size, vas_file_p);
	if(items != Page_Size) {
		return -1;
	}
	if(disk_hdr->magic[0] != CRASH_MAGIC) {
		console("va_server: bad magic 0x%lx\n", disk_hdr->magic[0]);
		return -1;
	}
	ret = fseek(vas_file_p, (long)((blk_pos + disk_hdr->map_block) * disk_hdr->blk_size), SEEK_SET);

	if(ret == -1) {
		printf("va_server: unable to fseek, err = %d\n", ferror(vas_file_p));
		return -1;
	}

	hdr->map_entries = disk_hdr->map_entries;
	hdr->va_per_entry = disk_hdr->va_per_entry;
	hdr->blk_offset = blk_pos - CRASH_OFFSET_BLKS;
	hdr->blk_size = disk_hdr->blk_size;
	Page_Size = disk_hdr->blk_size;    /* over-ride PAGE_SIZE */

	hdr->map = (struct crash_map_entry_v1 *)malloc(hdr->map_entries *
							 sizeof(struct crash_map_entry_v1));
	items = fread((void *)hdr->map, sizeof(struct crash_map_entry_v1), hdr->map_entries,
		      vas_file_p);
	if(items != hdr->map_entries) {
		printf("unable to read map entries, err = %d\n", errno);
		return -1;
	}

	hdr->start_va = hdr->map[0].start_va;
	hdr->end_va = hdr->start_va + hdr->map_entries * hdr->va_per_entry;

	if(!vas_map_base_v1) {
		vas_map_base_v1 = hdr;
		hdr->next = (struct map_hdr_v1 *)0;
	}
	else {
		hdr1 = vas_map_base_v1;
		while(hdr1->next)
			hdr1 = hdr1->next;
		hdr1->next = hdr;
		hdr->next = (struct map_hdr_v1 *)0;
	}

	free((void *)disk_hdr);
	return 0;

}