Subversion Repositories HelenOS

Rev

Rev 3454 | Rev 3470 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3438 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup trace
30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <syscall.h>
39
#include <ipc/ipc.h>
40
#include <fibril.h>
41
#include <errno.h>
42
#include <udebug.h>
43
#include <async.h>
3447 svoboda 44
#include <task.h>
3438 svoboda 45
 
46
// Temporary: service and method names
47
#include "proto.h"
48
#include <ipc/services.h>
49
#include "../../srv/vfs/vfs.h"
50
#include "../../srv/console/console.h"
51
 
52
#include "syscalls.h"
53
#include "ipcp.h"
54
#include "errors.h"
3444 svoboda 55
#include "trace.h"
3438 svoboda 56
 
57
#define THBUF_SIZE 64
3454 svoboda 58
uintptr_t thread_hash_buf[THBUF_SIZE];
59
int n_threads;
3438 svoboda 60
 
61
int next_thread_id;
62
 
63
int phoneid;
64
int abort_trace;
65
 
3454 svoboda 66
uintptr_t thash;
3438 svoboda 67
volatile int paused;
68
 
3454 svoboda 69
void thread_trace_start(uintptr_t thread_hash);
3438 svoboda 70
 
71
static proto_t *proto_console;
3444 svoboda 72
static task_id_t task_id;
3438 svoboda 73
 
3444 svoboda 74
/** Combination of events/data to print. */
75
display_mask_t display_mask;
76
 
3438 svoboda 77
static int task_connect(task_id_t task_id)
78
{
79
	int rc;
80
 
81
	rc = ipc_connect_kbox(task_id);
3439 svoboda 82
 
83
	if (rc == ENOTSUP) {
84
		printf("You do not have userspace debugging support "
85
		    "compiled in the kernel.\n");
86
		printf("Compile kernel with 'Support for userspace debuggers' "
87
		    "(CONFIG_UDEBUG) enabled.\n");
3442 svoboda 88
		return rc;
3439 svoboda 89
	}
90
 
3442 svoboda 91
	if (rc < 0) {
92
		printf("Error connecting\n");
93
		printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
94
		return rc;
95
	}
96
 
3438 svoboda 97
	phoneid = rc;
98
 
99
	rc = udebug_begin(phoneid);
3442 svoboda 100
	if (rc < 0) {
101
		printf("udebug_begin() -> %d\n", rc);
102
		return rc;
103
	}
3438 svoboda 104
 
105
	rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
3442 svoboda 106
	if (rc < 0) {
107
		printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
108
		return rc;
109
	}
3438 svoboda 110
 
111
	return 0;
112
}
113
 
114
static int get_thread_list(void)
115
{
116
	int rc;
117
	size_t tb_copied;
118
	size_t tb_needed;
119
	int i;
120
 
121
	rc = udebug_thread_read(phoneid, thread_hash_buf,
122
		THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
3442 svoboda 123
	if (rc < 0) {
124
		printf("udebug_thread_read() -> %d\n", rc);
125
		return rc;
126
	}
3438 svoboda 127
 
3454 svoboda 128
	n_threads = tb_copied / sizeof(uintptr_t);
3438 svoboda 129
 
3442 svoboda 130
	printf("Threads:");
131
	for (i = 0; i < n_threads; i++) {
3454 svoboda 132
		printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]);
3438 svoboda 133
	}
3454 svoboda 134
	printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
3438 svoboda 135
 
136
	return 0;
137
}
138
 
