Blame src/output/mdso_output_error.c

bfa928
/****************************************************************/
bfa928
/*  mdso: midipix dso scavenger                                 */
bfa928
/*  Copyright (C) 2015--2016  Z. Gilboa                         */
bfa928
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
bfa928
/****************************************************************/
bfa928
bfa928
#include <stdio.h>
bfa928
#include <string.h>
bfa928
#include <errno.h>
bfa928
#include <unistd.h>
bfa928
#include <mdso/mdso.h>
bfa928
bfa928
static const char aclr_reset[]   = "\x1b[0m";
bfa928
static const char aclr_bold[]    = "\x1b[1m";
bfa928
7d3ce7
static const char aclr_red[]     = "\x1b[31m";
bfa928
static const char aclr_green[]   = "\x1b[32m";
bfa928
static const char aclr_blue[]    = "\x1b[34m";
bfa928
static const char aclr_magenta[] = "\x1b[35m";
bfa928
f72b5d
static const char const * const mdso_error_strings[MDSO_ERR_CAP] = {
f72b5d
	[MDSO_ERR_FLOW_ERROR]       = "flow error: unexpected condition or other",
f72b5d
	[MDSO_ERR_FLEE_ERROR]       = "flees and bugs and cats and mice",
f72b5d
	[MDSO_ERR_NULL_CONTEXT]     = "null driver or unit context",
f72b5d
	[MDSO_ERR_NULL_SOURCE]      = "source file does not define any symbols",
f72b5d
	[MDSO_ERR_INVALID_CONTEXT]  = "invalid driver or unit context",
f72b5d
	[MDSO_ERR_INVALID_SOURCE]   = "invalid symbol definition source file",
f72b5d
	[MDSO_ERR_SOURCE_SIZE_ZERO] = "cannot map an empty symbol definition source file",
f72b5d
};
f72b5d
bfa928
static const char * mdso_output_error_header(const struct mdso_error_info * erri)
bfa928
{
1d4a9e
	if (erri->eflags & MDSO_ERROR_CHILD)
bfa928
		return "exec error upon";
bfa928
1d4a9e
	else if (erri->eflags & MDSO_ERROR_TOP_LEVEL)
bfa928
		return "error logged in";
bfa928
1d4a9e
	else if (erri->eflags & MDSO_ERROR_NESTED)
bfa928
		return "< returned to >";
bfa928
bfa928
	else
bfa928
		return "distorted state";
bfa928
}
bfa928
7d3ce7
static const char * mdso_output_unit_header(const struct mdso_error_info * erri)
7d3ce7
{
7d3ce7
	if (!(erri->eflags & MDSO_ERROR_CUSTOM))
7d3ce7
		return "while opening";
7d3ce7
7d3ce7
	else if (erri->elibcode == MDSO_ERR_SOURCE_SIZE_ZERO)
7d3ce7
		return "while mapping";
7d3ce7
7d3ce7
	else
7d3ce7
		return "while parsing";
7d3ce7
}
7d3ce7
bfa928
static const char * mdso_output_strerror(const struct mdso_error_info * erri)
bfa928
{
1d4a9e
	if (erri->eflags & MDSO_ERROR_CUSTOM)
f72b5d
		return ((erri->elibcode < 0) || (erri->elibcode >= MDSO_ERR_CAP))
f72b5d
			? "internal error: please report to the maintainer"
f72b5d
			: mdso_error_strings[erri->elibcode];
bfa928
1d4a9e
	else if (erri->eflags & MDSO_ERROR_NESTED)
bfa928
		return "";
bfa928
1d4a9e
	else if (erri->eflags & MDSO_ERROR_CHILD)
bfa928
		return "(see child process error messages)";
bfa928
1d4a9e
	else if (erri->esyscode == ENOBUFS)
bfa928
		return "input error: string length exceeds buffer size.";
bfa928
bfa928
	else
1d4a9e
		return strerror(erri->esyscode);
bfa928
}
bfa928
bfa928
static int mdso_output_error_record_plain(
bfa928
	const struct mdso_driver_ctx *	dctx,
bfa928
	const struct mdso_error_info *	erri)
bfa928
{
7d3ce7
	const char * epath;
bfa928
	const char * errdesc = mdso_output_strerror(erri);
bfa928
7d3ce7
	epath = erri->euctx
7d3ce7
		? *erri->euctx->path
7d3ce7
		: erri->eunit;
7d3ce7
7d3ce7
	if (epath && !(erri->eflags & MDSO_ERROR_NESTED))
7d3ce7
		if (fprintf(stderr,"%s: [%s] '%s':\n",
7d3ce7
				dctx->program,
7d3ce7
				mdso_output_unit_header(erri),
7d3ce7
				epath) < 0)
7d3ce7
			return -1;
7d3ce7
bfa928
	if (fprintf(stderr,"%s: %s %s(), line %d%s%s.\n",
bfa928
			dctx->program,
bfa928
			mdso_output_error_header(erri),
1d4a9e
			erri->efunction,
1d4a9e
			erri->eline,
bfa928
			strlen(errdesc) ? ": " : "",
bfa928
			errdesc) < 0)
bfa928
		return -1;
bfa928
bfa928
	return fflush(stderr);
bfa928
}
bfa928
bfa928
static int mdso_output_error_record_annotated(
bfa928
	const struct mdso_driver_ctx *	dctx,
bfa928
	const struct mdso_error_info *	erri)
