|
|
5874a9 |
/**************************************************************/
|
|
|
5874a9 |
/* tpax: a topological pax implementation */
|
|
|
bef28d |
/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */
|
|
|
5874a9 |
/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */
|
|
|
5874a9 |
/**************************************************************/
|
|
|
88751e |
|
|
|
88751e |
#include <stdio.h>
|
|
|
88751e |
#include <string.h>
|
|
|
88751e |
#include <errno.h>
|
|
|
88751e |
#include <unistd.h>
|
|
|
88751e |
#include <tpax/tpax.h>
|
|
|
88751e |
|
|
|
88751e |
#include "tpax_driver_impl.h"
|
|
|
88751e |
#include "tpax_dprintf_impl.h"
|
|
|
88751e |
|
|
|
88751e |
static const char aclr_reset[] = "\x1b[0m";
|
|
|
88751e |
static const char aclr_bold[] = "\x1b[1m";
|
|
|
88751e |
|
|
|
88751e |
static const char aclr_red[] = "\x1b[31m";
|
|
|
88751e |
static const char aclr_green[] = "\x1b[32m";
|
|
|
88751e |
static const char aclr_blue[] = "\x1b[34m";
|
|
|
88751e |
static const char aclr_magenta[] = "\x1b[35m";
|
|
|
88751e |
|
|
|
88751e |
static const char * const tpax_error_strings[TPAX_ERR_CAP] = {
|
|
|
88751e |
[TPAX_ERR_FLOW_ERROR] = "flow error: unexpected condition or other",
|
|
|
88751e |
[TPAX_ERR_FLEE_ERROR] = "flees and bugs and cats and mice",
|
|
|
88751e |
[TPAX_ERR_NULL_CONTEXT] = "null driver or unit context",
|
|
|
88751e |
[TPAX_ERR_BAD_DATA] = "corrupt or wrong daata",
|
|
|
e0fd2a |
[TPAX_ERR_FILE_CHANGED] = "file has changed",
|
|
|
e0fd2a |
[TPAX_ERR_REGION_SIZE] = "file too large for a memory snapshot",
|
|
|
88751e |
};
|
|
|
88751e |
|
|
|
88751e |
static const char * tpax_output_error_header(const struct tpax_error_info * erri)
|
|
|
88751e |
{
|
|
|
88751e |
if (erri->eflags & TPAX_ERROR_CHILD)
|
|
|
88751e |
return "exec error upon";
|
|
|
88751e |
|
|
|
88751e |
else if (erri->eflags & TPAX_ERROR_TOP_LEVEL)
|
|
|
88751e |
return "error logged in";
|
|
|
88751e |
|
|
|
88751e |
else if (erri->eflags & TPAX_ERROR_NESTED)
|
|
|
88751e |
return "< returned to >";
|
|
|
88751e |
|
|
|
88751e |
else
|
|
|
88751e |
return "distorted state";
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
static const char * tpax_output_unit_header(const struct tpax_error_info * erri)
|
|
|
88751e |
{
|
|
|
88751e |
if (!(erri->eflags & TPAX_ERROR_CUSTOM))
|
|
|
88751e |
return "while opening";
|
|
|
88751e |
|
|
|
88751e |
else
|
|
|
88751e |
return "while parsing";
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
static const char * tpax_output_strerror(
|
|
|
88751e |
const struct tpax_error_info * erri,
|
|
|
88751e |
char (*errbuf)[256])
|
|
|
88751e |
{
|
|
|
88751e |
if (erri->eflags & TPAX_ERROR_CUSTOM)
|
|
|
88751e |
return ((erri->elibcode < 0) || (erri->elibcode >= TPAX_ERR_CAP))
|
|
|
88751e |
? "internal error: please report to the maintainer"
|
|
|
88751e |
: tpax_error_strings[erri->elibcode];
|
|
|
88751e |
|
|
|
88751e |
else if (erri->eflags & TPAX_ERROR_NESTED)
|
|
|
88751e |
return "";
|
|
|
88751e |
|
|
|
88751e |
else if (erri->eflags & TPAX_ERROR_CHILD)
|
|
|
88751e |
return "(see child process error messages)";
|
|
|
88751e |
|
|
|
88751e |
else if (erri->esyscode == ENOBUFS)
|
|
|
88751e |
return "input error: string length exceeds buffer size";
|
|
|
88751e |
|
|
|
88751e |
else
|
|
|
88751e |
return strerror_r(erri->esyscode,*errbuf,sizeof(*errbuf))
|
|
|
88751e |
? "internal error: strerror_r(3) call failed"
|
|
|
88751e |
: *errbuf;
|
|
|
88751e |
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
static int tpax_output_error_record_plain(
|
|
|
88751e |
const struct tpax_driver_ctx * dctx,
|
|
|
88751e |
const struct tpax_error_info * erri)
|
|
|
88751e |
{
|
|
|
88751e |
const char * epath;
|
|
|
88751e |
char errbuf[256];
|
|
|
88751e |
|
|
|
88751e |
int fderr = tpax_driver_fderr(dctx);
|
|
|
88751e |
const char * errdesc = tpax_output_strerror(erri,&errbuf);
|
|
|
88751e |
|
|
|
88751e |
epath = erri->euctx
|
|
|
88751e |
? *erri->euctx->path
|
|
|
88751e |
: erri->eunit;
|
|
|
88751e |
|
|
|
88751e |
if (epath && !(erri->eflags & TPAX_ERROR_NESTED))
|
|
|
88751e |
if (tpax_dprintf(
|
|
|
88751e |
fderr,
|
|
|
88751e |
"%s: [%s] '%s':\n",
|
|
|
88751e |
dctx->program,
|
|
|
88751e |
tpax_output_unit_header(erri),
|
|
|
88751e |
epath) < 0)
|
|
|
88751e |
return -1;
|
|
|
88751e |
|
|
|
88751e |
if (tpax_dprintf(
|
|
|
88751e |
fderr,
|
|
|
88751e |
"%s: %s %s(), line %d%s%s.\n",
|
|
|
88751e |
dctx->program,
|
|
|
88751e |
tpax_output_error_header(erri),
|
|
|
88751e |
erri->efunction,
|
|
|
88751e |
erri->eline,
|
|
|
88751e |
strlen(errdesc) ? ": " : "",
|
|
|
88751e |
errdesc) < 0)
|
|
|
88751e |
return -1;
|
|
|
88751e |
|
|
|
88751e |
return 0;
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
static int tpax_output_error_record_annotated(
|
|
|
88751e |
const struct tpax_driver_ctx * dctx,
|
|
|
88751e |
const struct tpax_error_info * erri)
|
|
|
88751e |
{
|
|
|
88751e |
const char * epath;
|
|
|
88751e |
char errbuf[256];
|
|
|
88751e |
|
|
|
88751e |
int fderr = tpax_driver_fderr(dctx);
|
|
|
88751e |
const char * errdesc = tpax_output_strerror(erri,&errbuf);
|
|
|
88751e |
|
|
|
88751e |
epath = erri->euctx
|
|
|
88751e |
? *erri->euctx->path
|
|
|
88751e |
: erri->eunit;
|
|
|
88751e |
|
|
|
88751e |
if (epath && !(erri->eflags & TPAX_ERROR_NESTED))
|
|
|
88751e |
if (tpax_dprintf(
|
|
|
88751e |
fderr,
|
|
|
88751e |
"%s%s%s:%s %s[%s]%s %s%s'%s'%s:\n",
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,aclr_magenta,
|
|
|
88751e |
dctx->program,
|
|
|
88751e |
aclr_reset,
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,
|
|
|
88751e |
tpax_output_unit_header(erri),
|
|
|
88751e |
aclr_reset,
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,aclr_red,
|
|
|
88751e |
epath,
|
|
|
88751e |
aclr_reset) < 0)
|
|
|
88751e |
return -1;
|
|
|
88751e |
|
|
|
88751e |
if (tpax_dprintf(
|
|
|
88751e |
fderr,
|
|
|
88751e |
"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n",
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,aclr_magenta,
|
|
|
88751e |
dctx->program,
|
|
|
88751e |
aclr_reset,
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,
|
|
|
88751e |
tpax_output_error_header(erri),
|
|
|
88751e |
aclr_reset,
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,aclr_blue,
|
|
|
88751e |
erri->efunction,
|
|
|
88751e |
aclr_reset,
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,aclr_green,
|
|
|
88751e |
erri->eline,
|
|
|
88751e |
aclr_reset,
|
|
|
88751e |
strlen(errdesc) ? ": " : "",
|
|
|
88751e |
|
|
|
88751e |
aclr_bold,
|
|
|
88751e |
errdesc,
|
|
|
88751e |
aclr_reset) < 0)
|
|
|
88751e |
return -1;
|
|
|
88751e |
|
|
|
88751e |
return 0;
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
int tpax_output_error_record(
|
|
|
88751e |
const struct tpax_driver_ctx * dctx,
|
|
|
88751e |
const struct tpax_error_info * erri)
|
|
|
88751e |
{
|
|
|
88751e |
if (dctx->cctx->drvflags & TPAX_DRIVER_ANNOTATE_NEVER)
|
|
|
88751e |
return tpax_output_error_record_plain(dctx,erri);
|
|
|
88751e |
|
|
|
88751e |
else if (dctx->cctx->drvflags & TPAX_DRIVER_ANNOTATE_ALWAYS)
|
|
|
88751e |
return tpax_output_error_record_annotated(dctx,erri);
|
|
|
88751e |
|
|
|
88751e |
else if (isatty(tpax_driver_fderr(dctx)))
|
|
|
88751e |
return tpax_output_error_record_annotated(dctx,erri);
|
|
|
88751e |
|
|
|
88751e |
else
|
|
|
88751e |
return tpax_output_error_record_plain(dctx,erri);
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
static int tpax_output_error_vector_plain(const struct tpax_driver_ctx * dctx)
|
|
|
88751e |
{
|
|
|
88751e |
struct tpax_error_info ** perr;
|
|
|
88751e |
|
|
|
88751e |
for (perr=dctx->errv; *perr; perr++)
|
|
|
88751e |
if (tpax_output_error_record_plain(dctx,*perr))
|
|
|
88751e |
return -1;
|
|
|
88751e |
|
|
|
88751e |
return 0;
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
static int tpax_output_error_vector_annotated(const struct tpax_driver_ctx * dctx)
|
|
|
88751e |
{
|
|
|
88751e |
struct tpax_error_info ** perr;
|
|
|
88751e |
|
|
|
88751e |
for (perr=dctx->errv; *perr; perr++)
|
|
|
88751e |
if (tpax_output_error_record_annotated(dctx,*perr))
|
|
|
88751e |
return -1;
|
|
|
88751e |
|
|
|
88751e |
return 0;
|
|
|
88751e |
}
|
|
|
88751e |
|
|
|
88751e |
int tpax_output_error_vector(const struct tpax_driver_ctx * dctx)
|
|
|
88751e |
{
|
|
|
88751e |
if (dctx->cctx->drvflags & TPAX_DRIVER_ANNOTATE_NEVER)
|
|
|
88751e |
return tpax_output_error_vector_plain(dctx);
|
|
|
88751e |
|
|
|
88751e |
else if (dctx->cctx->drvflags & TPAX_DRIVER_ANNOTATE_ALWAYS)
|
|
|
88751e |
return tpax_output_error_vector_annotated(dctx);
|
|
|
88751e |
|
|
|
88751e |
else if (isatty(tpax_driver_fderr(dctx)))
|
|
|
88751e |
return tpax_output_error_vector_annotated(dctx);
|
|
|
88751e |
|
|
|
88751e |
else
|
|
|
88751e |
return tpax_output_error_vector_plain(dctx);
|
|
|
88751e |
}
|