drgn/libdrgn/binary_buffer.c
Omar Sandoval 5975d19580 libdrgn: report better errors when parsing DWARF/kmod index
If the DWARF index encounters any error while parsing, it returns an
error saying only "debug information is truncated", which makes it hard
to track down parsing errors. The kmod index parser silently swallows
errors. For both, replace the mread functions with a higher-level
binary_buffer interface that can include more information including the
location of the error. For example:

  /tmp/mybinary: .debug_info+0x4: expected at least 56 bytes, have 55

Signed-off-by: Omar Sandoval <osandov@osandov.com>
2020-11-13 17:00:07 -08:00

44 lines
1012 B
C

// Copyright (c) Facebook, Inc. and its affiliates.
// SPDX-License-Identifier: GPL-3.0+
#include "binary_buffer.h"
#include "drgn.h"
#include <stdarg.h>
static struct drgn_error *binary_buffer_error_vat(struct binary_buffer *bb,
const char *pos,
const char *format,
va_list ap)
{
char *message;
int ret = vasprintf(&message, format, ap);
if (ret == -1)
return &drgn_enomem;
struct drgn_error *err = bb->error_fn(bb, pos, message);
free(message);
return err;
}
struct drgn_error *binary_buffer_error(struct binary_buffer *bb,
const char *format, ...)
{
va_list ap;
va_start(ap, format);
struct drgn_error *err = binary_buffer_error_vat(bb, bb->prev, format,
ap);
va_end(ap);
return err;
}
struct drgn_error *binary_buffer_error_at(struct binary_buffer *bb,
const char *pos, const char *format,
...)
{
va_list ap;
va_start(ap, format);
struct drgn_error *err = binary_buffer_error_vat(bb, pos, format, ap);
va_end(ap);
return err;
}