File: dwarf_error.c

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (224 lines) | stat: -rw-r--r-- 6,761 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*

  Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  Portions Copyright (C) 2008-2020 David Anderson.  All Rights Reserved.

  This program is free software; you can redistribute it
  and/or modify it under the terms of version 2.1 of the
  GNU Lesser General Public License as published by the Free
  Software Foundation.

  This program is distributed in the hope that it would be
  useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.

  Further, this software is distributed without any warranty
  that it is free of the rightful claim of any third person
  regarding infringement or the like.  Any license provided
  herein, whether implied or otherwise, applies only to this
  software file.  Patent licenses, if any, provided herein
  do not apply to combinations of this program with other
  software, or any other product whatsoever.

  You should have received a copy of the GNU Lesser General
  Public License along with this program; if not, write the
  Free Software Foundation, Inc., 51 Franklin Street - Fifth
  Floor, Boston MA 02110-1301, USA.

*/

#include "config.h"
#include <stdio.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h> /* open(), off_t, size_t, ssize_t */
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#include "dwarf_incl.h"
#include "dwarf_alloc.h"
#include "dwarfstring.h"
#include "dwarf_error.h"

#undef DEBUG

/* Array to hold string representation of errors. Any time a
   define is added to the list in libdwarf.h, a string should be
   added to this Array
*/
#include "dwarf_errmsg_list.h"

/*  This function performs error handling as described in the
    libdwarf consumer document section 3.  Dbg is the Dwarf_debug
    structure being processed.  Error is a pointer to the pointer
    to the error descriptor that will be returned.  Errval is an
    error code listed in dwarf_error.h.

    If the malloc arena is exhausted we return a pointer to
    a special static error record.  This special singleton
    is mostly ignored by dwarf_dealloc().
    Users should not be storing Dwarf_Error pointers
    for long so this singleton is only going to cause
    confusion when callers try to save an out-of-memory
    Dwarf_Error pointer.
    The _dwarf_failsafe_error is intended to
    be an improvement over an abort() call.
    The failsafe means we will not abort due to
    a Dwarf_Error struct creation.
*/

/*  The user provides an explanatory string, the error
    number itself explains little.
    This prepends DW_DLE_USER_DECLARED_ERROR to the
    caller-provided string.
    New in April, 2020 .  Used by dwarfdump in a few
    circumstances. */
void
dwarf_error_creation(Dwarf_Debug dbg,
    Dwarf_Error *err,
    char *errmsg)
{
    dwarfstring m;
    if(!dbg) {
        return;
    }
    dwarfstring_constructor(&m);
    dwarfstring_append(&m,"DW_DLE_USER_DECLARED_ERROR: ");
    dwarfstring_append(&m,errmsg);
    _dwarf_error_string(dbg,err,
        DW_DLE_USER_DECLARED_ERROR,
        dwarfstring_string(&m));
    dwarfstring_destructor(&m);
}


void
_dwarf_error(Dwarf_Debug dbg, Dwarf_Error * error,
    Dwarf_Signed errval)
{
    _dwarf_error_string(dbg,error,errval,0);
}
void
_dwarf_error_string(Dwarf_Debug dbg, Dwarf_Error * error,
    Dwarf_Signed errval,char *msg)
{
    Dwarf_Error errptr;

    /*  Allow NULL dbg on entry, since sometimes that can happen and we
        want to report the upper-level error, not this one. */
    if (error) {
        /*  If dbg is NULL, use the alternate error struct. However,
            this will overwrite the earlier error. */
        if (dbg) {
            errptr =
                (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
            if (!errptr) {
                errptr = &_dwarf_failsafe_error;
                errptr->er_static_alloc = DE_STATIC;
            } else {
                errptr->er_static_alloc = DE_STANDARD;
            }
        } else {
            /*  We have no dbg to work with. dwarf_init
                failed. We hack
                up a special area. */
            errptr = _dwarf_special_no_dbg_error_malloc();
            if (!errptr) {
                errptr = &_dwarf_failsafe_error;
                errptr->er_static_alloc = DE_STATIC;
#ifdef DEBUG
                printf("libdwarfdetector no dbg, "
                    "using DE_STATIC alloc, addr"
                    " 0x%lx line %d %s\n",
                    (unsigned long)errptr,
                    __LINE__,__FILE__);
#endif /* DEBUG */


            } else {
                errptr->er_static_alloc = DE_MALLOC;
#ifdef DEBUG
                printf("libdwarfdetector no dbg, "
                    "static DE_MALLOC alloc, addr"
                    " 0x%lx line %d %s\n",
                    (unsigned long)errptr,
                    __LINE__,__FILE__);
#endif /* DEBUG */
            }
        }
        errptr->er_errval = errval;
        if (msg) {
            dwarfstring *em = 0;

#ifdef DEBUG
            printf("libdwarfdetector ALLOC creating error string"
                " %s errval %ld errptr 0x%lx \n",
                msg,(long)errval,(unsigned long)errptr);
#endif
            em = (dwarfstring *)calloc(1,sizeof(dwarfstring));
            if (em) {
                dwarfstring_constructor(em);
                dwarfstring_append(em,msg);
                errptr->er_msg = (void*)em;
            }
        }
        *error = errptr;
        return;
    }

    if (dbg  && dbg->de_errhand != NULL) {
        errptr = (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
        if (errptr == NULL) {
            errptr = &_dwarf_failsafe_error;
            errptr->er_static_alloc = DE_STATIC;
        }
        errptr->er_errval = errval;
        dbg->de_errhand(errptr, dbg->de_errarg);
        return;
    }
    fflush(stdout);
    fprintf(stdout,
        "\nNow abort() in libdwarf. "
        "No error argument or handler available.\n");
    fflush(stdout);
    abort();
}


Dwarf_Unsigned
dwarf_errno(Dwarf_Error error)
{
    if (!error) {
        return (0);
    }
    return (error->er_errval);
}

char*
dwarf_errmsg_by_number(Dwarf_Unsigned errornum )
{
    if (errornum >=
        (Dwarf_Signed)(sizeof(_dwarf_errmsgs) / sizeof(char *))) {
        return "Dwarf_Error value out of range";
    }
    return ((char *) _dwarf_errmsgs[errornum]);
}


/*
*/
char *
dwarf_errmsg(Dwarf_Error error)
{
    if (!error) {
        return "Dwarf_Error is NULL";
    }
    if (error->er_msg) {
        return dwarfstring_string(error->er_msg);
    }
    return  dwarf_errmsg_by_number(error->er_errval);
}