File: makedumpfile.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 (328 lines) | stat: -rw-r--r-- 7,812 bytes parent folder | download
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
/* 
 * makedumpfile.c
 * 
 * This code is for reading a dumpfile ganarated by makedumpfile command.
 *
 * Copyright (C) 2011  NEC Soft, Ltd.
 *
 * 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.
 *
 * Author: Ken'ichi Ohmichi <oomichi mxs nes nec co jp>
 */

#include "defs.h"
#include "makedumpfile.h"
#include <byteswap.h>

int flattened_format = 0;

struct flat_data {
	int64_t off_flattened;
	int64_t off_rearranged; /* offset which will be rearranged. */
	int64_t buf_size;
};

struct all_flat_data {
	unsigned long long	num_array;
	struct flat_data	*array;
	size_t			file_size;
};

struct all_flat_data afd;

struct makedumpfile_header fh_save;

static int
is_bigendian(void)
{
	int i = 0x12345678;

	if (*(char *)&i == 0x12)
		return TRUE;
	else
		return FALSE;
}

static unsigned long long
store_flat_data_array(char *file, struct flat_data **fda)
{
	int			result = FALSE, fd;
	int64_t			offset_fdh;
	unsigned long long	num_allocated = 0;
	unsigned long long	num_stored    = 0;
	unsigned long long	size_allocated;
	struct flat_data	*ptr = NULL, *cur, *new;
	struct makedumpfile_data_header	fdh;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		error(INFO, "unable to open dump file %s\n", file);
		return -1;
	}
	if (lseek(fd, MAX_SIZE_MDF_HEADER, SEEK_SET) < 0) {
		error(INFO, "%s: seek error (flat format)\n", file);
		close(fd);
		return -1;
	}
	while (1) {
		if (num_allocated <= num_stored) {
			num_allocated += 100;
			size_allocated = sizeof(struct flat_data)
					 * num_allocated;
			new = realloc(ptr, size_allocated);
			if (new == NULL) {
				error(INFO, 
				    "unable to realloc flat_data structures\n");
				break;
			}
			ptr = new;
		}
		offset_fdh = lseek(fd, 0x0, SEEK_CUR);

		if (read(fd, &fdh, sizeof(fdh)) < 0) {
			error(INFO, "read error: %s (flat format)\n", file);
			break;
		}
		if (!is_bigendian()){
			fdh.offset   = bswap_64(fdh.offset);
			fdh.buf_size = bswap_64(fdh.buf_size);
		}
		if (fdh.offset == END_FLAG_FLAT_HEADER) {
			result = TRUE;
			break;
		}
		cur = ptr + num_stored;
		cur->off_flattened  = offset_fdh + sizeof(fdh);
		cur->off_rearranged = fdh.offset;
		cur->buf_size       = fdh.buf_size;
		num_stored++;

		/* seek for next makedumpfile_data_header. */
		if (lseek(fd, fdh.buf_size, SEEK_CUR) < 0) {
			error(INFO, "%s: seek error (flat format)\n", file);
			break;
		}
	}
	close(fd);
	if (result == FALSE) {
		free(ptr);
		return -1;
	}
	*fda = ptr;

	return num_stored;
}

static void
sort_flat_data_array(struct flat_data **fda, unsigned long long num_fda)
{
	unsigned long long	i, j;
	struct flat_data	tmp, *cur_i, *cur_j;

	for (i = 0; i < num_fda - 1; i++) {
		for (j = i + 1; j < num_fda; j++) {
			cur_i = *fda + i;
			cur_j = *fda + j;

			if (cur_i->off_rearranged < cur_j->off_rearranged)
				continue;

			tmp.off_flattened  = cur_i->off_flattened;
			tmp.off_rearranged = cur_i->off_rearranged;
			tmp.buf_size       = cur_i->buf_size;

			cur_i->off_flattened  = cur_j->off_flattened;
			cur_i->off_rearranged = cur_j->off_rearranged;
			cur_i->buf_size       = cur_j->buf_size;

			cur_j->off_flattened  = tmp.off_flattened;
			cur_j->off_rearranged = tmp.off_rearranged;
			cur_j->buf_size       = tmp.buf_size;
		}
	}
}

static int
read_all_makedumpfile_data_header(char *file)
{
	unsigned long long	num;
	struct flat_data	*fda = NULL;
	long long retval;

	retval = num = store_flat_data_array(file, &fda);
	if (retval < 0)
		return FALSE;

	sort_flat_data_array(&fda, num);

	afd.num_array = num;
	afd.array     = fda;

	return TRUE;
}

