From 3b17c3414dc3885f05d058fee7bbfcc79773746d Mon Sep 17 00:00:00 2001
From: Thomas Chou <thomas@wytron.com.tw>
Date: Fri, 30 Nov 2007 11:58:07 +0800
Subject: [PATCH] nios2: add user space debug stub

This is a gdbserver helper for nios2 user space application debug. It
traps to kernel to flag current process for debugging. It is imported
from Microtronix Nios II Linux v1.4 release.

Signed-off-by: Thomas Chou <thomas@wytron.com.tw>

diff --git a/config/config.in b/config/config.in
index 43ba9d0..0577555 100644
--- a/config/config.in
+++ b/config/config.in
@@ -858,6 +858,7 @@ fi
 bool 'gdb (host)'		CONFIG_USER_GDB_HOST
 bool 'gdbreplay (old)'		CONFIG_USER_GDBSERVER_GDBREPLAY
 bool 'gdbserver (old)'		CONFIG_USER_GDBSERVER_GDBSERVER
+bool 'debug'			CONFIG_USER_DEBUG_DEBUG
 bool 'grep'			CONFIG_USER_GREP_GREP
 bool 'hd'			CONFIG_USER_HD_HD
 bool 'lcd'			CONFIG_USER_LCD_LCD
diff --git a/user/Makefile b/user/Makefile
index f7cdbbd..8af1c0b 100644
--- a/user/Makefile
+++ b/user/Makefile
@@ -77,6 +77,7 @@ dir_$(CONFIG_USER_CRYPTO_TOOLS_CRYPTOKEYTEST)  += crypto-tools
 dir_$(CONFIG_USER_CXXTEST_CXXTEST)          += cxxtest
 dir_$(CONFIG_USER_DDNS3_CLIENT_DDNS3)        += ddns3-client
 dir_$(CONFIG_USER_DE2TSCAL_DE2TSCAL)        += de2ts-cal
+dir_$(CONFIG_USER_DEBUG_DEBUG)		    += debug
 dir_$(CONFIG_USER_DEMO_BUTTON)              += demo
 dir_$(CONFIG_USER_DEMO_MORSE)               += demo
 dir_$(CONFIG_USER_DHRYSTONE_DHRYSTONE)      += dhrystone
diff --git a/user/debug/Makefile b/user/debug/Makefile
new file mode 100644
index 0000000..7fddf7d
--- /dev/null
+++ b/user/debug/Makefile
@@ -0,0 +1,11 @@
+EXEC = debug
+
+OBJS = debug.o
+
+$(EXEC): $(OBJS)
+
+romfs:
+	$(ROMFSINST) /bin/$(EXEC)
+
+clean:
+	-rm -f $(EXEC) *.elf *.gdb *.o
diff --git a/user/debug/debug.c b/user/debug/debug.c
new file mode 100644
index 0000000..7e0aab0
--- /dev/null
+++ b/user/debug/debug.c
@@ -0,0 +1,38 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <asm/traps.h>
+
+void usage( void )
+{
+	fprintf( stderr, "Usage: debug <application> <args..>\n" );
+}
+
+
+    
+int main( int argc, char *argv[] )
+{
+	int i;
+	if ( argc < 2 || !strcmp( argv[1], "--help" ) ) {
+		usage();
+		return 1;
+	}
+
+//	printf("DEBUG: trapping to kernel to flag current process for debugging ...\n");
+    __asm__ __volatile__ ("movi r2, %0\n\t"
+					 "trap"
+					 :
+					 :"i" (TRAP_ID_APPDEBUG) 
+					 :"r2");
+
+	printf( "DEBUG: running \"%s", argv[1] );
+	for(i = 2; i < argc; i++) printf( " %s", argv[i] );
+	printf( "\" ...\n" );
+
+	execvp( argv[1], &(argv[1]) );
+	printf("%s: %s\n", argv[1], (errno == ENOENT) ? "Bad command or file name" : strerror(errno));
+	_exit(0);
+}
+
-- 
1.5.3.3

