-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnf_debug.c
292 lines (248 loc) · 5.5 KB
/
nf_debug.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdarg.h>
#include <stdio.h>
#include <mint/arch/nf_ops.h>
#include <mint/mintbind.h>
#include <osbind.h>
#include <stdint.h>
#include <errno.h>
struct msgbuf {
char *buf;
size_t bufsize;
size_t used;
};
#define EMULATE_DEBUGPRINTF 0
#if EMULATE_DEBUGPRINTF
/*** ---------------------------------------------------------------------- ***/
static long debugprintf_putchar(int c, struct msgbuf *msg)
{
if (msg->used < msg->bufsize)
{
msg->buf[msg->used++] = c;
}
return 1;
}
/*** ---------------------------------------------------------------------- ***/
static long debugprintf_putc(struct msgbuf *msg, int c, int width)
{
long put;
put = debugprintf_putchar(c, msg);
while (--width > 0)
{
put += debugprintf_putchar(' ', msg);
}
return put;
}
/*** ---------------------------------------------------------------------- ***/
static long debugprintf_putl(struct msgbuf *msg, unsigned long u, int base, int width, int fill_char, int upper)
{
char obuf[32];
char *t = obuf;
long put = 0;
do {
*t = "0123456789abcdef"[u % base];
if (upper && *t >= 'a' && *t <= 'f')
*t = *t - 'a' + 'A';
t++;
u /= base;
width--;
} while (u > 0);
while (width-- > 0)
{
put += debugprintf_putchar(fill_char, msg);
}
while (t != obuf)
{
put += debugprintf_putchar(*--t, msg);
}
return put;
}
/*** ---------------------------------------------------------------------- ***/
static long debugprintf_puts(struct msgbuf *msg, const char *s, int width)
{
long put = 0;
if (s == NULL)
s = "(null)";
while (*s)
{
put += debugprintf_putchar(*s++, msg);
width--;
}
while (width-- > 0)
{
put += debugprintf_putchar(' ', msg);
}
return put;
}
/*** ---------------------------------------------------------------------- ***/
# define TIMESTEN(x) ((((x) << 2) + (x)) << 1)
static long debugprintf(struct msgbuf *msg, const char *fmt, va_list args)
{
char c;
char fill_char;
long len = 0;
int width;
int long_flag;
char *s_arg;
int i_arg;
long l_arg;
while ((c = *fmt++) != 0)
{
if (c != '%')
{
len += debugprintf_putc(msg, c, 1);
continue;
}
c = *fmt++;
width = 0;
long_flag = 0;
fill_char = ' ';
if (c == '0')
{
fill_char = '0';
c = *fmt++;
}
while (c >= '0' && c <= '9')
{
width = TIMESTEN(width) + (c - '0');
c = *fmt++;
}
if (c == 'l' || c == 'L')
{
long_flag = 1;
c = *fmt++;
}
if (!c)
break;
switch (c)
{
case '%':
len += debugprintf_putc(msg, c, width);
break;
case 'c':
i_arg = (int)va_arg(args, int32_t);
len += debugprintf_putc(msg, i_arg, width);
break;
case 's':
s_arg = (char *)(va_arg(args, int32_t));
len += debugprintf_puts(msg, s_arg, width);
break;
case 'i':
case 'd':
if (long_flag)
l_arg = (long)va_arg(args, int32_t);
else
l_arg = (int)va_arg(args, int32_t);
if (l_arg < 0)
{
len += debugprintf_putc(msg, '-', 1);
width--;
l_arg = -l_arg;
}
len += debugprintf_putl(msg, l_arg, 10, width, fill_char, FALSE);
break;
case 'o':
if (long_flag)
l_arg = (long)va_arg(args, int32_t);
else
l_arg = (unsigned int)va_arg(args, int32_t);
len += debugprintf_putl(msg, l_arg, 8, width, fill_char, FALSE);
break;
case 'x':
case 'X':
if (long_flag)
l_arg = (long)va_arg(args, int32_t);
else
l_arg = (unsigned int)va_arg(args, int32_t);
len += debugprintf_putl(msg, l_arg, 16, width, fill_char, c == 'X');
break;
case 'b':
if (long_flag)
l_arg = (long)va_arg(args, int32_t);
else
l_arg = (unsigned int)va_arg(args, int32_t);
len += debugprintf_putl(msg, l_arg, 2, width, fill_char, FALSE);
break;
case 'u':
if (long_flag)
l_arg = (long)va_arg(args, int32_t);
else
l_arg = (unsigned int)va_arg(args, int32_t);
len += debugprintf_putl(msg, l_arg, 10, width, fill_char, FALSE);
break;
case 'p':
l_arg = (long)va_arg(args, int32_t);
while (width > 10)
{
len += debugprintf_putchar(' ', msg);
width--;
}
len += debugprintf_puts(msg, "0x", 0);
len += debugprintf_putl(msg, l_arg, 16, 8, '0', FALSE);
break;
}
}
return len;
}
#endif
/*** ---------------------------------------------------------------------- ***/
int nf_debugvprintf(const char *format, va_list args)
{
struct nf_ops *ops;
long nfid_stderr;
int ret;
if ((ops = nf_init()) == NULL ||
(nfid_stderr = NF_GET_ID(ops, NF_ID_STDERR)) == 0)
{
#ifdef ENOSYS
errno = ENOSYS;
#else
errno = EIO;
#endif
return -1;
}
#if EMULATE_DEBUGPRINTF
{
struct msgbuf msg;
static char buf[2048];
msg.buf = buf;
msg.bufsize = sizeof(buf);
msg.used = 0;
ret = (int)debugprintf(&msg, format, args);
if (ret >= (int)sizeof(buf))
ret = (int)sizeof(buf) - 1;
buf[ret] = '\0';
ret = (int)ops->call(nfid_stderr | 0, (uint32_t)virt_to_phys(buf));
}
#else
{
#if defined(_PUREC_SOURCE) || !defined(HAVE_VASPRINTF)
static char buf[2048];
#ifdef HAVE_VSNPRINTF
ret = vsnprintf(buf, sizeof(buf), format, args);
#else
ret = vsprintf(buf, format, args);
#endif
ret = (int)ops->call(nfid_stderr | 0, (uint32_t)virt_to_phys(buf));
#else
char *buf = NULL;
ret = vasprintf(&buf, format, args);
if (buf)
{
ret = (int)ops->call(nfid_stderr | 0, (uint32_t)virt_to_phys(buf));
free(buf);
}
#endif
}
#endif
return ret;
}
/*** ---------------------------------------------------------------------- ***/
int nf_debugprintf(const char *format, ...)
{
int ret;
va_list args;
va_start(args, format);
ret = nf_debugvprintf(format, args);
va_end(args);
return ret;
}