gdb/linux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for GNU/Linux, architecture independent.

  2.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbtypes.h"
  16. #include "linux-tdep.h"
  17. #include "auxv.h"
  18. #include "target.h"
  19. #include "gdbthread.h"
  20. #include "gdbcore.h"
  21. #include "regcache.h"
  22. #include "regset.h"
  23. #include "elf/common.h"
  24. #include "elf-bfd.h"            /* for elfcore_write_* */
  25. #include "inferior.h"
  26. #include "cli/cli-utils.h"
  27. #include "arch-utils.h"
  28. #include "gdb_obstack.h"
  29. #include "observer.h"
  30. #include "objfiles.h"
  31. #include "infcall.h"

  32. #include <ctype.h>

  33. /* This enum represents the signals' numbers on a generic architecture
  34.    running the Linux kernel.  The definition of "generic" comes from
  35.    the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
  36.    tree, which is the "de facto" implementation of signal numbers to
  37.    be used by new architecture ports.

  38.    For those architectures which have differences between the generic
  39.    standard (e.g., Alpha), we define the different signals (and *only*
  40.    those) in the specific target-dependent file (e.g.,
  41.    alpha-linux-tdep.c, for Alpha).  Please refer to the architecture's
  42.    tdep file for more information.

  43.    ARM deserves a special mention here.  On the file
  44.    <arch/arm/include/uapi/asm/signal.h>, it defines only one different
  45.    (and ARM-only) signal, which is SIGSWI, with the same number as
  46.    SIGRTMIN.  This signal is used only for a very specific target,
  47.    called ArthurOS (from RISCOS).  Therefore, we do not handle it on
  48.    the ARM-tdep file, and we can safely use the generic signal handler
  49.    here for ARM targets.

  50.    As stated above, this enum is derived from
  51.    <include/uapi/asm-generic/signal.h>, from the Linux kernel
  52.    tree.  */

  53. enum
  54.   {
  55.     LINUX_SIGHUP = 1,
  56.     LINUX_SIGINT = 2,
  57.     LINUX_SIGQUIT = 3,
  58.     LINUX_SIGILL = 4,
  59.     LINUX_SIGTRAP = 5,
  60.     LINUX_SIGABRT = 6,
  61.     LINUX_SIGIOT = 6,
  62.     LINUX_SIGBUS = 7,
  63.     LINUX_SIGFPE = 8,
  64.     LINUX_SIGKILL = 9,
  65.     LINUX_SIGUSR1 = 10,
  66.     LINUX_SIGSEGV = 11,
  67.     LINUX_SIGUSR2 = 12,
  68.     LINUX_SIGPIPE = 13,
  69.     LINUX_SIGALRM = 14,
  70.     LINUX_SIGTERM = 15,
  71.     LINUX_SIGSTKFLT = 16,
  72.     LINUX_SIGCHLD = 17,
  73.     LINUX_SIGCONT = 18,
  74.     LINUX_SIGSTOP = 19,
  75.     LINUX_SIGTSTP = 20,
  76.     LINUX_SIGTTIN = 21,
  77.     LINUX_SIGTTOU = 22,
  78.     LINUX_SIGURG = 23,
  79.     LINUX_SIGXCPU = 24,
  80.     LINUX_SIGXFSZ = 25,
  81.     LINUX_SIGVTALRM = 26,
  82.     LINUX_SIGPROF = 27,
  83.     LINUX_SIGWINCH = 28,
  84.     LINUX_SIGIO = 29,
  85.     LINUX_SIGPOLL = LINUX_SIGIO,
  86.     LINUX_SIGPWR = 30,
  87.     LINUX_SIGSYS = 31,
  88.     LINUX_SIGUNUSED = 31,

  89.     LINUX_SIGRTMIN = 32,
  90.     LINUX_SIGRTMAX = 64,
  91.   };

  92. static struct gdbarch_data *linux_gdbarch_data_handle;

  93. struct linux_gdbarch_data
  94.   {
  95.     struct type *siginfo_type;
  96.   };

  97. static void *
  98. init_linux_gdbarch_data (struct gdbarch *gdbarch)
  99. {
  100.   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
  101. }

  102. static struct linux_gdbarch_data *
  103. get_linux_gdbarch_data (struct gdbarch *gdbarch)
  104. {
  105.   return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
  106. }

  107. /* Per-inferior data key.  */
  108. static const struct inferior_data *linux_inferior_data;

  109. /* Linux-specific cached data.  This is used by GDB for caching
  110.    purposes for each inferior.  This helps reduce the overhead of
  111.    transfering data from a remote target to the local host.  */
  112. struct linux_info
  113. {
  114.   /* Cache of the inferior's vsyscall/vDSO mapping range.  Only valid
  115.      if VSYSCALL_RANGE_P is positive.  This is cached because getting
  116.      at this info requires an auxv lookup (which is itself cached),
  117.      and looking through the inferior's mappings (which change
  118.      throughout execution and therefore cannot be cached).  */
  119.   struct mem_range vsyscall_range;

  120.   /* Zero if we haven't tried looking up the vsyscall's range before
  121.      yet.  Positive if we tried looking it up, and found it.  Negative
  122.      if we tried looking it up but failed.  */
  123.   int vsyscall_range_p;
  124. };

  125. /* Frees whatever allocated space there is to be freed and sets INF's
  126.    linux cache data pointer to NULL.  */

  127. static void
  128. invalidate_linux_cache_inf (struct inferior *inf)
  129. {
  130.   struct linux_info *info;

  131.   info = inferior_data (inf, linux_inferior_data);
  132.   if (info != NULL)
  133.     {
  134.       xfree (info);
  135.       set_inferior_data (inf, linux_inferior_data, NULL);
  136.     }
  137. }

  138. /* Handles the cleanup of the linux cache for inferior INF.  ARG is
  139.    ignored.  Callback for the inferior_appeared and inferior_exit
  140.    events.  */

  141. static void
  142. linux_inferior_data_cleanup (struct inferior *inf, void *arg)
  143. {
  144.   invalidate_linux_cache_inf (inf);
  145. }

  146. /* Fetch the linux cache info for INF.  This function always returns a
  147.    valid INFO pointer.  */

  148. static struct linux_info *
  149. get_linux_inferior_data (void)
  150. {
  151.   struct linux_info *info;
  152.   struct inferior *inf = current_inferior ();

  153.   info = inferior_data (inf, linux_inferior_data);
  154.   if (info == NULL)
  155.     {
  156.       info = XCNEW (struct linux_info);
  157.       set_inferior_data (inf, linux_inferior_data, info);
  158.     }

  159.   return info;
  160. }

  161. /* This function is suitable for architectures that don't
  162.    extend/override the standard siginfo structure.  */

  163. struct type *
  164. linux_get_siginfo_type (struct gdbarch *gdbarch)
  165. {
  166.   struct linux_gdbarch_data *linux_gdbarch_data;
  167.   struct type *int_type, *uint_type, *long_type, *void_ptr_type;
  168.   struct type *uid_type, *pid_type;
  169.   struct type *sigval_type, *clock_type;
  170.   struct type *siginfo_type, *sifields_type;
  171.   struct type *type;

  172.   linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
  173.   if (linux_gdbarch_data->siginfo_type != NULL)
  174.     return linux_gdbarch_data->siginfo_type;

  175.   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  176.                                  0, "int");
  177.   uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  178.                                  1, "unsigned int");
  179.   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  180.                                  0, "long");
  181.   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);

  182.   /* sival_t */
  183.   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
  184.   TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
  185.   append_composite_type_field (sigval_type, "sival_int", int_type);
  186.   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);

  187.   /* __pid_t */
  188.   pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  189.                         TYPE_LENGTH (int_type), "__pid_t");
  190.   TYPE_TARGET_TYPE (pid_type) = int_type;
  191.   TYPE_TARGET_STUB (pid_type) = 1;

  192.   /* __uid_t */
  193.   uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  194.                         TYPE_LENGTH (uint_type), "__uid_t");
  195.   TYPE_TARGET_TYPE (uid_type) = uint_type;
  196.   TYPE_TARGET_STUB (uid_type) = 1;

  197.   /* __clock_t */
  198.   clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
  199.                           TYPE_LENGTH (long_type), "__clock_t");
  200.   TYPE_TARGET_TYPE (clock_type) = long_type;
  201.   TYPE_TARGET_STUB (clock_type) = 1;

  202.   /* _sifields */
  203.   sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);

  204.   {
  205.     const int si_max_size = 128;
  206.     int si_pad_size;
  207.     int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;

  208.     /* _pad */
  209.     if (gdbarch_ptr_bit (gdbarch) == 64)
  210.       si_pad_size = (si_max_size / size_of_int) - 4;
  211.     else
  212.       si_pad_size = (si_max_size / size_of_int) - 3;
  213.     append_composite_type_field (sifields_type, "_pad",
  214.                                  init_vector_type (int_type, si_pad_size));
  215.   }

  216.   /* _kill */
  217.   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  218.   append_composite_type_field (type, "si_pid", pid_type);
  219.   append_composite_type_field (type, "si_uid", uid_type);
  220.   append_composite_type_field (sifields_type, "_kill", type);

  221.   /* _timer */
  222.   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  223.   append_composite_type_field (type, "si_tid", int_type);
  224.   append_composite_type_field (type, "si_overrun", int_type);
  225.   append_composite_type_field (type, "si_sigval", sigval_type);
  226.   append_composite_type_field (sifields_type, "_timer", type);

  227.   /* _rt */
  228.   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  229.   append_composite_type_field (type, "si_pid", pid_type);
  230.   append_composite_type_field (type, "si_uid", uid_type);
  231.   append_composite_type_field (type, "si_sigval", sigval_type);
  232.   append_composite_type_field (sifields_type, "_rt", type);

  233.   /* _sigchld */
  234.   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  235.   append_composite_type_field (type, "si_pid", pid_type);
  236.   append_composite_type_field (type, "si_uid", uid_type);
  237.   append_composite_type_field (type, "si_status", int_type);
  238.   append_composite_type_field (type, "si_utime", clock_type);
  239.   append_composite_type_field (type, "si_stime", clock_type);
  240.   append_composite_type_field (sifields_type, "_sigchld", type);

  241.   /* _sigfault */
  242.   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  243.   append_composite_type_field (type, "si_addr", void_ptr_type);
  244.   append_composite_type_field (sifields_type, "_sigfault", type);

  245.   /* _sigpoll */
  246.   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  247.   append_composite_type_field (type, "si_band", long_type);
  248.   append_composite_type_field (type, "si_fd", int_type);
  249.   append_composite_type_field (sifields_type, "_sigpoll", type);

  250.   /* struct siginfo */
  251.   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  252.   TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
  253.   append_composite_type_field (siginfo_type, "si_signo", int_type);
  254.   append_composite_type_field (siginfo_type, "si_errno", int_type);
  255.   append_composite_type_field (siginfo_type, "si_code", int_type);
  256.   append_composite_type_field_aligned (siginfo_type,
  257.                                        "_sifields", sifields_type,
  258.                                        TYPE_LENGTH (long_type));

  259.   linux_gdbarch_data->siginfo_type = siginfo_type;

  260.   return siginfo_type;
  261. }

  262. /* Return true if the target is running on uClinux instead of normal
  263.    Linux kernel.  */

  264. int
  265. linux_is_uclinux (void)
  266. {
  267.   CORE_ADDR dummy;

  268.   return (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
  269.           && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
  270. }

  271. static int
  272. linux_has_shared_address_space (struct gdbarch *gdbarch)
  273. {
  274.   return linux_is_uclinux ();
  275. }

  276. /* This is how we want PTIDs from core files to be printed.  */

  277. static char *
  278. linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
  279. {
  280.   static char buf[80];

  281.   if (ptid_get_lwp (ptid) != 0)
  282.     {
  283.       snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
  284.       return buf;
  285.     }

  286.   return normal_pid_to_str (ptid);
  287. }

  288. /* Service function for corefiles and info proc.  */

  289. static void
  290. read_mapping (const char *line,
  291.               ULONGEST *addr, ULONGEST *endaddr,
  292.               const char **permissions, size_t *permissions_len,
  293.               ULONGEST *offset,
  294.               const char **device, size_t *device_len,
  295.               ULONGEST *inode,
  296.               const char **filename)
  297. {
  298.   const char *p = line;

  299.   *addr = strtoulst (p, &p, 16);
  300.   if (*p == '-')
  301.     p++;
  302.   *endaddr = strtoulst (p, &p, 16);

  303.   p = skip_spaces_const (p);
  304.   *permissions = p;
  305.   while (*p && !isspace (*p))
  306.     p++;
  307.   *permissions_len = p - *permissions;

  308.   *offset = strtoulst (p, &p, 16);

  309.   p = skip_spaces_const (p);
  310.   *device = p;
  311.   while (*p && !isspace (*p))
  312.     p++;
  313.   *device_len = p - *device;

  314.   *inode = strtoulst (p, &p, 10);

  315.   p = skip_spaces_const (p);
  316.   *filename = p;
  317. }

  318. /* Implement the "info proc" command.  */

  319. static void
  320. linux_info_proc (struct gdbarch *gdbarch, const char *args,
  321.                  enum info_proc_what what)
  322. {
  323.   /* A long is used for pid instead of an int to avoid a loss of precision
  324.      compiler warning from the output of strtoul.  */
  325.   long pid;
  326.   int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
  327.   int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
  328.   int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
  329.   int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
  330.   int status_f = (what == IP_STATUS || what == IP_ALL);
  331.   int stat_f = (what == IP_STAT || what == IP_ALL);
  332.   char filename[100];
  333.   char *data;
  334.   int target_errno;

  335.   if (args && isdigit (args[0]))
  336.     {
  337.       char *tem;

  338.       pid = strtoul (args, &tem, 10);
  339.       args = tem;
  340.     }
  341.   else
  342.     {
  343.       if (!target_has_execution)
  344.         error (_("No current process: you must name one."));
  345.       if (current_inferior ()->fake_pid_p)
  346.         error (_("Can't determine the current process's PID: you must name one."));

  347.       pid = current_inferior ()->pid;
  348.     }

  349.   args = skip_spaces_const (args);
  350.   if (args && args[0])
  351.     error (_("Too many parameters: %s"), args);

  352.   printf_filtered (_("process %ld\n"), pid);
  353.   if (cmdline_f)
  354.     {
  355.       xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
  356.       data = target_fileio_read_stralloc (filename);
  357.       if (data)
  358.         {
  359.           struct cleanup *cleanup = make_cleanup (xfree, data);
  360.           printf_filtered ("cmdline = '%s'\n", data);
  361.           do_cleanups (cleanup);
  362.         }
  363.       else
  364.         warning (_("unable to open /proc file '%s'"), filename);
  365.     }
  366.   if (cwd_f)
  367.     {
  368.       xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
  369.       data = target_fileio_readlink (filename, &target_errno);
  370.       if (data)
  371.         {
  372.           struct cleanup *cleanup = make_cleanup (xfree, data);
  373.           printf_filtered ("cwd = '%s'\n", data);
  374.           do_cleanups (cleanup);
  375.         }
  376.       else
  377.         warning (_("unable to read link '%s'"), filename);
  378.     }
  379.   if (exe_f)
  380.     {
  381.       xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
  382.       data = target_fileio_readlink (filename, &target_errno);
  383.       if (data)
  384.         {
  385.           struct cleanup *cleanup = make_cleanup (xfree, data);
  386.           printf_filtered ("exe = '%s'\n", data);
  387.           do_cleanups (cleanup);
  388.         }
  389.       else
  390.         warning (_("unable to read link '%s'"), filename);
  391.     }
  392.   if (mappings_f)
  393.     {
  394.       xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
  395.       data = target_fileio_read_stralloc (filename);
  396.       if (data)
  397.         {
  398.           struct cleanup *cleanup = make_cleanup (xfree, data);
  399.           char *line;

  400.           printf_filtered (_("Mapped address spaces:\n\n"));
  401.           if (gdbarch_addr_bit (gdbarch) == 32)
  402.             {
  403.               printf_filtered ("\t%10s %10s %10s %10s %s\n",
  404.                            "Start Addr",
  405.                            "  End Addr",
  406.                            "      Size", "    Offset", "objfile");
  407.             }
  408.           else
  409.             {
  410.               printf_filtered (%18s %18s %10s %10s %s\n",
  411.                            "Start Addr",
  412.                            "  End Addr",
  413.                            "      Size", "    Offset", "objfile");
  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.               read_mapping (line, &addr, &endaddr,
  421.                             &permissions, &permissions_len,
  422.                             &offset, &device, &device_len,
  423.                             &inode, &filename);

  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.           do_cleanups (cleanup);
  444.         }
  445.       else
  446.         warning (_("unable to open /proc file '%s'"), filename);
  447.     }
  448.   if (status_f)
  449.     {
  450.       xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
  451.       data = target_fileio_read_stralloc (filename);
  452.       if (data)
  453.         {
  454.           struct cleanup *cleanup = make_cleanup (xfree, data);
  455.           puts_filtered (data);
  456.           do_cleanups (cleanup);
  457.         }
  458.       else
  459.         warning (_("unable to open /proc file '%s'"), filename);
  460.     }
  461.   if (stat_f)
  462.     {
  463.       xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
  464.       data = target_fileio_read_stralloc (filename);
  465.       if (data)
  466.         {
  467.           struct cleanup *cleanup = make_cleanup (xfree, data);
  468.           const char *p = data;

  469.           printf_filtered (_("Process: %s\n"),
  470.                            pulongest (strtoulst (p, &p, 10)));

  471.           p = skip_spaces_const (p);
  472.           if (*p == '(')
  473.             {
  474.               /* ps command also relies on no trailing fields
  475.                  ever contain ')'.  */
  476.               const char *ep = strrchr (p, ')');
  477.               if (ep != NULL)
  478.                 {
  479.                   printf_filtered ("Exec file: %.*s\n",
  480.                                    (int) (ep - p - 1), p + 1);
  481.                   p = ep + 1;
  482.                 }
  483.             }

  484.           p = skip_spaces_const (p);
  485.           if (*p)
  486.             printf_filtered (_("State: %c\n"), *p++);

  487.           if (*p)
  488.             printf_filtered (_("Parent process: %s\n"),
  489.                              pulongest (strtoulst (p, &p, 10)));
  490.           if (*p)
  491.             printf_filtered (_("Process group: %s\n"),
  492.                              pulongest (strtoulst (p, &p, 10)));
  493.           if (*p)
  494.             printf_filtered (_("Session id: %s\n"),
  495.                              pulongest (strtoulst (p, &p, 10)));
  496.           if (*p)
  497.             printf_filtered (_("TTY: %s\n"),
  498.                              pulongest (strtoulst (p, &p, 10)));
  499.           if (*p)
  500.             printf_filtered (_("TTY owner process group: %s\n"),
  501.                              pulongest (strtoulst (p, &p, 10)));

  502.           if (*p)
  503.             printf_filtered (_("Flags: %s\n"),
  504.                              hex_string (strtoulst (p, &p, 10)));
  505.           if (*p)
  506.             printf_filtered (_("Minor faults (no memory page): %s\n"),
  507.                              pulongest (strtoulst (p, &p, 10)));
  508.           if (*p)
  509.             printf_filtered (_("Minor faults, children: %s\n"),
  510.                              pulongest (strtoulst (p, &p, 10)));
  511.           if (*p)
  512.             printf_filtered (_("Major faults (memory page faults): %s\n"),
  513.                              pulongest (strtoulst (p, &p, 10)));
  514.           if (*p)
  515.             printf_filtered (_("Major faults, children: %s\n"),
  516.                              pulongest (strtoulst (p, &p, 10)));
  517.           if (*p)
  518.             printf_filtered (_("utime: %s\n"),
  519.                              pulongest (strtoulst (p, &p, 10)));
  520.           if (*p)
  521.             printf_filtered (_("stime: %s\n"),
  522.                              pulongest (strtoulst (p, &p, 10)));
  523.           if (*p)
  524.             printf_filtered (_("utime, children: %s\n"),
  525.                              pulongest (strtoulst (p, &p, 10)));
  526.           if (*p)
  527.             printf_filtered (_("stime, children: %s\n"),
  528.                              pulongest (strtoulst (p, &p, 10)));
  529.           if (*p)
  530.             printf_filtered (_("jiffies remaining in current "
  531.                                "time slice: %s\n"),
  532.                              pulongest (strtoulst (p, &p, 10)));
  533.           if (*p)
  534.             printf_filtered (_("'nice' value: %s\n"),
  535.                              pulongest (strtoulst (p, &p, 10)));
  536.           if (*p)
  537.             printf_filtered (_("jiffies until next timeout: %s\n"),
  538.                              pulongest (strtoulst (p, &p, 10)));
  539.           if (*p)
  540.             printf_filtered (_("jiffies until next SIGALRM: %s\n"),
  541.                              pulongest (strtoulst (p, &p, 10)));
  542.           if (*p)
  543.             printf_filtered (_("start time (jiffies since "
  544.                                "system boot): %s\n"),
  545.                              pulongest (strtoulst (p, &p, 10)));
  546.           if (*p)
  547.             printf_filtered (_("Virtual memory size: %s\n"),
  548.                              pulongest (strtoulst (p, &p, 10)));
  549.           if (*p)
  550.             printf_filtered (_("Resident set size: %s\n"),
  551.                              pulongest (strtoulst (p, &p, 10)));
  552.           if (*p)
  553.             printf_filtered (_("rlim: %s\n"),
  554.                              pulongest (strtoulst (p, &p, 10)));
  555.           if (*p)
  556.             printf_filtered (_("Start of text: %s\n"),
  557.                              hex_string (strtoulst (p, &p, 10)));
  558.           if (*p)
  559.             printf_filtered (_("End of text: %s\n"),
  560.                              hex_string (strtoulst (p, &p, 10)));
  561.           if (*p)
  562.             printf_filtered (_("Start of stack: %s\n"),
  563.                              hex_string (strtoulst (p, &p, 10)));
  564. #if 0        /* Don't know how architecture-dependent the rest is...
  565.            Anyway the signal bitmap info is available from "status".  */
  566.           if (*p)
  567.             printf_filtered (_("Kernel stack pointer: %s\n"),
  568.                              hex_string (strtoulst (p, &p, 10)));
  569.           if (*p)
  570.             printf_filtered (_("Kernel instr pointer: %s\n"),
  571.                              hex_string (strtoulst (p, &p, 10)));
  572.           if (*p)
  573.             printf_filtered (_("Pending signals bitmap: %s\n"),
  574.                              hex_string (strtoulst (p, &p, 10)));
  575.           if (*p)
  576.             printf_filtered (_("Blocked signals bitmap: %s\n"),
  577.                              hex_string (strtoulst (p, &p, 10)));
  578.           if (*p)
  579.             printf_filtered (_("Ignored signals bitmap: %s\n"),
  580.                              hex_string (strtoulst (p, &p, 10)));
  581.           if (*p)
  582.             printf_filtered (_("Catched signals bitmap: %s\n"),
  583.                              hex_string (strtoulst (p, &p, 10)));
  584.           if (*p)
  585.             printf_filtered (_("wchan (system call): %s\n"),
  586.                              hex_string (strtoulst (p, &p, 10)));
  587. #endif
  588.           do_cleanups (cleanup);
  589.         }
  590.       else
  591.         warning (_("unable to open /proc file '%s'"), filename);
  592.     }
  593. }

  594. /* Implement "info proc mappings" for a corefile.  */

  595. static void
  596. linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
  597. {
  598.   asection *section;
  599.   ULONGEST count, page_size;
  600.   unsigned char *descdata, *filenames, *descend, *contents;
  601.   size_t note_size;
  602.   unsigned int addr_size_bits, addr_size;
  603.   struct cleanup *cleanup;
  604.   struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
  605.   /* We assume this for reading 64-bit core files.  */
  606.   gdb_static_assert (sizeof (ULONGEST) >= 8);

  607.   section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
  608.   if (section == NULL)
  609.     {
  610.       warning (_("unable to find mappings in core file"));
  611.       return;
  612.     }

  613.   addr_size_bits = gdbarch_addr_bit (core_gdbarch);
  614.   addr_size = addr_size_bits / 8;
  615.   note_size = bfd_get_section_size (section);

  616.   if (note_size < 2 * addr_size)
  617.     error (_("malformed core note - too short for header"));

  618.   contents = xmalloc (note_size);
  619.   cleanup = make_cleanup (xfree, contents);
  620.   if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
  621.     error (_("could not get core note contents"));

  622.   descdata = contents;
  623.   descend = descdata + note_size;

  624.   if (descdata[note_size - 1] != '\0')
  625.     error (_("malformed note - does not end with \\0"));

  626.   count = bfd_get (addr_size_bits, core_bfd, descdata);
  627.   descdata += addr_size;

  628.   page_size = bfd_get (addr_size_bits, core_bfd, descdata);
  629.   descdata += addr_size;

  630.   if (note_size < 2 * addr_size + count * 3 * addr_size)
  631.     error (_("malformed note - too short for supplied file count"));

  632.   printf_filtered (_("Mapped address spaces:\n\n"));
  633.   if (gdbarch_addr_bit (gdbarch) == 32)
  634.     {
  635.       printf_filtered ("\t%10s %10s %10s %10s %s\n",
  636.                        "Start Addr",
  637.                        "  End Addr",
  638.                        "      Size", "    Offset", "objfile");
  639.     }
  640.   else
  641.     {
  642.       printf_filtered (%18s %18s %10s %10s %s\n",
  643.                        "Start Addr",
  644.                        "  End Addr",
  645.                        "      Size", "    Offset", "objfile");
  646.     }

  647.   filenames = descdata + count * 3 * addr_size;
  648.   while (--count > 0)
  649.     {
  650.       ULONGEST start, end, file_ofs;

  651.       if (filenames == descend)
  652.         error (_("malformed note - filenames end too early"));

  653.       start = bfd_get (addr_size_bits, core_bfd, descdata);
  654.       descdata += addr_size;
  655.       end = bfd_get (addr_size_bits, core_bfd, descdata);
  656.       descdata += addr_size;
  657.       file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
  658.       descdata += addr_size;

  659.       file_ofs *= page_size;

  660.       if (gdbarch_addr_bit (gdbarch) == 32)
  661.         printf_filtered ("\t%10s %10s %10s %10s %s\n",
  662.                          paddress (gdbarch, start),
  663.                          paddress (gdbarch, end),
  664.                          hex_string (end - start),
  665.                          hex_string (file_ofs),
  666.                          filenames);
  667.       else
  668.         printf_filtered (%18s %18s %10s %10s %s\n",
  669.                          paddress (gdbarch, start),
  670.                          paddress (gdbarch, end),
  671.                          hex_string (end - start),
  672.                          hex_string (file_ofs),
  673.                          filenames);

  674.       filenames += 1 + strlen ((char *) filenames);
  675.     }

  676.   do_cleanups (cleanup);
  677. }

  678. /* Implement "info proc" for a corefile.  */

  679. static void
  680. linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
  681.                       enum info_proc_what what)
  682. {
  683.   int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
  684.   int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);

  685.   if (exe_f)
  686.     {
  687.       const char *exe;

  688.       exe = bfd_core_file_failing_command (core_bfd);
  689.       if (exe != NULL)
  690.         printf_filtered ("exe = '%s'\n", exe);
  691.       else
  692.         warning (_("unable to find command name in core file"));
  693.     }

  694.   if (mappings_f)
  695.     linux_core_info_proc_mappings (gdbarch, args);

  696.   if (!exe_f && !mappings_f)
  697.     error (_("unable to handle request"));
  698. }

  699. typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
  700.                                             ULONGEST offset, ULONGEST inode,
  701.                                             int read, int write,
  702.                                             int exec, int modified,
  703.                                             const char *filename,
  704.                                             void *data);

  705. /* List memory regions in the inferior for a corefile.  */

  706. static int
  707. linux_find_memory_regions_full (struct gdbarch *gdbarch,
  708.                                 linux_find_memory_region_ftype *func,
  709.                                 void *obfd)
  710. {
  711.   char mapsfilename[100];
  712.   char *data;

  713.   /* We need to know the real target PID to access /proc.  */
  714.   if (current_inferior ()->fake_pid_p)
  715.     return 1;

  716.   xsnprintf (mapsfilename, sizeof mapsfilename,
  717.              "/proc/%d/smaps", current_inferior ()->pid);
  718.   data = target_fileio_read_stralloc (mapsfilename);
  719.   if (data == NULL)
  720.     {
  721.       /* Older Linux kernels did not support /proc/PID/smaps.  */
  722.       xsnprintf (mapsfilename, sizeof mapsfilename,
  723.                  "/proc/%d/maps", current_inferior ()->pid);
  724.       data = target_fileio_read_stralloc (mapsfilename);
  725.     }
  726.   if (data)
  727.     {
  728.       struct cleanup *cleanup = make_cleanup (xfree, data);
  729.       char *line;

  730.       line = strtok (data, "\n");
  731.       while (line)
  732.         {
  733.           ULONGEST addr, endaddr, offset, inode;
  734.           const char *permissions, *device, *filename;
  735.           size_t permissions_len, device_len;
  736.           int read, write, exec;
  737.           int modified = 0, has_anonymous = 0;

  738.           read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
  739.                         &offset, &device, &device_len, &inode, &filename);

  740.           /* Decode permissions.  */
  741.           read = (memchr (permissions, 'r', permissions_len) != 0);
  742.           write = (memchr (permissions, 'w', permissions_len) != 0);
  743.           exec = (memchr (permissions, 'x', permissions_len) != 0);

  744.           /* Try to detect if region was modified by parsing smaps counters.  */
  745.           for (line = strtok (NULL, "\n");
  746.                line && line[0] >= 'A' && line[0] <= 'Z';
  747.                line = strtok (NULL, "\n"))
  748.             {
  749.               char keyword[64 + 1];

  750.               if (sscanf (line, "%64s", keyword) != 1)
  751.                 {
  752.                   warning (_("Error parsing {s,}maps file '%s'"), mapsfilename);
  753.                   break;
  754.                 }
  755.               if (strcmp (keyword, "Anonymous:") == 0)
  756.                 has_anonymous = 1;
  757.               if (strcmp (keyword, "Shared_Dirty:") == 0
  758.                   || strcmp (keyword, "Private_Dirty:") == 0
  759.                   || strcmp (keyword, "Swap:") == 0
  760.                   || strcmp (keyword, "Anonymous:") == 0)
  761.                 {
  762.                   unsigned long number;

  763.                   if (sscanf (line, "%*s%lu", &number) != 1)
  764.                     {
  765.                       warning (_("Error parsing {s,}maps file '%s' number"),
  766.                                mapsfilename);
  767.                       break;
  768.                     }
  769.                   if (number != 0)
  770.                     modified = 1;
  771.                 }
  772.             }

  773.           /* Older Linux kernels did not support the "Anonymous:" counter.
  774.              If it is missing, we can't be sure - dump all the pages.  */
  775.           if (!has_anonymous)
  776.             modified = 1;

  777.           /* Invoke the callback function to create the corefile segment.  */
  778.           func (addr, endaddr - addr, offset, inode,
  779.                 read, write, exec, modified, filename, obfd);
  780.         }

  781.       do_cleanups (cleanup);
  782.       return 0;
  783.     }

  784.   return 1;
  785. }

  786. /* A structure for passing information through
  787.    linux_find_memory_regions_full.  */

  788. struct linux_find_memory_regions_data
  789. {
  790.   /* The original callback.  */

  791.   find_memory_region_ftype func;

  792.   /* The original datum.  */

  793.   void *obfd;
  794. };

  795. /* A callback for linux_find_memory_regions that converts between the
  796.    "full"-style callback and find_memory_region_ftype.  */

  797. static int
  798. linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
  799.                                  ULONGEST offset, ULONGEST inode,
  800.                                  int read, int write, int exec, int modified,
  801.                                  const char *filename, void *arg)
  802. {
  803.   struct linux_find_memory_regions_data *data = arg;

  804.   return data->func (vaddr, size, read, write, exec, modified, data->obfd);
  805. }

  806. /* A variant of linux_find_memory_regions_full that is suitable as the
  807.    gdbarch find_memory_regions method.  */

  808. static int
  809. linux_find_memory_regions (struct gdbarch *gdbarch,
  810.                            find_memory_region_ftype func, void *obfd)
  811. {
  812.   struct linux_find_memory_regions_data data;

  813.   data.func = func;
  814.   data.obfd = obfd;

  815.   return linux_find_memory_regions_full (gdbarch,
  816.                                          linux_find_memory_regions_thunk,
  817.                                          &data);
  818. }

  819. /* Determine which signal stopped execution.  */

  820. static int
  821. find_signalled_thread (struct thread_info *info, void *data)
  822. {
  823.   if (info->suspend.stop_signal != GDB_SIGNAL_0
  824.       && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
  825.     return 1;

  826.   return 0;
  827. }

  828. static enum gdb_signal
  829. find_stop_signal (void)
  830. {
  831.   struct thread_info *info =
  832.     iterate_over_threads (find_signalled_thread, NULL);

  833.   if (info)
  834.     return info->suspend.stop_signal;
  835.   else
  836.     return GDB_SIGNAL_0;
  837. }

  838. /* Generate corefile notes for SPU contexts.  */

  839. static char *
  840. linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
  841. {
  842.   static const char *spu_files[] =
  843.     {
  844.       "object-id",
  845.       "mem",
  846.       "regs",
  847.       "fpcr",
  848.       "lslr",
  849.       "decr",
  850.       "decr_status",
  851.       "signal1",
  852.       "signal1_type",
  853.       "signal2",
  854.       "signal2_type",
  855.       "event_mask",
  856.       "event_status",
  857.       "mbox_info",
  858.       "ibox_info",
  859.       "wbox_info",
  860.       "dma_info",
  861.       "proxydma_info",
  862.    };

  863.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  864.   gdb_byte *spu_ids;
  865.   LONGEST i, j, size;

  866.   /* Determine list of SPU ids.  */
  867.   size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
  868.                             NULL, &spu_ids);

  869.   /* Generate corefile notes for each SPU file.  */
  870.   for (i = 0; i < size; i += 4)
  871.     {
  872.       int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);

  873.       for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
  874.         {
  875.           char annex[32], note_name[32];
  876.           gdb_byte *spu_data;
  877.           LONGEST spu_len;

  878.           xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
  879.           spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
  880.                                        annex, &spu_data);
  881.           if (spu_len > 0)
  882.             {
  883.               xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
  884.               note_data = elfcore_write_note (obfd, note_data, note_size,
  885.                                               note_name, NT_SPU,
  886.                                               spu_data, spu_len);
  887.               xfree (spu_data);

  888.               if (!note_data)
  889.                 {
  890.                   xfree (spu_ids);
  891.                   return NULL;
  892.                 }
  893.             }
  894.         }
  895.     }

  896.   if (size > 0)
  897.     xfree (spu_ids);

  898.   return note_data;
  899. }

  900. /* This is used to pass information from
  901.    linux_make_mappings_corefile_notes through
  902.    linux_find_memory_regions_full.  */

  903. struct linux_make_mappings_data
  904. {
  905.   /* Number of files mapped.  */
  906.   ULONGEST file_count;

  907.   /* The obstack for the main part of the data.  */
  908.   struct obstack *data_obstack;

  909.   /* The filename obstack.  */
  910.   struct obstack *filename_obstack;

  911.   /* The architecture's "long" type.  */
  912.   struct type *long_type;
  913. };

  914. static linux_find_memory_region_ftype linux_make_mappings_callback;

  915. /* A callback for linux_find_memory_regions_full that updates the
  916.    mappings data for linux_make_mappings_corefile_notes.  */

  917. static int
  918. linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
  919.                               ULONGEST offset, ULONGEST inode,
  920.                               int read, int write, int exec, int modified,
  921.                               const char *filename, void *data)
  922. {
  923.   struct linux_make_mappings_data *map_data = data;
  924.   gdb_byte buf[sizeof (ULONGEST)];

  925.   if (*filename == '\0' || inode == 0)
  926.     return 0;

  927.   ++map_data->file_count;

  928.   pack_long (buf, map_data->long_type, vaddr);
  929.   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
  930.   pack_long (buf, map_data->long_type, vaddr + size);
  931.   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
  932.   pack_long (buf, map_data->long_type, offset);
  933.   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));

  934.   obstack_grow_str0 (map_data->filename_obstack, filename);

  935.   return 0;
  936. }

  937. /* Write the file mapping data to the core file, if possible.  OBFD is
  938.    the output BFD.  NOTE_DATA is the current note data, and NOTE_SIZE
  939.    is a pointer to the note size.  Returns the new NOTE_DATA and
  940.    updates NOTE_SIZE.  */

  941. static char *
  942. linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
  943.                                     char *note_data, int *note_size)
  944. {
  945.   struct cleanup *cleanup;
  946.   struct obstack data_obstack, filename_obstack;
  947.   struct linux_make_mappings_data mapping_data;
  948.   struct type *long_type
  949.     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
  950.   gdb_byte buf[sizeof (ULONGEST)];

  951.   obstack_init (&data_obstack);
  952.   cleanup = make_cleanup_obstack_free (&data_obstack);
  953.   obstack_init (&filename_obstack);
  954.   make_cleanup_obstack_free (&filename_obstack);

  955.   mapping_data.file_count = 0;
  956.   mapping_data.data_obstack = &data_obstack;
  957.   mapping_data.filename_obstack = &filename_obstack;
  958.   mapping_data.long_type = long_type;

  959.   /* Reserve space for the count.  */
  960.   obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
  961.   /* We always write the page size as 1 since we have no good way to
  962.      determine the correct value.  */
  963.   pack_long (buf, long_type, 1);
  964.   obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));

  965.   linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
  966.                                   &mapping_data);

  967.   if (mapping_data.file_count != 0)
  968.     {
  969.       /* Write the count to the obstack.  */
  970.       pack_long ((gdb_byte *) obstack_base (&data_obstack),
  971.                  long_type, mapping_data.file_count);

  972.       /* Copy the filenames to the data obstack.  */
  973.       obstack_grow (&data_obstack, obstack_base (&filename_obstack),
  974.                     obstack_object_size (&filename_obstack));

  975.       note_data = elfcore_write_note (obfd, note_data, note_size,
  976.                                       "CORE", NT_FILE,
  977.                                       obstack_base (&data_obstack),
  978.                                       obstack_object_size (&data_obstack));
  979.     }

  980.   do_cleanups (cleanup);
  981.   return note_data;
  982. }

  983. /* Structure for passing information from
  984.    linux_collect_thread_registers via an iterator to
  985.    linux_collect_regset_section_cb. */

  986. struct linux_collect_regset_section_cb_data
  987. {
  988.   struct gdbarch *gdbarch;
  989.   const struct regcache *regcache;
  990.   bfd *obfd;
  991.   char *note_data;
  992.   int *note_size;
  993.   unsigned long lwp;
  994.   enum gdb_signal stop_signal;
  995.   int abort_iteration;
  996. };

  997. /* Callback for iterate_over_regset_sections that records a single
  998.    regset in the corefile note section.  */

  999. static void
  1000. linux_collect_regset_section_cb (const char *sect_name, int size,
  1001.                                  const struct regset *regset,
  1002.                                  const char *human_name, void *cb_data)
  1003. {
  1004.   char *buf;
  1005.   struct linux_collect_regset_section_cb_data *data = cb_data;

  1006.   if (data->abort_iteration)
  1007.     return;

  1008.   gdb_assert (regset && regset->collect_regset);

  1009.   buf = xmalloc (size);
  1010.   regset->collect_regset (regset, data->regcache, -1, buf, size);

  1011.   /* PRSTATUS still needs to be treated specially.  */
  1012.   if (strcmp (sect_name, ".reg") == 0)
  1013.     data->note_data = (char *) elfcore_write_prstatus
  1014.       (data->obfd, data->note_data, data->note_size, data->lwp,
  1015.        gdb_signal_to_host (data->stop_signal), buf);
  1016.   else
  1017.     data->note_data = (char *) elfcore_write_register_note
  1018.       (data->obfd, data->note_data, data->note_size,
  1019.        sect_name, buf, size);
  1020.   xfree (buf);

  1021.   if (data->note_data == NULL)
  1022.     data->abort_iteration = 1;
  1023. }

  1024. /* Records the thread's register state for the corefile note
  1025.    section.  */

  1026. static char *
  1027. linux_collect_thread_registers (const struct regcache *regcache,
  1028.                                 ptid_t ptid, bfd *obfd,
  1029.                                 char *note_data, int *note_size,
  1030.                                 enum gdb_signal stop_signal)
  1031. {
  1032.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1033.   struct linux_collect_regset_section_cb_data data;

  1034.   data.gdbarch = gdbarch;
  1035.   data.regcache = regcache;
  1036.   data.obfd = obfd;
  1037.   data.note_data = note_data;
  1038.   data.note_size = note_size;
  1039.   data.stop_signal = stop_signal;
  1040.   data.abort_iteration = 0;

  1041.   /* For remote targets the LWP may not be available, so use the TID.  */
  1042.   data.lwp = ptid_get_lwp (ptid);
  1043.   if (!data.lwp)
  1044.     data.lwp = ptid_get_tid (ptid);

  1045.   gdbarch_iterate_over_regset_sections (gdbarch,
  1046.                                         linux_collect_regset_section_cb,
  1047.                                         &data, regcache);
  1048.   return data.note_data;
  1049. }

  1050. /* Fetch the siginfo data for the current thread, if it exists.  If
  1051.    there is no data, or we could not read it, return NULL.  Otherwise,
  1052.    return a newly malloc'd buffer holding the data and fill in *SIZE
  1053.    with the size of the data.  The caller is responsible for freeing
  1054.    the data.  */

  1055. static gdb_byte *
  1056. linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
  1057. {
  1058.   struct type *siginfo_type;
  1059.   gdb_byte *buf;
  1060.   LONGEST bytes_read;
  1061.   struct cleanup *cleanups;

  1062.   if (!gdbarch_get_siginfo_type_p (gdbarch))
  1063.     return NULL;

  1064.   siginfo_type = gdbarch_get_siginfo_type (gdbarch);

  1065.   buf = xmalloc (TYPE_LENGTH (siginfo_type));
  1066.   cleanups = make_cleanup (xfree, buf);

  1067.   bytes_read = target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
  1068.                             buf, 0, TYPE_LENGTH (siginfo_type));
  1069.   if (bytes_read == TYPE_LENGTH (siginfo_type))
  1070.     {
  1071.       discard_cleanups (cleanups);
  1072.       *size = bytes_read;
  1073.     }
  1074.   else
  1075.     {
  1076.       do_cleanups (cleanups);
  1077.       buf = NULL;
  1078.     }

  1079.   return buf;
  1080. }

  1081. struct linux_corefile_thread_data
  1082. {
  1083.   struct gdbarch *gdbarch;
  1084.   int pid;
  1085.   bfd *obfd;
  1086.   char *note_data;
  1087.   int *note_size;
  1088.   enum gdb_signal stop_signal;
  1089. };

  1090. /* Called by gdbthread.c once per thread.  Records the thread's
  1091.    register state for the corefile note section.  */

  1092. static int
  1093. linux_corefile_thread_callback (struct thread_info *info, void *data)
  1094. {
  1095.   struct linux_corefile_thread_data *args = data;

  1096.   /* It can be current thread
  1097.      which cannot be removed by update_thread_list.  */
  1098.   if (info->state == THREAD_EXITED)
  1099.     return 0;

  1100.   if (ptid_get_pid (info->ptid) == args->pid)
  1101.     {
  1102.       struct cleanup *old_chain;
  1103.       struct regcache *regcache;
  1104.       gdb_byte *siginfo_data;
  1105.       LONGEST siginfo_size = 0;

  1106.       regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);

  1107.       old_chain = save_inferior_ptid ();
  1108.       inferior_ptid = info->ptid;
  1109.       target_fetch_registers (regcache, -1);
  1110.       siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
  1111.       do_cleanups (old_chain);

  1112.       old_chain = make_cleanup (xfree, siginfo_data);

  1113.       args->note_data = linux_collect_thread_registers
  1114.         (regcache, info->ptid, args->obfd, args->note_data,
  1115.          args->note_size, args->stop_signal);

  1116.       /* Don't return anything if we got no register information above,
  1117.          such a core file is useless.  */
  1118.       if (args->note_data != NULL)
  1119.         if (siginfo_data != NULL)
  1120.           args->note_data = elfcore_write_note (args->obfd,
  1121.                                                 args->note_data,
  1122.                                                 args->note_size,
  1123.                                                 "CORE", NT_SIGINFO,
  1124.                                                 siginfo_data, siginfo_size);

  1125.       do_cleanups (old_chain);
  1126.     }

  1127.   return !args->note_data;
  1128. }

  1129. /* Fill the PRPSINFO structure with information about the process being
  1130.    debugged.  Returns 1 in case of success, 0 for failures.  Please note that
  1131.    even if the structure cannot be entirely filled (e.g., GDB was unable to
  1132.    gather information about the process UID/GID), this function will still
  1133.    return 1 since some information was already recorded.  It will only return
  1134.    0 iff nothing can be gathered.  */

  1135. static int
  1136. linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
  1137. {
  1138.   /* The filename which we will use to obtain some info about the process.
  1139.      We will basically use this to store the `/proc/PID/FILENAME' file.  */
  1140.   char filename[100];
  1141.   /* The full name of the program which generated the corefile.  */
  1142.   char *fname;
  1143.   /* The basename of the executable.  */
  1144.   const char *basename;
  1145.   /* The arguments of the program.  */
  1146.   char *psargs;
  1147.   char *infargs;
  1148.   /* The contents of `/proc/PID/stat' and `/proc/PID/status' files.  */
  1149.   char *proc_stat, *proc_status;
  1150.   /* Temporary buffer.  */
  1151.   char *tmpstr;
  1152.   /* The valid states of a process, according to the Linux kernel.  */
  1153.   const char valid_states[] = "RSDTZW";
  1154.   /* The program state.  */
  1155.   const char *prog_state;
  1156.   /* The state of the process.  */
  1157.   char pr_sname;
  1158.   /* The PID of the program which generated the corefile.  */
  1159.   pid_t pid;
  1160.   /* Process flags.  */
  1161.   unsigned int pr_flag;
  1162.   /* Process nice value.  */
  1163.   long pr_nice;
  1164.   /* The number of fields read by `sscanf'.  */
  1165.   int n_fields = 0;
  1166.   /* Cleanups.  */
  1167.   struct cleanup *c;
  1168.   int i;

  1169.   gdb_assert (p != NULL);

  1170.   /* Obtaining PID and filename.  */
  1171.   pid = ptid_get_pid (inferior_ptid);
  1172.   xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
  1173.   fname = target_fileio_read_stralloc (filename);

  1174.   if (fname == NULL || *fname == '\0')
  1175.     {
  1176.       /* No program name was read, so we won't be able to retrieve more
  1177.          information about the process.  */
  1178.       xfree (fname);
  1179.       return 0;
  1180.     }

  1181.   c = make_cleanup (xfree, fname);
  1182.   memset (p, 0, sizeof (*p));

  1183.   /* Defining the PID.  */
  1184.   p->pr_pid = pid;

  1185.   /* Copying the program name.  Only the basename matters.  */
  1186.   basename = lbasename (fname);
  1187.   strncpy (p->pr_fname, basename, sizeof (p->pr_fname));
  1188.   p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';

  1189.   infargs = get_inferior_args ();

  1190.   psargs = xstrdup (fname);
  1191.   if (infargs != NULL)
  1192.     psargs = reconcat (psargs, psargs, " ", infargs, NULL);

  1193.   make_cleanup (xfree, psargs);

  1194.   strncpy (p->pr_psargs, psargs, sizeof (p->pr_psargs));
  1195.   p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';

  1196.   xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
  1197.   proc_stat = target_fileio_read_stralloc (filename);
  1198.   make_cleanup (xfree, proc_stat);

  1199.   if (proc_stat == NULL || *proc_stat == '\0')
  1200.     {
  1201.       /* Despite being unable to read more information about the
  1202.          process, we return 1 here because at least we have its
  1203.          command line, PID and arguments.  */
  1204.       do_cleanups (c);
  1205.       return 1;
  1206.     }

  1207.   /* Ok, we have the stats.  It's time to do a little parsing of the
  1208.      contents of the buffer, so that we end up reading what we want.

  1209.      The following parsing mechanism is strongly based on the
  1210.      information generated by the `fs/proc/array.c' file, present in
  1211.      the Linux kernel tree.  More details about how the information is
  1212.      displayed can be obtained by seeing the manpage of proc(5),
  1213.      specifically under the entry of `/proc/[pid]/stat'.  */

  1214.   /* Getting rid of the PID, since we already have it.  */
  1215.   while (isdigit (*proc_stat))
  1216.     ++proc_stat;

  1217.   proc_stat = skip_spaces (proc_stat);

  1218.   /* ps command also relies on no trailing fields ever contain ')'.  */
  1219.   proc_stat = strrchr (proc_stat, ')');
  1220.   if (proc_stat == NULL)
  1221.     {
  1222.       do_cleanups (c);
  1223.       return 1;
  1224.     }
  1225.   proc_stat++;

  1226.   proc_stat = skip_spaces (proc_stat);

  1227.   n_fields = sscanf (proc_stat,
  1228.                      "%c"                /* Process state.  */
  1229.                      "%d%d%d"                /* Parent PID, group ID, session ID.  */
  1230.                      "%*d%*d"                /* tty_nr, tpgid (not used).  */
  1231.                      "%u"                /* Flags.  */
  1232.                      "%*s%*s%*s%*s"        /* minflt, cminflt, majflt,
  1233.                                            cmajflt (not used).  */
  1234.                      "%*s%*s%*s%*s"        /* utime, stime, cutime,
  1235.                                            cstime (not used).  */
  1236.                      "%*s"                /* Priority (not used).  */
  1237.                      "%ld",                /* Nice.  */
  1238.                      &pr_sname,
  1239.                      &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
  1240.                      &pr_flag,
  1241.                      &pr_nice);

  1242.   if (n_fields != 6)
  1243.     {
  1244.       /* Again, we couldn't read the complementary information about
  1245.          the process state.  However, we already have minimal
  1246.          information, so we just return 1 here.  */
  1247.       do_cleanups (c);
  1248.       return 1;
  1249.     }

  1250.   /* Filling the structure fields.  */
  1251.   prog_state = strchr (valid_states, pr_sname);
  1252.   if (prog_state != NULL)
  1253.     p->pr_state = prog_state - valid_states;
  1254.   else
  1255.     {
  1256.       /* Zero means "Running".  */
  1257.       p->pr_state = 0;
  1258.     }

  1259.   p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
  1260.   p->pr_zomb = p->pr_sname == 'Z';
  1261.   p->pr_nice = pr_nice;
  1262.   p->pr_flag = pr_flag;

  1263.   /* Finally, obtaining the UID and GID.  For that, we read and parse the
  1264.      contents of the `/proc/PID/status' file.  */
  1265.   xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
  1266.   proc_status = target_fileio_read_stralloc (filename);
  1267.   make_cleanup (xfree, proc_status);

  1268.   if (proc_status == NULL || *proc_status == '\0')
  1269.     {
  1270.       /* Returning 1 since we already have a bunch of information.  */
  1271.       do_cleanups (c);
  1272.       return 1;
  1273.     }

  1274.   /* Extracting the UID.  */
  1275.   tmpstr = strstr (proc_status, "Uid:");
  1276.   if (tmpstr != NULL)
  1277.     {
  1278.       /* Advancing the pointer to the beginning of the UID.  */
  1279.       tmpstr += sizeof ("Uid:");
  1280.       while (*tmpstr != '\0' && !isdigit (*tmpstr))
  1281.         ++tmpstr;

  1282.       if (isdigit (*tmpstr))
  1283.         p->pr_uid = strtol (tmpstr, &tmpstr, 10);
  1284.     }

  1285.   /* Extracting the GID.  */
  1286.   tmpstr = strstr (proc_status, "Gid:");
  1287.   if (tmpstr != NULL)
  1288.     {
  1289.       /* Advancing the pointer to the beginning of the GID.  */
  1290.       tmpstr += sizeof ("Gid:");
  1291.       while (*tmpstr != '\0' && !isdigit (*tmpstr))
  1292.         ++tmpstr;

  1293.       if (isdigit (*tmpstr))
  1294.         p->pr_gid = strtol (tmpstr, &tmpstr, 10);
  1295.     }

  1296.   do_cleanups (c);

  1297.   return 1;
  1298. }

  1299. /* Build the note section for a corefile, and return it in a malloc
  1300.    buffer.  */

  1301. static char *
  1302. linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
  1303. {
  1304.   struct linux_corefile_thread_data thread_args;
  1305.   struct elf_internal_linux_prpsinfo prpsinfo;
  1306.   char *note_data = NULL;
  1307.   gdb_byte *auxv;
  1308.   int auxv_len;
  1309.   volatile struct gdb_exception e;

  1310.   if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
  1311.     return NULL;

  1312.   if (linux_fill_prpsinfo (&prpsinfo))
  1313.     {
  1314.       if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch))
  1315.         {
  1316.           note_data = gdbarch_elfcore_write_linux_prpsinfo (gdbarch, obfd,
  1317.                                                             note_data, note_size,
  1318.                                                             &prpsinfo);
  1319.         }
  1320.       else
  1321.         {
  1322.           if (gdbarch_ptr_bit (gdbarch) == 64)
  1323.             note_data = elfcore_write_linux_prpsinfo64 (obfd,
  1324.                                                         note_data, note_size,
  1325.                                                         &prpsinfo);
  1326.           else
  1327.             note_data = elfcore_write_linux_prpsinfo32 (obfd,
  1328.                                                         note_data, note_size,
  1329.                                                         &prpsinfo);
  1330.         }
  1331.     }

  1332.   /* Thread register information.  */
  1333.   TRY_CATCH (e, RETURN_MASK_ERROR)
  1334.     {
  1335.       update_thread_list ();
  1336.     }
  1337.   if (e.reason < 0)
  1338.     exception_print (gdb_stderr, e);
  1339.   thread_args.gdbarch = gdbarch;
  1340.   thread_args.pid = ptid_get_pid (inferior_ptid);
  1341.   thread_args.obfd = obfd;
  1342.   thread_args.note_data = note_data;
  1343.   thread_args.note_size = note_size;
  1344.   thread_args.stop_signal = find_stop_signal ();
  1345.   iterate_over_threads (linux_corefile_thread_callback, &thread_args);
  1346.   note_data = thread_args.note_data;
  1347.   if (!note_data)
  1348.     return NULL;

  1349.   /* Auxillary vector.  */
  1350.   auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
  1351.                                 NULL, &auxv);
  1352.   if (auxv_len > 0)
  1353.     {
  1354.       note_data = elfcore_write_note (obfd, note_data, note_size,
  1355.                                       "CORE", NT_AUXV, auxv, auxv_len);
  1356.       xfree (auxv);

  1357.       if (!note_data)
  1358.         return NULL;
  1359.     }

  1360.   /* SPU information.  */
  1361.   note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
  1362.   if (!note_data)
  1363.     return NULL;

  1364.   /* File mappings.  */
  1365.   note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
  1366.                                                   note_data, note_size);

  1367.   return note_data;
  1368. }

  1369. /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
  1370.    gdbarch.h.  This function is not static because it is exported to
  1371.    other -tdep files.  */

  1372. enum gdb_signal
  1373. linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
  1374. {
  1375.   switch (signal)
  1376.     {
  1377.     case 0:
  1378.       return GDB_SIGNAL_0;

  1379.     case LINUX_SIGHUP:
  1380.       return GDB_SIGNAL_HUP;

  1381.     case LINUX_SIGINT:
  1382.       return GDB_SIGNAL_INT;

  1383.     case LINUX_SIGQUIT:
  1384.       return GDB_SIGNAL_QUIT;

  1385.     case LINUX_SIGILL:
  1386.       return GDB_SIGNAL_ILL;

  1387.     case LINUX_SIGTRAP:
  1388.       return GDB_SIGNAL_TRAP;

  1389.     case LINUX_SIGABRT:
  1390.       return GDB_SIGNAL_ABRT;

  1391.     case LINUX_SIGBUS:
  1392.       return GDB_SIGNAL_BUS;

  1393.     case LINUX_SIGFPE:
  1394.       return GDB_SIGNAL_FPE;

  1395.     case LINUX_SIGKILL:
  1396.       return GDB_SIGNAL_KILL;

  1397.     case LINUX_SIGUSR1:
  1398.       return GDB_SIGNAL_USR1;

  1399.     case LINUX_SIGSEGV:
  1400.       return GDB_SIGNAL_SEGV;

  1401.     case LINUX_SIGUSR2:
  1402.       return GDB_SIGNAL_USR2;

  1403.     case LINUX_SIGPIPE:
  1404.       return GDB_SIGNAL_PIPE;

  1405.     case LINUX_SIGALRM:
  1406.       return GDB_SIGNAL_ALRM;

  1407.     case LINUX_SIGTERM:
  1408.       return GDB_SIGNAL_TERM;

  1409.     case LINUX_SIGCHLD:
  1410.       return GDB_SIGNAL_CHLD;

  1411.     case LINUX_SIGCONT:
  1412.       return GDB_SIGNAL_CONT;

  1413.     case LINUX_SIGSTOP:
  1414.       return GDB_SIGNAL_STOP;

  1415.     case LINUX_SIGTSTP:
  1416.       return GDB_SIGNAL_TSTP;

  1417.     case LINUX_SIGTTIN:
  1418.       return GDB_SIGNAL_TTIN;

  1419.     case LINUX_SIGTTOU:
  1420.       return GDB_SIGNAL_TTOU;

  1421.     case LINUX_SIGURG:
  1422.       return GDB_SIGNAL_URG;

  1423.     case LINUX_SIGXCPU:
  1424.       return GDB_SIGNAL_XCPU;

  1425.     case LINUX_SIGXFSZ:
  1426.       return GDB_SIGNAL_XFSZ;

  1427.     case LINUX_SIGVTALRM:
  1428.       return GDB_SIGNAL_VTALRM;

  1429.     case LINUX_SIGPROF:
  1430.       return GDB_SIGNAL_PROF;

  1431.     case LINUX_SIGWINCH:
  1432.       return GDB_SIGNAL_WINCH;

  1433.     /* No way to differentiate between SIGIO and SIGPOLL.
  1434.        Therefore, we just handle the first one.  */
  1435.     case LINUX_SIGIO:
  1436.       return GDB_SIGNAL_IO;

  1437.     case LINUX_SIGPWR:
  1438.       return GDB_SIGNAL_PWR;

  1439.     case LINUX_SIGSYS:
  1440.       return GDB_SIGNAL_SYS;

  1441.     /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
  1442.        therefore we have to handle them here.  */
  1443.     case LINUX_SIGRTMIN:
  1444.       return GDB_SIGNAL_REALTIME_32;

  1445.     case LINUX_SIGRTMAX:
  1446.       return GDB_SIGNAL_REALTIME_64;
  1447.     }

  1448.   if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
  1449.     {
  1450.       int offset = signal - LINUX_SIGRTMIN + 1;

  1451.       return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
  1452.     }

  1453.   return GDB_SIGNAL_UNKNOWN;
  1454. }

  1455. /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
  1456.    gdbarch.h.  This function is not static because it is exported to
  1457.    other -tdep files.  */

  1458. int
  1459. linux_gdb_signal_to_target (struct gdbarch *gdbarch,
  1460.                             enum gdb_signal signal)
  1461. {
  1462.   switch (signal)
  1463.     {
  1464.     case GDB_SIGNAL_0:
  1465.       return 0;

  1466.     case GDB_SIGNAL_HUP:
  1467.       return LINUX_SIGHUP;

  1468.     case GDB_SIGNAL_INT:
  1469.       return LINUX_SIGINT;

  1470.     case GDB_SIGNAL_QUIT:
  1471.       return LINUX_SIGQUIT;

  1472.     case GDB_SIGNAL_ILL:
  1473.       return LINUX_SIGILL;

  1474.     case GDB_SIGNAL_TRAP:
  1475.       return LINUX_SIGTRAP;

  1476.     case GDB_SIGNAL_ABRT:
  1477.       return LINUX_SIGABRT;

  1478.     case GDB_SIGNAL_FPE:
  1479.       return LINUX_SIGFPE;

  1480.     case GDB_SIGNAL_KILL:
  1481.       return LINUX_SIGKILL;

  1482.     case GDB_SIGNAL_BUS:
  1483.       return LINUX_SIGBUS;

  1484.     case GDB_SIGNAL_SEGV:
  1485.       return LINUX_SIGSEGV;

  1486.     case GDB_SIGNAL_SYS:
  1487.       return LINUX_SIGSYS;

  1488.     case GDB_SIGNAL_PIPE:
  1489.       return LINUX_SIGPIPE;

  1490.     case GDB_SIGNAL_ALRM:
  1491.       return LINUX_SIGALRM;

  1492.     case GDB_SIGNAL_TERM:
  1493.       return LINUX_SIGTERM;

  1494.     case GDB_SIGNAL_URG:
  1495.       return LINUX_SIGURG;

  1496.     case GDB_SIGNAL_STOP:
  1497.       return LINUX_SIGSTOP;

  1498.     case GDB_SIGNAL_TSTP:
  1499.       return LINUX_SIGTSTP;

  1500.     case GDB_SIGNAL_CONT:
  1501.       return LINUX_SIGCONT;

  1502.     case GDB_SIGNAL_CHLD:
  1503.       return LINUX_SIGCHLD;

  1504.     case GDB_SIGNAL_TTIN:
  1505.       return LINUX_SIGTTIN;

  1506.     case GDB_SIGNAL_TTOU:
  1507.       return LINUX_SIGTTOU;

  1508.     case GDB_SIGNAL_IO:
  1509.       return LINUX_SIGIO;

  1510.     case GDB_SIGNAL_XCPU:
  1511.       return LINUX_SIGXCPU;

  1512.     case GDB_SIGNAL_XFSZ:
  1513.       return LINUX_SIGXFSZ;

  1514.     case GDB_SIGNAL_VTALRM:
  1515.       return LINUX_SIGVTALRM;

  1516.     case GDB_SIGNAL_PROF:
  1517.       return LINUX_SIGPROF;

  1518.     case GDB_SIGNAL_WINCH:
  1519.       return LINUX_SIGWINCH;

  1520.     case GDB_SIGNAL_USR1:
  1521.       return LINUX_SIGUSR1;

  1522.     case GDB_SIGNAL_USR2:
  1523.       return LINUX_SIGUSR2;

  1524.     case GDB_SIGNAL_PWR:
  1525.       return LINUX_SIGPWR;

  1526.     case GDB_SIGNAL_POLL:
  1527.       return LINUX_SIGPOLL;

  1528.     /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
  1529.        therefore we have to handle it here.  */
  1530.     case GDB_SIGNAL_REALTIME_32:
  1531.       return LINUX_SIGRTMIN;

  1532.     /* Same comment applies to _64.  */
  1533.     case GDB_SIGNAL_REALTIME_64:
  1534.       return LINUX_SIGRTMAX;
  1535.     }

  1536.   /* GDB_SIGNAL_REALTIME_33 to _64 are continuous.  */
  1537.   if (signal >= GDB_SIGNAL_REALTIME_33
  1538.       && signal <= GDB_SIGNAL_REALTIME_63)
  1539.     {
  1540.       int offset = signal - GDB_SIGNAL_REALTIME_33;

  1541.       return LINUX_SIGRTMIN + 1 + offset;
  1542.     }

  1543.   return -1;
  1544. }

  1545. /* Rummage through mappings to find a mapping's size.  */

  1546. static int
  1547. find_mapping_size (CORE_ADDR vaddr, unsigned long size,
  1548.                    int read, int write, int exec, int modified,
  1549.                    void *data)
  1550. {
  1551.   struct mem_range *range = data;

  1552.   if (vaddr == range->start)
  1553.     {
  1554.       range->length = size;
  1555.       return 1;
  1556.     }
  1557.   return 0;
  1558. }

  1559. /* Helper for linux_vsyscall_range that does the real work of finding
  1560.    the vsyscall's address range.  */

  1561. static int
  1562. linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
  1563. {
  1564.   if (target_auxv_search (&current_target, AT_SYSINFO_EHDR, &range->start) <= 0)
  1565.     return 0;

  1566.   /* This is installed by linux_init_abi below, so should always be
  1567.      available.  */
  1568.   gdb_assert (gdbarch_find_memory_regions_p (target_gdbarch ()));

  1569.   range->length = 0;
  1570.   gdbarch_find_memory_regions (gdbarch, find_mapping_size, range);
  1571.   return 1;
  1572. }

  1573. /* Implementation of the "vsyscall_range" gdbarch hook.  Handles
  1574.    caching, and defers the real work to linux_vsyscall_range_raw.  */

  1575. static int
  1576. linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
  1577. {
  1578.   struct linux_info *info = get_linux_inferior_data ();

  1579.   if (info->vsyscall_range_p == 0)
  1580.     {
  1581.       if (linux_vsyscall_range_raw (gdbarch, &info->vsyscall_range))
  1582.         info->vsyscall_range_p = 1;
  1583.       else
  1584.         info->vsyscall_range_p = -1;
  1585.     }

  1586.   if (info->vsyscall_range_p < 0)
  1587.     return 0;

  1588.   *range = info->vsyscall_range;
  1589.   return 1;
  1590. }

  1591. /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
  1592.    definitions would be dependent on compilation host.  */
  1593. #define GDB_MMAP_MAP_PRIVATE        0x02                /* Changes are private.  */
  1594. #define GDB_MMAP_MAP_ANONYMOUS        0x20                /* Don't use a file.  */

  1595. /* See gdbarch.sh 'infcall_mmap'.  */

  1596. static CORE_ADDR
  1597. linux_infcall_mmap (CORE_ADDR size, unsigned prot)
  1598. {
  1599.   struct objfile *objf;
  1600.   /* Do there still exist any Linux systems without "mmap64"?
  1601.      "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32.  */
  1602.   struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
  1603.   struct value *addr_val;
  1604.   struct gdbarch *gdbarch = get_objfile_arch (objf);
  1605.   CORE_ADDR retval;
  1606.   enum
  1607.     {
  1608.       ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_LAST
  1609.     };
  1610.   struct value *arg[ARG_LAST];

  1611.   arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
  1612.                                       0);
  1613.   /* Assuming sizeof (unsigned long) == sizeof (size_t).  */
  1614.   arg[ARG_LENGTH] = value_from_ulongest
  1615.                     (builtin_type (gdbarch)->builtin_unsigned_long, size);
  1616.   gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
  1617.                         | GDB_MMAP_PROT_EXEC))
  1618.               == 0);
  1619.   arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int, prot);
  1620.   arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
  1621.                                        GDB_MMAP_MAP_PRIVATE
  1622.                                        | GDB_MMAP_MAP_ANONYMOUS);
  1623.   arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1);
  1624.   arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64,
  1625.                                         0);
  1626.   addr_val = call_function_by_hand (mmap_val, ARG_LAST, arg);
  1627.   retval = value_as_address (addr_val);
  1628.   if (retval == (CORE_ADDR) -1)
  1629.     error (_("Failed inferior mmap call for %s bytes, errno is changed."),
  1630.            pulongest (size));
  1631.   return retval;
  1632. }

  1633. /* To be called from the various GDB_OSABI_LINUX handlers for the
  1634.    various GNU/Linux architectures and machine types.  */

  1635. void
  1636. linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  1637. {
  1638.   set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
  1639.   set_gdbarch_info_proc (gdbarch, linux_info_proc);
  1640.   set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
  1641.   set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
  1642.   set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes);
  1643.   set_gdbarch_has_shared_address_space (gdbarch,
  1644.                                         linux_has_shared_address_space);
  1645.   set_gdbarch_gdb_signal_from_target (gdbarch,
  1646.                                       linux_gdb_signal_from_target);
  1647.   set_gdbarch_gdb_signal_to_target (gdbarch,
  1648.                                     linux_gdb_signal_to_target);
  1649.   set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range);
  1650.   set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap);
  1651. }

  1652. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1653. extern initialize_file_ftype _initialize_linux_tdep;

  1654. void
  1655. _initialize_linux_tdep (void)
  1656. {
  1657.   linux_gdbarch_data_handle =
  1658.     gdbarch_data_register_post_init (init_linux_gdbarch_data);

  1659.   /* Set a cache per-inferior.  */
  1660.   linux_inferior_data
  1661.     = register_inferior_data_with_cleanup (NULL, linux_inferior_data_cleanup);
  1662.   /* Observers used to invalidate the cache when needed.  */
  1663.   observer_attach_inferior_exit (invalidate_linux_cache_inf);
  1664.   observer_attach_inferior_appeared (invalidate_linux_cache_inf);
  1665. }