Index: uspace/app/trace/trace.c
===================================================================
--- uspace/app/trace/trace.c	(revision fafb8e5dc8a80c87cf66270ca6f93d574a95471c)
+++ uspace/app/trace/trace.c	(revision 2443ad8f535160d516359619d12e8e3971670065)
@@ -47,5 +47,4 @@
 #include <mem.h>
 #include <str.h>
-#include <loader/loader.h>
 #include <io/console.h>
 #include <io/keycode.h>
@@ -86,6 +85,8 @@
 void thread_trace_start(uintptr_t thread_hash);
 
+static char *cmd_path;
+static char **cmd_args;
+
 static task_id_t task_id;
-static loader_t *task_ldr;
 static bool task_wait_for;
 
@@ -93,19 +94,5 @@
 display_mask_t display_mask;
 
-static errno_t program_run_fibril(void *arg);
 static errno_t cev_fibril(void *arg);
-
-static void program_run(void)
-{
-	fid_t fid;
-
-	fid = fibril_create(program_run_fibril, NULL);
-	if (fid == 0) {
-		printf("Error creating fibril\n");
-		exit(1);
-	}
-
-	fibril_add_ready(fid);
-}
 
 static void cev_fibril_start(void)
@@ -122,49 +109,49 @@
 }
 
-static errno_t program_run_fibril(void *arg)
-{
-	errno_t rc;
-
-	/*
-	 * This must be done in background as it will block until
-	 * we let the task reply to this call.
-	 */
-	rc = loader_run(task_ldr);
+static errno_t program_run(void)
+{
+	errno_t rc;
+
+	rc = task_spawnv_debug(&task_id, NULL, cmd_path,
+	    (const char *const *)cmd_args, &sess);
+
+	if (rc == ENOTSUP) {
+		printf("You do not have userspace debugging support "
+		    "compiled in the kernel.\n");
+		printf("Compile kernel with 'Support for userspace debuggers' "
+		    "(CONFIG_UDEBUG) enabled.\n");
+	}
+
 	if (rc != EOK) {
-		printf("Error running program\n");
-		exit(1);
-	}
-
-	task_ldr = NULL;
-
-	printf("program_run_fibril exiting\n");
-	return 0;
+		printf("Error running program (%s)\n", str_error_name(rc));
+		return rc;
+	}
+
+	return EOK;
 }
 
 static errno_t connect_task(task_id_t task_id)
 {
-	async_sess_t *ksess = async_connect_kbox(task_id);
-
-	if (!ksess) {
-		if (errno == ENOTSUP) {
-			printf("You do not have userspace debugging support "
-			    "compiled in the kernel.\n");
-			printf("Compile kernel with 'Support for userspace debuggers' "
-			    "(CONFIG_UDEBUG) enabled.\n");
-			return errno;
-		}
-
-		printf("Error connecting\n");
-		printf("ipc_connect_task(%" PRIu64 ") -> %s ", task_id, str_error_name(errno));
-		return errno;
-	}
-
-	errno_t rc = udebug_begin(ksess);
-	if (rc != EOK) {
-		printf("udebug_begin() -> %s\n", str_error_name(rc));
-		return rc;
-	}
-
-	rc = udebug_set_evmask(ksess, UDEBUG_EM_ALL);
+	errno_t rc;
+	bool debug_started = false;
+
+	if (sess == NULL) {
+		sess = async_connect_kbox(task_id);
+		if (sess == NULL) {
+			printf("Error connecting to task %" PRIu64 ".\n",
+			    task_id);
+			goto error;
+		}
+
+		rc = udebug_begin(sess);
+		if (rc != EOK) {
+			printf("Error starting debug session.\n");
+			goto error;
+		}
+
+		debug_started = true;
+	}
+
+	rc = udebug_set_evmask(sess, UDEBUG_EM_ALL);
 	if (rc != EOK) {
 		printf("udebug_set_evmask(0x%x) -> %s\n ", UDEBUG_EM_ALL, str_error_name(rc));
@@ -172,6 +159,11 @@
 	}
 
-	sess = ksess;
-	return 0;
+	return EOK;
+error:
+	if (debug_started)
+		udebug_end(sess);
+	if (sess != NULL)
+		async_hangup(sess);
+	return rc;
 }
 
@@ -198,5 +190,5 @@
 	printf("\ntotal of %zu threads\n", tb_needed / sizeof(uintptr_t));
 
-	return 0;
+	return EOK;
 }
 
@@ -488,5 +480,5 @@
 
 	printf("Finished tracing thread [%d].\n", thread_id);
-	return 0;
+	return EOK;
 }
 
@@ -502,76 +494,4 @@
 	}
 	fibril_add_ready(fid);
-}
-
-static loader_t *preload_task(const char *path, char **argv,
-    task_id_t *task_id)
-{
-	loader_t *ldr;
-	errno_t rc;
-
-	/* Spawn a program loader */
-	ldr = loader_connect();
-	if (ldr == NULL)
-		return NULL;
-
-	/* Get task ID. */
-	rc = loader_get_task_id(ldr, task_id);
-	if (rc != EOK)
-		goto error;
-
-	/* Send program. */
-	rc = loader_set_program_path(ldr, path);
-	if (rc != EOK)
-		goto error;
-
-	/* Send arguments */
-	rc = loader_set_args(ldr, (const char **) argv);
-	if (rc != EOK)
-		goto error;
-
-	/* Send default files */
-	int fd_root;
-	int fd_stdin;
-	int fd_stdout;
-	int fd_stderr;
-
-	fd_root = vfs_root();
-	if (fd_root >= 0) {
-		rc = loader_add_inbox(ldr, "root", fd_root);
-		vfs_put(fd_root);
-		if (rc != EOK)
-			goto error;
-	}
-
-	if ((stdin != NULL) && (vfs_fhandle(stdin, &fd_stdin) == EOK)) {
-		rc = loader_add_inbox(ldr, "stdin", fd_stdin);
-		if (rc != EOK)
-			goto error;
-	}
-
-	if ((stdout != NULL) && (vfs_fhandle(stdout, &fd_stdout) == EOK)) {
-		rc = loader_add_inbox(ldr, "stdout", fd_stdout);
-		if (rc != EOK)
-			goto error;
-	}
-
-	if ((stderr != NULL) && (vfs_fhandle(stderr, &fd_stderr) == EOK)) {
-		rc = loader_add_inbox(ldr, "stderr", fd_stderr);
-		if (rc != EOK)
-			goto error;
-	}
-
-	/* Load the program. */
-	rc = loader_load_program(ldr);
-	if (rc != EOK)
-		goto error;
-
-	/* Success */
-	return ldr;
-
-	/* Error exit */
-error:
-	loader_abort(ldr);
-	return NULL;
 }
 
@@ -807,5 +727,4 @@
 				++argv;
 				task_id = strtol(*argv, &err_p, 10);
-				task_ldr = NULL;
 				task_wait_for = false;
 				if (*err_p) {
@@ -848,5 +767,6 @@
 		printf("'%s'\n", *cp++);
 
-	task_ldr = preload_task(*argv, argv, &task_id);
+	cmd_path = *argv;
+	cmd_args = argv;
 	task_wait_for = true;
 
@@ -869,4 +789,7 @@
 
 	main_init();
+
+	if (cmd_path != NULL)
+		program_run();
 
 	rc = connect_task(task_id);
@@ -877,7 +800,4 @@
 
 	printf("Connected to task %" PRIu64 ".\n", task_id);
-
-	if (task_ldr != NULL)
-		program_run();
 
 	cev_fibril_start();