3455 svoboda 139
void val_print(sysarg_t val, val_type_t v_type)
3438 svoboda 140
{
3452 svoboda 141
	switch (v_type) {
142
	case V_VOID:
143
		printf("<void>");
144
		break;
145
 
146
	case V_INTEGER:
3455 svoboda 147
		printf("%ld", val);
3452 svoboda 148
		break;
149
 
150
	case V_HASH:
3455 svoboda 151
		printf("0x%08lx", val);
3452 svoboda 152
		break;
153
 
154
	case V_ERRNO:
155
		if (val >= -15 && val <= 0) {
3455 svoboda 156
			printf("%ld %s (%s)", val,
3452 svoboda 157
			    err_desc[-val].name,
158
			    err_desc[-val].desc);
3438 svoboda 159
		} else {
3455 svoboda 160
			printf("%ld", val);
3438 svoboda 161
		}
3452 svoboda 162
		break;
163
	case V_INT_ERRNO:
164
		if (val >= -15 && val < 0) {
3455 svoboda 165
			printf("%ld %s (%s)", val,
3452 svoboda 166
			    err_desc[-val].name,
167
			    err_desc[-val].desc);
3438 svoboda 168
		} else {
3455 svoboda 169
			printf("%ld", val);
3438 svoboda 170
		}
3452 svoboda 171
		break;
172
 
173
	case V_CHAR:
174
		if (val >= 0x20 && val < 0x7f) {
175
			printf("'%c'", val);
176
		} else {
177
			switch (val) {
178
			case '\a': printf("'\\a'"); break;
179
			case '\b': printf("'\\b'"); break;
180
			case '\n': printf("'\\n'"); break;
181
			case '\r': printf("'\\r'"); break;
182
			case '\t': printf("'\\t'"); break;
183
			case '\\': printf("'\\\\'"); break;
3455 svoboda 184
			default: printf("'\\x%02lX'", val); break;
3452 svoboda 185
			}
186
		}
187
		break;
3438 svoboda 188
	}
3452 svoboda 189
}
190
 
191
 
3455 svoboda 192
static void print_sc_retval(sysarg_t retval, val_type_t val_type)
3452 svoboda 193
{
194
	printf(" -> ");
195
	val_print(retval, val_type);
3438 svoboda 196
	putchar('\n');
197
}
198
 
3454 svoboda 199
static void print_sc_args(sysarg_t *sc_args, int n)
3438 svoboda 200
{
201
	int i;
202
 
203
	putchar('(');
3454 svoboda 204
	if (n > 0) printf("%ld", sc_args[0]);
3452 svoboda 205
	for (i = 1; i < n; i++) {
3454 svoboda 206
		printf(", %ld", sc_args[i]);
3438 svoboda 207
	}
208
	putchar(')');
209
}
210
 
3454 svoboda 211
static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
3438 svoboda 212
{
213
	ipc_call_t call;
3454 svoboda 214
	ipcarg_t phoneid;
3438 svoboda 215
 
216
	if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
217
		return;
218
 
219
	phoneid = sc_args[0];
220
 
221
	IPC_SET_METHOD(call, sc_args[1]);
222
	IPC_SET_ARG1(call, sc_args[2]);
223
	IPC_SET_ARG2(call, sc_args[3]);
224
	IPC_SET_ARG3(call, sc_args[4]);
225
	IPC_SET_ARG4(call, sc_args[5]);
226
	IPC_SET_ARG5(call, 0);
227
 
228
	ipcp_call_out(phoneid, &call, sc_rc);
229
}
230
 
3454 svoboda 231
static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
3438 svoboda 232
{
233
	ipc_call_t call;
234
	int rc;
235
 
236
	if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
237
		return;
238
 
239
	memset(&call, 0, sizeof(call));
240
	rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
241
 
242
	if (rc >= 0) {
243
		ipcp_call_out(sc_args[0], &call, sc_rc);
244
	}
245
}
246
 
3454 svoboda 247
static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
3438 svoboda 248
{
249
	ipc_call_t question, reply;
250
	int rc;
251
	int phoneidx;
252
 
253
//	printf("sc_ipc_call_sync_fast()\n");
254
	phoneidx = sc_args[0];
255
 
256
	IPC_SET_METHOD(question, sc_args[1]);
257
	IPC_SET_ARG1(question, sc_args[2]);
258
	IPC_SET_ARG2(question, sc_args[3]);
259
	IPC_SET_ARG3(question, sc_args[4]);
260
	IPC_SET_ARG4(question, 0);
261
	IPC_SET_ARG5(question, 0);
262
 
263
//	printf("memset\n");
264
	memset(&reply, 0, sizeof(reply));
265
//	printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
266
//		phoneid, &reply.args, sc_args[5], sizeof(reply.args));
267
	rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
268
//	printf("dmr->%d\n", rc);
269
	if (rc < 0) return;
270
 
271
//	printf("call ipc_call_sync\n");
272
	ipcp_call_sync(phoneidx, &question, &reply);
273
}
274
 
