This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

OpenBSD, implement target_pid_to_exec_file


Hi,

I was testing a couple of things in OpenBSD, and noticed that
when attaching to a running process, I have to specify the
exec file manually.  In some/most native ports, if we don't specify
an exec file, when attaching, GDB will try to find it by
pid.  This matching is implemented by the target_pid_to_exec_file
target op, but, the OpenBSD port doesn't currently implement it.

The attached patch fixes it.  Using kvm_openfiles with KVM_NO_FILES,
still gives you access to kvm_getargv, so you don't need special
privileges to access /dev/mem.  kvm_getargv gives you a full path
to the program.

Old:

./gdb --quiet
Setting up the environment for debugging gdb.
(gdb) attach 31542
Attaching to process 31542
0x0d480b15 in ?? ()
(gdb)

New:

./gdb --quiet
(gdb) attach 31542
Attaching to process 31542
Reading symbols 
from /net/home/pedro/gdb/multi_process/build-openbsd43/gdb/gdb...done.
Reading symbols from /usr/lib/libncurses.so.10.0...done.
Loaded symbols for /usr/lib/libncurses.so.10.0
Reading symbols from /usr/lib/libz.so.4.1...done.
Loaded symbols for /usr/lib/libz.so.4.1
Reading symbols from /usr/lib/libm.so.2.3...done.
Loaded symbols for /usr/lib/libm.so.2.3
Reading symbols from /usr/lib/libexpat.so.9.0...done.
Loaded symbols for /usr/lib/libexpat.so.9.0
Reading symbols from /usr/lib/libkvm.so.9.0...done.
Loaded symbols for /usr/lib/libkvm.so.9.0
Reading symbols from /usr/lib/libc.so.43.0...done.
Loaded symbols for /usr/lib/libc.so.43.0
Reading symbols from /usr/libexec/ld.so...done.
Loaded symbols for /usr/libexec/ld.so
[Switching to process 31542]
0x0d480b15 in poll () from /usr/lib/libc.so.43.0
(gdb)

This makes attach.exp pass cleanly, and there's no regressions
on i386-unknown-openbsd4.3.

A few notes/questions:

- Is this the right API to use?
- I don't know what is the max size of argv[0] on OpenBSD, so I made it
  heap based.  Is it _POSIX_PATH_MAX?  Or, should I leave it?
- I tested this on x86 OpenBSD 4.3.  I tweaked several archs, but not
  all.  Specifically, I didn't touch the ones that didn't have a
  ${arch}obsd-nat.c file.  I'll fix those up too if this patch is
  (mostly) OK.
- I've added warnings if this fails, but I'm not sure if this either
  can fail in some other versions I don't have access to -- the warning
  would perhaps be annoying then.
- Perhaps using this API is obvious to OpenBSD people, and I should
  then remove the "In at least OpenBSD 4.3...", or make it a reference
  to which version is required, if it doesn't work in all.

Otherwise,

 OK?

-- 
Pedro Alves
2008-08-13  Pedro Alves  <pedro@codesourcery.com>

	* obsd-nat.h: New.
	* obsd-nat.c: New.

	* amd64obsd-nat.c: Include obsd-nat.h.
	(_initialize_amd64obsd_nat): Register obsd_pid_to_exec_file.
	* i386obsd-nat.c: Include obsd-nat.h.
	(_initialize_i386obsd_nat): Register obsd_pid_to_exec_file.
	* mips64obsd-nat.c Include obsd-nat.h.
	(_initialize_mips64obsd_nat): Register obsd_pid_to_exec_file.
	* ppcobsd-nat.c: Include obsd-nat.h.
	(_initialize_ppcobsd_nat): Register obsd_pid_to_exec_file.

	* config/i386/obsd.mh (NATPDEPFILES): Add obsd-nat.o.
	* config/i386/obsd64.mh (NATPDEPFILES): Ditto.
	* config/i386/obsdaout.mh (NATPDEPFILES): Ditto.
	* config/powerpc/obsd.mh (NATPDEPFILES): Ditto.
	* config/mips/obsd64.mh (NATPDEPFILES): Ditto.
	(LOADLIBES): Set to -lkvm.

	* Makefile.in (ALLDEPFILES): Add obsd-nat.c.

