summaryrefslogtreecommitdiff
path: root/drivers/staging/ktap/userspace/main.c
blob: 2aa686a99914a2879e1d5ce4360816b81b0b49e4 (plain)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * main.c - ktap compiler and loader entry
 *
 * This file is part of ktap by Jovi Zhangwei.
 *
 * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
 *
 * ktap is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * ktap is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>

#include "../include/ktap_types.h"
#include "../include/ktap_opcodes.h"
#include "ktapc.h"


/*******************************************************************/

void *ktapc_reallocv(void *block, size_t osize, size_t nsize)
{
	return kp_reallocv(NULL, block, osize, nsize);
}

ktap_closure *ktapc_newlclosure(int n)
{
	return kp_newlclosure(NULL, n);
}

ktap_proto *ktapc_newproto()
{
	return kp_newproto(NULL);
}

const ktap_value *ktapc_table_get(ktap_table *t, const ktap_value *key)
{
	return kp_table_get(t, key);
}

void ktapc_table_setvalue(ktap_table *t, const ktap_value *key, ktap_value *val)
{
	kp_table_setvalue(NULL, t, key, val);
}

ktap_table *ktapc_table_new()
{
	return kp_table_new(NULL);
}

ktap_string *ktapc_ts_newlstr(const char *str, size_t l)
{
	return kp_tstring_newlstr(NULL, str, l);
}

ktap_string *ktapc_ts_new(const char *str)
{
	return kp_tstring_new(NULL, str);
}

int ktapc_ts_eqstr(ktap_string *a, ktap_string *b)
{
	return kp_tstring_eqstr(a, b);
}

static void ktapc_runerror(const char *err_msg_fmt, ...)
{
	va_list ap;

	fprintf(stderr, "ktapc_runerror\n");

	va_start(ap, err_msg_fmt);
	vfprintf(stderr, err_msg_fmt, ap);
	va_end(ap);

	exit(EXIT_FAILURE);
}

/*
 * todo: memory leak here
 */
char *ktapc_sprintf(const char *fmt, ...)
{
	char *msg = malloc(128);

	va_list argp;
	va_start(argp, fmt);
	vsprintf(msg, fmt, argp);
	va_end(argp);
	return msg;
}


#define MINSIZEARRAY	4

void *ktapc_growaux(void *block, int *size, size_t size_elems, int limit,
		    const char *what)
{
	void *newblock;
	int newsize;

	if (*size >= limit/2) {  /* cannot double it? */
		if (*size >= limit)  /* cannot grow even a little? */
			ktapc_runerror("too many %s (limit is %d)\n",
					what, limit);
		newsize = limit;  /* still have at least one free place */
	} else {
		newsize = (*size) * 2;
		if (newsize < MINSIZEARRAY)
			newsize = MINSIZEARRAY;  /* minimum size */
	}

	newblock = ktapc_reallocv(block, (*size) * size_elems, newsize * size_elems);
	*size = newsize;  /* update only when everything else is OK */
	return newblock;
}

/*************************************************************************/

#define print_base(i) \
	do {	\
		if (i < f->sizelocvars) /* it's a localvars */ \
			printf("%s", getstr(f->locvars[i].varname));  \
		else \
			printf("base + %d", i);	\
	} while (0)

#define print_RKC(instr)	\
	do {	\
		if (ISK(GETARG_C(instr))) \
			kp_showobj(NULL, k + INDEXK(GETARG_C(instr))); \
		else \
			print_base(GETARG_C(instr)); \
	} while (0)