3454 svoboda 275
static void sc_ipc_call_sync_slow(sysarg_t *sc_args)
3438 svoboda 276
{
277
	ipc_call_t question, reply;
278
	int rc;
279
 
280
	memset(&question, 0, sizeof(question));
281
	rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
282
	printf("dmr->%d\n", rc);
283
	if (rc < 0) return;
284
 
285
	memset(&reply, 0, sizeof(reply));
286
	rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
287
	printf("dmr->%d\n", rc);
288
	if (rc < 0) return;
289
 
290
	ipcp_call_sync(sc_args[0], &question, &reply);
291
}
292
 
3454 svoboda 293
static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
3438 svoboda 294
{
295
	ipc_call_t call;
296
	int rc;
297
 
298
	if (sc_rc == 0) return;
299
 
300
	memset(&call, 0, sizeof(call));
301
	rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
302
//	printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
303
//		phoneid, (int)&call, sc_args[0], sizeof(call), rc);
304
 
305
	if (rc >= 0) {
306
		ipcp_call_in(&call, sc_rc);
307
	}
308
}
309
 
3454 svoboda 310
static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
311
    unsigned sc_id, sysarg_t sc_rc)
3438 svoboda 312
{
3454 svoboda 313
	sysarg_t sc_args[6];
3438 svoboda 314
	int rc;
315
 
316
	/* Read syscall arguments */
317
	rc = udebug_args_read(phoneid, thread_hash, sc_args);
318
 
319
	async_serialize_start();
320
 
321
//	printf("[%d] ", thread_id);
322
 
323
	if (rc < 0) {
324
		printf("error\n");
325
		async_serialize_end();
326
		return;
327
	}
328
 
3444 svoboda 329
	if ((display_mask & DM_SYSCALL) != 0) {
330
		/* Print syscall name and arguments */
331
		printf("%s", syscall_desc[sc_id].name);
332
		print_sc_args(sc_args, syscall_desc[sc_id].n_args);
333
	}
3438 svoboda 334
 
335
	async_serialize_end();
336
}
337
 
3454 svoboda 338
static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
339
    unsigned sc_id, sysarg_t sc_rc)
3438 svoboda 340
{
3454 svoboda 341
	sysarg_t sc_args[6];
3438 svoboda 342
	int rv_type;
343
	int rc;
344
 
345
	/* Read syscall arguments */
346
	rc = udebug_args_read(phoneid, thread_hash, sc_args);
347
 
348
	async_serialize_start();
349
 
350
//	printf("[%d] ", thread_id);
351
 
352
	if (rc < 0) {
353
		printf("error\n");
354
		async_serialize_end();
355
		return;
356
	}
357
 
3444 svoboda 358
	if ((display_mask & DM_SYSCALL) != 0) {
359
		/* Print syscall return value */
360
		rv_type = syscall_desc[sc_id].rv_type;
361
		print_sc_retval(sc_rc, rv_type);
362
	}
3438 svoboda 363
 
364
	switch (sc_id) {
365
	case SYS_IPC_CALL_ASYNC_FAST:
366
		sc_ipc_call_async_fast(sc_args, sc_rc);
367
		break;
368
	case SYS_IPC_CALL_ASYNC_SLOW:
369
		sc_ipc_call_async_slow(sc_args, sc_rc);
370
		break;
371
	case SYS_IPC_CALL_SYNC_FAST:
372
		sc_ipc_call_sync_fast(sc_args);
373
		break;
374
	case SYS_IPC_CALL_SYNC_SLOW:
375
		sc_ipc_call_sync_slow(sc_args);
376
		break;
377
	case SYS_IPC_WAIT:
378
		sc_ipc_wait(sc_args, sc_rc);
379
		break;
380
	default:
381
		break;
382
	}
383
 
384
	async_serialize_end();
385
}
386
 
