gdb/1922: `gdb -p' fails on FreeBSD unless procfs is mounted

das@FreeBSD.ORG das@FreeBSD.ORG
Tue Apr 19 05:18:00 GMT 2005

>Number:         1922
>Category:       gdb
>Synopsis:       `gdb -p' fails on FreeBSD unless procfs is mounted
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          patch
>Submitter-Id:   net
>Arrival-Date:   Tue Apr 19 05:18:00 UTC 2005
>Originator:     das@FreeBSD.ORG
>Release:        6.1.1
FreeBSD VARK.MIT.EDU 6.0-CURRENT FreeBSD 6.0-CURRENT #7: Sun Apr 17 20:58:41 EDT 2005 das@VARK.MIT.EDU:/usr/scratch/vark/usr/home/t/freebsd/vark/src/sys/GENERIC  i386
Invoking `gdb -p' on FreeBSD without procfs mounted causes gdb to crash because it can't find the process' executable file.  Subsequently, the target process is killed.  There are really two problems here:

(1) gdb should use a different mechanism to find the
    text file on FreeBSD.

(2) gdb should cleanly detach from the process when it
    detects an internal error such as this so the
    process isn't killed.

The patch below solves problem (1).  However, it uses a feature that is only available in FreeBSD 6-CURRENT; gdb should fall back on procfs for older versions of FreeBSD.  (Note that on older versions of FreeBSD, the sysctl will simply return an error.)  Hopefully the patch will give someone with copyright assignment paperwork on file the necessary ideas to implement the complete solution (which requires fewer than 20 lines of code.)

Note that it isn't acceptable to simply say ``use procfs'', since procfs is disabled by default on FreeBSD due to its reputation for security problems.  I realize that there are other things in gdb that rely on procfs, but failure to attach to processes is perhaps the most glaring problem.

If someone can solve problem (2), that's great, although I'd be happy enough if someone simply addressed (1).  Thanks in advance!
> gdb -p xxxx
solib-svr4.c:1307: internal-error: legacy_fetch_link_map_offsets called without legacy link_map support enabled.
A problem internal to GDB has been detected, further debugging may prove unreliable.
Quit this debugging session? (y or n) y
Create a core file of GDB? (y or n) n
[both gdb and process xxxx die]
Index: contrib/gdb/gdb/fbsd-proc.c
RCS file: /cvs/src/contrib/gdb/gdb/fbsd-proc.c,v
retrieving revision
diff -u -r1.1.1.1 fbsd-proc.c
--- contrib/gdb/gdb/fbsd-proc.c 20 Jun 2004 18:16:56 -0000
+++ contrib/gdb/gdb/fbsd-proc.c 17 Apr 2005 01:40:58 -0000
@@ -26,6 +26,7 @@
 #include <sys/procfs.h>
 #include <sys/types.h>
+#include <sys/sysctl.h>
 #include "elf-bfd.h"
@@ -36,16 +37,20 @@
   char *path;
   char *buf;
+  int oid[4];
+  size_t buflen;
-  xasprintf (&path, "/proc/%d/file", pid);
   buf = xcalloc (MAXPATHLEN, sizeof (char));
-  make_cleanup (xfree, path);
+  buflen = MAXPATHLEN;
   make_cleanup (xfree, buf);
-  if (readlink (path, buf, MAXPATHLEN) > 0)
+  oid[0] = CTL_KERN;
+  oid[1] = KERN_PROC;
+  oid[3] = pid;
+  if (sysctl(oid, 4, buf, &buflen, 0, 0) == 0)
     return buf;
-  return NULL;
+  else
+    return NULL;
 static int

More information about the Gdb-prs mailing list