1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "linux-tdep.h"
25 #include "gdbthread.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h" /* for elfcore_write_* */
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdbsupport/gdb_obstack.h"
35 #include "observable.h"
39 #include "gdbsupport/gdb_regex.h"
40 #include "gdbsupport/enum-flags.h"
41 #include "gdbsupport/gdb_optional.h"
43 #include "gcore-elf.h"
44 #include "solib-svr4.h"
48 #include <unordered_map>
50 /* This enum represents the values that the user can choose when
51 informing the Linux kernel about which memory mappings will be
52 dumped in a corefile. They are described in the file
53 Documentation/filesystems/proc.txt, inside the Linux kernel
58 COREFILTER_ANON_PRIVATE
= 1 << 0,
59 COREFILTER_ANON_SHARED
= 1 << 1,
60 COREFILTER_MAPPED_PRIVATE
= 1 << 2,
61 COREFILTER_MAPPED_SHARED
= 1 << 3,
62 COREFILTER_ELF_HEADERS
= 1 << 4,
63 COREFILTER_HUGETLB_PRIVATE
= 1 << 5,
64 COREFILTER_HUGETLB_SHARED
= 1 << 6,
66 DEF_ENUM_FLAGS_TYPE (enum filter_flag
, filter_flags
);
68 /* This struct is used to map flags found in the "VmFlags:" field (in
69 the /proc/<PID>/smaps file). */
73 /* Zero if this structure has not been initialized yet. It
74 probably means that the Linux kernel being used does not emit
75 the "VmFlags:" field on "/proc/PID/smaps". */
77 unsigned int initialized_p
: 1;
79 /* Memory mapped I/O area (VM_IO, "io"). */
81 unsigned int io_page
: 1;
83 /* Area uses huge TLB pages (VM_HUGETLB, "ht"). */
85 unsigned int uses_huge_tlb
: 1;
87 /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd"). */
89 unsigned int exclude_coredump
: 1;
91 /* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
93 unsigned int shared_mapping
: 1;
95 /* Memory map has memory tagging enabled. */
97 unsigned int memory_tagging
: 1;
100 /* Data structure that holds the information contained in the
101 /proc/<pid>/smaps file. */
105 ULONGEST start_address
;
106 ULONGEST end_address
;
107 std::string filename
;
108 struct smaps_vmflags vmflags
;
121 /* Whether to take the /proc/PID/coredump_filter into account when
122 generating a corefile. */
124 static bool use_coredump_filter
= true;
126 /* Whether the value of smaps_vmflags->exclude_coredump should be
127 ignored, including mappings marked with the VM_DONTDUMP flag in
129 static bool dump_excluded_mappings
= false;
131 /* This enum represents the signals' numbers on a generic architecture
132 running the Linux kernel. The definition of "generic" comes from
133 the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
134 tree, which is the "de facto" implementation of signal numbers to
135 be used by new architecture ports.
137 For those architectures which have differences between the generic
138 standard (e.g., Alpha), we define the different signals (and *only*
139 those) in the specific target-dependent file (e.g.,
140 alpha-linux-tdep.c, for Alpha). Please refer to the architecture's
141 tdep file for more information.
143 ARM deserves a special mention here. On the file
144 <arch/arm/include/uapi/asm/signal.h>, it defines only one different
145 (and ARM-only) signal, which is SIGSWI, with the same number as
146 SIGRTMIN. This signal is used only for a very specific target,
147 called ArthurOS (from RISCOS). Therefore, we do not handle it on
148 the ARM-tdep file, and we can safely use the generic signal handler
149 here for ARM targets.
151 As stated above, this enum is derived from
152 <include/uapi/asm-generic/signal.h>, from the Linux kernel
173 LINUX_SIGSTKFLT
= 16,
183 LINUX_SIGVTALRM
= 26,
187 LINUX_SIGPOLL
= LINUX_SIGIO
,
190 LINUX_SIGUNUSED
= 31,
196 static struct gdbarch_data
*linux_gdbarch_data_handle
;
198 struct linux_gdbarch_data
200 struct type
*siginfo_type
;
201 int num_disp_step_buffers
;
205 init_linux_gdbarch_data (struct obstack
*obstack
)
207 return obstack_zalloc
<linux_gdbarch_data
> (obstack
);
210 static struct linux_gdbarch_data
*
211 get_linux_gdbarch_data (struct gdbarch
*gdbarch
)
213 return ((struct linux_gdbarch_data
*)
214 gdbarch_data (gdbarch
, linux_gdbarch_data_handle
));
217 /* Linux-specific cached data. This is used by GDB for caching
218 purposes for each inferior. This helps reduce the overhead of
219 transfering data from a remote target to the local host. */
222 /* Cache of the inferior's vsyscall/vDSO mapping range. Only valid
223 if VSYSCALL_RANGE_P is positive. This is cached because getting
224 at this info requires an auxv lookup (which is itself cached),
225 and looking through the inferior's mappings (which change
226 throughout execution and therefore cannot be cached). */
227 struct mem_range vsyscall_range
{};
229 /* Zero if we haven't tried looking up the vsyscall's range before
230 yet. Positive if we tried looking it up, and found it. Negative
231 if we tried looking it up but failed. */
232 int vsyscall_range_p
= 0;
234 /* Inferior's displaced step buffers. */
235 gdb::optional
<displaced_step_buffers
> disp_step_bufs
;
238 /* Per-inferior data key. */
239 static const struct inferior_key
<linux_info
> linux_inferior_data
;
241 /* Frees whatever allocated space there is to be freed and sets INF's
242 linux cache data pointer to NULL. */
245 invalidate_linux_cache_inf (struct inferior
*inf
)
247 linux_inferior_data
.clear (inf
);
250 /* Fetch the linux cache info for INF. This function always returns a
251 valid INFO pointer. */
253 static struct linux_info
*
254 get_linux_inferior_data (inferior
*inf
)
256 linux_info
*info
= linux_inferior_data
.get (inf
);
259 info
= linux_inferior_data
.emplace (inf
);
264 /* See linux-tdep.h. */
267 linux_get_siginfo_type_with_fields (struct gdbarch
*gdbarch
,
268 linux_siginfo_extra_fields extra_fields
)
270 struct linux_gdbarch_data
*linux_gdbarch_data
;
271 struct type
*int_type
, *uint_type
, *long_type
, *void_ptr_type
, *short_type
;
272 struct type
*uid_type
, *pid_type
;
273 struct type
*sigval_type
, *clock_type
;
274 struct type
*siginfo_type
, *sifields_type
;
277 linux_gdbarch_data
= get_linux_gdbarch_data (gdbarch
);
278 if (linux_gdbarch_data
->siginfo_type
!= NULL
)
279 return linux_gdbarch_data
->siginfo_type
;
281 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
283 uint_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
285 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
287 short_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
289 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
292 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
293 sigval_type
->set_name (xstrdup ("sigval_t"));
294 append_composite_type_field (sigval_type
, "sival_int", int_type
);
295 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
298 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
299 TYPE_LENGTH (int_type
) * TARGET_CHAR_BIT
, "__pid_t");
300 TYPE_TARGET_TYPE (pid_type
) = int_type
;
301 pid_type
->set_target_is_stub (true);
304 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
305 TYPE_LENGTH (uint_type
) * TARGET_CHAR_BIT
, "__uid_t");
306 TYPE_TARGET_TYPE (uid_type
) = uint_type
;
307 uid_type
->set_target_is_stub (true);
310 clock_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
311 TYPE_LENGTH (long_type
) * TARGET_CHAR_BIT
,
313 TYPE_TARGET_TYPE (clock_type
) = long_type
;
314 clock_type
->set_target_is_stub (true);
317 sifields_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
320 const int si_max_size
= 128;
322 int size_of_int
= gdbarch_int_bit (gdbarch
) / HOST_CHAR_BIT
;
325 if (gdbarch_ptr_bit (gdbarch
) == 64)
326 si_pad_size
= (si_max_size
/ size_of_int
) - 4;
328 si_pad_size
= (si_max_size
/ size_of_int
) - 3;
329 append_composite_type_field (sifields_type
, "_pad",
330 init_vector_type (int_type
, si_pad_size
));
334 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
335 append_composite_type_field (type
, "si_pid", pid_type
);
336 append_composite_type_field (type
, "si_uid", uid_type
);
337 append_composite_type_field (sifields_type
, "_kill", type
);
340 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
341 append_composite_type_field (type
, "si_tid", int_type
);
342 append_composite_type_field (type
, "si_overrun", int_type
);
343 append_composite_type_field (type
, "si_sigval", sigval_type
);
344 append_composite_type_field (sifields_type
, "_timer", type
);
347 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
348 append_composite_type_field (type
, "si_pid", pid_type
);
349 append_composite_type_field (type
, "si_uid", uid_type
);
350 append_composite_type_field (type
, "si_sigval", sigval_type
);
351 append_composite_type_field (sifields_type
, "_rt", type
);
354 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
355 append_composite_type_field (type
, "si_pid", pid_type
);
356 append_composite_type_field (type
, "si_uid", uid_type
);
357 append_composite_type_field (type
, "si_status", int_type
);
358 append_composite_type_field (type
, "si_utime", clock_type
);
359 append_composite_type_field (type
, "si_stime", clock_type
);
360 append_composite_type_field (sifields_type
, "_sigchld", type
);
363 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
364 append_composite_type_field (type
, "si_addr", void_ptr_type
);
366 /* Additional bound fields for _sigfault in case they were requested. */
367 if ((extra_fields
& LINUX_SIGINFO_FIELD_ADDR_BND
) != 0)
369 struct type
*sigfault_bnd_fields
;
371 append_composite_type_field (type
, "_addr_lsb", short_type
);
372 sigfault_bnd_fields
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
373 append_composite_type_field (sigfault_bnd_fields
, "_lower", void_ptr_type
);
374 append_composite_type_field (sigfault_bnd_fields
, "_upper", void_ptr_type
);
375 append_composite_type_field (type
, "_addr_bnd", sigfault_bnd_fields
);
377 append_composite_type_field (sifields_type
, "_sigfault", type
);
380 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
381 append_composite_type_field (type
, "si_band", long_type
);
382 append_composite_type_field (type
, "si_fd", int_type
);
383 append_composite_type_field (sifields_type
, "_sigpoll", type
);
386 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
387 append_composite_type_field (type
, "_call_addr", void_ptr_type
);
388 append_composite_type_field (type
, "_syscall", int_type
);
389 append_composite_type_field (type
, "_arch", uint_type
);
390 append_composite_type_field (sifields_type
, "_sigsys", type
);
393 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
394 siginfo_type
->set_name (xstrdup ("siginfo"));
395 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
396 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
397 append_composite_type_field (siginfo_type
, "si_code", int_type
);
398 append_composite_type_field_aligned (siginfo_type
,
399 "_sifields", sifields_type
,
400 TYPE_LENGTH (long_type
));
402 linux_gdbarch_data
->siginfo_type
= siginfo_type
;
407 /* This function is suitable for architectures that don't
408 extend/override the standard siginfo structure. */
411 linux_get_siginfo_type (struct gdbarch
*gdbarch
)
413 return linux_get_siginfo_type_with_fields (gdbarch
, 0);
416 /* Return true if the target is running on uClinux instead of normal
420 linux_is_uclinux (void)
423 target_ops
*target
= current_inferior ()->top_target ();
425 return (target_auxv_search (target
, AT_NULL
, &dummy
) > 0
426 && target_auxv_search (target
, AT_PAGESZ
, &dummy
) == 0);
430 linux_has_shared_address_space (struct gdbarch
*gdbarch
)
432 return linux_is_uclinux ();
435 /* This is how we want PTIDs from core files to be printed. */
438 linux_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
440 if (ptid
.lwp () != 0)
441 return string_printf ("LWP %ld", ptid
.lwp ());
443 return normal_pid_to_str (ptid
);
446 /* Data from one mapping from /proc/PID/maps. */
452 gdb::string_view permissions
;
454 gdb::string_view device
;
457 /* This field is guaranteed to be NULL-terminated, hence it is not a
459 const char *filename
;
462 /* Service function for corefiles and info proc. */
465 read_mapping (const char *line
)
467 struct mapping mapping
;
468 const char *p
= line
;
470 mapping
.addr
= strtoulst (p
, &p
, 16);
473 mapping
.endaddr
= strtoulst (p
, &p
, 16);
476 const char *permissions_start
= p
;
477 while (*p
&& !isspace (*p
))
479 mapping
.permissions
= {permissions_start
, (size_t) (p
- permissions_start
)};
481 mapping
.offset
= strtoulst (p
, &p
, 16);
484 const char *device_start
= p
;
485 while (*p
&& !isspace (*p
))
487 mapping
.device
= {device_start
, (size_t) (p
- device_start
)};
489 mapping
.inode
= strtoulst (p
, &p
, 10);
492 mapping
.filename
= p
;
497 /* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
499 This function was based on the documentation found on
500 <Documentation/filesystems/proc.txt>, on the Linux kernel.
502 Linux kernels before commit
503 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
507 decode_vmflags (char *p
, struct smaps_vmflags
*v
)
509 char *saveptr
= NULL
;
512 v
->initialized_p
= 1;
513 p
= skip_to_space (p
);
516 for (s
= strtok_r (p
, " ", &saveptr
);
518 s
= strtok_r (NULL
, " ", &saveptr
))
520 if (strcmp (s
, "io") == 0)
522 else if (strcmp (s
, "ht") == 0)
523 v
->uses_huge_tlb
= 1;
524 else if (strcmp (s
, "dd") == 0)
525 v
->exclude_coredump
= 1;
526 else if (strcmp (s
, "sh") == 0)
527 v
->shared_mapping
= 1;
528 else if (strcmp (s
, "mt") == 0)
529 v
->memory_tagging
= 1;
533 /* Regexes used by mapping_is_anonymous_p. Put in a structure because
534 they're initialized lazily. */
536 struct mapping_regexes
538 /* Matches "/dev/zero" filenames (with or without the "(deleted)"
539 string in the end). We know for sure, based on the Linux kernel
540 code, that memory mappings whose associated filename is
541 "/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
542 compiled_regex dev_zero
543 {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB
,
544 _("Could not compile regex to match /dev/zero filename")};
546 /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
547 string in the end). These filenames refer to shared memory
548 (shmem), and memory mappings associated with them are
549 MAP_ANONYMOUS as well. */
550 compiled_regex shmem_file
551 {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB
,
552 _("Could not compile regex to match shmem filenames")};
554 /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
555 0' code, which is responsible to decide if it is dealing with a
556 'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
557 FILE_DELETED matches, it does not necessarily mean that we are
558 dealing with an anonymous shared mapping. However, there is no
559 easy way to detect this currently, so this is the best
560 approximation we have.
562 As a result, GDB will dump readonly pages of deleted executables
563 when using the default value of coredump_filter (0x33), while the
564 Linux kernel will not dump those pages. But we can live with
566 compiled_regex file_deleted
567 {" (deleted)$", REG_NOSUB
,
568 _("Could not compile regex to match '<file> (deleted)'")};
571 /* Return 1 if the memory mapping is anonymous, 0 otherwise.
573 FILENAME is the name of the file present in the first line of the
574 memory mapping, in the "/proc/PID/smaps" output. For example, if
577 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
579 Then FILENAME will be "/path/to/file". */
582 mapping_is_anonymous_p (const char *filename
)
584 static gdb::optional
<mapping_regexes
> regexes
;
585 static int init_regex_p
= 0;
589 /* Let's be pessimistic and assume there will be an error while
590 compiling the regex'es. */
595 /* If we reached this point, then everything succeeded. */
599 if (init_regex_p
== -1)
601 const char deleted
[] = " (deleted)";
602 size_t del_len
= sizeof (deleted
) - 1;
603 size_t filename_len
= strlen (filename
);
605 /* There was an error while compiling the regex'es above. In
606 order to try to give some reliable information to the caller,
607 we just try to find the string " (deleted)" in the filename.
608 If we managed to find it, then we assume the mapping is
610 return (filename_len
>= del_len
611 && strcmp (filename
+ filename_len
- del_len
, deleted
) == 0);
614 if (*filename
== '\0'
615 || regexes
->dev_zero
.exec (filename
, 0, NULL
, 0) == 0
616 || regexes
->shmem_file
.exec (filename
, 0, NULL
, 0) == 0
617 || regexes
->file_deleted
.exec (filename
, 0, NULL
, 0) == 0)
623 /* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
624 MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
625 be dumped, or greater than 0 if it should.
627 In a nutshell, this is the logic that we follow in order to decide
628 if a mapping should be dumped or not.
630 - If the mapping is associated to a file whose name ends with
631 " (deleted)", or if the file is "/dev/zero", or if it is
632 "/SYSV%08x" (shared memory), or if there is no file associated
633 with it, or if the AnonHugePages: or the Anonymous: fields in the
634 /proc/PID/smaps have contents, then GDB considers this mapping to
635 be anonymous. Otherwise, GDB considers this mapping to be a
636 file-backed mapping (because there will be a file associated with
639 It is worth mentioning that, from all those checks described
640 above, the most fragile is the one to see if the file name ends
641 with " (deleted)". This does not necessarily mean that the
642 mapping is anonymous, because the deleted file associated with
643 the mapping may have been a hard link to another file, for
644 example. The Linux kernel checks to see if "i_nlink == 0", but
645 GDB cannot easily (and normally) do this check (iff running as
646 root, it could find the mapping in /proc/PID/map_files/ and
647 determine whether there still are other hard links to the
648 inode/file). Therefore, we made a compromise here, and we assume
649 that if the file name ends with " (deleted)", then the mapping is
650 indeed anonymous. FWIW, this is something the Linux kernel could
651 do better: expose this information in a more direct way.
653 - If we see the flag "sh" in the "VmFlags:" field (in
654 /proc/PID/smaps), then certainly the memory mapping is shared
655 (VM_SHARED). If we have access to the VmFlags, and we don't see
656 the "sh" there, then certainly the mapping is private. However,
657 Linux kernels before commit
658 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
659 "VmFlags:" field; in that case, we use another heuristic: if we
660 see 'p' in the permission flags, then we assume that the mapping
661 is private, even though the presence of the 's' flag there would
662 mean VM_MAYSHARE, which means the mapping could still be private.
663 This should work OK enough, however.
665 - Even if, at the end, we decided that we should not dump the
666 mapping, we still have to check if it is something like an ELF
667 header (of a DSO or an executable, for example). If it is, and
668 if the user is interested in dump it, then we should dump it. */
671 dump_mapping_p (filter_flags filterflags
, const struct smaps_vmflags
*v
,
672 int maybe_private_p
, int mapping_anon_p
, int mapping_file_p
,
673 const char *filename
, ULONGEST addr
, ULONGEST offset
)
675 /* Initially, we trust in what we received from our caller. This
676 value may not be very precise (i.e., it was probably gathered
677 from the permission line in the /proc/PID/smaps list, which
678 actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
679 what we have until we take a look at the "VmFlags:" field
680 (assuming that the version of the Linux kernel being used
681 supports it, of course). */
682 int private_p
= maybe_private_p
;
685 /* We always dump vDSO and vsyscall mappings, because it's likely that
686 there'll be no file to read the contents from at core load time.
687 The kernel does the same. */
688 if (strcmp ("[vdso]", filename
) == 0
689 || strcmp ("[vsyscall]", filename
) == 0)
692 if (v
->initialized_p
)
694 /* We never dump I/O mappings. */
698 /* Check if we should exclude this mapping. */
699 if (!dump_excluded_mappings
&& v
->exclude_coredump
)
702 /* Update our notion of whether this mapping is shared or
703 private based on a trustworthy value. */
704 private_p
= !v
->shared_mapping
;
706 /* HugeTLB checking. */
707 if (v
->uses_huge_tlb
)
709 if ((private_p
&& (filterflags
& COREFILTER_HUGETLB_PRIVATE
))
710 || (!private_p
&& (filterflags
& COREFILTER_HUGETLB_SHARED
)))
719 if (mapping_anon_p
&& mapping_file_p
)
721 /* This is a special situation. It can happen when we see a
722 mapping that is file-backed, but that contains anonymous
724 dump_p
= ((filterflags
& COREFILTER_ANON_PRIVATE
) != 0
725 || (filterflags
& COREFILTER_MAPPED_PRIVATE
) != 0);
727 else if (mapping_anon_p
)
728 dump_p
= (filterflags
& COREFILTER_ANON_PRIVATE
) != 0;
730 dump_p
= (filterflags
& COREFILTER_MAPPED_PRIVATE
) != 0;
734 if (mapping_anon_p
&& mapping_file_p
)
736 /* This is a special situation. It can happen when we see a
737 mapping that is file-backed, but that contains anonymous
739 dump_p
= ((filterflags
& COREFILTER_ANON_SHARED
) != 0
740 || (filterflags
& COREFILTER_MAPPED_SHARED
) != 0);
742 else if (mapping_anon_p
)
743 dump_p
= (filterflags
& COREFILTER_ANON_SHARED
) != 0;
745 dump_p
= (filterflags
& COREFILTER_MAPPED_SHARED
) != 0;
748 /* Even if we decided that we shouldn't dump this mapping, we still
749 have to check whether (a) the user wants us to dump mappings
750 containing an ELF header, and (b) the mapping in question
751 contains an ELF header. If (a) and (b) are true, then we should
754 A mapping contains an ELF header if it is a private mapping, its
755 offset is zero, and its first word is ELFMAG. */
756 if (!dump_p
&& private_p
&& offset
== 0
757 && (filterflags
& COREFILTER_ELF_HEADERS
) != 0)
759 /* Useful define specifying the size of the ELF magical
765 /* Let's check if we have an ELF header. */
767 if (target_read_memory (addr
, h
, SELFMAG
) == 0)
769 /* The EI_MAG* and ELFMAG* constants come from
771 if (h
[EI_MAG0
] == ELFMAG0
&& h
[EI_MAG1
] == ELFMAG1
772 && h
[EI_MAG2
] == ELFMAG2
&& h
[EI_MAG3
] == ELFMAG3
)
774 /* This mapping contains an ELF header, so we
784 /* As above, but return true only when we should dump the NT_FILE
788 dump_note_entry_p (filter_flags filterflags
, const struct smaps_vmflags
*v
,
789 int maybe_private_p
, int mapping_anon_p
, int mapping_file_p
,
790 const char *filename
, ULONGEST addr
, ULONGEST offset
)
792 /* vDSO and vsyscall mappings will end up in the core file. Don't
793 put them in the NT_FILE note. */
794 if (strcmp ("[vdso]", filename
) == 0
795 || strcmp ("[vsyscall]", filename
) == 0)
798 /* Otherwise, any other file-based mapping should be placed in the
803 /* Implement the "info proc" command. */
806 linux_info_proc (struct gdbarch
*gdbarch
, const char *args
,
807 enum info_proc_what what
)
809 /* A long is used for pid instead of an int to avoid a loss of precision
810 compiler warning from the output of strtoul. */
812 int cmdline_f
= (what
== IP_MINIMAL
|| what
== IP_CMDLINE
|| what
== IP_ALL
);
813 int cwd_f
= (what
== IP_MINIMAL
|| what
== IP_CWD
|| what
== IP_ALL
);
814 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
815 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
816 int status_f
= (what
== IP_STATUS
|| what
== IP_ALL
);
817 int stat_f
= (what
== IP_STAT
|| what
== IP_ALL
);
821 if (args
&& isdigit (args
[0]))
825 pid
= strtoul (args
, &tem
, 10);
830 if (!target_has_execution ())
831 error (_("No current process: you must name one."));
832 if (current_inferior ()->fake_pid_p
)
833 error (_("Can't determine the current process's PID: you must name one."));
835 pid
= current_inferior ()->pid
;
838 args
= skip_spaces (args
);
840 error (_("Too many parameters: %s"), args
);
842 gdb_printf (_("process %ld\n"), pid
);
845 xsnprintf (filename
, sizeof filename
, "/proc/%ld/cmdline", pid
);
847 ssize_t len
= target_fileio_read_alloc (NULL
, filename
, &buffer
);
851 gdb::unique_xmalloc_ptr
<char> cmdline ((char *) buffer
);
854 for (pos
= 0; pos
< len
- 1; pos
++)
856 if (buffer
[pos
] == '\0')
859 buffer
[len
- 1] = '\0';
860 gdb_printf ("cmdline = '%s'\n", buffer
);
863 warning (_("unable to open /proc file '%s'"), filename
);
867 xsnprintf (filename
, sizeof filename
, "/proc/%ld/cwd", pid
);
868 gdb::optional
<std::string
> contents
869 = target_fileio_readlink (NULL
, filename
, &target_errno
);
870 if (contents
.has_value ())
871 gdb_printf ("cwd = '%s'\n", contents
->c_str ());
873 warning (_("unable to read link '%s'"), filename
);
877 xsnprintf (filename
, sizeof filename
, "/proc/%ld/exe", pid
);
878 gdb::optional
<std::string
> contents
879 = target_fileio_readlink (NULL
, filename
, &target_errno
);
880 if (contents
.has_value ())
881 gdb_printf ("exe = '%s'\n", contents
->c_str ());
883 warning (_("unable to read link '%s'"), filename
);
887 xsnprintf (filename
, sizeof filename
, "/proc/%ld/maps", pid
);
888 gdb::unique_xmalloc_ptr
<char> map
889 = target_fileio_read_stralloc (NULL
, filename
);
894 gdb_printf (_("Mapped address spaces:\n\n"));
895 if (gdbarch_addr_bit (gdbarch
) == 32)
897 gdb_printf ("\t%10s %10s %10s %10s %s %s\n",
898 "Start Addr", " End Addr", " Size",
899 " Offset", "Perms ", "objfile");
903 gdb_printf (" %18s %18s %10s %10s %s %s\n",
904 "Start Addr", " End Addr", " Size",
905 " Offset", "Perms ", "objfile");
909 for (line
= strtok_r (map
.get (), "\n", &saveptr
);
911 line
= strtok_r (NULL
, "\n", &saveptr
))
913 struct mapping m
= read_mapping (line
);
915 if (gdbarch_addr_bit (gdbarch
) == 32)
917 gdb_printf ("\t%10s %10s %10s %10s %-5.*s %s\n",
918 paddress (gdbarch
, m
.addr
),
919 paddress (gdbarch
, m
.endaddr
),
920 hex_string (m
.endaddr
- m
.addr
),
921 hex_string (m
.offset
),
922 (int) m
.permissions
.size (),
923 m
.permissions
.data (),
928 gdb_printf (" %18s %18s %10s %10s %-5.*s %s\n",
929 paddress (gdbarch
, m
.addr
),
930 paddress (gdbarch
, m
.endaddr
),
931 hex_string (m
.endaddr
- m
.addr
),
932 hex_string (m
.offset
),
933 (int) m
.permissions
.size (),
934 m
.permissions
.data (),
940 warning (_("unable to open /proc file '%s'"), filename
);
944 xsnprintf (filename
, sizeof filename
, "/proc/%ld/status", pid
);
945 gdb::unique_xmalloc_ptr
<char> status
946 = target_fileio_read_stralloc (NULL
, filename
);
948 gdb_puts (status
.get ());
950 warning (_("unable to open /proc file '%s'"), filename
);
954 xsnprintf (filename
, sizeof filename
, "/proc/%ld/stat", pid
);
955 gdb::unique_xmalloc_ptr
<char> statstr
956 = target_fileio_read_stralloc (NULL
, filename
);
959 const char *p
= statstr
.get ();
961 gdb_printf (_("Process: %s\n"),
962 pulongest (strtoulst (p
, &p
, 10)));
967 /* ps command also relies on no trailing fields
969 const char *ep
= strrchr (p
, ')');
972 gdb_printf ("Exec file: %.*s\n",
973 (int) (ep
- p
- 1), p
+ 1);
980 gdb_printf (_("State: %c\n"), *p
++);
983 gdb_printf (_("Parent process: %s\n"),
984 pulongest (strtoulst (p
, &p
, 10)));
986 gdb_printf (_("Process group: %s\n"),
987 pulongest (strtoulst (p
, &p
, 10)));
989 gdb_printf (_("Session id: %s\n"),
990 pulongest (strtoulst (p
, &p
, 10)));
992 gdb_printf (_("TTY: %s\n"),
993 pulongest (strtoulst (p
, &p
, 10)));
995 gdb_printf (_("TTY owner process group: %s\n"),
996 pulongest (strtoulst (p
, &p
, 10)));
999 gdb_printf (_("Flags: %s\n"),
1000 hex_string (strtoulst (p
, &p
, 10)));
1002 gdb_printf (_("Minor faults (no memory page): %s\n"),
1003 pulongest (strtoulst (p
, &p
, 10)));
1005 gdb_printf (_("Minor faults, children: %s\n"),
1006 pulongest (strtoulst (p
, &p
, 10)));
1008 gdb_printf (_("Major faults (memory page faults): %s\n"),
1009 pulongest (strtoulst (p
, &p
, 10)));
1011 gdb_printf (_("Major faults, children: %s\n"),
1012 pulongest (strtoulst (p
, &p
, 10)));
1014 gdb_printf (_("utime: %s\n"),
1015 pulongest (strtoulst (p
, &p
, 10)));
1017 gdb_printf (_("stime: %s\n"),
1018 pulongest (strtoulst (p
, &p
, 10)));
1020 gdb_printf (_("utime, children: %s\n"),
1021 pulongest (strtoulst (p
, &p
, 10)));
1023 gdb_printf (_("stime, children: %s\n"),
1024 pulongest (strtoulst (p
, &p
, 10)));
1026 gdb_printf (_("jiffies remaining in current "
1027 "time slice: %s\n"),
1028 pulongest (strtoulst (p
, &p
, 10)));
1030 gdb_printf (_("'nice' value: %s\n"),
1031 pulongest (strtoulst (p
, &p
, 10)));
1033 gdb_printf (_("jiffies until next timeout: %s\n"),
1034 pulongest (strtoulst (p
, &p
, 10)));
1036 gdb_printf (_("jiffies until next SIGALRM: %s\n"),
1037 pulongest (strtoulst (p
, &p
, 10)));
1039 gdb_printf (_("start time (jiffies since "
1040 "system boot): %s\n"),
1041 pulongest (strtoulst (p
, &p
, 10)));
1043 gdb_printf (_("Virtual memory size: %s\n"),
1044 pulongest (strtoulst (p
, &p
, 10)));
1046 gdb_printf (_("Resident set size: %s\n"),
1047 pulongest (strtoulst (p
, &p
, 10)));
1049 gdb_printf (_("rlim: %s\n"),
1050 pulongest (strtoulst (p
, &p
, 10)));
1052 gdb_printf (_("Start of text: %s\n"),
1053 hex_string (strtoulst (p
, &p
, 10)));
1055 gdb_printf (_("End of text: %s\n"),
1056 hex_string (strtoulst (p
, &p
, 10)));
1058 gdb_printf (_("Start of stack: %s\n"),
1059 hex_string (strtoulst (p
, &p
, 10)));
1060 #if 0 /* Don't know how architecture-dependent the rest is...
1061 Anyway the signal bitmap info is available from "status". */
1063 gdb_printf (_("Kernel stack pointer: %s\n"),
1064 hex_string (strtoulst (p
, &p
, 10)));
1066 gdb_printf (_("Kernel instr pointer: %s\n"),
1067 hex_string (strtoulst (p
, &p
, 10)));
1069 gdb_printf (_("Pending signals bitmap: %s\n"),
1070 hex_string (strtoulst (p
, &p
, 10)));
1072 gdb_printf (_("Blocked signals bitmap: %s\n"),
1073 hex_string (strtoulst (p
, &p
, 10)));
1075 gdb_printf (_("Ignored signals bitmap: %s\n"),
1076 hex_string (strtoulst (p
, &p
, 10)));
1078 gdb_printf (_("Catched signals bitmap: %s\n"),
1079 hex_string (strtoulst (p
, &p
, 10)));
1081 gdb_printf (_("wchan (system call): %s\n"),
1082 hex_string (strtoulst (p
, &p
, 10)));
1086 warning (_("unable to open /proc file '%s'"), filename
);
1090 /* Implementation of `gdbarch_read_core_file_mappings', as defined in
1093 This function reads the NT_FILE note (which BFD turns into the
1094 section ".note.linuxcore.file"). The format of this note / section
1095 is described as follows in the Linux kernel sources in
1098 long count -- how many files are mapped
1099 long page_size -- units for file_ofs
1100 array of [COUNT] elements of
1104 followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
1106 CBFD is the BFD of the core file.
1108 PRE_LOOP_CB is the callback function to invoke prior to starting
1109 the loop which processes individual entries. This callback will
1110 only be executed after the note has been examined in enough
1111 detail to verify that it's not malformed in some way.
1113 LOOP_CB is the callback function that will be executed once
1114 for each mapping. */
1117 linux_read_core_file_mappings
1118 (struct gdbarch
*gdbarch
,
1120 read_core_file_mappings_pre_loop_ftype pre_loop_cb
,
1121 read_core_file_mappings_loop_ftype loop_cb
)
1123 /* Ensure that ULONGEST is big enough for reading 64-bit core files. */
1124 gdb_static_assert (sizeof (ULONGEST
) >= 8);
1126 /* It's not required that the NT_FILE note exists, so return silently
1127 if it's not found. Beyond this point though, we'll complain
1128 if problems are found. */
1129 asection
*section
= bfd_get_section_by_name (cbfd
, ".note.linuxcore.file");
1130 if (section
== nullptr)
1133 unsigned int addr_size_bits
= gdbarch_addr_bit (gdbarch
);
1134 unsigned int addr_size
= addr_size_bits
/ 8;
1135 size_t note_size
= bfd_section_size (section
);
1137 if (note_size
< 2 * addr_size
)
1139 warning (_("malformed core note - too short for header"));
1143 gdb::def_vector
<gdb_byte
> contents (note_size
);
1144 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1147 warning (_("could not get core note contents"));
1151 gdb_byte
*descdata
= contents
.data ();
1152 char *descend
= (char *) descdata
+ note_size
;
1154 if (descdata
[note_size
- 1] != '\0')
1156 warning (_("malformed note - does not end with \\0"));
1160 ULONGEST count
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1161 descdata
+= addr_size
;
1163 ULONGEST page_size
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1164 descdata
+= addr_size
;
1166 if (note_size
< 2 * addr_size
+ count
* 3 * addr_size
)
1168 warning (_("malformed note - too short for supplied file count"));
1172 char *filenames
= (char *) descdata
+ count
* 3 * addr_size
;
1174 /* Make sure that the correct number of filenames exist. Complain
1175 if there aren't enough or are too many. */
1176 char *f
= filenames
;
1177 for (int i
= 0; i
< count
; i
++)
1181 warning (_("malformed note - filename area is too small"));
1184 f
+= strnlen (f
, descend
- f
) + 1;
1186 /* Complain, but don't return early if the filename area is too big. */
1188 warning (_("malformed note - filename area is too big"));
1190 const bfd_build_id
*orig_build_id
= cbfd
->build_id
;
1191 std::unordered_map
<ULONGEST
, const bfd_build_id
*> vma_map
;
1193 /* Search for solib build-ids in the core file. Each time one is found,
1194 map the start vma of the corresponding elf header to the build-id. */
1195 for (bfd_section
*sec
= cbfd
->sections
; sec
!= nullptr; sec
= sec
->next
)
1197 cbfd
->build_id
= nullptr;
1199 if (sec
->flags
& SEC_LOAD
1200 && (get_elf_backend_data (cbfd
)->elf_backend_core_find_build_id
1201 (cbfd
, (bfd_vma
) sec
->filepos
)))
1202 vma_map
[sec
->vma
] = cbfd
->build_id
;
1205 cbfd
->build_id
= orig_build_id
;
1206 pre_loop_cb (count
);
1208 for (int i
= 0; i
< count
; i
++)
1210 ULONGEST start
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1211 descdata
+= addr_size
;
1212 ULONGEST end
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1213 descdata
+= addr_size
;
1215 = bfd_get (addr_size_bits
, core_bfd
, descdata
) * page_size
;
1216 descdata
+= addr_size
;
1217 char * filename
= filenames
;
1218 filenames
+= strlen ((char *) filenames
) + 1;
1219 const bfd_build_id
*build_id
= nullptr;
1220 auto vma_map_it
= vma_map
.find (start
);
1222 if (vma_map_it
!= vma_map
.end ())
1223 build_id
= vma_map_it
->second
;
1225 loop_cb (i
, start
, end
, file_ofs
, filename
, build_id
);
1229 /* Implement "info proc mappings" for a corefile. */
1232 linux_core_info_proc_mappings (struct gdbarch
*gdbarch
, const char *args
)
1234 linux_read_core_file_mappings (gdbarch
, core_bfd
,
1235 [=] (ULONGEST count
)
1237 gdb_printf (_("Mapped address spaces:\n\n"));
1238 if (gdbarch_addr_bit (gdbarch
) == 32)
1240 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1243 " Size", " Offset", "objfile");
1247 gdb_printf (" %18s %18s %10s %10s %s\n",
1250 " Size", " Offset", "objfile");
1253 [=] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
1254 const char *filename
, const bfd_build_id
*build_id
)
1256 if (gdbarch_addr_bit (gdbarch
) == 32)
1257 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1258 paddress (gdbarch
, start
),
1259 paddress (gdbarch
, end
),
1260 hex_string (end
- start
),
1261 hex_string (file_ofs
),
1264 gdb_printf (" %18s %18s %10s %10s %s\n",
1265 paddress (gdbarch
, start
),
1266 paddress (gdbarch
, end
),
1267 hex_string (end
- start
),
1268 hex_string (file_ofs
),
1273 /* Implement "info proc" for a corefile. */
1276 linux_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
1277 enum info_proc_what what
)
1279 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
1280 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
1286 exe
= bfd_core_file_failing_command (core_bfd
);
1288 gdb_printf ("exe = '%s'\n", exe
);
1290 warning (_("unable to find command name in core file"));
1294 linux_core_info_proc_mappings (gdbarch
, args
);
1296 if (!exe_f
&& !mappings_f
)
1297 error (_("unable to handle request"));
1300 /* Read siginfo data from the core, if possible. Returns -1 on
1301 failure. Otherwise, returns the number of bytes read. READBUF,
1302 OFFSET, and LEN are all as specified by the to_xfer_partial
1306 linux_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
1307 ULONGEST offset
, ULONGEST len
)
1309 thread_section_name
section_name (".note.linuxcore.siginfo", inferior_ptid
);
1310 asection
*section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
1311 if (section
== NULL
)
1314 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
, offset
, len
))
1320 typedef int linux_find_memory_region_ftype (ULONGEST vaddr
, ULONGEST size
,
1321 ULONGEST offset
, ULONGEST inode
,
1322 int read
, int write
,
1323 int exec
, int modified
,
1325 const char *filename
,
1328 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags
,
1329 const struct smaps_vmflags
*v
,
1330 int maybe_private_p
,
1333 const char *filename
,
1337 /* Helper function to parse the contents of /proc/<pid>/smaps into a data
1338 structure, for easy access.
1340 DATA is the contents of the smaps file. The parsed contents are stored
1341 into the SMAPS vector. */
1343 static std::vector
<struct smaps_data
>
1344 parse_smaps_data (const char *data
,
1345 const std::string maps_filename
)
1349 gdb_assert (data
!= nullptr);
1351 line
= strtok_r ((char *) data
, "\n", &t
);
1353 std::vector
<struct smaps_data
> smaps
;
1355 while (line
!= NULL
)
1357 struct smaps_vmflags v
;
1358 int read
, write
, exec
, priv
;
1359 int has_anonymous
= 0;
1363 memset (&v
, 0, sizeof (v
));
1364 struct mapping m
= read_mapping (line
);
1365 mapping_anon_p
= mapping_is_anonymous_p (m
.filename
);
1366 /* If the mapping is not anonymous, then we can consider it
1367 to be file-backed. These two states (anonymous or
1368 file-backed) seem to be exclusive, but they can actually
1369 coexist. For example, if a file-backed mapping has
1370 "Anonymous:" pages (see more below), then the Linux
1371 kernel will dump this mapping when the user specified
1372 that she only wants anonymous mappings in the corefile
1373 (*even* when she explicitly disabled the dumping of
1374 file-backed mappings). */
1375 mapping_file_p
= !mapping_anon_p
;
1377 /* Decode permissions. */
1378 auto has_perm
= [&m
] (char c
)
1379 { return m
.permissions
.find (c
) != gdb::string_view::npos
; };
1380 read
= has_perm ('r');
1381 write
= has_perm ('w');
1382 exec
= has_perm ('x');
1384 /* 'private' here actually means VM_MAYSHARE, and not
1385 VM_SHARED. In order to know if a mapping is really
1386 private or not, we must check the flag "sh" in the
1387 VmFlags field. This is done by decode_vmflags. However,
1388 if we are using a Linux kernel released before the commit
1389 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
1390 not have the VmFlags there. In this case, there is
1391 really no way to know if we are dealing with VM_SHARED,
1392 so we just assume that VM_MAYSHARE is enough. */
1393 priv
= has_perm ('p');
1395 /* Try to detect if region should be dumped by parsing smaps
1397 for (line
= strtok_r (NULL
, "\n", &t
);
1398 line
!= NULL
&& line
[0] >= 'A' && line
[0] <= 'Z';
1399 line
= strtok_r (NULL
, "\n", &t
))
1401 char keyword
[64 + 1];
1403 if (sscanf (line
, "%64s", keyword
) != 1)
1405 warning (_("Error parsing {s,}maps file '%s'"),
1406 maps_filename
.c_str ());
1410 if (strcmp (keyword
, "Anonymous:") == 0)
1412 /* Older Linux kernels did not support the
1413 "Anonymous:" counter. Check it here. */
1416 else if (strcmp (keyword
, "VmFlags:") == 0)
1417 decode_vmflags (line
, &v
);
1419 if (strcmp (keyword
, "AnonHugePages:") == 0
1420 || strcmp (keyword
, "Anonymous:") == 0)
1422 unsigned long number
;
1424 if (sscanf (line
, "%*s%lu", &number
) != 1)
1426 warning (_("Error parsing {s,}maps file '%s' number"),
1427 maps_filename
.c_str ());
1432 /* Even if we are dealing with a file-backed
1433 mapping, if it contains anonymous pages we
1434 consider it to be *also* an anonymous
1435 mapping, because this is what the Linux
1438 // Dump segments that have been written to.
1439 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1442 Note that if the mapping is already marked as
1443 file-backed (i.e., mapping_file_p is
1444 non-zero), then this is a special case, and
1445 this mapping will be dumped either when the
1446 user wants to dump file-backed *or* anonymous
1452 /* Save the smaps entry to the vector. */
1453 struct smaps_data map
;
1455 map
.start_address
= m
.addr
;
1456 map
.end_address
= m
.endaddr
;
1457 map
.filename
= m
.filename
;
1459 map
.read
= read
? true : false;
1460 map
.write
= write
? true : false;
1461 map
.exec
= exec
? true : false;
1462 map
.priv
= priv
? true : false;
1463 map
.has_anonymous
= has_anonymous
;
1464 map
.mapping_anon_p
= mapping_anon_p
? true : false;
1465 map
.mapping_file_p
= mapping_file_p
? true : false;
1466 map
.offset
= m
.offset
;
1467 map
.inode
= m
.inode
;
1469 smaps
.emplace_back (map
);
1475 /* Helper that checks if an address is in a memory tag page for a live
1479 linux_process_address_in_memtag_page (CORE_ADDR address
)
1481 if (current_inferior ()->fake_pid_p
)
1484 pid_t pid
= current_inferior ()->pid
;
1486 std::string smaps_file
= string_printf ("/proc/%d/smaps", pid
);
1488 gdb::unique_xmalloc_ptr
<char> data
1489 = target_fileio_read_stralloc (NULL
, smaps_file
.c_str ());
1491 if (data
== nullptr)
1494 /* Parse the contents of smaps into a vector. */
1495 std::vector
<struct smaps_data
> smaps
1496 = parse_smaps_data (data
.get (), smaps_file
);
1498 for (const smaps_data
&map
: smaps
)
1500 /* Is the address within [start_address, end_address) in a page
1501 mapped with memory tagging? */
1502 if (address
>= map
.start_address
1503 && address
< map
.end_address
1504 && map
.vmflags
.memory_tagging
)
1511 /* Helper that checks if an address is in a memory tag page for a core file
1515 linux_core_file_address_in_memtag_page (CORE_ADDR address
)
1517 if (core_bfd
== nullptr)
1520 memtag_section_info info
;
1521 return get_next_core_memtag_section (core_bfd
, nullptr, address
, info
);
1524 /* See linux-tdep.h. */
1527 linux_address_in_memtag_page (CORE_ADDR address
)
1529 if (!target_has_execution ())
1530 return linux_core_file_address_in_memtag_page (address
);
1532 return linux_process_address_in_memtag_page (address
);
1535 /* List memory regions in the inferior for a corefile. */
1538 linux_find_memory_regions_full (struct gdbarch
*gdbarch
,
1539 linux_dump_mapping_p_ftype
*should_dump_mapping_p
,
1540 linux_find_memory_region_ftype
*func
,
1544 /* Default dump behavior of coredump_filter (0x33), according to
1545 Documentation/filesystems/proc.txt from the Linux kernel
1547 filter_flags filterflags
= (COREFILTER_ANON_PRIVATE
1548 | COREFILTER_ANON_SHARED
1549 | COREFILTER_ELF_HEADERS
1550 | COREFILTER_HUGETLB_PRIVATE
);
1552 /* We need to know the real target PID to access /proc. */
1553 if (current_inferior ()->fake_pid_p
)
1556 pid
= current_inferior ()->pid
;
1558 if (use_coredump_filter
)
1560 std::string core_dump_filter_name
1561 = string_printf ("/proc/%d/coredump_filter", pid
);
1563 gdb::unique_xmalloc_ptr
<char> coredumpfilterdata
1564 = target_fileio_read_stralloc (NULL
, core_dump_filter_name
.c_str ());
1566 if (coredumpfilterdata
!= NULL
)
1570 sscanf (coredumpfilterdata
.get (), "%x", &flags
);
1571 filterflags
= (enum filter_flag
) flags
;
1575 std::string maps_filename
= string_printf ("/proc/%d/smaps", pid
);
1577 gdb::unique_xmalloc_ptr
<char> data
1578 = target_fileio_read_stralloc (NULL
, maps_filename
.c_str ());
1582 /* Older Linux kernels did not support /proc/PID/smaps. */
1583 maps_filename
= string_printf ("/proc/%d/maps", pid
);
1584 data
= target_fileio_read_stralloc (NULL
, maps_filename
.c_str ());
1586 if (data
== nullptr)
1590 /* Parse the contents of smaps into a vector. */
1591 std::vector
<struct smaps_data
> smaps
1592 = parse_smaps_data (data
.get (), maps_filename
.c_str ());
1594 for (const struct smaps_data
&map
: smaps
)
1596 int should_dump_p
= 0;
1598 if (map
.has_anonymous
)
1601 = should_dump_mapping_p (filterflags
, &map
.vmflags
,
1605 map
.filename
.c_str (),
1611 /* Older Linux kernels did not support the "Anonymous:" counter.
1612 If it is missing, we can't be sure - dump all the pages. */
1616 /* Invoke the callback function to create the corefile segment. */
1619 func (map
.start_address
, map
.end_address
- map
.start_address
,
1620 map
.offset
, map
.inode
, map
.read
, map
.write
, map
.exec
,
1621 1, /* MODIFIED is true because we want to dump
1623 map
.vmflags
.memory_tagging
!= 0,
1624 map
.filename
.c_str (), obfd
);
1631 /* A structure for passing information through
1632 linux_find_memory_regions_full. */
1634 struct linux_find_memory_regions_data
1636 /* The original callback. */
1638 find_memory_region_ftype func
;
1640 /* The original datum. */
1645 /* A callback for linux_find_memory_regions that converts between the
1646 "full"-style callback and find_memory_region_ftype. */
1649 linux_find_memory_regions_thunk (ULONGEST vaddr
, ULONGEST size
,
1650 ULONGEST offset
, ULONGEST inode
,
1651 int read
, int write
, int exec
, int modified
,
1653 const char *filename
, void *arg
)
1655 struct linux_find_memory_regions_data
*data
1656 = (struct linux_find_memory_regions_data
*) arg
;
1658 return data
->func (vaddr
, size
, read
, write
, exec
, modified
, memory_tagged
,
1662 /* A variant of linux_find_memory_regions_full that is suitable as the
1663 gdbarch find_memory_regions method. */
1666 linux_find_memory_regions (struct gdbarch
*gdbarch
,
1667 find_memory_region_ftype func
, void *obfd
)
1669 struct linux_find_memory_regions_data data
;
1674 return linux_find_memory_regions_full (gdbarch
,
1676 linux_find_memory_regions_thunk
,
1680 /* This is used to pass information from
1681 linux_make_mappings_corefile_notes through
1682 linux_find_memory_regions_full. */
1684 struct linux_make_mappings_data
1686 /* Number of files mapped. */
1687 ULONGEST file_count
;
1689 /* The obstack for the main part of the data. */
1690 struct obstack
*data_obstack
;
1692 /* The filename obstack. */
1693 struct obstack
*filename_obstack
;
1695 /* The architecture's "long" type. */
1696 struct type
*long_type
;
1699 static linux_find_memory_region_ftype linux_make_mappings_callback
;
1701 /* A callback for linux_find_memory_regions_full that updates the
1702 mappings data for linux_make_mappings_corefile_notes. */
1705 linux_make_mappings_callback (ULONGEST vaddr
, ULONGEST size
,
1706 ULONGEST offset
, ULONGEST inode
,
1707 int read
, int write
, int exec
, int modified
,
1709 const char *filename
, void *data
)
1711 struct linux_make_mappings_data
*map_data
1712 = (struct linux_make_mappings_data
*) data
;
1713 gdb_byte buf
[sizeof (ULONGEST
)];
1715 if (*filename
== '\0' || inode
== 0)
1718 ++map_data
->file_count
;
1720 pack_long (buf
, map_data
->long_type
, vaddr
);
1721 obstack_grow (map_data
->data_obstack
, buf
, TYPE_LENGTH (map_data
->long_type
));
1722 pack_long (buf
, map_data
->long_type
, vaddr
+ size
);
1723 obstack_grow (map_data
->data_obstack
, buf
, TYPE_LENGTH (map_data
->long_type
));
1724 pack_long (buf
, map_data
->long_type
, offset
);
1725 obstack_grow (map_data
->data_obstack
, buf
, TYPE_LENGTH (map_data
->long_type
));
1727 obstack_grow_str0 (map_data
->filename_obstack
, filename
);
1732 /* Write the file mapping data to the core file, if possible. OBFD is
1733 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1734 is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
1737 linux_make_mappings_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
,
1738 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
1741 struct linux_make_mappings_data mapping_data
;
1742 struct type
*long_type
1743 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
), 0, "long");
1744 gdb_byte buf
[sizeof (ULONGEST
)];
1746 auto_obstack data_obstack
, filename_obstack
;
1748 mapping_data
.file_count
= 0;
1749 mapping_data
.data_obstack
= &data_obstack
;
1750 mapping_data
.filename_obstack
= &filename_obstack
;
1751 mapping_data
.long_type
= long_type
;
1753 /* Reserve space for the count. */
1754 obstack_blank (&data_obstack
, TYPE_LENGTH (long_type
));
1755 /* We always write the page size as 1 since we have no good way to
1756 determine the correct value. */
1757 pack_long (buf
, long_type
, 1);
1758 obstack_grow (&data_obstack
, buf
, TYPE_LENGTH (long_type
));
1760 linux_find_memory_regions_full (gdbarch
,
1762 linux_make_mappings_callback
,
1765 if (mapping_data
.file_count
!= 0)
1767 /* Write the count to the obstack. */
1768 pack_long ((gdb_byte
*) obstack_base (&data_obstack
),
1769 long_type
, mapping_data
.file_count
);
1771 /* Copy the filenames to the data obstack. */
1772 int size
= obstack_object_size (&filename_obstack
);
1773 obstack_grow (&data_obstack
, obstack_base (&filename_obstack
),
1776 note_data
.reset (elfcore_write_file_note (obfd
, note_data
.release (), note_size
,
1777 obstack_base (&data_obstack
),
1778 obstack_object_size (&data_obstack
)));
1782 /* Fetch the siginfo data for the specified thread, if it exists. If
1783 there is no data, or we could not read it, return an empty
1786 static gdb::byte_vector
1787 linux_get_siginfo_data (thread_info
*thread
, struct gdbarch
*gdbarch
)
1789 struct type
*siginfo_type
;
1792 if (!gdbarch_get_siginfo_type_p (gdbarch
))
1793 return gdb::byte_vector ();
1795 scoped_restore_current_thread save_current_thread
;
1796 switch_to_thread (thread
);
1798 siginfo_type
= gdbarch_get_siginfo_type (gdbarch
);
1800 gdb::byte_vector
buf (TYPE_LENGTH (siginfo_type
));
1802 bytes_read
= target_read (current_inferior ()->top_target (),
1803 TARGET_OBJECT_SIGNAL_INFO
, NULL
,
1804 buf
.data (), 0, TYPE_LENGTH (siginfo_type
));
1805 if (bytes_read
!= TYPE_LENGTH (siginfo_type
))
1811 struct linux_corefile_thread_data
1813 linux_corefile_thread_data (struct gdbarch
*gdbarch
, bfd
*obfd
,
1814 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
1815 int *note_size
, gdb_signal stop_signal
)
1816 : gdbarch (gdbarch
), obfd (obfd
), note_data (note_data
),
1817 note_size (note_size
), stop_signal (stop_signal
)
1820 struct gdbarch
*gdbarch
;
1822 gdb::unique_xmalloc_ptr
<char> ¬e_data
;
1824 enum gdb_signal stop_signal
;
1827 /* Records the thread's register state for the corefile note
1831 linux_corefile_thread (struct thread_info
*info
,
1832 struct linux_corefile_thread_data
*args
)
1834 gcore_elf_build_thread_register_notes (args
->gdbarch
, info
,
1836 args
->obfd
, &args
->note_data
,
1839 /* Don't return anything if we got no register information above,
1840 such a core file is useless. */
1841 if (args
->note_data
!= NULL
)
1843 gdb::byte_vector siginfo_data
1844 = linux_get_siginfo_data (info
, args
->gdbarch
);
1845 if (!siginfo_data
.empty ())
1846 args
->note_data
.reset (elfcore_write_note (args
->obfd
,
1847 args
->note_data
.release (),
1850 siginfo_data
.data (),
1851 siginfo_data
.size ()));
1855 /* Fill the PRPSINFO structure with information about the process being
1856 debugged. Returns 1 in case of success, 0 for failures. Please note that
1857 even if the structure cannot be entirely filled (e.g., GDB was unable to
1858 gather information about the process UID/GID), this function will still
1859 return 1 since some information was already recorded. It will only return
1860 0 iff nothing can be gathered. */
1863 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo
*p
)
1865 /* The filename which we will use to obtain some info about the process.
1866 We will basically use this to store the `/proc/PID/FILENAME' file. */
1868 /* The basename of the executable. */
1869 const char *basename
;
1870 /* Temporary buffer. */
1872 /* The valid states of a process, according to the Linux kernel. */
1873 const char valid_states
[] = "RSDTZW";
1874 /* The program state. */
1875 const char *prog_state
;
1876 /* The state of the process. */
1878 /* The PID of the program which generated the corefile. */
1880 /* Process flags. */
1881 unsigned int pr_flag
;
1882 /* Process nice value. */
1884 /* The number of fields read by `sscanf'. */
1887 gdb_assert (p
!= NULL
);
1889 /* Obtaining PID and filename. */
1890 pid
= inferior_ptid
.pid ();
1891 xsnprintf (filename
, sizeof (filename
), "/proc/%d/cmdline", (int) pid
);
1892 /* The full name of the program which generated the corefile. */
1893 gdb::unique_xmalloc_ptr
<char> fname
1894 = target_fileio_read_stralloc (NULL
, filename
);
1896 if (fname
== NULL
|| fname
.get ()[0] == '\0')
1898 /* No program name was read, so we won't be able to retrieve more
1899 information about the process. */
1903 memset (p
, 0, sizeof (*p
));
1905 /* Defining the PID. */
1908 /* Copying the program name. Only the basename matters. */
1909 basename
= lbasename (fname
.get ());
1910 strncpy (p
->pr_fname
, basename
, sizeof (p
->pr_fname
) - 1);
1911 p
->pr_fname
[sizeof (p
->pr_fname
) - 1] = '\0';
1913 const std::string
&infargs
= current_inferior ()->args ();
1915 /* The arguments of the program. */
1916 std::string psargs
= fname
.get ();
1917 if (!infargs
.empty ())
1918 psargs
+= ' ' + infargs
;
1920 strncpy (p
->pr_psargs
, psargs
.c_str (), sizeof (p
->pr_psargs
) - 1);
1921 p
->pr_psargs
[sizeof (p
->pr_psargs
) - 1] = '\0';
1923 xsnprintf (filename
, sizeof (filename
), "/proc/%d/stat", (int) pid
);
1924 /* The contents of `/proc/PID/stat'. */
1925 gdb::unique_xmalloc_ptr
<char> proc_stat_contents
1926 = target_fileio_read_stralloc (NULL
, filename
);
1927 char *proc_stat
= proc_stat_contents
.get ();
1929 if (proc_stat
== NULL
|| *proc_stat
== '\0')
1931 /* Despite being unable to read more information about the
1932 process, we return 1 here because at least we have its
1933 command line, PID and arguments. */
1937 /* Ok, we have the stats. It's time to do a little parsing of the
1938 contents of the buffer, so that we end up reading what we want.
1940 The following parsing mechanism is strongly based on the
1941 information generated by the `fs/proc/array.c' file, present in
1942 the Linux kernel tree. More details about how the information is
1943 displayed can be obtained by seeing the manpage of proc(5),
1944 specifically under the entry of `/proc/[pid]/stat'. */
1946 /* Getting rid of the PID, since we already have it. */
1947 while (isdigit (*proc_stat
))
1950 proc_stat
= skip_spaces (proc_stat
);
1952 /* ps command also relies on no trailing fields ever contain ')'. */
1953 proc_stat
= strrchr (proc_stat
, ')');
1954 if (proc_stat
== NULL
)
1958 proc_stat
= skip_spaces (proc_stat
);
1960 n_fields
= sscanf (proc_stat
,
1961 "%c" /* Process state. */
1962 "%d%d%d" /* Parent PID, group ID, session ID. */
1963 "%*d%*d" /* tty_nr, tpgid (not used). */
1965 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1966 cmajflt (not used). */
1967 "%*s%*s%*s%*s" /* utime, stime, cutime,
1968 cstime (not used). */
1969 "%*s" /* Priority (not used). */
1972 &p
->pr_ppid
, &p
->pr_pgrp
, &p
->pr_sid
,
1978 /* Again, we couldn't read the complementary information about
1979 the process state. However, we already have minimal
1980 information, so we just return 1 here. */
1984 /* Filling the structure fields. */
1985 prog_state
= strchr (valid_states
, pr_sname
);
1986 if (prog_state
!= NULL
)
1987 p
->pr_state
= prog_state
- valid_states
;
1990 /* Zero means "Running". */
1994 p
->pr_sname
= p
->pr_state
> 5 ? '.' : pr_sname
;
1995 p
->pr_zomb
= p
->pr_sname
== 'Z';
1996 p
->pr_nice
= pr_nice
;
1997 p
->pr_flag
= pr_flag
;
1999 /* Finally, obtaining the UID and GID. For that, we read and parse the
2000 contents of the `/proc/PID/status' file. */
2001 xsnprintf (filename
, sizeof (filename
), "/proc/%d/status", (int) pid
);
2002 /* The contents of `/proc/PID/status'. */
2003 gdb::unique_xmalloc_ptr
<char> proc_status_contents
2004 = target_fileio_read_stralloc (NULL
, filename
);
2005 char *proc_status
= proc_status_contents
.get ();
2007 if (proc_status
== NULL
|| *proc_status
== '\0')
2009 /* Returning 1 since we already have a bunch of information. */
2013 /* Extracting the UID. */
2014 tmpstr
= strstr (proc_status
, "Uid:");
2017 /* Advancing the pointer to the beginning of the UID. */
2018 tmpstr
+= sizeof ("Uid:");
2019 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
2022 if (isdigit (*tmpstr
))
2023 p
->pr_uid
= strtol (tmpstr
, &tmpstr
, 10);
2026 /* Extracting the GID. */
2027 tmpstr
= strstr (proc_status
, "Gid:");
2030 /* Advancing the pointer to the beginning of the GID. */
2031 tmpstr
+= sizeof ("Gid:");
2032 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
2035 if (isdigit (*tmpstr
))
2036 p
->pr_gid
= strtol (tmpstr
, &tmpstr
, 10);
2042 /* Build the note section for a corefile, and return it in a malloc
2045 static gdb::unique_xmalloc_ptr
<char>
2046 linux_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
2048 struct elf_internal_linux_prpsinfo prpsinfo
;
2049 gdb::unique_xmalloc_ptr
<char> note_data
;
2051 if (! gdbarch_iterate_over_regset_sections_p (gdbarch
))
2054 if (linux_fill_prpsinfo (&prpsinfo
))
2056 if (gdbarch_ptr_bit (gdbarch
) == 64)
2057 note_data
.reset (elfcore_write_linux_prpsinfo64 (obfd
,
2058 note_data
.release (),
2059 note_size
, &prpsinfo
));
2061 note_data
.reset (elfcore_write_linux_prpsinfo32 (obfd
,
2062 note_data
.release (),
2063 note_size
, &prpsinfo
));
2066 /* Thread register information. */
2069 update_thread_list ();
2071 catch (const gdb_exception_error
&e
)
2073 exception_print (gdb_stderr
, e
);
2076 /* Like the kernel, prefer dumping the signalled thread first.
2077 "First thread" is what tools use to infer the signalled
2079 thread_info
*signalled_thr
= gcore_find_signalled_thread ();
2080 gdb_signal stop_signal
;
2081 if (signalled_thr
!= nullptr)
2082 stop_signal
= signalled_thr
->stop_signal ();
2084 stop_signal
= GDB_SIGNAL_0
;
2086 linux_corefile_thread_data
thread_args (gdbarch
, obfd
, note_data
, note_size
,
2089 if (signalled_thr
!= nullptr)
2090 linux_corefile_thread (signalled_thr
, &thread_args
);
2091 for (thread_info
*thr
: current_inferior ()->non_exited_threads ())
2093 if (thr
== signalled_thr
)
2096 linux_corefile_thread (thr
, &thread_args
);
2102 /* Auxillary vector. */
2103 gdb::optional
<gdb::byte_vector
> auxv
=
2104 target_read_alloc (current_inferior ()->top_target (),
2105 TARGET_OBJECT_AUXV
, NULL
);
2106 if (auxv
&& !auxv
->empty ())
2108 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
2109 note_size
, "CORE", NT_AUXV
,
2110 auxv
->data (), auxv
->size ()));
2116 /* File mappings. */
2117 linux_make_mappings_corefile_notes (gdbarch
, obfd
, note_data
, note_size
);
2119 /* Target description. */
2120 gcore_elf_make_tdesc_note (obfd
, ¬e_data
, note_size
);
2125 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
2126 gdbarch.h. This function is not static because it is exported to
2127 other -tdep files. */
2130 linux_gdb_signal_from_target (struct gdbarch
*gdbarch
, int signal
)
2135 return GDB_SIGNAL_0
;
2138 return GDB_SIGNAL_HUP
;
2141 return GDB_SIGNAL_INT
;
2144 return GDB_SIGNAL_QUIT
;
2147 return GDB_SIGNAL_ILL
;
2150 return GDB_SIGNAL_TRAP
;
2153 return GDB_SIGNAL_ABRT
;
2156 return GDB_SIGNAL_BUS
;
2159 return GDB_SIGNAL_FPE
;
2162 return GDB_SIGNAL_KILL
;
2165 return GDB_SIGNAL_USR1
;
2168 return GDB_SIGNAL_SEGV
;
2171 return GDB_SIGNAL_USR2
;
2174 return GDB_SIGNAL_PIPE
;
2177 return GDB_SIGNAL_ALRM
;
2180 return GDB_SIGNAL_TERM
;
2183 return GDB_SIGNAL_CHLD
;
2186 return GDB_SIGNAL_CONT
;
2189 return GDB_SIGNAL_STOP
;
2192 return GDB_SIGNAL_TSTP
;
2195 return GDB_SIGNAL_TTIN
;
2198 return GDB_SIGNAL_TTOU
;
2201 return GDB_SIGNAL_URG
;
2204 return GDB_SIGNAL_XCPU
;
2207 return GDB_SIGNAL_XFSZ
;
2209 case LINUX_SIGVTALRM
:
2210 return GDB_SIGNAL_VTALRM
;
2213 return GDB_SIGNAL_PROF
;
2215 case LINUX_SIGWINCH
:
2216 return GDB_SIGNAL_WINCH
;
2218 /* No way to differentiate between SIGIO and SIGPOLL.
2219 Therefore, we just handle the first one. */
2221 return GDB_SIGNAL_IO
;
2224 return GDB_SIGNAL_PWR
;
2227 return GDB_SIGNAL_SYS
;
2229 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
2230 therefore we have to handle them here. */
2231 case LINUX_SIGRTMIN
:
2232 return GDB_SIGNAL_REALTIME_32
;
2234 case LINUX_SIGRTMAX
:
2235 return GDB_SIGNAL_REALTIME_64
;
2238 if (signal
>= LINUX_SIGRTMIN
+ 1 && signal
<= LINUX_SIGRTMAX
- 1)
2240 int offset
= signal
- LINUX_SIGRTMIN
+ 1;
2242 return (enum gdb_signal
) ((int) GDB_SIGNAL_REALTIME_33
+ offset
);
2245 return GDB_SIGNAL_UNKNOWN
;
2248 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
2249 gdbarch.h. This function is not static because it is exported to
2250 other -tdep files. */
2253 linux_gdb_signal_to_target (struct gdbarch
*gdbarch
,
2254 enum gdb_signal signal
)
2261 case GDB_SIGNAL_HUP
:
2262 return LINUX_SIGHUP
;
2264 case GDB_SIGNAL_INT
:
2265 return LINUX_SIGINT
;
2267 case GDB_SIGNAL_QUIT
:
2268 return LINUX_SIGQUIT
;
2270 case GDB_SIGNAL_ILL
:
2271 return LINUX_SIGILL
;
2273 case GDB_SIGNAL_TRAP
:
2274 return LINUX_SIGTRAP
;
2276 case GDB_SIGNAL_ABRT
:
2277 return LINUX_SIGABRT
;
2279 case GDB_SIGNAL_FPE
:
2280 return LINUX_SIGFPE
;
2282 case GDB_SIGNAL_KILL
:
2283 return LINUX_SIGKILL
;
2285 case GDB_SIGNAL_BUS
:
2286 return LINUX_SIGBUS
;
2288 case GDB_SIGNAL_SEGV
:
2289 return LINUX_SIGSEGV
;
2291 case GDB_SIGNAL_SYS
:
2292 return LINUX_SIGSYS
;
2294 case GDB_SIGNAL_PIPE
:
2295 return LINUX_SIGPIPE
;
2297 case GDB_SIGNAL_ALRM
:
2298 return LINUX_SIGALRM
;
2300 case GDB_SIGNAL_TERM
:
2301 return LINUX_SIGTERM
;
2303 case GDB_SIGNAL_URG
:
2304 return LINUX_SIGURG
;
2306 case GDB_SIGNAL_STOP
:
2307 return LINUX_SIGSTOP
;
2309 case GDB_SIGNAL_TSTP
:
2310 return LINUX_SIGTSTP
;
2312 case GDB_SIGNAL_CONT
:
2313 return LINUX_SIGCONT
;
2315 case GDB_SIGNAL_CHLD
:
2316 return LINUX_SIGCHLD
;
2318 case GDB_SIGNAL_TTIN
:
2319 return LINUX_SIGTTIN
;
2321 case GDB_SIGNAL_TTOU
:
2322 return LINUX_SIGTTOU
;
2327 case GDB_SIGNAL_XCPU
:
2328 return LINUX_SIGXCPU
;
2330 case GDB_SIGNAL_XFSZ
:
2331 return LINUX_SIGXFSZ
;
2333 case GDB_SIGNAL_VTALRM
:
2334 return LINUX_SIGVTALRM
;
2336 case GDB_SIGNAL_PROF
:
2337 return LINUX_SIGPROF
;
2339 case GDB_SIGNAL_WINCH
:
2340 return LINUX_SIGWINCH
;
2342 case GDB_SIGNAL_USR1
:
2343 return LINUX_SIGUSR1
;
2345 case GDB_SIGNAL_USR2
:
2346 return LINUX_SIGUSR2
;
2348 case GDB_SIGNAL_PWR
:
2349 return LINUX_SIGPWR
;
2351 case GDB_SIGNAL_POLL
:
2352 return LINUX_SIGPOLL
;
2354 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
2355 therefore we have to handle it here. */
2356 case GDB_SIGNAL_REALTIME_32
:
2357 return LINUX_SIGRTMIN
;
2359 /* Same comment applies to _64. */
2360 case GDB_SIGNAL_REALTIME_64
:
2361 return LINUX_SIGRTMAX
;
2364 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
2365 if (signal
>= GDB_SIGNAL_REALTIME_33
2366 && signal
<= GDB_SIGNAL_REALTIME_63
)
2368 int offset
= signal
- GDB_SIGNAL_REALTIME_33
;
2370 return LINUX_SIGRTMIN
+ 1 + offset
;
2376 /* Helper for linux_vsyscall_range that does the real work of finding
2377 the vsyscall's address range. */
2380 linux_vsyscall_range_raw (struct gdbarch
*gdbarch
, struct mem_range
*range
)
2385 if (target_auxv_search (current_inferior ()->top_target (),
2386 AT_SYSINFO_EHDR
, &range
->start
) <= 0)
2389 /* It doesn't make sense to access the host's /proc when debugging a
2390 core file. Instead, look for the PT_LOAD segment that matches
2392 if (!target_has_execution ())
2397 phdrs_size
= bfd_get_elf_phdr_upper_bound (core_bfd
);
2398 if (phdrs_size
== -1)
2401 gdb::unique_xmalloc_ptr
<Elf_Internal_Phdr
>
2402 phdrs ((Elf_Internal_Phdr
*) xmalloc (phdrs_size
));
2403 num_phdrs
= bfd_get_elf_phdrs (core_bfd
, phdrs
.get ());
2404 if (num_phdrs
== -1)
2407 for (i
= 0; i
< num_phdrs
; i
++)
2408 if (phdrs
.get ()[i
].p_type
== PT_LOAD
2409 && phdrs
.get ()[i
].p_vaddr
== range
->start
)
2411 range
->length
= phdrs
.get ()[i
].p_memsz
;
2418 /* We need to know the real target PID to access /proc. */
2419 if (current_inferior ()->fake_pid_p
)
2422 pid
= current_inferior ()->pid
;
2424 /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
2425 reading /proc/PID/maps (2). The later identifies thread stacks
2426 in the output, which requires scanning every thread in the thread
2427 group to check whether a VMA is actually a thread's stack. With
2428 Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
2429 a few thousand threads, (1) takes a few miliseconds, while (2)
2430 takes several seconds. Also note that "smaps", what we read for
2431 determining core dump mappings, is even slower than "maps". */
2432 xsnprintf (filename
, sizeof filename
, "/proc/%ld/task/%ld/maps", pid
, pid
);
2433 gdb::unique_xmalloc_ptr
<char> data
2434 = target_fileio_read_stralloc (NULL
, filename
);
2438 char *saveptr
= NULL
;
2440 for (line
= strtok_r (data
.get (), "\n", &saveptr
);
2442 line
= strtok_r (NULL
, "\n", &saveptr
))
2444 ULONGEST addr
, endaddr
;
2445 const char *p
= line
;
2447 addr
= strtoulst (p
, &p
, 16);
2448 if (addr
== range
->start
)
2452 endaddr
= strtoulst (p
, &p
, 16);
2453 range
->length
= endaddr
- addr
;
2459 warning (_("unable to open /proc file '%s'"), filename
);
2464 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2465 caching, and defers the real work to linux_vsyscall_range_raw. */
2468 linux_vsyscall_range (struct gdbarch
*gdbarch
, struct mem_range
*range
)
2470 struct linux_info
*info
= get_linux_inferior_data (current_inferior ());
2472 if (info
->vsyscall_range_p
== 0)
2474 if (linux_vsyscall_range_raw (gdbarch
, &info
->vsyscall_range
))
2475 info
->vsyscall_range_p
= 1;
2477 info
->vsyscall_range_p
= -1;
2480 if (info
->vsyscall_range_p
< 0)
2483 *range
= info
->vsyscall_range
;
2487 /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
2488 definitions would be dependent on compilation host. */
2489 #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
2490 #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
2492 /* See gdbarch.sh 'infcall_mmap'. */
2495 linux_infcall_mmap (CORE_ADDR size
, unsigned prot
)
2497 struct objfile
*objf
;
2498 /* Do there still exist any Linux systems without "mmap64"?
2499 "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
2500 struct value
*mmap_val
= find_function_in_inferior ("mmap64", &objf
);
2501 struct value
*addr_val
;
2502 struct gdbarch
*gdbarch
= objf
->arch ();
2506 ARG_ADDR
, ARG_LENGTH
, ARG_PROT
, ARG_FLAGS
, ARG_FD
, ARG_OFFSET
, ARG_LAST
2508 struct value
*arg
[ARG_LAST
];
2510 arg
[ARG_ADDR
] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
,
2512 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2513 arg
[ARG_LENGTH
] = value_from_ulongest
2514 (builtin_type (gdbarch
)->builtin_unsigned_long
, size
);
2515 gdb_assert ((prot
& ~(GDB_MMAP_PROT_READ
| GDB_MMAP_PROT_WRITE
2516 | GDB_MMAP_PROT_EXEC
))
2518 arg
[ARG_PROT
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, prot
);
2519 arg
[ARG_FLAGS
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2520 GDB_MMAP_MAP_PRIVATE
2521 | GDB_MMAP_MAP_ANONYMOUS
);
2522 arg
[ARG_FD
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, -1);
2523 arg
[ARG_OFFSET
] = value_from_longest (builtin_type (gdbarch
)->builtin_int64
,
2525 addr_val
= call_function_by_hand (mmap_val
, NULL
, arg
);
2526 retval
= value_as_address (addr_val
);
2527 if (retval
== (CORE_ADDR
) -1)
2528 error (_("Failed inferior mmap call for %s bytes, errno is changed."),
2533 /* See gdbarch.sh 'infcall_munmap'. */
2536 linux_infcall_munmap (CORE_ADDR addr
, CORE_ADDR size
)
2538 struct objfile
*objf
;
2539 struct value
*munmap_val
= find_function_in_inferior ("munmap", &objf
);
2540 struct value
*retval_val
;
2541 struct gdbarch
*gdbarch
= objf
->arch ();
2545 ARG_ADDR
, ARG_LENGTH
, ARG_LAST
2547 struct value
*arg
[ARG_LAST
];
2549 arg
[ARG_ADDR
] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
,
2551 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2552 arg
[ARG_LENGTH
] = value_from_ulongest
2553 (builtin_type (gdbarch
)->builtin_unsigned_long
, size
);
2554 retval_val
= call_function_by_hand (munmap_val
, NULL
, arg
);
2555 retval
= value_as_long (retval_val
);
2557 warning (_("Failed inferior munmap call at %s for %s bytes, "
2558 "errno is changed."),
2559 hex_string (addr
), pulongest (size
));
2562 /* See linux-tdep.h. */
2565 linux_displaced_step_location (struct gdbarch
*gdbarch
)
2570 /* Determine entry point from target auxiliary vector. This avoids
2571 the need for symbols. Also, when debugging a stand-alone SPU
2572 executable, entry_point_address () will point to an SPU
2573 local-store address and is thus not usable as displaced stepping
2574 location. The auxiliary vector gets us the PowerPC-side entry
2575 point address instead. */
2576 if (target_auxv_search (current_inferior ()->top_target (),
2577 AT_ENTRY
, &addr
) <= 0)
2578 throw_error (NOT_SUPPORTED_ERROR
,
2579 _("Cannot find AT_ENTRY auxiliary vector entry."));
2581 /* Make certain that the address points at real code, and not a
2582 function descriptor. */
2583 addr
= gdbarch_convert_from_func_ptr_addr
2584 (gdbarch
, addr
, current_inferior ()->top_target ());
2586 /* Inferior calls also use the entry point as a breakpoint location.
2587 We don't want displaced stepping to interfere with those
2588 breakpoints, so leave space. */
2589 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &bp_len
);
2595 /* See linux-tdep.h. */
2597 displaced_step_prepare_status
2598 linux_displaced_step_prepare (gdbarch
*arch
, thread_info
*thread
,
2599 CORE_ADDR
&displaced_pc
)
2601 linux_info
*per_inferior
= get_linux_inferior_data (thread
->inf
);
2603 if (!per_inferior
->disp_step_bufs
.has_value ())
2605 /* Figure out the location of the buffers. They are contiguous, starting
2606 at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
2607 CORE_ADDR disp_step_buf_addr
2608 = linux_displaced_step_location (thread
->inf
->gdbarch
);
2609 int buf_len
= gdbarch_max_insn_length (arch
);
2611 linux_gdbarch_data
*gdbarch_data
= get_linux_gdbarch_data (arch
);
2612 gdb_assert (gdbarch_data
->num_disp_step_buffers
> 0);
2614 std::vector
<CORE_ADDR
> buffers
;
2615 for (int i
= 0; i
< gdbarch_data
->num_disp_step_buffers
; i
++)
2616 buffers
.push_back (disp_step_buf_addr
+ i
* buf_len
);
2618 per_inferior
->disp_step_bufs
.emplace (buffers
);
2621 return per_inferior
->disp_step_bufs
->prepare (thread
, displaced_pc
);
2624 /* See linux-tdep.h. */
2626 displaced_step_finish_status
2627 linux_displaced_step_finish (gdbarch
*arch
, thread_info
*thread
, gdb_signal sig
)
2629 linux_info
*per_inferior
= get_linux_inferior_data (thread
->inf
);
2631 gdb_assert (per_inferior
->disp_step_bufs
.has_value ());
2633 return per_inferior
->disp_step_bufs
->finish (arch
, thread
, sig
);
2636 /* See linux-tdep.h. */
2638 const displaced_step_copy_insn_closure
*
2639 linux_displaced_step_copy_insn_closure_by_addr (inferior
*inf
, CORE_ADDR addr
)
2641 linux_info
*per_inferior
= linux_inferior_data
.get (inf
);
2643 if (per_inferior
== nullptr
2644 || !per_inferior
->disp_step_bufs
.has_value ())
2647 return per_inferior
->disp_step_bufs
->copy_insn_closure_by_addr (addr
);
2650 /* See linux-tdep.h. */
2653 linux_displaced_step_restore_all_in_ptid (inferior
*parent_inf
, ptid_t ptid
)
2655 linux_info
*per_inferior
= linux_inferior_data
.get (parent_inf
);
2657 if (per_inferior
== nullptr
2658 || !per_inferior
->disp_step_bufs
.has_value ())
2661 per_inferior
->disp_step_bufs
->restore_in_ptid (ptid
);
2664 /* See linux-tdep.h. */
2667 linux_get_hwcap (struct target_ops
*target
)
2670 if (target_auxv_search (target
, AT_HWCAP
, &field
) != 1)
2675 /* See linux-tdep.h. */
2678 linux_get_hwcap2 (struct target_ops
*target
)
2681 if (target_auxv_search (target
, AT_HWCAP2
, &field
) != 1)
2686 /* Display whether the gcore command is using the
2687 /proc/PID/coredump_filter file. */
2690 show_use_coredump_filter (struct ui_file
*file
, int from_tty
,
2691 struct cmd_list_element
*c
, const char *value
)
2693 gdb_printf (file
, _("Use of /proc/PID/coredump_filter file to generate"
2694 " corefiles is %s.\n"), value
);
2697 /* Display whether the gcore command is dumping mappings marked with
2698 the VM_DONTDUMP flag. */
2701 show_dump_excluded_mappings (struct ui_file
*file
, int from_tty
,
2702 struct cmd_list_element
*c
, const char *value
)
2704 gdb_printf (file
, _("Dumping of mappings marked with the VM_DONTDUMP"
2705 " flag is %s.\n"), value
);
2708 /* To be called from the various GDB_OSABI_LINUX handlers for the
2709 various GNU/Linux architectures and machine types.
2711 NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
2712 displaced stepping is not supported. */
2715 linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
,
2716 int num_disp_step_buffers
)
2718 if (num_disp_step_buffers
> 0)
2720 linux_gdbarch_data
*gdbarch_data
= get_linux_gdbarch_data (gdbarch
);
2721 gdbarch_data
->num_disp_step_buffers
= num_disp_step_buffers
;
2723 set_gdbarch_displaced_step_prepare (gdbarch
,
2724 linux_displaced_step_prepare
);
2725 set_gdbarch_displaced_step_finish (gdbarch
, linux_displaced_step_finish
);
2726 set_gdbarch_displaced_step_copy_insn_closure_by_addr
2727 (gdbarch
, linux_displaced_step_copy_insn_closure_by_addr
);
2728 set_gdbarch_displaced_step_restore_all_in_ptid
2729 (gdbarch
, linux_displaced_step_restore_all_in_ptid
);
2732 set_gdbarch_core_pid_to_str (gdbarch
, linux_core_pid_to_str
);
2733 set_gdbarch_info_proc (gdbarch
, linux_info_proc
);
2734 set_gdbarch_core_info_proc (gdbarch
, linux_core_info_proc
);
2735 set_gdbarch_core_xfer_siginfo (gdbarch
, linux_core_xfer_siginfo
);
2736 set_gdbarch_read_core_file_mappings (gdbarch
, linux_read_core_file_mappings
);
2737 set_gdbarch_find_memory_regions (gdbarch
, linux_find_memory_regions
);
2738 set_gdbarch_make_corefile_notes (gdbarch
, linux_make_corefile_notes
);
2739 set_gdbarch_has_shared_address_space (gdbarch
,
2740 linux_has_shared_address_space
);
2741 set_gdbarch_gdb_signal_from_target (gdbarch
,
2742 linux_gdb_signal_from_target
);
2743 set_gdbarch_gdb_signal_to_target (gdbarch
,
2744 linux_gdb_signal_to_target
);
2745 set_gdbarch_vsyscall_range (gdbarch
, linux_vsyscall_range
);
2746 set_gdbarch_infcall_mmap (gdbarch
, linux_infcall_mmap
);
2747 set_gdbarch_infcall_munmap (gdbarch
, linux_infcall_munmap
);
2748 set_gdbarch_get_siginfo_type (gdbarch
, linux_get_siginfo_type
);
2751 void _initialize_linux_tdep ();
2753 _initialize_linux_tdep ()
2755 linux_gdbarch_data_handle
=
2756 gdbarch_data_register_pre_init (init_linux_gdbarch_data
);
2758 /* Observers used to invalidate the cache when needed. */
2759 gdb::observers::inferior_exit
.attach (invalidate_linux_cache_inf
,
2761 gdb::observers::inferior_appeared
.attach (invalidate_linux_cache_inf
,
2763 gdb::observers::inferior_execd
.attach (invalidate_linux_cache_inf
,
2766 add_setshow_boolean_cmd ("use-coredump-filter", class_files
,
2767 &use_coredump_filter
, _("\
2768 Set whether gcore should consider /proc/PID/coredump_filter."),
2770 Show whether gcore should consider /proc/PID/coredump_filter."),
2772 Use this command to set whether gcore should consider the contents\n\
2773 of /proc/PID/coredump_filter when generating the corefile. For more information\n\
2774 about this file, refer to the manpage of core(5)."),
2775 NULL
, show_use_coredump_filter
,
2776 &setlist
, &showlist
);
2778 add_setshow_boolean_cmd ("dump-excluded-mappings", class_files
,
2779 &dump_excluded_mappings
, _("\
2780 Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2782 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2784 Use this command to set whether gcore should dump mappings marked with the\n\
2785 VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
2786 more information about this file, refer to the manpage of proc(5) and core(5)."),
2787 NULL
, show_dump_excluded_mappings
,
2788 &setlist
, &showlist
);
2791 /* Fetch (and possibly build) an appropriate `link_map_offsets' for
2792 ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
2795 linux_ilp32_fetch_link_map_offsets ()
2797 static link_map_offsets lmo
;
2798 static link_map_offsets
*lmp
= nullptr;
2804 lmo
.r_version_offset
= 0;
2805 lmo
.r_version_size
= 4;
2806 lmo
.r_map_offset
= 4;
2807 lmo
.r_brk_offset
= 8;
2808 lmo
.r_ldsomap_offset
= -1;
2810 /* Everything we need is in the first 20 bytes. */
2811 lmo
.link_map_size
= 20;
2812 lmo
.l_addr_offset
= 0;
2813 lmo
.l_name_offset
= 4;
2814 lmo
.l_ld_offset
= 8;
2815 lmo
.l_next_offset
= 12;
2816 lmo
.l_prev_offset
= 16;
2823 linux_lp64_fetch_link_map_offsets ()
2825 static link_map_offsets lmo
;
2826 static link_map_offsets
*lmp
= nullptr;
2832 lmo
.r_version_offset
= 0;
2833 lmo
.r_version_size
= 4;
2834 lmo
.r_map_offset
= 8;
2835 lmo
.r_brk_offset
= 16;
2836 lmo
.r_ldsomap_offset
= -1;
2838 /* Everything we need is in the first 40 bytes. */
2839 lmo
.link_map_size
= 40;
2840 lmo
.l_addr_offset
= 0;
2841 lmo
.l_name_offset
= 8;
2842 lmo
.l_ld_offset
= 16;
2843 lmo
.l_next_offset
= 24;
2844 lmo
.l_prev_offset
= 32;