diff mbox series

[4/7] dump: Introduce dump_is_64bit() helper function

Message ID 20220301142213.28568-5-frankja@linux.ibm.com
State New
Headers show
Series dump: Cleanup and consolidation | expand

Commit Message

Janosch Frank March 1, 2022, 2:22 p.m. UTC
Checking d_class in dump_info leads to lengthy conditionals so let's
shorten things a bit by introducing a helper function.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 dump/dump.c           | 14 +++++++-------
 include/sysemu/dump.h |  6 ++++++
 2 files changed, 13 insertions(+), 7 deletions(-)

Comments

Marc-André Lureau March 1, 2022, 2:33 p.m. UTC | #1
Hi

On Tue, Mar 1, 2022 at 6:30 PM Janosch Frank <frankja@linux.ibm.com> wrote:

> Checking d_class in dump_info leads to lengthy conditionals so let's
> shorten things a bit by introducing a helper function.
>
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> ---
>  dump/dump.c           | 14 +++++++-------
>  include/sysemu/dump.h |  6 ++++++
>  2 files changed, 13 insertions(+), 7 deletions(-)
>
> diff --git a/dump/dump.c b/dump/dump.c
> index 242f83db95..bb152bddff 100644
> --- a/dump/dump.c
> +++ b/dump/dump.c
> @@ -481,7 +481,7 @@ static void write_elf_loads(DumpState *s, Error **errp)
>          get_offset_range(memory_mapping->phys_addr,
>                           memory_mapping->length,
>                           s, &offset, &filesz);
> -        if (s->dump_info.d_class == ELFCLASS64) {
> +        if (dump_is_64bit(s)) {
>              write_elf64_load(s, memory_mapping, phdr_index++, offset,
>                               filesz, &local_err);
>          } else {
> @@ -530,7 +530,7 @@ static void dump_begin(DumpState *s, Error **errp)
>       */
>
>      /* write elf header to vmcore */
> -    if (s->dump_info.d_class == ELFCLASS64) {
> +    if (dump_is_64bit(s)) {
>          write_elf64_header(s, &local_err);
>      } else {
>          write_elf32_header(s, &local_err);
> @@ -540,7 +540,7 @@ static void dump_begin(DumpState *s, Error **errp)
>          return;
>      }
>
> -    if (s->dump_info.d_class == ELFCLASS64) {
> +    if (dump_is_64bit(s)) {
>          /* write PT_NOTE to vmcore */
>          write_elf64_note(s, &local_err);
>          if (local_err) {
> @@ -761,7 +761,7 @@ static void get_note_sizes(DumpState *s, const void
> *note,
>      uint64_t name_sz;
>      uint64_t desc_sz;
>
> -    if (s->dump_info.d_class == ELFCLASS64) {
> +    if (dump_is_64bit(s)) {
>          const Elf64_Nhdr *hdr = note;
>          note_head_sz = sizeof(Elf64_Nhdr);
>          name_sz = tswap64(hdr->n_namesz);
> @@ -1023,7 +1023,7 @@ out:
>
>  static void write_dump_header(DumpState *s, Error **errp)
>  {
> -    if (s->dump_info.d_class == ELFCLASS32) {
> +    if (!dump_is_64bit(s)) {
>          create_header32(s, errp);
>      } else {
>          create_header64(s, errp);
> @@ -1716,7 +1716,7 @@ static void dump_init(DumpState *s, int fd, bool
> has_format,
>          uint32_t size;
>          uint16_t format;
>
> -        note_head_size = s->dump_info.d_class == ELFCLASS32 ?
> +        note_head_size = !dump_is_64bit(s) ?
>              sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
>
>          format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
> @@ -1821,7 +1821,7 @@ static void dump_init(DumpState *s, int fd, bool
> has_format,
>          }
>      }
>
> -    if (s->dump_info.d_class == ELFCLASS64) {
> +    if (dump_is_64bit(s)) {
>          s->phdr_offset = sizeof(Elf64_Ehdr);
>          s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) *
> s->phdr_num;
>          s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) *
> s->shdr_num;
> diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
> index ffc2ea1072..078b3d57a1 100644
> --- a/include/sysemu/dump.h
> +++ b/include/sysemu/dump.h
> @@ -203,4 +203,10 @@ typedef struct DumpState {
>  uint16_t cpu_to_dump16(DumpState *s, uint16_t val);
>  uint32_t cpu_to_dump32(DumpState *s, uint32_t val);
>  uint64_t cpu_to_dump64(DumpState *s, uint64_t val);
> +
> +static inline bool dump_is_64bit(DumpState *s)
> +{
> +    return s->dump_info.d_class == ELFCLASS64;
> +}
>

Since it's not used outside of dump.c (so far), I'd keep this as a regular
static function there. Otherwise, lgtm.
diff mbox series

Patch

diff --git a/dump/dump.c b/dump/dump.c
index 242f83db95..bb152bddff 100644
--- a/dump/dump.c
+++ b/dump/dump.c
@@ -481,7 +481,7 @@  static void write_elf_loads(DumpState *s, Error **errp)
         get_offset_range(memory_mapping->phys_addr,
                          memory_mapping->length,
                          s, &offset, &filesz);
-        if (s->dump_info.d_class == ELFCLASS64) {
+        if (dump_is_64bit(s)) {
             write_elf64_load(s, memory_mapping, phdr_index++, offset,
                              filesz, &local_err);
         } else {
@@ -530,7 +530,7 @@  static void dump_begin(DumpState *s, Error **errp)
      */
 
     /* write elf header to vmcore */
-    if (s->dump_info.d_class == ELFCLASS64) {
+    if (dump_is_64bit(s)) {
         write_elf64_header(s, &local_err);
     } else {
         write_elf32_header(s, &local_err);
@@ -540,7 +540,7 @@  static void dump_begin(DumpState *s, Error **errp)
         return;
     }
 
-    if (s->dump_info.d_class == ELFCLASS64) {
+    if (dump_is_64bit(s)) {
         /* write PT_NOTE to vmcore */
         write_elf64_note(s, &local_err);
         if (local_err) {
@@ -761,7 +761,7 @@  static void get_note_sizes(DumpState *s, const void *note,
     uint64_t name_sz;
     uint64_t desc_sz;
 
-    if (s->dump_info.d_class == ELFCLASS64) {
+    if (dump_is_64bit(s)) {
         const Elf64_Nhdr *hdr = note;
         note_head_sz = sizeof(Elf64_Nhdr);
         name_sz = tswap64(hdr->n_namesz);
@@ -1023,7 +1023,7 @@  out:
 
 static void write_dump_header(DumpState *s, Error **errp)
 {
-    if (s->dump_info.d_class == ELFCLASS32) {
+    if (!dump_is_64bit(s)) {
         create_header32(s, errp);
     } else {
         create_header64(s, errp);
@@ -1716,7 +1716,7 @@  static void dump_init(DumpState *s, int fd, bool has_format,
         uint32_t size;
         uint16_t format;
 
-        note_head_size = s->dump_info.d_class == ELFCLASS32 ?
+        note_head_size = !dump_is_64bit(s) ?
             sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
 
         format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
@@ -1821,7 +1821,7 @@  static void dump_init(DumpState *s, int fd, bool has_format,
         }
     }
 
-    if (s->dump_info.d_class == ELFCLASS64) {
+    if (dump_is_64bit(s)) {
         s->phdr_offset = sizeof(Elf64_Ehdr);
         s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
         s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
index ffc2ea1072..078b3d57a1 100644
--- a/include/sysemu/dump.h
+++ b/include/sysemu/dump.h
@@ -203,4 +203,10 @@  typedef struct DumpState {
 uint16_t cpu_to_dump16(DumpState *s, uint16_t val);
 uint32_t cpu_to_dump32(DumpState *s, uint32_t val);
 uint64_t cpu_to_dump64(DumpState *s, uint64_t val);
+
+static inline bool dump_is_64bit(DumpState *s)
+{
+    return s->dump_info.d_class == ELFCLASS64;
+}
+
 #endif