Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: glenn_engel@agilent.com
To: gdb-patches@sources.redhat.com
Subject: patch to gdbserver for names instead of pid numbers
Date: Mon, 09 Dec 2002 14:33:00 -0000	[thread overview]
Message-ID: <FC0B9DA2600ED4118F76009027AA5DDD06B3F0FA@ALEX2> (raw)


[-- 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

             reply	other threads:[~2002-12-09 22:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-09 14:33 glenn_engel [this message]
2002-12-10 11:37 ` Daniel Jacobowitz
2002-12-10 11:40 glenn_engel
2002-12-10 12:01 ` Andrew Cagney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=FC0B9DA2600ED4118F76009027AA5DDD06B3F0FA@ALEX2 \
    --to=glenn_engel@agilent.com \
    --cc=gdb-patches@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox