forked from dtrace4linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
345 lines (321 loc) · 8.19 KB
/
printf.c
File metadata and controls
345 lines (321 loc) · 8.19 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**********************************************************************/
/* File containing dtrace_printf and related debug functions. */
/* These functions rely on no kernel code and write to a trace */
/* buffer (/proc/dtrace/trace), so we can call them safely during */
/* critical regions of the kernel. */
/* */
/* Author: Paul D. Fox */
/* Date: December 2011 */
/* */
/* License: CDDL */
/**********************************************************************/
#include <linux/mm.h>
# undef zone
# define zone linux_zone
#include "dtrace_linux.h"
#include <sys/dtrace_impl.h>
#include "dtrace_proto.h"
/**********************************************************************/
/* This doesnt really do anything, but we might use it to dynamic */
/* switch print mode. */
/**********************************************************************/
int dtrace_printk;
module_param(dtrace_printk, int, 0);
/**********************************************************************/
/* dtrace_printf() buffer and state. */
/**********************************************************************/
#define LOG_BUFSIZ (64 * 1024)
char dtrace_buf[LOG_BUFSIZ];
int dbuf_i;
int dtrace_printf_disable;
const int log_bufsiz = LOG_BUFSIZ; /* So dtrace_linux.c can use it. */
/**********************************************************************/
/* Routine with zero outside dependencies, and hence callable from */
/* any context, to format a hrtime_t time difference. */
/**********************************************************************/
char *
hrtime_str(hrtime_t s)
{ int s1 = s / (1000 * 1000 * 1000);
int s2 = s % (1000 * 1000 * 1000);
int i;
static char buf[44];
static char tmp[44];
char *bp = buf;
for (i = 0; ; ) {
tmp[i++] = (s1 % 10) + '0';
s1 /= 10;
if (s1 == 0)
break;
}
for (; --i >= 0; ) {
*bp++ = tmp[i];
}
*bp++ = '.';
for (i = 0; i < 9; ) {
tmp[i++] = (s2 % 10) + '0';
s2 /= 10;
}
for (; --i >= 0; ) {
*bp++ = tmp[i];
}
*bp = '\0';
return buf;
}
/**********************************************************************/
/* Convert all our printks to the internal dtrace_printf. If you */
/* change the name of this function, you can revert to kernel */
/* printk()s, but might suffer deadlock in the printk() locking */
/* regime. */
/**********************************************************************/
asmlinkage int
printk(const char *fmt, ...)
{ va_list ap;
va_start(ap, fmt);
dtrace_vprintf(fmt, ap);
va_end(ap);
return 0;
}
int
dtrace_kernel_panic(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("Dtrace might have panic'ed kernel. Sorry.\n");
return NOTIFY_DONE;
}
/**********************************************************************/
/* Internal logging mechanism for dtrace. Avoid calls to printk if */
/* we are in dangerous territory). */
/**********************************************************************/
volatile int dtrace_printf_lock = -1;
# define ADDCH(ch) {dtrace_buf[dbuf_i] = ch; dbuf_i = (dbuf_i + 1) % LOG_BUFSIZ;}
#define MAX_DIGITS 40 /* In case of buggy divmod64.c */
static char tmp[48];
void
dtrace_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
dtrace_vprintf(fmt, ap);
va_end(ap);
}
static void
dtrace_printf_int(int n)
{ int i;
for (i = 0; ; ) {
tmp[i++] = (n % 10) + '0';
n /= 10;
if (n == 0)
break;
}
for (; --i >= 0; ) {
ADDCH(tmp[i]);
}
}
void
dtrace_vprintf(const char *fmt, va_list ap)
{ short ch;
unsigned long long n;
unsigned long sec, nsec;
char *cp;
short i;
short l_mode;
short zero;
short width;
static char digits[] = "0123456789abcdef";
hrtime_t hrt = dtrace_gethrtime();
static hrtime_t hrt0;
# if 0
/***********************************************/
/* Temp: dont wrap buffer - because we want */
/* to see first entries. */
/***********************************************/
if (dbuf_i >= LOG_BUFSIZ - 2048)
return;
# endif
if (dtrace_printf_disable)
return;
/***********************************************/
/* Try and avoid intermingled output from */
/* the other cpus. Dont do this if we */
/* interrupt our own cpu. */
/***********************************************/
while (dtrace_printf_lock >= 0 && dtrace_printf_lock != smp_processor_id())
;
/***********************************************/
/* Allow a blank string - dont generate any */
/* output. We often use this to add some */
/* slowdown to paths of code. We will wait */
/* for any locks, but not grab the lock, */
/* just for a bit more entropy. */
/***********************************************/
if (*fmt == '\0')
return;
dtrace_printf_lock = smp_processor_id();
/***********************************************/
/* Add in timestamp. */
/***********************************************/
if (hrt0 == 0)
hrt0 = hrt;
if (hrt) {
hrt -= hrt0;
sec = (unsigned long) (hrt / (1000 * 1000 * 1000));
nsec = (unsigned long) (hrt % (1000 * 1000 * 1000));
for (i = 0; ; ) {
tmp[i++] = (sec % 10) + '0';
sec /= 10;
if (sec == 0)
break;
}
for (; --i >= 0; ) {
ADDCH(tmp[i]);
}
ADDCH('.');
for (i = 0; i < 9; ) {
tmp[i++] = (nsec % 10) + '0';
nsec /= 10;
}
for (; --i >= 0; ) {
ADDCH(tmp[i]);
}
ADDCH(' ');
}
/***********************************************/
/* Add the current CPU. */
/***********************************************/
ADDCH('#');
dtrace_printf_int(smp_processor_id());
ADDCH(' ');
dtrace_printf_int(get_current()->pid);
if (irqs_disabled()) {
ADDCH('-');
} else {
ADDCH(':');
}
while ((ch = *fmt++) != '\0') {
if (ch != '%') {
ADDCH(ch);
continue;
}
zero = ' ';
width = -1;
if ((ch = *fmt++) == '\0')
break;
if (ch == '0')
zero = '0';
while (ch >= '0' && ch <= '9') {
if (width < 0)
width = ch - '0';
else
width = 10 * width + ch - '0';
if ((ch = *fmt++) == '\0')
break;
}
l_mode = FALSE;
if (ch == '*') {
width = (int) va_arg(ap, int);
if ((ch = *fmt++) == '\0')
break;
}
if (ch == '.') {
if ((ch = *fmt++) == '\0')
break;
}
if (ch == '*') {
width = (int) va_arg(ap, int);
if ((ch = *fmt++) == '\0')
break;
}
if (ch == 'l') {
l_mode = TRUE;
if ((ch = *fmt++) == '\0')
break;
if (ch == 'l') {
l_mode++;
if ((ch = *fmt++) == '\0')
break;
}
}
switch (ch) {
case 'c':
ch = (char) va_arg(ap, int);
ADDCH(ch);
break;
case 'd':
case 'u':
if (l_mode) {
n = va_arg(ap, unsigned long);
} else {
n = va_arg(ap, unsigned int);
}
if (ch == 'd' && (long) n < 0) {
ADDCH('-');
n = -n;
}
for (i = 0; i < MAX_DIGITS; i++) {
tmp[i] = '0' + (n % 10);
n /= 10;
if (n == 0)
break;
}
while (i >= 0)
ADDCH(tmp[i--]);
break;
case 'p':
#if defined(__i386) || defined(__arm__)
width = 8;
#else
width = 16;
#endif
zero = '0';
l_mode = TRUE;
// fallthru...
case 'x':
if (l_mode) {
n = va_arg(ap, unsigned long);
} else {
n = va_arg(ap, unsigned int);
}
for (i = 0; ; ) {
tmp[i++] = digits[(n & 0xf)];
n >>= 4;
if (n == 0)
break;
}
width -= i;
while (width-- > 0)
ADDCH(zero);
while (--i >= 0)
ADDCH(tmp[i]);
break;
case 's':
cp = va_arg(ap, char *);
if (cp == NULL)
cp = "(null)";
while (*cp) {
ADDCH(*cp++);
if (width >= 0) {
if (--width < 0)
break;
}
}
break;
}
}
dtrace_printf_lock = -1;
}
/**********************************************************************/
/* Utility routine for debugging, mostly not needed. Turn on all */
/* writes to the console - may be needed when debugging low level */
/* interrupts which crash the box. */
/**********************************************************************/
void
set_console_on(int flag)
{ int mode = flag ? 7 : 0;
static int first_time = TRUE;
static int *console_printk;
if (first_time) {
console_printk = get_proc_addr("console_printk");
first_time = FALSE;
}
if (console_printk)
console_printk[0] = mode;
}