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