1 /* Native-dependent code for FreeBSD.
3 Copyright (C) 2002-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdbthread.h"
26 #include <sys/types.h>
27 #include <sys/procfs.h>
28 #include <sys/sysctl.h>
33 /* Return the name of a file that can be opened to get the symbols for
34 the child process identified by PID. */
37 fbsd_pid_to_exec_file (struct target_ops
*self
, int pid
)
39 ssize_t len
= PATH_MAX
;
40 static char buf
[PATH_MAX
];
43 #ifdef KERN_PROC_PATHNAME
48 mib
[2] = KERN_PROC_PATHNAME
;
50 if (sysctl (mib
, 4, buf
, &len
, NULL
, 0) == 0)
54 xsnprintf (name
, PATH_MAX
, "/proc/%d/exe", pid
);
55 len
= readlink (name
, buf
, PATH_MAX
- 1);
66 fbsd_read_mapping (FILE *mapfile
, unsigned long *start
, unsigned long *end
,
69 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
71 int resident
, privateresident
;
75 /* As of FreeBSD 5.0-RELEASE, the layout is described in
76 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
77 new column was added to the procfs map. Therefore we can't use
78 fscanf since we need to support older releases too. */
79 if (fgets (buf
, sizeof buf
, mapfile
) != NULL
)
80 ret
= sscanf (buf
, "%lx %lx %d %d %lx %s", start
, end
,
81 &resident
, &privateresident
, &obj
, protection
);
83 return (ret
!= 0 && ret
!= EOF
);
86 /* Iterate over all the memory regions in the current inferior,
87 calling FUNC for each memory region. OBFD is passed as the last
91 fbsd_find_memory_regions (struct target_ops
*self
,
92 find_memory_region_ftype func
, void *obfd
)
94 pid_t pid
= ptid_get_pid (inferior_ptid
);
97 unsigned long start
, end
, size
;
99 int read
, write
, exec
;
100 struct cleanup
*cleanup
;
102 mapfilename
= xstrprintf ("/proc/%ld/map", (long) pid
);
103 cleanup
= make_cleanup (xfree
, mapfilename
);
104 mapfile
= fopen (mapfilename
, "r");
106 error (_("Couldn't open %s."), mapfilename
);
107 make_cleanup_fclose (mapfile
);
110 fprintf_filtered (gdb_stdout
,
111 "Reading memory regions from %s\n", mapfilename
);
113 /* Now iterate until end-of-file. */
114 while (fbsd_read_mapping (mapfile
, &start
, &end
, &protection
[0]))
118 read
= (strchr (protection
, 'r') != 0);
119 write
= (strchr (protection
, 'w') != 0);
120 exec
= (strchr (protection
, 'x') != 0);
124 fprintf_filtered (gdb_stdout
,
125 "Save segment, %ld bytes at %s (%c%c%c)\n",
126 size
, paddress (target_gdbarch (), start
),
132 /* Invoke the callback function to create the corefile segment.
133 Pass MODIFIED as true, we do not know the real modification state. */
134 func (start
, size
, read
, write
, exec
, 1, obfd
);
137 do_cleanups (cleanup
);