patch to gdbserver for names instead of pid numbers

glenn_engel
2002-12-09T22:14:57Z

[-- Attachment #1.1: Type: text/plain, Size: 543 bytes --]

Attached is a patch to gdbserver to allow the remote server to attach to a process by executable name rather than by pid number. It chooses the 'first' process name that matches, assuming it to be the group leader of a multi-threaded application. I found this to be extremely helpful in repeated use of gdbserver.

2002-12-09 Glenn Engel glenne@engel.org <mailto:glenne@engel.org>

* server.c (main): Allow process name in addition to PID number

--
Glenn

glenn_engel@agilent.com / Agilent Laboratories / NN7N / 425-335-2066

[-- Attachment #1.2: Type: text/html, Size: 1383 bytes --]

[-- Attachment #2: gdbserver.patch --]
[-- Type: application/octet-stream, Size: 6146 bytes --]

*** gdb/gdbserver/Makefile.in.orig Mon Dec 9 13:57:29 2002
--- gdb/gdbserver/Makefile.in Mon Dec 9 14:03:34 2002
***************
*** 116,122 ****
SFILES = $(srcdir)/low-hppabsd.c $(srcdir)/low-linux.c $(srcdir)/low-lynx.c \
$(srcdir)/low-nbsd.c $(srcdir)/low-sim.c $(srcdir)/low-sparc.c \
$(srcdir)/low-sun3.c $(srcdir)/utils.c $(srcdir)/server.c \
! $(srcdir)/remote-utils.c

DEPFILES = @GDBSERVER_DEPFILES@

--- 116,122 ----
SFILES = $(srcdir)/low-hppabsd.c $(srcdir)/low-linux.c $(srcdir)/low-lynx.c \
$(srcdir)/low-nbsd.c $(srcdir)/low-sim.c $(srcdir)/low-sparc.c \
$(srcdir)/low-sun3.c $(srcdir)/utils.c $(srcdir)/server.c \
! $(srcdir)/remote-utils.c ($srcdir)/findpid.c

DEPFILES = @GDBSERVER_DEPFILES@

***************
*** 124,130 ****
TAGFILES = $(SOURCES) ${HFILES} ${ALLPARAM} ${POSSLIBS}

OBS = inferiors.o regcache.o remote-utils.o server.o signals.o target.o \
! utils.o \
mem-break.o \
$(DEPFILES)
GDBSERVER_LIBS = @GDBSERVER_LIBS@
--- 124,130 ----
TAGFILES = $(SOURCES) ${HFILES} ${ALLPARAM} ${POSSLIBS}

OBS = inferiors.o regcache.o remote-utils.o server.o signals.o target.o \
! utils.o findpid.o \
mem-break.o \
$(DEPFILES)
GDBSERVER_LIBS = @GDBSERVER_LIBS@
***************
*** 245,250 ****
--- 245,251 ----
proc-service.o: proc-service.c $(server_h) $(gdb_proc_service_h)
regcache.o: regcache.c $(server_h) $(regdef_h)
remote-utils.o: remote-utils.c terminal.h $(server_h)
+ findpid.o: findpid.c findpid.h
server.o: server.c $(server_h)
target.o: target.c $(server_h)
thread-db.o: thread-db.c $(server_h) $(gdb_proc_service_h)
*** gdb/gdbserver/server.c.orig Mon Dec 9 13:24:16 2002
--- gdb/gdbserver/server.c Mon Dec 9 13:51:55 2002
***************
*** 20,25 ****
--- 20,26 ----
Boston, MA 02111-1307, USA. */

#include "server.h"
+ #include "findpid.h"

#include <unistd.h>
#include <signal.h>
***************
*** 131,137 ****
gdbserver_usage (void)
{
error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
! "\tgdbserver COMM --attach PID\n"
"\n"
"COMM may either be a tty device (for serial debugging), or \n"
"HOST:PORT to listen for a TCP connection.\n");
--- 132,138 ----
gdbserver_usage (void)
{
error ("Usage:\tgdbserver COMM PROG [ARGS ...]\n"
! "\tgdbserver COMM --attach PID|NAME\n"
"\n"
"COMM may either be a tty device (for serial debugging), or \n"
"HOST:PORT to listen for a TCP connection.\n");
***************
*** 162,169 ****
{
if (argc == 4
&& argv[3] != '\0'
! && (pid = strtoul (argv[3], &arg_end, 10)) != 0
! && *arg_end == '\0')
{
;
}
--- 163,170 ----
{
if (argc == 4
&& argv[3] != '\0'
! && (((pid = strtoul (argv[3], &arg_end, 10)) != 0 && *arg_end == '\0')
! || ((pid = find_pid_by_name(argv[3])) != -1)))
{
;
}
*** /dev/null Thu Aug 30 13:30:55 2001
--- gdb/gdbserver/findpid.h Mon Dec 9 14:02:55 2002
***************
*** 0 ****
--- 1,4 ----
+ #ifndef FINDPID_H
+ #include <stdlib.h>
+ extern pid_t find_pid_by_name( char* pidName);
+ #endif /* FINDPID_H */
*** /dev/null Thu Aug 30 13:30:55 2001
--- gdb/gdbserver/findpid.c Mon Dec 9 13:30:55 2002
***************
*** 0 ****
--- 1,95 ----
+ /* leveraged from busybox source */
+ /*
+ * Utility routines.
+ *
+ * Copyright (C) tons of folks. Tracking down who wrote what
+ * isn't something I'm going to worry about... If you wrote something
+ * here, please feel free to acknowledge your work.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
+ * Permission has been granted to redistribute this code under the GPL.
+ *
+ */
+
+ #include <stdio.h>
+ #include <ctype.h>
+ #include <dirent.h>
+ #include <stdlib.h>
+
+ #define READ_BUF_SIZE 50
+
+
+ /* find_pid_by_name()
+ *
+ * This finds the pid of the specified process.
+ * Currently, it's implemented by rummaging through
+ * the proc filesystem.
+ *
+ * Returns the first pid matching 'pidName'
+ */
+ extern pid_t find_pid_by_name( char* pidName)
+ {
+ DIR *dir;
+ struct dirent *next;
+
+ dir = opendir("/proc");
+ if (!dir)
+ perror("Cannot open /proc");
+
+ while ((next = readdir(dir)) != NULL) {
+ FILE *status;
+ char filename[READ_BUF_SIZE];
+ char buffer[READ_BUF_SIZE];
+ char name[READ_BUF_SIZE];
+
+ /* Must skip ".." since that is outside /proc */
+ if (strcmp(next->d_name, "..") == 0)
+ continue;
+
+ /* If it isn't a number, we don't want it */
+ if (!isdigit(*next->d_name))
+ continue;
+
+ sprintf(filename, "/proc/%s/status", next->d_name);
+ if (! (status = fopen(filename, "r")) ) {
+ continue;
+ }
+ if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
+ fclose(status);
+ continue;
+ }
+ fclose(status);
+
+ /* Buffer should contain a string like "Name: binary_name" */
+ sscanf(buffer, "%*s %s", name);
+ if (strcmp(name, pidName) == 0)
+ return strtol(next->d_name, NULL, 0);
+ }
+
+ return -1;
+ }
+
+ #ifdef STANDALONE_TEST_FINDPID
+ int main(int argc, char *argv[])
+ {
+ pid_t pid;
+ pid = find_pid_by_name(argv[1]);
+
+ printf("%d \n", pid);
+
+ }
+ #endif