2019-04-24 05:05:52 +08:00
|
|
|
/* Error table.
|
2020-01-01 15:57:01 +08:00
|
|
|
Copyright (C) 2019-2020 Free Software Foundation, Inc.
|
2019-04-24 05:05:52 +08:00
|
|
|
|
|
|
|
This file is part of libctf.
|
|
|
|
|
|
|
|
libctf is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; see the file COPYING. If not see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <ctf-impl.h>
|
libctf: restructure error handling to reduce relocations
Jose Marchesi noted that the traditional-Unix error array in ctf-error.c
introduces one reloc per error to initialize the array: 58 so far. We
can reduce this to zero using an array of carefully-sized individual
members which is used to construct a string table, that is then
referenced by the lookup functions: but doing this automatically is a
pain.
Bruno Haible wrote suitable code years ago: I got permission to reuse it
(Bruno says "... which I hereby put in the public domain"); I modified
it a tiny bit (similarly to what Ulrich Drepper did in the dsohowto
text, but I redid it from scratch), commented it up a bit, and shifted
the error table into that form, migrating it into the new file
ctf-error.h.
This has the advantage that it spotted both typos in the text of the
errors in the comments in ctf-api.h and typos in the error defines in
the comments in ctf-error.c, and places where the two were simply not
in sync. All are now fixed.
One new constant exists in ctf-api.h: CTF_NERR, since the old method of
working out the number of errors in ctf-error.c was no longer usable,
and it seems that the number of CTF errors is something users might
reasonably want as well. It should be pretty easy to keep up to date as
new errors are introduced.
include/
* ctf-api.h (ECTF_*): Improve comments.
(ECTF_NERR): New.
libctf/
* ctf-error.c: Include <stddef.h>, for offsetof.
(_ctf_errlist): Migrate to...
(_ctf_errlist_t): ... this.
(_ctf_erridx): New, indexes into _ctf_errlist_t.
(_ctf_nerr): Remove.
(ctf_errmsg): Adjust accordingly.
* Makefile.am (BUILT_SOURCES): Note...
(ctf-error.h): ... this new rule.
* Makefile.in: Regenerate.
* mkerrors.sed: New, process ctf-api.h to generate ctf-error.h.
* .gitignore: New, ignore ctf-error.h.
2020-06-03 02:07:08 +08:00
|
|
|
#include <stddef.h>
|
2020-06-05 01:07:38 +08:00
|
|
|
#include <string.h>
|
2019-04-24 05:05:52 +08:00
|
|
|
|
libctf: restructure error handling to reduce relocations
Jose Marchesi noted that the traditional-Unix error array in ctf-error.c
introduces one reloc per error to initialize the array: 58 so far. We
can reduce this to zero using an array of carefully-sized individual
members which is used to construct a string table, that is then
referenced by the lookup functions: but doing this automatically is a
pain.
Bruno Haible wrote suitable code years ago: I got permission to reuse it
(Bruno says "... which I hereby put in the public domain"); I modified
it a tiny bit (similarly to what Ulrich Drepper did in the dsohowto
text, but I redid it from scratch), commented it up a bit, and shifted
the error table into that form, migrating it into the new file
ctf-error.h.
This has the advantage that it spotted both typos in the text of the
errors in the comments in ctf-api.h and typos in the error defines in
the comments in ctf-error.c, and places where the two were simply not
in sync. All are now fixed.
One new constant exists in ctf-api.h: CTF_NERR, since the old method of
working out the number of errors in ctf-error.c was no longer usable,
and it seems that the number of CTF errors is something users might
reasonably want as well. It should be pretty easy to keep up to date as
new errors are introduced.
include/
* ctf-api.h (ECTF_*): Improve comments.
(ECTF_NERR): New.
libctf/
* ctf-error.c: Include <stddef.h>, for offsetof.
(_ctf_errlist): Migrate to...
(_ctf_errlist_t): ... this.
(_ctf_erridx): New, indexes into _ctf_errlist_t.
(_ctf_nerr): Remove.
(ctf_errmsg): Adjust accordingly.
* Makefile.am (BUILT_SOURCES): Note...
(ctf-error.h): ... this new rule.
* Makefile.in: Regenerate.
* mkerrors.sed: New, process ctf-api.h to generate ctf-error.h.
* .gitignore: New, ignore ctf-error.h.
2020-06-03 02:07:08 +08:00
|
|
|
/* This construct is due to Bruno Haible: much thanks. */
|
|
|
|
|
|
|
|
/* Give each structure member a unique name. The name does not matter, so we
|
|
|
|
use the line number in ctf-error.h to uniquify them. */
|
|
|
|
|
|
|
|
#define ERRSTRFIELD(line) ERRSTRFIELD1 (line)
|
|
|
|
#define ERRSTRFIELD1(line) ctf_errstr##line
|
|
|
|
|
|
|
|
/* The error message strings, each in a unique structure member precisely big
|
|
|
|
enough for that error, plus a str member to access them all as a string
|
|
|
|
table. */
|
|
|
|
|
|
|
|
static const union _ctf_errlist_t
|
|
|
|
{
|
|
|
|
__extension__ struct
|
|
|
|
{
|
|
|
|
#define _CTF_STR(n, s) char ERRSTRFIELD (__LINE__) [sizeof (s)];
|
|
|
|
#include "ctf-error.h"
|
|
|
|
#undef _CTF_STR
|
|
|
|
};
|
|
|
|
char str[1];
|
|
|
|
} _ctf_errlist =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#define _CTF_STR(n, s) s,
|
|
|
|
#include "ctf-error.h"
|
|
|
|
#undef _CTF_STR
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Offsets to each member in the string table, computed using offsetof. */
|
|
|
|
|
|
|
|
static const unsigned int _ctf_erridx[] =
|
|
|
|
{
|
|
|
|
#define _CTF_STR(n, s) [n - ECTF_BASE] = offsetof (union _ctf_errlist_t, ERRSTRFIELD (__LINE__)),
|
|
|
|
#include "ctf-error.h"
|
|
|
|
#undef _CTF_STR
|
|
|
|
};
|
2019-04-24 05:05:52 +08:00
|
|
|
|
|
|
|
const char *
|
|
|
|
ctf_errmsg (int error)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
libctf: restructure error handling to reduce relocations
Jose Marchesi noted that the traditional-Unix error array in ctf-error.c
introduces one reloc per error to initialize the array: 58 so far. We
can reduce this to zero using an array of carefully-sized individual
members which is used to construct a string table, that is then
referenced by the lookup functions: but doing this automatically is a
pain.
Bruno Haible wrote suitable code years ago: I got permission to reuse it
(Bruno says "... which I hereby put in the public domain"); I modified
it a tiny bit (similarly to what Ulrich Drepper did in the dsohowto
text, but I redid it from scratch), commented it up a bit, and shifted
the error table into that form, migrating it into the new file
ctf-error.h.
This has the advantage that it spotted both typos in the text of the
errors in the comments in ctf-api.h and typos in the error defines in
the comments in ctf-error.c, and places where the two were simply not
in sync. All are now fixed.
One new constant exists in ctf-api.h: CTF_NERR, since the old method of
working out the number of errors in ctf-error.c was no longer usable,
and it seems that the number of CTF errors is something users might
reasonably want as well. It should be pretty easy to keep up to date as
new errors are introduced.
include/
* ctf-api.h (ECTF_*): Improve comments.
(ECTF_NERR): New.
libctf/
* ctf-error.c: Include <stddef.h>, for offsetof.
(_ctf_errlist): Migrate to...
(_ctf_errlist_t): ... this.
(_ctf_erridx): New, indexes into _ctf_errlist_t.
(_ctf_nerr): Remove.
(ctf_errmsg): Adjust accordingly.
* Makefile.am (BUILT_SOURCES): Note...
(ctf-error.h): ... this new rule.
* Makefile.in: Regenerate.
* mkerrors.sed: New, process ctf-api.h to generate ctf-error.h.
* .gitignore: New, ignore ctf-error.h.
2020-06-03 02:07:08 +08:00
|
|
|
if (error >= ECTF_BASE && (error - ECTF_BASE) < ECTF_NERR)
|
|
|
|
str = _ctf_errlist.str + _ctf_erridx[error - ECTF_BASE];
|
2019-04-24 05:05:52 +08:00
|
|
|
else
|
2020-06-05 01:07:38 +08:00
|
|
|
str = (const char *) strerror (error);
|
2019-04-24 05:05:52 +08:00
|
|
|
|
|
|
|
return (str ? str : "Unknown error");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ctf_errno (ctf_file_t * fp)
|
|
|
|
{
|
|
|
|
return fp->ctf_errno;
|
|
|
|
}
|