This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
patch: when gdbserver port is 0, print port assigned by OS
- From: Mat Hostetter <mat at lcs dot mit dot edu>
- To: gdb-patches at sourceware dot org
- Date: 22 Feb 2007 09:40:58 -0500
- Subject: patch: when gdbserver port is 0, print port assigned by OS
2007-02-22 Mat Hostetter <mat@lcs.mit.edu>
* remote-utils.c (remote_open): When the user specifies a port
number of 0, print out the actual port assigned by the OS rather
than 0. This allows gdbserver to pick its own port number.
--- gdb/gdbserver/remote-utils.c 2006-10-17 12:02:27.000000000 -0400
+++ gdb/gdbserver/remote-utils.c 2007-02-21 23:15:37.204860000 -0500
@@ -193,6 +193,17 @@
|| listen (tmp_desc, 1))
perror_with_name ("Can't bind address");
+ /* If port is zero, a random port will be selected, and the
+ "fprintf()" below needs to know what port was selected. */
+ if (port == 0)
+ {
+ socklen_t len = sizeof (sockaddr);
+ if (getsockname (tmp_desc, (struct sockaddr *) &sockaddr, &len) < 0
+ || len < sizeof (sockaddr))
+ perror_with_name ("Can't determine port");
+ port = ntohs (sockaddr.sin_port);
+ }
+
fprintf (stderr, "Listening on port %d\n", port);
fflush (stderr);