Subversion Repositories HelenOS

Rev

Rev 3439 | Rev 3444 | 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>
44
 
45
// Temporary: service and method names
46
#include "proto.h"
47
#include <ipc/services.h>
48
#include "../../srv/vfs/vfs.h"
49
#include "../../srv/console/console.h"
50
 
51
#include "syscalls.h"
52
#include "ipcp.h"
53
#include "errors.h"
54
 
55
#define THBUF_SIZE 64
56
unsigned thread_hash_buf[THBUF_SIZE];
57
unsigned n_threads;
58
 
59
int next_thread_id;
60
 
61
int phoneid;
62
int abort_trace;
63
 
64
unsigned thash;
65
volatile int paused;
66
 
67
void thread_trace_start(unsigned thread_hash);
68
 
69
static proto_t *proto_console;
70
 
71
static int task_connect(task_id_t task_id)
72
{
73
	int rc;
74
 
75
	rc = ipc_connect_kbox(task_id);
3439 svoboda 76
 
77
	if (rc == ENOTSUP) {
78
		printf("You do not have userspace debugging support "
79
		    "compiled in the kernel.\n");
80
		printf("Compile kernel with 'Support for userspace debuggers' "
81
		    "(CONFIG_UDEBUG) enabled.\n");
3442 svoboda 82
		return rc;
3439 svoboda 83
	}
84
 
3442 svoboda 85
	if (rc < 0) {
86
		printf("Error connecting\n");
87
		printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
88
		return rc;
89
	}
90
 
3438 svoboda 91
	phoneid = rc;
92
 
93
	rc = udebug_begin(phoneid);
3442 svoboda 94
	if (rc < 0) {
95
		printf("udebug_begin() -> %d\n", rc);
96
		return rc;
97
	}
3438 svoboda 98
 
99
	rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
3442 svoboda 100
	if (rc < 0) {
101
		printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
102
		return rc;
103
	}
3438 svoboda 104
 
105
	return 0;
106
}
107
 