void
check_flattened_format(char *file)
{
	int fd;
	struct stat stat;
	struct makedumpfile_header fh;

	if (flattened_format)
		return;

	if (file_exists(file, &stat) && S_ISCHR(stat.st_mode))
		return;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		error(INFO, "unable to open dump file %s\n", file);
		return;
	}
	if (read(fd, &fh, sizeof(fh)) < 0) {
		error(INFO, "unable to read dump file %s\n", file);
		close(fd);
		return;
	}
	close(fd);

	if (!is_bigendian()){
		fh.type    = bswap_64(fh.type);
		fh.version = bswap_64(fh.version);
	}
	if ((strncmp(fh.signature, MAKEDUMPFILE_SIGNATURE, sizeof(MAKEDUMPFILE_SIGNATURE)) != 0) || 
	    (fh.type != TYPE_FLAT_HEADER))
		return;

	if (!read_all_makedumpfile_data_header(file))
		return;

	if (CRASHDEBUG(1))
		fprintf(fp, "%s: FLAT\n\n", file);

	fh_save = fh;

	flattened_format = TRUE;
}

static int
read_raw_dump_file(int fd, off_t offset, void *buf, size_t size)
{
	if (lseek(fd, offset, SEEK_SET) < 0) {
		if (CRASHDEBUG(1))
			error(INFO, "read_raw_dump_file: lseek error (flat format)\n");
		return FALSE;
	}
	if (read(fd, buf, size) < size) {
		if (CRASHDEBUG(1))
			error(INFO, "read_raw_dump_file: read error (flat format)\n");
		return FALSE;
	}

	return TRUE;
}

int
read_flattened_format(int fd, off_t offset, void *buf, size_t size)
{
	unsigned long long	index, index_start, index_end;
	int64_t			range_start, range_end;
	size_t			read_size, remain_size;
	off_t			offset_read;
	struct flat_data	*ptr;

	index_start = 0;
	index_end   = afd.num_array;

	while (1) {
		index = (index_start + index_end) / 2;
		ptr = afd.array + index;
		range_start = ptr->off_rearranged;
		range_end   = ptr->off_rearranged + ptr->buf_size;

		if ((range_start <= offset) && (offset < range_end)) {
			/* Found a corresponding array. */
			offset_read = (offset - range_start) + ptr->off_flattened;

			if (offset + size <= range_end) {
				if (!read_raw_dump_file(fd, offset_read, buf, size))
					return FALSE;
				break;
			}

			/* Searh other array corresponding to remaining data. */
			read_size   = range_end - offset;
			remain_size = size - read_size;
			if (!read_raw_dump_file(fd, offset_read, buf, read_size))
				return FALSE;
			if (!read_flattened_format(fd, offset + read_size,
					(char *)buf + read_size, remain_size))
				return FALSE;
			break;

		} else if ((index == index_start) &&
			   (index_start + 1 == index_end)) {
			/*
			 * Try to read not-written area. That is a common case,
			 * because the area might be skipped by lseek().
			 * This area should be the data filled with zero.
			 */
			ptr = afd.array + index_end;
			if (offset + size <= ptr->off_rearranged) {
				memset(buf, 0x0, size);
			} else {
				read_size   = ptr->off_rearranged - offset;
				remain_size = size - read_size;
				memset(buf, 0x0, read_size);
				if (!read_flattened_format(fd,
						offset + read_size,
						(char *)buf + read_size,
						remain_size))
					return FALSE;
			}
			break;

		} else if (offset < ptr->off_rearranged)
			index_end   = index;
		else
			index_start = index;
	}
	return TRUE;
}

int
is_flattened_format(char *file)
{
	check_flattened_format(file);
	return flattened_format;
}

void
dump_flat_header(FILE *ofp)
{
        int i;

	fprintf(ofp, "makedumpfile header:\n");
	fprintf(ofp, "          signature: \"");
	for (i = 0; i < SIG_LEN_MDF; i++) {
		if (!fh_save.signature[i])
			break;
		fprintf(ofp, "%c", fh_save.signature[i]);
	}
	fprintf(ofp, "\"\n");
	fprintf(ofp, "               type: %llx\n", (ulonglong)fh_save.type);
	fprintf(ofp, "            version: %llx\n", (ulonglong)fh_save.version);

	fprintf(ofp, "      all_flat_data:\n");
	fprintf(ofp, "          num_array: %lld\n", (ulonglong)afd.num_array);
	fprintf(ofp, "              array: %lx\n", (ulong)afd.array);
	fprintf(ofp, "          file_size: %ld\n\n", (ulong)afd.file_size);
}