2
* Copyright (c) 2008 Jiri Svoboda
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
47
#include <loader/loader.h>
48
#include <io/console.h>
49
#include <io/keycode.h>
50
#include <fibril_sync.h>
54
// Temporary: service and method names
56
#include <ipc/services.h>
57
#include "../../srv/vfs/vfs.h"
58
#include <ipc/console.h>
66
uintptr_t thread_hash_buf[THBUF_SIZE];
71
ipc_call_t thread_ipc_req[THBUF_SIZE];
78
static fibril_condvar_t state_cv;
79
static fibril_mutex_t state_lock;
81
static bool cev_valid;
82
static console_event_t cev;
84
void thread_trace_start(uintptr_t thread_hash);
86
static proto_t *proto_console;
87
static task_id_t task_id;
88
static loader_t *task_ldr;
89
static bool task_wait_for;
91
/** Combination of events/data to print. */
92
display_mask_t display_mask;
94
static int program_run_fibril(void *arg);
95
static int cev_fibril(void *arg);
97
static void program_run(void)
101
fid = fibril_create(program_run_fibril, NULL);
103
printf("Error creating fibril\n");
107
fibril_add_ready(fid);
110
static void cev_fibril_start(void)
114
fid = fibril_create(cev_fibril, NULL);
116
printf("Error creating fibril\n");
120
fibril_add_ready(fid);
123
static int program_run_fibril(void *arg)
128
* This must be done in background as it will block until
129
* we let the task reply to this call.
131
rc = loader_run(task_ldr);
133
printf("Error running program\n");
140
printf("program_run_fibril exiting\n");
145
static int connect_task(task_id_t task_id)
149
rc = ipc_connect_kbox(task_id);
152
printf("You do not have userspace debugging support "
153
"compiled in the kernel.\n");
154
printf("Compile kernel with 'Support for userspace debuggers' "
155
"(CONFIG_UDEBUG) enabled.\n");
160
printf("Error connecting\n");
161
printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
167
rc = udebug_begin(phoneid);
169
printf("udebug_begin() -> %d\n", rc);
173
rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
175
printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
182
static int get_thread_list(void)
189
rc = udebug_thread_read(phoneid, thread_hash_buf,
190
THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
192
printf("udebug_thread_read() -> %d\n", rc);
196
n_threads = tb_copied / sizeof(uintptr_t);
199
for (i = 0; i < n_threads; i++) {
200
printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]);
202
printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
207
void val_print(sysarg_t val, val_type_t v_type)
224
printf("0x%08lx", val);
228
if (sval >= -15 && sval <= 0) {
229
printf("%ld %s (%s)", sval,
230
err_desc[-sval].name,
231
err_desc[-sval].desc);
237
if (sval >= -15 && sval < 0) {
238
printf("%ld %s (%s)", sval,
239
err_desc[-sval].name,
240
err_desc[-sval].desc);
247
if (sval >= 0x20 && sval < 0x7f) {
248
printf("'%c'", sval);
251
case '\a': printf("'\\a'"); break;
252
case '\b': printf("'\\b'"); break;
253
case '\n': printf("'\\n'"); break;
254
case '\r': printf("'\\r'"); break;
255
case '\t': printf("'\\t'"); break;
256
case '\\': printf("'\\\\'"); break;
257
default: printf("'\\x%02lX'", val); break;
265
static void print_sc_retval(sysarg_t retval, val_type_t val_type)
268
val_print(retval, val_type);
272
static void print_sc_args(sysarg_t *sc_args, int n)
277
if (n > 0) printf("%ld", sc_args[0]);
278
for (i = 1; i < n; i++) {
279
printf(", %ld", sc_args[i]);
284
static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
289
if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
290
sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
293
phoneid = sc_args[0];
295
IPC_SET_METHOD(call, sc_args[1]);
296
IPC_SET_ARG1(call, sc_args[2]);
297
IPC_SET_ARG2(call, sc_args[3]);
298
IPC_SET_ARG3(call, sc_args[4]);
299
IPC_SET_ARG4(call, sc_args[5]);
300
IPC_SET_ARG5(call, 0);
302
ipcp_call_out(phoneid, &call, sc_rc);
305
static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
310
if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
311
sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
314
memset(&call, 0, sizeof(call));
315
rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
318
ipcp_call_out(sc_args[0], &call, sc_rc);
322
static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
324
ipc_call_t question, reply;
328
// printf("sc_ipc_call_sync_fast()\n");
329
phoneidx = sc_args[0];
331
IPC_SET_METHOD(question, sc_args[1]);
332
IPC_SET_ARG1(question, sc_args[2]);
333
IPC_SET_ARG2(question, sc_args[3]);
334
IPC_SET_ARG3(question, sc_args[4]);
335
IPC_SET_ARG4(question, 0);
336
IPC_SET_ARG5(question, 0);
338
// printf("memset\n");
339
memset(&reply, 0, sizeof(reply));
340
// printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
341
// phoneid, &reply.args, sc_args[5], sizeof(reply.args));
342
rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
343
// printf("dmr->%d\n", rc);
346
// printf("call ipc_call_sync\n");
347
ipcp_call_sync(phoneidx, &question, &reply);
350
static void sc_ipc_call_sync_slow_b(unsigned thread_id, sysarg_t *sc_args)
355
memset(&question, 0, sizeof(question));
356
rc = udebug_mem_read(phoneid, &question.args, sc_args[1],
357
sizeof(question.args));
360
printf("Error: mem_read->%d\n", rc);
364
thread_ipc_req[thread_id] = question;
367
static void sc_ipc_call_sync_slow_e(unsigned thread_id, sysarg_t *sc_args)
372
memset(&reply, 0, sizeof(reply));
373
rc = udebug_mem_read(phoneid, &reply.args, sc_args[2],
377
printf("Error: mem_read->%d\n", rc);
381
ipcp_call_sync(sc_args[0], &thread_ipc_req[thread_id], &reply);
384
static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
389
if (sc_rc == 0) return;
391
memset(&call, 0, sizeof(call));
392
rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
393
// printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
394
// phoneid, (int)&call, sc_args[0], sizeof(call), rc);
397
ipcp_call_in(&call, sc_rc);
401
static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
402
unsigned sc_id, sysarg_t sc_rc)
407
/* Read syscall arguments */
408
rc = udebug_args_read(phoneid, thread_hash, sc_args);
410
async_serialize_start();
412
// printf("[%d] ", thread_id);
416
async_serialize_end();
420
if ((display_mask & DM_SYSCALL) != 0) {
421
/* Print syscall name and arguments */
422
printf("%s", syscall_desc[sc_id].name);
423
print_sc_args(sc_args, syscall_desc[sc_id].n_args);
427
case SYS_IPC_CALL_SYNC_SLOW:
428
sc_ipc_call_sync_slow_b(thread_id, sc_args);
434
async_serialize_end();
437
static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
438
unsigned sc_id, sysarg_t sc_rc)
444
/* Read syscall arguments */
445
rc = udebug_args_read(phoneid, thread_hash, sc_args);
447
async_serialize_start();
449
// printf("[%d] ", thread_id);
453
async_serialize_end();
457
if ((display_mask & DM_SYSCALL) != 0) {
458
/* Print syscall return value */
459
rv_type = syscall_desc[sc_id].rv_type;
460
print_sc_retval(sc_rc, rv_type);
464
case SYS_IPC_CALL_ASYNC_FAST:
465
sc_ipc_call_async_fast(sc_args, sc_rc);
467
case SYS_IPC_CALL_ASYNC_SLOW:
468
sc_ipc_call_async_slow(sc_args, sc_rc);
470
case SYS_IPC_CALL_SYNC_FAST:
471
sc_ipc_call_sync_fast(sc_args);
473
case SYS_IPC_CALL_SYNC_SLOW:
474
sc_ipc_call_sync_slow_e(thread_id, sc_args);
477
sc_ipc_wait(sc_args, sc_rc);
483
async_serialize_end();
486
static void event_thread_b(uintptr_t hash)
488
async_serialize_start();
489
printf("New thread, hash 0x%lx\n", hash);
490
async_serialize_end();
492
thread_trace_start(hash);
495
static int trace_loop(void *thread_hash_arg)
499
uintptr_t thread_hash;
503
thread_hash = (uintptr_t)thread_hash_arg;
504
thread_id = next_thread_id++;
505
if (thread_id >= THBUF_SIZE) {
506
printf("Too many threads.\n");
510
printf("Start tracing thread [%d] (hash 0x%lx).\n", thread_id, thread_hash);
512
while (!abort_trace) {
514
fibril_mutex_lock(&state_lock);
516
printf("Thread [%d] paused. Press R to resume.\n",
520
fibril_condvar_wait(&state_cv, &state_lock);
522
printf("Thread [%d] resumed.\n", thread_id);
524
fibril_mutex_unlock(&state_lock);
526
/* Run thread until an event occurs */
527
rc = udebug_go(phoneid, thread_hash,
528
&ev_type, &val0, &val1);
530
// printf("rc = %d, ev_type=%d\n", rc, ev_type);
531
if (ev_type == UDEBUG_EVENT_FINISHED) {
532
/* Done tracing this thread */
538
case UDEBUG_EVENT_SYSCALL_B:
539
event_syscall_b(thread_id, thread_hash, val0, (int)val1);
541
case UDEBUG_EVENT_SYSCALL_E:
542
event_syscall_e(thread_id, thread_hash, val0, (int)val1);
544
case UDEBUG_EVENT_STOP:
545
printf("Stop event\n");
546
fibril_mutex_lock(&state_lock);
548
fibril_mutex_unlock(&state_lock);
550
case UDEBUG_EVENT_THREAD_B:
551
event_thread_b(val0);
553
case UDEBUG_EVENT_THREAD_E:
554
printf("Thread 0x%lx exited.\n", val0);
555
fibril_mutex_lock(&state_lock);
557
fibril_condvar_broadcast(&state_cv);
558
fibril_mutex_unlock(&state_lock);
561
printf("Unknown event type %d.\n", ev_type);
568
printf("Finished tracing thread [%d].\n", thread_id);
572
void thread_trace_start(uintptr_t thread_hash)
578
fid = fibril_create(trace_loop, (void *)thread_hash);
580
printf("Warning: Failed creating fibril\n");
582
fibril_add_ready(fid);
585
static loader_t *preload_task(const char *path, char *const argv[],
591
/* Spawn a program loader */
592
ldr = loader_connect();
597
rc = loader_get_task_id(ldr, task_id);
601
/* Send program pathname */
602
rc = loader_set_pathname(ldr, path);
607
rc = loader_set_args(ldr, argv);
611
/* Send default files */
612
fdi_node_t *files[4];
613
fdi_node_t stdin_node;
614
fdi_node_t stdout_node;
615
fdi_node_t stderr_node;
617
if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
618
files[0] = &stdin_node;
622
if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
623
files[1] = &stdout_node;
627
if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
628
files[2] = &stderr_node;
634
rc = loader_set_files(ldr, files);
638
/* Load the program. */
639
rc = loader_load_program(ldr);
653
static int cev_fibril(void *arg)
658
fibril_mutex_lock(&state_lock);
660
fibril_condvar_wait(&state_cv, &state_lock);
661
fibril_mutex_unlock(&state_lock);
663
if (!console_get_event(fphone(stdin), &cev))
666
fibril_mutex_lock(&state_lock);
668
fibril_condvar_broadcast(&state_cv);
669
fibril_mutex_unlock(&state_lock);
673
static void trace_task(task_id_t task_id)
683
* User apps now typically have console on phone 3.
684
* (Phones 1 and 2 are used by the loader).
686
ipcp_connection_set(3, 0, proto_console);
688
rc = get_thread_list();
690
printf("Failed to get thread list (error %d)\n", rc);
696
for (i = 0; i < n_threads; i++) {
697
thread_trace_start(thread_hash_buf[i]);
703
fibril_mutex_lock(&state_lock);
704
while (!cev_valid && !abort_trace)
705
fibril_condvar_wait(&state_cv, &state_lock);
706
fibril_mutex_unlock(&state_lock);
710
fibril_mutex_lock(&state_lock);
712
fibril_condvar_broadcast(&state_cv);
713
fibril_mutex_unlock(&state_lock);
718
if (ev.type != KEY_PRESS)
726
printf("Pause...\n");
727
rc = udebug_stop(phoneid, thash);
729
printf("Error: stop -> %d\n", rc);
732
fibril_mutex_lock(&state_lock);
734
fibril_condvar_broadcast(&state_cv);
735
fibril_mutex_unlock(&state_lock);
736
printf("Resume...\n");
741
printf("\nTerminate debugging session...\n");
752
static void main_init(void)
757
val_type_t arg_def[OPER_MAX_ARGS] = {
765
val_type_t resp_def[OPER_MAX_ARGS] = {
777
fibril_mutex_initialize(&state_lock);
778
fibril_condvar_initialize(&state_cv);
782
p = proto_new("vfs");
783
o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
784
proto_add_oper(p, VFS_IN_OPEN, o);
785
o = oper_new("open_node", 4, arg_def, V_INT_ERRNO, 0, resp_def);
786
proto_add_oper(p, VFS_IN_OPEN_NODE, o);
787
o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
788
proto_add_oper(p, VFS_IN_READ, o);
789
o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
790
proto_add_oper(p, VFS_IN_WRITE, o);
791
o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
792
proto_add_oper(p, VFS_IN_SEEK, o);
793
o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
794
proto_add_oper(p, VFS_IN_TRUNCATE, o);
795
o = oper_new("fstat", 1, arg_def, V_ERRNO, 0, resp_def);
796
proto_add_oper(p, VFS_IN_FSTAT, o);
797
o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
798
proto_add_oper(p, VFS_IN_CLOSE, o);
799
o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
800
proto_add_oper(p, VFS_IN_MOUNT, o);
801
/* o = oper_new("unmount", 0, arg_def);
802
proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
803
o = oper_new("sync", 1, arg_def, V_ERRNO, 0, resp_def);
804
proto_add_oper(p, VFS_IN_SYNC, o);
805
o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
806
proto_add_oper(p, VFS_IN_MKDIR, o);
807
o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
808
proto_add_oper(p, VFS_IN_UNLINK, o);
809
o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
810
proto_add_oper(p, VFS_IN_RENAME, o);
811
o = oper_new("stat", 0, arg_def, V_ERRNO, 0, resp_def);
812
proto_add_oper(p, VFS_IN_STAT, o);
814
proto_register(SERVICE_VFS, p);
816
p = proto_new("console");
818
o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
819
proto_add_oper(p, VFS_IN_WRITE, o);
821
resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
822
resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR;
823
o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def);
826
o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
827
proto_add_oper(p, CONSOLE_CLEAR, o);
829
arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
830
o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
831
proto_add_oper(p, CONSOLE_GOTO, o);
833
resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
834
o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
835
proto_add_oper(p, CONSOLE_GET_SIZE, o);
837
arg_def[0] = V_INTEGER;
838
o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def);
839
proto_add_oper(p, CONSOLE_SET_STYLE, o);
840
arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER;
841
o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def);
842
proto_add_oper(p, CONSOLE_SET_COLOR, o);
843
arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
844
o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def);
845
proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o);
846
o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
847
proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
850
proto_register(SERVICE_CONSOLE, p);
853
static void print_syntax()
856
printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
857
printf("or\ttrace [+<events>] -t <task_id>\n");
858
printf("Events: (default is +tp)\n");
860
printf("\tt ... Thread creation and termination\n");
861
printf("\ts ... System calls\n");
862
printf("\ti ... Low-level IPC\n");
863
printf("\tp ... Protocol level\n");
865
printf("Examples:\n");
866
printf("\ttrace +s /app/tetris\n");
867
printf("\ttrace +tsip -t 12\n");
870
static display_mask_t parse_display_mask(char *text)
879
case 't': dm = dm | DM_THREAD; break;
880
case 's': dm = dm | DM_SYSCALL; break;
881
case 'i': dm = dm | DM_IPC; break;
882
case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
884
printf("Unexpected event type '%c'.\n", *c);
894
static int parse_args(int argc, char *argv[])
906
display_mask = parse_display_mask(&arg[1]);
907
} else if (arg[0] == '-') {
909
/* Trace an already running task */
911
task_id = strtol(*argv, &err_p, 10);
913
task_wait_for = false;
915
printf("Task ID syntax error\n");
920
printf("Uknown option '%s'\n", arg[0]);
932
if (argc == 0) return 0;
933
printf("Extra arguments\n");
939
printf("Missing argument\n");
944
/* Preload the specified program file. */
945
printf("Spawning '%s' with arguments:\n", *argv);
948
while (*cp) printf("'%s'\n", *cp++);
950
task_ldr = preload_task(*argv, argv, &task_id);
951
task_wait_for = true;
956
int main(int argc, char *argv[])
962
printf("System Call / IPC Tracer\n");
963
printf("Controls: Q - Quit, P - Pause, R - Resume\n");
965
display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
967
if (parse_args(argc, argv) < 0)
972
rc = connect_task(task_id);
974
printf("Failed connecting to task %lld.\n", task_id);
978
printf("Connected to task %lld.\n", task_id);
980
if (task_ldr != NULL)
987
printf("Waiting for task to exit.\n");
989
rc = task_wait(task_id, &texit, &retval);
991
printf("Failed waiting for task.\n");
995
if (texit == TASK_EXIT_NORMAL) {
996
printf("Task exited normally, return value %d.\n",
999
printf("Task exited unexpectedly.\n");