source: mainline/uspace/app/trace/trace.c@ 9a1b20c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9a1b20c was 9a1b20c, checked in by Jiri Svoboda <jirik.svoboda@…>, 17 years ago

Merge syscall tracer (trace) and relevant part of udebug interface from tracing to trunk.

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