3454 svoboda 387
static void event_thread_b(uintptr_t hash)
3438 svoboda 388
{
389
	async_serialize_start();
3454 svoboda 390
	printf("New thread, hash 0x%lx\n", hash);
3438 svoboda 391
	async_serialize_end();
392
 
393
	thread_trace_start(hash);
394
}
395
 
396
static int trace_loop(void *thread_hash_arg)
397
{
398
	int rc;
399
	unsigned ev_type;
3454 svoboda 400
	uintptr_t thread_hash;
3438 svoboda 401
	unsigned thread_id;
3454 svoboda 402
	sysarg_t val0, val1;
3438 svoboda 403
 
3454 svoboda 404
	thread_hash = (uintptr_t)thread_hash_arg;
3438 svoboda 405
	thread_id = next_thread_id++;
406
 
3454 svoboda 407
	printf("Start tracing thread [%d] (hash 0x%lx)\n", thread_id, thread_hash);
3438 svoboda 408
 
409
	while (!abort_trace) {
410
 
411
		/* Run thread until an event occurs */
412
		rc = udebug_go(phoneid, thread_hash,
413
		    &ev_type, &val0, &val1);
414
 
415
//		printf("rc = %d, ev_type=%d\n", rc, ev_type);
416
		if (ev_type == UDEBUG_EVENT_FINISHED) {
3442 svoboda 417
			/* Done tracing this thread */
3438 svoboda 418
			break;
419
		}
420
 
421
		if (rc >= 0) {
422
			switch (ev_type) {
423
			case UDEBUG_EVENT_SYSCALL_B:
424
				event_syscall_b(thread_id, thread_hash, val0, (int)val1);
425
				break;
426
			case UDEBUG_EVENT_SYSCALL_E:
427
				event_syscall_e(thread_id, thread_hash, val0, (int)val1);
428
				break;
429
			case UDEBUG_EVENT_STOP:
3442 svoboda 430
				printf("Stop event\n");
431
				printf("Waiting for resume\n");
3438 svoboda 432
				while (paused) {
433
					usleep(1000000);
434
					fibril_yield();
435
					printf(".");
436
				}
3442 svoboda 437
				printf("Resumed\n");
3438 svoboda 438
				break;
439
			case UDEBUG_EVENT_THREAD_B:
440
				event_thread_b(val0);
441
				break;
442
			case UDEBUG_EVENT_THREAD_E:
3454 svoboda 443
				printf("Thread 0x%lx exited\n", val0);
3438 svoboda 444
				abort_trace = 1;
445
				break;
446
			default:
3442 svoboda 447
				printf("Unknown event type %d\n", ev_type);
3438 svoboda 448
				break;
449
			}
450
		}
451
 
452
	}
453
 
3442 svoboda 454
	printf("Finished tracing thread [%d]\n", thread_id);
3438 svoboda 455
	return 0;
456
}
457
 
3454 svoboda 458
void thread_trace_start(uintptr_t thread_hash)
3438 svoboda 459
{
460
	fid_t fid;
461
 
462
	thash = thread_hash;
463
 
464
	fid = fibril_create(trace_loop, (void *)thread_hash);
465
	if (fid == 0) {
466
		printf("Warning: Failed creating fibril\n");
467
	}
468
	fibril_add_ready(fid);
469
}
470
 
