GDB (API)
/home/stan/gdb/src/gdb/linux-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for GNU/Linux, architecture independent.
00002 
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdbtypes.h"
00022 #include "linux-tdep.h"
00023 #include "auxv.h"
00024 #include "target.h"
00025 #include "gdbthread.h"
00026 #include "gdbcore.h"
00027 #include "regcache.h"
00028 #include "regset.h"
00029 #include "elf/common.h"
00030 #include "elf-bfd.h"            /* for elfcore_write_* */
00031 #include "inferior.h"
00032 #include "cli/cli-utils.h"
00033 #include "arch-utils.h"
00034 #include "gdb_obstack.h"
00035 #include "cli/cli-utils.h"
00036 
00037 #include <ctype.h>
00038 
00039 /* This enum represents the signals' numbers on a generic architecture
00040    running the Linux kernel.  The definition of "generic" comes from
00041    the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
00042    tree, which is the "de facto" implementation of signal numbers to
00043    be used by new architecture ports.
00044 
00045    For those architectures which have differences between the generic
00046    standard (e.g., Alpha), we define the different signals (and *only*
00047    those) in the specific target-dependent file (e.g.,
00048    alpha-linux-tdep.c, for Alpha).  Please refer to the architecture's
00049    tdep file for more information.
00050 
00051    ARM deserves a special mention here.  On the file
00052    <arch/arm/include/uapi/asm/signal.h>, it defines only one different
00053    (and ARM-only) signal, which is SIGSWI, with the same number as
00054    SIGRTMIN.  This signal is used only for a very specific target,
00055    called ArthurOS (from RISCOS).  Therefore, we do not handle it on
00056    the ARM-tdep file, and we can safely use the generic signal handler
00057    here for ARM targets.
00058 
00059    As stated above, this enum is derived from
00060    <include/uapi/asm-generic/signal.h>, from the Linux kernel
00061    tree.  */
00062 
00063 enum
00064   {
00065     LINUX_SIGHUP = 1,
00066     LINUX_SIGINT = 2,
00067     LINUX_SIGQUIT = 3,
00068     LINUX_SIGILL = 4,
00069     LINUX_SIGTRAP = 5,
00070     LINUX_SIGABRT = 6,
00071     LINUX_SIGIOT = 6,
00072     LINUX_SIGBUS = 7,
00073     LINUX_SIGFPE = 8,
00074     LINUX_SIGKILL = 9,
00075     LINUX_SIGUSR1 = 10,
00076     LINUX_SIGSEGV = 11,
00077     LINUX_SIGUSR2 = 12,
00078     LINUX_SIGPIPE = 13,
00079     LINUX_SIGALRM = 14,
00080     LINUX_SIGTERM = 15,
00081     LINUX_SIGSTKFLT = 16,
00082     LINUX_SIGCHLD = 17,
00083     LINUX_SIGCONT = 18,
00084     LINUX_SIGSTOP = 19,
00085     LINUX_SIGTSTP = 20,
00086     LINUX_SIGTTIN = 21,
00087     LINUX_SIGTTOU = 22,
00088     LINUX_SIGURG = 23,
00089     LINUX_SIGXCPU = 24,
00090     LINUX_SIGXFSZ = 25,
00091     LINUX_SIGVTALRM = 26,
00092     LINUX_SIGPROF = 27,
00093     LINUX_SIGWINCH = 28,
00094     LINUX_SIGIO = 29,
00095     LINUX_SIGPOLL = LINUX_SIGIO,
00096     LINUX_SIGPWR = 30,
00097     LINUX_SIGSYS = 31,
00098     LINUX_SIGUNUSED = 31,
00099 
00100     LINUX_SIGRTMIN = 32,
00101     LINUX_SIGRTMAX = 64,
00102   };
00103 
00104 static struct gdbarch_data *linux_gdbarch_data_handle;
00105 
00106 struct linux_gdbarch_data
00107   {
00108     struct type *siginfo_type;
00109   };
00110 
00111 static void *
00112 init_linux_gdbarch_data (struct gdbarch *gdbarch)
00113 {
00114   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
00115 }
00116 
00117 static struct linux_gdbarch_data *
00118 get_linux_gdbarch_data (struct gdbarch *gdbarch)
00119 {
00120   return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
00121 }
00122 
00123 /* This function is suitable for architectures that don't
00124    extend/override the standard siginfo structure.  */
00125 
00126 struct type *
00127 linux_get_siginfo_type (struct gdbarch *gdbarch)
00128 {
00129   struct linux_gdbarch_data *linux_gdbarch_data;
00130   struct type *int_type, *uint_type, *long_type, *void_ptr_type;
00131   struct type *uid_type, *pid_type;
00132   struct type *sigval_type, *clock_type;
00133   struct type *siginfo_type, *sifields_type;
00134   struct type *type;
00135 
00136   linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
00137   if (linux_gdbarch_data->siginfo_type != NULL)
00138     return linux_gdbarch_data->siginfo_type;
00139 
00140   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
00141                                 0, "int");
00142   uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
00143                                  1, "unsigned int");
00144   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
00145                                  0, "long");
00146   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
00147 
00148   /* sival_t */
00149   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
00150   TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
00151   append_composite_type_field (sigval_type, "sival_int", int_type);
00152   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
00153 
00154   /* __pid_t */
00155   pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
00156                         TYPE_LENGTH (int_type), "__pid_t");
00157   TYPE_TARGET_TYPE (pid_type) = int_type;
00158   TYPE_TARGET_STUB (pid_type) = 1;
00159 
00160   /* __uid_t */
00161   uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
00162                         TYPE_LENGTH (uint_type), "__uid_t");
00163   TYPE_TARGET_TYPE (uid_type) = uint_type;
00164   TYPE_TARGET_STUB (uid_type) = 1;
00165 
00166   /* __clock_t */
00167   clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
00168                           TYPE_LENGTH (long_type), "__clock_t");
00169   TYPE_TARGET_TYPE (clock_type) = long_type;
00170   TYPE_TARGET_STUB (clock_type) = 1;
00171 
00172   /* _sifields */
00173   sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
00174 
00175   {
00176     const int si_max_size = 128;
00177     int si_pad_size;
00178     int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
00179 
00180     /* _pad */
00181     if (gdbarch_ptr_bit (gdbarch) == 64)
00182       si_pad_size = (si_max_size / size_of_int) - 4;
00183     else
00184       si_pad_size = (si_max_size / size_of_int) - 3;
00185     append_composite_type_field (sifields_type, "_pad",
00186                                  init_vector_type (int_type, si_pad_size));
00187   }
00188 
00189   /* _kill */
00190   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00191   append_composite_type_field (type, "si_pid", pid_type);
00192   append_composite_type_field (type, "si_uid", uid_type);
00193   append_composite_type_field (sifields_type, "_kill", type);
00194 
00195   /* _timer */
00196   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00197   append_composite_type_field (type, "si_tid", int_type);
00198   append_composite_type_field (type, "si_overrun", int_type);
00199   append_composite_type_field (type, "si_sigval", sigval_type);
00200   append_composite_type_field (sifields_type, "_timer", type);
00201 
00202   /* _rt */
00203   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00204   append_composite_type_field (type, "si_pid", pid_type);
00205   append_composite_type_field (type, "si_uid", uid_type);
00206   append_composite_type_field (type, "si_sigval", sigval_type);
00207   append_composite_type_field (sifields_type, "_rt", type);
00208 
00209   /* _sigchld */
00210   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00211   append_composite_type_field (type, "si_pid", pid_type);
00212   append_composite_type_field (type, "si_uid", uid_type);
00213   append_composite_type_field (type, "si_status", int_type);
00214   append_composite_type_field (type, "si_utime", clock_type);
00215   append_composite_type_field (type, "si_stime", clock_type);
00216   append_composite_type_field (sifields_type, "_sigchld", type);
00217 
00218   /* _sigfault */
00219   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00220   append_composite_type_field (type, "si_addr", void_ptr_type);
00221   append_composite_type_field (sifields_type, "_sigfault", type);
00222 
00223   /* _sigpoll */
00224   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00225   append_composite_type_field (type, "si_band", long_type);
00226   append_composite_type_field (type, "si_fd", int_type);
00227   append_composite_type_field (sifields_type, "_sigpoll", type);
00228 
00229   /* struct siginfo */
00230   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00231   TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
00232   append_composite_type_field (siginfo_type, "si_signo", int_type);
00233   append_composite_type_field (siginfo_type, "si_errno", int_type);
00234   append_composite_type_field (siginfo_type, "si_code", int_type);
00235   append_composite_type_field_aligned (siginfo_type,
00236                                        "_sifields", sifields_type,
00237                                        TYPE_LENGTH (long_type));
00238 
00239   linux_gdbarch_data->siginfo_type = siginfo_type;
00240 
00241   return siginfo_type;
00242 }
00243 
00244 /* Return true if the target is running on uClinux instead of normal
00245    Linux kernel.  */
00246 
00247 int
00248 linux_is_uclinux (void)
00249 {
00250   CORE_ADDR dummy;
00251 
00252   return (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
00253           && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
00254 }
00255 
00256 static int
00257 linux_has_shared_address_space (struct gdbarch *gdbarch)
00258 {
00259   return linux_is_uclinux ();
00260 }
00261 
00262 /* This is how we want PTIDs from core files to be printed.  */
00263 
00264 static char *
00265 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
00266 {
00267   static char buf[80];
00268 
00269   if (ptid_get_lwp (ptid) != 0)
00270     {
00271       snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
00272       return buf;
00273     }
00274 
00275   return normal_pid_to_str (ptid);
00276 }
00277 
00278 /* Service function for corefiles and info proc.  */
00279 
00280 static void
00281 read_mapping (const char *line,
00282               ULONGEST *addr, ULONGEST *endaddr,
00283               const char **permissions, size_t *permissions_len,
00284               ULONGEST *offset,
00285               const char **device, size_t *device_len,
00286               ULONGEST *inode,
00287               const char **filename)
00288 {
00289   const char *p = line;
00290 
00291   *addr = strtoulst (p, &p, 16);
00292   if (*p == '-')
00293     p++;
00294   *endaddr = strtoulst (p, &p, 16);
00295 
00296   p = skip_spaces_const (p);
00297   *permissions = p;
00298   while (*p && !isspace (*p))
00299     p++;
00300   *permissions_len = p - *permissions;
00301 
00302   *offset = strtoulst (p, &p, 16);
00303 
00304   p = skip_spaces_const (p);
00305   *device = p;
00306   while (*p && !isspace (*p))
00307     p++;
00308   *device_len = p - *device;
00309 
00310   *inode = strtoulst (p, &p, 10);
00311 
00312   p = skip_spaces_const (p);
00313   *filename = p;
00314 }
00315 
00316 /* Implement the "info proc" command.  */
00317 
00318 static void
00319 linux_info_proc (struct gdbarch *gdbarch, char *args,
00320                  enum info_proc_what what)
00321 {
00322   /* A long is used for pid instead of an int to avoid a loss of precision
00323      compiler warning from the output of strtoul.  */
00324   long pid;
00325   int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
00326   int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
00327   int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
00328   int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
00329   int status_f = (what == IP_STATUS || what == IP_ALL);
00330   int stat_f = (what == IP_STAT || what == IP_ALL);
00331   char filename[100];
00332   char *data;
00333   int target_errno;
00334 
00335   if (args && isdigit (args[0]))
00336     pid = strtoul (args, &args, 10);
00337   else
00338     {
00339       if (!target_has_execution)
00340         error (_("No current process: you must name one."));
00341       if (current_inferior ()->fake_pid_p)
00342         error (_("Can't determine the current process's PID: you must name one."));
00343 
00344       pid = current_inferior ()->pid;
00345     }
00346 
00347   args = skip_spaces (args);
00348   if (args && args[0])
00349     error (_("Too many parameters: %s"), args);
00350 
00351   printf_filtered (_("process %ld\n"), pid);
00352   if (cmdline_f)
00353     {
00354       xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
00355       data = target_fileio_read_stralloc (filename);
00356       if (data)
00357         {
00358           struct cleanup *cleanup = make_cleanup (xfree, data);
00359           printf_filtered ("cmdline = '%s'\n", data);
00360           do_cleanups (cleanup);
00361         }
00362       else
00363         warning (_("unable to open /proc file '%s'"), filename);
00364     }
00365   if (cwd_f)
00366     {
00367       xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
00368       data = target_fileio_readlink (filename, &target_errno);
00369       if (data)
00370         {
00371           struct cleanup *cleanup = make_cleanup (xfree, data);
00372           printf_filtered ("cwd = '%s'\n", data);
00373           do_cleanups (cleanup);
00374         }
00375       else
00376         warning (_("unable to read link '%s'"), filename);
00377     }
00378   if (exe_f)
00379     {
00380       xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
00381       data = target_fileio_readlink (filename, &target_errno);
00382       if (data)
00383         {
00384           struct cleanup *cleanup = make_cleanup (xfree, data);
00385           printf_filtered ("exe = '%s'\n", data);
00386           do_cleanups (cleanup);
00387         }
00388       else
00389         warning (_("unable to read link '%s'"), filename);
00390     }
00391   if (mappings_f)
00392     {
00393       xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
00394       data = target_fileio_read_stralloc (filename);
00395       if (data)
00396         {
00397           struct cleanup *cleanup = make_cleanup (xfree, data);
00398           char *line;
00399 
00400           printf_filtered (_("Mapped address spaces:\n\n"));
00401           if (gdbarch_addr_bit (gdbarch) == 32)
00402             {
00403               printf_filtered ("\t%10s %10s %10s %10s %s\n",
00404                            "Start Addr",
00405                            "  End Addr",
00406                            "      Size", "    Offset", "objfile");
00407             }
00408           else
00409             {
00410               printf_filtered ("  %18s %18s %10s %10s %s\n",
00411                            "Start Addr",
00412                            "  End Addr",
00413                            "      Size", "    Offset", "objfile");
00414             }
00415 
00416           for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
00417             {
00418               ULONGEST addr, endaddr, offset, inode;
00419               const char *permissions, *device, *filename;
00420               size_t permissions_len, device_len;
00421 
00422               read_mapping (line, &addr, &endaddr,
00423                             &permissions, &permissions_len,
00424                             &offset, &device, &device_len,
00425                             &inode, &filename);
00426 
00427               if (gdbarch_addr_bit (gdbarch) == 32)
00428                 {
00429                   printf_filtered ("\t%10s %10s %10s %10s %s\n",
00430                                    paddress (gdbarch, addr),
00431                                    paddress (gdbarch, endaddr),
00432                                    hex_string (endaddr - addr),
00433                                    hex_string (offset),
00434                                    *filename? filename : "");
00435                 }
00436               else
00437                 {
00438                   printf_filtered ("  %18s %18s %10s %10s %s\n",
00439                                    paddress (gdbarch, addr),
00440                                    paddress (gdbarch, endaddr),
00441                                    hex_string (endaddr - addr),
00442                                    hex_string (offset),
00443                                    *filename? filename : "");
00444                 }
00445             }
00446 
00447           do_cleanups (cleanup);
00448         }
00449       else
00450         warning (_("unable to open /proc file '%s'"), filename);
00451     }
00452   if (status_f)
00453     {
00454       xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
00455       data = target_fileio_read_stralloc (filename);
00456       if (data)
00457         {
00458           struct cleanup *cleanup = make_cleanup (xfree, data);
00459           puts_filtered (data);
00460           do_cleanups (cleanup);
00461         }
00462       else
00463         warning (_("unable to open /proc file '%s'"), filename);
00464     }
00465   if (stat_f)
00466     {
00467       xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
00468       data = target_fileio_read_stralloc (filename);
00469       if (data)
00470         {
00471           struct cleanup *cleanup = make_cleanup (xfree, data);
00472           const char *p = data;
00473 
00474           printf_filtered (_("Process: %s\n"),
00475                            pulongest (strtoulst (p, &p, 10)));
00476 
00477           p = skip_spaces_const (p);
00478           if (*p == '(')
00479             {
00480               const char *ep = strchr (p, ')');
00481               if (ep != NULL)
00482                 {
00483                   printf_filtered ("Exec file: %.*s\n",
00484                                    (int) (ep - p - 1), p + 1);
00485                   p = ep + 1;
00486                 }
00487             }
00488 
00489           p = skip_spaces_const (p);
00490           if (*p)
00491             printf_filtered (_("State: %c\n"), *p++);
00492 
00493           if (*p)
00494             printf_filtered (_("Parent process: %s\n"),
00495                              pulongest (strtoulst (p, &p, 10)));
00496           if (*p)
00497             printf_filtered (_("Process group: %s\n"),
00498                              pulongest (strtoulst (p, &p, 10)));
00499           if (*p)
00500             printf_filtered (_("Session id: %s\n"),
00501                              pulongest (strtoulst (p, &p, 10)));
00502           if (*p)
00503             printf_filtered (_("TTY: %s\n"),
00504                              pulongest (strtoulst (p, &p, 10)));
00505           if (*p)
00506             printf_filtered (_("TTY owner process group: %s\n"),
00507                              pulongest (strtoulst (p, &p, 10)));
00508 
00509           if (*p)
00510             printf_filtered (_("Flags: %s\n"),
00511                              hex_string (strtoulst (p, &p, 10)));
00512           if (*p)
00513             printf_filtered (_("Minor faults (no memory page): %s\n"),
00514                              pulongest (strtoulst (p, &p, 10)));
00515           if (*p)
00516             printf_filtered (_("Minor faults, children: %s\n"),
00517                              pulongest (strtoulst (p, &p, 10)));
00518           if (*p)
00519             printf_filtered (_("Major faults (memory page faults): %s\n"),
00520                              pulongest (strtoulst (p, &p, 10)));
00521           if (*p)
00522             printf_filtered (_("Major faults, children: %s\n"),
00523                              pulongest (strtoulst (p, &p, 10)));
00524           if (*p)
00525             printf_filtered (_("utime: %s\n"),
00526                              pulongest (strtoulst (p, &p, 10)));
00527           if (*p)
00528             printf_filtered (_("stime: %s\n"),
00529                              pulongest (strtoulst (p, &p, 10)));
00530           if (*p)
00531             printf_filtered (_("utime, children: %s\n"),
00532                              pulongest (strtoulst (p, &p, 10)));
00533           if (*p)
00534             printf_filtered (_("stime, children: %s\n"),
00535                              pulongest (strtoulst (p, &p, 10)));
00536           if (*p)
00537             printf_filtered (_("jiffies remaining in current "
00538                                "time slice: %s\n"),
00539                              pulongest (strtoulst (p, &p, 10)));
00540           if (*p)
00541             printf_filtered (_("'nice' value: %s\n"),
00542                              pulongest (strtoulst (p, &p, 10)));
00543           if (*p)
00544             printf_filtered (_("jiffies until next timeout: %s\n"),
00545                              pulongest (strtoulst (p, &p, 10)));
00546           if (*p)
00547             printf_filtered (_("jiffies until next SIGALRM: %s\n"),
00548                              pulongest (strtoulst (p, &p, 10)));
00549           if (*p)
00550             printf_filtered (_("start time (jiffies since "
00551                                "system boot): %s\n"),
00552                              pulongest (strtoulst (p, &p, 10)));
00553           if (*p)
00554             printf_filtered (_("Virtual memory size: %s\n"),
00555                              pulongest (strtoulst (p, &p, 10)));
00556           if (*p)
00557             printf_filtered (_("Resident set size: %s\n"),
00558                              pulongest (strtoulst (p, &p, 10)));
00559           if (*p)
00560             printf_filtered (_("rlim: %s\n"),
00561                              pulongest (strtoulst (p, &p, 10)));
00562           if (*p)
00563             printf_filtered (_("Start of text: %s\n"),
00564                              hex_string (strtoulst (p, &p, 10)));
00565           if (*p)
00566             printf_filtered (_("End of text: %s\n"),
00567                              hex_string (strtoulst (p, &p, 10)));
00568           if (*p)
00569             printf_filtered (_("Start of stack: %s\n"),
00570                              hex_string (strtoulst (p, &p, 10)));
00571 #if 0   /* Don't know how architecture-dependent the rest is...
00572            Anyway the signal bitmap info is available from "status".  */
00573           if (*p)
00574             printf_filtered (_("Kernel stack pointer: %s\n"),
00575                              hex_string (strtoulst (p, &p, 10)));
00576           if (*p)
00577             printf_filtered (_("Kernel instr pointer: %s\n"),
00578                              hex_string (strtoulst (p, &p, 10)));
00579           if (*p)
00580             printf_filtered (_("Pending signals bitmap: %s\n"),
00581                              hex_string (strtoulst (p, &p, 10)));
00582           if (*p)
00583             printf_filtered (_("Blocked signals bitmap: %s\n"),
00584                              hex_string (strtoulst (p, &p, 10)));
00585           if (*p)
00586             printf_filtered (_("Ignored signals bitmap: %s\n"),
00587                              hex_string (strtoulst (p, &p, 10)));
00588           if (*p)
00589             printf_filtered (_("Catched signals bitmap: %s\n"),
00590                              hex_string (strtoulst (p, &p, 10)));
00591           if (*p)
00592             printf_filtered (_("wchan (system call): %s\n"),
00593                              hex_string (strtoulst (p, &p, 10)));
00594 #endif
00595           do_cleanups (cleanup);
00596         }
00597       else
00598         warning (_("unable to open /proc file '%s'"), filename);
00599     }
00600 }
00601 
00602 /* Implement "info proc mappings" for a corefile.  */
00603 
00604 static void
00605 linux_core_info_proc_mappings (struct gdbarch *gdbarch, char *args)
00606 {
00607   asection *section;
00608   ULONGEST count, page_size;
00609   unsigned char *descdata, *filenames, *descend, *contents;
00610   size_t note_size;
00611   unsigned int addr_size_bits, addr_size;
00612   struct cleanup *cleanup;
00613   struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
00614   /* We assume this for reading 64-bit core files.  */
00615   gdb_static_assert (sizeof (ULONGEST) >= 8);
00616 
00617   section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
00618   if (section == NULL)
00619     {
00620       warning (_("unable to find mappings in core file"));
00621       return;
00622     }
00623 
00624   addr_size_bits = gdbarch_addr_bit (core_gdbarch);
00625   addr_size = addr_size_bits / 8;
00626   note_size = bfd_get_section_size (section);
00627 
00628   if (note_size < 2 * addr_size)
00629     error (_("malformed core note - too short for header"));
00630 
00631   contents = xmalloc (note_size);
00632   cleanup = make_cleanup (xfree, contents);
00633   if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
00634     error (_("could not get core note contents"));
00635 
00636   descdata = contents;
00637   descend = descdata + note_size;
00638 
00639   if (descdata[note_size - 1] != '\0')
00640     error (_("malformed note - does not end with \\0"));
00641 
00642   count = bfd_get (addr_size_bits, core_bfd, descdata);
00643   descdata += addr_size;
00644 
00645   page_size = bfd_get (addr_size_bits, core_bfd, descdata);
00646   descdata += addr_size;
00647 
00648   if (note_size < 2 * addr_size + count * 3 * addr_size)
00649     error (_("malformed note - too short for supplied file count"));
00650 
00651   printf_filtered (_("Mapped address spaces:\n\n"));
00652   if (gdbarch_addr_bit (gdbarch) == 32)
00653     {
00654       printf_filtered ("\t%10s %10s %10s %10s %s\n",
00655                        "Start Addr",
00656                        "  End Addr",
00657                        "      Size", "    Offset", "objfile");
00658     }
00659   else
00660     {
00661       printf_filtered ("  %18s %18s %10s %10s %s\n",
00662                        "Start Addr",
00663                        "  End Addr",
00664                        "      Size", "    Offset", "objfile");
00665     }
00666 
00667   filenames = descdata + count * 3 * addr_size;
00668   while (--count > 0)
00669     {
00670       ULONGEST start, end, file_ofs;
00671 
00672       if (filenames == descend)
00673         error (_("malformed note - filenames end too early"));
00674 
00675       start = bfd_get (addr_size_bits, core_bfd, descdata);
00676       descdata += addr_size;
00677       end = bfd_get (addr_size_bits, core_bfd, descdata);
00678       descdata += addr_size;
00679       file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
00680       descdata += addr_size;
00681 
00682       file_ofs *= page_size;
00683 
00684       if (gdbarch_addr_bit (gdbarch) == 32)
00685         printf_filtered ("\t%10s %10s %10s %10s %s\n",
00686                          paddress (gdbarch, start),
00687                          paddress (gdbarch, end),
00688                          hex_string (end - start),
00689                          hex_string (file_ofs),
00690                          filenames);
00691       else
00692         printf_filtered ("  %18s %18s %10s %10s %s\n",
00693                          paddress (gdbarch, start),
00694                          paddress (gdbarch, end),
00695                          hex_string (end - start),
00696                          hex_string (file_ofs),
00697                          filenames);
00698 
00699       filenames += 1 + strlen ((char *) filenames);
00700     }
00701 
00702   do_cleanups (cleanup);
00703 }
00704 
00705 /* Implement "info proc" for a corefile.  */
00706 
00707 static void
00708 linux_core_info_proc (struct gdbarch *gdbarch, char *args,
00709                       enum info_proc_what what)
00710 {
00711   int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
00712   int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
00713 
00714   if (exe_f)
00715     {
00716       const char *exe;
00717 
00718       exe = bfd_core_file_failing_command (core_bfd);
00719       if (exe != NULL)
00720         printf_filtered ("exe = '%s'\n", exe);
00721       else
00722         warning (_("unable to find command name in core file"));
00723     }
00724 
00725   if (mappings_f)
00726     linux_core_info_proc_mappings (gdbarch, args);
00727 
00728   if (!exe_f && !mappings_f)
00729     error (_("unable to handle request"));
00730 }
00731 
00732 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
00733                                             ULONGEST offset, ULONGEST inode,
00734                                             int read, int write,
00735                                             int exec, int modified,
00736                                             const char *filename,
00737                                             void *data);
00738 
00739 /* List memory regions in the inferior for a corefile.  */
00740 
00741 static int
00742 linux_find_memory_regions_full (struct gdbarch *gdbarch,
00743                                 linux_find_memory_region_ftype *func,
00744                                 void *obfd)
00745 {
00746   char mapsfilename[100];
00747   char *data;
00748 
00749   /* We need to know the real target PID to access /proc.  */
00750   if (current_inferior ()->fake_pid_p)
00751     return 1;
00752 
00753   xsnprintf (mapsfilename, sizeof mapsfilename,
00754              "/proc/%d/smaps", current_inferior ()->pid);
00755   data = target_fileio_read_stralloc (mapsfilename);
00756   if (data == NULL)
00757     {
00758       /* Older Linux kernels did not support /proc/PID/smaps.  */
00759       xsnprintf (mapsfilename, sizeof mapsfilename,
00760                  "/proc/%d/maps", current_inferior ()->pid);
00761       data = target_fileio_read_stralloc (mapsfilename);
00762     }
00763   if (data)
00764     {
00765       struct cleanup *cleanup = make_cleanup (xfree, data);
00766       char *line;
00767 
00768       line = strtok (data, "\n");
00769       while (line)
00770         {
00771           ULONGEST addr, endaddr, offset, inode;
00772           const char *permissions, *device, *filename;
00773           size_t permissions_len, device_len;
00774           int read, write, exec;
00775           int modified = 0, has_anonymous = 0;
00776 
00777           read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
00778                         &offset, &device, &device_len, &inode, &filename);
00779 
00780           /* Decode permissions.  */
00781           read = (memchr (permissions, 'r', permissions_len) != 0);
00782           write = (memchr (permissions, 'w', permissions_len) != 0);
00783           exec = (memchr (permissions, 'x', permissions_len) != 0);
00784 
00785           /* Try to detect if region was modified by parsing smaps counters.  */
00786           for (line = strtok (NULL, "\n");
00787                line && line[0] >= 'A' && line[0] <= 'Z';
00788                line = strtok (NULL, "\n"))
00789             {
00790               char keyword[64 + 1];
00791 
00792               if (sscanf (line, "%64s", keyword) != 1)
00793                 {
00794                   warning (_("Error parsing {s,}maps file '%s'"), mapsfilename);
00795                   break;
00796                 }
00797               if (strcmp (keyword, "Anonymous:") == 0)
00798                 has_anonymous = 1;
00799               if (strcmp (keyword, "Shared_Dirty:") == 0
00800                   || strcmp (keyword, "Private_Dirty:") == 0
00801                   || strcmp (keyword, "Swap:") == 0
00802                   || strcmp (keyword, "Anonymous:") == 0)
00803                 {
00804                   unsigned long number;
00805 
00806                   if (sscanf (line, "%*s%lu", &number) != 1)
00807                     {
00808                       warning (_("Error parsing {s,}maps file '%s' number"),
00809                                mapsfilename);
00810                       break;
00811                     }
00812                   if (number != 0)
00813                     modified = 1;
00814                 }
00815             }
00816 
00817           /* Older Linux kernels did not support the "Anonymous:" counter.
00818              If it is missing, we can't be sure - dump all the pages.  */
00819           if (!has_anonymous)
00820             modified = 1;
00821 
00822           /* Invoke the callback function to create the corefile segment.  */
00823           func (addr, endaddr - addr, offset, inode,
00824                 read, write, exec, modified, filename, obfd);
00825         }
00826 
00827       do_cleanups (cleanup);
00828       return 0;
00829     }
00830 
00831   return 1;
00832 }
00833 
00834 /* A structure for passing information through
00835    linux_find_memory_regions_full.  */
00836 
00837 struct linux_find_memory_regions_data
00838 {
00839   /* The original callback.  */
00840 
00841   find_memory_region_ftype func;
00842 
00843   /* The original datum.  */
00844 
00845   void *obfd;
00846 };
00847 
00848 /* A callback for linux_find_memory_regions that converts between the
00849    "full"-style callback and find_memory_region_ftype.  */
00850 
00851 static int
00852 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
00853                                  ULONGEST offset, ULONGEST inode,
00854                                  int read, int write, int exec, int modified,
00855                                  const char *filename, void *arg)
00856 {
00857   struct linux_find_memory_regions_data *data = arg;
00858 
00859   return data->func (vaddr, size, read, write, exec, modified, data->obfd);
00860 }
00861 
00862 /* A variant of linux_find_memory_regions_full that is suitable as the
00863    gdbarch find_memory_regions method.  */
00864 
00865 static int
00866 linux_find_memory_regions (struct gdbarch *gdbarch,
00867                            find_memory_region_ftype func, void *obfd)
00868 {
00869   struct linux_find_memory_regions_data data;
00870 
00871   data.func = func;
00872   data.obfd = obfd;
00873 
00874   return linux_find_memory_regions_full (gdbarch,
00875                                          linux_find_memory_regions_thunk,
00876                                          &data);
00877 }
00878 
00879 /* Determine which signal stopped execution.  */
00880 
00881 static int
00882 find_signalled_thread (struct thread_info *info, void *data)
00883 {
00884   if (info->suspend.stop_signal != GDB_SIGNAL_0
00885       && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
00886     return 1;
00887 
00888   return 0;
00889 }
00890 
00891 static enum gdb_signal
00892 find_stop_signal (void)
00893 {
00894   struct thread_info *info =
00895     iterate_over_threads (find_signalled_thread, NULL);
00896 
00897   if (info)
00898     return info->suspend.stop_signal;
00899   else
00900     return GDB_SIGNAL_0;
00901 }
00902 
00903 /* Generate corefile notes for SPU contexts.  */
00904 
00905 static char *
00906 linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
00907 {
00908   static const char *spu_files[] =
00909     {
00910       "object-id",
00911       "mem",
00912       "regs",
00913       "fpcr",
00914       "lslr",
00915       "decr",
00916       "decr_status",
00917       "signal1",
00918       "signal1_type",
00919       "signal2",
00920       "signal2_type",
00921       "event_mask",
00922       "event_status",
00923       "mbox_info",
00924       "ibox_info",
00925       "wbox_info",
00926       "dma_info",
00927       "proxydma_info",
00928    };
00929 
00930   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00931   gdb_byte *spu_ids;
00932   LONGEST i, j, size;
00933 
00934   /* Determine list of SPU ids.  */
00935   size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
00936                             NULL, &spu_ids);
00937 
00938   /* Generate corefile notes for each SPU file.  */
00939   for (i = 0; i < size; i += 4)
00940     {
00941       int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
00942 
00943       for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
00944         {
00945           char annex[32], note_name[32];
00946           gdb_byte *spu_data;
00947           LONGEST spu_len;
00948 
00949           xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
00950           spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
00951                                        annex, &spu_data);
00952           if (spu_len > 0)
00953             {
00954               xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
00955               note_data = elfcore_write_note (obfd, note_data, note_size,
00956                                               note_name, NT_SPU,
00957                                               spu_data, spu_len);
00958               xfree (spu_data);
00959 
00960               if (!note_data)
00961                 {
00962                   xfree (spu_ids);
00963                   return NULL;
00964                 }
00965             }
00966         }
00967     }
00968 
00969   if (size > 0)
00970     xfree (spu_ids);
00971 
00972   return note_data;
00973 }
00974 
00975 /* This is used to pass information from
00976    linux_make_mappings_corefile_notes through
00977    linux_find_memory_regions_full.  */
00978 
00979 struct linux_make_mappings_data
00980 {
00981   /* Number of files mapped.  */
00982   ULONGEST file_count;
00983 
00984   /* The obstack for the main part of the data.  */
00985   struct obstack *data_obstack;
00986 
00987   /* The filename obstack.  */
00988   struct obstack *filename_obstack;
00989 
00990   /* The architecture's "long" type.  */
00991   struct type *long_type;
00992 };
00993 
00994 static linux_find_memory_region_ftype linux_make_mappings_callback;
00995 
00996 /* A callback for linux_find_memory_regions_full that updates the
00997    mappings data for linux_make_mappings_corefile_notes.  */
00998 
00999 static int
01000 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
01001                               ULONGEST offset, ULONGEST inode,
01002                               int read, int write, int exec, int modified,
01003                               const char *filename, void *data)
01004 {
01005   struct linux_make_mappings_data *map_data = data;
01006   gdb_byte buf[sizeof (ULONGEST)];
01007 
01008   if (*filename == '\0' || inode == 0)
01009     return 0;
01010 
01011   ++map_data->file_count;
01012 
01013   pack_long (buf, map_data->long_type, vaddr);
01014   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
01015   pack_long (buf, map_data->long_type, vaddr + size);
01016   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
01017   pack_long (buf, map_data->long_type, offset);
01018   obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
01019 
01020   obstack_grow_str0 (map_data->filename_obstack, filename);
01021 
01022   return 0;
01023 }
01024 
01025 /* Write the file mapping data to the core file, if possible.  OBFD is
01026    the output BFD.  NOTE_DATA is the current note data, and NOTE_SIZE
01027    is a pointer to the note size.  Returns the new NOTE_DATA and
01028    updates NOTE_SIZE.  */
01029 
01030 static char *
01031 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
01032                                     char *note_data, int *note_size)
01033 {
01034   struct cleanup *cleanup;
01035   struct obstack data_obstack, filename_obstack;
01036   struct linux_make_mappings_data mapping_data;
01037   struct type *long_type
01038     = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
01039   gdb_byte buf[sizeof (ULONGEST)];
01040 
01041   obstack_init (&data_obstack);
01042   cleanup = make_cleanup_obstack_free (&data_obstack);
01043   obstack_init (&filename_obstack);
01044   make_cleanup_obstack_free (&filename_obstack);
01045 
01046   mapping_data.file_count = 0;
01047   mapping_data.data_obstack = &data_obstack;
01048   mapping_data.filename_obstack = &filename_obstack;
01049   mapping_data.long_type = long_type;
01050 
01051   /* Reserve space for the count.  */
01052   obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
01053   /* We always write the page size as 1 since we have no good way to
01054      determine the correct value.  */
01055   pack_long (buf, long_type, 1);
01056   obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
01057 
01058   linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
01059                                   &mapping_data);
01060 
01061   if (mapping_data.file_count != 0)
01062     {
01063       /* Write the count to the obstack.  */
01064       pack_long ((gdb_byte *) obstack_base (&data_obstack),
01065                  long_type, mapping_data.file_count);
01066 
01067       /* Copy the filenames to the data obstack.  */
01068       obstack_grow (&data_obstack, obstack_base (&filename_obstack),
01069                     obstack_object_size (&filename_obstack));
01070 
01071       note_data = elfcore_write_note (obfd, note_data, note_size,
01072                                       "CORE", NT_FILE,
01073                                       obstack_base (&data_obstack),
01074                                       obstack_object_size (&data_obstack));
01075     }
01076 
01077   do_cleanups (cleanup);
01078   return note_data;
01079 }
01080 
01081 /* Records the thread's register state for the corefile note
01082    section.  */
01083 
01084 static char *
01085 linux_collect_thread_registers (const struct regcache *regcache,
01086                                 ptid_t ptid, bfd *obfd,
01087                                 char *note_data, int *note_size,
01088                                 enum gdb_signal stop_signal)
01089 {
01090   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01091   struct core_regset_section *sect_list;
01092   unsigned long lwp;
01093 
01094   sect_list = gdbarch_core_regset_sections (gdbarch);
01095   gdb_assert (sect_list);
01096 
01097   /* For remote targets the LWP may not be available, so use the TID.  */
01098   lwp = ptid_get_lwp (ptid);
01099   if (!lwp)
01100     lwp = ptid_get_tid (ptid);
01101 
01102   while (sect_list->sect_name != NULL)
01103     {
01104       const struct regset *regset;
01105       char *buf;
01106 
01107       regset = gdbarch_regset_from_core_section (gdbarch,
01108                                                  sect_list->sect_name,
01109                                                  sect_list->size);
01110       gdb_assert (regset && regset->collect_regset);
01111 
01112       buf = xmalloc (sect_list->size);
01113       regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
01114 
01115       /* PRSTATUS still needs to be treated specially.  */
01116       if (strcmp (sect_list->sect_name, ".reg") == 0)
01117         note_data = (char *) elfcore_write_prstatus
01118                                (obfd, note_data, note_size, lwp,
01119                                 gdb_signal_to_host (stop_signal), buf);
01120       else
01121         note_data = (char *) elfcore_write_register_note
01122                                (obfd, note_data, note_size,
01123                                 sect_list->sect_name, buf, sect_list->size);
01124       xfree (buf);
01125       sect_list++;
01126 
01127       if (!note_data)
01128         return NULL;
01129     }
01130 
01131   return note_data;
01132 }
01133 
01134 /* Fetch the siginfo data for the current thread, if it exists.  If
01135    there is no data, or we could not read it, return NULL.  Otherwise,
01136    return a newly malloc'd buffer holding the data and fill in *SIZE
01137    with the size of the data.  The caller is responsible for freeing
01138    the data.  */
01139 
01140 static gdb_byte *
01141 linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
01142 {
01143   struct type *siginfo_type;
01144   gdb_byte *buf;
01145   LONGEST bytes_read;
01146   struct cleanup *cleanups;
01147 
01148   if (!gdbarch_get_siginfo_type_p (gdbarch))
01149     return NULL;
01150   
01151   siginfo_type = gdbarch_get_siginfo_type (gdbarch);
01152 
01153   buf = xmalloc (TYPE_LENGTH (siginfo_type));
01154   cleanups = make_cleanup (xfree, buf);
01155 
01156   bytes_read = target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
01157                             buf, 0, TYPE_LENGTH (siginfo_type));
01158   if (bytes_read == TYPE_LENGTH (siginfo_type))
01159     {
01160       discard_cleanups (cleanups);
01161       *size = bytes_read;
01162     }
01163   else
01164     {
01165       do_cleanups (cleanups);
01166       buf = NULL;
01167     }
01168 
01169   return buf;
01170 }
01171 
01172 struct linux_corefile_thread_data
01173 {
01174   struct gdbarch *gdbarch;
01175   int pid;
01176   bfd *obfd;
01177   char *note_data;
01178   int *note_size;
01179   int num_notes;
01180   enum gdb_signal stop_signal;
01181   linux_collect_thread_registers_ftype collect;
01182 };
01183 
01184 /* Called by gdbthread.c once per thread.  Records the thread's
01185    register state for the corefile note section.  */
01186 
01187 static int
01188 linux_corefile_thread_callback (struct thread_info *info, void *data)
01189 {
01190   struct linux_corefile_thread_data *args = data;
01191 
01192   if (ptid_get_pid (info->ptid) == args->pid)
01193     {
01194       struct cleanup *old_chain;
01195       struct regcache *regcache;
01196       gdb_byte *siginfo_data;
01197       LONGEST siginfo_size;
01198 
01199       regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
01200 
01201       old_chain = save_inferior_ptid ();
01202       inferior_ptid = info->ptid;
01203       target_fetch_registers (regcache, -1);
01204       siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
01205       do_cleanups (old_chain);
01206 
01207       old_chain = make_cleanup (xfree, siginfo_data);
01208 
01209       args->note_data = args->collect (regcache, info->ptid, args->obfd,
01210                                        args->note_data, args->note_size,
01211                                        args->stop_signal);
01212       args->num_notes++;
01213 
01214       if (siginfo_data != NULL)
01215         {
01216           args->note_data = elfcore_write_note (args->obfd,
01217                                                 args->note_data,
01218                                                 args->note_size,
01219                                                 "CORE", NT_SIGINFO,
01220                                                 siginfo_data, siginfo_size);
01221           args->num_notes++;
01222         }
01223 
01224       do_cleanups (old_chain);
01225     }
01226 
01227   return !args->note_data;
01228 }
01229 
01230 /* Fill the PRPSINFO structure with information about the process being
01231    debugged.  Returns 1 in case of success, 0 for failures.  Please note that
01232    even if the structure cannot be entirely filled (e.g., GDB was unable to
01233    gather information about the process UID/GID), this function will still
01234    return 1 since some information was already recorded.  It will only return
01235    0 iff nothing can be gathered.  */
01236 
01237 static int
01238 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
01239 {
01240   /* The filename which we will use to obtain some info about the process.
01241      We will basically use this to store the `/proc/PID/FILENAME' file.  */
01242   char filename[100];
01243   /* The full name of the program which generated the corefile.  */
01244   char *fname;
01245   /* The basename of the executable.  */
01246   const char *basename;
01247   /* The arguments of the program.  */
01248   char *psargs;
01249   char *infargs;
01250   /* The contents of `/proc/PID/stat' and `/proc/PID/status' files.  */
01251   char *proc_stat, *proc_status;
01252   /* Temporary buffer.  */
01253   char *tmpstr;
01254   /* The valid states of a process, according to the Linux kernel.  */
01255   const char valid_states[] = "RSDTZW";
01256   /* The program state.  */
01257   const char *prog_state;
01258   /* The state of the process.  */
01259   char pr_sname;
01260   /* The PID of the program which generated the corefile.  */
01261   pid_t pid;
01262   /* Process flags.  */
01263   unsigned int pr_flag;
01264   /* Process nice value.  */
01265   long pr_nice;
01266   /* The number of fields read by `sscanf'.  */
01267   int n_fields = 0;
01268   /* Cleanups.  */
01269   struct cleanup *c;
01270   int i;
01271 
01272   gdb_assert (p != NULL);
01273 
01274   /* Obtaining PID and filename.  */
01275   pid = ptid_get_pid (inferior_ptid);
01276   xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
01277   fname = target_fileio_read_stralloc (filename);
01278 
01279   if (fname == NULL || *fname == '\0')
01280     {
01281       /* No program name was read, so we won't be able to retrieve more
01282          information about the process.  */
01283       xfree (fname);
01284       return 0;
01285     }
01286 
01287   c = make_cleanup (xfree, fname);
01288   memset (p, 0, sizeof (*p));
01289 
01290   /* Defining the PID.  */
01291   p->pr_pid = pid;
01292 
01293   /* Copying the program name.  Only the basename matters.  */
01294   basename = lbasename (fname);
01295   strncpy (p->pr_fname, basename, sizeof (p->pr_fname));
01296   p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
01297 
01298   infargs = get_inferior_args ();
01299 
01300   psargs = xstrdup (fname);
01301   if (infargs != NULL)
01302     psargs = reconcat (psargs, psargs, " ", infargs, NULL);
01303 
01304   make_cleanup (xfree, psargs);
01305 
01306   strncpy (p->pr_psargs, psargs, sizeof (p->pr_psargs));
01307   p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
01308 
01309   xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
01310   proc_stat = target_fileio_read_stralloc (filename);
01311   make_cleanup (xfree, proc_stat);
01312 
01313   if (proc_stat == NULL || *proc_stat == '\0')
01314     {
01315       /* Despite being unable to read more information about the
01316          process, we return 1 here because at least we have its
01317          command line, PID and arguments.  */
01318       do_cleanups (c);
01319       return 1;
01320     }
01321 
01322   /* Ok, we have the stats.  It's time to do a little parsing of the
01323      contents of the buffer, so that we end up reading what we want.
01324 
01325      The following parsing mechanism is strongly based on the
01326      information generated by the `fs/proc/array.c' file, present in
01327      the Linux kernel tree.  More details about how the information is
01328      displayed can be obtained by seeing the manpage of proc(5),
01329      specifically under the entry of `/proc/[pid]/stat'.  */
01330 
01331   /* Getting rid of the PID, since we already have it.  */
01332   while (isdigit (*proc_stat))
01333     ++proc_stat;
01334 
01335   proc_stat = skip_spaces (proc_stat);
01336 
01337   /* Getting rid of the executable name, since we already have it.  We
01338      know that this name will be in parentheses, so we can safely look
01339      for the close-paren.  */
01340   while (*proc_stat != ')')
01341     ++proc_stat;
01342   ++proc_stat;
01343 
01344   proc_stat = skip_spaces (proc_stat);
01345 
01346   n_fields = sscanf (proc_stat,
01347                      "%c"               /* Process state.  */
01348                      "%d%d%d"           /* Parent PID, group ID, session ID.  */
01349                      "%*d%*d"           /* tty_nr, tpgid (not used).  */
01350                      "%u"               /* Flags.  */
01351                      "%*s%*s%*s%*s"     /* minflt, cminflt, majflt,
01352                                            cmajflt (not used).  */
01353                      "%*s%*s%*s%*s"     /* utime, stime, cutime,
01354                                            cstime (not used).  */
01355                      "%*s"              /* Priority (not used).  */
01356                      "%ld",             /* Nice.  */
01357                      &pr_sname,
01358                      &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
01359                      &pr_flag,
01360                      &pr_nice);
01361 
01362   if (n_fields != 6)
01363     {
01364       /* Again, we couldn't read the complementary information about
01365          the process state.  However, we already have minimal
01366          information, so we just return 1 here.  */
01367       do_cleanups (c);
01368       return 1;
01369     }
01370 
01371   /* Filling the structure fields.  */
01372   prog_state = strchr (valid_states, pr_sname);
01373   if (prog_state != NULL)
01374     p->pr_state = prog_state - valid_states;
01375   else
01376     {
01377       /* Zero means "Running".  */
01378       p->pr_state = 0;
01379     }
01380 
01381   p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
01382   p->pr_zomb = p->pr_sname == 'Z';
01383   p->pr_nice = pr_nice;
01384   p->pr_flag = pr_flag;
01385 
01386   /* Finally, obtaining the UID and GID.  For that, we read and parse the
01387      contents of the `/proc/PID/status' file.  */
01388   xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
01389   proc_status = target_fileio_read_stralloc (filename);
01390   make_cleanup (xfree, proc_status);
01391 
01392   if (proc_status == NULL || *proc_status == '\0')
01393     {
01394       /* Returning 1 since we already have a bunch of information.  */
01395       do_cleanups (c);
01396       return 1;
01397     }
01398 
01399   /* Extracting the UID.  */
01400   tmpstr = strstr (proc_status, "Uid:");
01401   if (tmpstr != NULL)
01402     {
01403       /* Advancing the pointer to the beginning of the UID.  */
01404       tmpstr += sizeof ("Uid:");
01405       while (*tmpstr != '\0' && !isdigit (*tmpstr))
01406         ++tmpstr;
01407 
01408       if (isdigit (*tmpstr))
01409         p->pr_uid = strtol (tmpstr, &tmpstr, 10);
01410     }
01411 
01412   /* Extracting the GID.  */
01413   tmpstr = strstr (proc_status, "Gid:");
01414   if (tmpstr != NULL)
01415     {
01416       /* Advancing the pointer to the beginning of the GID.  */
01417       tmpstr += sizeof ("Gid:");
01418       while (*tmpstr != '\0' && !isdigit (*tmpstr))
01419         ++tmpstr;
01420 
01421       if (isdigit (*tmpstr))
01422         p->pr_gid = strtol (tmpstr, &tmpstr, 10);
01423     }
01424 
01425   do_cleanups (c);
01426 
01427   return 1;
01428 }
01429 
01430 /* Fills the "to_make_corefile_note" target vector.  Builds the note
01431    section for a corefile, and returns it in a malloc buffer.  */
01432 
01433 char *
01434 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
01435                            linux_collect_thread_registers_ftype collect)
01436 {
01437   struct linux_corefile_thread_data thread_args;
01438   struct elf_internal_linux_prpsinfo prpsinfo;
01439   char *note_data = NULL;
01440   gdb_byte *auxv;
01441   int auxv_len;
01442 
01443   if (linux_fill_prpsinfo (&prpsinfo))
01444     {
01445       if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch))
01446         {
01447           note_data = gdbarch_elfcore_write_linux_prpsinfo (gdbarch, obfd,
01448                                                             note_data, note_size,
01449                                                             &prpsinfo);
01450         }
01451       else
01452         {
01453           if (gdbarch_ptr_bit (gdbarch) == 64)
01454             note_data = elfcore_write_linux_prpsinfo64 (obfd,
01455                                                         note_data, note_size,
01456                                                         &prpsinfo);
01457           else
01458             note_data = elfcore_write_linux_prpsinfo32 (obfd,
01459                                                         note_data, note_size,
01460                                                         &prpsinfo);
01461         }
01462     }
01463 
01464   /* Thread register information.  */
01465   thread_args.gdbarch = gdbarch;
01466   thread_args.pid = ptid_get_pid (inferior_ptid);
01467   thread_args.obfd = obfd;
01468   thread_args.note_data = note_data;
01469   thread_args.note_size = note_size;
01470   thread_args.num_notes = 0;
01471   thread_args.stop_signal = find_stop_signal ();
01472   thread_args.collect = collect;
01473   iterate_over_threads (linux_corefile_thread_callback, &thread_args);
01474   note_data = thread_args.note_data;
01475   if (!note_data)
01476     return NULL;
01477 
01478   /* Auxillary vector.  */
01479   auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
01480                                 NULL, &auxv);
01481   if (auxv_len > 0)
01482     {
01483       note_data = elfcore_write_note (obfd, note_data, note_size,
01484                                       "CORE", NT_AUXV, auxv, auxv_len);
01485       xfree (auxv);
01486 
01487       if (!note_data)
01488         return NULL;
01489     }
01490 
01491   /* SPU information.  */
01492   note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
01493   if (!note_data)
01494     return NULL;
01495 
01496   /* File mappings.  */
01497   note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
01498                                                   note_data, note_size);
01499 
01500   make_cleanup (xfree, note_data);
01501   return note_data;
01502 }
01503 
01504 static char *
01505 linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
01506 {
01507   /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
01508      converted to gdbarch_core_regset_sections, we no longer need to fall back
01509      to the target method at this point.  */
01510 
01511   if (!gdbarch_core_regset_sections (gdbarch))
01512     return target_make_corefile_notes (obfd, note_size);
01513   else
01514     return linux_make_corefile_notes (gdbarch, obfd, note_size,
01515                                       linux_collect_thread_registers);
01516 }
01517 
01518 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
01519    gdbarch.h.  This function is not static because it is exported to
01520    other -tdep files.  */
01521 
01522 enum gdb_signal
01523 linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
01524 {
01525   switch (signal)
01526     {
01527     case 0:
01528       return GDB_SIGNAL_0;
01529 
01530     case LINUX_SIGHUP:
01531       return GDB_SIGNAL_HUP;
01532 
01533     case LINUX_SIGINT:
01534       return GDB_SIGNAL_INT;
01535 
01536     case LINUX_SIGQUIT:
01537       return GDB_SIGNAL_QUIT;
01538 
01539     case LINUX_SIGILL:
01540       return GDB_SIGNAL_ILL;
01541 
01542     case LINUX_SIGTRAP:
01543       return GDB_SIGNAL_TRAP;
01544 
01545     case LINUX_SIGABRT:
01546       return GDB_SIGNAL_ABRT;
01547 
01548     case LINUX_SIGBUS:
01549       return GDB_SIGNAL_BUS;
01550 
01551     case LINUX_SIGFPE:
01552       return GDB_SIGNAL_FPE;
01553 
01554     case LINUX_SIGKILL:
01555       return GDB_SIGNAL_KILL;
01556 
01557     case LINUX_SIGUSR1:
01558       return GDB_SIGNAL_USR1;
01559 
01560     case LINUX_SIGSEGV:
01561       return GDB_SIGNAL_SEGV;
01562 
01563     case LINUX_SIGUSR2:
01564       return GDB_SIGNAL_USR2;
01565 
01566     case LINUX_SIGPIPE:
01567       return GDB_SIGNAL_PIPE;
01568 
01569     case LINUX_SIGALRM:
01570       return GDB_SIGNAL_ALRM;
01571 
01572     case LINUX_SIGTERM:
01573       return GDB_SIGNAL_TERM;
01574 
01575     case LINUX_SIGCHLD:
01576       return GDB_SIGNAL_CHLD;
01577 
01578     case LINUX_SIGCONT:
01579       return GDB_SIGNAL_CONT;
01580 
01581     case LINUX_SIGSTOP:
01582       return GDB_SIGNAL_STOP;
01583 
01584     case LINUX_SIGTSTP:
01585       return GDB_SIGNAL_TSTP;
01586 
01587     case LINUX_SIGTTIN:
01588       return GDB_SIGNAL_TTIN;
01589 
01590     case LINUX_SIGTTOU:
01591       return GDB_SIGNAL_TTOU;
01592 
01593     case LINUX_SIGURG:
01594       return GDB_SIGNAL_URG;
01595 
01596     case LINUX_SIGXCPU:
01597       return GDB_SIGNAL_XCPU;
01598 
01599     case LINUX_SIGXFSZ:
01600       return GDB_SIGNAL_XFSZ;
01601 
01602     case LINUX_SIGVTALRM:
01603       return GDB_SIGNAL_VTALRM;
01604 
01605     case LINUX_SIGPROF:
01606       return GDB_SIGNAL_PROF;
01607 
01608     case LINUX_SIGWINCH:
01609       return GDB_SIGNAL_WINCH;
01610 
01611     /* No way to differentiate between SIGIO and SIGPOLL.
01612        Therefore, we just handle the first one.  */
01613     case LINUX_SIGIO:
01614       return GDB_SIGNAL_IO;
01615 
01616     case LINUX_SIGPWR:
01617       return GDB_SIGNAL_PWR;
01618 
01619     case LINUX_SIGSYS:
01620       return GDB_SIGNAL_SYS;
01621 
01622     /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
01623        therefore we have to handle them here.  */
01624     case LINUX_SIGRTMIN:
01625       return GDB_SIGNAL_REALTIME_32;
01626 
01627     case LINUX_SIGRTMAX:
01628       return GDB_SIGNAL_REALTIME_64;
01629     }
01630 
01631   if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
01632     {
01633       int offset = signal - LINUX_SIGRTMIN + 1;
01634 
01635       return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
01636     }
01637 
01638   return GDB_SIGNAL_UNKNOWN;
01639 }
01640 
01641 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
01642    gdbarch.h.  This function is not static because it is exported to
01643    other -tdep files.  */
01644 
01645 int
01646 linux_gdb_signal_to_target (struct gdbarch *gdbarch,
01647                             enum gdb_signal signal)
01648 {
01649   switch (signal)
01650     {
01651     case GDB_SIGNAL_0:
01652       return 0;
01653 
01654     case GDB_SIGNAL_HUP:
01655       return LINUX_SIGHUP;
01656 
01657     case GDB_SIGNAL_INT:
01658       return LINUX_SIGINT;
01659 
01660     case GDB_SIGNAL_QUIT:
01661       return LINUX_SIGQUIT;
01662 
01663     case GDB_SIGNAL_ILL:
01664       return LINUX_SIGILL;
01665 
01666     case GDB_SIGNAL_TRAP:
01667       return LINUX_SIGTRAP;
01668 
01669     case GDB_SIGNAL_ABRT:
01670       return LINUX_SIGABRT;
01671 
01672     case GDB_SIGNAL_FPE:
01673       return LINUX_SIGFPE;
01674 
01675     case GDB_SIGNAL_KILL:
01676       return LINUX_SIGKILL;
01677 
01678     case GDB_SIGNAL_BUS:
01679       return LINUX_SIGBUS;
01680 
01681     case GDB_SIGNAL_SEGV:
01682       return LINUX_SIGSEGV;
01683 
01684     case GDB_SIGNAL_SYS:
01685       return LINUX_SIGSYS;
01686 
01687     case GDB_SIGNAL_PIPE:
01688       return LINUX_SIGPIPE;
01689 
01690     case GDB_SIGNAL_ALRM:
01691       return LINUX_SIGALRM;
01692 
01693     case GDB_SIGNAL_TERM:
01694       return LINUX_SIGTERM;
01695 
01696     case GDB_SIGNAL_URG:
01697       return LINUX_SIGURG;
01698 
01699     case GDB_SIGNAL_STOP:
01700       return LINUX_SIGSTOP;
01701 
01702     case GDB_SIGNAL_TSTP:
01703       return LINUX_SIGTSTP;
01704 
01705     case GDB_SIGNAL_CONT:
01706       return LINUX_SIGCONT;
01707 
01708     case GDB_SIGNAL_CHLD:
01709       return LINUX_SIGCHLD;
01710 
01711     case GDB_SIGNAL_TTIN:
01712       return LINUX_SIGTTIN;
01713 
01714     case GDB_SIGNAL_TTOU:
01715       return LINUX_SIGTTOU;
01716 
01717     case GDB_SIGNAL_IO:
01718       return LINUX_SIGIO;
01719 
01720     case GDB_SIGNAL_XCPU:
01721       return LINUX_SIGXCPU;
01722 
01723     case GDB_SIGNAL_XFSZ:
01724       return LINUX_SIGXFSZ;
01725 
01726     case GDB_SIGNAL_VTALRM:
01727       return LINUX_SIGVTALRM;
01728 
01729     case GDB_SIGNAL_PROF:
01730       return LINUX_SIGPROF;
01731 
01732     case GDB_SIGNAL_WINCH:
01733       return LINUX_SIGWINCH;
01734 
01735     case GDB_SIGNAL_USR1:
01736       return LINUX_SIGUSR1;
01737 
01738     case GDB_SIGNAL_USR2:
01739       return LINUX_SIGUSR2;
01740 
01741     case GDB_SIGNAL_PWR:
01742       return LINUX_SIGPWR;
01743 
01744     case GDB_SIGNAL_POLL:
01745       return LINUX_SIGPOLL;
01746 
01747     /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
01748        therefore we have to handle it here.  */
01749     case GDB_SIGNAL_REALTIME_32:
01750       return LINUX_SIGRTMIN;
01751 
01752     /* Same comment applies to _64.  */
01753     case GDB_SIGNAL_REALTIME_64:
01754       return LINUX_SIGRTMAX;
01755     }
01756 
01757   /* GDB_SIGNAL_REALTIME_33 to _64 are continuous.  */
01758   if (signal >= GDB_SIGNAL_REALTIME_33
01759       && signal <= GDB_SIGNAL_REALTIME_63)
01760     {
01761       int offset = signal - GDB_SIGNAL_REALTIME_33;
01762 
01763       return LINUX_SIGRTMIN + 1 + offset;
01764     }
01765 
01766   return -1;
01767 }
01768 
01769 /* To be called from the various GDB_OSABI_LINUX handlers for the
01770    various GNU/Linux architectures and machine types.  */
01771 
01772 void
01773 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
01774 {
01775   set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
01776   set_gdbarch_info_proc (gdbarch, linux_info_proc);
01777   set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
01778   set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
01779   set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
01780   set_gdbarch_has_shared_address_space (gdbarch,
01781                                         linux_has_shared_address_space);
01782   set_gdbarch_gdb_signal_from_target (gdbarch,
01783                                       linux_gdb_signal_from_target);
01784   set_gdbarch_gdb_signal_to_target (gdbarch,
01785                                     linux_gdb_signal_to_target);
01786 }
01787 
01788 /* Provide a prototype to silence -Wmissing-prototypes.  */
01789 extern initialize_file_ftype _initialize_linux_tdep;
01790 
01791 void
01792 _initialize_linux_tdep (void)
01793 {
01794   linux_gdbarch_data_handle =
01795     gdbarch_data_register_post_init (init_linux_gdbarch_data);
01796 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines