2003-11-06 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / fbsd-proc.c
CommitLineData
a90cd31d
MK
1/* FreeBSD-specific methods for using the /proc file system.
2 Copyright 2002 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
22#include "gdbcore.h"
23#include "inferior.h"
b66d6d2e 24#include "gdb_string.h"
a90cd31d
MK
25
26#include <sys/procfs.h>
27#include <sys/types.h>
28
29#include "elf-bfd.h"
30
31#include "gregset.h"
32
33char *
34child_pid_to_exec_file (int pid)
35{
36 char *path;
37 char *buf;
38
a4e4e501 39 xasprintf (&path, "/proc/%d/file", pid);
a90cd31d
MK
40 buf = xcalloc (MAXPATHLEN, sizeof (char));
41 make_cleanup (xfree, path);
42 make_cleanup (xfree, buf);
43
44 if (readlink (path, buf, MAXPATHLEN) > 0)
45 return buf;
46
47 return NULL;
48}
49
50static int
51read_mapping (FILE *mapfile,
52 unsigned long *start,
53 unsigned long *end,
54 char *protection)
55{
56 int resident, privateresident;
57 unsigned long obj;
58 int ref_count, shadow_count;
59 unsigned flags;
60 char cow[5], access[4];
61 char type[8];
62 int ret;
63
64 /* The layout is described in /usr/src/miscfs/procfs/procfs_map.c. */
65 ret = fscanf (mapfile, "%lx %lx %d %d %lx %s %d %d %x %s %s %s\n",
66 start, end,
67 &resident, &privateresident, &obj,
68 protection,
69 &ref_count, &shadow_count, &flags, cow, access, type);
70
71 return (ret != 0 && ret != EOF);
72}
73
74static int
75fbsd_find_memory_regions (int (*func) (CORE_ADDR,
76 unsigned long,
77 int, int, int,
78 void *),
79 void *obfd)
80{
81 pid_t pid = ptid_get_pid (inferior_ptid);
82 char *mapfilename;
83 FILE *mapfile;
84 unsigned long start, end, size;
85 char protection[4];
86 int read, write, exec;
87
a4e4e501 88 xasprintf (&mapfilename, "/proc/%ld/map", (long) pid);
a90cd31d
MK
89 mapfile = fopen (mapfilename, "r");
90 if (mapfile == NULL)
91 error ("Couldn't open %s\n", mapfilename);
92
93 if (info_verbose)
94 fprintf_filtered (gdb_stdout,
95 "Reading memory regions from %s\n", mapfilename);
96
97 /* Now iterate until end-of-file. */
98 while (read_mapping (mapfile, &start, &end, &protection[0]))
99 {
100 size = end - start;
101
102 read = (strchr (protection, 'r') != 0);
103 write = (strchr (protection, 'w') != 0);
104 exec = (strchr (protection, 'x') != 0);
105
106 if (info_verbose)
107 {
108 fprintf_filtered (gdb_stdout,
109 "Save segment, %ld bytes at 0x%s (%c%c%c)\n",
110 size, paddr_nz (start),
111 read ? 'r' : '-',
112 write ? 'w' : '-',
113 exec ? 'x' : '-');
114 }
115
116 /* Invoke the callback function to create the corefile segment. */
117 func (start, size, read, write, exec, obfd);
118 }
119
120 fclose (mapfile);
121 return 0;
122}
123
124static char *
125fbsd_make_corefile_notes (bfd *obfd, int *note_size)
126{
127 gregset_t gregs;
128 fpregset_t fpregs;
129 char *note_data = NULL;
9a12a2a0
MK
130 Elf_Internal_Ehdr *i_ehdrp;
131
132 /* Put a "FreeBSD" label in the ELF header. */
133 i_ehdrp = elf_elfheader (obfd);
134 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
a90cd31d
MK
135
136 fill_gregset (&gregs, -1);
25e60c9b
MK
137 note_data = elfcore_write_prstatus (obfd, note_data, note_size,
138 ptid_get_pid (inferior_ptid),
139 stop_signal, &gregs);
a90cd31d
MK
140
141 fill_fpregset (&fpregs, -1);
25e60c9b
MK
142 note_data = elfcore_write_prfpreg (obfd, note_data, note_size,
143 &fpregs, sizeof (fpregs));
a90cd31d
MK
144
145 if (get_exec_file (0))
146 {
147 char *fname = strrchr (get_exec_file (0), '/') + 1;
a4e4e501 148 char *psargs = xstrdup (fname);
a90cd31d
MK
149
150 if (get_inferior_args ())
151 psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
152
25e60c9b
MK
153 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
154 fname, psargs);
a90cd31d
MK
155 }
156
157 make_cleanup (xfree, note_data);
158 return note_data;
159}
160\f
161
162void
163_initialize_fbsd_proc (void)
164{
165 extern void inftarg_set_find_memory_regions ();
166 extern void inftarg_set_make_corefile_notes ();
167
168 inftarg_set_find_memory_regions (fbsd_find_memory_regions);
169 inftarg_set_make_corefile_notes (fbsd_make_corefile_notes);
170}
This page took 0.190341 seconds and 4 git commands to generate.