xref: /illumos-gate/usr/src/lib/libdwarf/common/pro_error.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
1 /*
2 
3   Copyright (C) 2000,2002,2004 Silicon Graphics, Inc.  All Rights Reserved.
4   Portions Copyright 2011 David Anderson. All Rights Reserved.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of version 2.1 of the GNU Lesser General Public License
8   as published by the Free Software Foundation.
9 
10   This program is distributed in the hope that it would be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 
14   Further, this software is distributed without any warranty that it is
15   free of the rightful claim of any third person regarding infringement
16   or the like.  Any license provided herein, whether implied or
17   otherwise, applies only to this software file.  Patent licenses, if
18   any, provided herein do not apply to combinations of this program with
19   other software, or any other product whatsoever.
20 
21   You should have received a copy of the GNU Lesser General Public
22   License along with this program; if not, write the Free Software
23   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24   USA.
25 
26 */
27 
28 #include "config.h"
29 #include "libdwarfdefs.h"
30 
31 #include <stdio.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h> /* for exit(), C89 malloc */
36 #endif /* HAVE_STDLIB_H */
37 #include "pro_incl.h"
38 #include <stddef.h>
39 #include "dwarf.h"
40 #include "libdwarf.h"
41 #include "pro_opaque.h"
42 #include "pro_error.h"
43 #include "pro_alloc.h"
44 
45 
46 extern char *_dwarf_errmsgs[];
47 
48 /*
49     This function performs error handling as described in the
50     libdwarf consumer document section 3.  Dbg is the Dwarf_P_debug
51     structure being processed.  Error is a pointer to the pointer
52     to the error descriptor that will be returned.  Errval is an
53     error code listed in dwarf_error.h.
54 
55     The error number may be retrieved from the Dwarf_Error
56     by calling dwarf_errno().
57     The error string implied by the error number may be retrieved
58     from the Dwarf_Error by calling dwarf_errmsg().
59 */
60 void
61 _dwarf_p_error(Dwarf_P_Debug dbg,
62     Dwarf_Error * error, Dwarf_Unsigned errval)
63 {
64     Dwarf_Error errptr;
65 
66     if (errval > DW_DLE_LAST) {
67         /*  We do not expect to ever see such an error number,
68             DW_DLE_LO_USER is not used. */
69         fprintf(stderr,"ERROR VALUE: %lu - %s\n",
70             (unsigned long) errval, "this error value is unknown to libdwarf.");
71     }
72     /*  Allow NULL dbg on entry, since sometimes that can happen and we
73         want to report the upper-level error, not this one. */
74     if (error != NULL) {
75         errptr = (Dwarf_Error)
76             _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_Error_s));
77         if (errptr == NULL) {
78             fprintf(stderr,
79                 "Could not allocate Dwarf_Error structure\n");
80             abort();
81         }
82         errptr->er_errval = (Dwarf_Signed) errval;
83         *error = errptr;
84         return;
85     }
86 
87     if (dbg != NULL && dbg->de_errhand != NULL) {
88         errptr = (Dwarf_Error)
89             _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_Error_s));
90         if (errptr == NULL) {
91             fprintf(stderr,
92                 "Could not allocate Dwarf_Error structure\n");
93             abort();
94         }
95         errptr->er_errval = (Dwarf_Signed) errval;
96         dbg->de_errhand(errptr, dbg->de_errarg);
97         return;
98     }
99 
100     abort();
101 }
102