Print this page
9709 Remove support for BZIP2 from dump
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/savecore/savecore.c
+++ new/usr/src/cmd/savecore/savecore.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1983, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2016 Joyent, Inc.
24 24 */
25 25 /*
26 - * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26 + * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
27 27 */
28 28
29 29 #include <stdio.h>
30 30 #include <stdlib.h>
31 31 #include <stdarg.h>
32 32 #include <unistd.h>
33 33 #include <fcntl.h>
34 34 #include <errno.h>
35 35 #include <string.h>
36 36 #include <deflt.h>
37 37 #include <time.h>
38 38 #include <syslog.h>
39 39 #include <stropts.h>
40 40 #include <pthread.h>
41 41 #include <limits.h>
42 42 #include <atomic.h>
43 43 #include <libnvpair.h>
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
44 44 #include <libintl.h>
45 45 #include <sys/mem.h>
46 46 #include <sys/statvfs.h>
47 47 #include <sys/dumphdr.h>
48 48 #include <sys/dumpadm.h>
49 49 #include <sys/compress.h>
50 50 #include <sys/panic.h>
51 51 #include <sys/sysmacros.h>
52 52 #include <sys/stat.h>
53 53 #include <sys/resource.h>
54 -#include <bzip2/bzlib.h>
55 54 #include <sys/fm/util.h>
56 55 #include <fm/libfmevent.h>
57 56 #include <sys/int_fmtio.h>
58 57
59 58
60 59 /* fread/fwrite buffer size */
61 60 #define FBUFSIZE (1ULL << 20)
62 61
63 62 /* minimum size for output buffering */
64 63 #define MINCOREBLKSIZE (1ULL << 17)
65 64
66 65 /* create this file if metrics collection is enabled in the kernel */
67 66 #define METRICSFILE "METRICS.csv"
68 67
69 68 static char progname[9] = "savecore";
70 69 static char *savedir; /* savecore directory */
71 70 static char *dumpfile; /* source of raw crash dump */
72 71 static long bounds = -1; /* numeric suffix */
73 72 static long pagesize; /* dump pagesize */
74 73 static int dumpfd = -1; /* dumpfile descriptor */
75 74 static boolean_t have_dumpfile = B_TRUE; /* dumpfile existence */
76 75 static dumphdr_t corehdr, dumphdr; /* initial and terminal dumphdrs */
77 76 static boolean_t dump_incomplete; /* dumphdr indicates incomplete */
78 77 static boolean_t fm_panic; /* dump is the result of fm_panic */
79 78 static offset_t endoff; /* offset of end-of-dump header */
80 79 static int verbose; /* chatty mode */
81 80 static int disregard_valid_flag; /* disregard valid flag */
82 81 static int livedump; /* dump the current running system */
83 82 static int interactive; /* user invoked; no syslog */
84 83 static int csave; /* save dump compressed */
85 84 static int filemode; /* processing file, not dump device */
86 85 static int percent_done; /* progress indicator */
87 86 static int sec_done; /* progress last report time */
88 87 static hrtime_t startts; /* timestamp at start */
89 88 static volatile uint64_t saved; /* count of pages written */
90 89 static volatile uint64_t zpages; /* count of zero pages not written */
91 90 static dumpdatahdr_t datahdr; /* compression info */
92 91 static long coreblksize; /* preferred write size (st_blksize) */
93 92 static int cflag; /* run as savecore -c */
94 93 static int mflag; /* run as savecore -m */
95 94
96 95 /*
97 96 * Payload information for the events we raise. These are used
98 97 * in raise_event to determine what payload to include.
99 98 */
100 99 #define SC_PAYLOAD_SAVEDIR 0x0001 /* Include savedir in event */
101 100 #define SC_PAYLOAD_INSTANCE 0x0002 /* Include bounds instance number */
102 101 #define SC_PAYLOAD_IMAGEUUID 0x0004 /* Include dump OS instance uuid */
103 102 #define SC_PAYLOAD_CRASHTIME 0x0008 /* Include epoch crashtime */
104 103 #define SC_PAYLOAD_PANICSTR 0x0010 /* Include panic string */
105 104 #define SC_PAYLOAD_PANICSTACK 0x0020 /* Include panic string */
106 105 #define SC_PAYLOAD_FAILREASON 0x0040 /* Include failure reason */
107 106 #define SC_PAYLOAD_DUMPCOMPLETE 0x0080 /* Include completeness indicator */
108 107 #define SC_PAYLOAD_ISCOMPRESSED 0x0100 /* Dump is in vmdump.N form */
109 108 #define SC_PAYLOAD_DUMPADM_EN 0x0200 /* Is dumpadm enabled or not? */
110 109 #define SC_PAYLOAD_FM_PANIC 0x0400 /* Panic initiated by FMA */
111 110 #define SC_PAYLOAD_JUSTCHECKING 0x0800 /* Run with -c flag? */
112 111
113 112 enum sc_event_type {
114 113 SC_EVENT_DUMP_PENDING,
115 114 SC_EVENT_SAVECORE_FAILURE,
116 115 SC_EVENT_DUMP_AVAILABLE
117 116 };
118 117
119 118 /*
120 119 * Common payload
121 120 */
122 121 #define _SC_PAYLOAD_CMN \
123 122 SC_PAYLOAD_IMAGEUUID | \
124 123 SC_PAYLOAD_CRASHTIME | \
125 124 SC_PAYLOAD_PANICSTR | \
126 125 SC_PAYLOAD_PANICSTACK | \
127 126 SC_PAYLOAD_DUMPCOMPLETE | \
128 127 SC_PAYLOAD_FM_PANIC | \
129 128 SC_PAYLOAD_SAVEDIR
130 129
131 130 static const struct {
132 131 const char *sce_subclass;
133 132 uint32_t sce_payload;
134 133 } sc_event[] = {
135 134 /*
136 135 * SC_EVENT_DUMP_PENDING
137 136 */
138 137 {
139 138 "dump_pending_on_device",
140 139 _SC_PAYLOAD_CMN | SC_PAYLOAD_DUMPADM_EN |
141 140 SC_PAYLOAD_JUSTCHECKING
142 141 },
143 142
144 143 /*
145 144 * SC_EVENT_SAVECORE_FAILURE
146 145 */
147 146 {
148 147 "savecore_failure",
149 148 _SC_PAYLOAD_CMN | SC_PAYLOAD_INSTANCE | SC_PAYLOAD_FAILREASON
150 149 },
151 150
152 151 /*
153 152 * SC_EVENT_DUMP_AVAILABLE
154 153 */
155 154 {
156 155 "dump_available",
157 156 _SC_PAYLOAD_CMN | SC_PAYLOAD_INSTANCE | SC_PAYLOAD_ISCOMPRESSED
158 157 },
159 158 };
160 159
161 160 static void raise_event(enum sc_event_type, char *);
162 161
163 162 static void
164 163 usage(void)
165 164 {
166 165 (void) fprintf(stderr,
167 166 "usage: %s [-Lvd] [-f dumpfile] [dirname]\n", progname);
168 167 exit(1);
169 168 }
170 169
171 170 #define SC_SL_NONE 0x0001 /* no syslog */
172 171 #define SC_SL_ERR 0x0002 /* syslog if !interactive, LOG_ERR */
173 172 #define SC_SL_WARN 0x0004 /* syslog if !interactive, LOG_WARNING */
174 173 #define SC_IF_VERBOSE 0x0008 /* message only if -v */
175 174 #define SC_IF_ISATTY 0x0010 /* message only if interactive */
176 175 #define SC_EXIT_OK 0x0020 /* exit(0) */
177 176 #define SC_EXIT_ERR 0x0040 /* exit(1) */
178 177 #define SC_EXIT_PEND 0x0080 /* exit(2) */
179 178 #define SC_EXIT_FM 0x0100 /* exit(3) */
180 179
181 180 #define _SC_ALLEXIT (SC_EXIT_OK | SC_EXIT_ERR | SC_EXIT_PEND | SC_EXIT_FM)
182 181
183 182 static void
184 183 logprint(uint32_t flags, char *message, ...)
185 184 {
186 185 va_list args;
187 186 char buf[1024];
188 187 int do_always = ((flags & (SC_IF_VERBOSE | SC_IF_ISATTY)) == 0);
189 188 int do_ifverb = (flags & SC_IF_VERBOSE) && verbose;
190 189 int do_ifisatty = (flags & SC_IF_ISATTY) && interactive;
191 190 int code;
192 191 static int logprint_raised = 0;
193 192
194 193 if (do_always || do_ifverb || do_ifisatty) {
195 194 va_start(args, message);
196 195 /*LINTED: E_SEC_PRINTF_VAR_FMT*/
197 196 (void) vsnprintf(buf, sizeof (buf), message, args);
198 197 (void) fprintf(stderr, "%s: %s\n", progname, buf);
199 198 if (!interactive) {
200 199 switch (flags & (SC_SL_NONE | SC_SL_ERR | SC_SL_WARN)) {
201 200 case SC_SL_ERR:
202 201 /*LINTED: E_SEC_PRINTF_VAR_FMT*/
203 202 syslog(LOG_ERR, buf);
204 203 break;
205 204
206 205 case SC_SL_WARN:
207 206 /*LINTED: E_SEC_PRINTF_VAR_FMT*/
208 207 syslog(LOG_WARNING, buf);
209 208 break;
210 209
211 210 default:
212 211 break;
213 212 }
214 213 }
215 214 va_end(args);
216 215 }
217 216
218 217 switch (flags & _SC_ALLEXIT) {
219 218 case 0:
220 219 return;
221 220
222 221 case SC_EXIT_OK:
223 222 code = 0;
224 223 break;
225 224
226 225 case SC_EXIT_PEND:
227 226 /*
228 227 * Raise an ireport saying why we are exiting. Do not
229 228 * raise if run as savecore -m. If something in the
230 229 * raise_event codepath calls logprint avoid recursion.
231 230 */
232 231 if (!mflag && logprint_raised++ == 0)
233 232 raise_event(SC_EVENT_SAVECORE_FAILURE, buf);
234 233 code = 2;
235 234 break;
236 235
237 236 case SC_EXIT_FM:
238 237 code = 3;
239 238 break;
240 239
241 240 case SC_EXIT_ERR:
242 241 default:
243 242 if (!mflag && logprint_raised++ == 0 && have_dumpfile)
244 243 raise_event(SC_EVENT_SAVECORE_FAILURE, buf);
245 244 code = 1;
246 245 break;
247 246 }
248 247
249 248 exit(code);
250 249 }
251 250
252 251 /*
253 252 * System call / libc wrappers that exit on error.
254 253 */
255 254 static int
256 255 Open(const char *name, int oflags, mode_t mode)
257 256 {
258 257 int fd;
259 258
260 259 if ((fd = open64(name, oflags, mode)) == -1)
261 260 logprint(SC_SL_ERR | SC_EXIT_ERR, "open(\"%s\"): %s",
262 261 name, strerror(errno));
263 262 return (fd);
264 263 }
265 264
266 265 static void
267 266 Fread(void *buf, size_t size, FILE *f)
268 267 {
269 268 if (fread(buf, size, 1, f) != 1)
270 269 logprint(SC_SL_ERR | SC_EXIT_ERR, "fread: %s",
271 270 strerror(errno));
272 271 }
273 272
274 273 static void
275 274 Fwrite(void *buf, size_t size, FILE *f)
276 275 {
277 276 if (fwrite(buf, size, 1, f) != 1)
278 277 logprint(SC_SL_ERR | SC_EXIT_ERR, "fwrite: %s",
279 278 strerror(errno));
280 279 }
281 280
282 281 static void
283 282 Fseek(offset_t off, FILE *f)
284 283 {
285 284 if (fseeko64(f, off, SEEK_SET) != 0)
286 285 logprint(SC_SL_ERR | SC_EXIT_ERR, "fseeko64: %s",
287 286 strerror(errno));
288 287 }
289 288
290 289 typedef struct stat64 Stat_t;
291 290
292 291 static void
293 292 Fstat(int fd, Stat_t *sb, const char *fname)
294 293 {
295 294 if (fstat64(fd, sb) != 0)
296 295 logprint(SC_SL_ERR | SC_EXIT_ERR, "fstat(\"%s\"): %s", fname,
297 296 strerror(errno));
298 297 }
299 298
300 299 static void
301 300 Stat(const char *fname, Stat_t *sb)
302 301 {
303 302 if (stat64(fname, sb) != 0) {
304 303 have_dumpfile = B_FALSE;
305 304 logprint(SC_SL_ERR | SC_EXIT_ERR, "failed to get status "
306 305 "of file %s", fname);
307 306 }
308 307 }
309 308
310 309 static void
311 310 Pread(int fd, void *buf, size_t size, offset_t off)
312 311 {
313 312 ssize_t sz = pread64(fd, buf, size, off);
314 313
315 314 if (sz < 0)
316 315 logprint(SC_SL_ERR | SC_EXIT_ERR,
317 316 "pread: %s", strerror(errno));
318 317 else if (sz != size)
319 318 logprint(SC_SL_ERR | SC_EXIT_ERR,
320 319 "pread: size %ld != %ld", sz, size);
321 320 }
322 321
323 322 static void
324 323 Pwrite(int fd, void *buf, size_t size, off64_t off)
325 324 {
326 325 if (pwrite64(fd, buf, size, off) != size)
327 326 logprint(SC_SL_ERR | SC_EXIT_ERR, "pwrite: %s",
328 327 strerror(errno));
329 328 }
330 329
331 330 static void *
332 331 Zalloc(size_t size)
333 332 {
334 333 void *buf;
335 334
336 335 if ((buf = calloc(size, 1)) == NULL)
337 336 logprint(SC_SL_ERR | SC_EXIT_ERR, "calloc: %s",
338 337 strerror(errno));
339 338 return (buf);
340 339 }
341 340
342 341 static long
343 342 read_number_from_file(const char *filename, long default_value)
344 343 {
345 344 long file_value = -1;
346 345 FILE *fp;
347 346
348 347 if ((fp = fopen(filename, "r")) != NULL) {
349 348 (void) fscanf(fp, "%ld", &file_value);
350 349 (void) fclose(fp);
351 350 }
352 351 return (file_value < 0 ? default_value : file_value);
353 352 }
354 353
355 354 static void
356 355 read_dumphdr(void)
357 356 {
358 357 if (filemode)
359 358 dumpfd = Open(dumpfile, O_RDONLY, 0644);
360 359 else
361 360 dumpfd = Open(dumpfile, O_RDWR | O_DSYNC, 0644);
362 361 endoff = llseek(dumpfd, -DUMP_OFFSET, SEEK_END) & -DUMP_OFFSET;
363 362 Pread(dumpfd, &dumphdr, sizeof (dumphdr), endoff);
364 363 Pread(dumpfd, &datahdr, sizeof (datahdr), endoff + sizeof (dumphdr));
365 364
366 365 pagesize = dumphdr.dump_pagesize;
367 366
368 367 if (dumphdr.dump_magic != DUMP_MAGIC)
369 368 logprint(SC_SL_NONE | SC_EXIT_PEND, "bad magic number %x",
370 369 dumphdr.dump_magic);
↓ open down ↓ |
306 lines elided |
↑ open up ↑ |
371 370
372 371 if ((dumphdr.dump_flags & DF_VALID) == 0 && !disregard_valid_flag)
373 372 logprint(SC_SL_NONE | SC_IF_VERBOSE | SC_EXIT_OK,
374 373 "dump already processed");
375 374
376 375 if (dumphdr.dump_version != DUMP_VERSION)
377 376 logprint(SC_SL_NONE | SC_IF_VERBOSE | SC_EXIT_PEND,
378 377 "dump version (%d) != %s version (%d)",
379 378 dumphdr.dump_version, progname, DUMP_VERSION);
380 379
380 + if (datahdr.dump_clevel > DUMP_CLEVEL_LZJB)
381 + logprint(SC_SL_NONE | SC_EXIT_PEND,
382 + "unsupported compression format (%d)", datahdr.dump_clevel);
383 +
381 384 if (dumphdr.dump_wordsize != DUMP_WORDSIZE)
382 385 logprint(SC_SL_NONE | SC_EXIT_PEND,
383 386 "dump is from %u-bit kernel - cannot save on %u-bit kernel",
384 387 dumphdr.dump_wordsize, DUMP_WORDSIZE);
385 388
386 389 if (datahdr.dump_datahdr_magic == DUMP_DATAHDR_MAGIC) {
387 390 if (datahdr.dump_datahdr_version != DUMP_DATAHDR_VERSION)
388 391 logprint(SC_SL_NONE | SC_IF_VERBOSE | SC_EXIT_PEND,
389 392 "dump data version (%d) != %s data version (%d)",
390 393 datahdr.dump_datahdr_version, progname,
391 394 DUMP_DATAHDR_VERSION);
392 395 } else {
393 396 (void) memset(&datahdr, 0, sizeof (datahdr));
394 397 datahdr.dump_maxcsize = pagesize;
395 398 }
396 399
397 400 /*
398 401 * Read the initial header, clear the valid bits, and compare headers.
399 402 * The main header may have been overwritten by swapping if we're
400 403 * using a swap partition as the dump device, in which case we bail.
401 404 */
402 405 Pread(dumpfd, &corehdr, sizeof (dumphdr_t), dumphdr.dump_start);
403 406
404 407 corehdr.dump_flags &= ~DF_VALID;
405 408 dumphdr.dump_flags &= ~DF_VALID;
406 409
407 410 if (memcmp(&corehdr, &dumphdr, sizeof (dumphdr_t)) != 0) {
408 411 /*
409 412 * Clear valid bit so we don't complain on every invocation.
410 413 */
411 414 if (!filemode)
412 415 Pwrite(dumpfd, &dumphdr, sizeof (dumphdr), endoff);
413 416 logprint(SC_SL_ERR | SC_EXIT_ERR,
414 417 "initial dump header corrupt");
415 418 }
416 419 }
417 420
418 421 static void
419 422 check_space(int csave)
420 423 {
421 424 struct statvfs fsb;
422 425 int64_t spacefree, dumpsize, minfree, datasize;
423 426
424 427 if (statvfs(".", &fsb) < 0)
425 428 logprint(SC_SL_ERR | SC_EXIT_ERR, "statvfs: %s",
426 429 strerror(errno));
427 430
428 431 dumpsize = dumphdr.dump_data - dumphdr.dump_start;
429 432 datasize = dumphdr.dump_npages * pagesize;
430 433 if (!csave)
431 434 dumpsize += datasize;
432 435 else
433 436 dumpsize += datahdr.dump_data_csize;
434 437
435 438 spacefree = (int64_t)fsb.f_bavail * fsb.f_frsize;
436 439 minfree = 1024LL * read_number_from_file("minfree", 1024);
437 440 if (spacefree < minfree + dumpsize) {
438 441 logprint(SC_SL_ERR | SC_EXIT_ERR,
439 442 "not enough space in %s (%lld MB avail, %lld MB needed)",
440 443 savedir, spacefree >> 20, (minfree + dumpsize) >> 20);
441 444 }
442 445 }
443 446
444 447 static void
445 448 build_dump_map(int corefd, const pfn_t *pfn_table)
446 449 {
447 450 long i;
448 451 static long misses = 0;
449 452 size_t dump_mapsize = (corehdr.dump_hashmask + 1) * sizeof (dump_map_t);
450 453 mem_vtop_t vtop;
451 454 dump_map_t *dmp = Zalloc(dump_mapsize);
452 455 char *inbuf = Zalloc(FBUFSIZE);
453 456 FILE *in = fdopen(dup(dumpfd), "rb");
454 457
455 458 (void) setvbuf(in, inbuf, _IOFBF, FBUFSIZE);
456 459 Fseek(dumphdr.dump_map, in);
457 460
458 461 corehdr.dump_data = corehdr.dump_map + roundup(dump_mapsize, pagesize);
459 462
460 463 for (i = 0; i < corehdr.dump_nvtop; i++) {
461 464 long first = 0;
462 465 long last = corehdr.dump_npages - 1;
463 466 long middle = 0;
464 467 pfn_t pfn = 0;
465 468 uintptr_t h;
466 469
467 470 Fread(&vtop, sizeof (mem_vtop_t), in);
468 471 while (last >= first) {
469 472 middle = (first + last) / 2;
470 473 pfn = pfn_table[middle];
471 474 if (pfn == vtop.m_pfn)
472 475 break;
473 476 if (pfn < vtop.m_pfn)
474 477 first = middle + 1;
475 478 else
476 479 last = middle - 1;
477 480 }
478 481 if (pfn != vtop.m_pfn) {
479 482 if (++misses <= 10)
480 483 (void) fprintf(stderr,
481 484 "pfn %ld not found for as=%p, va=%p\n",
482 485 vtop.m_pfn, (void *)vtop.m_as, vtop.m_va);
483 486 continue;
484 487 }
485 488
486 489 dmp[i].dm_as = vtop.m_as;
487 490 dmp[i].dm_va = (uintptr_t)vtop.m_va;
488 491 dmp[i].dm_data = corehdr.dump_data +
489 492 ((uint64_t)middle << corehdr.dump_pageshift);
490 493
491 494 h = DUMP_HASH(&corehdr, dmp[i].dm_as, dmp[i].dm_va);
492 495 dmp[i].dm_next = dmp[h].dm_first;
493 496 dmp[h].dm_first = corehdr.dump_map + i * sizeof (dump_map_t);
494 497 }
495 498
496 499 Pwrite(corefd, dmp, dump_mapsize, corehdr.dump_map);
497 500 free(dmp);
498 501 (void) fclose(in);
499 502 free(inbuf);
500 503 }
501 504
502 505 /*
503 506 * Copy whole sections of the dump device to the file.
504 507 */
505 508 static void
506 509 Copy(offset_t dumpoff, len_t nb, offset_t *offp, int fd, char *buf,
507 510 size_t sz)
508 511 {
509 512 size_t nr;
510 513 offset_t off = *offp;
511 514
512 515 while (nb > 0) {
513 516 nr = sz < nb ? sz : (size_t)nb;
514 517 Pread(dumpfd, buf, nr, dumpoff);
515 518 Pwrite(fd, buf, nr, off);
516 519 off += nr;
517 520 dumpoff += nr;
518 521 nb -= nr;
519 522 }
520 523 *offp = off;
521 524 }
522 525
523 526 /*
524 527 * Copy pages when the dump data header is missing.
525 528 * This supports older kernels with latest savecore.
526 529 */
527 530 static void
528 531 CopyPages(offset_t *offp, int fd, char *buf, size_t sz)
529 532 {
530 533 uint32_t csize;
531 534 FILE *in = fdopen(dup(dumpfd), "rb");
532 535 FILE *out = fdopen(dup(fd), "wb");
533 536 char *cbuf = Zalloc(pagesize);
534 537 char *outbuf = Zalloc(FBUFSIZE);
535 538 pgcnt_t np = dumphdr.dump_npages;
536 539
537 540 (void) setvbuf(out, outbuf, _IOFBF, FBUFSIZE);
538 541 (void) setvbuf(in, buf, _IOFBF, sz);
539 542 Fseek(dumphdr.dump_data, in);
540 543
541 544 Fseek(*offp, out);
542 545 while (np > 0) {
543 546 Fread(&csize, sizeof (uint32_t), in);
544 547 Fwrite(&csize, sizeof (uint32_t), out);
545 548 *offp += sizeof (uint32_t);
546 549 if (csize > pagesize || csize == 0) {
547 550 logprint(SC_SL_ERR,
548 551 "CopyPages: page %lu csize %d (0x%x) pagesize %d",
549 552 dumphdr.dump_npages - np, csize, csize,
550 553 pagesize);
551 554 break;
552 555 }
553 556 Fread(cbuf, csize, in);
554 557 Fwrite(cbuf, csize, out);
555 558 *offp += csize;
556 559 np--;
557 560 }
558 561 (void) fclose(in);
559 562 (void) fclose(out);
560 563 free(outbuf);
561 564 free(buf);
562 565 }
563 566
564 567 /*
565 568 * Concatenate dump contents into a new file.
566 569 * Update corehdr with new offsets.
567 570 */
568 571 static void
569 572 copy_crashfile(const char *corefile)
570 573 {
571 574 int corefd = Open(corefile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
572 575 size_t bufsz = FBUFSIZE;
573 576 char *inbuf = Zalloc(bufsz);
574 577 offset_t coreoff;
575 578 size_t nb;
576 579
577 580 logprint(SC_SL_ERR | SC_IF_VERBOSE,
578 581 "Copying %s to %s/%s\n", dumpfile, savedir, corefile);
579 582
580 583 /*
581 584 * This dump file is still compressed
582 585 */
583 586 corehdr.dump_flags |= DF_COMPRESSED | DF_VALID;
584 587
585 588 /*
586 589 * Leave room for corehdr, it is updated and written last
587 590 */
588 591 corehdr.dump_start = 0;
589 592 coreoff = sizeof (corehdr);
590 593
591 594 /*
592 595 * Read in the compressed symbol table, copy it to corefile.
593 596 */
594 597 coreoff = roundup(coreoff, pagesize);
595 598 corehdr.dump_ksyms = coreoff;
596 599 Copy(dumphdr.dump_ksyms, dumphdr.dump_ksyms_csize, &coreoff, corefd,
597 600 inbuf, bufsz);
598 601
599 602 /*
600 603 * Save the pfn table.
601 604 */
602 605 coreoff = roundup(coreoff, pagesize);
603 606 corehdr.dump_pfn = coreoff;
604 607 Copy(dumphdr.dump_pfn, dumphdr.dump_npages * sizeof (pfn_t), &coreoff,
605 608 corefd, inbuf, bufsz);
606 609
607 610 /*
608 611 * Save the dump map.
609 612 */
610 613 coreoff = roundup(coreoff, pagesize);
611 614 corehdr.dump_map = coreoff;
612 615 Copy(dumphdr.dump_map, dumphdr.dump_nvtop * sizeof (mem_vtop_t),
613 616 &coreoff, corefd, inbuf, bufsz);
614 617
615 618 /*
616 619 * Save the data pages.
617 620 */
618 621 coreoff = roundup(coreoff, pagesize);
619 622 corehdr.dump_data = coreoff;
620 623 if (datahdr.dump_data_csize != 0)
621 624 Copy(dumphdr.dump_data, datahdr.dump_data_csize, &coreoff,
622 625 corefd, inbuf, bufsz);
623 626 else
624 627 CopyPages(&coreoff, corefd, inbuf, bufsz);
625 628
626 629 /*
627 630 * Now write the modified dump header to front and end of the copy.
628 631 * Make it look like a valid dump device.
629 632 *
630 633 * From dumphdr.h: Two headers are written out: one at the
631 634 * beginning of the dump, and the other at the very end of the
632 635 * dump device. The terminal header is at a known location
633 636 * (end of device) so we can always find it.
634 637 *
635 638 * Pad with zeros to each DUMP_OFFSET boundary.
636 639 */
637 640 (void) memset(inbuf, 0, DUMP_OFFSET);
638 641
639 642 nb = DUMP_OFFSET - (coreoff & (DUMP_OFFSET - 1));
640 643 if (nb > 0) {
641 644 Pwrite(corefd, inbuf, nb, coreoff);
642 645 coreoff += nb;
643 646 }
644 647
645 648 Pwrite(corefd, &corehdr, sizeof (corehdr), coreoff);
646 649 coreoff += sizeof (corehdr);
647 650
648 651 Pwrite(corefd, &datahdr, sizeof (datahdr), coreoff);
649 652 coreoff += sizeof (datahdr);
650 653
651 654 nb = DUMP_OFFSET - (coreoff & (DUMP_OFFSET - 1));
652 655 if (nb > 0) {
653 656 Pwrite(corefd, inbuf, nb, coreoff);
654 657 }
655 658
656 659 free(inbuf);
657 660 Pwrite(corefd, &corehdr, sizeof (corehdr), corehdr.dump_start);
658 661
659 662 /*
660 663 * Write out the modified dump header to the dump device.
661 664 * The dump device has been processed, so DF_VALID is clear.
662 665 */
663 666 if (!filemode)
664 667 Pwrite(dumpfd, &dumphdr, sizeof (dumphdr), endoff);
665 668
666 669 (void) close(corefd);
667 670 }
668 671
669 672 /*
670 673 * compressed streams
671 674 */
672 675 typedef struct blockhdr blockhdr_t;
673 676 typedef struct block block_t;
674 677
675 678 struct blockhdr {
676 679 block_t *head;
677 680 block_t *tail;
678 681 };
679 682
680 683 struct block {
681 684 block_t *next;
682 685 char *block;
683 686 int size;
684 687 };
685 688
686 689 typedef enum streamstate {
687 690 STREAMSTART,
688 691 STREAMPAGES
689 692 } streamstate_t;
690 693
691 694 typedef struct stream {
692 695 streamstate_t state;
↓ open down ↓ |
302 lines elided |
↑ open up ↑ |
693 696 int init;
694 697 int tag;
695 698 int bound;
696 699 int nout;
697 700 char *blkbuf;
698 701 blockhdr_t blocks;
699 702 pgcnt_t pagenum;
700 703 pgcnt_t curpage;
701 704 pgcnt_t npages;
702 705 pgcnt_t done;
703 - bz_stream strm;
704 706 dumpcsize_t sc;
705 707 dumpstreamhdr_t sh;
706 708 } stream_t;
707 709
708 710 static stream_t *streams;
709 711 static stream_t *endstreams;
710 712
711 713 const int cs = sizeof (dumpcsize_t);
712 714
713 715 typedef struct tinfo {
714 716 pthread_t tid;
715 717 int corefd;
716 718 } tinfo_t;
717 719
718 720 static int threads_stop;
719 721 static int threads_active;
720 722 static tinfo_t *tinfo;
721 723 static tinfo_t *endtinfo;
722 724
723 725 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
724 726 static pthread_cond_t cvfree = PTHREAD_COND_INITIALIZER;
725 727 static pthread_cond_t cvwork = PTHREAD_COND_INITIALIZER;
726 728 static pthread_cond_t cvbarrier = PTHREAD_COND_INITIALIZER;
727 729
728 730 static blockhdr_t freeblocks;
729 731
730 732 static void
731 733 enqt(blockhdr_t *h, block_t *b)
732 734 {
733 735 b->next = NULL;
734 736 if (h->tail == NULL)
735 737 h->head = b;
736 738 else
737 739 h->tail->next = b;
738 740 h->tail = b;
739 741 }
740 742
741 743 static block_t *
742 744 deqh(blockhdr_t *h)
743 745 {
744 746 block_t *b = h->head;
745 747
746 748 if (b != NULL) {
747 749 h->head = b->next;
748 750 if (h->head == NULL)
749 751 h->tail = NULL;
750 752 }
751 753 return (b);
752 754 }
753 755
754 756 static void *runstreams(void *arg);
755 757
756 758 static void
757 759 initstreams(int corefd, int nstreams, int maxcsize)
758 760 {
759 761 int nthreads;
760 762 int nblocks;
761 763 int i;
762 764 block_t *b;
763 765 tinfo_t *t;
764 766
765 767 nthreads = sysconf(_SC_NPROCESSORS_ONLN);
766 768 if (nstreams < nthreads)
767 769 nthreads = nstreams;
768 770 if (nthreads < 1)
769 771 nthreads = 1;
770 772 nblocks = nthreads * 2;
771 773
772 774 tinfo = Zalloc(nthreads * sizeof (tinfo_t));
773 775 endtinfo = &tinfo[nthreads];
774 776
775 777 /* init streams */
776 778 streams = Zalloc(nstreams * sizeof (stream_t));
777 779 endstreams = &streams[nstreams];
778 780
779 781 /* init stream block buffers */
780 782 for (i = 0; i < nblocks; i++) {
781 783 b = Zalloc(sizeof (block_t));
782 784 b->block = Zalloc(maxcsize);
783 785 enqt(&freeblocks, b);
784 786 }
785 787
786 788 /* init worker threads */
787 789 (void) pthread_mutex_lock(&lock);
788 790 threads_active = 1;
789 791 threads_stop = 0;
790 792 for (t = tinfo; t != endtinfo; t++) {
791 793 t->corefd = dup(corefd);
792 794 if (t->corefd < 0) {
793 795 nthreads = t - tinfo;
794 796 endtinfo = t;
795 797 break;
796 798 }
797 799 if (pthread_create(&t->tid, NULL, runstreams, t) != 0)
798 800 logprint(SC_SL_ERR | SC_EXIT_ERR, "pthread_create: %s",
799 801 strerror(errno));
800 802 }
801 803 (void) pthread_mutex_unlock(&lock);
802 804 }
803 805
804 806 static void
805 807 sbarrier()
806 808 {
807 809 stream_t *s;
808 810
809 811 (void) pthread_mutex_lock(&lock);
810 812 for (s = streams; s != endstreams; s++) {
811 813 while (s->bound || s->blocks.head != NULL)
812 814 (void) pthread_cond_wait(&cvbarrier, &lock);
813 815 }
814 816 (void) pthread_mutex_unlock(&lock);
815 817 }
816 818
817 819 static void
818 820 stopstreams()
819 821 {
820 822 tinfo_t *t;
821 823
822 824 if (threads_active) {
823 825 sbarrier();
824 826 (void) pthread_mutex_lock(&lock);
825 827 threads_stop = 1;
826 828 (void) pthread_cond_signal(&cvwork);
827 829 (void) pthread_mutex_unlock(&lock);
828 830 for (t = tinfo; t != endtinfo; t++)
829 831 (void) pthread_join(t->tid, NULL);
830 832 free(tinfo);
831 833 tinfo = NULL;
832 834 threads_active = 0;
833 835 }
834 836 }
835 837
836 838 static block_t *
837 839 getfreeblock()
838 840 {
839 841 block_t *b;
840 842
841 843 (void) pthread_mutex_lock(&lock);
842 844 while ((b = deqh(&freeblocks)) == NULL)
843 845 (void) pthread_cond_wait(&cvfree, &lock);
844 846 (void) pthread_mutex_unlock(&lock);
845 847 return (b);
846 848 }
847 849
848 850 /* data page offset from page number */
849 851 #define BTOP(b) ((b) >> dumphdr.dump_pageshift)
850 852 #define PTOB(p) ((p) << dumphdr.dump_pageshift)
851 853 #define DATAOFF(p) (corehdr.dump_data + PTOB(p))
852 854
853 855 /* check for coreblksize boundary */
854 856 static int
855 857 isblkbnd(pgcnt_t pgnum)
856 858 {
857 859 return (P2PHASE(DATAOFF(pgnum), coreblksize) == 0);
858 860 }
859 861
860 862 static int
861 863 iszpage(char *buf)
862 864 {
863 865 size_t sz;
864 866 uint64_t *pl;
865 867
866 868 /*LINTED:E_BAD_PTR_CAST_ALIGN*/
867 869 pl = (uint64_t *)(buf);
868 870 for (sz = 0; sz < pagesize; sz += sizeof (*pl))
869 871 if (*pl++ != 0)
870 872 return (0);
871 873 return (1);
872 874 }
873 875
874 876 volatile uint_t *hist;
875 877
876 878 /* write pages to the core file */
877 879 static void
878 880 putpage(int corefd, char *buf, pgcnt_t pgnum, pgcnt_t np)
879 881 {
880 882 atomic_inc_uint(&hist[np]);
881 883 if (np > 0)
882 884 Pwrite(corefd, buf, PTOB(np), DATAOFF(pgnum));
883 885 }
884 886
885 887 /*
886 888 * Process one lzjb block.
887 889 * No object (stream header or page) will be split over a block boundary.
888 890 */
889 891 static void
890 892 lzjbblock(int corefd, stream_t *s, char *block, size_t blocksz)
891 893 {
892 894 int in = 0;
893 895 int csize;
894 896 int doflush;
895 897 char *out;
896 898 size_t dsize;
897 899 dumpcsize_t sc;
898 900 dumpstreamhdr_t sh;
899 901
900 902 if (!s->init) {
901 903 s->init = 1;
902 904 if (s->blkbuf == NULL)
903 905 s->blkbuf = Zalloc(coreblksize);
904 906 s->state = STREAMSTART;
905 907 }
906 908 while (in < blocksz) {
907 909 switch (s->state) {
908 910 case STREAMSTART:
909 911 (void) memcpy(&sh, block + in, sizeof (sh));
910 912 in += sizeof (sh);
911 913 if (strcmp(DUMP_STREAM_MAGIC, sh.stream_magic) != 0)
912 914 logprint(SC_SL_ERR | SC_EXIT_ERR,
913 915 "LZJB STREAMSTART: bad stream header");
914 916 if (sh.stream_npages > datahdr.dump_maxrange)
915 917 logprint(SC_SL_ERR | SC_EXIT_ERR,
916 918 "LZJB STREAMSTART: bad range: %d > %d",
917 919 sh.stream_npages, datahdr.dump_maxrange);
918 920 s->pagenum = sh.stream_pagenum;
919 921 s->npages = sh.stream_npages;
920 922 s->curpage = s->pagenum;
921 923 s->nout = 0;
922 924 s->done = 0;
923 925 s->state = STREAMPAGES;
924 926 break;
925 927 case STREAMPAGES:
926 928 (void) memcpy(&sc, block + in, cs);
927 929 in += cs;
928 930 csize = DUMP_GET_CSIZE(sc);
929 931 if (csize > pagesize)
930 932 logprint(SC_SL_ERR | SC_EXIT_ERR,
931 933 "LZJB STREAMPAGES: bad csize=%d", csize);
932 934
933 935 out = s->blkbuf + PTOB(s->nout);
934 936 dsize = decompress(block + in, out, csize, pagesize);
935 937
936 938 if (dsize != pagesize)
937 939 logprint(SC_SL_ERR | SC_EXIT_ERR,
938 940 "LZJB STREAMPAGES: dsize %d != pagesize %d",
939 941 dsize, pagesize);
940 942
941 943 in += csize;
942 944 atomic_inc_64(&saved);
943 945
944 946 doflush = 0;
945 947 if (s->nout == 0 && iszpage(out)) {
946 948 doflush = 1;
947 949 atomic_inc_64(&zpages);
948 950 } else if (++s->nout >= BTOP(coreblksize) ||
949 951 isblkbnd(s->curpage + s->nout)) {
950 952 doflush = 1;
951 953 }
952 954 if (++s->done >= s->npages) {
953 955 s->state = STREAMSTART;
954 956 doflush = 1;
955 957 }
↓ open down ↓ |
242 lines elided |
↑ open up ↑ |
956 958 if (doflush) {
957 959 putpage(corefd, s->blkbuf, s->curpage, s->nout);
958 960 s->nout = 0;
959 961 s->curpage = s->pagenum + s->done;
960 962 }
961 963 break;
962 964 }
963 965 }
964 966 }
965 967
966 -/* bzlib library reports errors with this callback */
967 -void
968 -bz_internal_error(int errcode)
969 -{
970 - logprint(SC_SL_ERR | SC_EXIT_ERR, "bz_internal_error: err %s\n",
971 - BZ2_bzErrorString(errcode));
972 -}
973 -
974 -/*
975 - * Return one object in the stream.
976 - *
977 - * An object (stream header or page) will likely span an input block
978 - * of compression data. Return non-zero when an entire object has been
979 - * retrieved from the stream.
980 - */
981 -static int
982 -bz2decompress(stream_t *s, void *buf, size_t size)
983 -{
984 - int rc;
985 -
986 - if (s->strm.avail_out == 0) {
987 - s->strm.next_out = buf;
988 - s->strm.avail_out = size;
989 - }
990 - while (s->strm.avail_in > 0) {
991 - rc = BZ2_bzDecompress(&s->strm);
992 - if (rc == BZ_STREAM_END) {
993 - rc = BZ2_bzDecompressReset(&s->strm);
994 - if (rc != BZ_OK)
995 - logprint(SC_SL_ERR | SC_EXIT_ERR,
996 - "BZ2_bzDecompressReset: %s",
997 - BZ2_bzErrorString(rc));
998 - continue;
999 - }
1000 -
1001 - if (s->strm.avail_out == 0)
1002 - break;
1003 - }
1004 - return (s->strm.avail_out == 0);
1005 -}
1006 -
1007 -/*
1008 - * Process one bzip2 block.
1009 - * The interface is documented here:
1010 - * http://www.bzip.org/1.0.5/bzip2-manual-1.0.5.html
1011 - */
1012 -static void
1013 -bz2block(int corefd, stream_t *s, char *block, size_t blocksz)
1014 -{
1015 - int rc = 0;
1016 - int doflush;
1017 - char *out;
1018 -
1019 - if (!s->init) {
1020 - s->init = 1;
1021 - rc = BZ2_bzDecompressInit(&s->strm, 0, 0);
1022 - if (rc != BZ_OK)
1023 - logprint(SC_SL_ERR | SC_EXIT_ERR,
1024 - "BZ2_bzDecompressInit: %s", BZ2_bzErrorString(rc));
1025 - if (s->blkbuf == NULL)
1026 - s->blkbuf = Zalloc(coreblksize);
1027 - s->strm.avail_out = 0;
1028 - s->state = STREAMSTART;
1029 - }
1030 - s->strm.next_in = block;
1031 - s->strm.avail_in = blocksz;
1032 -
1033 - while (s->strm.avail_in > 0) {
1034 - switch (s->state) {
1035 - case STREAMSTART:
1036 - if (!bz2decompress(s, &s->sh, sizeof (s->sh)))
1037 - return;
1038 - if (strcmp(DUMP_STREAM_MAGIC, s->sh.stream_magic) != 0)
1039 - logprint(SC_SL_ERR | SC_EXIT_ERR,
1040 - "BZ2 STREAMSTART: bad stream header");
1041 - if (s->sh.stream_npages > datahdr.dump_maxrange)
1042 - logprint(SC_SL_ERR | SC_EXIT_ERR,
1043 - "BZ2 STREAMSTART: bad range: %d > %d",
1044 - s->sh.stream_npages, datahdr.dump_maxrange);
1045 - s->pagenum = s->sh.stream_pagenum;
1046 - s->npages = s->sh.stream_npages;
1047 - s->curpage = s->pagenum;
1048 - s->nout = 0;
1049 - s->done = 0;
1050 - s->state = STREAMPAGES;
1051 - break;
1052 - case STREAMPAGES:
1053 - out = s->blkbuf + PTOB(s->nout);
1054 - if (!bz2decompress(s, out, pagesize))
1055 - return;
1056 -
1057 - atomic_inc_64(&saved);
1058 -
1059 - doflush = 0;
1060 - if (s->nout == 0 && iszpage(out)) {
1061 - doflush = 1;
1062 - atomic_inc_64(&zpages);
1063 - } else if (++s->nout >= BTOP(coreblksize) ||
1064 - isblkbnd(s->curpage + s->nout)) {
1065 - doflush = 1;
1066 - }
1067 - if (++s->done >= s->npages) {
1068 - s->state = STREAMSTART;
1069 - doflush = 1;
1070 - }
1071 - if (doflush) {
1072 - putpage(corefd, s->blkbuf, s->curpage, s->nout);
1073 - s->nout = 0;
1074 - s->curpage = s->pagenum + s->done;
1075 - }
1076 - break;
1077 - }
1078 - }
1079 -}
1080 -
1081 968 /* report progress */
1082 969 static void
1083 970 report_progress()
1084 971 {
1085 972 int sec, percent;
1086 973
1087 974 if (!interactive)
1088 975 return;
1089 976
1090 977 percent = saved * 100LL / corehdr.dump_npages;
1091 978 sec = (gethrtime() - startts) / NANOSEC;
1092 979 if (percent > percent_done || sec > sec_done) {
1093 980 (void) printf("\r%2d:%02d %3d%% done", sec / 60, sec % 60,
1094 981 percent);
1095 982 (void) fflush(stdout);
1096 983 sec_done = sec;
1097 984 percent_done = percent;
1098 985 }
1099 986 }
1100 987
1101 988 /* thread body */
1102 989 static void *
1103 990 runstreams(void *arg)
1104 991 {
1105 992 tinfo_t *t = arg;
1106 993 stream_t *s;
1107 994 block_t *b;
1108 995 int bound;
1109 996
1110 997 (void) pthread_mutex_lock(&lock);
1111 998 while (!threads_stop) {
1112 999 bound = 0;
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
1113 1000 for (s = streams; s != endstreams; s++) {
1114 1001 if (s->bound || s->blocks.head == NULL)
1115 1002 continue;
1116 1003 s->bound = 1;
1117 1004 bound = 1;
1118 1005 (void) pthread_cond_signal(&cvwork);
1119 1006 while (s->blocks.head != NULL) {
1120 1007 b = deqh(&s->blocks);
1121 1008 (void) pthread_mutex_unlock(&lock);
1122 1009
1123 - if (datahdr.dump_clevel < DUMP_CLEVEL_BZIP2)
1124 - lzjbblock(t->corefd, s, b->block,
1125 - b->size);
1126 - else
1127 - bz2block(t->corefd, s, b->block,
1128 - b->size);
1010 + lzjbblock(t->corefd, s, b->block,
1011 + b->size);
1129 1012
1130 1013 (void) pthread_mutex_lock(&lock);
1131 1014 enqt(&freeblocks, b);
1132 1015 (void) pthread_cond_signal(&cvfree);
1133 1016
1134 1017 report_progress();
1135 1018 }
1136 1019 s->bound = 0;
1137 1020 (void) pthread_cond_signal(&cvbarrier);
1138 1021 }
1139 1022 if (!bound && !threads_stop)
1140 1023 (void) pthread_cond_wait(&cvwork, &lock);
1141 1024 }
1142 1025 (void) close(t->corefd);
1143 1026 (void) pthread_cond_signal(&cvwork);
1144 1027 (void) pthread_mutex_unlock(&lock);
1145 1028 return (arg);
1146 1029 }
1147 1030
1148 1031 /*
1149 1032 * Process compressed pages.
1150 1033 *
1151 1034 * The old format, now called single-threaded lzjb, is a 32-bit size
1152 1035 * word followed by 'size' bytes of lzjb compression data for one
1153 1036 * page. The new format extends this by storing a 12-bit "tag" in the
1154 1037 * upper bits of the size word. When the size word is pagesize or
1155 1038 * less, it is assumed to be one lzjb page. When the size word is
1156 1039 * greater than pagesize, it is assumed to be a "stream block",
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
1157 1040 * belonging to up to 4095 streams. In practice, the number of streams
1158 1041 * is set to one less than the number of CPUs running at crash
1159 1042 * time. One CPU processes the crash dump, the remaining CPUs
1160 1043 * separately process groups of data pages.
1161 1044 *
1162 1045 * savecore creates a thread per stream, but never more threads than
1163 1046 * the number of CPUs running savecore. This is because savecore can
1164 1047 * be processing a crash file from a remote machine, which may have
1165 1048 * more CPUs.
1166 1049 *
1167 - * When the kernel uses parallel lzjb or parallel bzip2, we expect a
1168 - * series of 128KB blocks of compression data. In this case, each
1169 - * block has a "tag", in the range 1-4095. Each block is handed off to
1170 - * to the threads running "runstreams". The dump format is either lzjb
1171 - * or bzip2, never a mixture. These threads, in turn, process the
1172 - * compression data for groups of pages. Groups of pages are delimited
1173 - * by a "stream header", which indicates a starting pfn and number of
1174 - * pages. When a stream block has been read, the condition variable
1175 - * "cvwork" is signalled, which causes one of the avaiable threads to
1176 - * wake up and process the stream.
1050 + * When the kernel uses parallel compression we expect a series of 128KB
1051 + * blocks of compression data. In this case, each block has a "tag" in
1052 + * the range 1-4095. Each block is handed off to the threads running
1053 + * "runstreams". These threads, in turn, process the compression data
1054 + * for groups of pages. Groups of pages are delimited by a "stream header",
1055 + * which indicates a starting pfn and number of pages. When a stream block
1056 + * has been read, the condition variable "cvwork" is signalled, which causes
1057 + * one of the available threads to wake up and process the stream.
1177 1058 *
1178 1059 * In the parallel case there will be streams blocks encoding all data
1179 1060 * pages. The stream of blocks is terminated by a zero size
1180 1061 * word. There can be a few lzjb pages tacked on the end, depending on
1181 1062 * the architecture. The sbarrier function ensures that all stream
1182 1063 * blocks have been processed so that the page number for the few
1183 1064 * single pages at the end can be known.
1184 1065 */
1185 1066 static void
1186 1067 decompress_pages(int corefd)
1187 1068 {
1188 1069 char *cpage = NULL;
1189 1070 char *dpage = NULL;
1190 1071 char *out;
1191 1072 pgcnt_t curpage = 0;
1192 1073 block_t *b;
1193 1074 FILE *dumpf;
1194 1075 FILE *tracef = NULL;
1195 1076 stream_t *s;
1196 1077 size_t dsize;
1197 1078 size_t insz = FBUFSIZE;
1198 1079 char *inbuf = Zalloc(insz);
1199 1080 uint32_t csize;
1200 1081 dumpcsize_t dcsize;
1201 1082 int nstreams = datahdr.dump_nstreams;
1202 1083 int maxcsize = datahdr.dump_maxcsize;
1203 1084 int nout = 0, tag, doflush;
1204 1085
1205 1086 dumpf = fdopen(dup(dumpfd), "rb");
1206 1087 if (dumpf == NULL)
1207 1088 logprint(SC_SL_ERR | SC_EXIT_ERR, "fdopen: %s",
1208 1089 strerror(errno));
1209 1090
1210 1091 (void) setvbuf(dumpf, inbuf, _IOFBF, insz);
1211 1092 Fseek(dumphdr.dump_data, dumpf);
1212 1093
1213 1094 /*LINTED: E_CONSTANT_CONDITION*/
1214 1095 while (1) {
1215 1096
1216 1097 /*
1217 1098 * The csize word delimits stream blocks.
1218 1099 * See dumphdr.h for a description.
1219 1100 */
1220 1101 Fread(&dcsize, sizeof (dcsize), dumpf);
1221 1102
1222 1103 tag = DUMP_GET_TAG(dcsize);
1223 1104 csize = DUMP_GET_CSIZE(dcsize);
1224 1105
1225 1106 if (tag != 0) { /* a stream block */
1226 1107
1227 1108 if (nstreams == 0)
1228 1109 logprint(SC_SL_ERR | SC_EXIT_ERR,
1229 1110 "starting data header is missing");
1230 1111
1231 1112 if (tag > nstreams)
1232 1113 logprint(SC_SL_ERR | SC_EXIT_ERR,
1233 1114 "stream tag %d not in range 1..%d",
1234 1115 tag, nstreams);
1235 1116
1236 1117 if (csize > maxcsize)
1237 1118 logprint(SC_SL_ERR | SC_EXIT_ERR,
1238 1119 "block size 0x%x > max csize 0x%x",
1239 1120 csize, maxcsize);
1240 1121
1241 1122 if (streams == NULL)
1242 1123 initstreams(corefd, nstreams, maxcsize);
1243 1124 s = &streams[tag - 1];
1244 1125 s->tag = tag;
1245 1126
1246 1127 b = getfreeblock();
1247 1128 b->size = csize;
1248 1129 Fread(b->block, csize, dumpf);
1249 1130
1250 1131 (void) pthread_mutex_lock(&lock);
1251 1132 enqt(&s->blocks, b);
1252 1133 if (!s->bound)
1253 1134 (void) pthread_cond_signal(&cvwork);
1254 1135 (void) pthread_mutex_unlock(&lock);
1255 1136
1256 1137 } else if (csize > 0) { /* one lzjb page */
1257 1138
1258 1139 if (csize > pagesize)
1259 1140 logprint(SC_SL_ERR | SC_EXIT_ERR,
1260 1141 "csize 0x%x > pagesize 0x%x",
1261 1142 csize, pagesize);
1262 1143
1263 1144 if (cpage == NULL)
1264 1145 cpage = Zalloc(pagesize);
1265 1146 if (dpage == NULL) {
1266 1147 dpage = Zalloc(coreblksize);
1267 1148 nout = 0;
1268 1149 }
1269 1150
1270 1151 Fread(cpage, csize, dumpf);
1271 1152
1272 1153 out = dpage + PTOB(nout);
1273 1154 dsize = decompress(cpage, out, csize, pagesize);
1274 1155
1275 1156 if (dsize != pagesize)
1276 1157 logprint(SC_SL_ERR | SC_EXIT_ERR,
1277 1158 "dsize 0x%x != pagesize 0x%x",
1278 1159 dsize, pagesize);
1279 1160
1280 1161 /*
1281 1162 * wait for streams to flush so that 'saved' is correct
1282 1163 */
1283 1164 if (threads_active)
1284 1165 sbarrier();
1285 1166
1286 1167 doflush = 0;
1287 1168 if (nout == 0)
1288 1169 curpage = saved;
1289 1170
1290 1171 atomic_inc_64(&saved);
1291 1172
1292 1173 if (nout == 0 && iszpage(dpage)) {
1293 1174 doflush = 1;
1294 1175 atomic_inc_64(&zpages);
1295 1176 } else if (++nout >= BTOP(coreblksize) ||
1296 1177 isblkbnd(curpage + nout) ||
1297 1178 saved >= dumphdr.dump_npages) {
1298 1179 doflush = 1;
1299 1180 }
1300 1181
1301 1182 if (doflush) {
1302 1183 putpage(corefd, dpage, curpage, nout);
1303 1184 nout = 0;
1304 1185 }
1305 1186
1306 1187 report_progress();
1307 1188
1308 1189 /*
1309 1190 * Non-streams lzjb does not use blocks. Stop
1310 1191 * here if all the pages have been decompressed.
1311 1192 */
1312 1193 if (saved >= dumphdr.dump_npages)
1313 1194 break;
1314 1195
1315 1196 } else {
1316 1197 break; /* end of data */
1317 1198 }
1318 1199 }
1319 1200
1320 1201 stopstreams();
1321 1202 if (tracef != NULL)
1322 1203 (void) fclose(tracef);
1323 1204 (void) fclose(dumpf);
1324 1205 if (inbuf)
1325 1206 free(inbuf);
1326 1207 if (cpage)
1327 1208 free(cpage);
1328 1209 if (dpage)
1329 1210 free(dpage);
1330 1211 if (streams)
1331 1212 free(streams);
1332 1213 }
1333 1214
1334 1215 static void
1335 1216 build_corefile(const char *namelist, const char *corefile)
1336 1217 {
1337 1218 size_t pfn_table_size = dumphdr.dump_npages * sizeof (pfn_t);
1338 1219 size_t ksyms_size = dumphdr.dump_ksyms_size;
1339 1220 size_t ksyms_csize = dumphdr.dump_ksyms_csize;
1340 1221 pfn_t *pfn_table;
1341 1222 char *ksyms_base = Zalloc(ksyms_size);
1342 1223 char *ksyms_cbase = Zalloc(ksyms_csize);
1343 1224 size_t ksyms_dsize;
1344 1225 Stat_t st;
1345 1226 int corefd = Open(corefile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1346 1227 int namefd = Open(namelist, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1347 1228
1348 1229 (void) printf("Constructing namelist %s/%s\n", savedir, namelist);
1349 1230
1350 1231 /*
1351 1232 * Determine the optimum write size for the core file
1352 1233 */
1353 1234 Fstat(corefd, &st, corefile);
1354 1235
1355 1236 if (verbose > 1)
1356 1237 (void) printf("%s: %ld block size\n", corefile,
1357 1238 (long)st.st_blksize);
1358 1239 coreblksize = st.st_blksize;
1359 1240 if (coreblksize < MINCOREBLKSIZE || !ISP2(coreblksize))
1360 1241 coreblksize = MINCOREBLKSIZE;
1361 1242
1362 1243 hist = Zalloc((sizeof (uint64_t) * BTOP(coreblksize)) + 1);
1363 1244
1364 1245 /*
1365 1246 * This dump file is now uncompressed
1366 1247 */
1367 1248 corehdr.dump_flags &= ~DF_COMPRESSED;
1368 1249
1369 1250 /*
1370 1251 * Read in the compressed symbol table, copy it to corefile,
1371 1252 * decompress it, and write the result to namelist.
1372 1253 */
1373 1254 corehdr.dump_ksyms = pagesize;
1374 1255 Pread(dumpfd, ksyms_cbase, ksyms_csize, dumphdr.dump_ksyms);
1375 1256 Pwrite(corefd, ksyms_cbase, ksyms_csize, corehdr.dump_ksyms);
1376 1257
1377 1258 ksyms_dsize = decompress(ksyms_cbase, ksyms_base, ksyms_csize,
1378 1259 ksyms_size);
1379 1260 if (ksyms_dsize != ksyms_size)
1380 1261 logprint(SC_SL_WARN,
1381 1262 "bad data in symbol table, %lu of %lu bytes saved",
1382 1263 ksyms_dsize, ksyms_size);
1383 1264
1384 1265 Pwrite(namefd, ksyms_base, ksyms_size, 0);
1385 1266 (void) close(namefd);
1386 1267 free(ksyms_cbase);
1387 1268 free(ksyms_base);
1388 1269
1389 1270 (void) printf("Constructing corefile %s/%s\n", savedir, corefile);
1390 1271
1391 1272 /*
1392 1273 * Read in and write out the pfn table.
1393 1274 */
1394 1275 pfn_table = Zalloc(pfn_table_size);
1395 1276 corehdr.dump_pfn = corehdr.dump_ksyms + roundup(ksyms_size, pagesize);
1396 1277 Pread(dumpfd, pfn_table, pfn_table_size, dumphdr.dump_pfn);
1397 1278 Pwrite(corefd, pfn_table, pfn_table_size, corehdr.dump_pfn);
1398 1279
1399 1280 /*
1400 1281 * Convert the raw translation data into a hashed dump map.
1401 1282 */
1402 1283 corehdr.dump_map = corehdr.dump_pfn + roundup(pfn_table_size, pagesize);
1403 1284 build_dump_map(corefd, pfn_table);
1404 1285 free(pfn_table);
1405 1286
1406 1287 /*
1407 1288 * Decompress the pages
1408 1289 */
1409 1290 decompress_pages(corefd);
1410 1291 (void) printf(": %ld of %ld pages saved\n", (pgcnt_t)saved,
1411 1292 dumphdr.dump_npages);
1412 1293
1413 1294 if (verbose)
1414 1295 (void) printf("%ld (%ld%%) zero pages were not written\n",
1415 1296 (pgcnt_t)zpages, (pgcnt_t)zpages * 100 /
1416 1297 dumphdr.dump_npages);
1417 1298
1418 1299 if (saved != dumphdr.dump_npages)
1419 1300 logprint(SC_SL_WARN, "bad data after page %ld", saved);
1420 1301
1421 1302 /*
1422 1303 * Write out the modified dump headers.
1423 1304 */
1424 1305 Pwrite(corefd, &corehdr, sizeof (corehdr), 0);
1425 1306 if (!filemode)
1426 1307 Pwrite(dumpfd, &dumphdr, sizeof (dumphdr), endoff);
1427 1308
1428 1309 (void) close(corefd);
1429 1310 }
1430 1311
1431 1312 /*
1432 1313 * When the system panics, the kernel saves all undelivered messages (messages
1433 1314 * that never made it out to syslogd(1M)) in the dump. At a mimimum, the
1434 1315 * panic message itself will always fall into this category. Upon reboot,
1435 1316 * the syslog startup script runs savecore -m to recover these messages.
1436 1317 *
1437 1318 * To do this, we read the unsent messages from the dump and send them to
1438 1319 * /dev/conslog on priority band 1. This has the effect of prepending them
1439 1320 * to any already-accumulated messages in the console backlog, thus preserving
1440 1321 * temporal ordering across the reboot.
1441 1322 *
1442 1323 * Note: since savecore -m is used *only* for this purpose, it does *not*
1443 1324 * attempt to save the crash dump. The dump will be saved later, after
1444 1325 * syslogd(1M) starts, by the savecore startup script.
1445 1326 */
1446 1327 static int
1447 1328 message_save(void)
1448 1329 {
1449 1330 offset_t dumpoff = -(DUMP_OFFSET + DUMP_LOGSIZE);
1450 1331 offset_t ldoff;
1451 1332 log_dump_t ld;
1452 1333 log_ctl_t lc;
1453 1334 struct strbuf ctl, dat;
1454 1335 int logfd;
1455 1336
1456 1337 logfd = Open("/dev/conslog", O_WRONLY, 0644);
1457 1338 dumpfd = Open(dumpfile, O_RDWR | O_DSYNC, 0644);
1458 1339 dumpoff = llseek(dumpfd, dumpoff, SEEK_END) & -DUMP_OFFSET;
1459 1340
1460 1341 ctl.buf = (void *)&lc;
1461 1342 ctl.len = sizeof (log_ctl_t);
1462 1343
1463 1344 dat.buf = Zalloc(DUMP_LOGSIZE);
1464 1345
1465 1346 for (;;) {
1466 1347 ldoff = dumpoff;
1467 1348
1468 1349 Pread(dumpfd, &ld, sizeof (log_dump_t), dumpoff);
1469 1350 dumpoff += sizeof (log_dump_t);
1470 1351 dat.len = ld.ld_msgsize;
1471 1352
1472 1353 if (ld.ld_magic == 0)
1473 1354 break;
1474 1355
1475 1356 if (ld.ld_magic != LOG_MAGIC)
1476 1357 logprint(SC_SL_ERR | SC_IF_VERBOSE | SC_EXIT_ERR,
1477 1358 "bad magic %x", ld.ld_magic);
1478 1359
1479 1360 if (dat.len >= DUMP_LOGSIZE)
1480 1361 logprint(SC_SL_ERR | SC_IF_VERBOSE | SC_EXIT_ERR,
1481 1362 "bad size %d", ld.ld_msgsize);
1482 1363
1483 1364 Pread(dumpfd, ctl.buf, ctl.len, dumpoff);
1484 1365 dumpoff += ctl.len;
1485 1366
1486 1367 if (ld.ld_csum != checksum32(ctl.buf, ctl.len))
1487 1368 logprint(SC_SL_ERR | SC_IF_VERBOSE | SC_EXIT_OK,
1488 1369 "bad log_ctl checksum");
1489 1370
1490 1371 lc.flags |= SL_LOGONLY;
1491 1372
1492 1373 Pread(dumpfd, dat.buf, dat.len, dumpoff);
1493 1374 dumpoff += dat.len;
1494 1375
1495 1376 if (ld.ld_msum != checksum32(dat.buf, dat.len))
1496 1377 logprint(SC_SL_ERR | SC_IF_VERBOSE | SC_EXIT_OK,
1497 1378 "bad message checksum");
1498 1379
1499 1380 if (putpmsg(logfd, &ctl, &dat, 1, MSG_BAND) == -1)
1500 1381 logprint(SC_SL_ERR | SC_EXIT_ERR, "putpmsg: %s",
1501 1382 strerror(errno));
1502 1383
1503 1384 ld.ld_magic = 0; /* clear magic so we never save twice */
1504 1385 Pwrite(dumpfd, &ld, sizeof (log_dump_t), ldoff);
1505 1386 }
1506 1387 return (0);
1507 1388 }
1508 1389
1509 1390 static long
1510 1391 getbounds(const char *f)
1511 1392 {
1512 1393 long b = -1;
1513 1394 const char *p = strrchr(f, '/');
1514 1395
1515 1396 if (p == NULL || strncmp(p, "vmdump", 6) != 0)
1516 1397 p = strstr(f, "vmdump");
1517 1398
1518 1399 if (p != NULL && *p == '/')
1519 1400 p++;
1520 1401
1521 1402 (void) sscanf(p ? p : f, "vmdump.%ld", &b);
1522 1403
1523 1404 return (b);
1524 1405 }
1525 1406
1526 1407 static void
1527 1408 stack_retrieve(char *stack)
1528 1409 {
1529 1410 summary_dump_t sd;
1530 1411 offset_t dumpoff = -(DUMP_OFFSET + DUMP_LOGSIZE +
1531 1412 DUMP_ERPTSIZE);
1532 1413 dumpoff -= DUMP_SUMMARYSIZE;
1533 1414
1534 1415 dumpfd = Open(dumpfile, O_RDWR | O_DSYNC, 0644);
1535 1416 dumpoff = llseek(dumpfd, dumpoff, SEEK_END) & -DUMP_OFFSET;
1536 1417
1537 1418 Pread(dumpfd, &sd, sizeof (summary_dump_t), dumpoff);
1538 1419 dumpoff += sizeof (summary_dump_t);
1539 1420
1540 1421 if (sd.sd_magic == 0) {
1541 1422 *stack = '\0';
1542 1423 return;
1543 1424 }
1544 1425
1545 1426 if (sd.sd_magic != SUMMARY_MAGIC) {
1546 1427 *stack = '\0';
1547 1428 logprint(SC_SL_NONE | SC_IF_VERBOSE,
1548 1429 "bad summary magic %x", sd.sd_magic);
1549 1430 return;
1550 1431 }
1551 1432 Pread(dumpfd, stack, STACK_BUF_SIZE, dumpoff);
1552 1433 if (sd.sd_ssum != checksum32(stack, STACK_BUF_SIZE))
1553 1434 logprint(SC_SL_NONE | SC_IF_VERBOSE, "bad stack checksum");
1554 1435 }
1555 1436
1556 1437 static void
1557 1438 raise_event(enum sc_event_type evidx, char *warn_string)
1558 1439 {
1559 1440 uint32_t pl = sc_event[evidx].sce_payload;
1560 1441 char panic_stack[STACK_BUF_SIZE];
1561 1442 nvlist_t *attr = NULL;
1562 1443 char uuidbuf[36 + 1];
1563 1444 int err = 0;
1564 1445
1565 1446 if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0)
1566 1447 goto publish; /* try to send payload-free event */
1567 1448
1568 1449 if (pl & SC_PAYLOAD_SAVEDIR && savedir != NULL)
1569 1450 err |= nvlist_add_string(attr, "dumpdir", savedir);
1570 1451
1571 1452 if (pl & SC_PAYLOAD_INSTANCE && bounds != -1)
1572 1453 err |= nvlist_add_int64(attr, "instance", bounds);
1573 1454
1574 1455 if (pl & SC_PAYLOAD_ISCOMPRESSED) {
1575 1456 err |= nvlist_add_boolean_value(attr, "compressed",
1576 1457 csave ? B_TRUE : B_FALSE);
1577 1458 }
1578 1459
1579 1460 if (pl & SC_PAYLOAD_DUMPADM_EN) {
1580 1461 char *disabled = defread("DUMPADM_ENABLE=no");
1581 1462
1582 1463 err |= nvlist_add_boolean_value(attr, "savecore-enabled",
1583 1464 disabled ? B_FALSE : B_TRUE);
1584 1465 }
1585 1466
1586 1467 if (pl & SC_PAYLOAD_IMAGEUUID) {
1587 1468 (void) strncpy(uuidbuf, corehdr.dump_uuid, 36);
1588 1469 uuidbuf[36] = '\0';
1589 1470 err |= nvlist_add_string(attr, "os-instance-uuid", uuidbuf);
1590 1471 }
1591 1472
1592 1473 if (pl & SC_PAYLOAD_CRASHTIME) {
1593 1474 err |= nvlist_add_int64(attr, "crashtime",
1594 1475 (int64_t)corehdr.dump_crashtime);
1595 1476 }
1596 1477
1597 1478 if (pl & SC_PAYLOAD_PANICSTR && corehdr.dump_panicstring[0] != '\0') {
1598 1479 err |= nvlist_add_string(attr, "panicstr",
1599 1480 corehdr.dump_panicstring);
1600 1481 }
1601 1482
1602 1483 if (pl & SC_PAYLOAD_PANICSTACK) {
1603 1484 stack_retrieve(panic_stack);
1604 1485
1605 1486 if (panic_stack[0] != '\0') {
1606 1487 /*
1607 1488 * The summary page may not be present if the dump
1608 1489 * was previously recorded compressed.
1609 1490 */
1610 1491 (void) nvlist_add_string(attr, "panicstack",
1611 1492 panic_stack);
1612 1493 }
1613 1494 }
1614 1495
1615 1496 /* add warning string if this is an ireport for dump failure */
1616 1497 if (pl & SC_PAYLOAD_FAILREASON && warn_string != NULL)
1617 1498 (void) nvlist_add_string(attr, "failure-reason", warn_string);
1618 1499
1619 1500 if (pl & SC_PAYLOAD_DUMPCOMPLETE)
1620 1501 err |= nvlist_add_boolean_value(attr, "dump-incomplete",
1621 1502 dump_incomplete ? B_TRUE : B_FALSE);
1622 1503
1623 1504 if (pl & SC_PAYLOAD_FM_PANIC) {
1624 1505 err |= nvlist_add_boolean_value(attr, "fm-panic",
1625 1506 fm_panic ? B_TRUE : B_FALSE);
1626 1507 }
1627 1508
1628 1509 if (pl & SC_PAYLOAD_JUSTCHECKING) {
1629 1510 err |= nvlist_add_boolean_value(attr, "will-attempt-savecore",
1630 1511 cflag ? B_FALSE : B_TRUE);
1631 1512 }
1632 1513
1633 1514 if (err)
1634 1515 logprint(SC_SL_WARN, "Errors while constructing '%s' "
1635 1516 "event payload; will try to publish anyway.");
1636 1517 publish:
1637 1518 if (fmev_rspublish_nvl(FMEV_RULESET_ON_SUNOS,
1638 1519 "panic", sc_event[evidx].sce_subclass, FMEV_HIPRI,
1639 1520 attr) != FMEV_SUCCESS) {
1640 1521 logprint(SC_SL_ERR, "failed to publish '%s' event: %s",
1641 1522 sc_event[evidx].sce_subclass, fmev_strerror(fmev_errno));
1642 1523 nvlist_free(attr);
1643 1524 }
1644 1525
1645 1526 }
1646 1527
1647 1528
1648 1529 int
1649 1530 main(int argc, char *argv[])
1650 1531 {
1651 1532 int i, c, bfd;
1652 1533 Stat_t st;
1653 1534 struct rlimit rl;
1654 1535 long filebounds = -1;
1655 1536 char namelist[30], corefile[30], boundstr[30];
1656 1537 dumpfile = NULL;
1657 1538
1658 1539 startts = gethrtime();
1659 1540
1660 1541 (void) getrlimit(RLIMIT_NOFILE, &rl);
1661 1542 rl.rlim_cur = rl.rlim_max;
1662 1543 (void) setrlimit(RLIMIT_NOFILE, &rl);
1663 1544
1664 1545 openlog(progname, LOG_ODELAY, LOG_AUTH);
1665 1546
1666 1547 (void) defopen("/etc/dumpadm.conf");
1667 1548 savedir = defread("DUMPADM_SAVDIR=");
1668 1549 if (savedir != NULL)
1669 1550 savedir = strdup(savedir);
1670 1551
1671 1552 while ((c = getopt(argc, argv, "Lvcdmf:")) != EOF) {
1672 1553 switch (c) {
1673 1554 case 'L':
1674 1555 livedump++;
1675 1556 break;
1676 1557 case 'v':
1677 1558 verbose++;
1678 1559 break;
1679 1560 case 'c':
1680 1561 cflag++;
1681 1562 break;
1682 1563 case 'd':
1683 1564 disregard_valid_flag++;
1684 1565 break;
1685 1566 case 'm':
1686 1567 mflag++;
1687 1568 break;
1688 1569 case 'f':
1689 1570 dumpfile = optarg;
1690 1571 filebounds = getbounds(dumpfile);
1691 1572 break;
1692 1573 case '?':
1693 1574 usage();
1694 1575 }
1695 1576 }
1696 1577
1697 1578 /*
1698 1579 * If doing something other than extracting an existing dump (i.e.
1699 1580 * dumpfile has been provided as an option), the user must be root.
1700 1581 */
1701 1582 if (geteuid() != 0 && dumpfile == NULL) {
1702 1583 (void) fprintf(stderr, "%s: %s %s\n", progname,
1703 1584 gettext("you must be root to use"), progname);
1704 1585 exit(1);
1705 1586 }
1706 1587
1707 1588 interactive = isatty(STDOUT_FILENO);
1708 1589
1709 1590 if (cflag && livedump)
1710 1591 usage();
1711 1592
1712 1593 if (dumpfile == NULL || livedump)
1713 1594 dumpfd = Open("/dev/dump", O_RDONLY, 0444);
1714 1595
1715 1596 if (dumpfile == NULL) {
1716 1597 dumpfile = Zalloc(MAXPATHLEN);
1717 1598 if (ioctl(dumpfd, DIOCGETDEV, dumpfile) == -1) {
1718 1599 have_dumpfile = B_FALSE;
1719 1600 logprint(SC_SL_NONE | SC_IF_ISATTY | SC_EXIT_ERR,
1720 1601 "no dump device configured");
1721 1602 }
1722 1603 }
1723 1604
1724 1605 if (mflag)
1725 1606 return (message_save());
1726 1607
1727 1608 if (optind == argc - 1)
1728 1609 savedir = argv[optind];
1729 1610
1730 1611 if (savedir == NULL || optind < argc - 1)
1731 1612 usage();
1732 1613
1733 1614 if (livedump && ioctl(dumpfd, DIOCDUMP, NULL) == -1)
1734 1615 logprint(SC_SL_NONE | SC_EXIT_ERR,
1735 1616 "dedicated dump device required");
1736 1617
1737 1618 (void) close(dumpfd);
1738 1619 dumpfd = -1;
1739 1620
1740 1621 Stat(dumpfile, &st);
1741 1622
1742 1623 filemode = S_ISREG(st.st_mode);
1743 1624
1744 1625 if (!filemode && defread("DUMPADM_CSAVE=off") == NULL)
1745 1626 csave = 1;
1746 1627
1747 1628 read_dumphdr();
1748 1629
1749 1630 /*
1750 1631 * We want this message to go to the log file, but not the console.
1751 1632 * There's no good way to do that with the existing syslog facility.
1752 1633 * We could extend it to handle this, but there doesn't seem to be
1753 1634 * a general need for it, so we isolate the complexity here instead.
1754 1635 */
1755 1636 if (dumphdr.dump_panicstring[0] != '\0') {
1756 1637 int logfd = Open("/dev/conslog", O_WRONLY, 0644);
1757 1638 log_ctl_t lc;
1758 1639 struct strbuf ctl, dat;
1759 1640 char msg[DUMP_PANICSIZE + 100];
1760 1641 char fmt[] = "reboot after panic: %s";
1761 1642 uint32_t msgid;
1762 1643
1763 1644 STRLOG_MAKE_MSGID(fmt, msgid);
1764 1645
1765 1646 /* LINTED: E_SEC_SPRINTF_UNBOUNDED_COPY */
1766 1647 (void) sprintf(msg, "%s: [ID %u FACILITY_AND_PRIORITY] ",
1767 1648 progname, msgid);
1768 1649 /* LINTED: E_SEC_PRINTF_VAR_FMT */
1769 1650 (void) sprintf(msg + strlen(msg), fmt,
1770 1651 dumphdr.dump_panicstring);
1771 1652
1772 1653 lc.pri = LOG_AUTH | LOG_ERR;
1773 1654 lc.flags = SL_CONSOLE | SL_LOGONLY;
1774 1655 lc.level = 0;
1775 1656
1776 1657 ctl.buf = (void *)&lc;
1777 1658 ctl.len = sizeof (log_ctl_t);
1778 1659
1779 1660 dat.buf = (void *)msg;
1780 1661 dat.len = strlen(msg) + 1;
1781 1662
1782 1663 (void) putmsg(logfd, &ctl, &dat, 0);
1783 1664 (void) close(logfd);
1784 1665 }
1785 1666
1786 1667 if ((dumphdr.dump_flags & DF_COMPLETE) == 0) {
1787 1668 logprint(SC_SL_WARN, "incomplete dump on dump device");
1788 1669 dump_incomplete = B_TRUE;
1789 1670 }
1790 1671
1791 1672 if (dumphdr.dump_fm_panic)
1792 1673 fm_panic = B_TRUE;
1793 1674
1794 1675 /*
1795 1676 * We have a valid dump on a dump device and know as much about
1796 1677 * it as we're going to at this stage. Raise an event for
1797 1678 * logging and so that FMA can open a case for this panic.
1798 1679 * Avoid this step for FMA-initiated panics - FMA will replay
1799 1680 * ereports off the dump device independently of savecore and
1800 1681 * will make a diagnosis, so we don't want to open two cases
1801 1682 * for the same event. Also avoid raising an event for a
1802 1683 * livedump, or when we inflating a compressed dump.
1803 1684 */
1804 1685 if (!fm_panic && !livedump && !filemode)
1805 1686 raise_event(SC_EVENT_DUMP_PENDING, NULL);
1806 1687
1807 1688 logprint(SC_SL_WARN, "System dump time: %s",
1808 1689 ctime(&dumphdr.dump_crashtime));
1809 1690
1810 1691 /*
1811 1692 * Option -c is designed for use from svc-dumpadm where we know
1812 1693 * that dumpadm -n is in effect but run savecore -c just to
1813 1694 * get the above dump_pending_on_device event raised. If it is run
1814 1695 * interactively then just print further panic details.
1815 1696 */
1816 1697 if (cflag) {
1817 1698 char *disabled = defread("DUMPADM_ENABLE=no");
1818 1699 int lvl = interactive ? SC_SL_WARN : SC_SL_ERR;
1819 1700 int ec = fm_panic ? SC_EXIT_FM : SC_EXIT_PEND;
1820 1701
1821 1702 logprint(lvl | ec,
1822 1703 "Panic crashdump pending on dump device%s "
1823 1704 "run savecore(1M) manually to extract. "
1824 1705 "Image UUID %s%s.",
1825 1706 disabled ? " but dumpadm -n in effect;" : ";",
1826 1707 corehdr.dump_uuid,
1827 1708 fm_panic ? "(fault-management initiated)" : "");
1828 1709 /*NOTREACHED*/
1829 1710 }
1830 1711
1831 1712 if (chdir(savedir) == -1)
1832 1713 logprint(SC_SL_ERR | SC_EXIT_ERR, "chdir(\"%s\"): %s",
1833 1714 savedir, strerror(errno));
1834 1715
1835 1716 check_space(csave);
1836 1717
1837 1718 if (filebounds < 0)
1838 1719 bounds = read_number_from_file("bounds", 0);
1839 1720 else
1840 1721 bounds = filebounds;
1841 1722
1842 1723 if (csave) {
1843 1724 size_t metrics_size = datahdr.dump_metrics;
1844 1725
1845 1726 (void) sprintf(corefile, "vmdump.%ld", bounds);
1846 1727
1847 1728 datahdr.dump_metrics = 0;
1848 1729
1849 1730 logprint(SC_SL_ERR,
1850 1731 "Saving compressed system crash dump in %s/%s",
1851 1732 savedir, corefile);
1852 1733
1853 1734 copy_crashfile(corefile);
1854 1735
1855 1736 /*
1856 1737 * Raise a fault management event that indicates the system
1857 1738 * has panicked. We know a reasonable amount about the
1858 1739 * condition at this time, but the dump is still compressed.
1859 1740 */
1860 1741 if (!livedump && !fm_panic)
1861 1742 raise_event(SC_EVENT_DUMP_AVAILABLE, NULL);
1862 1743
1863 1744 if (metrics_size > 0) {
1864 1745 int sec = (gethrtime() - startts) / 1000 / 1000 / 1000;
1865 1746 FILE *mfile = fopen(METRICSFILE, "a");
1866 1747 char *metrics = Zalloc(metrics_size + 1);
1867 1748
1868 1749 Pread(dumpfd, metrics, metrics_size, endoff +
1869 1750 sizeof (dumphdr) + sizeof (datahdr));
1870 1751
1871 1752 if (sec < 1)
1872 1753 sec = 1;
1873 1754
1874 1755 if (mfile == NULL) {
1875 1756 logprint(SC_SL_WARN,
1876 1757 "Can't create %s:\n%s",
1877 1758 METRICSFILE, metrics);
1878 1759 } else {
1879 1760 (void) fprintf(mfile, "[[[[,,,");
1880 1761 for (i = 0; i < argc; i++)
1881 1762 (void) fprintf(mfile, "%s ", argv[i]);
1882 1763 (void) fprintf(mfile, "\n");
1883 1764 (void) fprintf(mfile, ",,,%s %s %s %s %s\n",
1884 1765 dumphdr.dump_utsname.sysname,
1885 1766 dumphdr.dump_utsname.nodename,
1886 1767 dumphdr.dump_utsname.release,
1887 1768 dumphdr.dump_utsname.version,
1888 1769 dumphdr.dump_utsname.machine);
1889 1770 (void) fprintf(mfile, ",,,%s dump time %s\n",
1890 1771 dumphdr.dump_flags & DF_LIVE ? "Live" :
1891 1772 "Crash", ctime(&dumphdr.dump_crashtime));
1892 1773 (void) fprintf(mfile, ",,,%s/%s\n", savedir,
1893 1774 corefile);
1894 1775 (void) fprintf(mfile, "Metrics:\n%s\n",
1895 1776 metrics);
1896 1777 (void) fprintf(mfile, "Copy pages,%ld\n",
1897 1778 dumphdr. dump_npages);
1898 1779 (void) fprintf(mfile, "Copy time,%d\n", sec);
1899 1780 (void) fprintf(mfile, "Copy pages/sec,%ld\n",
1900 1781 dumphdr.dump_npages / sec);
1901 1782 (void) fprintf(mfile, "]]]]\n");
1902 1783 (void) fclose(mfile);
1903 1784 }
1904 1785 free(metrics);
1905 1786 }
1906 1787
1907 1788 logprint(SC_SL_ERR,
1908 1789 "Decompress the crash dump with "
1909 1790 "\n'savecore -vf %s/%s'",
1910 1791 savedir, corefile);
1911 1792
1912 1793 } else {
1913 1794 (void) sprintf(namelist, "unix.%ld", bounds);
1914 1795 (void) sprintf(corefile, "vmcore.%ld", bounds);
1915 1796
1916 1797 if (interactive && filebounds >= 0 && access(corefile, F_OK)
1917 1798 == 0)
1918 1799 logprint(SC_SL_NONE | SC_EXIT_ERR,
1919 1800 "%s already exists: remove with "
1920 1801 "'rm -f %s/{unix,vmcore}.%ld'",
1921 1802 corefile, savedir, bounds);
1922 1803
1923 1804 logprint(SC_SL_ERR,
1924 1805 "saving system crash dump in %s/{unix,vmcore}.%ld",
1925 1806 savedir, bounds);
1926 1807
1927 1808 build_corefile(namelist, corefile);
1928 1809
1929 1810 if (!livedump && !filemode && !fm_panic)
1930 1811 raise_event(SC_EVENT_DUMP_AVAILABLE, NULL);
1931 1812
1932 1813 if (access(METRICSFILE, F_OK) == 0) {
1933 1814 int sec = (gethrtime() - startts) / 1000 / 1000 / 1000;
1934 1815 FILE *mfile = fopen(METRICSFILE, "a");
1935 1816
1936 1817 if (sec < 1)
1937 1818 sec = 1;
1938 1819
1939 1820 if (mfile == NULL) {
1940 1821 logprint(SC_SL_WARN,
1941 1822 "Can't create %s: %s",
1942 1823 METRICSFILE, strerror(errno));
1943 1824 } else {
1944 1825 (void) fprintf(mfile, "[[[[,,,");
1945 1826 for (i = 0; i < argc; i++)
1946 1827 (void) fprintf(mfile, "%s ", argv[i]);
1947 1828 (void) fprintf(mfile, "\n");
1948 1829 (void) fprintf(mfile, ",,,%s/%s\n", savedir,
1949 1830 corefile);
1950 1831 (void) fprintf(mfile, ",,,%s %s %s %s %s\n",
1951 1832 dumphdr.dump_utsname.sysname,
1952 1833 dumphdr.dump_utsname.nodename,
1953 1834 dumphdr.dump_utsname.release,
1954 1835 dumphdr.dump_utsname.version,
1955 1836 dumphdr.dump_utsname.machine);
1956 1837 (void) fprintf(mfile,
1957 1838 "Uncompress pages,%"PRIu64"\n", saved);
1958 1839 (void) fprintf(mfile, "Uncompress time,%d\n",
1959 1840 sec);
1960 1841 (void) fprintf(mfile, "Uncompress pages/sec,%"
1961 1842 PRIu64"\n", saved / sec);
1962 1843 (void) fprintf(mfile, "]]]]\n");
1963 1844 (void) fclose(mfile);
1964 1845 }
1965 1846 }
1966 1847 }
1967 1848
1968 1849 if (filebounds < 0) {
1969 1850 (void) sprintf(boundstr, "%ld\n", bounds + 1);
1970 1851 bfd = Open("bounds", O_WRONLY | O_CREAT | O_TRUNC, 0644);
1971 1852 Pwrite(bfd, boundstr, strlen(boundstr), 0);
1972 1853 (void) close(bfd);
1973 1854 }
1974 1855
1975 1856 if (verbose) {
1976 1857 int sec = (gethrtime() - startts) / 1000 / 1000 / 1000;
1977 1858
1978 1859 (void) printf("%d:%02d dump %s is done\n",
1979 1860 sec / 60, sec % 60,
1980 1861 csave ? "copy" : "decompress");
1981 1862 }
1982 1863
1983 1864 if (verbose > 1 && hist != NULL) {
1984 1865 int i, nw;
1985 1866
1986 1867 for (i = 1, nw = 0; i <= BTOP(coreblksize); ++i)
1987 1868 nw += hist[i] * i;
1988 1869 (void) printf("pages count %%\n");
1989 1870 for (i = 0; i <= BTOP(coreblksize); ++i) {
1990 1871 if (hist[i] == 0)
1991 1872 continue;
1992 1873 (void) printf("%3d %5u %6.2f\n",
1993 1874 i, hist[i], 100.0 * hist[i] * i / nw);
1994 1875 }
1995 1876 }
1996 1877
1997 1878 (void) close(dumpfd);
1998 1879 dumpfd = -1;
1999 1880
2000 1881 return (0);
2001 1882 }
↓ open down ↓ |
815 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX