blob: 8d5b8b6cbb58983ff938d0dbbd8afb5aea15b57e [file] [log] [blame]
Andrew Geissler615f2f12022-07-15 14:00:58 -05001From 7a8c6a06c86e133e4346b1dc66483bd8d0d3c716 Mon Sep 17 00:00:00 2001
2From: John Ogness <john.ogness@linutronix.de>
3Date: Tue, 24 Aug 2021 21:10:43 +0200
4Subject: [PATCH] minicoredumper: retry elf parsing as long as needed
5
6As was reported in github issue #2 ("maximum number of tries
7insufficient, in rare cases, for elf parse"), the number of retries
8for parsing a process may be insufficient. Rather than setting an
9upper limit on the maximum number of retries, track the number of
10headers seen. As long as the number of seen headers is greater than
11the previous try, try again.
12
13In order to avoid introducing any new issues, preserve the behavior
14of retrying at least 10 times, even if no new headers are seen.
15
16Reported-by: github.com/ssajal-wr
17Signed-off-by: John Ogness <john.ogness@linutronix.de>
18
19Upstream-Status: Backport [7a8c6a06c86e133e4346b1dc66483bd8d0d3c716]
20
21Signed-off-by: Sakib Sajal <sakib.sajal@windriver.com>
22---
23 src/minicoredumper/corestripper.c | 30 +++++++++++++++++++++++-------
24 1 file changed, 23 insertions(+), 7 deletions(-)
25
26diff --git a/src/minicoredumper/corestripper.c b/src/minicoredumper/corestripper.c
27index 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--
1272.25.1
128