Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Error handling in libdwfl. |
Roland McGrath | ed431dd | 2010-05-06 00:52:51 -0700 | [diff] [blame] | 2 | Copyright (C) 2005-2010 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
| 11 | |
| 12 | or |
| 13 | |
| 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 |
| 17 | |
| 18 | or both in parallel, as here. |
| 19 | |
| 20 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
| 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | # include <config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <assert.h> |
| 34 | #include <libintl.h> |
| 35 | #include <stdbool.h> |
| 36 | #include <stdint.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <errno.h> |
| 39 | |
| 40 | #include "libdwflP.h" |
| 41 | |
| 42 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 43 | /* The error number. */ |
| 44 | static __thread int global_error; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | int |
| 48 | dwfl_errno (void) |
| 49 | { |
Ulrich Drepper | 7e678fa | 2009-01-10 18:02:05 -0800 | [diff] [blame] | 50 | int result = global_error; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 51 | global_error = DWFL_E_NOERROR; |
| 52 | return result; |
| 53 | } |
Roland McGrath | ed431dd | 2010-05-06 00:52:51 -0700 | [diff] [blame] | 54 | INTDEF (dwfl_errno) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | static const struct msgtable |
| 58 | { |
| 59 | #define DWFL_ERROR(name, text) char msg_##name[sizeof text]; |
| 60 | DWFL_ERRORS |
| 61 | #undef DWFL_ERROR |
| 62 | } msgtable = |
| 63 | { |
| 64 | #define DWFL_ERROR(name, text) text, |
| 65 | DWFL_ERRORS |
| 66 | #undef DWFL_ERROR |
| 67 | }; |
| 68 | #define msgstr (&msgtable.msg_NOERROR[0]) |
| 69 | |
| 70 | static const uint_fast16_t msgidx[] = |
| 71 | { |
| 72 | #define DWFL_ERROR(name, text) \ |
| 73 | [DWFL_E_##name] = offsetof (struct msgtable, msg_##name), |
| 74 | DWFL_ERRORS |
| 75 | #undef DWFL_ERROR |
| 76 | }; |
| 77 | #define nmsgidx (sizeof msgidx / sizeof msgidx[0]) |
| 78 | |
| 79 | |
| 80 | static inline int |
| 81 | canonicalize (Dwfl_Error error) |
| 82 | { |
| 83 | unsigned int value; |
| 84 | |
| 85 | switch (error) |
| 86 | { |
| 87 | default: |
| 88 | value = error; |
| 89 | if ((value &~ 0xffff) != 0) |
| 90 | break; |
| 91 | assert (value < nmsgidx); |
| 92 | break; |
| 93 | case DWFL_E_ERRNO: |
| 94 | value = DWFL_E (ERRNO, errno); |
| 95 | break; |
| 96 | case DWFL_E_LIBELF: |
| 97 | value = DWFL_E (LIBELF, elf_errno ()); |
| 98 | break; |
| 99 | case DWFL_E_LIBDW: |
Roland McGrath | 4959bf8 | 2005-08-09 10:31:08 +0000 | [diff] [blame] | 100 | value = DWFL_E (LIBDW, INTUSE(dwarf_errno) ()); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 101 | break; |
| 102 | #if 0 |
| 103 | DWFL_E_LIBEBL: |
| 104 | value = DWFL_E (LIBEBL, ebl_errno ()); |
| 105 | break; |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | return value; |
| 110 | } |
| 111 | |
| 112 | int |
Ulrich Drepper | 077c65f | 2006-07-12 19:54:51 +0000 | [diff] [blame] | 113 | internal_function |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 114 | __libdwfl_canon_error (Dwfl_Error error) |
| 115 | { |
| 116 | return canonicalize (error); |
| 117 | } |
| 118 | |
| 119 | void |
Ulrich Drepper | 077c65f | 2006-07-12 19:54:51 +0000 | [diff] [blame] | 120 | internal_function |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 121 | __libdwfl_seterrno (Dwfl_Error error) |
| 122 | { |
Ulrich Drepper | 7e678fa | 2009-01-10 18:02:05 -0800 | [diff] [blame] | 123 | global_error = canonicalize (error); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | const char * |
| 128 | dwfl_errmsg (error) |
| 129 | int error; |
| 130 | { |
| 131 | if (error == 0 || error == -1) |
| 132 | { |
Ulrich Drepper | 7e678fa | 2009-01-10 18:02:05 -0800 | [diff] [blame] | 133 | int last_error = global_error; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 134 | |
| 135 | if (error == 0 && last_error == 0) |
| 136 | return NULL; |
| 137 | |
| 138 | error = last_error; |
| 139 | global_error = DWFL_E_NOERROR; |
| 140 | } |
| 141 | |
| 142 | switch (error &~ 0xffff) |
| 143 | { |
| 144 | case OTHER_ERROR (ERRNO): |
| 145 | return strerror_r (error & 0xffff, "bad", 0); |
| 146 | case OTHER_ERROR (LIBELF): |
| 147 | return elf_errmsg (error & 0xffff); |
| 148 | case OTHER_ERROR (LIBDW): |
Roland McGrath | 4959bf8 | 2005-08-09 10:31:08 +0000 | [diff] [blame] | 149 | return INTUSE(dwarf_errmsg) (error & 0xffff); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 150 | #if 0 |
| 151 | case OTHER_ERROR (LIBEBL): |
| 152 | return ebl_errmsg (error & 0xffff); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | return _(&msgstr[msgidx[(unsigned int) error < nmsgidx |
| 157 | ? error : DWFL_E_UNKNOWN_ERROR]]); |
| 158 | } |
| 159 | INTDEF (dwfl_errmsg) |