04afbb17aaabd76c864c4c36428489ba36c8747e
[external/binutils.git] / gdb / linux-tdep.c
1 /* Target-dependent code for GNU/Linux, architecture independent.
2
3    Copyright (C) 2009-2013 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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "gdbtypes.h"
22 #include "linux-tdep.h"
23 #include "auxv.h"
24 #include "target.h"
25 #include "gdbthread.h"
26 #include "gdbcore.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h"            /* for elfcore_write_* */
31 #include "inferior.h"
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdb_obstack.h"
35 #include "cli/cli-utils.h"
36
37 #include <ctype.h>
38
39 static struct gdbarch_data *linux_gdbarch_data_handle;
40
41 struct linux_gdbarch_data
42   {
43     struct type *siginfo_type;
44   };
45
46 static void *
47 init_linux_gdbarch_data (struct gdbarch *gdbarch)
48 {
49   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
50 }
51
52 static struct linux_gdbarch_data *
53 get_linux_gdbarch_data (struct gdbarch *gdbarch)
54 {
55   return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
56 }
57
58 /* This function is suitable for architectures that don't
59    extend/override the standard siginfo structure.  */
60
61 struct type *
62 linux_get_siginfo_type (struct gdbarch *gdbarch)
63 {
64   struct linux_gdbarch_data *linux_gdbarch_data;
65   struct type *int_type, *uint_type, *long_type, *void_ptr_type;
66   struct type *uid_type, *pid_type;
67   struct type *sigval_type, *clock_type;
68   struct type *siginfo_type, *sifields_type;
69   struct type *type;
70
71   linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
72   if (linux_gdbarch_data->siginfo_type != NULL)
73     return linux_gdbarch_data->siginfo_type;
74
75   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
76                                 0, "int");
77   uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
78                                  1, "unsigned int");
79   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
80                                  0, "long");
81   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
82
83   /* sival_t */
84   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
85   TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
86   append_composite_type_field (sigval_type, "sival_int", int_type);
87   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
88
89   /* __pid_t */
90   pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
91                         TYPE_LENGTH (int_type), "__pid_t");
92   TYPE_TARGET_TYPE (pid_type) = int_type;
93   TYPE_TARGET_STUB (pid_type) = 1;
94
95   /* __uid_t */
96   uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
97                         TYPE_LENGTH (uint_type), "__uid_t");
98   TYPE_TARGET_TYPE (uid_type) = uint_type;
99   TYPE_TARGET_STUB (uid_type) = 1;
100
101   /* __clock_t */
102   clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
103                           TYPE_LENGTH (long_type), "__clock_t");
104   TYPE_TARGET_TYPE (clock_type) = long_type;
105   TYPE_TARGET_STUB (clock_type) = 1;
106
107   /* _sifields */
108   sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
109
110   {
111     const int si_max_size = 128;
112     int si_pad_size;
113     int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
114
115     /* _pad */
116     if (gdbarch_ptr_bit (gdbarch) == 64)
117       si_pad_size = (si_max_size / size_of_int) - 4;
118     else
119       si_pad_size = (si_max_size / size_of_int) - 3;
120     append_composite_type_field (sifields_type, "_pad",
121                                  init_vector_type (int_type, si_pad_size));
122   }
123
124   /* _kill */
125   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
126   append_composite_type_field (type, "si_pid", pid_type);
127   append_composite_type_field (type, "si_uid", uid_type);
128   append_composite_type_field (sifields_type, "_kill", type);
129
130   /* _timer */
131   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
132   append_composite_type_field (type, "si_tid", int_type);
133   append_composite_type_field (type, "si_overrun", int_type);
134   append_composite_type_field (type, "si_sigval", sigval_type);
135   append_composite_type_field (sifields_type, "_timer", type);
136
137   /* _rt */
138   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
139   append_composite_type_field (type, "si_pid", pid_type);
140   append_composite_type_field (type, "si_uid", uid_type);
141   append_composite_type_field (type, "si_sigval", sigval_type);
142   append_composite_type_field (sifields_type, "_rt", type);
143
144   /* _sigchld */
145   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
146   append_composite_type_field (type, "si_pid", pid_type);
147   append_composite_type_field (type, "si_uid", uid_type);
148   append_composite_type_field (type, "si_status", int_type);
149   append_composite_type_field (type, "si_utime", clock_type);
150   append_composite_type_field (type, "si_stime", clock_type);
151   append_composite_type_field (sifields_type, "_sigchld", type);
152
153   /* _sigfault */
154   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
155   append_composite_type_field (type, "si_addr", void_ptr_type);
156   append_composite_type_field (sifields_type, "_sigfault", type);
157
158   /* _sigpoll */
159   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
160   append_composite_type_field (type, "si_band", long_type);
161   append_composite_type_field (type, "si_fd", int_type);
162   append_composite_type_field (sifields_type, "_sigpoll", type);
163
164   /* struct siginfo */
165   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
166   TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
167   append_composite_type_field (siginfo_type, "si_signo", int_type);
168   append_composite_type_field (siginfo_type, "si_errno", int_type);
169   append_composite_type_field (siginfo_type, "si_code", int_type);
170   append_composite_type_field_aligned (siginfo_type,
171                                        "_sifields", sifields_type,
172                                        TYPE_LENGTH (long_type));
173
174   linux_gdbarch_data->siginfo_type = siginfo_type;
175
176   return siginfo_type;
177 }
178
179 static int
180 linux_has_shared_address_space (struct gdbarch *gdbarch)
181 {
182   /* Determine whether we are running on uClinux or normal Linux
183      kernel.  */
184   CORE_ADDR dummy;
185   int target_is_uclinux;
186
187   target_is_uclinux
188     = (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
189        && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
190
191   return target_is_uclinux;
192 }
193
194 /* This is how we want PTIDs from core files to be printed.  */
195
196 static char *
197 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
198 {
199   static char buf[80];
200
201   if (ptid_get_lwp (ptid) != 0)
202     {
203       snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
204       return buf;
205     }
206
207   return normal_pid_to_str (ptid);
208 }
209
210 /* Service function for corefiles and info proc.  */
211
212 static void
213 read_mapping (const char *line,
214               ULONGEST *addr, ULONGEST *endaddr,
215               const char **permissions, size_t *permissions_len,
216               ULONGEST *offset,
217               const char **device, size_t *device_len,
218               ULONGEST *inode,
219               const char **filename)
220 {
221   const char *p = line;
222
223   *addr = strtoulst (p, &p, 16);
224   if (*p == '-')
225     p++;
226   *endaddr = strtoulst (p, &p, 16);
227
228   while (*p && isspace (*p))
229     p++;
230   *permissions = p;
231   while (*p && !isspace (*p))
232     p++;
233   *permissions_len = p - *permissions;
234
235   *offset = strtoulst (p, &p, 16);
236
237   while (*p && isspace (*p))
238     p++;
239   *device = p;
240   while (*p && !isspace (*p))
241     p++;
242   *device_len = p - *device;
243
244   *inode = strtoulst (p, &p, 10);
245
246   while (*p && isspace (*p))
247     p++;
248   *filename = p;
249 }
250
251 /* Implement the "info proc" command.  */
252
253 static void
254 linux_info_proc (struct gdbarch *gdbarch, char *args,
255                  enum info_proc_what what)
256 {
257   /* A long is used for pid instead of an int to avoid a loss of precision
258      compiler warning from the output of strtoul.  */
259   long pid;
260   int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
261   int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
262   int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
263   int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
264   int status_f = (what == IP_STATUS || what == IP_ALL);
265   int stat_f = (what == IP_STAT || what == IP_ALL);
266   char filename[100];
267   gdb_byte *data;
268   int target_errno;
269
270   if (args && isdigit (args[0]))
271     pid = strtoul (args, &args, 10);
272   else
273     {
274       if (!target_has_execution)
275         error (_("No current process: you must name one."));
276       if (current_inferior ()->fake_pid_p)
277         error (_("Can't determine the current process's PID: you must name one."));
278
279       pid = current_inferior ()->pid;
280     }
281
282   args = skip_spaces (args);
283   if (args && args[0])
284     error (_("Too many parameters: %s"), args);
285
286   printf_filtered (_("process %ld\n"), pid);
287   if (cmdline_f)
288     {
289       xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
290       data = target_fileio_read_stralloc (filename);
291       if (data)
292         {
293           struct cleanup *cleanup = make_cleanup (xfree, data);
294           printf_filtered ("cmdline = '%s'\n", data);
295           do_cleanups (cleanup);
296         }
297       else
298         warning (_("unable to open /proc file '%s'"), filename);
299     }
300   if (cwd_f)
301     {
302       xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
303       data = target_fileio_readlink (filename, &target_errno);
304       if (data)
305         {
306           struct cleanup *cleanup = make_cleanup (xfree, data);
307           printf_filtered ("cwd = '%s'\n", data);
308           do_cleanups (cleanup);
309         }
310       else
311         warning (_("unable to read link '%s'"), filename);
312     }
313   if (exe_f)
314     {
315       xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
316       data = target_fileio_readlink (filename, &target_errno);
317       if (data)
318         {
319           struct cleanup *cleanup = make_cleanup (xfree, data);
320           printf_filtered ("exe = '%s'\n", data);
321           do_cleanups (cleanup);
322         }
323       else
324         warning (_("unable to read link '%s'"), filename);
325     }
326   if (mappings_f)
327     {
328       xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
329       data = target_fileio_read_stralloc (filename);
330       if (data)
331         {
332           struct cleanup *cleanup = make_cleanup (xfree, data);
333           char *line;
334
335           printf_filtered (_("Mapped address spaces:\n\n"));
336           if (gdbarch_addr_bit (gdbarch) == 32)
337             {
338               printf_filtered ("\t%10s %10s %10s %10s %s\n",
339                            "Start Addr",
340                            "  End Addr",
341                            "      Size", "    Offset", "objfile");
342             }
343           else
344             {
345               printf_filtered ("  %18s %18s %10s %10s %s\n",
346                            "Start Addr",
347                            "  End Addr",
348                            "      Size", "    Offset", "objfile");
349             }
350
351           for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
352             {
353               ULONGEST addr, endaddr, offset, inode;
354               const char *permissions, *device, *filename;
355               size_t permissions_len, device_len;
356
357               read_mapping (line, &addr, &endaddr,
358                             &permissions, &permissions_len,
359                             &offset, &device, &device_len,
360                             &inode, &filename);
361
362               if (gdbarch_addr_bit (gdbarch) == 32)
363                 {
364                   printf_filtered ("\t%10s %10s %10s %10s %s\n",
365                                    paddress (gdbarch, addr),
366                                    paddress (gdbarch, endaddr),
367                                    hex_string (endaddr - addr),
368                                    hex_string (offset),
369                                    *filename? filename : "");
370                 }
371               else
372                 {
373                   printf_filtered ("  %18s %18s %10s %10s %s\n",
374                                    paddress (gdbarch, addr),
375                                    paddress (gdbarch, endaddr),
376                                    hex_string (endaddr - addr),
377                                    hex_string (offset),
378                                    *filename? filename : "");
379                 }
380             }
381
382           do_cleanups (cleanup);
383         }
384       else
385         warning (_("unable to open /proc file '%s'"), filename);
386     }
387   if (status_f)
388     {
389       xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
390       data = target_fileio_read_stralloc (filename);
391       if (data)
392         {
393           struct cleanup *cleanup = make_cleanup (xfree, data);
394           puts_filtered (data);
395           do_cleanups (cleanup);
396         }
397       else
398         warning (_("unable to open /proc file '%s'"), filename);
399     }
400   if (stat_f)
401     {
402       xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
403       data = target_fileio_read_stralloc (filename);
404       if (data)
405         {
406           struct cleanup *cleanup = make_cleanup (xfree, data);
407           const char *p = data;
408
409           printf_filtered (_("Process: %s\n"),
410                            pulongest (strtoulst (p, &p, 10)));
411
412           while (*p && isspace (*p))
413             p++;
414           if (*p == '(')
415             {
416               const char *ep = strchr (p, ')');
417               if (ep != NULL)
418                 {
419                   printf_filtered ("Exec file: %.*s\n",
420                                    (int) (ep - p - 1), p + 1);
421                   p = ep + 1;
422                 }
423             }
424
425           while (*p && isspace (*p))
426             p++;
427           if (*p)
428             printf_filtered (_("State: %c\n"), *p++);
429
430           if (*p)
431             printf_filtered (_("Parent process: %s\n"),
432                              pulongest (strtoulst (p, &p, 10)));
433           if (*p)
434             printf_filtered (_("Process group: %s\n"),
435                              pulongest (strtoulst (p, &p, 10)));
436           if (*p)
437             printf_filtered (_("Session id: %s\n"),
438                              pulongest (strtoulst (p, &p, 10)));
439           if (*p)
440             printf_filtered (_("TTY: %s\n"),
441                              pulongest (strtoulst (p, &p, 10)));
442           if (*p)
443             printf_filtered (_("TTY owner process group: %s\n"),
444                              pulongest (strtoulst (p, &p, 10)));
445
446           if (*p)
447             printf_filtered (_("Flags: %s\n"),
448                              hex_string (strtoulst (p, &p, 10)));
449           if (*p)
450             printf_filtered (_("Minor faults (no memory page): %s\n"),
451                              pulongest (strtoulst (p, &p, 10)));
452           if (*p)
453             printf_filtered (_("Minor faults, children: %s\n"),
454                              pulongest (strtoulst (p, &p, 10)));
455           if (*p)
456             printf_filtered (_("Major faults (memory page faults): %s\n"),
457                              pulongest (strtoulst (p, &p, 10)));
458           if (*p)
459             printf_filtered (_("Major faults, children: %s\n"),
460                              pulongest (strtoulst (p, &p, 10)));
461           if (*p)
462             printf_filtered (_("utime: %s\n"),
463                              pulongest (strtoulst (p, &p, 10)));
464           if (*p)
465             printf_filtered (_("stime: %s\n"),
466                              pulongest (strtoulst (p, &p, 10)));
467           if (*p)
468             printf_filtered (_("utime, children: %s\n"),
469                              pulongest (strtoulst (p, &p, 10)));
470           if (*p)
471             printf_filtered (_("stime, children: %s\n"),
472                              pulongest (strtoulst (p, &p, 10)));
473           if (*p)
474             printf_filtered (_("jiffies remaining in current "
475                                "time slice: %s\n"),
476                              pulongest (strtoulst (p, &p, 10)));
477           if (*p)
478             printf_filtered (_("'nice' value: %s\n"),
479                              pulongest (strtoulst (p, &p, 10)));
480           if (*p)
481             printf_filtered (_("jiffies until next timeout: %s\n"),
482                              pulongest (strtoulst (p, &p, 10)));
483           if (*p)
484             printf_filtered (_("jiffies until next SIGALRM: %s\n"),
485                              pulongest (strtoulst (p, &p, 10)));
486           if (*p)
487             printf_filtered (_("start time (jiffies since "
488                                "system boot): %s\n"),
489                              pulongest (strtoulst (p, &p, 10)));
490           if (*p)
491             printf_filtered (_("Virtual memory size: %s\n"),
492                              pulongest (strtoulst (p, &p, 10)));
493           if (*p)
494             printf_filtered (_("Resident set size: %s\n"),
495                              pulongest (strtoulst (p, &p, 10)));
496           if (*p)
497             printf_filtered (_("rlim: %s\n"),
498                              pulongest (strtoulst (p, &p, 10)));
499           if (*p)
500             printf_filtered (_("Start of text: %s\n"),
501                              hex_string (strtoulst (p, &p, 10)));
502           if (*p)
503             printf_filtered (_("End of text: %s\n"),
504                              hex_string (strtoulst (p, &p, 10)));
505           if (*p)
506             printf_filtered (_("Start of stack: %s\n"),
507                              hex_string (strtoulst (p, &p, 10)));
508 #if 0   /* Don't know how architecture-dependent the rest is...
509            Anyway the signal bitmap info is available from "status".  */
510           if (*p)
511             printf_filtered (_("Kernel stack pointer: %s\n"),
512                              hex_string (strtoulst (p, &p, 10)));
513           if (*p)
514             printf_filtered (_("Kernel instr pointer: %s\n"),
515                              hex_string (strtoulst (p, &p, 10)));
516           if (*p)
517             printf_filtered (_("Pending signals bitmap: %s\n"),
518                              hex_string (strtoulst (p, &p, 10)));
519           if (*p)
520             printf_filtered (_("Blocked signals bitmap: %s\n"),
521                              hex_string (strtoulst (p, &p, 10)));
522           if (*p)
523             printf_filtered (_("Ignored signals bitmap: %s\n"),
524                              hex_string (strtoulst (p, &p, 10)));
525           if (*p)
526             printf_filtered (_("Catched signals bitmap: %s\n"),
527                              hex_string (strtoulst (p, &p, 10)));
528           if (*p)
529             printf_filtered (_("wchan (system call): %s\n"),
530                              hex_string (strtoulst (p, &p, 10)));
531 #endif
532           do_cleanups (cleanup);
533         }
534       else
535         warning (_("unable to open /proc file '%s'"), filename);
536     }
537 }
538
539 /* Implement "info proc mappings" for a corefile.  */
540
541 static void
542 linux_core_info_proc_mappings (struct gdbarch *gdbarch, char *args)
543 {
544   asection *section;
545   ULONGEST count, page_size;
546   unsigned char *descdata, *filenames, *descend, *contents;
547   size_t note_size;
548   unsigned int addr_size_bits, addr_size;
549   struct cleanup *cleanup;
550   struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
551   /* We assume this for reading 64-bit core files.  */
552   gdb_static_assert (sizeof (ULONGEST) >= 8);
553
554   section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
555   if (section == NULL)
556     {
557       warning (_("unable to find mappings in core file"));
558       return;
559     }
560
561   addr_size_bits = gdbarch_addr_bit (core_gdbarch);
562   addr_size = addr_size_bits / 8;
563   note_size = bfd_get_section_size (section);
564
565   if (note_size < 2 * addr_size)
566     error (_("malformed core note - too short for header"));
567
568   contents = xmalloc (note_size);
569   cleanup = make_cleanup (xfree, contents);
570   if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
571     error (_("could not get core note contents"));
572
573   descdata = contents;
574   descend = descdata + note_size;
575
576   if (descdata[note_size - 1] != '\0')
577     error (_("malformed note - does not end with \\0"));
578
579   count = bfd_get (addr_size_bits, core_bfd, descdata);
580   descdata += addr_size;
581
582   page_size = bfd_get (addr_size_bits, core_bfd, descdata);
583   descdata += addr_size;
584
585   if (note_size < 2 * addr_size + count * 3 * addr_size)
586     error (_("malformed note - too short for supplied file count"));
587
588   printf_filtered (_("Mapped address spaces:\n\n"));
589   if (gdbarch_addr_bit (gdbarch) == 32)
590     {
591       printf_filtered ("\t%10s %10s %10s %10s %s\n",
592                        "Start Addr",
593                        "  End Addr",
594                        "      Size", "    Offset", "objfile");
595     }
596   else
597     {
598       printf_filtered ("  %18s %18s %10s %10s %s\n",
599                        "Start Addr",
600                        "  End Addr",
601                        "      Size", "    Offset", "objfile");
602     }
603
604   filenames = descdata + count * 3 * addr_size;
605   while (--count > 0)
606     {
607       ULONGEST start, end, file_ofs;
608
609       if (filenames == descend)
610         error (_("malformed note - filenames end too early"));
611
612       start = bfd_get (addr_size_bits, core_bfd, descdata);
613       descdata += addr_size;
614       end = bfd_get (addr_size_bits, core_bfd, descdata);
615       descdata += addr_size;
616       file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
617       descdata += addr_size;
618
619       file_ofs *= page_size;
620
621       if (gdbarch_addr_bit (gdbarch) == 32)
622         printf_filtered ("\t%10s %10s %10s %10s %s\n",
623                          paddress (gdbarch, start),
624                          paddress (gdbarch, end),
625                          hex_string (end - start),
626                          hex_string (file_ofs),
627                          filenames);
628       else
629         printf_filtered ("  %18s %18s %10s %10s %s\n",
630                          paddress (gdbarch, start),
631                          paddress (gdbarch, end),
632                          hex_string (end - start),
633                          hex_string (file_ofs),
634                          filenames);
635
636       filenames += 1 + strlen ((char *) filenames);
637     }
638
639   do_cleanups (cleanup);
640 }
641
642 /* Implement "info proc" for a corefile.  */
643
644 static void
645 linux_core_info_proc (struct gdbarch *gdbarch, char *args,
646                       enum info_proc_what what)
647 {
648   int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
649   int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
650
651   if (exe_f)
652     {
653       const char *exe;
654
655       exe = bfd_core_file_failing_command (core_bfd);
656       if (exe != NULL)
657         printf_filtered ("exe = '%s'\n", exe);
658       else
659         warning (_("unable to find command name in core file"));
660     }
661
662   if (mappings_f)
663     linux_core_info_proc_mappings (gdbarch, args);
664
665   if (!exe_f && !mappings_f)
666     error (_("unable to handle request"));
667 }
668
669 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
670                                             ULONGEST offset, ULONGEST inode,
671                                             int read, int write,
672                                             int exec, int modified,
673                                             const char *filename,
674                                             void *data);
675
676 /* List memory regions in the inferior for a corefile.  */
677
678 static int
679 linux_find_memory_regions_full (struct gdbarch *gdbarch,
680                                 linux_find_memory_region_ftype *func,
681                                 void *obfd)
682 {
683   char filename[100];
684   gdb_byte *data;
685
686   /* We need to know the real target PID to access /proc.  */
687   if (current_inferior ()->fake_pid_p)
688     return 1;
689
690   xsnprintf (filename, sizeof filename,
691              "/proc/%d/smaps", current_inferior ()->pid);
692   data = target_fileio_read_stralloc (filename);
693   if (data == NULL)
694     {
695       /* Older Linux kernels did not support /proc/PID/smaps.  */
696       xsnprintf (filename, sizeof filename,
697                  "/proc/%d/maps", current_inferior ()->pid);
698       data = target_fileio_read_stralloc (filename);
699     }
700   if (data)
701     {
702       struct cleanup *cleanup = make_cleanup (xfree, data);
703       char *line;
704
705       line = strtok (data, "\n");
706       while (line)
707         {
708           ULONGEST addr, endaddr, offset, inode;
709           const char *permissions, *device, *filename;
710           size_t permissions_len, device_len;
711           int read, write, exec;
712           int modified = 0, has_anonymous = 0;
713
714           read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
715                         &offset, &device, &device_len, &inode, &filename);
716
717           /* Decode permissions.  */
718           read = (memchr (permissions, 'r', permissions_len) != 0);
719           write = (memchr (permissions, 'w', permissions_len) != 0);
720           exec = (memchr (permissions, 'x', permissions_len) != 0);
721
722           /* Try to detect if region was modified by parsing smaps counters.  */
723           for (line = strtok (NULL, "\n");
724                line && line[0] >= 'A' && line[0] <= 'Z';
725                line = strtok (NULL, "\n"))
726             {
727               char keyword[64 + 1];
728               unsigned long number;
729
730               if (sscanf (line, "%64s%lu kB\n", keyword, &number) != 2)
731                 {
732                   warning (_("Error parsing {s,}maps file '%s'"), filename);
733                   break;
734                 }
735               if (strcmp (keyword, "Anonymous:") == 0)
736                 has_anonymous = 1;
737               if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
738                                   || strcmp (keyword, "Private_Dirty:") == 0
739                                   || strcmp (keyword, "Swap:") == 0
740                                   || strcmp (keyword, "Anonymous:") == 0))
741                 modified = 1;
742             }
743
744           /* Older Linux kernels did not support the "Anonymous:" counter.
745              If it is missing, we can't be sure - dump all the pages.  */
746           if (!has_anonymous)
747             modified = 1;
748
749           /* Invoke the callback function to create the corefile segment.  */
750           func (addr, endaddr - addr, offset, inode,
751                 read, write, exec, modified, filename, obfd);
752         }
753
754       do_cleanups (cleanup);
755       return 0;
756     }
757
758   return 1;
759 }
760
761 /* A structure for passing information through
762    linux_find_memory_regions_full.  */
763
764 struct linux_find_memory_regions_data
765 {
766   /* The original callback.  */
767
768   find_memory_region_ftype func;
769
770   /* The original datum.  */
771
772   void *obfd;
773 };
774
775 /* A callback for linux_find_memory_regions that converts between the
776    "full"-style callback and find_memory_region_ftype.  */
777
778 static int
779 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
780                                  ULONGEST offset, ULONGEST inode,
781                                  int read, int write, int exec, int modified,
782                                  const char *filename, void *arg)
783 {
784   struct linux_find_memory_regions_data *data = arg;
785
786   return data->func (vaddr, size, read, write, exec, modified, data->obfd);
787 }
788
789 /* A variant of linux_find_memory_regions_full that is suitable as the
790    gdbarch find_memory_regions method.  */
791
792 static int
793 linux_find_memory_regions (struct gdbarch *gdbarch,
794                            find_memory_region_ftype func, void *obfd)
795 {
796   struct linux_find_memory_regions_data data;
797
798   data.func = func;
799   data.obfd = obfd;
800
801   return linux_find_memory_regions_full (gdbarch,
802                                          linux_find_memory_regions_thunk,
803                                          &data);
804 }
805
806 /* Determine which signal stopped execution.  */
807
808 static int
809 find_signalled_thread (struct thread_info *info, void *data)
810 {
811   if (info->suspend.stop_signal != GDB_SIGNAL_0
812       && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
813     return 1;
814
815   return 0;
816 }
817
818 static enum gdb_signal
819 find_stop_signal (void)
820 {
821   struct thread_info *info =
822     iterate_over_threads (find_signalled_thread, NULL);
823
824   if (info)
825     return info->suspend.stop_signal;
826   else
827     return GDB_SIGNAL_0;
828 }
829
830 /* Generate corefile notes for SPU contexts.  */
831
832 static char *
833 linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
834 {
835   static const char *spu_files[] =
836     {
837       "object-id",
838       "mem",
839       "regs",
840       "fpcr",
841       "lslr",
842       "decr",
843       "decr_status",
844       "signal1",
845       "signal1_type",
846       "signal2",
847       "signal2_type",
848       "event_mask",
849       "event_status",
850       "mbox_info",
851       "ibox_info",
852       "wbox_info",
853       "dma_info",
854       "proxydma_info",
855    };
856
857   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
858   gdb_byte *spu_ids;
859   LONGEST i, j, size;
860
861   /* Determine list of SPU ids.  */
862   size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
863                             NULL, &spu_ids);
864
865   /* Generate corefile notes for each SPU file.  */
866   for (i = 0; i < size; i += 4)
867     {
868       int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
869
870       for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
871         {
872           char annex[32], note_name[32];
873           gdb_byte *spu_data;
874           LONGEST spu_len;
875
876           xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
877           spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
878                                        annex, &spu_data);
879           if (spu_len > 0)
880             {
881               xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
882               note_data = elfcore_write_note (obfd, note_data, note_size,
883                                               note_name, NT_SPU,
884                                               spu_data, spu_len);
885               xfree (spu_data);
886
887               if (!note_data)
888                 {
889                   xfree (spu_ids);
890                   return NULL;
891                 }
892             }
893         }
894     }
895
896   if (size > 0)
897     xfree (spu_ids);
898
899   return note_data;
900 }
901
902 /* This is used to pass information from
903    linux_make_mappings_corefile_notes through
904    linux_find_memory_regions_full.  */
905
906 struct linux_make_mappings_data
907 {
908   /* Number of files mapped.  */
909   ULONGEST file_count;
910
911   /* The obstack for the main part of the data.  */
912   struct obstack *data_obstack;
913
914   /* The filename obstack.  */
915   struct obstack *filename_obstack;
916
917   /* The architecture's "long" type.  */
918   struct type *long_type;
919 };
920
921 static linux_find_memory_region_ftype linux_make_mappings_callback;
922
923 /* A callback for linux_find_memory_regions_full that updates the
924    mappings data for linux_make_mappings_corefile_notes.  */
925
926 static int
927 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
928                               ULONGEST offset, ULONGEST inode,
929                               int read, int write, int exec, int modified,
930                               const char *filename, void *data)
931 {
932   struct linux_make_mappings_data *map_data = data;
933   gdb_byte buf[sizeof (ULONGEST)];
934
935   if (*filename == '\0' || inode == 0)
936     return 0;
937
938   ++map_data->file_count;
939
940   pack_long (buf, map_data->long_type, vaddr);
941   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
942   pack_long (buf, map_data->long_type, vaddr + size);
943   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
944   pack_long (buf, map_data->long_type, offset);
945   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
946
947   obstack_grow_str0 (map_data->filename_obstack, filename);
948
949   return 0;
950 }
951
952 /* Write the file mapping data to the core file, if possible.  OBFD is
953    the output BFD.  NOTE_DATA is the current note data, and NOTE_SIZE
954    is a pointer to the note size.  Returns the new NOTE_DATA and
955    updates NOTE_SIZE.  */
956
957 static char *
958 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
959                                     char *note_data, int *note_size)
960 {
961   struct cleanup *cleanup;
962   struct obstack data_obstack, filename_obstack;
963   struct linux_make_mappings_data mapping_data;
964   struct type *long_type
965     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
966   gdb_byte buf[sizeof (ULONGEST)];
967
968   obstack_init (&data_obstack);
969   cleanup = make_cleanup_obstack_free (&data_obstack);
970   obstack_init (&filename_obstack);
971   make_cleanup_obstack_free (&filename_obstack);
972
973   mapping_data.file_count = 0;
974   mapping_data.data_obstack = &data_obstack;
975   mapping_data.filename_obstack = &filename_obstack;
976   mapping_data.long_type = long_type;
977
978   /* Reserve space for the count.  */
979   obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
980   /* We always write the page size as 1 since we have no good way to
981      determine the correct value.  */
982   pack_long (buf, long_type, 1);
983   obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
984
985   linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
986                                   &mapping_data);
987
988   if (mapping_data.file_count != 0)
989     {
990       /* Write the count to the obstack.  */
991       pack_long (obstack_base (&data_obstack), long_type,
992                  mapping_data.file_count);
993
994       /* Copy the filenames to the data obstack.  */
995       obstack_grow (&data_obstack, obstack_base (&filename_obstack),
996                     obstack_object_size (&filename_obstack));
997
998       note_data = elfcore_write_note (obfd, note_data, note_size,
999                                       "CORE", NT_FILE,
1000                                       obstack_base (&data_obstack),
1001                                       obstack_object_size (&data_obstack));
1002     }
1003
1004   do_cleanups (cleanup);
1005   return note_data;
1006 }
1007
1008 /* Records the thread's register state for the corefile note
1009    section.  */
1010
1011 static char *
1012 linux_collect_thread_registers (const struct regcache *regcache,
1013                                 ptid_t ptid, bfd *obfd,
1014                                 char *note_data, int *note_size,
1015                                 enum gdb_signal stop_signal)
1016 {
1017   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1018   struct core_regset_section *sect_list;
1019   unsigned long lwp;
1020
1021   sect_list = gdbarch_core_regset_sections (gdbarch);
1022   gdb_assert (sect_list);
1023
1024   /* For remote targets the LWP may not be available, so use the TID.  */
1025   lwp = ptid_get_lwp (ptid);
1026   if (!lwp)
1027     lwp = ptid_get_tid (ptid);
1028
1029   while (sect_list->sect_name != NULL)
1030     {
1031       const struct regset *regset;
1032       char *buf;
1033
1034       regset = gdbarch_regset_from_core_section (gdbarch,
1035                                                  sect_list->sect_name,
1036                                                  sect_list->size);
1037       gdb_assert (regset && regset->collect_regset);
1038
1039       buf = xmalloc (sect_list->size);
1040       regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
1041
1042       /* PRSTATUS still needs to be treated specially.  */
1043       if (strcmp (sect_list->sect_name, ".reg") == 0)
1044         note_data = (char *) elfcore_write_prstatus
1045                                (obfd, note_data, note_size, lwp,
1046                                 gdb_signal_to_host (stop_signal), buf);
1047       else
1048         note_data = (char *) elfcore_write_register_note
1049                                (obfd, note_data, note_size,
1050                                 sect_list->sect_name, buf, sect_list->size);
1051       xfree (buf);
1052       sect_list++;
1053
1054       if (!note_data)
1055         return NULL;
1056     }
1057
1058   return note_data;
1059 }
1060
1061 /* Fetch the siginfo data for the current thread, if it exists.  If
1062    there is no data, or we could not read it, return NULL.  Otherwise,
1063    return a newly malloc'd buffer holding the data and fill in *SIZE
1064    with the size of the data.  The caller is responsible for freeing
1065    the data.  */
1066
1067 static gdb_byte *
1068 linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
1069 {
1070   struct type *siginfo_type;
1071   gdb_byte *buf;
1072   LONGEST bytes_read;
1073   struct cleanup *cleanups;
1074
1075   if (!gdbarch_get_siginfo_type_p (gdbarch))
1076     return NULL;
1077   
1078   siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1079
1080   buf = xmalloc (TYPE_LENGTH (siginfo_type));
1081   cleanups = make_cleanup (xfree, buf);
1082
1083   bytes_read = target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
1084                             buf, 0, TYPE_LENGTH (siginfo_type));
1085   if (bytes_read == TYPE_LENGTH (siginfo_type))
1086     {
1087       discard_cleanups (cleanups);
1088       *size = bytes_read;
1089     }
1090   else
1091     {
1092       do_cleanups (cleanups);
1093       buf = NULL;
1094     }
1095
1096   return buf;
1097 }
1098
1099 struct linux_corefile_thread_data
1100 {
1101   struct gdbarch *gdbarch;
1102   int pid;
1103   bfd *obfd;
1104   char *note_data;
1105   int *note_size;
1106   int num_notes;
1107   enum gdb_signal stop_signal;
1108   linux_collect_thread_registers_ftype collect;
1109 };
1110
1111 /* Called by gdbthread.c once per thread.  Records the thread's
1112    register state for the corefile note section.  */
1113
1114 static int
1115 linux_corefile_thread_callback (struct thread_info *info, void *data)
1116 {
1117   struct linux_corefile_thread_data *args = data;
1118
1119   if (ptid_get_pid (info->ptid) == args->pid)
1120     {
1121       struct cleanup *old_chain;
1122       struct regcache *regcache;
1123       gdb_byte *siginfo_data;
1124       LONGEST siginfo_size;
1125
1126       regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
1127
1128       old_chain = save_inferior_ptid ();
1129       inferior_ptid = info->ptid;
1130       target_fetch_registers (regcache, -1);
1131       siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
1132       do_cleanups (old_chain);
1133
1134       old_chain = make_cleanup (xfree, siginfo_data);
1135
1136       args->note_data = args->collect (regcache, info->ptid, args->obfd,
1137                                        args->note_data, args->note_size,
1138                                        args->stop_signal);
1139       args->num_notes++;
1140
1141       if (siginfo_data != NULL)
1142         {
1143           args->note_data = elfcore_write_note (args->obfd,
1144                                                 args->note_data,
1145                                                 args->note_size,
1146                                                 "CORE", NT_SIGINFO,
1147                                                 siginfo_data, siginfo_size);
1148           args->num_notes++;
1149         }
1150
1151       do_cleanups (old_chain);
1152     }
1153
1154   return !args->note_data;
1155 }
1156
1157 /* Fill the PRPSINFO structure with information about the process being
1158    debugged.  Returns 1 in case of success, 0 for failures.  Please note that
1159    even if the structure cannot be entirely filled (e.g., GDB was unable to
1160    gather information about the process UID/GID), this function will still
1161    return 1 since some information was already recorded.  It will only return
1162    0 iff nothing can be gathered.  */
1163
1164 static int
1165 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
1166 {
1167   /* The filename which we will use to obtain some info about the process.
1168      We will basically use this to store the `/proc/PID/FILENAME' file.  */
1169   char filename[100];
1170   /* The full name of the program which generated the corefile.  */
1171   char *fname;
1172   /* The basename of the executable.  */
1173   const char *basename;
1174   /* The arguments of the program.  */
1175   char *psargs;
1176   char *infargs;
1177   /* The contents of `/proc/PID/stat' and `/proc/PID/status' files.  */
1178   char *proc_stat, *proc_status;
1179   /* Temporary buffer.  */
1180   char *tmpstr;
1181   /* The valid states of a process, according to the Linux kernel.  */
1182   const char valid_states[] = "RSDTZW";
1183   /* The program state.  */
1184   const char *prog_state;
1185   /* The state of the process.  */
1186   char pr_sname;
1187   /* The PID of the program which generated the corefile.  */
1188   pid_t pid;
1189   /* Process flags.  */
1190   unsigned int pr_flag;
1191   /* Process nice value.  */
1192   long pr_nice;
1193   /* The number of fields read by `sscanf'.  */
1194   int n_fields = 0;
1195   /* Cleanups.  */
1196   struct cleanup *c;
1197   int i;
1198
1199   gdb_assert (p != NULL);
1200
1201   /* Obtaining PID and filename.  */
1202   pid = ptid_get_pid (inferior_ptid);
1203   xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
1204   fname = target_fileio_read_stralloc (filename);
1205
1206   if (fname == NULL || *fname == '\0')
1207     {
1208       /* No program name was read, so we won't be able to retrieve more
1209          information about the process.  */
1210       xfree (fname);
1211       return 0;
1212     }
1213
1214   c = make_cleanup (xfree, fname);
1215   memset (p, 0, sizeof (*p));
1216
1217   /* Defining the PID.  */
1218   p->pr_pid = pid;
1219
1220   /* Copying the program name.  Only the basename matters.  */
1221   basename = lbasename (fname);
1222   strncpy (p->pr_fname, basename, sizeof (p->pr_fname));
1223   p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
1224
1225   infargs = get_inferior_args ();
1226
1227   psargs = xstrdup (fname);
1228   if (infargs != NULL)
1229     psargs = reconcat (psargs, psargs, " ", infargs, NULL);
1230
1231   make_cleanup (xfree, psargs);
1232
1233   strncpy (p->pr_psargs, psargs, sizeof (p->pr_psargs));
1234   p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
1235
1236   xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
1237   proc_stat = target_fileio_read_stralloc (filename);
1238   make_cleanup (xfree, proc_stat);
1239
1240   if (proc_stat == NULL || *proc_stat == '\0')
1241     {
1242       /* Despite being unable to read more information about the
1243          process, we return 1 here because at least we have its
1244          command line, PID and arguments.  */
1245       do_cleanups (c);
1246       return 1;
1247     }
1248
1249   /* Ok, we have the stats.  It's time to do a little parsing of the
1250      contents of the buffer, so that we end up reading what we want.
1251
1252      The following parsing mechanism is strongly based on the
1253      information generated by the `fs/proc/array.c' file, present in
1254      the Linux kernel tree.  More details about how the information is
1255      displayed can be obtained by seeing the manpage of proc(5),
1256      specifically under the entry of `/proc/[pid]/stat'.  */
1257
1258   /* Getting rid of the PID, since we already have it.  */
1259   while (isdigit (*proc_stat))
1260     ++proc_stat;
1261
1262   proc_stat = skip_spaces (proc_stat);
1263
1264   /* Getting rid of the executable name, since we already have it.  We
1265      know that this name will be in parentheses, so we can safely look
1266      for the close-paren.  */
1267   while (*proc_stat != ')')
1268     ++proc_stat;
1269   ++proc_stat;
1270
1271   proc_stat = skip_spaces (proc_stat);
1272
1273   n_fields = sscanf (proc_stat,
1274                      "%c"               /* Process state.  */
1275                      "%d%d%d"           /* Parent PID, group ID, session ID.  */
1276                      "%*d%*d"           /* tty_nr, tpgid (not used).  */
1277                      "%u"               /* Flags.  */
1278                      "%*s%*s%*s%*s"     /* minflt, cminflt, majflt,
1279                                            cmajflt (not used).  */
1280                      "%*s%*s%*s%*s"     /* utime, stime, cutime,
1281                                            cstime (not used).  */
1282                      "%*s"              /* Priority (not used).  */
1283                      "%ld",             /* Nice.  */
1284                      &pr_sname,
1285                      &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1286                      &pr_flag,
1287                      &pr_nice);
1288
1289   if (n_fields != 6)
1290     {
1291       /* Again, we couldn't read the complementary information about
1292          the process state.  However, we already have minimal
1293          information, so we just return 1 here.  */
1294       do_cleanups (c);
1295       return 1;
1296     }
1297
1298   /* Filling the structure fields.  */
1299   prog_state = strchr (valid_states, pr_sname);
1300   if (prog_state != NULL)
1301     p->pr_state = prog_state - valid_states;
1302   else
1303     {
1304       /* Zero means "Running".  */
1305       p->pr_state = 0;
1306     }
1307
1308   p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
1309   p->pr_zomb = p->pr_sname == 'Z';
1310   p->pr_nice = pr_nice;
1311   p->pr_flag = pr_flag;
1312
1313   /* Finally, obtaining the UID and GID.  For that, we read and parse the
1314      contents of the `/proc/PID/status' file.  */
1315   xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
1316   proc_status = target_fileio_read_stralloc (filename);
1317   make_cleanup (xfree, proc_status);
1318
1319   if (proc_status == NULL || *proc_status == '\0')
1320     {
1321       /* Returning 1 since we already have a bunch of information.  */
1322       do_cleanups (c);
1323       return 1;
1324     }
1325
1326   /* Extracting the UID.  */
1327   tmpstr = strstr (proc_status, "Uid:");
1328   if (tmpstr != NULL)
1329     {
1330       /* Advancing the pointer to the beginning of the UID.  */
1331       tmpstr += sizeof ("Uid:");
1332       while (*tmpstr != '\0' && !isdigit (*tmpstr))
1333         ++tmpstr;
1334
1335       if (isdigit (*tmpstr))
1336         p->pr_uid = strtol (tmpstr, &tmpstr, 10);
1337     }
1338
1339   /* Extracting the GID.  */
1340   tmpstr = strstr (proc_status, "Gid:");
1341   if (tmpstr != NULL)
1342     {
1343       /* Advancing the pointer to the beginning of the GID.  */
1344       tmpstr += sizeof ("Gid:");
1345       while (*tmpstr != '\0' && !isdigit (*tmpstr))
1346         ++tmpstr;
1347
1348       if (isdigit (*tmpstr))
1349         p->pr_gid = strtol (tmpstr, &tmpstr, 10);
1350     }
1351
1352   do_cleanups (c);
1353
1354   return 1;
1355 }
1356
1357 /* Fills the "to_make_corefile_note" target vector.  Builds the note
1358    section for a corefile, and returns it in a malloc buffer.  */
1359
1360 char *
1361 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
1362                            linux_collect_thread_registers_ftype collect)
1363 {
1364   struct linux_corefile_thread_data thread_args;
1365   struct elf_internal_linux_prpsinfo prpsinfo;
1366   char *note_data = NULL;
1367   gdb_byte *auxv;
1368   int auxv_len;
1369
1370   if (linux_fill_prpsinfo (&prpsinfo))
1371     {
1372       if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch))
1373         {
1374           note_data = gdbarch_elfcore_write_linux_prpsinfo (gdbarch, obfd,
1375                                                             note_data, note_size,
1376                                                             &prpsinfo);
1377         }
1378       else
1379         {
1380           if (gdbarch_ptr_bit (gdbarch) == 64)
1381             note_data = elfcore_write_linux_prpsinfo64 (obfd,
1382                                                         note_data, note_size,
1383                                                         &prpsinfo);
1384           else
1385             note_data = elfcore_write_linux_prpsinfo32 (obfd,
1386                                                         note_data, note_size,
1387                                                         &prpsinfo);
1388         }
1389     }
1390
1391   /* Thread register information.  */
1392   thread_args.gdbarch = gdbarch;
1393   thread_args.pid = ptid_get_pid (inferior_ptid);
1394   thread_args.obfd = obfd;
1395   thread_args.note_data = note_data;
1396   thread_args.note_size = note_size;
1397   thread_args.num_notes = 0;
1398   thread_args.stop_signal = find_stop_signal ();
1399   thread_args.collect = collect;
1400   iterate_over_threads (linux_corefile_thread_callback, &thread_args);
1401   note_data = thread_args.note_data;
1402   if (!note_data)
1403     return NULL;
1404
1405   /* Auxillary vector.  */
1406   auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
1407                                 NULL, &auxv);
1408   if (auxv_len > 0)
1409     {
1410       note_data = elfcore_write_note (obfd, note_data, note_size,
1411                                       "CORE", NT_AUXV, auxv, auxv_len);
1412       xfree (auxv);
1413
1414       if (!note_data)
1415         return NULL;
1416     }
1417
1418   /* SPU information.  */
1419   note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
1420   if (!note_data)
1421     return NULL;
1422
1423   /* File mappings.  */
1424   note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
1425                                                   note_data, note_size);
1426
1427   make_cleanup (xfree, note_data);
1428   return note_data;
1429 }
1430
1431 static char *
1432 linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1433 {
1434   /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
1435      converted to gdbarch_core_regset_sections, we no longer need to fall back
1436      to the target method at this point.  */
1437
1438   if (!gdbarch_core_regset_sections (gdbarch))
1439     return target_make_corefile_notes (obfd, note_size);
1440   else
1441     return linux_make_corefile_notes (gdbarch, obfd, note_size,
1442                                       linux_collect_thread_registers);
1443 }
1444
1445 /* To be called from the various GDB_OSABI_LINUX handlers for the
1446    various GNU/Linux architectures and machine types.  */
1447
1448 void
1449 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1450 {
1451   set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
1452   set_gdbarch_info_proc (gdbarch, linux_info_proc);
1453   set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
1454   set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
1455   set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
1456   set_gdbarch_has_shared_address_space (gdbarch,
1457                                         linux_has_shared_address_space);
1458 }
1459
1460 /* Provide a prototype to silence -Wmissing-prototypes.  */
1461 extern initialize_file_ftype _initialize_linux_tdep;
1462
1463 void
1464 _initialize_linux_tdep (void)
1465 {
1466   linux_gdbarch_data_handle =
1467     gdbarch_data_register_post_init (init_linux_gdbarch_data);
1468 }