linux-tdep.c 82 KB
Newer Older
1 2
/* Target-dependent code for GNU/Linux, architecture independent.

3
   Copyright (C) 2009-2020 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

   This file is part of GDB.

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

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

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

#include "defs.h"
#include "gdbtypes.h"
22
#include "linux-tdep.h"
23 24
#include "auxv.h"
#include "target.h"
25 26 27 28
#include "gdbthread.h"
#include "gdbcore.h"
#include "regcache.h"
#include "regset.h"
29
#include "elf/common.h"
30
#include "elf-bfd.h"            /* for elfcore_write_* */
31
#include "inferior.h"
32
#include "cli/cli-utils.h"
Tom Tromey's avatar
gdb  
Tom Tromey committed
33 34
#include "arch-utils.h"
#include "gdb_obstack.h"
Tom Tromey's avatar
Tom Tromey committed
35
#include "observable.h"
Jan Kratochvil's avatar
Jan Kratochvil committed
36 37
#include "objfiles.h"
#include "infcall.h"
38
#include "gdbcmd.h"
39
#include "gdb_regex.h"
Tom Tromey's avatar
Tom Tromey committed
40 41
#include "gdbsupport/enum-flags.h"
#include "gdbsupport/gdb_optional.h"
42 43

#include <ctype.h>
44

45 46 47 48 49 50
/* This enum represents the values that the user can choose when
   informing the Linux kernel about which memory mappings will be
   dumped in a corefile.  They are described in the file
   Documentation/filesystems/proc.txt, inside the Linux kernel
   tree.  */

Pedro Alves's avatar
Pedro Alves committed
51
enum filter_flag
52 53 54 55 56 57 58 59 60
  {
    COREFILTER_ANON_PRIVATE = 1 << 0,
    COREFILTER_ANON_SHARED = 1 << 1,
    COREFILTER_MAPPED_PRIVATE = 1 << 2,
    COREFILTER_MAPPED_SHARED = 1 << 3,
    COREFILTER_ELF_HEADERS = 1 << 4,
    COREFILTER_HUGETLB_PRIVATE = 1 << 5,
    COREFILTER_HUGETLB_SHARED = 1 << 6,
  };
Pedro Alves's avatar
Pedro Alves committed
61
DEF_ENUM_FLAGS_TYPE (enum filter_flag, filter_flags);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

/* This struct is used to map flags found in the "VmFlags:" field (in
   the /proc/<PID>/smaps file).  */

struct smaps_vmflags
  {
    /* Zero if this structure has not been initialized yet.  It
       probably means that the Linux kernel being used does not emit
       the "VmFlags:" field on "/proc/PID/smaps".  */

    unsigned int initialized_p : 1;

    /* Memory mapped I/O area (VM_IO, "io").  */

    unsigned int io_page : 1;

    /* Area uses huge TLB pages (VM_HUGETLB, "ht").  */

    unsigned int uses_huge_tlb : 1;

    /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd").  */

    unsigned int exclude_coredump : 1;

    /* Is this a MAP_SHARED mapping (VM_SHARED, "sh").  */

    unsigned int shared_mapping : 1;
  };

91 92 93
/* Whether to take the /proc/PID/coredump_filter into account when
   generating a corefile.  */

94
static bool use_coredump_filter = true;
95

96 97 98
/* Whether the value of smaps_vmflags->exclude_coredump should be
   ignored, including mappings marked with the VM_DONTDUMP flag in
   the dump.  */
99
static bool dump_excluded_mappings = false;
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/* This enum represents the signals' numbers on a generic architecture
   running the Linux kernel.  The definition of "generic" comes from
   the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
   tree, which is the "de facto" implementation of signal numbers to
   be used by new architecture ports.

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

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

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

enum
  {
    LINUX_SIGHUP = 1,
    LINUX_SIGINT = 2,
    LINUX_SIGQUIT = 3,
    LINUX_SIGILL = 4,
    LINUX_SIGTRAP = 5,
    LINUX_SIGABRT = 6,
    LINUX_SIGIOT = 6,
    LINUX_SIGBUS = 7,
    LINUX_SIGFPE = 8,
    LINUX_SIGKILL = 9,
    LINUX_SIGUSR1 = 10,
    LINUX_SIGSEGV = 11,
    LINUX_SIGUSR2 = 12,
    LINUX_SIGPIPE = 13,
    LINUX_SIGALRM = 14,
    LINUX_SIGTERM = 15,
    LINUX_SIGSTKFLT = 16,
    LINUX_SIGCHLD = 17,
    LINUX_SIGCONT = 18,
    LINUX_SIGSTOP = 19,
    LINUX_SIGTSTP = 20,
    LINUX_SIGTTIN = 21,
    LINUX_SIGTTOU = 22,
    LINUX_SIGURG = 23,
    LINUX_SIGXCPU = 24,
    LINUX_SIGXFSZ = 25,
    LINUX_SIGVTALRM = 26,
    LINUX_SIGPROF = 27,
    LINUX_SIGWINCH = 28,
    LINUX_SIGIO = 29,
    LINUX_SIGPOLL = LINUX_SIGIO,
    LINUX_SIGPWR = 30,
    LINUX_SIGSYS = 31,
    LINUX_SIGUNUSED = 31,

    LINUX_SIGRTMIN = 32,
    LINUX_SIGRTMAX = 64,
  };

Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
166 167 168
static struct gdbarch_data *linux_gdbarch_data_handle;

struct linux_gdbarch_data
169 170 171 172
{
  struct type *siginfo_type;
  int num_disp_step_buffers;
};
Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
173 174

static void *
175
init_linux_gdbarch_data (struct obstack *obstack)
Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
176
{
177
  return obstack_zalloc<linux_gdbarch_data> (obstack);
Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
178 179 180 181 182
}

static struct linux_gdbarch_data *
get_linux_gdbarch_data (struct gdbarch *gdbarch)
{
Simon Marchi's avatar
Simon Marchi committed
183 184
  return ((struct linux_gdbarch_data *)
	  gdbarch_data (gdbarch, linux_gdbarch_data_handle));
Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
185 186
}

187 188 189 190 191 192 193 194 195 196
/* Linux-specific cached data.  This is used by GDB for caching
   purposes for each inferior.  This helps reduce the overhead of
   transfering data from a remote target to the local host.  */
