-
Notifications
You must be signed in to change notification settings - Fork 18
/
printz.c
350 lines (309 loc) · 6.99 KB
/
printz.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
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
346
347
348
349
350
#include "printz.h"
#include "zus.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>
#include <dlfcn.h>
#include <errno.h>
#include <ctype.h>
struct module_ddbg {
char name[ZUS_LIBFS_MAX_PATH];
__u16 n_dbg_entries;
struct _ddebug *dbg_entries[0];
};
static struct ddbg_db {
struct module_ddbg *modules[ZUS_LIBFS_MAX_NR];
__u16 mod_count;
__u32 next_id;
} ddbg_db = {};
static void _init_ddbg(struct _ddebug *dd, const char *modname)
{
char *no_path_name = strrchr(dd->filename, '/');
dd->id = ++ddbg_db.next_id;
dd->modname = modname;
if (no_path_name)
dd->filename = no_path_name + 1;
}
int zus_add_module_ddbg(const char *fs_name, void *handle)
{
struct _ddebug *iter, *stop;
size_t mddbg_sz;
int i;
int n_dbg;
struct module_ddbg *modd;
if (strlen(fs_name) >= sizeof(modd->name)) {
ERROR("Name too-long fs_name=%s\n", fs_name);
return -EINVAL;
}
iter = dlsym(handle, "__start_zus_ddbg");
if (!iter) {
ERROR("Unable to get library start symbol\n");
return -EINVAL;
}
stop = dlsym(handle, "__stop_zus_ddbg");
if (!stop) {
ERROR("Unable to get library start symbol\n");
return -EINVAL;
}
n_dbg = stop - iter;
mddbg_sz = sizeof(struct module_ddbg) +
n_dbg * sizeof(struct _ddebug *);
ddbg_db.modules[ddbg_db.mod_count] = calloc(1, mddbg_sz);
if (!ddbg_db.modules[ddbg_db.mod_count])
return -ENOMEM;
modd = ddbg_db.modules[ddbg_db.mod_count];
strncpy(modd->name, fs_name, sizeof(modd->name) - 1);
modd->n_dbg_entries = n_dbg;
for (i = 0; i < n_dbg; ++i, ++iter) {
_init_ddbg(iter, modd->name);
modd->dbg_entries[i] = iter;
}
++ddbg_db.mod_count;
return 0;
}
void zus_free_ddbg_db(void)
{
int i;
for (i = 0; i < ddbg_db.mod_count && ddbg_db.modules[i]; ++i)
free(ddbg_db.modules[i]);
}
static void _copy_format(const char *fmt, char *buff, size_t sz)
{
size_t len = strlen(fmt);
size_t i;
size_t buff_i = 0;
for (i = 0; i < len && buff_i < sz; ++i) {
switch (fmt[i]) {
case '\n':
case '\t':
if (buff_i + 2 > sz)
return;
*buff++ = '\\';
*buff++ = fmt[i] == '\n' ? 'n' : 't';
buff_i += 2;
break;
default:
*buff++ = fmt[i];
++buff_i;
}
}
*buff = 0;
}
#define MAX_FORMAT_SIZE 512
int zus_ddbg_read(struct zufs_ddbg_info *zdi)
{
char *buff = zdi->msg;
size_t buff_sz = zdi->len;
struct module_ddbg *modd;
struct _ddebug *ddbg;
int mod_i, i;
size_t buff_jmp;
char format[MAX_FORMAT_SIZE + 1];
for (mod_i = 0; mod_i < ddbg_db.mod_count; ++mod_i) {
modd = ddbg_db.modules[mod_i];
for (i = 0; i < modd->n_dbg_entries && buff_sz > 0; ++i) {
ddbg = modd->dbg_entries[i];
if (ddbg->id <= zdi->id)
continue;
_copy_format(ddbg->format, format, MAX_FORMAT_SIZE);
buff_jmp =
snprintf(buff, buff_sz,
"%s:%d [%s] %s =%s \"%s\"\n",
ddbg->filename, ddbg->lineno,
ddbg->modname, ddbg->function,
ddbg->active ? "p" : "_",
format);
if (buff_jmp > buff_sz) {
*buff = 0;
goto out;
}
buff_sz -= buff_jmp;
buff += buff_jmp;
zdi->id = ddbg->id;
}
++modd;
}
out:
zdi->len = strlen(zdi->msg);
return 0;
}
enum ddbg_cmd {
DDBG_CMD_UNSET = 0,
DDBG_CMD_ENABLE,
DDBG_CMD_DISABLE,
};
#define MAX_DDBG_CMD_TOKENS 9
struct ddbg_ctl {
char *tokens[MAX_DDBG_CMD_TOKENS];
int ntokens;
const char *modname;
const char *function;
const char *filename;
unsigned int lineno;
const char *format;
enum ddbg_cmd cmd;
};
static int _tokenize(char *buf, struct ddbg_ctl *cmd)
{
while (*buf) {
char *end;
/* Skip leading whitespace */
for (; isspace(*buf); ++buf)
;
if (!*buf)
break;
/* Skip comment */
if (*buf == '#')
break;
if (*buf == '"' || *buf == '\'') {
int quote = *buf++;
for (end = buf; *end && *end != quote; end++)
;
if (!*end) {
ERROR("unclosed quote: %s\n", buf);
return -EINVAL; /* unclosed quote */
}
} else {
for (end = buf; *end && !isspace(*end); end++)
;
}
/* `buf' is start of word, `end' is one past its end */
if (cmd->ntokens == MAX_DDBG_CMD_TOKENS) {
ERROR("too many ddbg cmd tokens\n");
return -EINVAL;
}
if (*end)
*end++ = 0;
cmd->tokens[cmd->ntokens++] = buf;
buf = end;
}
return 0;
}
#define NO_LINE_NUMBER ((unsigned int) -1)
enum {
DDBG_CRIT_MOD = 1,
DDBG_CRIT_FUNC,
DDBG_CRIT_FILE,
DDBG_CRIT_LINENO,
DDBG_CRIT_FMT,
DDBG_CRIT_ENABLE,
DDBG_CRIT_DISABLE,
};
static int _token_type(const char *token)
{
if (strcmp(token, "module") == 0)
return DDBG_CRIT_MOD;
if (strcmp(token, "func") == 0)
return DDBG_CRIT_FUNC;
if (strcmp(token, "file") == 0)
return DDBG_CRIT_FILE;
if (strcmp(token, "line") == 0)
return DDBG_CRIT_LINENO;
if (strcmp(token, "format") == 0)
return DDBG_CRIT_FMT;
if (strcmp(token, "+p") == 0)
return DDBG_CRIT_ENABLE;
if (strcmp(token, "-p") == 0)
return DDBG_CRIT_DISABLE;
return -EINVAL;
}
#define REQUIRE_ADDITIONAL_TOKEN \
do { if (++i >= ddc->ntokens) return -EINVAL; } while (0)
static int _parse(struct ddbg_ctl *ddc)
{
int i = 0;
int ttype;
while (i < ddc->ntokens) {
ttype = _token_type(ddc->tokens[i]);
switch (ttype) {
case DDBG_CRIT_MOD:
REQUIRE_ADDITIONAL_TOKEN;
ddc->modname = ddc->tokens[i];
break;
case DDBG_CRIT_FUNC:
REQUIRE_ADDITIONAL_TOKEN;
ddc->function = ddc->tokens[i];
break;
case DDBG_CRIT_FILE:
REQUIRE_ADDITIONAL_TOKEN;
ddc->filename = ddc->tokens[i];
break;
case DDBG_CRIT_LINENO: {
char *leftover = NULL;
REQUIRE_ADDITIONAL_TOKEN;
ddc->lineno = strtoul(ddc->tokens[i], &leftover, 10);
if (*leftover) /* Someone is having fun */
return -EINVAL;
break;
}
case DDBG_CRIT_FMT:
REQUIRE_ADDITIONAL_TOKEN;
ddc->format = ddc->tokens[i];
break;
case DDBG_CRIT_ENABLE:
ddc->cmd = DDBG_CMD_ENABLE;
break;
case DDBG_CRIT_DISABLE:
ddc->cmd = DDBG_CMD_DISABLE;
break;
default:
ERROR("Unkonwn token %s\n", ddc->tokens[i]);
return -EINVAL;
}
++i;
}
if (ddc->cmd == DDBG_CMD_UNSET) {
ERROR("no ddbg command is given\n");
return -EINVAL;
}
return 0;
}
static int _process(struct ddbg_ctl *ddc)
{
bool enable = ddc->cmd == DDBG_CMD_ENABLE;
struct module_ddbg *modd;
struct _ddebug *ddbg;
int mod_i, i;
for (mod_i = 0; mod_i < ddbg_db.mod_count; ++mod_i) {
modd = ddbg_db.modules[mod_i];
if (ddc->modname && strcmp(ddc->modname, modd->name))
continue;
for (i = 0; i < modd->n_dbg_entries; ++i) {
ddbg = modd->dbg_entries[i];
if (ddc->filename &&
strcmp(ddc->filename, ddbg->filename))
continue;
if (ddc->function &&
strcmp(ddc->function, ddbg->function))
continue;
if (ddc->lineno != NO_LINE_NUMBER &&
ddc->lineno != ddbg->lineno)
continue;
if (ddc->format && !strstr(ddbg->format, ddc->format))
continue;
ddbg->active = enable;
}
++modd;
}
return 0;
}
int zus_ddbg_write(struct zufs_ddbg_info *zdi)
{
struct ddbg_ctl ddc = {
.lineno = NO_LINE_NUMBER,
.cmd = DDBG_CMD_UNSET,
};
int err;
err = _tokenize(zdi->msg, &ddc);
if (err)
return err;
err = _parse(&ddc);
if (err)
return err;
err = _process(&ddc);
if (err)
return err;
return 0;
}