Copyright updates for 2007.
[external/binutils.git] / gdb / auxv.c
1 /* Auxiliary vector support for GDB, the GNU debugger.
2
3    Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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 2 of the License, or
10    (at your option) any later version.
11
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.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 #include "defs.h"
23 #include "target.h"
24 #include "gdbtypes.h"
25 #include "command.h"
26 #include "inferior.h"
27 #include "valprint.h"
28 #include "gdb_assert.h"
29
30 #include "auxv.h"
31 #include "elf/common.h"
32
33 #include <unistd.h>
34 #include <fcntl.h>
35
36
37 /* This function is called like a to_xfer_partial hook,
38    but must be called with TARGET_OBJECT_AUXV.
39    It handles access via /proc/PID/auxv, which is the common method.
40    This function is appropriate for doing:
41            #define NATIVE_XFER_AUXV     procfs_xfer_auxv
42    for a native target that uses inftarg.c's child_xfer_partial hook.  */
43
44 LONGEST
45 procfs_xfer_auxv (struct target_ops *ops,
46                   int /* enum target_object */ object,
47                   const char *annex,
48                   gdb_byte *readbuf,
49                   const gdb_byte *writebuf,
50                   ULONGEST offset,
51                   LONGEST len)
52 {
53   char *pathname;
54   int fd;
55   LONGEST n;
56
57   gdb_assert (object == TARGET_OBJECT_AUXV);
58   gdb_assert (readbuf || writebuf);
59
60   pathname = xstrprintf ("/proc/%d/auxv", PIDGET (inferior_ptid));
61   fd = open (pathname, writebuf != NULL ? O_WRONLY : O_RDONLY);
62   xfree (pathname);
63   if (fd < 0)
64     return -1;
65
66   if (offset != (ULONGEST) 0
67       && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
68     n = -1;
69   else if (readbuf != NULL)
70     n = read (fd, readbuf, len);
71   else
72     n = write (fd, writebuf, len);
73
74   (void) close (fd);
75
76   return n;
77 }
78
79 /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
80    Return 0 if *READPTR is already at the end of the buffer.
81    Return -1 if there is insufficient buffer for a whole entry.
82    Return 1 if an entry was read into *TYPEP and *VALP.  */
83 int
84 target_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
85                    gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
86 {
87   const int sizeof_auxv_field = TYPE_LENGTH (builtin_type_void_data_ptr);
88   gdb_byte *ptr = *readptr;
89
90   if (endptr == ptr)
91     return 0;
92
93   if (endptr - ptr < sizeof_auxv_field * 2)
94     return -1;
95
96   *typep = extract_unsigned_integer (ptr, sizeof_auxv_field);
97   ptr += sizeof_auxv_field;
98   *valp = extract_unsigned_integer (ptr, sizeof_auxv_field);
99   ptr += sizeof_auxv_field;
100
101   *readptr = ptr;
102   return 1;
103 }
104
105 /* Extract the auxiliary vector entry with a_type matching MATCH.
106    Return zero if no such entry was found, or -1 if there was
107    an error getting the information.  On success, return 1 after
108    storing the entry's value field in *VALP.  */
109 int
110 target_auxv_search (struct target_ops *ops, CORE_ADDR match, CORE_ADDR *valp)
111 {
112   CORE_ADDR type, val;
113   gdb_byte *data;
114   LONGEST n = target_read_alloc (ops, TARGET_OBJECT_AUXV, NULL, &data);
115   gdb_byte *ptr = data;
116   int ents = 0;
117
118   if (n <= 0)
119     return n;
120
121   while (1)
122     switch (target_auxv_parse (ops, &ptr, data + n, &type, &val))
123       {
124       case 1:                   /* Here's an entry, check it.  */
125         if (type == match)
126           {
127             xfree (data);
128             *valp = val;
129             return 1;
130           }
131         break;
132       case 0:                   /* End of the vector.  */
133         xfree (data);
134         return 0;
135       default:                  /* Bogosity.  */
136         xfree (data);
137         return -1;
138       }
139
140   /*NOTREACHED*/
141 }
142
143
144 /* Print the contents of the target's AUXV on the specified file. */
145 int
146 fprint_target_auxv (struct ui_file *file, struct target_ops *ops)
147 {
148   CORE_ADDR type, val;
149   gdb_byte *data;
150   LONGEST len = target_read_alloc (ops, TARGET_OBJECT_AUXV, NULL,
151                                    &data);
152   gdb_byte *ptr = data;
153   int ents = 0;
154
155   if (len <= 0)
156     return len;
157
158   while (target_auxv_parse (ops, &ptr, data + len, &type, &val) > 0)
159     {
160       extern int addressprint;
161       const char *name = "???";
162       const char *description = "";
163       enum { dec, hex, str } flavor = hex;
164
165       switch (type)
166         {
167 #define TAG(tag, text, kind) \
168         case tag: name = #tag; description = text; flavor = kind; break
169           TAG (AT_NULL, _("End of vector"), hex);
170           TAG (AT_IGNORE, _("Entry should be ignored"), hex);
171           TAG (AT_EXECFD, _("File descriptor of program"), dec);
172           TAG (AT_PHDR, _("Program headers for program"), hex);
173           TAG (AT_PHENT, _("Size of program header entry"), dec);
174           TAG (AT_PHNUM, _("Number of program headers"), dec);
175           TAG (AT_PAGESZ, _("System page size"), dec);
176           TAG (AT_BASE, _("Base address of interpreter"), hex);
177           TAG (AT_FLAGS, _("Flags"), hex);
178           TAG (AT_ENTRY, _("Entry point of program"), hex);
179           TAG (AT_NOTELF, _("Program is not ELF"), dec);
180           TAG (AT_UID, _("Real user ID"), dec);
181           TAG (AT_EUID, _("Effective user ID"), dec);
182           TAG (AT_GID, _("Real group ID"), dec);
183           TAG (AT_EGID, _("Effective group ID"), dec);
184           TAG (AT_CLKTCK, _("Frequency of times()"), dec);
185           TAG (AT_PLATFORM, _("String identifying platform"), str);
186           TAG (AT_HWCAP, _("Machine-dependent CPU capability hints"), hex);
187           TAG (AT_FPUCW, _("Used FPU control word"), dec);
188           TAG (AT_DCACHEBSIZE, _("Data cache block size"), dec);
189           TAG (AT_ICACHEBSIZE, _("Instruction cache block size"), dec);
190           TAG (AT_UCACHEBSIZE, _("Unified cache block size"), dec);
191           TAG (AT_IGNOREPPC, _("Entry should be ignored"), dec);
192           TAG (AT_SYSINFO, _("Special system info/entry points"), hex);
193           TAG (AT_SYSINFO_EHDR, _("System-supplied DSO's ELF header"), hex);
194           TAG (AT_SECURE, _("Boolean, was exec setuid-like?"), dec);
195           TAG (AT_SUN_UID, _("Effective user ID"), dec);
196           TAG (AT_SUN_RUID, _("Real user ID"), dec);
197           TAG (AT_SUN_GID, _("Effective group ID"), dec);
198           TAG (AT_SUN_RGID, _("Real group ID"), dec);
199           TAG (AT_SUN_LDELF, _("Dynamic linker's ELF header"), hex);
200           TAG (AT_SUN_LDSHDR, _("Dynamic linker's section headers"), hex);
201           TAG (AT_SUN_LDNAME, _("String giving name of dynamic linker"), str);
202           TAG (AT_SUN_LPAGESZ, _("Large pagesize"), dec);
203           TAG (AT_SUN_PLATFORM, _("Platform name string"), str);
204           TAG (AT_SUN_HWCAP, _("Machine-dependent CPU capability hints"), hex);
205           TAG (AT_SUN_IFLUSH, _("Should flush icache?"), dec);
206           TAG (AT_SUN_CPU, _("CPU name string"), str);
207           TAG (AT_SUN_EMUL_ENTRY, _("COFF entry point address"), hex);
208           TAG (AT_SUN_EMUL_EXECFD, _("COFF executable file descriptor"), dec);
209           TAG (AT_SUN_EXECNAME,
210                _("Canonicalized file name given to execve"), str);
211           TAG (AT_SUN_MMU, _("String for name of MMU module"), str);
212           TAG (AT_SUN_LDDATA, _("Dynamic linker's data segment address"), hex);
213         }
214
215       fprintf_filtered (file, "%-4s %-20s %-30s ",
216                         paddr_d (type), name, description);
217       switch (flavor)
218         {
219         case dec:
220           fprintf_filtered (file, "%s\n", paddr_d (val));
221           break;
222         case hex:
223           fprintf_filtered (file, "0x%s\n", paddr_nz (val));
224           break;
225         case str:
226           if (addressprint)
227             fprintf_filtered (file, "0x%s", paddr_nz (val));
228           val_print_string (val, -1, 1, file);
229           fprintf_filtered (file, "\n");
230           break;
231         }
232       ++ents;
233     }
234
235   xfree (data);
236
237   return ents;
238 }
239
240 static void
241 info_auxv_command (char *cmd, int from_tty)
242 {
243   if (! target_has_stack)
244     error (_("The program has no auxiliary information now."));
245   else
246     {
247       int ents = fprint_target_auxv (gdb_stdout, &current_target);
248       if (ents < 0)
249         error (_("No auxiliary vector found, or failed reading it."));
250       else if (ents == 0)
251         error (_("Auxiliary vector is empty."));
252     }
253 }
254
255
256 extern initialize_file_ftype _initialize_auxv; /* -Wmissing-prototypes; */
257
258 void
259 _initialize_auxv (void)
260 {
261   add_info ("auxv", info_auxv_command,
262             _("Display the inferior's auxiliary vector.\n\
263 This is information provided by the operating system at program startup."));
264 }