---
 gdb/Makefile.in             |    1 
 gdb/amd64obsd-nat.c         |    9 +++-
 gdb/config/i386/obsd.mh     |    2 -
 gdb/config/i386/obsd64.mh   |    2 -
 gdb/config/i386/obsdaout.mh |    2 -
 gdb/config/mips/obsd64.mh   |    4 +-
 gdb/config/powerpc/obsd.mh  |    2 -
 gdb/i386obsd-nat.c          |    9 +++-
 gdb/mips64obsd-nat.c        |    3 +
 gdb/obsd-nat.c              |   81 ++++++++++++++++++++++++++++++++++++++++++++
 gdb/obsd-nat.h              |   28 +++++++++++++++
 gdb/ppcobsd-nat.c           |    2 +
 12 files changed, 136 insertions(+), 9 deletions(-)

Index: src/gdb/obsd-nat.h
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ src/gdb/obsd-nat.h	2008-08-13 02:02:23.000000000 +0100
@@ -0,0 +1,28 @@
+/* Native-dependent code for OpenBSD.
+
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef OBSD_NAT_H
+#define OBSD_NAT_H
+
+/* Return a the name of file that can be opened to get the symbols for
+   the child process identified by PID.  */
+
+extern char *obsd_pid_to_exec_file (int pid);
+
+#endif /* obsd-nat.h */
Index: src/gdb/obsd-nat.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ src/gdb/obsd-nat.c	2008-08-13 02:02:23.000000000 +0100
@@ -0,0 +1,81 @@
+/* Native-dependent code for OpenBSD.
+
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "target.h"
+
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <kvm.h>
+
+/* Buffer returned by obsd_pid_to_exec_file.  */
+static char *pid_to_exec_file_retbuf = NULL;
+
+/* Return a the name of file that can be opened to get the symbols for
+   the child process identified by PID.  */
+
+char *
+obsd_pid_to_exec_file (int pid)
+{
+  int nentries;
+  kvm_t *kd;
+  struct kinfo_proc *kp;
+  char **argv_p;
+  char errbuf[_POSIX2_LINE_MAX];
+
+  xfree (pid_to_exec_file_retbuf);
+  pid_to_exec_file_retbuf = NULL;
+
+  /* In at least OpenBSD 4.3, specifying KVM_NO_FILES allows
+     unpriviledged access to a minimal number of things, kvm_getargv
+     included.  */
+  kd = kvm_openfiles (NULL, NULL, NULL, KVM_NO_FILES, errbuf);
+  if (kd == 0)
+    {
+      warning ("%s", errbuf);
+      return NULL;
+    }
+
+  kp = kvm_getprocs (kd, KERN_PROC_PID, pid, &nentries);
+  if (kp == 0)
+    {
+      warning ("%s", kvm_geterr (kd));
+      return NULL;
+    }
+
+  argv_p = kvm_getargv (kd, kp, 0);
+  if (argv_p == 0)
+    {
+      warning ("%s", kvm_geterr (kd));
+      return NULL;
+    }
+
+  pid_to_exec_file_retbuf = strdup (argv_p[0]);
+  return pid_to_exec_file_retbuf;
+}
+
+/* Prevent warning from -Wmissing-prototypes.  */
+void _initialize_obsd_nat (void);
+
+void
+_initialize_obsd_nat (void)
+{
+  /* Free up the last buffer used.  */
+  make_final_cleanup (xfree, pid_to_exec_file_retbuf);
+}
Index: src/gdb/amd64obsd-nat.c
===================================================================
--- src.orig/gdb/amd64obsd-nat.c	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/amd64obsd-nat.c	2008-08-13 02:02:23.000000000 +0100
@@ -26,6 +26,7 @@
 
 #include "amd64-tdep.h"
 #include "amd64-nat.h"