static void decode_instruction(ktap_proto *f, int instr)
{
	int opcode = GET_OPCODE(instr);
	ktap_value *k;

	k = f->k;

	printf("%.8x\t", instr);
	printf("%s\t", ktap_opnames[opcode]);

	switch (opcode) {
	case OP_GETTABUP:
		print_base(GETARG_A(instr));
		printf(" <- ");

		if (GETARG_B(instr) == 0)
			printf("global");
		else
			printf("upvalues[%d]", GETARG_B(instr));

		printf("{"); print_RKC(instr); printf("}");

		break;
	case OP_GETTABLE:
		print_base(GETARG_A(instr));
		printf(" <- ");

		print_base(GETARG_B(instr));

		printf("{");
		print_RKC(instr);
		printf("}");
		break;
	case OP_LOADK:
		printf("\t");
		print_base(GETARG_A(instr));
		printf(" <- ");

		kp_showobj(NULL, k + GETARG_Bx(instr));
		break;
	case OP_CALL:
		printf("\t");
		print_base(GETARG_A(instr));
		break;
	case OP_JMP:
		printf("\t%d", GETARG_sBx(instr));
		break;
	default:
		break;
	}

	printf("\n");
}

static int function_nr = 0;

/* this is a debug function used for check bytecode chunk file */
static void dump_function(int level, ktap_proto *f)
{
	int i;

	printf("\n----------------------------------------------------\n");
	printf("function %d [level %d]:\n", function_nr++, level);
	printf("linedefined: %d\n", f->linedefined);
	printf("lastlinedefined: %d\n", f->lastlinedefined);
	printf("numparams: %d\n", f->numparams);
	printf("is_vararg: %d\n", f->is_vararg);
	printf("maxstacksize: %d\n", f->maxstacksize);
	printf("source: %s\n", getstr(f->source));
	printf("sizelineinfo: %d \t", f->sizelineinfo);
	for (i = 0; i < f->sizelineinfo; i++)
		printf("%d ", f->lineinfo[i]);
	printf("\n");

	printf("sizek: %d\n", f->sizek);
	for (i = 0; i < f->sizek; i++) {
		switch(f->k[i].type) {
		case KTAP_TNIL:
			printf("\tNIL\n");
			break;
		case KTAP_TBOOLEAN:
			printf("\tBOOLEAN: ");
			printf("%d\n", f->k[i].val.b);
			break;
		case KTAP_TNUMBER:
			printf("\tTNUMBER: ");
			printf("%ld\n", f->k[i].val.n);
			break;
		case KTAP_TSTRING:
			printf("\tTSTRING: ");
			printf("%s\n", svalue(&(f->k[i])));

			break;
		default:
			printf("\terror: unknow constant type\n");
		}
	}

	printf("sizelocvars: %d\n", f->sizelocvars);
	for (i = 0; i < f->sizelocvars; i++) {
		printf("\tlocvars: %s startpc: %d endpc: %d\n",
			getstr(f->locvars[i].varname), f->locvars[i].startpc,
			f->locvars[i].endpc);
	}

	printf("sizeupvalues: %d\n", f->sizeupvalues);
	for (i = 0; i < f->sizeupvalues; i++) {
		printf("\tname: %s instack: %d idx: %d\n",
			getstr(f->upvalues[i].name), f->upvalues[i].instack,
			f->upvalues[i].idx);
	}

	printf("\n");
	printf("sizecode: %d\n", f->sizecode);
	for (i = 0; i < f->sizecode; i++)
		decode_instruction(f, f->code[i]);

	printf("sizep: %d\n", f->sizep);
	for (i = 0; i < f->sizep; i++)
		dump_function(level + 1, f->p[i]);

}

static void usage(const char *msg_fmt, ...)
{
	va_list ap;

	va_start(ap, msg_fmt);
	fprintf(stderr, msg_fmt, ap);
	va_end(ap);

	fprintf(stderr,
"Usage: ktap [options] file [script args] -- cmd [args]\n"
"   or: ktap [options] -e one-liner  -- cmd [args]\n"
"\n"
"Options and arguments:\n"
"  -o file        : send script output to file, instead of stderr\n"
"  -p pid         : specific tracing pid\n"
"  -C cpu         : cpu to monitor in system-wide\n"
"  -T             : show timestamp for event\n"
"  -V             : show version\n"
"  -v             : enable verbose mode\n"
"  -s             : simple event tracing\n"
"  -b             : list byte codes\n"
"  file           : program read from script file\n"
"  -- cmd [args]  : workload to tracing\n");

	exit(EXIT_FAILURE);
}

