|
|
7a865b |
From a7c4cb8e998571cb3dd62e907935a1e052b15d6c Mon Sep 17 00:00:00 2001
|
|
|
7a865b |
From: Lianbo Jiang <lijiang@redhat.com>
|
|
|
7a865b |
Date: Fri, 23 Aug 2019 20:05:38 +0800
|
|
|
7a865b |
Subject: [PATCH 3/5] Cleanup: move it back from util_lib/elf_info.c
|
|
|
7a865b |
|
|
|
7a865b |
Some code related to vmcore-dmesg.c is put into the util_lib, which
|
|
|
7a865b |
is not very reasonable, so lets move it back and tidy up those code.
|
|
|
7a865b |
|
|
|
7a865b |
In addition, that will also help to limit the size of vmcore-dmesg.txt
|
|
|
7a865b |
in vmcore-dmesg.c instead of elf_info.c.
|
|
|
7a865b |
|
|
|
7a865b |
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
|
|
|
7a865b |
Signed-off-by: Simon Horman <horms@verge.net.au>
|
|
|
7a865b |
---
|
|
|
7a865b |
util_lib/elf_info.c | 48 +++++++++----------------------------
|
|
|
7a865b |
util_lib/include/elf_info.h | 2 +-
|
|
|
7a865b |
vmcore-dmesg/vmcore-dmesg.c | 30 ++++++++++++++++++++++-
|
|
|
7a865b |
3 files changed, 41 insertions(+), 39 deletions(-)
|
|
|
7a865b |
|
|
|
7a865b |
diff --git a/util_lib/elf_info.c b/util_lib/elf_info.c
|
|
|
7a865b |
index 5d0efaafab53..2bce5cb1713c 100644
|
|
|
7a865b |
--- a/util_lib/elf_info.c
|
|
|
7a865b |
+++ b/util_lib/elf_info.c
|
|
|
7a865b |
@@ -531,19 +531,7 @@ static int32_t read_file_s32(int fd, uint64_t addr)
|
|
|
7a865b |
return read_file_u32(fd, addr);
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
-static void write_to_stdout(char *buf, unsigned int nr)
|
|
|
7a865b |
-{
|
|
|
7a865b |
- ssize_t ret;
|
|
|
7a865b |
-
|
|
|
7a865b |
- ret = write(STDOUT_FILENO, buf, nr);
|
|
|
7a865b |
- if (ret != nr) {
|
|
|
7a865b |
- fprintf(stderr, "Failed to write out the dmesg log buffer!:"
|
|
|
7a865b |
- " %s\n", strerror(errno));
|
|
|
7a865b |
- exit(54);
|
|
|
7a865b |
- }
|
|
|
7a865b |
-}
|
|
|
7a865b |
-
|
|
|
7a865b |
-static void dump_dmesg_legacy(int fd)
|
|
|
7a865b |
+static void dump_dmesg_legacy(int fd, void (*handler)(char*, unsigned int))
|
|
|
7a865b |
{
|
|
|
7a865b |
uint64_t log_buf, log_buf_offset;
|
|
|
7a865b |
unsigned log_end, logged_chars, log_end_wrapped;
|
|
|
7a865b |
@@ -604,7 +592,8 @@ static void dump_dmesg_legacy(int fd)
|
|
|
7a865b |
*/
|
|
|
7a865b |
logged_chars = log_end < log_buf_len ? log_end : log_buf_len;
|
|
|
7a865b |
|
|
|
7a865b |
- write_to_stdout(buf + (log_buf_len - logged_chars), logged_chars);
|
|
|
7a865b |
+ if (handler)
|
|
|
7a865b |
+ handler(buf + (log_buf_len - logged_chars), logged_chars);
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
static inline uint16_t struct_val_u16(char *ptr, unsigned int offset)
|
|
|
7a865b |
@@ -623,7 +612,7 @@ static inline uint64_t struct_val_u64(char *ptr, unsigned int offset)
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
/* Read headers of log records and dump accordingly */
|
|
|
7a865b |
-static void dump_dmesg_structured(int fd)
|
|
|
7a865b |
+static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
|
|
|
7a865b |
{
|
|
|
7a865b |
#define OUT_BUF_SIZE 4096
|
|
|
7a865b |
uint64_t log_buf, log_buf_offset, ts_nsec;
|
|
|
7a865b |
@@ -733,7 +722,8 @@ static void dump_dmesg_structured(int fd)
|
|
|
7a865b |
out_buf[len++] = c;
|
|
|
7a865b |
|
|
|
7a865b |
if (len >= OUT_BUF_SIZE - 64) {
|
|
|
7a865b |
- write_to_stdout(out_buf, len);
|
|
|
7a865b |
+ if (handler)
|
|
|
7a865b |
+ handler(out_buf, len);
|
|
|
7a865b |
len = 0;
|
|
|
7a865b |
}
|
|
|
7a865b |
}
|
|
|
7a865b |
@@ -752,16 +742,16 @@ static void dump_dmesg_structured(int fd)
|
|
|
7a865b |
current_idx += loglen;
|
|
|
7a865b |
}
|
|
|
7a865b |
free(buf);
|
|
|
7a865b |
- if (len)
|
|
|
7a865b |
- write_to_stdout(out_buf, len);
|
|
|
7a865b |
+ if (len && handler)
|
|
|
7a865b |
+ handler(out_buf, len);
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
-static void dump_dmesg(int fd)
|
|
|
7a865b |
+void dump_dmesg(int fd, void (*handler)(char*, unsigned int))
|
|
|
7a865b |
{
|
|
|
7a865b |
if (log_first_idx_vaddr)
|
|
|
7a865b |
- dump_dmesg_structured(fd);
|
|
|
7a865b |
+ dump_dmesg_structured(fd, handler);
|
|
|
7a865b |
else
|
|
|
7a865b |
- dump_dmesg_legacy(fd);
|
|
|
7a865b |
+ dump_dmesg_legacy(fd, handler);
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
int read_elf(int fd)
|
|
|
7a865b |
@@ -808,22 +798,6 @@ int read_elf(int fd)
|
|
|
7a865b |
return 0;
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
-int read_elf_vmcore(int fd)
|
|
|
7a865b |
-{
|
|
|
7a865b |
- int ret;
|
|
|
7a865b |
-
|
|
|
7a865b |
- ret = read_elf(fd);
|
|
|
7a865b |
- if (ret > 0) {
|
|
|
7a865b |
- fprintf(stderr, "Unable to read ELF information"
|
|
|
7a865b |
- " from vmcore\n");
|
|
|
7a865b |
- return ret;
|
|
|
7a865b |
- }
|
|
|
7a865b |
-
|
|
|
7a865b |
- dump_dmesg(fd);
|
|
|
7a865b |
-
|
|
|
7a865b |
- return 0;
|
|
|
7a865b |
-}
|
|
|
7a865b |
-
|
|
|
7a865b |
int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off)
|
|
|
7a865b |
{
|
|
|
7a865b |
int ret;
|
|
|
7a865b |
diff --git a/util_lib/include/elf_info.h b/util_lib/include/elf_info.h
|
|
|
7a865b |
index c328a1b0ecf2..4bc9279ba603 100644
|
|
|
7a865b |
--- a/util_lib/include/elf_info.h
|
|
|
7a865b |
+++ b/util_lib/include/elf_info.h
|
|
|
7a865b |
@@ -30,6 +30,6 @@ int get_pt_load(int idx,
|
|
|
7a865b |
unsigned long long *virt_end);
|
|
|
7a865b |
int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off);
|
|
|
7a865b |
int read_elf(int fd);
|
|
|
7a865b |
-int read_elf_vmcore(int fd);
|
|
|
7a865b |
+void dump_dmesg(int fd, void (*handler)(char*, unsigned int));
|
|
|
7a865b |
|
|
|
7a865b |
#endif /* ELF_INFO_H */
|
|
|
7a865b |
diff --git a/vmcore-dmesg/vmcore-dmesg.c b/vmcore-dmesg/vmcore-dmesg.c
|
|
|
7a865b |
index bebc348a657e..fe7df8ec372c 100644
|
|
|
7a865b |
--- a/vmcore-dmesg/vmcore-dmesg.c
|
|
|
7a865b |
+++ b/vmcore-dmesg/vmcore-dmesg.c
|
|
|
7a865b |
@@ -5,6 +5,34 @@ typedef Elf32_Nhdr Elf_Nhdr;
|
|
|
7a865b |
|
|
|
7a865b |
extern const char *fname;
|
|
|
7a865b |
|
|
|
7a865b |
+static void write_to_stdout(char *buf, unsigned int nr)
|
|
|
7a865b |
+{
|
|
|
7a865b |
+ ssize_t ret;
|
|
|
7a865b |
+
|
|
|
7a865b |
+ ret = write(STDOUT_FILENO, buf, nr);
|
|
|
7a865b |
+ if (ret != nr) {
|
|
|
7a865b |
+ fprintf(stderr, "Failed to write out the dmesg log buffer!:"
|
|
|
7a865b |
+ " %s\n", strerror(errno));
|
|
|
7a865b |
+ exit(54);
|
|
|
7a865b |
+ }
|
|
|
7a865b |
+}
|
|
|
7a865b |
+
|
|
|
7a865b |
+static int read_vmcore_dmesg(int fd, void (*handler)(char*, unsigned int))
|
|
|
7a865b |
+{
|
|
|
7a865b |
+ int ret;
|
|
|
7a865b |
+
|
|
|
7a865b |
+ ret = read_elf(fd);
|
|
|
7a865b |
+ if (ret > 0) {
|
|
|
7a865b |
+ fprintf(stderr, "Unable to read ELF information"
|
|
|
7a865b |
+ " from vmcore\n");
|
|
|
7a865b |
+ return ret;
|
|
|
7a865b |
+ }
|
|
|
7a865b |
+
|
|
|
7a865b |
+ dump_dmesg(fd, handler);
|
|
|
7a865b |
+
|
|
|
7a865b |
+ return 0;
|
|
|
7a865b |
+}
|
|
|
7a865b |
+
|
|
|
7a865b |
int main(int argc, char **argv)
|
|
|
7a865b |
{
|
|
|
7a865b |
ssize_t ret;
|
|
|
7a865b |
@@ -23,7 +51,7 @@ int main(int argc, char **argv)
|
|
|
7a865b |
return 2;
|
|
|
7a865b |
}
|
|
|
7a865b |
|
|
|
7a865b |
- ret = read_elf_vmcore(fd);
|
|
|
7a865b |
+ ret = read_vmcore_dmesg(fd, write_to_stdout);
|
|
|
7a865b |
|
|
|
7a865b |
close(fd);
|
|
|
7a865b |
|
|
|
7a865b |
--
|
|
|
7a865b |
2.17.1
|
|
|
7a865b |
|