libdrgn: add drgn_error_from_string_builder()

And use that instead of exposing drgn_error_create_nodup().
This commit is contained in:
Omar Sandoval 2019-05-14 10:16:01 -07:00
parent b0f10d3b58
commit e21ed988fb
3 changed files with 23 additions and 15 deletions

View File

@ -10,6 +10,7 @@
#include <string.h> #include <string.h>
#include "internal.h" #include "internal.h"
#include "string_builder.h"
LIBDRGN_PUBLIC struct drgn_error drgn_enomem = { LIBDRGN_PUBLIC struct drgn_error drgn_enomem = {
.code = DRGN_ERROR_NO_MEMORY, .code = DRGN_ERROR_NO_MEMORY,
@ -26,8 +27,8 @@ struct drgn_error drgn_not_elf = {
.message = "not an ELF file", .message = "not an ELF file",
}; };
struct drgn_error *drgn_error_create_nodup(enum drgn_error_code code, static struct drgn_error *drgn_error_create_nodup(enum drgn_error_code code,
char *message) char *message)
{ {
struct drgn_error *err; struct drgn_error *err;
@ -102,6 +103,18 @@ LIBDRGN_PUBLIC struct drgn_error *drgn_error_format(enum drgn_error_code code,
return drgn_error_create_nodup(code, message); return drgn_error_create_nodup(code, message);
} }
struct drgn_error *drgn_error_from_string_builder(enum drgn_error_code code,
struct string_builder *sb)
{
char *message;
if (!string_builder_finalize(sb, &message)) {
free(sb->str);
return &drgn_enomem;
}
return drgn_error_create_nodup(code, message);
}
LIBDRGN_PUBLIC void drgn_error_fwrite(FILE *file, struct drgn_error *err) LIBDRGN_PUBLIC void drgn_error_fwrite(FILE *file, struct drgn_error *err)
{ {
if (err->code == DRGN_ERROR_OS) { if (err->code == DRGN_ERROR_OS) {

View File

@ -40,14 +40,15 @@ extern struct drgn_error drgn_stop;
/** Not an ELF file. */ /** Not an ELF file. */
extern struct drgn_error drgn_not_elf; extern struct drgn_error drgn_not_elf;
struct string_builder;
/** /**
* Create a @ref drgn_error from a string that does not need to be duplicated. * Create a @ref drgn_error with a message from a @ref string_builder.
* *
* If there is a failure to allocate memory for the error, @p message is freed * This finalizes the string builder.
* and @ref drgn_enomem is returned instead.
*/ */
struct drgn_error *drgn_error_create_nodup(enum drgn_error_code code, struct drgn_error *drgn_error_from_string_builder(enum drgn_error_code code,
char *message); struct string_builder *sb);
/** Create a @ref drgn_error from the libelf error indicator. */ /** Create a @ref drgn_error from the libelf error indicator. */
struct drgn_error *drgn_error_libelf(void) struct drgn_error *drgn_error_libelf(void)

View File

@ -1402,14 +1402,8 @@ static struct drgn_error *load_kernel_debug_info(struct drgn_program *prog)
goto err; goto err;
if (missing_debug_info.len) { if (missing_debug_info.len) {
char *msg; return drgn_error_from_string_builder(DRGN_ERROR_MISSING_DEBUG_INFO,
&missing_debug_info);
if (!string_builder_finalize(&missing_debug_info, &msg)) {
err = &drgn_enomem;
goto err;
}
return drgn_error_create_nodup(DRGN_ERROR_MISSING_DEBUG_INFO,
msg);
} }
return NULL; return NULL;