471
static void trace_active_task(task_id_t task_id)
472
{
473
	int i;
474
	int rc;
475
	int c;
476
 
477
	rc = task_connect(task_id);
478
	if (rc < 0) {
479
		printf("Failed to connect to task %lld\n", task_id);
480
		return;
481
	}
482
 
483
	printf("Connected to task %lld\n", task_id);
484
 
485
	ipcp_init();
486
 
3442 svoboda 487
	/* 
488
	 * User apps now typically have console on phone 3.
489
	 * (Phones 1 and 2 are used by the loader).
490
	 */
491
	ipcp_connection_set(3, 0, proto_console);
492
 
3438 svoboda 493
	rc = get_thread_list();
494
	if (rc < 0) {
495
		printf("Failed to get thread list (error %d)\n", rc);
496
		return;
497
	}
498
 
499
	abort_trace = 0;
500
 
501
	for (i = 0; i < n_threads; i++) {
502
		thread_trace_start(thread_hash_buf[i]);
503
	}
504
 
505
	while(1) {
506
		c = getchar();
507
		if (c == 'q') break;
508
		if (c == 'p') {
509
			paused = 1;
510
			rc = udebug_stop(phoneid, thash);
511
			printf("stop -> %d\n", rc);
512
		}
513
		if (c == 'r') {
514
			paused = 0;
515
		}
516
	}
517
 
3442 svoboda 518
	printf("\nTerminate debugging session...\n");
3438 svoboda 519
	abort_trace = 1;
520
	udebug_end(phoneid);
521
	ipc_hangup(phoneid);
522
 
523
	ipcp_cleanup();
524
 
3442 svoboda 525
	printf("Done\n");
3438 svoboda 526
	return;
527
}
528
 
529
static void main_init(void)
530
{
531
	proto_t *p;
532
	oper_t *o;
533
 
3452 svoboda 534
	val_type_t arg_def[OPER_MAX_ARGS] = {
535
		V_INTEGER,
536
		V_INTEGER,
537
		V_INTEGER,
538
		V_INTEGER,
539
		V_INTEGER		
540
	};
541
 
542
	val_type_t resp_def[OPER_MAX_ARGS] = {
543
		V_INTEGER,
544
		V_INTEGER,
545
		V_INTEGER,
546
		V_INTEGER,
547
		V_INTEGER		
548
	};
549
 
3438 svoboda 550
	next_thread_id = 1;
551
	paused = 0;
552
 
553
	proto_init();
554
 
555
	p = proto_new("vfs");
3452 svoboda 556
	o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
3438 svoboda 557
	proto_add_oper(p, VFS_READ, o);
3452 svoboda 558
	o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
3438 svoboda 559
	proto_add_oper(p, VFS_WRITE, o);
3452 svoboda 560
	o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
3438 svoboda 561
	proto_add_oper(p, VFS_TRUNCATE, o);
3452 svoboda 562
	o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
3438 svoboda 563
	proto_add_oper(p, VFS_MOUNT, o);
3452 svoboda 564
/*	o = oper_new("unmount", 0, arg_def);
565
	proto_add_oper(p, VFS_UNMOUNT, o);*/
3438 svoboda 566
 
567
	proto_register(SERVICE_VFS, p);
568
 
569
	p = proto_new("console");
3452 svoboda 570
	resp_def[0] = V_CHAR;
571
	o = oper_new("getchar", 0, arg_def, V_INTEGER, 2, resp_def);
3438 svoboda 572
	proto_add_oper(p, CONSOLE_GETCHAR, o);
3452 svoboda 573
 
574
	arg_def[0] = V_CHAR;
575
	o = oper_new("putchar", 1, arg_def, V_VOID, 0, resp_def);
3438 svoboda 576
	proto_add_oper(p, CONSOLE_PUTCHAR, o);
3452 svoboda 577
	o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
3438 svoboda 578
	proto_add_oper(p, CONSOLE_CLEAR, o);
3452 svoboda 579
 
580
	arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
581
	o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
3438 svoboda 582
	proto_add_oper(p, CONSOLE_GOTO, o);
3452 svoboda 583
 
584
	resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
585
	o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
3438 svoboda 586
	proto_add_oper(p, CONSOLE_GETSIZE, o);
3452 svoboda 587
	o = oper_new("flush", 0, arg_def, V_VOID, 0, resp_def);
3438 svoboda 588
	proto_add_oper(p, CONSOLE_FLUSH, o);
3452 svoboda 589
 
590
	arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
591
	o = oper_new("set_style", 2, arg_def, V_INTEGER, 0, resp_def);
3438 svoboda 592
	proto_add_oper(p, CONSOLE_SET_STYLE, o);
3452 svoboda 593
	o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
3438 svoboda 594
	proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
595
 
596
	proto_console = p;
597
	proto_register(SERVICE_CONSOLE, p);
598
}
599
 
