* gdb.cp/gdb2495.exp: Skip if gdb,nosignals.
[deliverable/binutils-gdb.git] / gdb / auxv.c
CommitLineData
14ed0a8b
RM
1/* Auxiliary vector support for GDB, the GNU debugger.
2
4c38e0a4 3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 4 Free Software Foundation, Inc.
14ed0a8b
RM
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
14ed0a8b
RM
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14ed0a8b
RM
20
21#include "defs.h"
22#include "target.h"
23#include "gdbtypes.h"
24#include "command.h"
25#include "inferior.h"
26#include "valprint.h"
27#include "gdb_assert.h"
28
29#include "auxv.h"
30#include "elf/common.h"
31
32#include <unistd.h>
33#include <fcntl.h>
34
35
81b92222
PA
36/* This function is called like a to_xfer_partial hook, but must be
37 called with TARGET_OBJECT_AUXV. It handles access via
38 /proc/PID/auxv, which is a common method for native targets. */
14ed0a8b
RM
39
40LONGEST
41procfs_xfer_auxv (struct target_ops *ops,
81b92222 42 enum target_object object,
14ed0a8b 43 const char *annex,
36aa5e41
AC
44 gdb_byte *readbuf,
45 const gdb_byte *writebuf,
14ed0a8b
RM
46 ULONGEST offset,
47 LONGEST len)
48{
49 char *pathname;
50 int fd;
51 LONGEST n;
52
53 gdb_assert (object == TARGET_OBJECT_AUXV);
54 gdb_assert (readbuf || writebuf);
55
56 pathname = xstrprintf ("/proc/%d/auxv", PIDGET (inferior_ptid));
57 fd = open (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY);
58 xfree (pathname);
59 if (fd < 0)
60 return -1;
61
62 if (offset != (ULONGEST) 0
63 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
64 n = -1;
65 else if (readbuf != NULL)
66 n = read (fd, readbuf, len);
67 else
68 n = write (fd, writebuf, len);
69
70 (void) close (fd);
71
72 return n;
73}
74
14ed0a8b
RM
75/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
76 Return 0 if *READPTR is already at the end of the buffer.
77 Return -1 if there is insufficient buffer for a whole entry.
78 Return 1 if an entry was read into *TYPEP and *VALP. */
2c0b251b 79static int
c47ffbe3 80default_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
36aa5e41 81 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
14ed0a8b 82{
ffe5a37e
UW
83 const int sizeof_auxv_field = gdbarch_ptr_bit (target_gdbarch)
84 / TARGET_CHAR_BIT;
e17a4113 85 const enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
36aa5e41 86 gdb_byte *ptr = *readptr;
14ed0a8b
RM
87
88 if (endptr == ptr)
89 return 0;
90
91 if (endptr - ptr < sizeof_auxv_field * 2)
92 return -1;
93
e17a4113 94 *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
14ed0a8b 95 ptr += sizeof_auxv_field;
e17a4113 96 *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
14ed0a8b
RM
97 ptr += sizeof_auxv_field;
98
99 *readptr = ptr;
100 return 1;
101}
102
c47ffbe3
VP
103/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
104 Return 0 if *READPTR is already at the end of the buffer.
105 Return -1 if there is insufficient buffer for a whole entry.
106 Return 1 if an entry was read into *TYPEP and *VALP. */
107int
108target_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
109 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
110{
111 struct target_ops *t;
112 for (t = ops; t != NULL; t = t->beneath)
113 if (t->to_auxv_parse != NULL)
114 return t->to_auxv_parse (t, readptr, endptr, typep, valp);
115
116 return default_auxv_parse (ops, readptr, endptr, typep, valp);
117}
118
14ed0a8b
RM
119/* Extract the auxiliary vector entry with a_type matching MATCH.
120 Return zero if no such entry was found, or -1 if there was
121 an error getting the information. On success, return 1 after
122 storing the entry's value field in *VALP. */
123int
124target_auxv_search (struct target_ops *ops, CORE_ADDR match, CORE_ADDR *valp)
125{
126 CORE_ADDR type, val;
36aa5e41 127 gdb_byte *data;
13547ab6 128 LONGEST n = target_read_alloc (ops, TARGET_OBJECT_AUXV, NULL, &data);
36aa5e41 129 gdb_byte *ptr = data;
14ed0a8b
RM
130 int ents = 0;
131
132 if (n <= 0)
133 return n;
134
135 while (1)
136 switch (target_auxv_parse (ops, &ptr, data + n, &type, &val))
137 {
138 case 1: /* Here's an entry, check it. */
139 if (type == match)
140 {
141 xfree (data);
142 *valp = val;
143 return 1;
144 }
145 break;
146 case 0: /* End of the vector. */
147 xfree (data);
148 return 0;
149 default: /* Bogosity. */
150 xfree (data);
151 return -1;
152 }
153
154 /*NOTREACHED*/
155}
156
157
158/* Print the contents of the target's AUXV on the specified file. */
159int
160fprint_target_auxv (struct ui_file *file, struct target_ops *ops)
161{
162 CORE_ADDR type, val;
36aa5e41 163 gdb_byte *data;
13547ab6
DJ
164 LONGEST len = target_read_alloc (ops, TARGET_OBJECT_AUXV, NULL,
165 &data);
36aa5e41 166 gdb_byte *ptr = data;
14ed0a8b
RM
167 int ents = 0;
168
169 if (len <= 0)
170 return len;
171
172 while (target_auxv_parse (ops, &ptr, data + len, &type, &val) > 0)
173 {
14ed0a8b
RM
174 const char *name = "???";
175 const char *description = "";
176 enum { dec, hex, str } flavor = hex;
177
178 switch (type)
179 {
180#define TAG(tag, text, kind) \
181 case tag: name = #tag; description = text; flavor = kind; break
edefbb7c
AC
182 TAG (AT_NULL, _("End of vector"), hex);
183 TAG (AT_IGNORE, _("Entry should be ignored"), hex);
184 TAG (AT_EXECFD, _("File descriptor of program"), dec);
185 TAG (AT_PHDR, _("Program headers for program"), hex);
186 TAG (AT_PHENT, _("Size of program header entry"), dec);
187 TAG (AT_PHNUM, _("Number of program headers"), dec);
188 TAG (AT_PAGESZ, _("System page size"), dec);
189 TAG (AT_BASE, _("Base address of interpreter"), hex);
190 TAG (AT_FLAGS, _("Flags"), hex);
191 TAG (AT_ENTRY, _("Entry point of program"), hex);
192 TAG (AT_NOTELF, _("Program is not ELF"), dec);
193 TAG (AT_UID, _("Real user ID"), dec);
194 TAG (AT_EUID, _("Effective user ID"), dec);
195 TAG (AT_GID, _("Real group ID"), dec);
196 TAG (AT_EGID, _("Effective group ID"), dec);
197 TAG (AT_CLKTCK, _("Frequency of times()"), dec);
198 TAG (AT_PLATFORM, _("String identifying platform"), str);
199 TAG (AT_HWCAP, _("Machine-dependent CPU capability hints"), hex);
200 TAG (AT_FPUCW, _("Used FPU control word"), dec);
201 TAG (AT_DCACHEBSIZE, _("Data cache block size"), dec);
202 TAG (AT_ICACHEBSIZE, _("Instruction cache block size"), dec);
203 TAG (AT_UCACHEBSIZE, _("Unified cache block size"), dec);
204 TAG (AT_IGNOREPPC, _("Entry should be ignored"), dec);
759cc328 205 TAG (AT_BASE_PLATFORM, _("String identifying base platform"), str);
ba30a4e9 206 TAG (AT_RANDOM, _("Address of 16 random bytes"), hex);
759cc328
UW
207 TAG (AT_EXECFN, _("File name of executable"), str);
208 TAG (AT_SECURE, _("Boolean, was exec setuid-like?"), dec);
edefbb7c
AC
209 TAG (AT_SYSINFO, _("Special system info/entry points"), hex);
210 TAG (AT_SYSINFO_EHDR, _("System-supplied DSO's ELF header"), hex);
edefbb7c
AC
211 TAG (AT_SUN_UID, _("Effective user ID"), dec);
212 TAG (AT_SUN_RUID, _("Real user ID"), dec);
213 TAG (AT_SUN_GID, _("Effective group ID"), dec);
214 TAG (AT_SUN_RGID, _("Real group ID"), dec);
215 TAG (AT_SUN_LDELF, _("Dynamic linker's ELF header"), hex);
216 TAG (AT_SUN_LDSHDR, _("Dynamic linker's section headers"), hex);
217 TAG (AT_SUN_LDNAME, _("String giving name of dynamic linker"), str);
218 TAG (AT_SUN_LPAGESZ, _("Large pagesize"), dec);
219 TAG (AT_SUN_PLATFORM, _("Platform name string"), str);
220 TAG (AT_SUN_HWCAP, _("Machine-dependent CPU capability hints"), hex);
221 TAG (AT_SUN_IFLUSH, _("Should flush icache?"), dec);
222 TAG (AT_SUN_CPU, _("CPU name string"), str);
223 TAG (AT_SUN_EMUL_ENTRY, _("COFF entry point address"), hex);
224 TAG (AT_SUN_EMUL_EXECFD, _("COFF executable file descriptor"), dec);
14ed0a8b 225 TAG (AT_SUN_EXECNAME,
edefbb7c
AC
226 _("Canonicalized file name given to execve"), str);
227 TAG (AT_SUN_MMU, _("String for name of MMU module"), str);
228 TAG (AT_SUN_LDDATA, _("Dynamic linker's data segment address"), hex);
77d49ac6
MK
229 TAG (AT_SUN_AUXFLAGS,
230 _("AF_SUN_ flags passed from the kernel"), hex);
14ed0a8b
RM
231 }
232
233 fprintf_filtered (file, "%-4s %-20s %-30s ",
623d3eb1 234 plongest (type), name, description);
14ed0a8b
RM
235 switch (flavor)
236 {
237 case dec:
623d3eb1 238 fprintf_filtered (file, "%s\n", plongest (val));
14ed0a8b
RM
239 break;
240 case hex:
5af949e3 241 fprintf_filtered (file, "%s\n", paddress (target_gdbarch, val));
14ed0a8b
RM
242 break;
243 case str:
79a45b7d
TT
244 {
245 struct value_print_options opts;
246 get_user_print_options (&opts);
247 if (opts.addressprint)
5af949e3 248 fprintf_filtered (file, "%s", paddress (target_gdbarch, val));
6c7a06a3
TT
249 val_print_string (builtin_type (target_gdbarch)->builtin_char,
250 val, -1, file, &opts);
79a45b7d
TT
251 fprintf_filtered (file, "\n");
252 }
14ed0a8b
RM
253 break;
254 }
255 ++ents;
7c6467a4
PP
256 if (type == AT_NULL)
257 break;
14ed0a8b
RM
258 }
259
260 xfree (data);
261
262 return ents;
263}
264
265static void
266info_auxv_command (char *cmd, int from_tty)
267{
14ed0a8b 268 if (! target_has_stack)
edefbb7c 269 error (_("The program has no auxiliary information now."));
14ed0a8b
RM
270 else
271 {
272 int ents = fprint_target_auxv (gdb_stdout, &current_target);
273 if (ents < 0)
edefbb7c 274 error (_("No auxiliary vector found, or failed reading it."));
14ed0a8b 275 else if (ents == 0)
edefbb7c 276 error (_("Auxiliary vector is empty."));
14ed0a8b
RM
277 }
278}
279
280
281extern initialize_file_ftype _initialize_auxv; /* -Wmissing-prototypes; */
282
283void
284_initialize_auxv (void)
285{
286 add_info ("auxv", info_auxv_command,
edefbb7c
AC
287 _("Display the inferior's auxiliary vector.\n\
288This is information provided by the operating system at program startup."));
14ed0a8b 289}
This page took 0.334482 seconds and 4 git commands to generate.