Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 1 | From 7a8c6a06c86e133e4346b1dc66483bd8d0d3c716 Mon Sep 17 00:00:00 2001 |
| 2 | From: John Ogness <john.ogness@linutronix.de> |
| 3 | Date: Tue, 24 Aug 2021 21:10:43 +0200 |
| 4 | Subject: [PATCH] minicoredumper: retry elf parsing as long as needed |
| 5 | |
| 6 | As was reported in github issue #2 ("maximum number of tries |
| 7 | insufficient, in rare cases, for elf parse"), the number of retries |
| 8 | for parsing a process may be insufficient. Rather than setting an |
| 9 | upper limit on the maximum number of retries, track the number of |
| 10 | headers seen. As long as the number of seen headers is greater than |
| 11 | the previous try, try again. |
| 12 | |
| 13 | In order to avoid introducing any new issues, preserve the behavior |
| 14 | of retrying at least 10 times, even if no new headers are seen. |
| 15 | |
| 16 | Reported-by: github.com/ssajal-wr |
| 17 | Signed-off-by: John Ogness <john.ogness@linutronix.de> |
| 18 | |
| 19 | Upstream-Status: Backport [7a8c6a06c86e133e4346b1dc66483bd8d0d3c716] |
| 20 | |
| 21 | Signed-off-by: Sakib Sajal <sakib.sajal@windriver.com> |
| 22 | --- |
| 23 | src/minicoredumper/corestripper.c | 30 +++++++++++++++++++++++------- |
| 24 | 1 file changed, 23 insertions(+), 7 deletions(-) |
| 25 | |
| 26 | diff --git a/src/minicoredumper/corestripper.c b/src/minicoredumper/corestripper.c |
| 27 | index d96d1df..c96b350 100644 |
| 28 | --- a/src/minicoredumper/corestripper.c |
| 29 | +++ b/src/minicoredumper/corestripper.c |
| 30 | @@ -761,7 +761,7 @@ static int init_log(struct dump_info *di) |
| 31 | typedef int elf_parse_cb(struct dump_info *di, Elf *elf, GElf_Phdr *phdr); |
| 32 | |
| 33 | static int do_elf_ph_parse(struct dump_info *di, GElf_Phdr *type, |
| 34 | - elf_parse_cb *callback) |
| 35 | + elf_parse_cb *callback, size_t *phnum_found) |
| 36 | { |
| 37 | GElf_Ehdr ehdr_mem; |
| 38 | GElf_Ehdr *ehdr; |
| 39 | @@ -770,6 +770,9 @@ static int do_elf_ph_parse(struct dump_info *di, GElf_Phdr *type, |
| 40 | size_t phnum; |
| 41 | size_t cnt; |
| 42 | |
| 43 | + if (phnum_found) |
| 44 | + *phnum_found = 0; |
| 45 | + |
| 46 | /* start from beginning of core */ |
| 47 | if (lseek64(di->elf_fd, 0, SEEK_SET) == -1) { |
| 48 | info("lseek failed: %s", strerror(errno)); |
| 49 | @@ -809,6 +812,9 @@ static int do_elf_ph_parse(struct dump_info *di, GElf_Phdr *type, |
| 50 | goto out; |
| 51 | } |
| 52 | |
| 53 | + if (phnum_found) |
| 54 | + *phnum_found = phnum; |
| 55 | + |
| 56 | for (cnt = 0; cnt < phnum; cnt++) { |
| 57 | GElf_Phdr phdr_mem; |
| 58 | GElf_Phdr *phdr; |
| 59 | @@ -891,7 +897,7 @@ static int vma_cb(struct dump_info *di, Elf *elf, GElf_Phdr *phdr) |
| 60 | /* |
| 61 | * Tries to parse the found ELF headers and reads all vmas from it. |
| 62 | */ |
| 63 | -static int parse_vma_info(struct dump_info *di) |
| 64 | +static int parse_vma_info(struct dump_info *di, size_t *phnum_found) |
| 65 | { |
| 66 | unsigned long min_off = ULONG_MAX; |
| 67 | unsigned long max_len = 0; |
| 68 | @@ -911,7 +917,7 @@ static int parse_vma_info(struct dump_info *di) |
| 69 | memset(&type, 0, sizeof(type)); |
| 70 | type.p_type = PT_LOAD; |
| 71 | type.p_flags = PF_R; |
| 72 | - if (do_elf_ph_parse(di, &type, vma_cb) != 0) |
| 73 | + if (do_elf_ph_parse(di, &type, vma_cb, phnum_found) != 0) |
| 74 | return -1; |
| 75 | |
| 76 | for (v = di->vma; v; v = v->next) { |
| 77 | @@ -1614,8 +1620,10 @@ int add_core_data(struct dump_info *di, off64_t dest_offset, size_t len, |
| 78 | */ |
| 79 | static int init_src_core(struct dump_info *di, int src) |
| 80 | { |
| 81 | + size_t last_phnum = 0; |
| 82 | int tries = 0; |
| 83 | int ret = -1; |
| 84 | + size_t phnum; |
| 85 | size_t len; |
| 86 | char *buf; |
| 87 | long pos; |
| 88 | @@ -1642,7 +1650,7 @@ again: |
| 89 | goto out; |
| 90 | |
| 91 | /* try to elf-parse the core to read vma info */ |
| 92 | - ret = parse_vma_info(di); |
| 93 | + ret = parse_vma_info(di, &phnum); |
| 94 | |
| 95 | /* restore our position */ |
| 96 | if (lseek64(di->elf_fd, pos, SEEK_SET) == -1) |
| 97 | @@ -1653,9 +1661,17 @@ again: |
| 98 | |
| 99 | tries++; |
| 100 | |
| 101 | - /* maybe try again */ |
| 102 | - if (tries < 10) |
| 103 | + if (phnum > last_phnum) { |
| 104 | + /* new headers found, keep trying */ |
| 105 | + last_phnum = phnum; |
| 106 | goto again; |
| 107 | + } else if (tries < 10) { |
| 108 | + /* |
| 109 | + * even if no new headers are found, |
| 110 | + * retry at least 10 times |
| 111 | + */ |
| 112 | + goto again; |
| 113 | + } |
| 114 | |
| 115 | goto out; |
| 116 | } |
| 117 | @@ -2106,7 +2122,7 @@ static int dump_stacks(struct dump_info *di) |
| 118 | /* find and set the first task */ |
| 119 | memset(&type, 0, sizeof(type)); |
| 120 | type.p_type = PT_NOTE; |
| 121 | - do_elf_ph_parse(di, &type, note_cb); |
| 122 | + do_elf_ph_parse(di, &type, note_cb, NULL); |
| 123 | } |
| 124 | |
| 125 | if (di->first_pid) |
| 126 | -- |
| 127 | 2.25.1 |
| 128 | |