Patchwork [v6,04/11] dump: Add API to write elf notes to buffer

login
register
mail settings
Submitter Qiao Nuohan
Date Jan. 5, 2014, 7:27 a.m.
Message ID <1388906864-1083-5-git-send-email-qiaonuohan@cn.fujitsu.com>
Download mbox | patch
Permalink /patch/306926/
State New
Headers show

Comments

Qiao Nuohan - Jan. 5, 2014, 7:27 a.m.
the function can be used by write_elf32_notes/write_elf64_notes to write notes
to a buffer. If fd_write_vmcore is used, write_elf32_notes/write_elf64_notes
will write elf notes to vmcore directly. Instead, if buf_write_note is used,
elf notes will be written to opaque->note_buf at first.

Signed-off-by: Qiao Nuohan <qiaonuohan@cn.fujitsu.com>
---
 dump.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
Laszlo Ersek - Jan. 6, 2014, 6:46 p.m.
some tangential comments:

On 01/05/14 08:27, Qiao Nuohan wrote:
> the function can be used by write_elf32_notes/write_elf64_notes to write notes
> to a buffer. If fd_write_vmcore is used, write_elf32_notes/write_elf64_notes
> will write elf notes to vmcore directly. Instead, if buf_write_note is used,
> elf notes will be written to opaque->note_buf at first.
> 
> Signed-off-by: Qiao Nuohan <qiaonuohan@cn.fujitsu.com>
> ---
>  dump.c |   19 +++++++++++++++++++
>  1 files changed, 19 insertions(+), 0 deletions(-)
> 
> diff --git a/dump.c b/dump.c
> index 764db39..3b9cf00 100644
> --- a/dump.c
> +++ b/dump.c
> @@ -76,6 +76,9 @@ typedef struct DumpState {
>      int64_t begin;
>      int64_t length;
>      Error **errp;
> +
> +    void *note_buf;
> +    size_t note_buf_offset;
>  } DumpState;
>  
>  static int dump_cleanup(DumpState *s)
> @@ -754,6 +757,22 @@ static int write_buffer(int fd, bool flag_flatten, off_t offset, void *buf,
>      return 0;
>  }
>  
> +static int buf_write_note(void *buf, size_t size, void *opaque)
> +{

"const void *buf" would have been more "elegant".

> +    DumpState *s = opaque;
> +
> +    /* note_buf is not enough */
> +    if (s->note_buf_offset + size > s->note_size) {
> +        return -1;
> +    }
> +
> +    memcpy(s->note_buf + s->note_buf_offset, buf, size);

Giving type "uint8_t" to "*note_buf" would have been preferable.
Addition to a pointer-to-void is a constraint violation in standard C
("... operand shall be a pointer to an object type ..."), ie. it's a gcc
extension here, but I guess we can live with it.

Using s->note_size as limit seems correct.

> +
> +    s->note_buf_offset += size;
> +
> +    return 0;
> +}
> +
>  static ram_addr_t get_start_block(DumpState *s)
>  {
>      GuestPhysBlock *block;
> 

Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Qiao Nuohan - Jan. 7, 2014, 6:17 a.m.
On 01/07/2014 02:46 AM, Laszlo Ersek wrote:
>>   static int dump_cleanup(DumpState *s)
>> >  @@ -754,6 +757,22 @@ static int write_buffer(int fd, bool flag_flatten, off_t offset, void *buf,
>> >        return 0;
>> >    }
>> >
>> >  +static int buf_write_note(void *buf, size_t size, void *opaque)
>> >  +{
> "const void *buf" would have been more "elegant".
>
>> >  +    DumpState *s = opaque;
>> >  +
>> >  +    /* note_buf is not enough */
>> >  +    if (s->note_buf_offset + size>  s->note_size) {
>> >  +        return -1;
>> >  +    }
>> >  +
>> >  +    memcpy(s->note_buf + s->note_buf_offset, buf, size);
> Giving type "uint8_t" to "*note_buf" would have been preferable.
> Addition to a pointer-to-void is a constraint violation in standard C
> ("... operand shall be a pointer to an object type ..."), ie. it's a gcc
> extension here, but I guess we can live with it.
>
> Using s->note_size as limit seems correct.
>

Acked.

Patch

diff --git a/dump.c b/dump.c
index 764db39..3b9cf00 100644
--- a/dump.c
+++ b/dump.c
@@ -76,6 +76,9 @@  typedef struct DumpState {
     int64_t begin;
     int64_t length;
     Error **errp;
+
+    void *note_buf;
+    size_t note_buf_offset;
 } DumpState;
 
 static int dump_cleanup(DumpState *s)
@@ -754,6 +757,22 @@  static int write_buffer(int fd, bool flag_flatten, off_t offset, void *buf,
     return 0;
 }
 
+static int buf_write_note(void *buf, size_t size, void *opaque)
+{
+    DumpState *s = opaque;
+
+    /* note_buf is not enough */
+    if (s->note_buf_offset + size > s->note_size) {
+        return -1;
+    }
+
+    memcpy(s->note_buf + s->note_buf_offset, buf, size);
+
+    s->note_buf_offset += size;
+
+    return 0;
+}
+
 static ram_addr_t get_start_block(DumpState *s)
 {
     GuestPhysBlock *block;