-
Notifications
You must be signed in to change notification settings - Fork 3
/
makedumpfile.c
11682 lines (10143 loc) · 297 KB
/
makedumpfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* makedumpfile.c
*
* Copyright (C) 2006, 2007, 2008, 2009, 2011 NEC Corporation
*
* 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.
*/
#include "makedumpfile.h"
#include "print_info.h"
#include "detect_cycle.h"
#include "dwarf_info.h"
#include "elf_info.h"
#include "erase_info.h"
#include "sadump_info.h"
#include <stddef.h>
#include <ctype.h>
#include <sys/time.h>
#include <limits.h>
#include <assert.h>
#include <zlib.h>
#include <libkdumpfile/addrxlat.h>
/* 10 pages cover 5-level paging, 2-level Xen p2m and 3 data page. */
#define PAGE_CACHE_SIZE 10
struct symbol_table symbol_table;
struct size_table size_table;
struct offset_table offset_table;
struct array_table array_table;
struct number_table number_table;
struct save_control sc;
struct vm_table vt = { 0 };
struct DumpInfo *info = NULL;
struct SplitBlock *splitblock = NULL;
struct vmap_pfns *gvmem_pfns;
int nr_gvmem_pfns;
extern int find_vmemmap();
char filename_stdout[] = FILENAME_STDOUT;
static unsigned long long write_bytes;
static void first_cycle(mdf_pfn_t start, mdf_pfn_t max, struct cycle *cycle)
{
cycle->start_pfn = round(start, info->pfn_cyclic);
cycle->end_pfn = cycle->start_pfn + info->pfn_cyclic;
if (cycle->end_pfn > max)
cycle->end_pfn = max;
/*
* Mitigate statistics problem in ELF dump mode.
* A cycle must start with a pfn that is divisible by BITPERBYTE.
* See create_bitmap_from_memhole().
*/
if (info->flag_elf_dumpfile && cycle->start_pfn < start)
cycle->start_pfn = round(start, BITPERBYTE);
cycle->exclude_pfn_start = 0;
cycle->exclude_pfn_end = 0;
}
static void update_cycle(mdf_pfn_t max, struct cycle *cycle)
{
cycle->start_pfn= cycle->end_pfn;
cycle->end_pfn= cycle->start_pfn + info->pfn_cyclic;
if (cycle->end_pfn > max)
cycle->end_pfn = max;
}
static int end_cycle(mdf_pfn_t max, struct cycle *cycle)
{
return (cycle->start_pfn >= max)?TRUE:FALSE;
}
#define for_each_cycle(start, max, C) \
for (first_cycle(start, max, C); !end_cycle(max, C); \
update_cycle(max, C))
/*
* The numbers of the excluded pages
*/
mdf_pfn_t pfn_zero;
mdf_pfn_t pfn_memhole;
mdf_pfn_t pfn_cache;
mdf_pfn_t pfn_cache_private;
mdf_pfn_t pfn_user;
mdf_pfn_t pfn_free;
mdf_pfn_t pfn_hwpoison;
mdf_pfn_t pfn_offline;
mdf_pfn_t pfn_elf_excluded;
mdf_pfn_t num_dumped;
int retcd = FAILED; /* return code */
#define INITIALIZE_LONG_TABLE(table, value) \
do { \
size_member = sizeof(long); \
num_member = sizeof(table) / size_member; \
ptr_long_table = (long *)&table; \
for (i = 0; i < num_member; i++, ptr_long_table++) \
*ptr_long_table = value; \
} while (0)
static void setup_page_is_buddy(void);
void
initialize_tables(void)
{
int i, size_member, num_member;
unsigned long long *ptr_symtable;
long *ptr_long_table;
/*
* Initialize the symbol table.
*/
size_member = sizeof(symbol_table.mem_map);
num_member = sizeof(symbol_table) / size_member;
ptr_symtable = (unsigned long long *)&symbol_table;
for (i = 0; i < num_member; i++, ptr_symtable++)
*ptr_symtable = NOT_FOUND_SYMBOL;
INITIALIZE_LONG_TABLE(size_table, NOT_FOUND_STRUCTURE);
INITIALIZE_LONG_TABLE(offset_table, NOT_FOUND_STRUCTURE);
INITIALIZE_LONG_TABLE(array_table, NOT_FOUND_STRUCTURE);
INITIALIZE_LONG_TABLE(number_table, NOT_FOUND_NUMBER);
}
unsigned long
paddr_to_vaddr(unsigned long long paddr)
{
addrxlat_fulladdr_t faddr;
addrxlat_ctx_t *xlatctx;
addrxlat_sys_t *xlatsys;
addrxlat_status xlaterr;
kdump_status status;
status = kdump_get_addrxlat(info->ctx_memory, &xlatctx, &xlatsys);
if (status != KDUMP_OK) {
ERRMSG("Can't get address translation: %s.\n",
kdump_get_err(info->ctx_memory));
return NOT_PADDR;
}
faddr.addr = paddr;
faddr.as = ADDRXLAT_KPHYSADDR;
xlaterr = addrxlat_fulladdr_conv(&faddr, ADDRXLAT_KVADDR,
xlatctx, xlatsys);
if (xlaterr != ADDRXLAT_OK) {
ERRMSG("Can't convert a physical address (0x%llx) to virtual address: %s.\n",
paddr, addrxlat_ctx_get_err(xlatctx));
faddr.addr = NOT_PADDR;
}
addrxlat_sys_decref(xlatsys);
addrxlat_ctx_decref(xlatctx);
return faddr.addr;
}
/*
* Translate a virtual address to a physical address using a specific
* kdump context.
*/
unsigned long long
vaddr_to_paddr_ctx(unsigned long vaddr, kdump_ctx_t *ctx)
{
addrxlat_fulladdr_t faddr;
addrxlat_ctx_t *xlatctx;
addrxlat_sys_t *xlatsys;
addrxlat_status xlaterr;
kdump_status status;
status = kdump_get_addrxlat(ctx, &xlatctx, &xlatsys);
if (status != KDUMP_OK) {
ERRMSG("Can't get address translation: %s.\n",
kdump_get_err(ctx));
return NOT_PADDR;
}
faddr.addr = vaddr;
faddr.as = ADDRXLAT_KVADDR;
xlaterr = addrxlat_fulladdr_conv(&faddr, ADDRXLAT_MACHPHYSADDR,
xlatctx, xlatsys);
if (xlaterr != ADDRXLAT_OK) {
ERRMSG("Can't convert a virtual address (0x%lx) to physical address: %s.\n",
vaddr, addrxlat_ctx_get_err(xlatctx));
faddr.addr = NOT_PADDR;
}
addrxlat_sys_decref(xlatsys);
addrxlat_ctx_decref(xlatctx);
return faddr.addr;
}
/*
* Translate a virtual address to a physical address.
*/
unsigned long long
vaddr_to_paddr(unsigned long vaddr)
{
return vaddr_to_paddr_ctx(vaddr, info->ctx_memory);
}
/*
* Translate a domain-0's physical address to machine address.
*/
static unsigned long long
ptom_xen(unsigned long long paddr)
{
info->xen_p2m_xlat.base.addr = paddr;
return addrxlat_walk(&info->xen_p2m_xlat) == ADDRXLAT_OK
? info->xen_p2m_xlat.base.addr
: NOT_PADDR;
}
/*
* Get the number of the page descriptors from the ELF info.
*/
int
get_max_mapnr(void)
{
unsigned long long max_paddr;
if (info->flag_refiltering) {
if (info->dh_memory->header_version >= 6)
info->max_mapnr = info->kh_memory->max_mapnr_64;
else
info->max_mapnr = info->dh_memory->max_mapnr;
return TRUE;
}
if (info->flag_sadump) {
info->max_mapnr = sadump_get_max_mapnr();
return TRUE;
}
max_paddr = get_max_paddr();
info->max_mapnr = paddr_to_pfn(roundup(max_paddr, PAGESIZE()));
DEBUG_MSG("\n");
DEBUG_MSG("max_mapnr : %llx\n", info->max_mapnr);
return TRUE;
}
/*
* Find the largest PFN that can be translated to a MFN.
*/
static int
find_dom0_mapnr(void)
{
addrxlat_map_t *map;
const addrxlat_range_t *range;
addrxlat_step_t xlat;
addrxlat_addr_t left, right, addr;
addrxlat_status err;
kdump_status status;
status = kdump_get_addrxlat(info->ctx_memory, &xlat.ctx, &xlat.sys);
if (status != KDUMP_OK) {
ERRMSG("Can't get address translation: %s.\n",
kdump_get_err(info->ctx_memory));
return FALSE;
}
map = addrxlat_sys_get_map(xlat.sys, ADDRXLAT_SYS_MAP_KPHYS_MACHPHYS);
if (!map || addrxlat_map_len(map) < 1)
goto err;
range = addrxlat_map_ranges(map);
xlat.meth = addrxlat_sys_get_meth(xlat.sys, range->meth);
left = 0;
right = (range->endoff + 1) >> PAGESHIFT();
while (right - left > 1) {
addr = ((left + right) >> 1) << PAGESHIFT();
xlat.base.addr = addr;
err = addrxlat_walk(&xlat);
if (err == ADDRXLAT_OK)
left = addr >> PAGESHIFT();
else if (err == ADDRXLAT_ERR_NOTPRESENT)
right = addr >> PAGESHIFT();
else
goto err_xlat;
}
info->dom0_mapnr = right;
addrxlat_sys_decref(xlat.sys);
addrxlat_ctx_decref(xlat.ctx);
return TRUE;
err_xlat:
ERRMSG("Can't translate a physical address (0x%"ADDRXLAT_PRIxADDR") to machine address: %s.\n",
addr, addrxlat_ctx_get_err(xlat.ctx));
err:
addrxlat_sys_decref(xlat.sys);
addrxlat_ctx_decref(xlat.ctx);
return FALSE;
}
/*
* Get the number of the page descriptors for Xen.
*/
int
get_dom0_mapnr()
{
unsigned long max_pfn;
if (SYMBOL(max_pfn) != NOT_FOUND_SYMBOL) {
if (!readmem(VADDR, SYMBOL(max_pfn), &max_pfn, sizeof max_pfn)) {
ERRMSG("Can't read domain-0 max_pfn.\n");
return FALSE;
}
info->dom0_mapnr = max_pfn;
} else if (!find_dom0_mapnr()) {
/* dom0_mapnr is unavailable, which may be non-critical */
return TRUE;
}
DEBUG_MSG("domain-0 pfn : %llx\n", info->dom0_mapnr);
return TRUE;
}
int
is_in_same_page(unsigned long vaddr1, unsigned long vaddr2)
{
if (round(vaddr1, info->page_size) == round(vaddr2, info->page_size))
return TRUE;
return FALSE;
}
/* For Linux 6.6 and later */
#define IS_HUGETLB ((unsigned long)-1)
static inline int
isHugetlb(unsigned long dtor)
{
return (dtor == IS_HUGETLB)
|| ((NUMBER(HUGETLB_PAGE_DTOR) != NOT_FOUND_NUMBER)
&& (NUMBER(HUGETLB_PAGE_DTOR) == dtor))
|| ((SYMBOL(free_huge_page) != NOT_FOUND_SYMBOL)
&& (SYMBOL(free_huge_page) == dtor));
}
static int
isOffline(unsigned long flags, unsigned int _mapcount)
{
if (NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE) == NOT_FOUND_NUMBER)
return FALSE;
if (flags & (1UL << NUMBER(PG_slab)))
return FALSE;
if (_mapcount == (int)NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE))
return TRUE;
return FALSE;
}
static int
is_cache_page(unsigned long flags)
{
if (isLRU(flags))
return TRUE;
/* PG_swapcache is valid only if:
* a. PG_swapbacked bit is set, or
* b. PG_swapbacked did not exist (kernels before 4.10-rc1).
*/
if ((NUMBER(PG_swapbacked) == NOT_FOUND_NUMBER || isSwapBacked(flags))
&& isSwapCache(flags))
return TRUE;
return FALSE;
}
static inline unsigned long
calculate_len_buf_out(long page_size)
{
unsigned long zlib, lzo, snappy, zstd;
zlib = lzo = snappy = zstd = 0;
zlib = compressBound(page_size);
#ifdef USELZO
lzo = page_size + page_size / 16 + 64 + 3;
#endif
#ifdef USESNAPPY
snappy = snappy_max_compressed_length(page_size);
#endif
#ifdef USEZSTD
zstd = ZSTD_compressBound(page_size);
#endif
return MAX(zlib, MAX(lzo, MAX(snappy, zstd)));
}
#define BITMAP_SECT_LEN 4096
static inline int is_dumpable(struct dump_bitmap *, mdf_pfn_t, struct cycle *cycle);
int
readmem(int type_addr, unsigned long long addr, void *bufptr, size_t size)
{
size_t size_orig = size;
kdump_ctx_t *ctx;
kdump_addrspace_t as;
switch (type_addr) {
case VADDR:
ctx = info->ctx_memory;
as = KDUMP_KVADDR;
break;
case PADDR:
ctx = info->ctx_memory;
as = KDUMP_MACHPHYSADDR;
break;
case VADDR_XEN:
ctx = info->ctx_memory_xen;
as = KDUMP_KVADDR;
break;
default:
ERRMSG("Invalid address type (%d).\n", type_addr);
goto error;
}
if (kdump_read(ctx, as, addr, bufptr, &size) != KDUMP_OK) {
ERRMSG("%s\n", kdump_get_err(info->ctx_memory));
goto error;
}
return size;
error:
ERRMSG("type_addr: %d, addr:%llx, size:%zd, read:%zd\n",
type_addr, addr, size_orig, size);
return FALSE;
}
int32_t
get_kernel_version(char *release)
{
int32_t version;
long maj, min, rel;
char *start, *end;
if (info->kernel_version)
return info->kernel_version;
/*
* This method checks that vmlinux and vmcore are same kernel version.
*/
start = release;
maj = strtol(start, &end, 10);
if (maj == LONG_MAX)
return FALSE;
start = end + 1;
min = strtol(start, &end, 10);
if (min == LONG_MAX)
return FALSE;
start = end + 1;
rel = strtol(start, &end, 10);
if (rel == LONG_MAX)
return FALSE;
version = KERNEL_VERSION(maj, min, rel);
if ((version < OLDEST_VERSION) || (LATEST_VERSION < version)) {
MSG("The kernel version is not supported.\n");
MSG("The makedumpfile operation may be incomplete.\n");
}
return version;
}
int
is_page_size(long page_size)
{
/*
* Page size is restricted to a hamming weight of 1.
*/
if (page_size > 0 && !(page_size & (page_size - 1)))
return TRUE;
return FALSE;
}
int
set_page_size(long page_size)
{
if (!is_page_size(page_size)) {
ERRMSG("Invalid page_size: %ld", page_size);
return FALSE;
}
info->page_size = page_size;
info->page_shift = ffs(info->page_size) - 1;
DEBUG_MSG("page_size : %ld\n", info->page_size);
return TRUE;
}
int
fallback_to_current_page_size(void)
{
if (!set_page_size(sysconf(_SC_PAGE_SIZE)))
return FALSE;
DEBUG_MSG("WARNING: Cannot determine page size (no vmcoreinfo).\n");
DEBUG_MSG("Using the dump kernel page size: %ld\n",
info->page_size);
return TRUE;
}
static int populate_kernel_version(void)
{
struct utsname utsname;
if (uname(&utsname)) {
ERRMSG("Cannot get name and information about current kernel : %s\n",
strerror(errno));
return FALSE;
}
info->kernel_version = get_kernel_version(utsname.release);
return TRUE;
}
int
check_release(void)
{
unsigned long utsname;
/*
* Get the kernel version.
*/
if (SYMBOL(system_utsname) != NOT_FOUND_SYMBOL) {
utsname = SYMBOL(system_utsname);
} else if (SYMBOL(init_uts_ns) != NOT_FOUND_SYMBOL) {
if (OFFSET(uts_namespace.name) != NOT_FOUND_STRUCTURE)
utsname = SYMBOL(init_uts_ns) + OFFSET(uts_namespace.name);
else
utsname = SYMBOL(init_uts_ns) + sizeof(int);
} else {
ERRMSG("Can't get the symbol of system_utsname.\n");
return FALSE;
}
if (!readmem(VADDR, utsname, &info->system_utsname,
sizeof(struct utsname))) {
ERRMSG("Can't get the address of system_utsname.\n");
return FALSE;
}
if (info->flag_read_vmcoreinfo) {
if (strcmp(info->system_utsname.release, info->release)) {
ERRMSG("%s and %s don't match.\n",
info->name_vmcoreinfo, info->name_memory);
retcd = WRONG_RELEASE;
return FALSE;
}
}
info->kernel_version = get_kernel_version(info->system_utsname.release);
if (info->kernel_version == FALSE) {
ERRMSG("Can't get the kernel version.\n");
return FALSE;
}
return TRUE;
}
static int
has_linux_vmcoreinfo(void)
{
kdump_attr_t attr;
kdump_status status;
status = kdump_get_attr(info->ctx_memory, "linux.vmcoreinfo", &attr);
return status == KDUMP_OK;
}
int
open_vmcoreinfo(char *mode)
{
FILE *file_vmcoreinfo;
if ((file_vmcoreinfo = fopen(info->name_vmcoreinfo, mode)) == NULL) {
ERRMSG("Can't open the vmcoreinfo file(%s). %s\n",
info->name_vmcoreinfo, strerror(errno));
return FALSE;
}
info->file_vmcoreinfo = file_vmcoreinfo;
return TRUE;
}
int
open_kernel_file(void)
{
int fd;
if (info->name_vmlinux) {
if ((fd = open(info->name_vmlinux, O_RDONLY)) < 0) {
ERRMSG("Can't open the kernel file(%s). %s\n",
info->name_vmlinux, strerror(errno));
return FALSE;
}
info->fd_vmlinux = fd;
}
if (info->name_xen_syms) {
if ((fd = open(info->name_xen_syms, O_RDONLY)) < 0) {
ERRMSG("Can't open the kernel file(%s). %s\n",
info->name_xen_syms, strerror(errno));
return FALSE;
}
info->fd_xen_syms = fd;
}
return TRUE;
}
int
check_kdump_compressed(char *filename)
{
struct disk_dump_header dh;
if (!__read_disk_dump_header(&dh, filename))
return ERROR;
if (strncmp(dh.signature, KDUMP_SIGNATURE, SIG_LEN))
return FALSE;
return TRUE;
}
int
get_kdump_compressed_header_info(char *filename)
{
struct disk_dump_header dh;
struct kdump_sub_header kh;
if (!read_disk_dump_header(&dh, filename))
return FALSE;
if (!read_kdump_sub_header(&kh, filename))
return FALSE;
if (dh.header_version < 1) {
ERRMSG("header does not have dump_level member\n");
return FALSE;
}
DEBUG_MSG("diskdump main header\n");
DEBUG_MSG(" signature : %s\n", dh.signature);
DEBUG_MSG(" header_version : %d\n", dh.header_version);
DEBUG_MSG(" status : %d\n", dh.status);
DEBUG_MSG(" block_size : %d\n", dh.block_size);
DEBUG_MSG(" sub_hdr_size : %d\n", dh.sub_hdr_size);
DEBUG_MSG(" bitmap_blocks : %d\n", dh.bitmap_blocks);
DEBUG_MSG(" max_mapnr : 0x%x\n", dh.max_mapnr);
DEBUG_MSG(" total_ram_blocks : %d\n", dh.total_ram_blocks);
DEBUG_MSG(" device_blocks : %d\n", dh.device_blocks);
DEBUG_MSG(" written_blocks : %d\n", dh.written_blocks);
DEBUG_MSG(" current_cpu : %d\n", dh.current_cpu);
DEBUG_MSG(" nr_cpus : %d\n", dh.nr_cpus);
DEBUG_MSG("kdump sub header\n");
DEBUG_MSG(" phys_base : 0x%lx\n", kh.phys_base);
DEBUG_MSG(" dump_level : %d\n", kh.dump_level);
DEBUG_MSG(" split : %d\n", kh.split);
DEBUG_MSG(" start_pfn : 0x%lx\n", kh.start_pfn);
DEBUG_MSG(" end_pfn : 0x%lx\n", kh.end_pfn);
if (dh.header_version >= 6) {
/* A dumpfile contains full 64bit values. */
DEBUG_MSG(" start_pfn_64 : 0x%llx\n", kh.start_pfn_64);
DEBUG_MSG(" end_pfn_64 : 0x%llx\n", kh.end_pfn_64);
DEBUG_MSG(" max_mapnr_64 : 0x%llx\n", kh.max_mapnr_64);
}
info->dh_memory = malloc(sizeof(dh));
if (info->dh_memory == NULL) {
ERRMSG("Can't allocate memory for the header. %s\n",
strerror(errno));
return FALSE;
}
memcpy(info->dh_memory, &dh, sizeof(dh));
memcpy(&info->timestamp, &dh.timestamp, sizeof(dh.timestamp));
info->kh_memory = malloc(sizeof(kh));
if (info->kh_memory == NULL) {
ERRMSG("Can't allocate memory for the sub header. %s\n",
strerror(errno));
goto error;
}
memcpy(info->kh_memory, &kh, sizeof(kh));
set_nr_cpus(dh.nr_cpus);
if (dh.header_version >= 3) {
/* A dumpfile contains vmcoreinfo data. */
set_vmcoreinfo(kh.offset_vmcoreinfo, kh.size_vmcoreinfo);
DEBUG_MSG(" offset_vmcoreinfo: 0x%llx\n",
(unsigned long long)kh.offset_vmcoreinfo);
DEBUG_MSG(" size_vmcoreinfo : 0x%ld\n", kh.size_vmcoreinfo);
}
if (dh.header_version >= 4) {
/* A dumpfile contains ELF note section. */
set_pt_note(kh.offset_note, kh.size_note);
DEBUG_MSG(" offset_note : 0x%llx\n",
(unsigned long long)kh.offset_note);
DEBUG_MSG(" size_note : 0x%ld\n", kh.size_note);
}
if (dh.header_version >= 5) {
/* A dumpfile contains erased information. */
set_eraseinfo(kh.offset_eraseinfo, kh.size_eraseinfo);
DEBUG_MSG(" offset_eraseinfo : 0x%llx\n",
(unsigned long long)kh.offset_eraseinfo);
DEBUG_MSG(" size_eraseinfo : 0x%ld\n", kh.size_eraseinfo);
}
return TRUE;
error:
free(info->dh_memory);
info->dh_memory = NULL;
return FALSE;
}
int
open_dump_memory(int *fdp, kdump_ctx_t **ctxp)
{
int fd;
kdump_mmap_policy_t mmap_policy;
kdump_ctx_t *ctx;
if ((fd = open(info->name_memory, O_RDONLY)) < 0) {
ERRMSG("Can't open the dump memory(%s). %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
ctx = kdump_new();
if (!ctx) {
ERRMSG("Can't allocate libkdumpfile context.");
goto error;
}
if (kdump_set_number_attr(ctx, "cache.size",
(info->num_threads ?: 1) * PAGE_CACHE_SIZE)
!= KDUMP_OK) {
ERRMSG("Can't set libkdumpfile cache size.");
goto error_ctx;
}
mmap_policy = info->flag_usemmap
? KDUMP_MMAP_TRY_ONCE
: KDUMP_MMAP_NEVER;
if (kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_MMAP_POLICY,
mmap_policy) != KDUMP_OK) {
ERRMSG("Can't set mmap policy: %s\n", kdump_get_err(ctx));
goto error_ctx;
}
if (kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_SET ".number", 1)
!= KDUMP_OK) {
ERRMSG("Can't initialize dump memory(%s). %s\n",
info->name_memory, kdump_get_err(ctx));
goto error_ctx;
}
kdump_set_string_attr(ctx, KDUMP_ATTR_FILE_SET ".0.name",
info->name_memory);
if (kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_SET ".0.fd", fd)
!= KDUMP_OK) {
ERRMSG("Can't initialize dump memory(%s). %s\n",
info->name_memory, kdump_get_err(ctx));
goto error_ctx;
}
*fdp = fd;
*ctxp = ctx;
return TRUE;
error_ctx:
kdump_free(ctx);
error:
close(fd);
return FALSE;
}
int
open_dump_file(void)
{
int fd;
int open_flags = O_RDWR|O_CREAT|O_TRUNC;
if (!info->flag_force)
open_flags |= O_EXCL;
if (info->flag_flatten) {
fd = STDOUT_FILENO;
info->name_dumpfile = filename_stdout;
} else if (info->flag_dry_run) {
fd = -1;
} else if ((fd = open(info->name_dumpfile, open_flags,
S_IRUSR|S_IWUSR)) < 0) {
ERRMSG("Can't open the dump file(%s). %s\n",
info->name_dumpfile, strerror(errno));
return FALSE;
}
info->fd_dumpfile = fd;
return TRUE;
}
int
check_dump_file(const char *path)
{
char *err_str;
if (access(path, F_OK) != 0)
return TRUE; /* File does not exist */
if (info->flag_force) {
if (access(path, W_OK) == 0)
return TRUE; /* We have write permission */
err_str = strerror(errno);
} else {
err_str = strerror(EEXIST);
}
ERRMSG("Can't open the dump file (%s). %s\n", path, err_str);
return FALSE;
}
int
open_dump_bitmap(void)
{
char *tmpname;
size_t len;
int i, fd;
/* Unnecessary to open */
if (!info->working_dir && !info->flag_reassemble && !info->flag_refiltering
&& !info->flag_sadump && !info->flag_mem_usage && info->flag_cyclic)
return TRUE;
tmpname = getenv("TMPDIR");
if (info->working_dir)
tmpname = info->working_dir;
else if (!tmpname)
tmpname = "/tmp";
/* +2 for '/' and terminating '\0' */
len = strlen(FILENAME_BITMAP) + strlen(tmpname) + 2;
info->name_bitmap = malloc(len);
if (!info->name_bitmap) {
ERRMSG("Can't allocate memory for the filename. %s\n",
strerror(errno));
return FALSE;
}
snprintf(info->name_bitmap, len, "%s/%s", tmpname, FILENAME_BITMAP);
if ((fd = mkstemp(info->name_bitmap)) < 0) {
ERRMSG("Can't open the bitmap file(%s). %s\n",
info->name_bitmap, strerror(errno));
return FALSE;
}
info->fd_bitmap = fd;
if (info->flag_split) {
/*
* Reserve file descriptors of bitmap for creating split
* dumpfiles by multiple processes, because a bitmap file will
* be unlinked just after this and it is not possible to open
* a bitmap file later.
*/
for (i = 0; i < info->num_dumpfile; i++) {
if ((fd = open(info->name_bitmap, O_RDONLY)) < 0) {
ERRMSG("Can't open the bitmap file(%s). %s\n",
info->name_bitmap, strerror(errno));
return FALSE;
}
SPLITTING_FD_BITMAP(i) = fd;
}
}
if (info->num_threads) {
/*
* Reserve file descriptors of bitmap for creating dumpfiles
* parallelly, because a bitmap file will be unlinked just after
* this and it is not possible to open a bitmap file later.
*/
for (i = 0; i < info->num_threads; i++) {
if ((fd = open(info->name_bitmap, O_RDONLY)) < 0) {
ERRMSG("Can't open the bitmap file(%s). %s\n",
info->name_bitmap, strerror(errno));
return FALSE;
}
FD_BITMAP_PARALLEL(i) = fd;
}
}
unlink(info->name_bitmap);
return TRUE;
}
/*
* Open the following files when it generates the vmcoreinfo file.
* - vmlinux
* - vmcoreinfo file
*/
int
open_files_for_generating_vmcoreinfo(void)
{
if (!open_kernel_file())
return FALSE;
if (!open_vmcoreinfo("w"))
return FALSE;
return TRUE;
}
/*
* Open the following file when it rearranges the dump data.
* - dump file
*/
int
open_files_for_rearranging_dumpdata(void)
{
if (!open_dump_file())
return FALSE;
return TRUE;
}
/*
* Open the following files when it creates the dump file.
* - dump mem
* - bit map
* if it reads the vmcoreinfo file
* - vmcoreinfo file
* else
* - vmlinux
*/
int
open_files_for_creating_dumpfile(void)
{
kdump_num_t num;
int status;
if (info->flag_read_vmcoreinfo) {
if (!open_vmcoreinfo("r"))
return FALSE;
} else {
if (!open_kernel_file())
return FALSE;
}
if (!open_dump_memory(&info->fd_memory, &info->ctx_memory))
return FALSE;
num = KDUMP_XEN_NONE;
kdump_get_number_attr(info->ctx_memory, KDUMP_ATTR_XEN_TYPE, &num);
if (num != KDUMP_XEN_NONE) {
info->is_xen = TRUE;
DEBUG_MSG("Xen kdump\n");
} else {
info->is_xen = FALSE;
DEBUG_MSG("Linux kdump\n");
}
status = check_kdump_compressed(info->name_memory);
if (status == TRUE) {
info->flag_refiltering = TRUE;
return get_kdump_compressed_header_info(info->name_memory);
}
return check_and_get_sadump_header_info(info->name_memory);
}
static int
set_memory_ostype(kdump_ctx_t *ctx, const char *type)
{
if (kdump_set_string_attr(ctx, KDUMP_ATTR_OSTYPE, type) != KDUMP_OK) {
ERRMSG("Can't initialize as a %s dump: %s\n",
type, kdump_get_err(ctx));
return FALSE;
}
return TRUE;
}