struct linux_info
{
  /* Cache of the inferior's vsyscall/vDSO mapping range.  Only valid
     if VSYSCALL_RANGE_P is positive.  This is cached because getting
     at this info requires an auxv lookup (which is itself cached),
     and looking through the inferior's mappings (which change
     throughout execution and therefore cannot be cached).  */
197
  struct mem_range vsyscall_range {};
198 199 200 201

  /* Zero if we haven't tried looking up the vsyscall's range before
     yet.  Positive if we tried looking it up, and found it.  Negative
     if we tried looking it up but failed.  */
202
  int vsyscall_range_p = 0;
203

204 205
  /* Inferior's displaced step buffers.  */
  gdb::optional<displaced_step_buffers> disp_step_bufs;
206 207
};

208 209 210
/* Per-inferior data key.  */
static const struct inferior_key<linux_info> linux_inferior_data;

211 212 213 214 215 216
/* Frees whatever allocated space there is to be freed and sets INF's
   linux cache data pointer to NULL.  */

static void
invalidate_linux_cache_inf (struct inferior *inf)
{
217
  linux_inferior_data.clear (inf);
218 219 220 221 222 223
}

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

static struct linux_info *
224
get_linux_inferior_data (inferior *inf)
225
{
226
  linux_info *info = linux_inferior_data.get (inf);
227

228
  if (info == nullptr)
229
    info = linux_inferior_data.emplace (inf);
230 231 232 233

  return info;
}

234
/* See linux-tdep.h.  */
235

236
struct type *
237 238
linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
				    linux_siginfo_extra_fields extra_fields)
239
{
Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
240
  struct linux_gdbarch_data *linux_gdbarch_data;
241
  struct type *int_type, *uint_type, *long_type, *void_ptr_type, *short_type;
242 243 244 245 246
  struct type *uid_type, *pid_type;
  struct type *sigval_type, *clock_type;
  struct type *siginfo_type, *sifields_type;
  struct type *type;

Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
247 248 249 250
  linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
  if (linux_gdbarch_data->siginfo_type != NULL)
    return linux_gdbarch_data->siginfo_type;

251 252 253 254 255 256
  int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
			 	0, "int");
  uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
				 1, "unsigned int");
  long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
				 0, "long");
257 258
  short_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
				 0, "short");
259 260 261
  void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);

  /* sival_t */
262
  sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
263
  sigval_type->set_name (xstrdup ("sigval_t"));
264 265 266 267
  append_composite_type_field (sigval_type, "sival_int", int_type);
  append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);

  /* __pid_t */
268
  pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
269
			TYPE_LENGTH (int_type) * TARGET_CHAR_BIT, "__pid_t");
270
  TYPE_TARGET_TYPE (pid_type) = int_type;
271
  pid_type->set_target_is_stub (true);
272 273

  /* __uid_t */
274
  uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
275
			TYPE_LENGTH (uint_type) * TARGET_CHAR_BIT, "__uid_t");
276
  TYPE_TARGET_TYPE (uid_type) = uint_type;
277
  uid_type->set_target_is_stub (true);
278 279

  /* __clock_t */
280
  clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
281 282
			  TYPE_LENGTH (long_type) * TARGET_CHAR_BIT,
			  "__clock_t");
283
  TYPE_TARGET_TYPE (clock_type) = long_type;
284
  clock_type->set_target_is_stub (true);
285 286

  /* _sifields */
287
  sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

  {
    const int si_max_size = 128;
    int si_pad_size;
    int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;

    /* _pad */
    if (gdbarch_ptr_bit (gdbarch) == 64)
      si_pad_size = (si_max_size / size_of_int) - 4;
    else
      si_pad_size = (si_max_size / size_of_int) - 3;
    append_composite_type_field (sifields_type, "_pad",
				 init_vector_type (int_type, si_pad_size));
  }

  /* _kill */
304
  type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
305 306 307 308 309
  append_composite_type_field (type, "si_pid", pid_type);
  append_composite_type_field (type, "si_uid", uid_type);
  append_composite_type_field (sifields_type, "_kill", type);

  /* _timer */
