jtagemul.c

Go to the documentation of this file.
00001 //
00002 // JTAGEMUL.C
00003 //
00004 // Emulates a JTAG hardware interface via the parallel port
00005 // and a JTAG/parallel port dongle to target hardware that
00006 // interfaces to GDB via the remote debugging protocol.
00007 //
00008 // Copyright (c) 2002, Jason Riffel - TotalEmbedded LLC.
00009 // All rights reserved.
00010 //
00011 // Redistribution and use in source and binary forms, with or without 
00012 // modification, are permitted provided that the following conditions 
00013 // are met:
00014 //
00015 // Redistributions of source code must retain the above copyright 
00016 // notice, this list of conditions and the following disclaimer. 
00017 //
00018 // Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in 
00020 // the documentation and/or other materials provided with the
00021 // distribution. 
00022 //
00023 // Neither the name of TotalEmbedded nor the names of its 
00024 // contributors may be used to endorse or promote products derived 
00025 // from this software without specific prior written permission.
00026 // 
00027 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00028 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00029 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
00030 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00031 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
00032 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00033 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00034 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00035 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00036 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
00037 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00038 // POSSIBILITY OF SUCH DAMAGE.
00039 //
00040 
00041 //
00042 // INCLUDES
00043 //
00044 #include <errno.h>
00045 #include <signal.h>
00046 #include <stdio.h>
00047 #include <unistd.h>
00048 #include <fcntl.h>
00049 #include <sys/types.h>
00050 #include <sys/socket.h>
00051 #include <sys/wait.h>
00052 #include <netinet/in.h>
00053 #include <netdb.h>
00054 #include "jtagsock.h"
00055 #include "gdbrdp.h"
00056 #include "cmdparser.h"
00057 #include "mips32.h"
00058 
00059 //
00060 // DEFINES
00061 //
00062 #define PORTNUM                 50010
00063 
00064 extern int config_gdbserver_quitgdb;
00065 
00066 //
00067 // main
00068 //
00069 // The heart of it all.
00070 //
00071 void ctrl_c_op(int signo)
00072 {
00073         printf("kkkkkkkkkkkkkkkkkkkk\n");
00074         signal(SIGPIPE,SIG_IGN);
00075 }
00076 
00077 int cmd_gdbserver(int argc,char **argv)
00078 {
00079         int i_socket, i_currentsocket, i_flags;
00080         int port = argc > 1 ? strtoul(argv[1],0,0) :PORTNUM;
00081 
00082 //      signal(SIGPIPE,ctrl_c_op);
00083         setbuffer(stdout,0,0);
00084         setbuffer(stderr,0,0);
00085 
00086         fMIPS32_Init();
00087         // Establish connection to port
00088         i_socket = fJTAGSOCK_Establish(port);
00089         if (i_socket < 0)
00090         {
00091                 // Establish failed.
00092                 perror("fJTAGSOCK_Establish failed.");
00093                 exit(1);
00094         }
00095 
00096 
00097         config_gdbserver_quitgdb = 0;
00098         // Forever
00099         for (;;)
00100         {
00101                 if(config_gdbserver_quitgdb)break;
00102                 printf("Attached to port %d, waiting for gdb.\nyou can also input command on console\n",port);
00103 
00104                 i_flags = fcntl(i_socket, F_GETFL, 0); 
00105                 i_flags |= O_NONBLOCK;
00106                 fcntl(i_socket, F_SETFL, i_flags); 
00107                 // Wait for a connection to occur
00108                 i_currentsocket = fJTAGSOCK_GetConnection(i_socket);
00109                 if (i_currentsocket < 0)
00110                 {
00111                         perror("fJTAGSOCK_GetConnection failed.");
00112                         close(i_socket);
00113                         return 2;
00114                 }
00115 
00116                 // Change the socket to non-blocking
00117                 i_flags = fcntl(i_currentsocket, F_GETFL, 0); 
00118                 i_flags |= O_NONBLOCK;
00119                 fcntl(i_currentsocket, F_SETFL, i_flags); 
00120 
00121                 printf("GDB successfully attached.\r\n");
00122 
00123                 force_acc();
00124                 ui_ejtag_state=dMIPS32_EJTAG_WAIT_FOR_CONTINUE;
00125                 ui_last_signal=SIGTRAP;
00126 
00127                 // Run the GDB Remote Debugging Protocol handler (the guts)
00128                 fGDBRDP_Handler(i_currentsocket);
00129         }
00130 
00131         // Should never reach here
00132         close(i_socket);
00133         close(i_currentsocket);
00134         return(1);
00135 }
00136 static int cmd_quitgdb(int argc,char **argv)
00137 {
00138 extern int config_gdbserver_quitgdb;
00139 if(argc > 1)
00140  config_gdbserver_quitgdb = strtoul(argv[1],0,0);
00141 else
00142  config_gdbserver_quitgdb = 1;
00143  return 0;
00144 }
00145 mycmd_init(gdbserver,cmd_gdbserver,"gdbserver","gdbserver [port],\tenv:compareasid,comparedata,quitgdb");
00146 mycmd_init(quitgdb,cmd_quitgdb,"quitgdb [n]","quitgdb n>1 force quit");

Generated on Fri Nov 5 07:24:46 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6