Index: uspace/lib/c/generic/task.c
===================================================================
--- uspace/lib/c/generic/task.c	(revision fafb8e5dc8a80c87cf66270ca6f93d574a95471c)
+++ uspace/lib/c/generic/task.c	(revision 0a201454d136e12704a02743625fcf6428e1268a)
@@ -35,4 +35,5 @@
  */
 
+#include <async.h>
 #include <task.h>
 #include <loader/loader.h>
@@ -46,4 +47,5 @@
 #include <ns.h>
 #include <stdlib.h>
+#include <udebug.h>
 #include <libc.h>
 #include "private/ns.h"
@@ -94,4 +96,5 @@
  * This is really just a convenience wrapper over the more complicated
  * loader API. Arguments are passed as a null-terminated array of strings.
+ * A debug session is created optionally.
  *
  * @param id   If not NULL, the ID of the task is stored here on success.
@@ -100,10 +103,12 @@
  * @param path Pathname of the binary to execute.
  * @param argv Command-line arguments.
- *
- * @return Zero on success or an error code.
- *
- */
-errno_t task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
-    const char *const args[])
+ * @param rsess   Place to store pointer to debug session or @c NULL
+ *                not to start a debug session
+ *
+ * @return Zero on success or an error code.
+ *
+ */
+errno_t task_spawnv_debug(task_id_t *id, task_wait_t *wait, const char *path,
+    const char *const args[], async_sess_t **rsess)
 {
 	/* Send default files */
@@ -125,28 +130,54 @@
 	}
 
-	return task_spawnvf(id, wait, path, args, fd_stdin, fd_stdout,
-	    fd_stderr);
+	return task_spawnvf_debug(id, wait, path, args, fd_stdin, fd_stdout,
+	    fd_stderr, rsess);
 }
 
 /** Create a new task by running an executable from the filesystem.
+ *
+ * This is really just a convenience wrapper over the more complicated
+ * loader API. Arguments are passed as a null-terminated array of strings.
+ *
+ * @param id   If not NULL, the ID of the task is stored here on success.
+ * @param wait If not NULL, setup waiting for task's return value and store
+ *             the information necessary for waiting here on success.
+ * @param path Pathname of the binary to execute.
+ * @param argv Command-line arguments.
+ *
+ * @return Zero on success or an error code.
+ *
+ */
+errno_t task_spawnv(task_id_t *id, task_wait_t *wait, const char *path,
+    const char *const args[])
+{
+	return task_spawnv_debug(id, wait, path, args, NULL);
+}
+
+/** Create a new task by loading an executable from the filesystem.
  *
  * This is really just a convenience wrapper over the more complicated
  * loader API. Arguments are passed as a null-terminated array of strings.
  * Files are passed as null-terminated array of pointers to fdi_node_t.
+ * A debug session is created optionally.
  *
  * @param id      If not NULL, the ID of the task is stored here on success.
- * @param wait    If not NULL, setup waiting for task's return value and store
+ * @param wait    If not NULL, setup waiting for task's return value and store.
  * @param path    Pathname of the binary to execute.
- * @param argv    Command-line arguments.
- * @param std_in  File to use as stdin.
- * @param std_out File to use as stdout.
- * @param std_err File to use as stderr.
- *
- * @return Zero on success or an error code.
- *
- */
-errno_t task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
-    const char *const args[], int fd_stdin, int fd_stdout, int fd_stderr)
-{
+ * @param argv    Command-line arguments
+ * @param std_in  File to use as stdin
+ * @param std_out File to use as stdout
+ * @param std_err File to use as stderr
+ * @param rsess   Place to store pointer to debug session or @c NULL
+ *                not to start a debug session
+ *
+ * @return Zero on success or an error code
+ *
+ */
+errno_t task_spawnvf_debug(task_id_t *id, task_wait_t *wait,
+    const char *path, const char *const args[], int fd_stdin, int fd_stdout,
+    int fd_stderr, async_sess_t **rsess)
+{
+	async_sess_t *ksess = NULL;
+
 	/* Connect to a program loader. */
 	loader_t *ldr = loader_connect();
@@ -217,16 +248,39 @@
 	}
 
-	/* Run it. */
-	rc = loader_run(ldr);
-	if (rc != EOK)
-		goto error;
+	/* Start a debug session if requested */
+	if (rsess != NULL) {
+		ksess = async_connect_kbox(task_id);
+		if (ksess == NULL) {
+			/* Most likely debugging support is not compiled in */
+			rc = ENOTSUP;
+			goto error;
+		}
+
+		rc = udebug_begin(ksess);
+		if (rc != EOK)
+			goto error;
+
+		/*
+		 * Run it, not waiting for response. It would never come
+		 * as the loader is stopped.
+		 */
+		loader_run_nowait(ldr);
+	} else {
+		/* Run it. */
+		rc = loader_run(ldr);
+		if (rc != EOK)
+			goto error;
+	}
 
 	/* Success */
 	if (id != NULL)
 		*id = task_id;
-
+	if (rsess != NULL)
+		*rsess = ksess;
 	return EOK;
 
 error:
+	if (ksess != NULL)
+		async_hangup(ksess);
 	if (wait_initialized)
 		task_cancel_wait(wait);
@@ -235,4 +289,27 @@
 	loader_abort(ldr);
 	return rc;
+}
+
+/** Create a new task by running an executable from the filesystem.
+ *
+ * Arguments are passed as a null-terminated array of strings.
+ * Files are passed as null-terminated array of pointers to fdi_node_t.
+ *
+ * @param id      If not NULL, the ID of the task is stored here on success.
+ * @param wait    If not NULL, setup waiting for task's return value and store.
+ * @param path    Pathname of the binary to execute
+ * @param argv    Command-line arguments
+ * @param std_in  File to use as stdin
+ * @param std_out File to use as stdout
+ * @param std_err File to use as stderr
+ *
+ * @return Zero on success or an error code.
+ *
+ */
+errno_t task_spawnvf(task_id_t *id, task_wait_t *wait, const char *path,
+    const char *const args[], int fd_stdin, int fd_stdout, int fd_stderr)
+{
+	return task_spawnvf_debug(id, wait, path, args, fd_stdin, fd_stdout,
+	    fd_stderr, NULL);
 }
 