bfa928
{
7d3ce7
	const char * epath;
bfa928
	const char * errdesc = mdso_output_strerror(erri);
bfa928
7d3ce7
	epath = erri->euctx
7d3ce7
		? *erri->euctx->path
7d3ce7
		: erri->eunit;
7d3ce7
7d3ce7
	if (epath && !(erri->eflags & MDSO_ERROR_NESTED))
7d3ce7
		if (fprintf(
7d3ce7
				stderr,
7d3ce7
				"%s%s%s:%s %s[%s]%s %s%s'%s'%s:\n",
7d3ce7
7d3ce7
				aclr_bold,aclr_magenta,
7d3ce7
				dctx->program,
7d3ce7
				aclr_reset,
7d3ce7
7d3ce7
				aclr_bold,
7d3ce7
				mdso_output_unit_header(erri),
7d3ce7
				aclr_reset,
7d3ce7
7d3ce7
				aclr_bold,aclr_red,
7d3ce7
				epath,
7d3ce7
				aclr_reset) < 0)
7d3ce7
			return -1;
7d3ce7
bfa928
	if (fprintf(
bfa928
			stderr,
bfa928
			"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n",
bfa928
bfa928
			aclr_bold,aclr_magenta,
bfa928
			dctx->program,
bfa928
			aclr_reset,
bfa928
bfa928
			aclr_bold,
bfa928
			mdso_output_error_header(erri),
bfa928
			aclr_reset,
bfa928
bfa928
			aclr_bold,aclr_blue,
1d4a9e
			erri->efunction,
bfa928
			aclr_reset,
bfa928
bfa928
			aclr_bold,aclr_green,
1d4a9e
			erri->eline,
bfa928
			aclr_reset,
bfa928
			strlen(errdesc) ? ": " : "",
bfa928
bfa928
			aclr_bold,
bfa928
			mdso_output_strerror(erri),
bfa928
			aclr_reset) < 0)
bfa928
		return -1;
bfa928
bfa928
	return fflush(stderr);
bfa928
}
bfa928
bfa928
int mdso_output_error_record(
bfa928
	const struct mdso_driver_ctx *	dctx,
bfa928
	const struct mdso_error_info *	erri)
bfa928
{
bfa928
	if (dctx->cctx->drvflags & MDSO_DRIVER_ANNOTATE_NEVER)
bfa928
		return mdso_output_error_record_plain(dctx,erri);
bfa928
bfa928
	else if (dctx->cctx->drvflags & MDSO_DRIVER_ANNOTATE_ALWAYS)
bfa928
		return mdso_output_error_record_annotated(dctx,erri);
bfa928
bfa928
	else if (isatty(STDERR_FILENO))
bfa928
		return mdso_output_error_record_annotated(dctx,erri);
bfa928
bfa928
	else
bfa928
		return mdso_output_error_record_plain(dctx,erri);
bfa928
}
bfa928
bfa928
static int mdso_output_error_vector_plain(const struct mdso_driver_ctx * dctx)
bfa928
{
bfa928
	struct mdso_error_info ** perr;
bfa928
bfa928
	for (perr=dctx->errv; *perr; perr++)
bfa928
		if (mdso_output_error_record_plain(dctx,*perr))
bfa928
			return -1;
bfa928
bfa928
	return 0;
bfa928
}
bfa928
bfa928
static int mdso_output_error_vector_annotated(const struct mdso_driver_ctx * dctx)
bfa928
{
bfa928
	struct mdso_error_info ** perr;
bfa928
bfa928
	for (perr=dctx->errv; *perr; perr++)
bfa928
		if (mdso_output_error_record_annotated(dctx,*perr))
bfa928
			return -1;
bfa928
bfa928
	return 0;
bfa928
}
bfa928
bfa928
int mdso_output_error_vector(const struct mdso_driver_ctx * dctx)
bfa928
{
bfa928
	if (dctx->cctx->drvflags & MDSO_DRIVER_ANNOTATE_NEVER)
bfa928
		return mdso_output_error_vector_plain(dctx);
bfa928
bfa928
	else if (dctx->cctx->drvflags & MDSO_DRIVER_ANNOTATE_ALWAYS)
bfa928
		return mdso_output_error_vector_annotated(dctx);
bfa928
bfa928
	else if (isatty(STDERR_FILENO))
bfa928
		return mdso_output_error_vector_annotated(dctx);
bfa928
bfa928
	else
bfa928
		return mdso_output_error_vector_plain(dctx);
bfa928
}