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