-
Notifications
You must be signed in to change notification settings - Fork 0
/
erase_info.c
2468 lines (2177 loc) · 59.8 KB
/
erase_info.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
/*
* erase_info.c
*
* Created by: Mahesh J Salgaonkar <[email protected]>
*
* Copyright (C) 2011 IBM Corporation
* Copyright (C) 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 "dwarf_info.h"
#include "erase_info.h"
#include <dlfcn.h>
struct erase_info *erase_info = NULL;
unsigned long num_erase_info = 1; /* Node 0 is unused. */
struct call_back eppic_cb = {
&get_domain_all,
&readmem,
&get_die_attr_type,
&get_die_name,
&get_die_offset,
&get_die_length,
&get_die_member_all,
&get_die_nfields_all,
&get_symbol_addr_all,
&update_filter_info_raw
};
/*
* flags for config_entry.flag
*/
#define FILTER_ENTRY 0x0001
#define SIZE_ENTRY 0x0002
#define ITERATION_ENTRY 0x0004
#define LIST_ENTRY 0x0008
#define SYMBOL_ENTRY 0x0010
#define VAR_ENTRY 0x0020
#define TRAVERSAL_ENTRY 0x0040
#define ENTRY_RESOLVED 0x8000
/*
* flags for get_config()
*/
#define CONFIG_SKIP_SECTION 0x01
#define CONFIG_NEW_CMD 0x02
#define IS_KEYWORD(tkn) \
(!strcmp(tkn, "erase") || !strcmp(tkn, "size") || \
!strcmp(tkn, "nullify") || !strcmp(tkn, "for") || \
!strcmp(tkn, "in") || !strcmp(tkn, "within") || \
!strcmp(tkn, "endfor"))
struct module_sym_table {
unsigned int num_modules;
unsigned int current_mod;
struct module_info *modules;
};
/*
* Filtering physical address range.
*/
struct filter_info {
unsigned long long vaddr; /* symbol address for debugging */
unsigned long long paddr;
long size;
/* direct access to update erase information node */
int erase_info_idx; /* 0= invalid index */
int size_idx;
int erase_ch;
struct filter_info *next;
unsigned short nullify;
};
/*
* Filter config information
*/
struct filter_config {
char *name_filterconfig;
FILE *file_filterconfig;
char *cur_module;
char *saved_token;
char *token;
int new_section;
int line_count;
};
struct config_entry {
char *name;
char *type_name;
char *symbol_expr; /* original symbol expression */
unsigned short flag;
unsigned short nullify;
unsigned long long sym_addr; /* Symbol address */
unsigned long vaddr; /* Symbol address or
value pointed by sym_addr */
unsigned long long cmp_addr; /* for LIST_ENTRY */
unsigned long offset;
unsigned long type_flag;
long array_length;
long index;
long size;
int line; /* Line number in config file. */
int erase_info_idx; /* 0= invalid index */
struct config_entry *refer_to;
struct config_entry *next;
};
struct config {
char *module_name;
struct config_entry *iter_entry;
struct config_entry *list_entry;
int num_filter_symbols;
struct config_entry **filter_symbol;
struct config_entry **size_symbol;
};
static struct module_sym_table mod_st = { 0 };
static struct filter_info *filter_info = NULL;
static struct filter_config filter_config;
static char config_buf[BUFSIZE_FGETS];
/*
* Internal functions.
*/
static struct module_info *
get_loaded_module(char *mod_name)
{
unsigned int i;
struct module_info *modules;
modules = mod_st.modules;
if (strcmp(mod_name, modules[mod_st.current_mod].name)) {
for (i = 0; i < mod_st.num_modules; i++) {
if (!strcmp(mod_name, modules[i].name))
break;
}
if (i == mod_st.num_modules)
return NULL;
/* set the current_mod for fast lookup next time */
mod_st.current_mod = i;
}
return &modules[mod_st.current_mod];
}
static unsigned long long
find_module_symbol(struct module_info *module_ptr, char *symname)
{
int i;
struct symbol_info *sym_info;
sym_info = module_ptr->sym_info;
if (!sym_info)
return FALSE;
for (i = 1; i < module_ptr->num_syms; i++) {
if (sym_info[i].name && !strcmp(sym_info[i].name, symname))
return sym_info[i].value;
}
return NOT_FOUND_SYMBOL;
}
static int
sym_in_module(char *symname, unsigned long long *symbol_addr)
{
char *module_name;
struct module_info *module_ptr;
module_name = get_dwarf_module_name();
if (!mod_st.num_modules
|| !strcmp(module_name, "vmlinux")
|| !strcmp(module_name, "xen-syms"))
return FALSE;
module_ptr = get_loaded_module(module_name);
if (!module_ptr)
return FALSE;
*symbol_addr = find_module_symbol(module_ptr, symname);
if (*symbol_addr == NOT_FOUND_SYMBOL)
return FALSE;
else
return TRUE;
}
static unsigned int
get_num_modules(unsigned long head, unsigned int *num)
{
unsigned long cur;
unsigned int num_modules = 0;
if (!num)
return FALSE;
if (!readmem(VADDR, head + OFFSET(list_head.next), &cur, sizeof cur)) {
ERRMSG("Can't get next list_head.\n");
return FALSE;
}
while (cur != head) {
num_modules++;
if (!readmem(VADDR, cur + OFFSET(list_head.next),
&cur, sizeof cur)) {
ERRMSG("Can't get next list_head.\n");
return FALSE;
}
}
*num = num_modules;
return TRUE;
}
static void
free_symbol_info(struct module_info *module)
{
int i;
if (module->num_syms == 0)
return;
for (i = 1; i < module->num_syms; i++)
if (module->sym_info[i].name)
free(module->sym_info[i].name);
free(module->sym_info);
}
static void
clean_module_symbols(void)
{
int i;
for (i = 0; i < mod_st.num_modules; i++)
free_symbol_info(&mod_st.modules[i]);
if (mod_st.num_modules) {
free(mod_st.modules);
mod_st.modules = NULL;
mod_st.num_modules = 0;
}
}
static int
__load_module_symbol(struct module_info *modules, unsigned long addr_module)
{
int ret = FALSE;
unsigned int nsym;
unsigned long symtab, strtab;
unsigned long mod_base, mod_init;
unsigned int mod_size, mod_init_size;
unsigned char *module_struct_mem = NULL;
unsigned char *module_core_mem = NULL;
unsigned char *module_init_mem = NULL;
unsigned char *symtab_mem;
char *module_name, *strtab_mem, *nameptr;
unsigned int num_symtab;
/* Allocate buffer to read struct module data from vmcore. */
if ((module_struct_mem = calloc(1, SIZE(module))) == NULL) {
ERRMSG("Failed to allocate buffer for module\n");
return FALSE;
}
if (!readmem(VADDR, addr_module, module_struct_mem,
SIZE(module))) {
ERRMSG("Can't get module info.\n");
goto out;
}
module_name = (char *)(module_struct_mem + OFFSET(module.name));
strncpy(modules->name, module_name, MOD_NAME_LEN - 1);
modules->name[MOD_NAME_LEN - 1] = '\0';
mod_init = ULONG(module_struct_mem +
OFFSET(module.module_init));
mod_init_size = UINT(module_struct_mem +
OFFSET(module.init_size));
mod_base = ULONG(module_struct_mem +
OFFSET(module.module_core));
mod_size = UINT(module_struct_mem +
OFFSET(module.core_size));
DEBUG_MSG("Module: %s, Base: 0x%lx, Size: %u\n",
module_name, mod_base, mod_size);
if (mod_init_size > 0) {
module_init_mem = calloc(1, mod_init_size);
if (module_init_mem == NULL) {
ERRMSG("Can't allocate memory for module "
"init\n");
goto out;
}
if (!readmem(VADDR, mod_init, module_init_mem,
mod_init_size)) {
ERRMSG("Can't access module init in memory.\n");
goto out;
}
}
if ((module_core_mem = calloc(1, mod_size)) == NULL) {
ERRMSG("Can't allocate memory for module\n");
goto out;
}
if (!readmem(VADDR, mod_base, module_core_mem, mod_size)) {
ERRMSG("Can't access module in memory.\n");
goto out;
}
num_symtab = UINT(module_struct_mem +
OFFSET(module.num_symtab));
if (!num_symtab) {
ERRMSG("%s: Symbol info not available\n", module_name);
goto out;
}
modules->num_syms = num_symtab;
DEBUG_MSG("num_sym: %d\n", num_symtab);
symtab = ULONG(module_struct_mem + OFFSET(module.symtab));
strtab = ULONG(module_struct_mem + OFFSET(module.strtab));
/* check if symtab and strtab are inside the module space. */
if (!IN_RANGE(symtab, mod_base, mod_size) &&
!IN_RANGE(symtab, mod_init, mod_init_size)) {
ERRMSG("%s: module symtab is outside of module "
"address space\n", module_name);
goto out;
}
if (IN_RANGE(symtab, mod_base, mod_size))
symtab_mem = module_core_mem + (symtab - mod_base);
else
symtab_mem = module_init_mem + (symtab - mod_init);
if (!IN_RANGE(strtab, mod_base, mod_size) &&
!IN_RANGE(strtab, mod_init, mod_init_size)) {
ERRMSG("%s: module strtab is outside of module "
"address space\n", module_name);
goto out;
}
if (IN_RANGE(strtab, mod_base, mod_size))
strtab_mem = (char *)(module_core_mem
+ (strtab - mod_base));
else
strtab_mem = (char *)(module_init_mem
+ (strtab - mod_init));
modules->sym_info = calloc(num_symtab, sizeof(struct symbol_info));
if (modules->sym_info == NULL) {
ERRMSG("Can't allocate memory to store sym info\n");
goto out;
}
/* symbols starts from 1 */
for (nsym = 1; nsym < num_symtab; nsym++) {
Elf32_Sym *sym32;
Elf64_Sym *sym64;
/*
* TODO:
* If case of ELF vmcore then the word size can be
* determined using flag_elf64_memory flag.
* But in case of kdump-compressed dump, kdump header
* does not carry word size info. May be in future
* this info will be available in kdump header.
* Until then, in order to make this logic work on both
* situation we depend on pointer_size that is
* extracted from vmlinux dwarf information.
*/
if ((get_pointer_size() * 8) == 64) {
sym64 = (Elf64_Sym *) (symtab_mem
+ (nsym * sizeof(Elf64_Sym)));
modules->sym_info[nsym].value =
(unsigned long long) sym64->st_value;
nameptr = strtab_mem + sym64->st_name;
} else {
sym32 = (Elf32_Sym *) (symtab_mem
+ (nsym * sizeof(Elf32_Sym)));
modules->sym_info[nsym].value =
(unsigned long long) sym32->st_value;
nameptr = strtab_mem + sym32->st_name;
}
if (strlen(nameptr))
modules->sym_info[nsym].name = strdup(nameptr);
DEBUG_MSG("\t[%d] %llx %s\n", nsym,
modules->sym_info[nsym].value, nameptr);
}
ret = TRUE;
out:
free(module_struct_mem);
free(module_core_mem);
free(module_init_mem);
return ret;
}
static int
load_module_symbols(void)
{
unsigned long head, cur, cur_module;
struct module_info *modules = NULL;
unsigned int i = 0;
head = SYMBOL(modules);
if (!get_num_modules(head, &mod_st.num_modules) ||
!mod_st.num_modules) {
ERRMSG("Can't get module count\n");
return FALSE;
}
mod_st.modules = calloc(mod_st.num_modules,
sizeof(struct module_info));
if (!mod_st.modules) {
ERRMSG("Can't allocate memory for module info\n");
return FALSE;
}
modules = mod_st.modules;
if (!readmem(VADDR, head + OFFSET(list_head.next), &cur, sizeof cur)) {
ERRMSG("Can't get next list_head.\n");
return FALSE;
}
/* Travese the list and read module symbols */
while (cur != head) {
cur_module = cur - OFFSET(module.list);
if (!__load_module_symbol(&modules[i], cur_module))
return FALSE;
if (!readmem(VADDR, cur + OFFSET(list_head.next),
&cur, sizeof cur)) {
ERRMSG("Can't get next list_head.\n");
return FALSE;
}
i++;
}
return TRUE;
}
static void
free_config_entry(struct config_entry *ce)
{
struct config_entry *p;
while(ce) {
p = ce;
ce = p->next;
if (p->name)
free(p->name);
if (p->type_name)
free(p->type_name);
if (p->symbol_expr)
free(p->symbol_expr);
free(p);
}
}
static void
free_config(struct config *config)
{
int i;
if (config == NULL)
return;
if (config->module_name)
free(config->module_name);
for (i = 0; i < config->num_filter_symbols; i++) {
if (config->filter_symbol[i])
free_config_entry(config->filter_symbol[i]);
if (config->size_symbol[i])
free_config_entry(config->size_symbol[i]);
}
if (config->filter_symbol)
free(config->filter_symbol);
if (config->size_symbol)
free(config->size_symbol);
free(config);
}
static void
print_config_entry(struct config_entry *ce)
{
while (ce) {
DEBUG_MSG("Name: %s\n", ce->name);
DEBUG_MSG("Type Name: %s, ", ce->type_name);
DEBUG_MSG("flag: %x, ", ce->flag);
DEBUG_MSG("Type flag: %lx, ", ce->type_flag);
DEBUG_MSG("sym_addr: %llx, ", ce->sym_addr);
DEBUG_MSG("vaddr: %lx, ", ce->vaddr);
DEBUG_MSG("offset: %llx, ", (unsigned long long)ce->offset);
DEBUG_MSG("size: %ld\n", ce->size);
ce = ce->next;
}
}
/*
* Read the non-terminal's which are in the form of <Symbol>[.member[...]]
*/
static struct config_entry *
create_config_entry(const char *token, unsigned short flag, int line)
{
struct config_entry *ce = NULL, *ptr, *prev_ce;
char *str, *cur, *next;
long len;
int depth = 0;
if (!token)
return NULL;
cur = str = strdup(token);
prev_ce = ptr = NULL;
while (cur != NULL) {
if ((next = strchr(cur, '.')) != NULL) {
*next++ = '\0';
}
if (!strlen(cur)) {
cur = next;
continue;
}
if ((ptr = calloc(1, sizeof(struct config_entry))) == NULL) {
ERRMSG("Can't allocate memory for config_entry\n");
goto err_out;
}
ptr->line = line;
ptr->flag |= flag;
if (depth == 0) {
/* First node is always a symbol name */
ptr->flag |= SYMBOL_ENTRY;
}
if (flag & ITERATION_ENTRY) {
/* Max depth for iteration entry is 1 */
if (depth > 0) {
ERRMSG("Config error at %d: Invalid iteration "
"variable entry.\n", line);
goto err_out;
}
ptr->name = strdup(cur);
}
if (flag & (FILTER_ENTRY | LIST_ENTRY)) {
ptr->name = strdup(cur);
}
if (flag & SIZE_ENTRY) {
char ch = '\0';
int n = 0;
/* See if absolute length is provided */
if ((depth == 0) &&
((n = sscanf(cur, "%ld%c", &len, &ch)) > 0)) {
if (len < 0) {
ERRMSG("Config error at %d: size "
"value must be positive.\n",
line);
goto err_out;
}
ptr->size = len;
ptr->flag |= ENTRY_RESOLVED;
if (n == 2) {
/* Handle suffix.
* K = Kilobytes
* M = Megabytes
*/
switch (ch) {
case 'M':
case 'm':
ptr->size *= 1024;
case 'K':
case 'k':
ptr->size *= 1024;
break;
}
}
}
else
ptr->name = strdup(cur);
}
if (prev_ce) {
prev_ce->next = ptr;
prev_ce = ptr;
} else
ce = prev_ce = ptr;
cur = next;
ptr = NULL;
depth++;
}
free(str);
return ce;
err_out:
if (ce)
free_config_entry(ce);
if (ptr)
free_config_entry(ptr);
free(str);
return NULL;
}
static int
is_module_loaded(char *mod_name)
{
if (!strcmp(mod_name, "vmlinux") || get_loaded_module(mod_name))
return TRUE;
return FALSE;
}
/*
* read filter config file and return each string token. If the parameter
* expected_token is non-NULL, then return the current token if it matches
* with expected_token otherwise save the current token and return NULL.
* At start of every module section filter_config.new_section is set to 1 and
* subsequent function invocations return NULL untill filter_config.new_section
* is reset to 0 by passing @flag = CONFIG_NEW_CMD (0x02).
*
* Parameters:
* @expected_token INPUT
* Token string to match with currnet token.
* =NULL - return the current available token.
*
* @flag INPUT
* =0x01 - Skip to next module section.
* =0x02 - Treat the next token as next filter command and reset.
*
* @line OUTPUT
* Line number of current token in filter config file.
*
* @cur_mod OUTPUT
* Points to current module section name on non-NULL return value.
*
* @eof OUTPUT
* set to -1 when end of file is reached.
* set to -2 when end of section is reached.
*/
#define NOT_REACH_END (0)
#define REACH_END_OF_FILE (-1)
#define REACH_END_OF_SECTION (-2)
static char *
get_config_token(char *expected_token, unsigned char flag, int *line,
char **cur_mod, int *eof)
{
char *p;
struct filter_config *fc = &filter_config;
int skip = flag & CONFIG_SKIP_SECTION;
if (!fc->file_filterconfig)
return NULL;
if (eof)
*eof = NOT_REACH_END;
/*
* set token and saved_token to NULL if skip module section is set
* to 1.
*/
if (skip) {
fc->token = NULL;
fc->saved_token = NULL;
} else if (fc->saved_token) {
fc->token = fc->saved_token;
fc->saved_token = NULL;
} else if (fc->token)
fc->token = strtok(NULL, " ");
/* Read next line if we are done all tokens from previous line */
while (!fc->token && fgets(config_buf, sizeof(config_buf),
fc->file_filterconfig)) {
if ((p = strchr(config_buf, '\n'))) {
*p = '\0';
fc->line_count++;
}
if ((p = strchr(config_buf, '#'))) {
*p = '\0';
}
/* replace all tabs with spaces */
for (p = config_buf; *p != '\0'; p++)
if (*p == '\t')
*p = ' ';
if (config_buf[0] == '[') {
/* module section entry */
p = strchr(config_buf, ']');
if (!p) {
ERRMSG("Config error at %d: Invalid module "
"section entry.\n", fc->line_count);
/* skip to next valid module section */
skip = 1;
} else {
/*
* Found the valid module section. Reset the
* skip flag.
*/
*p = '\0';
if (fc->cur_module)
free(fc->cur_module);
fc->cur_module = strdup(&config_buf[1]);
fc->new_section = 1;
skip = 0;
}
continue;
}
/*
* If symbol info for current module is not loaded then
* skip to next module section.
*/
if (skip ||
(fc->cur_module && !is_module_loaded(fc->cur_module)))
continue;
fc->token = strtok(config_buf, " ");
}
if (!fc->token) {
if (eof)
*eof = REACH_END_OF_FILE;
return NULL;
}
if (fc->new_section && !(flag & CONFIG_NEW_CMD)) {
fc->saved_token = fc->token;
if (eof)
*eof = REACH_END_OF_SECTION;
return NULL;
}
fc->new_section = 0;
if (cur_mod)
*cur_mod = fc->cur_module;
if (line)
*line = fc->line_count;
if (expected_token && strcmp(fc->token, expected_token)) {
fc->saved_token = fc->token;
return NULL;
}
return fc->token;
}
static int
read_size_entry(struct config *config, int line, int idx)
{
char *token = get_config_token(NULL, 0, &line, NULL, NULL);
if (!token || IS_KEYWORD(token)) {
ERRMSG("Config error at %d: expected size symbol after"
" 'size' keyword.\n", line);
return FALSE;
}
config->size_symbol[idx] = create_config_entry(token, SIZE_ENTRY, line);
if (!config->size_symbol[idx]) {
ERRMSG("Error at line %d: Failed to read size symbol\n",
line);
return FALSE;
}
if (config->iter_entry && config->size_symbol[idx]->name &&
(!strcmp(config->size_symbol[idx]->name,
config->iter_entry->name))) {
config->size_symbol[idx]->flag &= ~SYMBOL_ENTRY;
config->size_symbol[idx]->flag |= VAR_ENTRY;
config->size_symbol[idx]->refer_to = config->iter_entry;
}
return TRUE;
}
/*
* Read erase command entry. The erase command syntax is:
*
* erase <Symbol>[.member[...]] [size <SizeValue>[K|M]]
* erase <Symbol>[.member[...]] [size <SizeSymbol>]
* erase <Symbol>[.member[...]] [nullify]
*/
static int
read_erase_cmd_entry(struct config *config, int line)
{
int size, idx;
char *token = get_config_token(NULL, 0, &line, NULL, NULL);
if (!token || IS_KEYWORD(token)) {
ERRMSG("Config error at %d: expected kernel symbol after"
" 'erase' command.\n", line);
return FALSE;
}
idx = config->num_filter_symbols;
config->num_filter_symbols++;
size = config->num_filter_symbols * sizeof(struct config_entry *);
config->filter_symbol = realloc(config->filter_symbol, size);
config->size_symbol = realloc(config->size_symbol, size);
if (!config->filter_symbol || !config->size_symbol) {
ERRMSG("Can't get memory to read config symbols.\n");
return FALSE;
}
config->filter_symbol[idx] = NULL;
config->size_symbol[idx] = NULL;
config->filter_symbol[idx] =
create_config_entry(token, FILTER_ENTRY, line);
if (!config->filter_symbol[idx]) {
ERRMSG("Error at line %d: Failed to read filter symbol\n",
line);
return FALSE;
}
/*
* Save the symbol expression string for generation of eraseinfo data
* later while writing dumpfile.
*/
config->filter_symbol[idx]->symbol_expr = strdup(token);
if (config->iter_entry) {
if (strcmp(config->filter_symbol[idx]->name,
config->iter_entry->name)) {
ERRMSG("Config error at %d: unused iteration"
" variable '%s'.\n", line,
config->iter_entry->name);
return FALSE;
}
config->filter_symbol[idx]->flag &= ~SYMBOL_ENTRY;
config->filter_symbol[idx]->flag |= VAR_ENTRY;
config->filter_symbol[idx]->refer_to = config->iter_entry;
}
if (get_config_token("nullify", 0, &line, NULL, NULL)) {
config->filter_symbol[idx]->nullify = 1;
} else if (get_config_token("size", 0, &line, NULL, NULL)) {
if (!read_size_entry(config, line, idx))
return FALSE;
}
return TRUE;
}
static int
add_traversal_entry(struct config_entry *ce, char *member, int line)
{
if (!ce)
return FALSE;
while (ce->next)
ce = ce->next;
ce->next = create_config_entry(member, LIST_ENTRY, line);
if (ce->next == NULL) {
ERRMSG("Error at line %d: Failed to read 'via' member\n",
line);
return FALSE;
}
ce->next->flag |= TRAVERSAL_ENTRY;
ce->next->flag &= ~SYMBOL_ENTRY;
return TRUE;
}
static int
read_list_entry(struct config *config, int line)
{
char *token = get_config_token(NULL, 0, &line, NULL, NULL);
if (!token || IS_KEYWORD(token)) {
ERRMSG("Config error at %d: expected list symbol after"
" 'in' keyword.\n", line);
return FALSE;
}
config->list_entry = create_config_entry(token, LIST_ENTRY, line);
if (!config->list_entry) {
ERRMSG("Error at line %d: Failed to read list symbol\n",
line);
return FALSE;
}
/* Check if user has provided 'via' or 'within' keyword */
if (get_config_token("via", 0, &line, NULL, NULL)) {
/* next token is traversal member NextMember */
token = get_config_token(NULL, 0, &line, NULL, NULL);
if (!token) {
ERRMSG("Config error at %d: expected member name after"
" 'via' keyword.\n", line);
return FALSE;
}
if (!add_traversal_entry(config->list_entry, token, line))
return FALSE;
}
else if (get_config_token("within", 0, &line, NULL, NULL)) {
char *s_name, *lh_member;
/* next value is StructName:ListHeadMember */
s_name = get_config_token(NULL, 0, &line, NULL, NULL);
if (!s_name || IS_KEYWORD(s_name)) {
ERRMSG("Config error at %d: expected struct name after"
" 'within' keyword.\n", line);
return FALSE;
}
lh_member = strchr(s_name, ':');
if (lh_member) {
*lh_member++ = '\0';
if (!strlen(lh_member)) {
ERRMSG("Config error at %d: expected list_head"
" member after ':'.\n", line);
return FALSE;
}
config->iter_entry->next =
create_config_entry(lh_member,
ITERATION_ENTRY, line);
if (!config->iter_entry->next)
return FALSE;
config->iter_entry->next->flag &= ~SYMBOL_ENTRY;
}
if (!strlen(s_name)) {
ERRMSG("Config error at %d: Invalid token found "
"after 'within' keyword.\n", line);
return FALSE;
}
config->iter_entry->type_name = strdup(s_name);
}
return TRUE;
}
/*
* Read the iteration entry (LoopConstruct). The syntax is:
*
* for <id> in {<ArrayVar> |
* <StructVar> via <NextMember> |
* <ListHeadVar> within <StructName>:<ListHeadMember>}
* erase <id>[.MemberExpression] [size <SizeExpression>|nullify]
* [erase <id>...]
* [...]
* endfor
*/
static int
read_iteration_entry(struct config *config, int line)
{
int eof = NOT_REACH_END;
char *token = get_config_token(NULL, 0, &line, NULL, NULL);
if (!token || IS_KEYWORD(token)) {
ERRMSG("Config error at %d: expected iteration VAR entry after"
" 'for' keyword.\n", line);
return FALSE;
}
config->iter_entry =
create_config_entry(token, ITERATION_ENTRY, line);
if (!config->iter_entry) {
ERRMSG("Error at line %d: "
"Failed to read iteration VAR entry.\n", line);
return FALSE;
}
if (!get_config_token("in", 0, &line, NULL, NULL)) {
char *token;
token = get_config_token(NULL, 0, &line, NULL, NULL);
if (token)
ERRMSG("Config error at %d: Invalid token '%s'.\n",
line, token);
ERRMSG("Config error at %d: expected token 'in'.\n", line);
return FALSE;
}
if (!read_list_entry(config, line))
return FALSE;
while (!get_config_token("endfor", 0, &line, NULL, &eof) && !eof) {
if (get_config_token("erase", 0, &line, NULL, NULL)) {
if (!read_erase_cmd_entry(config, line))
return FALSE;
} else {
token = get_config_token(NULL, 0, &line, NULL, NULL);
ERRMSG("Config error at %d: "
"Invalid token '%s'.\n", line, token);
return FALSE;
}
}
if (eof != NOT_REACH_END) {
ERRMSG("Config error at %d: No matching 'endfor' found.\n",
line);
return FALSE;
}
return TRUE;
}
/*
* Configuration file 'makedumpfile.conf' contains filter commands.
* Every individual filter command is considered as a config entry.
* A config entry can be provided on a single line or multiple lines.
*/
static struct config *
get_config(int skip)
{
struct config *config;
char *token = NULL;
static int line_count = 0;
char *cur_module = NULL;
int eof = NOT_REACH_END;
unsigned char flag = CONFIG_NEW_CMD;