108
static int get_thread_list(void)
109
{
110
	int rc;
111
	size_t tb_copied;
112
	size_t tb_needed;
113
	int i;
114
 
115
	rc = udebug_thread_read(phoneid, thread_hash_buf,
116
		THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
3442 svoboda 117
	if (rc < 0) {
118
		printf("udebug_thread_read() -> %d\n", rc);
119
		return rc;
120
	}
3438 svoboda 121
 
122
	n_threads = tb_copied / sizeof(unsigned);
123
 
3442 svoboda 124
	printf("Threads:");
125
	for (i = 0; i < n_threads; i++) {
126
		printf(" [%d] (hash 0x%u)", 1+i, thread_hash_buf[i]);
3438 svoboda 127
	}
128
	printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
129
 
130
	return 0;
131
}
132
 
133
static void print_sc_retval(int retval, rv_type_t rv_type)
134
{
135
	printf (" -> ");
136
	if (rv_type == RV_INTEGER) {
137
		printf("%d", retval);
138
	} else if (rv_type == RV_HASH) {
139
		printf("0x%08x", retval);
140
	} else if (rv_type == RV_ERRNO) {
141
		if (retval >= -15 && retval <= 0) {
142
			printf("%d %s (%s)", retval,
143
			    err_desc[retval].name,
144
			    err_desc[retval].desc);
145
		} else {
146
			printf("%d", retval);
147
		}
148
	} else if (rv_type == RV_INT_ERRNO) {
149
		if (retval >= -15 && retval < 0) {
150
			printf("%d %s (%s)", retval,
151
			    err_desc[retval].name,
152
			    err_desc[retval].desc);
153
		} else {
154
			printf("%d", retval);
155
		}
156
	}
157
	putchar('\n');
158
}
159
 
160
static void print_sc_args(unsigned *sc_args, int n)
161
{
162
	int i;
163
 
164
	putchar('(');
165
	if (n > 0) printf("%d", sc_args[0]);
166
	for (i=1; i<n; i++) {
167
		printf(", %d", sc_args[i]);
168
	}
169
	putchar(')');
170
}
171
 
172
static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
173
{
174
	ipc_call_t call;
175
	int phoneid;
176
 
177
	if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
178
		return;
179
 
180
	phoneid = sc_args[0];
181
 
182
	IPC_SET_METHOD(call, sc_args[1]);
183
	IPC_SET_ARG1(call, sc_args[2]);
184
	IPC_SET_ARG2(call, sc_args[3]);
185
	IPC_SET_ARG3(call, sc_args[4]);
186
	IPC_SET_ARG4(call, sc_args[5]);
187
	IPC_SET_ARG5(call, 0);
188
 
189
	ipcp_call_out(phoneid, &call, sc_rc);
190
}
191
 
192
static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
193
{
194
	ipc_call_t call;
195
	int rc;
196
 
197
	if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
198
		return;
199
 
200
	memset(&call, 0, sizeof(call));
201
	rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
202
 
203
	if (rc >= 0) {
204
		ipcp_call_out(sc_args[0], &call, sc_rc);
205
	}
206
}
207
 
208
static void sc_ipc_call_sync_fast(unsigned *sc_args)
209
{
210
	ipc_call_t question, reply;
211
	int rc;
212
	int phoneidx;
213
 
214
//	printf("sc_ipc_call_sync_fast()\n");
215
	phoneidx = sc_args[0];
216
 
217
	IPC_SET_METHOD(question, sc_args[1]);
218
	IPC_SET_ARG1(question, sc_args[2]);
219
	IPC_SET_ARG2(question, sc_args[3]);
220
	IPC_SET_ARG3(question, sc_args[4]);
221
	IPC_SET_ARG4(question, 0);
222
	IPC_SET_ARG5(question, 0);
223
 
224
//	printf("memset\n");
225
	memset(&reply, 0, sizeof(reply));
226
//	printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
227
//		phoneid, &reply.args, sc_args[5], sizeof(reply.args));
228
	rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
229
//	printf("dmr->%d\n", rc);
230
	if (rc < 0) return;
231
 
232
//	printf("call ipc_call_sync\n");
233
	ipcp_call_sync(phoneidx, &question, &reply);
234
}
235
 
236
static void sc_ipc_call_sync_slow(unsigned *sc_args)
237
{
238
	ipc_call_t question, reply;
239
	int rc;
240
 
241
	memset(&question, 0, sizeof(question));
242
	rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
243
	printf("dmr->%d\n", rc);
244
	if (rc < 0) return;
245
 
246
	memset(&reply, 0, sizeof(reply));
247
	rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
248
	printf("dmr->%d\n", rc);
249
	if (rc < 0) return;
250
 
251
	ipcp_call_sync(sc_args[0], &question, &reply);
252
}
253
 
254
static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
255
{
256
	ipc_call_t call;
257
	int rc;
258
 
259
	if (sc_rc == 0) return;
260
 
261
	memset(&call, 0, sizeof(call));
262
	rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
263
//	printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
264
//		phoneid, (int)&call, sc_args[0], sizeof(call), rc);
265
 
266
	if (rc >= 0) {
267
		ipcp_call_in(&call, sc_rc);
268
	}
269
}
270
 
271
static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
272
{
273
	unsigned sc_args[6];
274
	int rc;
275
 
276
	/* Read syscall arguments */
277
	rc = udebug_args_read(phoneid, thread_hash, sc_args);
278
 
279
	async_serialize_start();
280
 
281
//	printf("[%d] ", thread_id);
282
 
283
	if (rc < 0) {
284
		printf("error\n");
285
		async_serialize_end();
286
		return;
287
	}
288
 
289
	/* Print syscall name, id and arguments */
290
	printf("%s", syscall_desc[sc_id].name);
291
	print_sc_args(sc_args, syscall_desc[sc_id].n_args);
292
 
293
	async_serialize_end();
294
}
295
 
296
static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
297
{
298
	unsigned sc_args[6];
299
	int rv_type;
300
	int rc;
301
 
302
	/* Read syscall arguments */
303
	rc = udebug_args_read(phoneid, thread_hash, sc_args);
304
 
305
	async_serialize_start();
306
 
307
//	printf("[%d] ", thread_id);
308
 
309
	if (rc < 0) {
310
		printf("error\n");
311
		async_serialize_end();
312
		return;
313
	}
314
 
315
	rv_type = syscall_desc[sc_id].rv_type;
316
	print_sc_retval(sc_rc, rv_type);
317
 
318
	switch (sc_id) {
319
	case SYS_IPC_CALL_ASYNC_FAST:
320
		sc_ipc_call_async_fast(sc_args, sc_rc);
321
		break;
322
	case SYS_IPC_CALL_ASYNC_SLOW:
323
		sc_ipc_call_async_slow(sc_args, sc_rc);
324
		break;
325
	case SYS_IPC_CALL_SYNC_FAST:
326
		sc_ipc_call_sync_fast(sc_args);
327
		break;
328
	case SYS_IPC_CALL_SYNC_SLOW:
329
		sc_ipc_call_sync_slow(sc_args);
330
		break;
331
	case SYS_IPC_WAIT:
332
		sc_ipc_wait(sc_args, sc_rc);
333
		break;
334
	default:
335
		break;
336
	}
337
 
338
	async_serialize_end();
339
}
340
 
341
static void event_thread_b(unsigned hash)
342
{
343
	async_serialize_start();
3442 svoboda 344
	printf("New thread, hash 0x%x\n", hash);
3438 svoboda 345
	async_serialize_end();
346
 
347
	thread_trace_start(hash);
348
}
349
 
350
static int trace_loop(void *thread_hash_arg)
351
{
352
	int rc;
353
	unsigned ev_type;
354
	unsigned thread_hash;
355
	unsigned thread_id;
356
	unsigned val0, val1;
357
 
358
	thread_hash = (unsigned)thread_hash_arg;
359
	thread_id = next_thread_id++;
360
 
3442 svoboda 361
	printf("Start tracing thread [%d] (hash 0x%x)\n", thread_id, thread_hash);
3438 svoboda 362
 
363
	while (!abort_trace) {
364
 
365
		/* Run thread until an event occurs */
366
		rc = udebug_go(phoneid, thread_hash,
367
		    &ev_type, &val0, &val1);
368
 
369
//		printf("rc = %d, ev_type=%d\n", rc, ev_type);
370
		if (ev_type == UDEBUG_EVENT_FINISHED) {
3442 svoboda 371
			/* Done tracing this thread */
3438 svoboda 372
			break;
373
		}
374
 
375
		if (rc >= 0) {
376
			switch (ev_type) {
377
			case UDEBUG_EVENT_SYSCALL_B:
378
				event_syscall_b(thread_id, thread_hash, val0, (int)val1);
379
				break;
380
			case UDEBUG_EVENT_SYSCALL_E:
381
				event_syscall_e(thread_id, thread_hash, val0, (int)val1);
382
				break;
383
			case UDEBUG_EVENT_STOP:
3442 svoboda 384
				printf("Stop event\n");
385
				printf("Waiting for resume\n");
3438 svoboda 386
				while (paused) {
387
					usleep(1000000);
388
					fibril_yield();
389
					printf(".");
390
				}
3442 svoboda 391
				printf("Resumed\n");
3438 svoboda 392
				break;
393
			case UDEBUG_EVENT_THREAD_B:
394
				event_thread_b(val0);
395
				break;
396
			case UDEBUG_EVENT_THREAD_E:
3442 svoboda 397
				printf("Thread 0x%x exited\n", val0);
3438 svoboda 398
				abort_trace = 1;
399
				break;
400
			default:
3442 svoboda 401
				printf("Unknown event type %d\n", ev_type);
3438 svoboda 402
				break;
403
			}
404
		}
405
 
406
	}
407
 
3442 svoboda 408
	printf("Finished tracing thread [%d]\n", thread_id);
3438 svoboda 409
	return 0;
410
}
411
 
412
void thread_trace_start(unsigned thread_hash)
413
{
414
	fid_t fid;
415
 
416
	thash = thread_hash;
417
 
418
	fid = fibril_create(trace_loop, (void *)thread_hash);
419
	if (fid == 0) {
420
		printf("Warning: Failed creating fibril\n");
421
	}
422
	fibril_add_ready(fid);
423
}
424
 
425
static void trace_active_task(task_id_t task_id)
426
{
427
	int i;
428
	int rc;
429
	int c;
430
 
431
	printf("Syscall Tracer\n");
432
 
433
	rc = task_connect(task_id);
434
	if (rc < 0) {
435
		printf("Failed to connect to task %lld\n", task_id);
436
		return;
437
	}
438
 
439
	printf("Connected to task %lld\n", task_id);
440
 
441
	ipcp_init();
442
 
3442 svoboda 443
	/* 
444
	 * User apps now typically have console on phone 3.
445
	 * (Phones 1 and 2 are used by the loader).
446
	 */
447
	ipcp_connection_set(3, 0, proto_console);
448
 
3438 svoboda 449
	rc = get_thread_list();
450
	if (rc < 0) {
451
		printf("Failed to get thread list (error %d)\n", rc);
452
		return;
453
	}
454
 
455
	abort_trace = 0;
456
 
457
	for (i = 0; i < n_threads; i++) {
458
		thread_trace_start(thread_hash_buf[i]);
459
	}
460
 
461
	while(1) {
462
		c = getchar();
463
		if (c == 'q') break;
464
		if (c == 'p') {
465
			paused = 1;
466
			rc = udebug_stop(phoneid, thash);
467
			printf("stop -> %d\n", rc);
468
		}
469
		if (c == 'r') {
470
			paused = 0;
471
		}
472
	}
473
 
3442 svoboda 474
	printf("\nTerminate debugging session...\n");
3438 svoboda 475
	abort_trace = 1;
476
	udebug_end(phoneid);
477
	ipc_hangup(phoneid);
478
 
479
	ipcp_cleanup();
480
 
3442 svoboda 481
	printf("Done\n");
3438 svoboda 482
	return;
483
}
484
 
485
static void main_init(void)
486
{
487
	proto_t *p;
488
	oper_t *o;
489
 
490
	next_thread_id = 1;
491
	paused = 0;
492
 
493
	proto_init();
494
 
495
	p = proto_new("vfs");
496
	o = oper_new("read");
497
	proto_add_oper(p, VFS_READ, o);
498
	o = oper_new("write");
499
	proto_add_oper(p, VFS_WRITE, o);
500
	o = oper_new("truncate");
501
	proto_add_oper(p, VFS_TRUNCATE, o);
502
	o = oper_new("mount");
503
	proto_add_oper(p, VFS_MOUNT, o);
504
	o = oper_new("unmount");
505
	proto_add_oper(p, VFS_UNMOUNT, o);
506
 
507
	proto_register(SERVICE_VFS, p);
508
 
509
	p = proto_new("console");
510
	o = oper_new("getchar");
511
	proto_add_oper(p, CONSOLE_GETCHAR, o);
512
	o = oper_new("putchar");
513
	proto_add_oper(p, CONSOLE_PUTCHAR, o);
514
	o = oper_new("clear");
515
	proto_add_oper(p, CONSOLE_CLEAR, o);
516
	o = oper_new("goto");
517
	proto_add_oper(p, CONSOLE_GOTO, o);
518
	o = oper_new("getsize");
519
	proto_add_oper(p, CONSOLE_GETSIZE, o);
520
	o = oper_new("flush");
521
	proto_add_oper(p, CONSOLE_FLUSH, o);
522
	o = oper_new("set_style");
523
	proto_add_oper(p, CONSOLE_SET_STYLE, o);
524
	o = oper_new("cursor_visibility");
525
	proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
526
	o = oper_new("flush");
527
	proto_add_oper(p, CONSOLE_FLUSH, o);
528
 
529
	proto_console = p;
530
	proto_register(SERVICE_CONSOLE, p);
531
}
532
 
533
static void print_syntax()
534
{
3442 svoboda 535
	printf("Syntax: trace <task_id>\n");
3438 svoboda 536
}
537
 
538
int main(int argc, char *argv[])
539
{
540
	task_id_t task_id;
541
	char *err_p;
542
 
543
	if (argc != 2) {
544
		printf("Mising argument\n");
545
		print_syntax();
546
		return 1;
547
	}
548
 
549
	task_id = strtol(argv[1], &err_p, 10);
550
 
551
	if (*err_p) {
552
		printf("Task ID syntax error\n");
553
		print_syntax();
554
		return 1;
555
	}
556
 
557
	main_init();
558
	trace_active_task(task_id);
559
}
560
 
561
/** @}
562
 */