+#include "obsd-nat.h"
 
 /* Mapping between the general-purpose registers in OpenBSD/amd64
    `struct reg' format and GDB's register cache layout for
@@ -133,12 +134,16 @@ void _initialize_amd64obsd_nat (void);
 void
 _initialize_amd64obsd_nat (void)
 {
+  struct target_ops *t;
+
   amd64_native_gregset32_reg_offset = amd64obsd32_r_reg_offset;
   amd64_native_gregset32_num_regs = ARRAY_SIZE (amd64obsd32_r_reg_offset);
   amd64_native_gregset64_reg_offset = amd64obsd_r_reg_offset;
 
-  /* We've got nothing to add to the common *BSD/amd64 target.  */
-  add_target (amd64bsd_target ());
+  /* Add in local overrides.  */
+  t = amd64bsd_target ();
+  t->to_pid_to_exec_file = obsd_pid_to_exec_file;
+  add_target (t);
 
   /* Support debugging kernel virtual memory images.  */
   bsd_kvm_add_target (amd64obsd_supply_pcb);
Index: src/gdb/i386obsd-nat.c
===================================================================
--- src.orig/gdb/i386obsd-nat.c	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/i386obsd-nat.c	2008-08-13 02:02:23.000000000 +0100
@@ -28,6 +28,7 @@
 #include <machine/frame.h>
 #include <machine/pcb.h>
 
+#include "obsd-nat.h"
 #include "i386-tdep.h"
 #include "i386bsd-nat.h"
 #include "bsd-kvm.h"
@@ -94,8 +95,12 @@ void _initialize_i386obsd_nat (void);
 void
 _initialize_i386obsd_nat (void)
 {
-  /* We've got nothing to add to the common *BSD/i386 target.  */
-  add_target (i386bsd_target ());
+  struct target_ops *t;
+
+  /* Add in local overrides.  */
+  t = i386bsd_target ();
+  t->to_pid_to_exec_file = obsd_pid_to_exec_file;
+  add_target (t);
 
   /* Support debugging kernel virtual memory images.  */
   bsd_kvm_add_target (i386obsd_supply_pcb);
Index: src/gdb/mips64obsd-nat.c
===================================================================
--- src.orig/gdb/mips64obsd-nat.c	2008-08-12 18:15:37.000000000 +0100
+++ src/gdb/mips64obsd-nat.c	2008-08-13 02:02:23.000000000 +0100
@@ -29,6 +29,8 @@
 #include "mips-tdep.h"
 #include "inf-ptrace.h"
 
+#include "obsd-nat.h"
+
 /* Shorthand for some register numbers used below.  */
 #define MIPS_PC_REGNUM	MIPS_EMBED_PC_REGNUM
 #define MIPS_FP0_REGNUM	MIPS_EMBED_FP0_REGNUM
@@ -119,5 +121,6 @@ _initialize_mips64obsd_nat (void)
   t = inf_ptrace_target ();
   t->to_fetch_registers = mips64obsd_fetch_inferior_registers;
   t->to_store_registers = mips64obsd_store_inferior_registers;
+  t->to_pid_to_exec_file = obsd_pid_to_exec_file;
   add_target (t);
 }
Index: src/gdb/ppcobsd-nat.c
===================================================================
--- src.orig/gdb/ppcobsd-nat.c	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/ppcobsd-nat.c	2008-08-13 02:02:23.000000000 +0100
@@ -35,6 +35,7 @@
 #include "ppcobsd-tdep.h"
 #include "inf-ptrace.h"
 #include "bsd-kvm.h"
+#include "obsd-nat.h"
 
 /* OpenBSD/powerpc didn't have PT_GETFPREGS/PT_SETFPREGS until release
    4.0.  On older releases the floating-point registers are handled by
@@ -197,6 +198,7 @@ _initialize_ppcobsd_nat (void)
   t = inf_ptrace_target ();
   t->to_fetch_registers = ppcobsd_fetch_registers;
   t->to_store_registers = ppcobsd_store_registers;
+  t->to_pid_to_exec_file = obsd_pid_to_exec_file;
   add_target (t);
 
   /* General-purpose registers.  */
