gdbstub user mode : local/unix socket (cf remote pipe connexion addition)
Hi
I failed to add stdio remote pipe connection method to the linux-user gdbstub
mode. But adding local/unix sockets instead solves the main part of my problem.
GDB options :
-g trcp_portnumber (deprecated, for compatibility) -gdb tcp::portnumber
-gdb local::portname
Local/unix sockets allow for instance
Quoted 1 line
qemu-arm -gdb
/tmp/my_private_socket_nameexecutable_file
gdb> target remote | socat stdio unix-connect:/tmp/my_private_socket_name
Files modified : gdbstub.c gdbstub.h linux-user/main.c (tar file attached)
Diff from latest stable release (patch "Rework configuration via command line not applied).
Best regards
Ph. Waille
gdbstub.c +25 -73
@@ -27,7 +27,6 @@#include <errno.h> #include <unistd.h> #include <fcntl.h> -#include <sys/socket.h> #include "qemu.h" #else
@@ -304,7 +303,6 @@#ifdef CONFIG_USER_ONLY /* XXX: This is not thread safe. Do we care? */ static int gdbserver_fd =-1 ; -static int gdbserver_family;static int get_char (GDBState *s) {
@@ -2171,23 +2169,13 @@static void gdb_accept (void ) {GDBState *s; -struct sockaddr_in addr_in; -struct sockaddr_un addr_un; -struct sockaddr *addr; +struct sockaddr_in sockaddr;socklen_t len;int val, fd; -switch (gdbserver_family) - { -case AF_LOCAL : - addr = (struct sockaddr *) &addr_un; len =sizeof (addr_un);break ; - -default :/* AF_INET by default */ - addr = (struct sockaddr *) &addr_in; len =sizeof (addr_in);break ; - } -for (;;) { - fd =accept (gdbserver_fd, addr, &len); + len =sizeof (sockaddr); + fd =accept (gdbserver_fd, (struct sockaddr *)&sockaddr, &len);if (fd <0 && errno !=EINTR ) {perror ("accept" );return ;
@@ -2196,12 +2184,9 @@ } } -if (gdbserver_family ==AF_INET ) - { -/* set short latency */ - val =1 ; -setsockopt (fd,IPPROTO_TCP ,TCP_NODELAY , (char *)&val,sizeof (val)); - } +/* set short latency */ + val =1 ; +setsockopt (fd,IPPROTO_TCP ,TCP_NODELAY , (char *)&val,sizeof (val)); s =qemu_mallocz (sizeof (GDBState));
@@ -2216,59 +2201,29 @@fcntl (fd,F_SETFL ,O_NONBLOCK ); } -static int string_start_with (char **name,const char *prefix) - { -int res =0 ; -if ((strlen (*name) >strlen (prefix)) && - !strncmp (*name, prefix,strlen (prefix))) - {*name +=strlen (prefix); res=1 ;} -return res; - } - -static int gdbserver_open (char *portname) +static int gdbserver_open (int port) { -/* local::port_filename or tcp::port_number or port_number */ +struct sockaddr_in sockaddr; +int fd, val, ret; -struct sockaddr_in sockaddr_in; -struct sockaddr_un sockaddr_un; -int pnum,ret,val, fd; - - sockaddr_in.sin_family =AF_INET ; - sockaddr_in.sin_addr.s_addr =0 ; - sockaddr_un.sun_family =AF_LOCAL ; - - gdbserver_family=0 ; - ret =-1 ; + fd =socket (PF_INET ,SOCK_STREAM ,0 ); +if (fd <0 ) { +perror ("socket" ); +return -1 ; + } -if (string_start_with (&portname,"local::" )) - { - gdbserver_family =AF_LOCAL ; -strcpy (sockaddr_un.sun_path,portname); - fd =socket (gdbserver_family,SOCK_STREAM ,0 ); -if (fd <0 ) {perror ("socket" );return -1 ;} - ret =bind (fd, &sockaddr_un,sizeof (sockaddr_un)); - } -else - { - gdbserver_family =AF_INET ; -/* remove optinal tcp:: prefix */ - val =string_start_with (&portname,"tcp::" ); -/* portname should be a valid port number */ -if (sscanf (portname,"%d" , &pnum) ==1 ) - { - fd =socket (gdbserver_family,SOCK_STREAM ,0 ); -if (fd <0 ) {perror ("socket" );return -1 ;} -setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , - (char *)"1" ,1 ); - sockaddr_in.sin_port =htons (pnum); - ret =bind (fd, &sockaddr_in,sizeof (sockaddr_in)); - } - } - +/* allow fast reuse */ + val =1 ; +setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , (char *)&val,sizeof (val)); + + sockaddr.sin_family =AF_INET ; + sockaddr.sin_port =htons (port); + sockaddr.sin_addr.s_addr =0 ; + ret =bind (fd, (struct sockaddr *)&sockaddr,sizeof (sockaddr));if (ret <0 ) {perror ("bind" );return -1 ; - } + } ret =listen (fd,0 );if (ret <0 ) {perror ("listen" );
@@ -2277,12 +2232,9 @@return fd; } -int gdbserver_start (char *portname) +int gdbserver_start (int port) { -if (!portname || !*portname) -return -1 ; - - gdbserver_fd =gdbserver_open (portname); + gdbserver_fd =gdbserver_open (port);if (gdbserver_fd <0 )return -1 ;/* accept connections */
gdbstub.h +1 -1
@@ -14,7 +14,7 @@int gdb_handlesig (CPUState *,int );void gdb_exit (CPUState *,int );void gdb_signalled (CPUState *,int ); -int gdbserver_start (char *); +int gdbserver_start (int );void gdbserver_fork (CPUState *);#else int gdbserver_start (const char *port);
linux-user/main.c +6 -8
@@ -2186,9 +2186,7 @@"\n" "Standard options:\n" "-h print this help\n" -"-g port (deprecated : use -gdb tcp::port)\n" -"-gdb tcp::port wait gdb connection to TCP socket port\n" -"-gdb local::name wait gdb connection to local (unix) socket name\n" +"-g port wait gdb connection to port\n" "-L path set the elf interpreter prefix (default=%s)\n" "-s size set the stack size in bytes (default=%ld)\n" "-cpu model select CPU (-cpu ? for list)\n"
@@ -2243,7 +2241,7 @@CPUState *env;int optind;const char *r; -char *gdbstub_portname =NULL ; +int gdbstub_port =0 ;char **target_environ, **wrk;envlist_t *envlist =NULL ;
@@ -2324,10 +2322,10 @@fprintf (stderr,"page size must be a power of two\n" );exit (1 ); } - }else if (!strcmp (r,"gdb" ) || !strcmp (r,"g" )) { + }else if (!strcmp (r,"g" )) {if (optind >= argc)break ; - gdbstub_portname = argv[optind++]; + gdbstub_port =atoi (argv[optind++]); }else if (!strcmp (r,"r" )) { qemu_uname_release = argv[optind++]; }else if (!strcmp (r,"cpu" )) {
@@ -2683,8 +2681,8 @@ ts->heap_limit =0 ;#endif -if (gdbstub_portname !=NULL ) { -gdbserver_start (gdbstub_portname); +if (gdbstub_port) { +gdbserver_start (gdbstub_port);gdb_handlesig (env,0 ); }cpu_loop (env);
Signature · 7 lines
----------------------------------------------------------------------------- Philippe WAILLE email : [email protected] IMAG ID (Informatique et distribution) Tel : 04 76 61 20 13 ENSIMAG - antenne de Montbonnot Foreign : 33 4 76 61 20 13 INOVALLEE Fax : 04 76 61 20 99 51, avenue Jean Kuntzmann 38330 MONTBONNOT SAINT MARTIN
On Thu, Apr 02, 2009 at 03:53:12PM +0200, Philippe Waille wrote:
I suggest this patch :
allow unix-domain socket gdb connection in user-mode gdbstub (updated version : fix socket address reuse error in my previous patch)
-------------------------------------------------------------------------
gdbstub.c +27 -72
@@ -27,7 +27,6 @@#include <errno.h> #include <unistd.h> #include <fcntl.h> -#include <sys/socket.h> #include "qemu.h" #else
@@ -304,7 +303,6 @@#ifdef CONFIG_USER_ONLY /* XXX: This is not thread safe. Do we care? */ static int gdbserver_fd =-1 ; -static int gdbserver_family;static int get_char (GDBState *s) {
@@ -2171,23 +2169,13 @@static void gdb_accept (void ) {GDBState *s; -struct sockaddr_in addr_in; -struct sockaddr_un addr_un; -struct sockaddr *addr; +struct sockaddr_in sockaddr;socklen_t len; -int fd; - -switch (gdbserver_family) - { -case AF_LOCAL : - addr = (struct sockaddr *) &addr_un; len =sizeof (addr_un);break ; - -default :/* AF_INET by default */ - addr = (struct sockaddr *) &addr_in; len =sizeof (addr_in);break ; - } +int val, fd;for (;;) { - fd =accept (gdbserver_fd, addr, &len); + len =sizeof (sockaddr); + fd =accept (gdbserver_fd, (struct sockaddr *)&sockaddr, &len);if (fd <0 && errno !=EINTR ) {perror ("accept" );return ;
@@ -2196,6 +2184,10 @@ } } +/* set short latency */ + val =1 ; +setsockopt (fd,IPPROTO_TCP ,TCP_NODELAY , (char *)&val,sizeof (val)); + s =qemu_mallocz (sizeof (GDBState));memset (s,0 ,sizeof (GDBState));
@@ -2209,63 +2201,29 @@fcntl (fd,F_SETFL ,O_NONBLOCK ); } -static int string_start_with (char **name,const char *prefix) - { -int res =0 ; -if ((strlen (*name) >strlen (prefix)) && - !strncmp (*name, prefix,strlen (prefix))) - {*name +=strlen (prefix); res=1 ;} -return res; - } - -static int gdbserver_open (char *portname) +static int gdbserver_open (int port) { -/* local::port_filename or tcp::port_number or port_number */ - -struct sockaddr_in sockaddr_in; -struct sockaddr_un sockaddr_un; -int pnum,ret,val, fd; +struct sockaddr_in sockaddr; +int fd, val, ret; - sockaddr_in.sin_family =AF_INET ; - sockaddr_in.sin_addr.s_addr =0 ; - sockaddr_un.sun_family =AF_LOCAL ; - - gdbserver_family=0 ; - ret =-1 ; + fd =socket (PF_INET ,SOCK_STREAM ,0 ); +if (fd <0 ) { +perror ("socket" ); +return -1 ; + } -if (string_start_with (&portname,"local::" )) - { - gdbserver_family =AF_LOCAL ; -strcpy (sockaddr_un.sun_path,portname); - fd =socket (gdbserver_family,SOCK_STREAM ,0 ); -if (fd <0 ) {perror ("socket" );return -1 ;} - val =1 ; -setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , - &val,sizeof (val)); - ret =bind (fd, &sockaddr_un,sizeof (sockaddr_un)); - } -else - { - gdbserver_family =AF_INET ; -/* remove optinal tcp:: prefix */ - val =string_start_with (&portname,"tcp::" ); -/* portname should be a valid port number */ -if (sscanf (portname,"%d" , &pnum) ==1 ) - { - sockaddr_in.sin_port =htons (pnum); - fd =socket (gdbserver_family,SOCK_STREAM ,0 ); -if (fd <0 ) {perror ("socket" );return -1 ;} - val =1 ; -setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , - &val,sizeof (val)); - ret =bind (fd, &sockaddr_in,sizeof (sockaddr_in)); - } - } - +/* allow fast reuse */ + val =1 ; +setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , (char *)&val,sizeof (val)); + + sockaddr.sin_family =AF_INET ; + sockaddr.sin_port =htons (port); + sockaddr.sin_addr.s_addr =0 ; + ret =bind (fd, (struct sockaddr *)&sockaddr,sizeof (sockaddr));if (ret <0 ) {perror ("bind" );return -1 ; - } + } ret =listen (fd,0 );if (ret <0 ) {perror ("listen" );
@@ -2274,12 +2232,9 @@return fd; } -int gdbserver_start (char *portname) +int gdbserver_start (int port) { -if (!portname || !*portname) -return -1 ; - - gdbserver_fd =gdbserver_open (portname); + gdbserver_fd =gdbserver_open (port);if (gdbserver_fd <0 )return -1 ;/* accept connections */
gdbstub.h +1 -1
@@ -14,7 +14,7 @@int gdb_handlesig (CPUState *,int );void gdb_exit (CPUState *,int );void gdb_signalled (CPUState *,int ); -int gdbserver_start (char *); +int gdbserver_start (int );void gdbserver_fork (CPUState *);#else int gdbserver_start (const char *port);
Seulement dans qemu-0.10.1_modified: gdbstub_orig.c
linux-user/main.c +6 -8
@@ -2186,9 +2186,7 @@"\n" "Standard options:\n" "-h print this help\n" -"-g port (deprecated : use -gdb tcp::port)\n" -"-gdb tcp::port wait gdb connection to TCP socket port\n" -"-gdb local::name wait gdb connection to local (unix) socket name\n" +"-g port wait gdb connection to port\n" "-L path set the elf interpreter prefix (default=%s)\n" "-s size set the stack size in bytes (default=%ld)\n" "-cpu model select CPU (-cpu ? for list)\n"
@@ -2243,7 +2241,7 @@CPUState *env;int optind;const char *r; -char *gdbstub_portname =NULL ; +int gdbstub_port =0 ;char **target_environ, **wrk;envlist_t *envlist =NULL ;
@@ -2324,10 +2322,10 @@fprintf (stderr,"page size must be a power of two\n" );exit (1 ); } - }else if (!strcmp (r,"gdb" ) || !strcmp (r,"g" )) { + }else if (!strcmp (r,"g" )) {if (optind >= argc)break ; - gdbstub_portname = argv[optind++]; + gdbstub_port =atoi (argv[optind++]); }else if (!strcmp (r,"r" )) { qemu_uname_release = argv[optind++]; }else if (!strcmp (r,"cpu" )) {
@@ -2683,8 +2681,8 @@ ts->heap_limit =0 ;#endif -if (gdbstub_portname !=NULL ) { -gdbserver_start (gdbstub_portname); +if (gdbstub_port) { +gdbserver_start (gdbstub_port);gdb_handlesig (env,0 ); }cpu_loop (env);
-----------------------------------------------------------------------------
Philippe WAILLE email : [email protected]
IMAG ID (Informatique et distribution) Tel : 04 76 61 20 13
ENSIMAG - antenne de Montbonnot Foreign : 33 4 76 61 20 13
INOVALLEE Fax : 04 76 61 20 99
51, avenue Jean Kuntzmann
38330 MONTBONNOT SAINT MARTIN
Quoted 6 lines · Philippe Waille
On Thu, Apr 02, 2009 at 03:53:12PM +0200, Philippe Waille wrote:
I suggest this patch :
allow unix-domain socket gdb connection in user-mode gdbstub (updated version : fix socket address reuse error in my previous patch)
For unknown reasons my comments on the first version didn't made it to the list. Find them below, I think they still apply.
Jan
----------
Quoted 5 lines · Philippe Waille
Hi
I failed to add stdio remote pipe connection method to the linux-user gdbstub
mode. But adding local/unix sockets instead solves the main part of my problem.
Great. Thanks for working on this!
Quoted 3 lines
GDB options :
-gtrcp_portnumber(deprecated, for compatibility)
IMHO we should rather drop with a hard cut this instead of keeping (potentially) confusing legacy around.
Review
Quoted 2 lines
(quoted diff) +0
-2-gdb tcp::portnumber -gdb local::portname
Why not using the standard syntax from the system emulation instead inventing a new one? Even if you do not support all variations of qemu-char, at least the corresponding ones should be compatible.
Quoted 11 lines
Local/unix sockets allow for instance
Quoted 1 line
qemu-arm -gdb
/tmp/my_private_socket_nameexecutable_filegdb> target remote | socat stdio unix-connect:/tmp/my_private_socket_name
Files modified :
gdbstub.cgdbstub.hlinux-user/main.c(tar file attached)Diff from latest stable release (patch "Rework configuration via command line not applied).
If the patches have conflicts, maybe we can stack them. They logically belong together (at least once we align the syntax :->), so they should come as a pair.
gdbstub.c
Quoted 6 lines
Best regards
Ph. Waillegdbstub.c +0
-0
Reversed patch. A simple svn diff (or git diff, whatever you use) might be a better idea than manual diffing.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2 Corporate Competence Center Embedded Linux