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