310
  type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
311 312 313 314 315 316
  append_composite_type_field (type, "si_tid", int_type);
  append_composite_type_field (type, "si_overrun", int_type);
  append_composite_type_field (type, "si_sigval", sigval_type);
  append_composite_type_field (sifields_type, "_timer", type);

  /* _rt */
317
  type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
318 319 320 321 322 323
  append_composite_type_field (type, "si_pid", pid_type);
  append_composite_type_field (type, "si_uid", uid_type);
  append_composite_type_field (type, "si_sigval", sigval_type);
  append_composite_type_field (sifields_type, "_rt", type);

  /* _sigchld */
324
  type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
325 326 327 328 329 330 331 332
  append_composite_type_field (type, "si_pid", pid_type);
  append_composite_type_field (type, "si_uid", uid_type);
  append_composite_type_field (type, "si_status", int_type);
  append_composite_type_field (type, "si_utime", clock_type);
  append_composite_type_field (type, "si_stime", clock_type);
  append_composite_type_field (sifields_type, "_sigchld", type);

  /* _sigfault */
333
  type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
334
  append_composite_type_field (type, "si_addr", void_ptr_type);
335 336 337 338 339 340 341 342 343 344 345 346

  /* Additional bound fields for _sigfault in case they were requested.  */
  if ((extra_fields & LINUX_SIGINFO_FIELD_ADDR_BND) != 0)
    {
      struct type *sigfault_bnd_fields;

      append_composite_type_field (type, "_addr_lsb", short_type);
      sigfault_bnd_fields = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
      append_composite_type_field (sigfault_bnd_fields, "_lower", void_ptr_type);
      append_composite_type_field (sigfault_bnd_fields, "_upper", void_ptr_type);
      append_composite_type_field (type, "_addr_bnd", sigfault_bnd_fields);
    }
347 348 349
  append_composite_type_field (sifields_type, "_sigfault", type);

  /* _sigpoll */
350
  type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
351 352 353 354 355
  append_composite_type_field (type, "si_band", long_type);
  append_composite_type_field (type, "si_fd", int_type);
  append_composite_type_field (sifields_type, "_sigpoll", type);

  /* struct siginfo */
356
  siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
357
  siginfo_type->set_name (xstrdup ("siginfo"));
358 359 360 361 362 363 364
  append_composite_type_field (siginfo_type, "si_signo", int_type);
  append_composite_type_field (siginfo_type, "si_errno", int_type);
  append_composite_type_field (siginfo_type, "si_code", int_type);
  append_composite_type_field_aligned (siginfo_type,
				       "_sifields", sifields_type,
				       TYPE_LENGTH (long_type));

Jan Kratochvil's avatar
gdb/  
Jan Kratochvil committed
365 366
  linux_gdbarch_data->siginfo_type = siginfo_type;

367 368
  return siginfo_type;
}
369

370 371 372 373 374 375 376 377 378
/* This function is suitable for architectures that don't
   extend/override the standard siginfo structure.  */

static struct type *
linux_get_siginfo_type (struct gdbarch *gdbarch)
{
  return linux_get_siginfo_type_with_fields (gdbarch, 0);
}

Yao Qi's avatar
gdb/  
Yao Qi committed
379 380 381 382 383
/* Return true if the target is running on uClinux instead of normal
   Linux kernel.  */

int
linux_is_uclinux (void)
384 385 386
{
  CORE_ADDR dummy;

387 388
  return (target_auxv_search (current_top_target (), AT_NULL, &dummy) > 0
	  && target_auxv_search (current_top_target (), AT_PAGESZ, &dummy) == 0);
Yao Qi's avatar
gdb/  
Yao Qi committed
389
}
390

Yao Qi's avatar
gdb/  
Yao Qi committed
391 392 393 394
static int
linux_has_shared_address_space (struct gdbarch *gdbarch)
{
  return linux_is_uclinux ();
395
}
396 397 398

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

399
static std::string
400 401
linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
{
Tom Tromey's avatar
Tom Tromey committed
402
  if (ptid.lwp () != 0)
403
    return string_printf ("LWP %ld", ptid.lwp ());
404 405 406 407

  return normal_pid_to_str (ptid);
}

408 409 410 411 412 413 414
/* Service function for corefiles and info proc.  */

static void
read_mapping (const char *line,
	      ULONGEST *addr, ULONGEST *endaddr,
	      const char **permissions, size_t *permissions_len,
	      ULONGEST *offset,
415
	      const char **device, size_t *device_len,
416 417 418 419 420 421 422 423 424 425
	      ULONGEST *inode,
	      const char **filename)
{
  const char *p = line;

  *addr = strtoulst (p, &p, 16);
  if (*p == '-')
    p++;
  *endaddr = strtoulst (p, &p, 16);

426
  p = skip_spaces (p);
427 428 429 430 431 432 433
  *permissions = p;
  while (*p && !isspace (*p))
    p++;
  *permissions_len = p - *permissions;

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

434
  p = skip_spaces (p);
435 436 437 438 439 440 441
  *device = p;
  while (*p && !isspace (*p))
    p++;
  *device_len = p - *device;

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

442
  p = skip_spaces (p);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  *filename = p;
}

