error.c
3.93 KB
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
/*==============================================================================
Module: error.c
$Revision: 1.1.1.2 $
$Date: 2002/10/29 08:07:15 $
$Author: blythe $
$Source: /root/leakn64/depot/rf/sw/n64os20l/tools/gcord/com/error.c,v $
error handling
==============================================================================*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <libelf.h>
#include <libdwarf.h>
#include "option.h"
#include "global.h"
extern int _doprnt(char *, va_list, FILE);
extern int errno;
extern int sys_nerr;
extern char *sys_errlist[];
#undef stderr
#define stderr stdout
/*------------------------------------------------------------------------------
print a note, a situation where no error should occur.
It's something that the user should be aware of.
------------------------------------------------------------------------------*/
void note(const *format, ...)
{
va_list ap;
printf("Note: ");
va_start(ap, format);
_doprnt(format, ap, stderr);
printf("\n");
va_end(ap);
}
/*------------------------------------------------------------------------------
print a warning, a situation where error may occur.
------------------------------------------------------------------------------*/
void warning(const *format, ...)
{
va_list ap;
fprintf(stderr, "Warning: ");
va_start(ap, format );
_doprnt(format, ap, stderr);
fprintf(stderr, ".\n");
va_end(ap);
}
/*------------------------------------------------------------------------------
print a error and exit.
------------------------------------------------------------------------------*/
void error(const *format, ...)
{
va_list ap;
fprintf(stderr, "Error: ");
va_start(ap, format);
_doprnt(format, ap, stderr);
fprintf(stderr, ".\n");
va_end(ap);
exit(1);
}
/*------------------------------------------------------------------------------
sysfatal:
- caused by error return values of a system call when it shouldn't.
- program exits.
------------------------------------------------------------------------------*/
void sysfatal(const *format, ...)
{
va_list ap;
if (errno == -1)
{
fprintf(stderr, "Error: ");
fprintf(stderr, "unexpected end of file, ");
}
else if (errno < sys_nerr)
{
fprintf(stderr, "Error: ");
fprintf(stderr, "%s, ", sys_errlist[errno]);
}
else
{
fprintf(stderr, "Error: ");
fprintf(stderr, "error code %d, ", errno);
}
va_start(ap, format);
_doprnt(format, ap, stderr);
fprintf(stderr, ".\n");
va_end(ap);
exit(1);
}
/*------------------------------------------------------------------------------
dump info with header
------------------------------------------------------------------------------*/
void dump(const *format, ...)
{
va_list ap;
printf("<Debug> ");
va_start(ap, format);
_doprnt(format, ap, stdout);
printf("\n");
va_end(ap);
}
/*------------------------------------------------------------------------------
print a dwarf error.
------------------------------------------------------------------------------*/
void print_dwarf_error (Dwarf_Debug dbg, int dwarf_code,
Dwarf_Error err, const *format, ...)
{
va_list ap;
va_start(ap, format);
fprintf(stderr, "Dwarf Error: ");
if(dwarf_code == DW_DLV_ERROR)
{
char *errmsg = dwarf_errmsg(err);
long long errno = dwarf_errno(err);
fprintf(stderr,"%s (%lld) ", errmsg, errno);
dwarf_dealloc (dbg, errmsg, DW_DLA_STRING);
}
else if (dwarf_code == DW_DLV_NO_ENTRY)
{
fprintf(stderr,"NO ENTRY ");
}
else if (dwarf_code == DW_DLV_OK)
{
fprintf(stderr,"DLV_OK ");
}
else
{
fprintf(stderr,"code %d ",dwarf_code);
}
_doprnt(format, ap, stderr);
fprintf(stderr,"\n");
va_end(ap);
exit(1);
}