600
static void print_syntax()
601
{
3447 svoboda 602
	printf("Syntax:\n");
603
	printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
604
	printf("or\ttrace [+<events>] -t <task_id>\n");
3444 svoboda 605
	printf("Events: (default is +tp)\n");
606
	printf("\n");
607
	printf("\tt ... Thread creation and termination\n");
608
	printf("\ts ... System calls\n");
609
	printf("\ti ... Low-level IPC\n");
610
	printf("\tp ... Protocol level\n");
611
	printf("\n");
3447 svoboda 612
	printf("Examples:\n");
613
	printf("\ttrace +s /app/tetris\n");
614
	printf("\ttrace +tsip -t 12\n");
3438 svoboda 615
}
616
 
3444 svoboda 617
static display_mask_t parse_display_mask(char *text)
3438 svoboda 618
{
3444 svoboda 619
	display_mask_t dm;
620
	char *c;
621
 
622
	c = text;
623
 
624
	while (*c) {
625
		switch (*c) {
626
		case 't': dm = dm | DM_THREAD; break;
627
		case 's': dm = dm | DM_SYSCALL; break;
628
		case 'i': dm = dm | DM_IPC; break;
629
		case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
630
		default:
631
			printf("Unexpected event type '%c'\n", *c);
632
			exit(1);
633
		}
634
 
635
		++c;
636
	}
637
 
638
	return dm;
639
}
640
 
641
static int parse_args(int argc, char *argv[])
642
{
643
	char *arg;
3438 svoboda 644
	char *err_p;
645
 
3447 svoboda 646
	task_id = 0;
647
 
3444 svoboda 648
	--argc; ++argv;
649
 
3447 svoboda 650
	while (argc > 0) {
3444 svoboda 651
		arg = *argv;
652
		if (arg[0] == '+') {
653
			display_mask = parse_display_mask(&arg[1]);
3447 svoboda 654
		} else if (arg[0] == '-') {
655
			if (arg[1] == 't') {
656
				/* Trace an already running task */
657
				--argc; ++argv;
658
				task_id = strtol(*argv, &err_p, 10);
659
				if (*err_p) {
660
					printf("Task ID syntax error\n");
661
					print_syntax();
662
					return -1;
663
				}
664
			} else {
665
				printf("Uknown option '%s'\n", arg[0]);
666
				print_syntax();
667
				return -1;
668
			}
3444 svoboda 669
		} else {
3447 svoboda 670
			break;
3444 svoboda 671
		}
672
 
673
		--argc; ++argv;
674
	}
675
 
3447 svoboda 676
	if (task_id != 0) {
3454 svoboda 677
		if (argc == 0) return 0;
3447 svoboda 678
		printf("Extra arguments\n");
3438 svoboda 679
		print_syntax();
3447 svoboda 680
		return -1;
3438 svoboda 681
	}
682
 
3447 svoboda 683
	if (argc < 1) {
684
		printf("Missing argument\n");
3438 svoboda 685
		print_syntax();
3444 svoboda 686
		return -1;
3438 svoboda 687
	}
688
 
3447 svoboda 689
	/* Execute the specified command and trace the new task. */
690
	printf("Spawning '%s' with arguments:\n", *argv);
691
	{
692
		char **cp = argv;
693
		while (*cp) printf("'%s'\n", *cp++);
694
	}
695
	task_id = task_spawn(*argv, argv);
696
 
3444 svoboda 697
	return 0;
698
}
699
 
700
int main(int argc, char *argv[])
701
{
702
	printf("System Call / IPC Tracer\n");
703
 
704
	display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
705
 
706
	if (parse_args(argc, argv) < 0)
707
		return 1;
708
 
3438 svoboda 709
	main_init();
710
	trace_active_task(task_id);
3444 svoboda 711
 
712
	return 0;
3438 svoboda 713
}
714
 
715
/** @}
716
 */