1 /* Error handling in libdwfl.
2 Copyright (C) 2005-2015 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
43 /* The error number. */
44 static __thread int global_error;
50 int result = global_error;
51 global_error = DWFL_E_NOERROR;
59 #define DWFL_ERROR(name, text) char msg_##name[sizeof text];
66 struct msgtable table;
68 #define DWFL_ERROR(name, text) + sizeof text
76 #define DWFL_ERROR(name, text) text,
81 #define msgstr (msgtable.strings)
83 static const uint_fast16_t msgidx[] =
85 #define DWFL_ERROR(name, text) \
86 [DWFL_E_##name] = offsetof (struct msgtable, msg_##name),
90 #define nmsgidx (sizeof msgidx / sizeof msgidx[0])
94 canonicalize (Dwfl_Error error)
102 if ((value &~ 0xffff) != 0)
104 assert (value < nmsgidx);
107 value = DWFL_E (ERRNO, errno);
110 value = DWFL_E (LIBELF, elf_errno ());
113 value = DWFL_E (LIBDW, INTUSE(dwarf_errno) ());
117 value = DWFL_E (LIBEBL, ebl_errno ());
127 __libdwfl_canon_error (Dwfl_Error error)
129 return canonicalize (error);
134 __libdwfl_seterrno (Dwfl_Error error)
136 global_error = canonicalize (error);
141 dwfl_errmsg (int error)
143 if (error == 0 || error == -1)
145 int last_error = global_error;
147 if (error == 0 && last_error == 0)
151 global_error = DWFL_E_NOERROR;
154 switch (error &~ 0xffff)
156 case OTHER_ERROR (ERRNO):
157 return strerror_r (error & 0xffff, "bad", 0);
158 case OTHER_ERROR (LIBELF):
159 return elf_errmsg (error & 0xffff);
160 case OTHER_ERROR (LIBDW):
161 return INTUSE(dwarf_errmsg) (error & 0xffff);
163 case OTHER_ERROR (LIBEBL):
164 return ebl_errmsg (error & 0xffff);
168 return _(&msgstr[msgidx[(unsigned int) error < nmsgidx
169 ? error : DWFL_E_UNKNOWN_ERROR]]);