/* Helper function to decode the "VmFlags" field in /proc/PID/smaps.

   This function was based on the documentation found on
   <Documentation/filesystems/proc.txt>, on the Linux kernel.

   Linux kernels before commit
   834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
   field on smaps.  */

static void
decode_vmflags (char *p, struct smaps_vmflags *v)
{
  char *saveptr = NULL;
  const char *s;

  v->initialized_p = 1;
  p = skip_to_space (p);
  p = skip_spaces (p);

  for (s = strtok_r (p, " ", &saveptr);
       s != NULL;
       s = strtok_r (NULL, " ", &saveptr))
    {
      if (strcmp (s, "io") == 0)
	v->io_page = 1;
      else if (strcmp (s, "ht") == 0)
	v->uses_huge_tlb = 1;
      else if (strcmp (s, "dd") == 0)
	v->exclude_coredump = 1;
      else if (strcmp (s, "sh") == 0)
	v->shared_mapping = 1;
    }
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
/* Regexes used by mapping_is_anonymous_p.  Put in a structure because
   they're initialized lazily.  */

struct mapping_regexes
{
  /* Matches "/dev/zero" filenames (with or without the "(deleted)"
     string in the end).  We know for sure, based on the Linux kernel
     code, that memory mappings whose associated filename is
     "/dev/zero" are guaranteed to be MAP_ANONYMOUS.  */
  compiled_regex dev_zero
    {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB,
     _("Could not compile regex to match /dev/zero filename")};

  /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
     string in the end).  These filenames refer to shared memory
     (shmem), and memory mappings associated with them are
     MAP_ANONYMOUS as well.  */
  compiled_regex shmem_file
    {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB,
     _("Could not compile regex to match shmem filenames")};

  /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
     0' code, which is responsible to decide if it is dealing with a
     'MAP_SHARED | MAP_ANONYMOUS' mapping.  In other words, if
     FILE_DELETED matches, it does not necessarily mean that we are
     dealing with an anonymous shared mapping.  However, there is no
     easy way to detect this currently, so this is the best
     approximation we have.

     As a result, GDB will dump readonly pages of deleted executables
     when using the default value of coredump_filter (0x33), while the
     Linux kernel will not dump those pages.  But we can live with
     that.  */
  compiled_regex file_deleted
    {" (deleted)$", REG_NOSUB,
     _("Could not compile regex to match '<file> (deleted)'")};
};

518 519 520 521 522 523 524 525 526 527 528 529 530
/* Return 1 if the memory mapping is anonymous, 0 otherwise.

   FILENAME is the name of the file present in the first line of the
   memory mapping, in the "/proc/PID/smaps" output.  For example, if
   the first line is:

   7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770   /path/to/file

   Then FILENAME will be "/path/to/file".  */