ktap_global_state dummy_global_state;

static void init_dummy_global_state()
{
	memset(&dummy_global_state, 0, sizeof(ktap_global_state));
	dummy_global_state.seed = 201236;

        kp_tstring_resize(NULL, 32); /* set inital string hashtable size */
}

#define handle_error(str) do { perror(str); exit(-1); } while(0)

static struct ktap_parm uparm;
static int ktap_trunk_mem_size = 1024;

static int ktapc_writer(const void* p, size_t sz, void* ud)
{
	if (uparm.trunk_len + sz > ktap_trunk_mem_size) {
		int new_size = (uparm.trunk_len + sz) * 2;
		uparm.trunk = realloc(uparm.trunk, new_size);
		ktap_trunk_mem_size = new_size;
	}

	memcpy(uparm.trunk + uparm.trunk_len, p, sz);
	uparm.trunk_len += sz;

	return 0;
}


static int forks;
static char **workload_argv;

static int fork_workload(int ktap_fd)
{
	int pid;

	pid = fork();
	if (pid < 0)
		handle_error("failed to fork");

	if (pid > 0)
		return pid;

	signal(SIGTERM, SIG_DFL);

	execvp("", workload_argv);

	/*
	 * waiting ktapvm prepare all tracing event
	 * make it more robust in future.
	 */
	pause();

	execvp(workload_argv[0], workload_argv);

	perror(workload_argv[0]);
	exit(-1);

	return -1;
}

#define KTAPVM_PATH "/sys/kernel/debug/ktap/ktapvm"

static char *output_filename;
pid_t ktap_pid;

static int run_ktapvm()
{
        int ktapvm_fd, ktap_fd;
	int ret;

	ktap_pid = getpid();

	ktapvm_fd = open(KTAPVM_PATH, O_RDONLY);
	if (ktapvm_fd < 0)
		handle_error("open " KTAPVM_PATH " failed");

	ktap_fd = ioctl(ktapvm_fd, 0, NULL);
	if (ktap_fd < 0)
		handle_error("ioctl ktapvm failed");

	ktapio_create(output_filename);

	if (forks) {
		uparm.trace_pid = fork_workload(ktap_fd);
		uparm.workload = 1;
	}

	ret = ioctl(ktap_fd, KTAP_CMD_IOC_RUN, &uparm);

	close(ktap_fd);
	close(ktapvm_fd);

	return ret;
}

int verbose;
static int dump_bytecode;
static char oneline_src[1024];
static int trace_pid = -1;
static int trace_cpu = -1;
static int print_timestamp;

#define SIMPLE_ONE_LINER_FMT	\
	"trace %s { print(cpu(), tid(), execname(), argevent) }"

static const char *script_file;
static int script_args_start;
static int script_args_end;

