Rev 2882 | Rev 2894 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2832 | svoboda | 1 | /** @addtogroup sctrace |
2 | * @{ |
||
3 | */ |
||
4 | /** @file |
||
5 | */ |
||
6 | |||
7 | #include <stdio.h> |
||
2878 | svoboda | 8 | #include <stdlib.h> |
2832 | svoboda | 9 | #include <unistd.h> |
10 | #include <syscall.h> |
||
11 | #include <ipc/ipc.h> |
||
2835 | svoboda | 12 | #include <fibril.h> |
13 | #include <errno.h> |
||
2838 | svoboda | 14 | #include <udebug.h> |
2862 | svoboda | 15 | #include <async.h> |
2832 | svoboda | 16 | |
2880 | svoboda | 17 | // Temporary: service and method names |
2878 | svoboda | 18 | #include "proto.h" |
19 | #include <ipc/services.h> |
||
2880 | svoboda | 20 | #include "../../srv/vfs/vfs.h" |
2882 | svoboda | 21 | #include "../../srv/console/console.h" |
2878 | svoboda | 22 | |
2832 | svoboda | 23 | #include "syscalls.h" |
2874 | svoboda | 24 | #include "ipcp.h" |
2832 | svoboda | 25 | #include "errors.h" |
26 | #include "debug_api.h" |
||
27 | |||
2868 | svoboda | 28 | #define THBUF_SIZE 64 |
29 | unsigned thread_hash_buf[THBUF_SIZE]; |
||
2850 | svoboda | 30 | unsigned n_threads; |
2832 | svoboda | 31 | |
2868 | svoboda | 32 | int next_thread_id; |
33 | |||
2832 | svoboda | 34 | int phoneid; |
2835 | svoboda | 35 | int abort_trace; |
2832 | svoboda | 36 | |
2868 | svoboda | 37 | void thread_trace_start(unsigned thread_hash); |
38 | |||
2882 | svoboda | 39 | static proto_t *proto_console; |
2868 | svoboda | 40 | |
2832 | svoboda | 41 | int task_connect(int taskid) |
42 | { |
||
43 | int rc; |
||
44 | |||
45 | printf("ipc_connect_task(%d)...\n", taskid); |
||
46 | rc = ipc_connect_kbox(taskid); |
||
47 | printf("-> %d\n", rc); |
||
48 | phoneid = rc; |
||
49 | if (rc < 0) return rc; |
||
50 | |||
51 | printf("debug_begin()\n"); |
||
52 | rc = debug_begin(phoneid); |
||
53 | printf("-> %d\n", rc); |
||
54 | if (rc < 0) return rc; |
||
55 | |||
56 | return 0; |
||
57 | } |
||
58 | |||
59 | int get_thread_list(void) |
||
60 | { |
||
61 | int rc; |
||
62 | int tb_copied; |
||
63 | int tb_needed; |
||
64 | int i; |
||
65 | |||
66 | |||
67 | printf("send IPC_M_DEBUG_THREAD_READ message\n"); |
||
2868 | svoboda | 68 | rc = debug_thread_read(phoneid, (unsigned)thread_hash_buf, |
69 | THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed); |
||
2832 | svoboda | 70 | printf("-> %d\n", rc); |
71 | if (rc < 0) return rc; |
||
72 | |||
2850 | svoboda | 73 | n_threads = tb_copied / sizeof(unsigned); |
74 | |||
2832 | svoboda | 75 | printf("thread IDs:"); |
2850 | svoboda | 76 | for (i=0; i<n_threads; i++) { |
2868 | svoboda | 77 | printf(" %u", thread_hash_buf[i]); |
2832 | svoboda | 78 | } |
79 | printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned)); |
||
80 | |||
81 | return 0; |
||
82 | } |
||
83 | |||
84 | void print_sc_retval(int retval, rv_type_t rv_type) |
||
85 | { |
||
86 | printf (" -> "); |
||
87 | if (rv_type == RV_INTEGER) { |
||
88 | printf("%d", retval); |
||
89 | } else if (rv_type == RV_HASH) { |
||
90 | printf("0x%08x", retval); |
||
91 | } else if (rv_type == RV_ERRNO) { |
||
92 | if (retval >= -15 && retval <= 0) { |
||
93 | printf("%d %s (%s)", retval, |
||
94 | err_desc[retval].name, |
||
95 | err_desc[retval].desc); |
||
96 | } else { |
||
97 | printf("%d", retval); |
||
98 | } |
||
99 | } else if (rv_type == RV_INT_ERRNO) { |
||
100 | if (retval >= -15 && retval < 0) { |
||
101 | printf("%d %s (%s)", retval, |
||
102 | err_desc[retval].name, |
||
103 | err_desc[retval].desc); |
||
104 | } else { |
||
105 | printf("%d", retval); |
||
106 | } |
||
107 | } |
||
108 | putchar('\n'); |
||
109 | } |
||
110 | |||
111 | void print_sc_args(unsigned *sc_args, int n) |
||
112 | { |
||
113 | int i; |
||
114 | |||
115 | putchar('('); |
||
116 | if (n > 0) printf("%d", sc_args[0]); |
||
117 | for (i=1; i<n; i++) { |
||
118 | printf(", %d", sc_args[i]); |
||
119 | } |
||
120 | putchar(')'); |
||
121 | } |
||
122 | |||
2872 | svoboda | 123 | void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc) |
2871 | svoboda | 124 | { |
125 | ipc_call_t call; |
||
126 | int phoneid; |
||
2872 | svoboda | 127 | |
128 | if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY) |
||
129 | return; |
||
2871 | svoboda | 130 | |
131 | phoneid = sc_args[0]; |
||
132 | |||
133 | IPC_SET_METHOD(call, sc_args[1]); |
||
134 | IPC_SET_ARG1(call, sc_args[2]); |
||
135 | IPC_SET_ARG2(call, sc_args[3]); |
||
136 | IPC_SET_ARG3(call, sc_args[4]); |
||
137 | IPC_SET_ARG4(call, sc_args[5]); |
||
138 | IPC_SET_ARG5(call, 0); |
||
139 | |||
2874 | svoboda | 140 | ipcp_call_out(phoneid, &call, sc_rc); |
2871 | svoboda | 141 | } |
142 | |||
2872 | svoboda | 143 | void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc) |
2832 | svoboda | 144 | { |
2871 | svoboda | 145 | ipc_call_t call; |
2832 | svoboda | 146 | int rc; |
147 | |||
2872 | svoboda | 148 | if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY) |
149 | return; |
||
150 | |||
2871 | svoboda | 151 | memset(&call, 0, sizeof(call)); |
152 | rc = debug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args)); |
||
2832 | svoboda | 153 | |
154 | if (rc >= 0) { |
||
2874 | svoboda | 155 | ipcp_call_out(sc_args[0], &call, sc_rc); |
2832 | svoboda | 156 | } |
157 | } |
||
158 | |||
2872 | svoboda | 159 | void sc_ipc_call_sync_fast(unsigned *sc_args) |
160 | { |
||
161 | ipc_call_t question, reply; |
||
162 | int rc; |
||
163 | int phoneidx; |
||
164 | |||
2877 | svoboda | 165 | // printf("sc_ipc_call_sync_fast()\n"); |
2872 | svoboda | 166 | phoneidx = sc_args[0]; |
167 | |||
168 | IPC_SET_METHOD(question, sc_args[1]); |
||
169 | IPC_SET_ARG1(question, sc_args[2]); |
||
170 | IPC_SET_ARG2(question, sc_args[3]); |
||
171 | IPC_SET_ARG3(question, sc_args[4]); |
||
172 | IPC_SET_ARG4(question, 0); |
||
173 | IPC_SET_ARG5(question, 0); |
||
174 | |||
2877 | svoboda | 175 | // printf("memset\n"); |
2872 | svoboda | 176 | memset(&reply, 0, sizeof(reply)); |
2877 | svoboda | 177 | // printf("debug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n", |
178 | // phoneid, &reply.args, sc_args[5], sizeof(reply.args)); |
||
2872 | svoboda | 179 | rc = debug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args)); |
2877 | svoboda | 180 | // printf("dmr->%d\n", rc); |
2872 | svoboda | 181 | if (rc < 0) return; |
182 | |||
2877 | svoboda | 183 | // printf("call ipc_call_sync\n"); |
2874 | svoboda | 184 | ipcp_call_sync(phoneidx, &question, &reply); |
2872 | svoboda | 185 | } |
186 | |||
187 | void sc_ipc_call_sync_slow(unsigned *sc_args) |
||
188 | { |
||
189 | ipc_call_t question, reply; |
||
190 | int rc; |
||
191 | |||
192 | memset(&question, 0, sizeof(question)); |
||
193 | rc = debug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args)); |
||
194 | printf("dmr->%d\n", rc); |
||
195 | if (rc < 0) return; |
||
196 | |||
197 | memset(&reply, 0, sizeof(reply)); |
||
198 | rc = debug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args)); |
||
199 | printf("dmr->%d\n", rc); |
||
200 | if (rc < 0) return; |
||
201 | |||
2874 | svoboda | 202 | ipcp_call_sync(sc_args[0], &question, &reply); |
2872 | svoboda | 203 | } |
204 | |||
2871 | svoboda | 205 | void sc_ipc_wait(unsigned *sc_args, int sc_rc) |
206 | { |
||
207 | ipc_call_t call; |
||
208 | int rc; |
||
209 | |||
210 | if (sc_rc == 0) return 0; |
||
211 | |||
212 | memset(&call, 0, sizeof(call)); |
||
213 | rc = debug_mem_read(phoneid, &call, sc_args[0], sizeof(call)); |
||
2872 | svoboda | 214 | // printf("debug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n", |
215 | // phoneid, (int)&call, sc_args[0], sizeof(call), rc); |
||
2871 | svoboda | 216 | |
217 | if (rc >= 0) { |
||
2874 | svoboda | 218 | ipcp_call_in(&call, sc_rc); |
2871 | svoboda | 219 | } |
220 | } |
||
221 | |||
2868 | svoboda | 222 | void event_syscall(unsigned thread_id, unsigned thread_hash, unsigned sc_id, int sc_rc) |
2867 | svoboda | 223 | { |
224 | unsigned sc_args[6]; |
||
225 | int rv_type; |
||
226 | int rc; |
||
227 | |||
228 | /* Read syscall arguments */ |
||
2868 | svoboda | 229 | rc = debug_args_read(phoneid, thread_hash, sc_args); |
2867 | svoboda | 230 | |
231 | async_serialize_start(); |
||
232 | |||
2872 | svoboda | 233 | // printf("[%d] ", thread_id); |
2867 | svoboda | 234 | |
235 | if (rc < 0) { |
||
236 | printf("error\n"); |
||
237 | async_serialize_end(); |
||
238 | return; |
||
239 | } |
||
240 | |||
241 | /* Print syscall name, id and arguments */ |
||
242 | printf("%s", syscall_desc[sc_id].name); |
||
243 | print_sc_args(sc_args, syscall_desc[sc_id].n_args); |
||
244 | rv_type = syscall_desc[sc_id].rv_type; |
||
245 | print_sc_retval(sc_rc, rv_type); |
||
246 | |||
247 | switch (sc_id) { |
||
2871 | svoboda | 248 | case SYS_IPC_CALL_ASYNC_FAST: |
2872 | svoboda | 249 | sc_ipc_call_async_fast(sc_args, sc_rc); |
2871 | svoboda | 250 | break; |
2867 | svoboda | 251 | case SYS_IPC_CALL_ASYNC_SLOW: |
2872 | svoboda | 252 | sc_ipc_call_async_slow(sc_args, sc_rc); |
2867 | svoboda | 253 | break; |
2872 | svoboda | 254 | case SYS_IPC_CALL_SYNC_FAST: |
255 | sc_ipc_call_sync_fast(sc_args); |
||
256 | break; |
||
257 | case SYS_IPC_CALL_SYNC_SLOW: |
||
258 | sc_ipc_call_sync_slow(sc_args); |
||
259 | break; |
||
2871 | svoboda | 260 | case SYS_IPC_WAIT: |
261 | sc_ipc_wait(sc_args, sc_rc); |
||
262 | break; |
||
2867 | svoboda | 263 | default: |
264 | break; |
||
265 | } |
||
266 | |||
267 | async_serialize_end(); |
||
268 | } |
||
269 | |||
270 | void event_new_thread(unsigned hash) |
||
271 | { |
||
272 | async_serialize_start(); |
||
273 | printf("new thread, hash 0x%x\n", hash); |
||
274 | async_serialize_end(); |
||
2868 | svoboda | 275 | |
276 | thread_trace_start(hash); |
||
2867 | svoboda | 277 | } |
278 | |||
2868 | svoboda | 279 | void trace_loop(void *thread_hash_arg) |
2835 | svoboda | 280 | { |
2832 | svoboda | 281 | int rc; |
282 | unsigned ev_type; |
||
2868 | svoboda | 283 | unsigned thread_hash; |
284 | unsigned thread_id; |
||
2867 | svoboda | 285 | unsigned val0, val1; |
2832 | svoboda | 286 | |
2868 | svoboda | 287 | thread_hash = (unsigned)thread_hash_arg; |
288 | thread_id = next_thread_id++; |
||
2835 | svoboda | 289 | |
2868 | svoboda | 290 | printf("trace_loop(%d)\n", thread_id); |
291 | |||
2835 | svoboda | 292 | while (!abort_trace) { |
293 | |||
2867 | svoboda | 294 | /* Run thread until an event occurs */ |
2868 | svoboda | 295 | rc = debug_go(phoneid, thread_hash, |
2867 | svoboda | 296 | &ev_type, &val0, &val1); |
2832 | svoboda | 297 | |
2872 | svoboda | 298 | // printf("rc = %d, ev_type=%d\n", rc, ev_type); |
2838 | svoboda | 299 | if (ev_type == UDEBUG_EVENT_FINISHED) { |
2868 | svoboda | 300 | printf("thread %u debugging finished\n", thread_id); |
2838 | svoboda | 301 | break; |
302 | } |
||
303 | |||
2832 | svoboda | 304 | if (rc >= 0) { |
2867 | svoboda | 305 | switch (ev_type) { |
306 | case UDEBUG_EVENT_SYSCALL: |
||
2868 | svoboda | 307 | event_syscall(thread_id, thread_hash, val0, (int)val1); |
2867 | svoboda | 308 | break; |
309 | case UDEBUG_EVENT_NEW_THREAD: |
||
310 | event_new_thread(val0); |
||
311 | break; |
||
312 | default: |
||
313 | printf("unknown event type %d\n", ev_type); |
||
314 | break; |
||
315 | } |
||
2832 | svoboda | 316 | } |
317 | |||
318 | } |
||
2835 | svoboda | 319 | |
2868 | svoboda | 320 | printf("trace_loop(%d) exiting\n", thread_id); |
2832 | svoboda | 321 | } |
322 | |||
2868 | svoboda | 323 | void thread_trace_start(unsigned thread_hash) |
324 | { |
||
325 | fid_t fid; |
||
2832 | svoboda | 326 | |
2868 | svoboda | 327 | fid = fibril_create(trace_loop, (void *)thread_hash); |
328 | if (fid == 0) { |
||
329 | printf("Warning: Failed creating fibril\n"); |
||
330 | } |
||
331 | fibril_add_ready(fid); |
||
332 | } |
||
333 | |||
2832 | svoboda | 334 | void trace_active_task(void) |
335 | { |
||
336 | int taskid; |
||
337 | int i; |
||
338 | int rc; |
||
339 | |||
340 | printf("Syscall Tracer\n"); |
||
341 | printf("Press 'c' to connect\n"); |
||
342 | while ((i = getchar()) != 'c') |
||
343 | putchar(i); |
||
344 | |||
2850 | svoboda | 345 | taskid = 14; |
2832 | svoboda | 346 | rc = task_connect(taskid); |
347 | if (rc < 0) { |
||
348 | printf("Failed to connect to task %d\n", taskid); |
||
349 | return; |
||
350 | } |
||
351 | |||
352 | printf("Connected to task %d\n", taskid); |
||
353 | |||
2873 | svoboda | 354 | ipcp_init(); |
2882 | svoboda | 355 | ipcp_connection_set(1, 0, proto_console); |
2873 | svoboda | 356 | |
2832 | svoboda | 357 | rc = get_thread_list(); |
358 | if (rc < 0) { |
||
359 | printf("Failed to get thread list (error %d)\n", rc); |
||
360 | return; |
||
361 | } |
||
362 | |||
2850 | svoboda | 363 | abort_trace = 0; |
2832 | svoboda | 364 | |
2850 | svoboda | 365 | for (i = 0; i < n_threads; i++) { |
2868 | svoboda | 366 | thread_trace_start(thread_hash_buf[i]); |
2850 | svoboda | 367 | } |
368 | |||
369 | getchar(); |
||
370 | |||
371 | printf("terminate debugging session...\n"); |
||
372 | abort_trace = 1; |
||
2835 | svoboda | 373 | debug_end(phoneid); |
2850 | svoboda | 374 | ipc_hangup(phoneid); |
375 | |||
2873 | svoboda | 376 | ipcp_cleanup(); |
377 | |||
2832 | svoboda | 378 | printf("done\n"); |
379 | return; |
||
380 | } |
||
381 | |||
2878 | svoboda | 382 | static void main_init(void) |
2832 | svoboda | 383 | { |
2878 | svoboda | 384 | proto_t *p; |
2880 | svoboda | 385 | oper_t *o; |
2878 | svoboda | 386 | |
2868 | svoboda | 387 | next_thread_id = 1; |
388 | |||
2878 | svoboda | 389 | proto_init(); |
390 | |||
2880 | svoboda | 391 | p = proto_new("vfs"); |
2883 | svoboda | 392 | o = oper_new("read"); |
393 | proto_add_oper(p, VFS_READ, o); |
||
394 | o = oper_new("write"); |
||
395 | proto_add_oper(p, VFS_WRITE, o); |
||
396 | o = oper_new("truncate"); |
||
397 | proto_add_oper(p, VFS_TRUNCATE, o); |
||
398 | o = oper_new("mount"); |
||
2880 | svoboda | 399 | proto_add_oper(p, VFS_MOUNT, o); |
2883 | svoboda | 400 | o = oper_new("unmount"); |
401 | proto_add_oper(p, VFS_UNMOUNT, o); |
||
2880 | svoboda | 402 | |
2878 | svoboda | 403 | proto_register(SERVICE_VFS, p); |
2882 | svoboda | 404 | |
405 | p = proto_new("console"); |
||
2883 | svoboda | 406 | o = oper_new("getchar"); |
407 | proto_add_oper(p, CONSOLE_GETCHAR, o); |
||
408 | o = oper_new("putchar"); |
||
2882 | svoboda | 409 | proto_add_oper(p, CONSOLE_PUTCHAR, o); |
2883 | svoboda | 410 | o = oper_new("clear"); |
411 | proto_add_oper(p, CONSOLE_CLEAR, o); |
||
412 | o = oper_new("goto"); |
||
413 | proto_add_oper(p, CONSOLE_GOTO, o); |
||
414 | o = oper_new("getsize"); |
||
415 | proto_add_oper(p, CONSOLE_GETSIZE, o); |
||
416 | o = oper_new("flush"); |
||
417 | proto_add_oper(p, CONSOLE_FLUSH, o); |
||
418 | o = oper_new("set_style"); |
||
419 | proto_add_oper(p, CONSOLE_SET_STYLE, o); |
||
420 | o = oper_new("flush"); |
||
421 | proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o); |
||
422 | o = oper_new("cursor_visibility"); |
||
423 | proto_add_oper(p, CONSOLE_FLUSH, o); |
||
2882 | svoboda | 424 | |
425 | proto_console = p; |
||
426 | proto_register(SERVICE_CONSOLE, p); |
||
2878 | svoboda | 427 | } |
428 | |||
429 | int main(void) |
||
430 | { |
||
431 | main_init(); |
||
432 | |||
2835 | svoboda | 433 | while (1) { |
434 | trace_active_task(); |
||
435 | } |
||
2832 | svoboda | 436 | } |
437 | |||
438 | /** @} |
||
439 | */ |