Index: src/gdb/config/i386/obsd.mh
===================================================================
--- src.orig/gdb/config/i386/obsd.mh	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/config/i386/obsd.mh	2008-08-13 02:02:23.000000000 +0100
@@ -1,5 +1,5 @@
 # Host: OpenBSD/i386 ELF
 NATDEPFILES= fork-child.o inf-ptrace.o \
-	i386bsd-nat.o i386obsd-nat.o bsd-kvm.o
+	i386bsd-nat.o i386obsd-nat.o obsd-nat.o bsd-kvm.o
 
 LOADLIBES= -lkvm
Index: src/gdb/config/i386/obsd64.mh
===================================================================
--- src.orig/gdb/config/i386/obsd64.mh	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/config/i386/obsd64.mh	2008-08-13 02:02:23.000000000 +0100
@@ -1,5 +1,5 @@
 # Host: OpenBSD/amd64
 NATDEPFILES= fork-child.o inf-ptrace.o \
-	amd64-nat.o amd64bsd-nat.o amd64obsd-nat.o bsd-kvm.o
+	amd64-nat.o amd64bsd-nat.o amd64obsd-nat.o obsd-nat.o bsd-kvm.o
 
 LOADLIBES= -lkvm
Index: src/gdb/config/i386/obsdaout.mh
===================================================================
--- src.orig/gdb/config/i386/obsdaout.mh	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/config/i386/obsdaout.mh	2008-08-13 02:02:23.000000000 +0100
@@ -1,6 +1,6 @@
 # Host: OpenBSD/i386 a.out
 NATDEPFILES= fork-child.o inf-ptrace.o \
-	i386bsd-nat.o i386nbsd-nat.o i386obsd-nat.o bsd-kvm.o \
+	i386bsd-nat.o i386nbsd-nat.o i386obsd-nat.o obsd-nat.o bsd-kvm.o \
 	solib.o solib-sunos.o
 
 LOADLIBES= -lkvm
Index: src/gdb/config/powerpc/obsd.mh
===================================================================
--- src.orig/gdb/config/powerpc/obsd.mh	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/config/powerpc/obsd.mh	2008-08-13 02:02:23.000000000 +0100
@@ -1,4 +1,4 @@
 # Host: OpenBSD/powerpc
-NATDEPFILES= fork-child.o inf-ptrace.o ppcobsd-nat.o bsd-kvm.o
+NATDEPFILES= fork-child.o inf-ptrace.o ppcobsd-nat.o obsd-nat.o bsd-kvm.o
 
 LOADLIBES= -lkvm
Index: src/gdb/config/mips/obsd64.mh
===================================================================
--- src.orig/gdb/config/mips/obsd64.mh	2008-08-12 18:15:38.000000000 +0100
+++ src/gdb/config/mips/obsd64.mh	2008-08-13 02:39:35.000000000 +0100
@@ -1,2 +1,4 @@
 # Host: OpenBSD/mips64
-NATDEPFILES= fork-child.o inf-ptrace.o mips64obsd-nat.o
+NATDEPFILES= fork-child.o inf-ptrace.o mips64obsd-nat.o obsd-nat.o
+
+LOADLIBES= -lkvm
Index: src/gdb/Makefile.in
===================================================================
--- src.orig/gdb/Makefile.in	2008-08-12 18:15:37.000000000 +0100
+++ src/gdb/Makefile.in	2008-08-13 02:02:23.000000000 +0100
@@ -1311,6 +1311,7 @@ ALLDEPFILES = \
 	solib-osf.c \
 	solib-target.c \
 	somread.c solib-som.c \
+	obsd-nat.c \
 	posix-hdep.c \
 	ppc-sysv-tdep.c ppc-linux-nat.c ppc-linux-tdep.c \
 	ppcnbsd-nat.c ppcnbsd-tdep.c \

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]