static int
mapping_is_anonymous_p (const char *filename)
{
531
  static gdb::optional<mapping_regexes> regexes;
532 533 534 535 536 537 538 539
  static int init_regex_p = 0;

  if (!init_regex_p)
    {
      /* Let's be pessimistic and assume there will be an error while
	 compiling the regex'es.  */
      init_regex_p = -1;

540
      regexes.emplace ();
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

      /* If we reached this point, then everything succeeded.  */
      init_regex_p = 1;
    }

  if (init_regex_p == -1)
    {
      const char deleted[] = " (deleted)";
      size_t del_len = sizeof (deleted) - 1;
      size_t filename_len = strlen (filename);

      /* There was an error while compiling the regex'es above.  In
	 order to try to give some reliable information to the caller,
	 we just try to find the string " (deleted)" in the filename.
	 If we managed to find it, then we assume the mapping is
	 anonymous.  */
      return (filename_len >= del_len
	      && strcmp (filename + filename_len - del_len, deleted) == 0);
    }

  if (*filename == '\0'
562 563 564
      || regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
      || regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
      || regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
565 566 567 568 569 570
    return 1;

  return 0;
}

/* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
571 572
   MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
   be dumped, or greater than 0 if it should.
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

   In a nutshell, this is the logic that we follow in order to decide
   if a mapping should be dumped or not.

   - If the mapping is associated to a file whose name ends with
     " (deleted)", or if the file is "/dev/zero", or if it is
     "/SYSV%08x" (shared memory), or if there is no file associated
     with it, or if the AnonHugePages: or the Anonymous: fields in the
     /proc/PID/smaps have contents, then GDB considers this mapping to
     be anonymous.  Otherwise, GDB considers this mapping to be a
     file-backed mapping (because there will be a file associated with
     it).
 
     It is worth mentioning that, from all those checks described
     above, the most fragile is the one to see if the file name ends
     with " (deleted)".  This does not necessarily mean that the
     mapping is anonymous, because the deleted file associated with
     the mapping may have been a hard link to another file, for
     example.  The Linux kernel checks to see if "i_nlink == 0", but
     GDB cannot easily (and normally) do this check (iff running as
     root, it could find the mapping in /proc/PID/map_files/ and
     determine whether there still are other hard links to the
     inode/file).  Therefore, we made a compromise here, and we assume
     that if the file name ends with " (deleted)", then the mapping is
     indeed anonymous.  FWIW, this is something the Linux kernel could
     do better: expose this information in a more direct way.
 
   - If we see the flag "sh" in the "VmFlags:" field (in
     /proc/PID/smaps), then certainly the memory mapping is shared
     (VM_SHARED).  If we have access to the VmFlags, and we don't see
     the "sh" there, then certainly the mapping is private.  However,
     Linux kernels before commit
     834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
     "VmFlags:" field; in that case, we use another heuristic: if we
     see 'p' in the permission flags, then we assume that the mapping
     is private, even though the presence of the 's' flag there would
     mean VM_MAYSHARE, which means the mapping could still be private.
610 611 612 613 614 615
     This should work OK enough, however.

   - Even if, at the end, we decided that we should not dump the
     mapping, we still have to check if it is something like an ELF
     header (of a DSO or an executable, for example).  If it is, and
     if the user is interested in dump it, then we should dump it.  */
616 617

static int
Pedro Alves's avatar
Pedro Alves committed
618
dump_mapping_p (filter_flags filterflags, const struct smaps_vmflags *v,
619
		int maybe_private_p, int mapping_anon_p, int mapping_file_p,
620
		const char *filename, ULONGEST addr, ULONGEST offset)
621 622 623 624 625 626 627 628 629
{
  /* Initially, we trust in what we received from our caller.  This
     value may not be very precise (i.e., it was probably gathered
     from the permission line in the /proc/PID/smaps list, which
     actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
     what we have until we take a look at the "VmFlags:" field
     (assuming that the version of the Linux kernel being used
     supports it, of course).  */
  int private_p = maybe_private_p;
630
  int dump_p;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

  /* We always dump vDSO and vsyscall mappings, because it's likely that
     there'll be no file to read the contents from at core load time.
     The kernel does the same.  */
  if (strcmp ("[vdso]", filename) == 0
      || strcmp ("[vsyscall]", filename) == 0)
    return 1;

  if (v->initialized_p)
    {
      /* We never dump I/O mappings.  */
      if (v->io_page)
	return 0;

      /* Check if we should exclude this mapping.  */
646
      if (!dump_excluded_mappings && v->exclude_coredump)
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	return 0;

      /* Update our notion of whether this mapping is shared or
	 private based on a trustworthy value.  */
      private_p = !v->shared_mapping;

      /* HugeTLB checking.  */
      if (v->uses_huge_tlb)
	{
	  if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
	      || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
	    return 1;

	  return 0;
	}
    }

  if (private_p)
    {
      if (mapping_anon_p && mapping_file_p)
	{
	  /* This is a special situation.  It can happen when we see a
	     mapping that is file-backed, but that contains anonymous
	     pages.  */
671 672
	  dump_p = ((filterflags & COREFILTER_ANON_PRIVATE) != 0
		    || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
673 674
	}
      else if (mapping_anon_p)
675
	dump_p = (filterflags & COREFILTER_ANON_PRIVATE) != 0;
676
      else
677
	dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
678 679 680 681 682 683 684 685
    }
  else
    {
      if (mapping_anon_p && mapping_file_p)
	{
	  /* This is a special situation.  It can happen when we see a
	     mapping that is file-backed, but that contains anonymous
	     pages.  */
686 687
	  dump_p = ((filterflags & COREFILTER_ANON_SHARED) != 0
		    || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
688 689
	}
      else if (mapping_anon_p)
690
	dump_p = (filterflags & COREFILTER_ANON_SHARED) != 0;
691
      else
692
	dump_p = (filterflags & COREFILTER_MAPPED_SHARED) != 0;
693
    }
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

  /* Even if we decided that we shouldn't dump this mapping, we still
     have to check whether (a) the user wants us to dump mappings
     containing an ELF header, and (b) the mapping in question
     contains an ELF header.  If (a) and (b) are true, then we should
     dump this mapping.

     A mapping contains an ELF header if it is a private mapping, its
     offset is zero, and its first word is ELFMAG.  */
  if (!dump_p && private_p && offset == 0
      && (filterflags & COREFILTER_ELF_HEADERS) != 0)
    {
      /* Useful define specifying the size of the ELF magical
	 header.  */
#ifndef SELFMAG
#define SELFMAG 4
#endif

712 713 714
      /* Let's check if we have an ELF header.  */
      gdb_byte h[SELFMAG];
      if (target_read_memory (addr, h, SELFMAG) == 0)
715 716 717 718 719 720 721 722 723 724 725 726 727 728
	{
	  /* The EI_MAG* and ELFMAG* constants come from
	     <elf/common.h>.  */
	  if (h[EI_MAG0] == ELFMAG0 && h[EI_MAG1] == ELFMAG1
	      && h[EI_MAG2] == ELFMAG2 && h[EI_MAG3] == ELFMAG3)
	    {
	      /* This mapping contains an ELF header, so we
		 should dump it.  */
	      dump_p = 1;
	    }
	}
    }

  return dump_p;
729 730
}

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/* As above, but return true only when we should dump the NT_FILE
   entry.  */

static int
dump_note_entry_p (filter_flags filterflags, const struct smaps_vmflags *v,
		int maybe_private_p, int mapping_anon_p, int mapping_file_p,
		const char *filename, ULONGEST addr, ULONGEST offset)
{
  /* vDSO and vsyscall mappings will end up in the core file.  Don't
     put them in the NT_FILE note.  */
  if (strcmp ("[vdso]", filename) == 0
      || strcmp ("[vsyscall]", filename) == 0)
    return 0;

  /* Otherwise, any other file-based mapping should be placed in the
     note.  */
747
  return 1;
748 749
}

750 751 752
/* Implement the "info proc" command.  */

static void
Tom Tromey's avatar
Tom Tromey committed
753
linux_info_proc (struct gdbarch *gdbarch, const char *args,
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
		 enum info_proc_what what)
{
  /* A long is used for pid instead of an int to avoid a loss of precision
     compiler warning from the output of strtoul.  */
  long pid;
  int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
  int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
  int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
  int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
  int status_f = (what == IP_STATUS || what == IP_ALL);
  int stat_f = (what == IP_STAT || what == IP_ALL);
  char filename[100];
  int target_errno;

  if (args && isdigit (args[0]))
Tom Tromey's avatar
Tom Tromey committed
769 770 771 772 773 774
    {
      char *tem;

      pid = strtoul (args, &tem, 10);
      args = tem;
    }
775 776
  else
    {
Tom Tromey's avatar
Tom Tromey committed
777
      if (!target_has_execution ())
778 779 780 781 782 783 784
	error (_("No current process: you must name one."));
      if (current_inferior ()->fake_pid_p)
	error (_("Can't determine the current process's PID: you must name one."));

      pid = current_inferior ()->pid;
    }

785
  args = skip_spaces (args);
786 787 788 789 790 791 792
  if (args && args[0])
    error (_("Too many parameters: %s"), args);

  printf_filtered (_("process %ld\n"), pid);
  if (cmdline_f)
    {
      xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
      gdb_byte *buffer;
      ssize_t len = target_fileio_read_alloc (NULL, filename, &buffer);

      if (len > 0)
	{
	  gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
	  ssize_t pos;

	  for (pos = 0; pos < len - 1; pos++)
	    {
	      if (buffer[pos] == '\0')
		buffer[pos] = ' ';
	    }
	  buffer[len - 1] = '\0';
	  printf_filtered ("cmdline = '%s'\n", buffer);
	}
809 810 811 812 813 814
      else
	warning (_("unable to open /proc file '%s'"), filename);
    }
  if (cwd_f)
    {
      xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
815 816 817 818
      gdb::optional<std::string> contents
	= target_fileio_readlink (NULL, filename, &target_errno);
      if (contents.has_value ())
	printf_filtered ("cwd = '%s'\n", contents->c_str ());
819 820 821 822 823 824
      else
	warning (_("unable to read link '%s'"), filename);
    }
  if (exe_f)
    {
      xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
825 826 827 828
      gdb::optional<std::string> contents
	= target_fileio_readlink (NULL, filename, &target_errno);
      if (contents.has_value ())
	printf_filtered ("exe = '%s'\n", contents->c_str ());
829 830 831 832 833 834
      else
	warning (_("unable to read link '%s'"), filename);
    }
  if (mappings_f)
    {
      xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
835 836 837
      gdb::unique_xmalloc_ptr<char> map
	= target_fileio_read_stralloc (NULL, filename);
      if (map != NULL)
838 839 840 841 842 843 844 845 846 847
	{
	  char *line;

	  printf_filtered (_("Mapped address spaces:\n\n"));
	  if (gdbarch_addr_bit (gdbarch) == 32)
	    {
	      printf_filtered ("\t%10s %10s %10s %10s %s\n",
			   "Start Addr",
			   "  End Addr",
			   "      Size", "    Offset", "objfile");
848
	    }
849
	  else
850
	    {
851 852 853 854 855 856
	      printf_filtered ("  %18s %18s %10s %10s %s\n",
			   "Start Addr",
			   "  End Addr",
			   "      Size", "    Offset", "objfile");
	    }

857 858
	  char *saveptr;
	  for (line = strtok_r (map.get (), "\n", &saveptr);
859
	       line;
860
	       line = strtok_r (NULL, "\n", &saveptr))
861 862
	    {
	      ULONGEST addr, endaddr, offset, inode;
Tom Tromey's avatar
Tom Tromey committed
863
	      const char *permissions, *device, *mapping_filename;
864 865 866 867 868
	      size_t permissions_len, device_len;

	      read_mapping (line, &addr, &endaddr,
			    &permissions, &permissions_len,
			    &offset, &device, &device_len,
Tom Tromey's avatar
Tom Tromey committed
869
			    &inode, &mapping_filename);
870 871

	      if (gdbarch_addr_bit (gdbarch) == 32)
872 873
		{
		  printf_filtered ("\t%10s %10s %10s %10s %s\n",
874 875 876 877
				   paddress (gdbarch, addr),
				   paddress (gdbarch, endaddr),
				   hex_string (endaddr - addr),
				   hex_string (offset),
Tom Tromey's avatar
Tom Tromey committed
878
				   *mapping_filename ? mapping_filename : "");
879 880
		}
	      else
881 882
		{
		  printf_filtered ("  %18s %18s %10s %10s %s\n",
883 884 885 886
				   paddress (gdbarch, addr),
				   paddress (gdbarch, endaddr),
				   hex_string (endaddr - addr),
				   hex_string (offset),
Tom Tromey's avatar
Tom Tromey committed
887
				   *mapping_filename ? mapping_filename : "");
888
		}
889 890 891 892 893 894 895 896
	    }
	}
      else
	warning (_("unable to open /proc file '%s'"), filename);
    }
  if (status_f)
    {
      xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
897 898 899 900
      gdb::unique_xmalloc_ptr<char> status
	= target_fileio_read_stralloc (NULL, filename);
      if (status)
	puts_filtered (status.get ());
901 902 903 904 905 906
      else
	warning (_("unable to open /proc file '%s'"), filename);
    }
  if (stat_f)
    {
      xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
907 908 909
      gdb::unique_xmalloc_ptr<char> statstr
	= target_fileio_read_stralloc (NULL, filename);
      if (statstr)
910
	{
911
	  const char *p = statstr.get ();
912 913 914 915

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

916
	  p = skip_spaces (p);
917
	  if (*p == '(')
918
	    {
919 920 921
	      /* ps command also relies on no trailing fields
		 ever contain ')'.  */
	      const char *ep = strrchr (p, ')');
922 923 924 925 926 927
	      if (ep != NULL)
		{
		  printf_filtered ("Exec file: %.*s\n",
				   (int) (ep - p - 1), p + 1);
		  p = ep + 1;
		}
928 929
	    }

930
	  p = skip_spaces (p);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	  if (*p)
	    printf_filtered (_("State: %c\n"), *p++);

	  if (*p)
	    printf_filtered (_("Parent process: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Process group: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Session id: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("TTY: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("TTY owner process group: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));

	  if (*p)
	    printf_filtered (_("Flags: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Minor faults (no memory page): %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Minor faults, children: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Major faults (memory page faults): %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Major faults, children: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("utime: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("stime: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("utime, children: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("stime, children: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("jiffies remaining in current "
			       "time slice: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("'nice' value: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("jiffies until next timeout: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("jiffies until next SIGALRM: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("start time (jiffies since "
			       "system boot): %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Virtual memory size: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Resident set size: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("rlim: %s\n"),
			     pulongest (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Start of text: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("End of text: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Start of stack: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
#if 0	/* Don't know how architecture-dependent the rest is...
	   Anyway the signal bitmap info is available from "status".  */
	  if (*p)
	    printf_filtered (_("Kernel stack pointer: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Kernel instr pointer: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Pending signals bitmap: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Blocked signals bitmap: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Ignored signals bitmap: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("Catched signals bitmap: %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
	  if (*p)
	    printf_filtered (_("wchan (system call): %s\n"),
			     hex_string (strtoulst (p, &p, 10)));
#endif
	}
      else
	warning (_("unable to open /proc file '%s'"), filename);
    }
}

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
/* Implementation of `gdbarch_read_core_file_mappings', as defined in
   gdbarch.h.
   
   This function reads the NT_FILE note (which BFD turns into the
   section ".note.linuxcore.file").  The format of this note / section
   is described as follows in the Linux kernel sources in
   fs/binfmt_elf.c:
   
      long count     -- how many files are mapped
      long page_size -- units for file_ofs
      array of [COUNT] elements of
	long start
	long end
	long file_ofs
      followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
      
   CBFD is the BFD of the core file.

   PRE_LOOP_CB is the callback function to invoke prior to starting
   the loop which processes individual entries.  This callback will
   only be executed after the note has been examined in enough
   detail to verify that it's not malformed in some way.
   
   LOOP_CB is the callback function that will be executed once
   for each mapping.  */
Tom Tromey's avatar
gdb  
Tom Tromey committed
1067 1068

static void
1069 1070 1071
linux_read_core_file_mappings (struct gdbarch *gdbarch,
			       struct bfd *cbfd,
			       gdb::function_view<void (ULONGEST count)>
1072
				 pre_loop_cb,
1073
			       gdb::function_view<void (int num,
1074
							ULONGEST start,
1075 1076
							ULONGEST end,
							ULONGEST file_ofs,
1077
							const char *filename)>
1078
				 loop_cb)
Tom Tromey's avatar
gdb  
Tom Tromey committed
1079
{
1080
  /* Ensure that ULONGEST is big enough for reading 64-bit core files.  */
Tom Tromey's avatar
gdb  
Tom Tromey committed
1081 1082
  gdb_static_assert (sizeof (ULONGEST) >= 8);

1083 1084 1085 1086 1087 1088
  /* It's not required that the NT_FILE note exists, so return silently
     if it's not found.  Beyond this point though, we'll complain
     if problems are found.  */
  asection *section = bfd_get_section_by_name (cbfd, ".note.linuxcore.file");
  if (section == nullptr)
    return;
Tom Tromey's avatar
gdb  
Tom Tromey committed
1089

1090 1091 1092
  unsigned int addr_size_bits = gdbarch_addr_bit (gdbarch);
  unsigned int addr_size = addr_size_bits / 8;
  size_t note_size = bfd_section_size (section);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1093 1094

  if (note_size < 2 * addr_size)
1095 1096 1097 1098
    {
      warning (_("malformed core note - too short for header"));
      return;
    }
Tom Tromey's avatar
gdb  
Tom Tromey committed
1099

1100
  gdb::def_vector<gdb_byte> contents (note_size);
Tom Tromey's avatar
Tom Tromey committed
1101 1102
  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
				 0, note_size))
1103 1104 1105 1106
    {
      warning (_("could not get core note contents"));
      return;
    }
Tom Tromey's avatar
gdb  
Tom Tromey committed
1107

1108 1109
  gdb_byte *descdata = contents.data ();
  char *descend = (char *) descdata + note_size;
Tom Tromey's avatar
gdb  
Tom Tromey committed
1110 1111

  if (descdata[note_size - 1] != '\0')
1112 1113 1114 1115
    {
      warning (_("malformed note - does not end with \\0"));
      return;
    }
Tom Tromey's avatar
gdb  
Tom Tromey committed
1116

1117
  ULONGEST count = bfd_get (addr_size_bits, core_bfd, descdata);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1118 1119
  descdata += addr_size;

1120
  ULONGEST page_size = bfd_get (addr_size_bits, core_bfd, descdata);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1121 1122 1123 1124
  descdata += addr_size;

  if (note_size < 2 * addr_size + count * 3 * addr_size)
    {
1125 1126
      warning (_("malformed note - too short for supplied file count"));
      return;
Tom Tromey's avatar
gdb  
Tom Tromey committed
1127 1128
    }

1129 1130 1131 1132 1133 1134
  char *filenames = (char *) descdata + count * 3 * addr_size;

  /* Make sure that the correct number of filenames exist.  Complain
     if there aren't enough or are too many.  */
  char *f = filenames;
  for (int i = 0; i < count; i++)
Tom Tromey's avatar
gdb  
Tom Tromey committed
1135
    {
1136
      if (f >= descend)
1137
	{
1138 1139 1140 1141 1142 1143 1144 1145
	  warning (_("malformed note - filename area is too small"));
	  return;
	}
      f += strnlen (f, descend - f) + 1;
    }
  /* Complain, but don't return early if the filename area is too big.  */
  if (f != descend)
    warning (_("malformed note - filename area is too big"));
Tom Tromey's avatar
gdb  
Tom Tromey committed
1146

1147
  pre_loop_cb (count);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1148

1149 1150 1151
  for (int i = 0; i < count; i++)
    {
      ULONGEST start = bfd_get (addr_size_bits, core_bfd, descdata);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1152
      descdata += addr_size;
1153
      ULONGEST end = bfd_get (addr_size_bits, core_bfd, descdata);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1154
      descdata += addr_size;
1155
      ULONGEST file_ofs
1156
	= bfd_get (addr_size_bits, core_bfd, descdata) * page_size;
Tom Tromey's avatar
gdb  
Tom Tromey committed
1157
      descdata += addr_size;
1158 1159
      char * filename = filenames;
      filenames += strlen ((char *) filenames) + 1;
Tom Tromey's avatar
gdb  
Tom Tromey committed
1160

1161
      loop_cb (i, start, end, file_ofs, filename);
Tom Tromey's avatar
gdb  
Tom Tromey committed
1162 1163 1164
    }
}

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
/* Implement "info proc mappings" for a corefile.  */

static void
linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
{
  linux_read_core_file_mappings (gdbarch, core_bfd,
    [=] (ULONGEST count)
      {
	printf_filtered (_("Mapped address spaces:\n\n"));
	if (gdbarch_addr_bit (gdbarch) == 32)
	  {
	    printf_filtered ("\t%10s %10s %10s %10s %s\n",
			     "Start Addr",
			     "  End Addr",
			     "      Size", "    Offset", "objfile");
	  }
	else
	  {
	    printf_filtered ("  %18s %18s %10s %10s %s\n",
			     "Start Addr",
			     "  End Addr",
			     "      Size", "    Offset", "objfile");
	  }
      },
    [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
1190
	 const char *filename)
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
      {
	if (gdbarch_addr_bit (gdbarch) == 32)
	  printf_filtered ("\t%10s %10s %10s %10s %s\n",
			   paddress (gdbarch, start),
			   paddress (gdbarch, end),
			   hex_string (end - start),
			   hex_string (file_ofs),
			   filename);
	else
	  printf_filtered ("  %18s %18s %10s %10s %s\n",
			   paddress (gdbarch, start),
			   paddress (gdbarch, end),
			   hex_string (end - start),
			   hex_string (file_ofs),
			   filename);
      });
}

Tom Tromey's avatar
gdb  
Tom Tromey committed
1209 1210 1211
/* Implement "info proc" for a corefile.  */

static void
Tom Tromey's avatar
Tom Tromey committed
1212
linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
Tom Tromey's avatar
gdb  
Tom Tromey committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233