static void parse_option(int argc, char **argv)
{
	char pid[32] = {0};
	char cpu_str[32] = {0};
	char *next_arg;
	int i, j;

	for (i = 1; i < argc; i++) {
		if (argv[i][0] != '-') {
			script_file = argv[i];
			if (!script_file)
				usage("");

			script_args_start = i + 1;
			script_args_end = argc;

			for (j = i + 1; j < argc; j++) {
				if (argv[j][0] == '-' && argv[j][1] == '-')
					goto found_cmd;
			}

			return;
		}

		if (argv[i][0] == '-' && argv[i][1] == '-') {
			j = i;
			goto found_cmd;
		}

		next_arg = argv[i + 1];

		switch (argv[i][1]) {
		case 'o':
			output_filename = malloc(strlen(next_arg) + 1);
			if (!output_filename)
				return;

			strncpy(output_filename, next_arg, strlen(next_arg));
			i++;
			break;
		case 'e':
			strncpy(oneline_src, next_arg, strlen(next_arg));
			i++;
			break;
		case 'p':
			strncpy(pid, next_arg, strlen(next_arg));
			trace_pid = atoi(pid);
			i++;
			break;
		case 'C':
			strncpy(cpu_str, next_arg, strlen(next_arg));
			trace_cpu = atoi(cpu_str);
			i++;
			break;
		case 'T':
			print_timestamp = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case 's':
			sprintf(oneline_src, SIMPLE_ONE_LINER_FMT, next_arg);
			i++;
			break;
		case 'b':
			dump_bytecode = 1;
			break;
		case 'V':
		case '?':
		case 'h':
			usage("");
			break;
		default:
			usage("wrong argument\n");
			break;
		}
	}

	return;

 found_cmd:
	script_args_end = j;
	forks = 1;
	workload_argv = &argv[j + 1];
}

static void compile(const char *input)
{
	ktap_closure *cl;
	char *buff;
	struct stat sb;
	int fdin;

	if (oneline_src[0] != '\0') {
		init_dummy_global_state();
		cl = ktapc_parser(oneline_src, input);
		goto dump;
	}

	fdin = open(input, O_RDONLY);
	if (fdin < 0) {
		fprintf(stderr, "open file %s failed\n", input);
		exit(-1);
	}

	if (fstat(fdin, &sb) == -1)
		handle_error("fstat failed");

	buff = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fdin, 0);
	if (buff == MAP_FAILED)
		handle_error("mmap failed");

	init_dummy_global_state();
	cl = ktapc_parser(buff, input);

	munmap(buff, sb.st_size);
	close(fdin);

 dump:
	if (dump_bytecode) {
		dump_function(1, cl->l.p);
		exit(0);
	}

	/* ktapc output */
	uparm.trunk = malloc(ktap_trunk_mem_size);
	if (!uparm.trunk)
		handle_error("malloc failed");

	ktapc_dump(cl->l.p, ktapc_writer, NULL, 0);
}

int main(int argc, char **argv)
{
	char **ktapvm_argv;
	int new_index, i;
	int ret;

	if (argc == 1)
		usage("");

	parse_option(argc, argv);

	if (oneline_src[0] != '\0')
		script_file = "one-liner";

	compile(script_file);

	ktapvm_argv = (char **)malloc(sizeof(char *)*(script_args_end -
					script_args_start + 1));
	if (!ktapvm_argv) {
		fprintf(stderr, "canno allocate ktapvm_argv\n");
		return -1;
	}

	ktapvm_argv[0] = malloc(strlen(script_file) + 1);
	if (!ktapvm_argv[0]) {
		fprintf(stderr, "canno allocate memory\n");
		return -1;
	}
	strcpy(ktapvm_argv[0], script_file);
	ktapvm_argv[0][strlen(script_file)] = '\0';

	/* pass rest argv into ktapvm */
	new_index = 1;
	for (i = script_args_start; i < script_args_end; i++) {
		ktapvm_argv[new_index] = malloc(strlen(argv[i]) + 1);
		if (!ktapvm_argv[new_index]) {
			fprintf(stderr, "canno allocate memory\n");
			return -1;
		}
		strcpy(ktapvm_argv[new_index], argv[i]);
		ktapvm_argv[new_index][strlen(argv[i])] = '\0';
		new_index++;
	}

	uparm.argv = ktapvm_argv;
	uparm.argc = new_index;
	uparm.verbose = verbose;
	uparm.trace_pid = trace_pid;
	uparm.trace_cpu = trace_cpu;
	uparm.print_timestamp = print_timestamp;

	/* start running into kernel ktapvm */
	ret = run_ktapvm();

	cleanup_event_resources();
	return ret;
}