1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2024 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"
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"
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 struct linux_gdbarch_data
198 struct type
*siginfo_type
= nullptr;
199 int num_disp_step_buffers
= 0;
202 static const registry
<gdbarch
>::key
<linux_gdbarch_data
>
203 linux_gdbarch_data_handle
;
205 static struct linux_gdbarch_data
*
206 get_linux_gdbarch_data (struct gdbarch
*gdbarch
)
208 struct linux_gdbarch_data
*result
= linux_gdbarch_data_handle
.get (gdbarch
);
209 if (result
== nullptr)
210 result
= linux_gdbarch_data_handle
.emplace (gdbarch
);
214 /* Linux-specific cached data. This is used by GDB for caching
215 purposes for each inferior. This helps reduce the overhead of
216 transfering data from a remote target to the local host. */
219 /* Cache of the inferior's vsyscall/vDSO mapping range. Only valid
220 if VSYSCALL_RANGE_P is positive. This is cached because getting
221 at this info requires an auxv lookup (which is itself cached),
222 and looking through the inferior's mappings (which change
223 throughout execution and therefore cannot be cached). */
224 struct mem_range vsyscall_range
{};
226 /* Zero if we haven't tried looking up the vsyscall's range before
227 yet. Positive if we tried looking it up, and found it. Negative
228 if we tried looking it up but failed. */
229 int vsyscall_range_p
= 0;
231 /* Inferior's displaced step buffers. */
232 std::optional
<displaced_step_buffers
> disp_step_bufs
;
235 /* Per-inferior data key. */
236 static const registry
<inferior
>::key
<linux_info
> linux_inferior_data
;
238 /* Frees whatever allocated space there is to be freed and sets INF's
239 linux cache data pointer to NULL. */
242 invalidate_linux_cache_inf (struct inferior
*inf
)
244 linux_inferior_data
.clear (inf
);
247 /* inferior_execd observer. */
250 linux_inferior_execd (inferior
*exec_inf
, inferior
*follow_inf
)
252 invalidate_linux_cache_inf (follow_inf
);
255 /* Fetch the linux cache info for INF. This function always returns a
256 valid INFO pointer. */
258 static struct linux_info
*
259 get_linux_inferior_data (inferior
*inf
)
261 linux_info
*info
= linux_inferior_data
.get (inf
);
264 info
= linux_inferior_data
.emplace (inf
);
269 /* See linux-tdep.h. */
272 linux_get_siginfo_type_with_fields (struct gdbarch
*gdbarch
,
273 linux_siginfo_extra_fields extra_fields
)
275 struct linux_gdbarch_data
*linux_gdbarch_data
;
276 struct type
*int_type
, *uint_type
, *long_type
, *void_ptr_type
, *short_type
;
277 struct type
*uid_type
, *pid_type
;
278 struct type
*sigval_type
, *clock_type
;
279 struct type
*siginfo_type
, *sifields_type
;
282 linux_gdbarch_data
= get_linux_gdbarch_data (gdbarch
);
283 if (linux_gdbarch_data
->siginfo_type
!= NULL
)
284 return linux_gdbarch_data
->siginfo_type
;
286 type_allocator
alloc (gdbarch
);
288 int_type
= init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
290 uint_type
= init_integer_type (alloc
, gdbarch_int_bit (gdbarch
),
292 long_type
= init_integer_type (alloc
, gdbarch_long_bit (gdbarch
),
294 short_type
= init_integer_type (alloc
, gdbarch_long_bit (gdbarch
),
296 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
299 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
300 sigval_type
->set_name (xstrdup ("sigval_t"));
301 append_composite_type_field (sigval_type
, "sival_int", int_type
);
302 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
305 pid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
306 int_type
->length () * TARGET_CHAR_BIT
,
308 pid_type
->set_target_type (int_type
);
309 pid_type
->set_target_is_stub (true);
312 uid_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
313 uint_type
->length () * TARGET_CHAR_BIT
,
315 uid_type
->set_target_type (uint_type
);
316 uid_type
->set_target_is_stub (true);
319 clock_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
,
320 long_type
->length () * TARGET_CHAR_BIT
,
322 clock_type
->set_target_type (long_type
);
323 clock_type
->set_target_is_stub (true);
326 sifields_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
329 const int si_max_size
= 128;
331 int size_of_int
= gdbarch_int_bit (gdbarch
) / HOST_CHAR_BIT
;
334 if (gdbarch_ptr_bit (gdbarch
) == 64)
335 si_pad_size
= (si_max_size
/ size_of_int
) - 4;
337 si_pad_size
= (si_max_size
/ size_of_int
) - 3;
338 append_composite_type_field (sifields_type
, "_pad",
339 init_vector_type (int_type
, si_pad_size
));
343 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
344 append_composite_type_field (type
, "si_pid", pid_type
);
345 append_composite_type_field (type
, "si_uid", uid_type
);
346 append_composite_type_field (sifields_type
, "_kill", type
);
349 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
350 append_composite_type_field (type
, "si_tid", int_type
);
351 append_composite_type_field (type
, "si_overrun", int_type
);
352 append_composite_type_field (type
, "si_sigval", sigval_type
);
353 append_composite_type_field (sifields_type
, "_timer", type
);
356 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
357 append_composite_type_field (type
, "si_pid", pid_type
);
358 append_composite_type_field (type
, "si_uid", uid_type
);
359 append_composite_type_field (type
, "si_sigval", sigval_type
);
360 append_composite_type_field (sifields_type
, "_rt", type
);
363 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
364 append_composite_type_field (type
, "si_pid", pid_type
);
365 append_composite_type_field (type
, "si_uid", uid_type
);
366 append_composite_type_field (type
, "si_status", int_type
);
367 append_composite_type_field (type
, "si_utime", clock_type
);
368 append_composite_type_field (type
, "si_stime", clock_type
);
369 append_composite_type_field (sifields_type
, "_sigchld", type
);
372 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
373 append_composite_type_field (type
, "si_addr", void_ptr_type
);
375 /* Additional bound fields for _sigfault in case they were requested. */
376 if ((extra_fields
& LINUX_SIGINFO_FIELD_ADDR_BND
) != 0)
378 struct type
*sigfault_bnd_fields
;
380 append_composite_type_field (type
, "_addr_lsb", short_type
);
381 sigfault_bnd_fields
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
382 append_composite_type_field (sigfault_bnd_fields
, "_lower", void_ptr_type
);
383 append_composite_type_field (sigfault_bnd_fields
, "_upper", void_ptr_type
);
384 append_composite_type_field (type
, "_addr_bnd", sigfault_bnd_fields
);
386 append_composite_type_field (sifields_type
, "_sigfault", type
);
389 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
390 append_composite_type_field (type
, "si_band", long_type
);
391 append_composite_type_field (type
, "si_fd", int_type
);
392 append_composite_type_field (sifields_type
, "_sigpoll", type
);
395 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
396 append_composite_type_field (type
, "_call_addr", void_ptr_type
);
397 append_composite_type_field (type
, "_syscall", int_type
);
398 append_composite_type_field (type
, "_arch", uint_type
);
399 append_composite_type_field (sifields_type
, "_sigsys", type
);
402 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
403 siginfo_type
->set_name (xstrdup ("siginfo"));
404 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
405 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
406 append_composite_type_field (siginfo_type
, "si_code", int_type
);
407 append_composite_type_field_aligned (siginfo_type
,
408 "_sifields", sifields_type
,
409 long_type
->length ());
411 linux_gdbarch_data
->siginfo_type
= siginfo_type
;
416 /* This function is suitable for architectures that don't
417 extend/override the standard siginfo structure. */
420 linux_get_siginfo_type (struct gdbarch
*gdbarch
)
422 return linux_get_siginfo_type_with_fields (gdbarch
, 0);
425 /* Return true if the target is running on uClinux instead of normal
429 linux_is_uclinux (void)
433 return (target_auxv_search (AT_NULL
, &dummy
) > 0
434 && target_auxv_search (AT_PAGESZ
, &dummy
) == 0);
438 linux_has_shared_address_space (struct gdbarch
*gdbarch
)
440 return linux_is_uclinux ();
443 /* This is how we want PTIDs from core files to be printed. */
446 linux_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
448 if (ptid
.lwp () != 0)
449 return string_printf ("LWP %ld", ptid
.lwp ());
451 return normal_pid_to_str (ptid
);
454 /* Data from one mapping from /proc/PID/maps. */
460 std::string_view permissions
;
462 std::string_view device
;
465 /* This field is guaranteed to be NULL-terminated, hence it is not a
467 const char *filename
;
470 /* Service function for corefiles and info proc. */
473 read_mapping (const char *line
)
475 struct mapping mapping
;
476 const char *p
= line
;
478 mapping
.addr
= strtoulst (p
, &p
, 16);
481 mapping
.endaddr
= strtoulst (p
, &p
, 16);
484 const char *permissions_start
= p
;
485 while (*p
&& !isspace (*p
))
487 mapping
.permissions
= {permissions_start
, (size_t) (p
- permissions_start
)};
489 mapping
.offset
= strtoulst (p
, &p
, 16);
492 const char *device_start
= p
;
493 while (*p
&& !isspace (*p
))
495 mapping
.device
= {device_start
, (size_t) (p
- device_start
)};
497 mapping
.inode
= strtoulst (p
, &p
, 10);
500 mapping
.filename
= p
;
505 /* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
507 This function was based on the documentation found on
508 <Documentation/filesystems/proc.txt>, on the Linux kernel.
510 Linux kernels before commit
511 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
515 decode_vmflags (char *p
, struct smaps_vmflags
*v
)
517 char *saveptr
= NULL
;
520 v
->initialized_p
= 1;
521 p
= skip_to_space (p
);
524 for (s
= strtok_r (p
, " ", &saveptr
);
526 s
= strtok_r (NULL
, " ", &saveptr
))
528 if (strcmp (s
, "io") == 0)
530 else if (strcmp (s
, "ht") == 0)
531 v
->uses_huge_tlb
= 1;
532 else if (strcmp (s
, "dd") == 0)
533 v
->exclude_coredump
= 1;
534 else if (strcmp (s
, "sh") == 0)
535 v
->shared_mapping
= 1;
536 else if (strcmp (s
, "mt") == 0)
537 v
->memory_tagging
= 1;
541 /* Regexes used by mapping_is_anonymous_p. Put in a structure because
542 they're initialized lazily. */
544 struct mapping_regexes
546 /* Matches "/dev/zero" filenames (with or without the "(deleted)"
547 string in the end). We know for sure, based on the Linux kernel
548 code, that memory mappings whose associated filename is
549 "/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
550 compiled_regex dev_zero
551 {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB
,
552 _("Could not compile regex to match /dev/zero filename")};
554 /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
555 string in the end). These filenames refer to shared memory
556 (shmem), and memory mappings associated with them are
557 MAP_ANONYMOUS as well. */
558 compiled_regex shmem_file
559 {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB
,
560 _("Could not compile regex to match shmem filenames")};
562 /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
563 0' code, which is responsible to decide if it is dealing with a
564 'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
565 FILE_DELETED matches, it does not necessarily mean that we are
566 dealing with an anonymous shared mapping. However, there is no
567 easy way to detect this currently, so this is the best
568 approximation we have.
570 As a result, GDB will dump readonly pages of deleted executables
571 when using the default value of coredump_filter (0x33), while the
572 Linux kernel will not dump those pages. But we can live with
574 compiled_regex file_deleted
575 {" (deleted)$", REG_NOSUB
,
576 _("Could not compile regex to match '<file> (deleted)'")};
579 /* Return 1 if the memory mapping is anonymous, 0 otherwise.
581 FILENAME is the name of the file present in the first line of the
582 memory mapping, in the "/proc/PID/smaps" output. For example, if
585 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
587 Then FILENAME will be "/path/to/file". */
590 mapping_is_anonymous_p (const char *filename
)
592 static std::optional
<mapping_regexes
> regexes
;
593 static int init_regex_p
= 0;
597 /* Let's be pessimistic and assume there will be an error while
598 compiling the regex'es. */
603 /* If we reached this point, then everything succeeded. */
607 if (init_regex_p
== -1)
609 const char deleted
[] = " (deleted)";
610 size_t del_len
= sizeof (deleted
) - 1;
611 size_t filename_len
= strlen (filename
);
613 /* There was an error while compiling the regex'es above. In
614 order to try to give some reliable information to the caller,
615 we just try to find the string " (deleted)" in the filename.
616 If we managed to find it, then we assume the mapping is
618 return (filename_len
>= del_len
619 && strcmp (filename
+ filename_len
- del_len
, deleted
) == 0);
622 if (*filename
== '\0'
623 || regexes
->dev_zero
.exec (filename
, 0, NULL
, 0) == 0
624 || regexes
->shmem_file
.exec (filename
, 0, NULL
, 0) == 0
625 || regexes
->file_deleted
.exec (filename
, 0, NULL
, 0) == 0)
631 /* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
632 MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
633 be dumped, or greater than 0 if it should.
635 In a nutshell, this is the logic that we follow in order to decide
636 if a mapping should be dumped or not.
638 - If the mapping is associated to a file whose name ends with
639 " (deleted)", or if the file is "/dev/zero", or if it is
640 "/SYSV%08x" (shared memory), or if there is no file associated
641 with it, or if the AnonHugePages: or the Anonymous: fields in the
642 /proc/PID/smaps have contents, then GDB considers this mapping to
643 be anonymous. Otherwise, GDB considers this mapping to be a
644 file-backed mapping (because there will be a file associated with
647 It is worth mentioning that, from all those checks described
648 above, the most fragile is the one to see if the file name ends
649 with " (deleted)". This does not necessarily mean that the
650 mapping is anonymous, because the deleted file associated with
651 the mapping may have been a hard link to another file, for
652 example. The Linux kernel checks to see if "i_nlink == 0", but
653 GDB cannot easily (and normally) do this check (iff running as
654 root, it could find the mapping in /proc/PID/map_files/ and
655 determine whether there still are other hard links to the
656 inode/file). Therefore, we made a compromise here, and we assume
657 that if the file name ends with " (deleted)", then the mapping is
658 indeed anonymous. FWIW, this is something the Linux kernel could
659 do better: expose this information in a more direct way.
661 - If we see the flag "sh" in the "VmFlags:" field (in
662 /proc/PID/smaps), then certainly the memory mapping is shared
663 (VM_SHARED). If we have access to the VmFlags, and we don't see
664 the "sh" there, then certainly the mapping is private. However,
665 Linux kernels before commit
666 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
667 "VmFlags:" field; in that case, we use another heuristic: if we
668 see 'p' in the permission flags, then we assume that the mapping
669 is private, even though the presence of the 's' flag there would
670 mean VM_MAYSHARE, which means the mapping could still be private.
671 This should work OK enough, however.
673 - Even if, at the end, we decided that we should not dump the
674 mapping, we still have to check if it is something like an ELF
675 header (of a DSO or an executable, for example). If it is, and
676 if the user is interested in dump it, then we should dump it. */
679 dump_mapping_p (filter_flags filterflags
, const struct smaps_vmflags
*v
,
680 int maybe_private_p
, int mapping_anon_p
, int mapping_file_p
,
681 const char *filename
, ULONGEST addr
, ULONGEST offset
)
683 /* Initially, we trust in what we received from our caller. This
684 value may not be very precise (i.e., it was probably gathered
685 from the permission line in the /proc/PID/smaps list, which
686 actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
687 what we have until we take a look at the "VmFlags:" field
688 (assuming that the version of the Linux kernel being used
689 supports it, of course). */
690 int private_p
= maybe_private_p
;
693 /* We always dump vDSO and vsyscall mappings, because it's likely that
694 there'll be no file to read the contents from at core load time.
695 The kernel does the same. */
696 if (strcmp ("[vdso]", filename
) == 0
697 || strcmp ("[vsyscall]", filename
) == 0)
700 if (v
->initialized_p
)
702 /* We never dump I/O mappings. */
706 /* Check if we should exclude this mapping. */
707 if (!dump_excluded_mappings
&& v
->exclude_coredump
)
710 /* Update our notion of whether this mapping is shared or
711 private based on a trustworthy value. */
712 private_p
= !v
->shared_mapping
;
714 /* HugeTLB checking. */
715 if (v
->uses_huge_tlb
)
717 if ((private_p
&& (filterflags
& COREFILTER_HUGETLB_PRIVATE
))
718 || (!private_p
&& (filterflags
& COREFILTER_HUGETLB_SHARED
)))
727 if (mapping_anon_p
&& mapping_file_p
)
729 /* This is a special situation. It can happen when we see a
730 mapping that is file-backed, but that contains anonymous
732 dump_p
= ((filterflags
& COREFILTER_ANON_PRIVATE
) != 0
733 || (filterflags
& COREFILTER_MAPPED_PRIVATE
) != 0);
735 else if (mapping_anon_p
)
736 dump_p
= (filterflags
& COREFILTER_ANON_PRIVATE
) != 0;
738 dump_p
= (filterflags
& COREFILTER_MAPPED_PRIVATE
) != 0;
742 if (mapping_anon_p
&& mapping_file_p
)
744 /* This is a special situation. It can happen when we see a
745 mapping that is file-backed, but that contains anonymous
747 dump_p
= ((filterflags
& COREFILTER_ANON_SHARED
) != 0
748 || (filterflags
& COREFILTER_MAPPED_SHARED
) != 0);
750 else if (mapping_anon_p
)
751 dump_p
= (filterflags
& COREFILTER_ANON_SHARED
) != 0;
753 dump_p
= (filterflags
& COREFILTER_MAPPED_SHARED
) != 0;
756 /* Even if we decided that we shouldn't dump this mapping, we still
757 have to check whether (a) the user wants us to dump mappings
758 containing an ELF header, and (b) the mapping in question
759 contains an ELF header. If (a) and (b) are true, then we should
762 A mapping contains an ELF header if it is a private mapping, its
763 offset is zero, and its first word is ELFMAG. */
764 if (!dump_p
&& private_p
&& offset
== 0
765 && (filterflags
& COREFILTER_ELF_HEADERS
) != 0)
767 /* Useful define specifying the size of the ELF magical
773 /* Let's check if we have an ELF header. */
775 if (target_read_memory (addr
, h
, SELFMAG
) == 0)
777 /* The EI_MAG* and ELFMAG* constants come from
779 if (h
[EI_MAG0
] == ELFMAG0
&& h
[EI_MAG1
] == ELFMAG1
780 && h
[EI_MAG2
] == ELFMAG2
&& h
[EI_MAG3
] == ELFMAG3
)
782 /* This mapping contains an ELF header, so we
792 /* As above, but return true only when we should dump the NT_FILE
796 dump_note_entry_p (filter_flags filterflags
, const struct smaps_vmflags
*v
,
797 int maybe_private_p
, int mapping_anon_p
, int mapping_file_p
,
798 const char *filename
, ULONGEST addr
, ULONGEST offset
)
800 /* vDSO and vsyscall mappings will end up in the core file. Don't
801 put them in the NT_FILE note. */
802 if (strcmp ("[vdso]", filename
) == 0
803 || strcmp ("[vsyscall]", filename
) == 0)
806 /* Otherwise, any other file-based mapping should be placed in the
811 /* Implement the "info proc" command. */
814 linux_info_proc (struct gdbarch
*gdbarch
, const char *args
,
815 enum info_proc_what what
)
817 /* A long is used for pid instead of an int to avoid a loss of precision
818 compiler warning from the output of strtoul. */
820 int cmdline_f
= (what
== IP_MINIMAL
|| what
== IP_CMDLINE
|| what
== IP_ALL
);
821 int cwd_f
= (what
== IP_MINIMAL
|| what
== IP_CWD
|| what
== IP_ALL
);
822 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
823 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
824 int status_f
= (what
== IP_STATUS
|| what
== IP_ALL
);
825 int stat_f
= (what
== IP_STAT
|| what
== IP_ALL
);
827 fileio_error target_errno
;
829 if (args
&& isdigit (args
[0]))
833 pid
= strtoul (args
, &tem
, 10);
838 if (!target_has_execution ())
839 error (_("No current process: you must name one."));
840 if (current_inferior ()->fake_pid_p
)
841 error (_("Can't determine the current process's PID: you must name one."));
843 pid
= current_inferior ()->pid
;
846 args
= skip_spaces (args
);
848 error (_("Too many parameters: %s"), args
);
850 gdb_printf (_("process %ld\n"), pid
);
853 xsnprintf (filename
, sizeof filename
, "/proc/%ld/cmdline", pid
);
855 ssize_t len
= target_fileio_read_alloc (NULL
, filename
, &buffer
);
859 gdb::unique_xmalloc_ptr
<char> cmdline ((char *) buffer
);
862 for (pos
= 0; pos
< len
- 1; pos
++)
864 if (buffer
[pos
] == '\0')
867 buffer
[len
- 1] = '\0';
868 gdb_printf ("cmdline = '%s'\n", buffer
);
871 warning (_("unable to open /proc file '%s'"), filename
);
875 xsnprintf (filename
, sizeof filename
, "/proc/%ld/cwd", pid
);
876 std::optional
<std::string
> contents
877 = target_fileio_readlink (NULL
, filename
, &target_errno
);
878 if (contents
.has_value ())
879 gdb_printf ("cwd = '%s'\n", contents
->c_str ());
881 warning (_("unable to read link '%s'"), filename
);
885 xsnprintf (filename
, sizeof filename
, "/proc/%ld/exe", pid
);
886 std::optional
<std::string
> contents
887 = target_fileio_readlink (NULL
, filename
, &target_errno
);
888 if (contents
.has_value ())
889 gdb_printf ("exe = '%s'\n", contents
->c_str ());
891 warning (_("unable to read link '%s'"), filename
);
895 xsnprintf (filename
, sizeof filename
, "/proc/%ld/maps", pid
);
896 gdb::unique_xmalloc_ptr
<char> map
897 = target_fileio_read_stralloc (NULL
, filename
);
902 gdb_printf (_("Mapped address spaces:\n\n"));
903 if (gdbarch_addr_bit (gdbarch
) == 32)
905 gdb_printf ("\t%10s %10s %10s %10s %s %s\n",
906 "Start Addr", " End Addr", " Size",
907 " Offset", "Perms ", "objfile");
911 gdb_printf (" %18s %18s %10s %10s %s %s\n",
912 "Start Addr", " End Addr", " Size",
913 " Offset", "Perms ", "objfile");
917 for (line
= strtok_r (map
.get (), "\n", &saveptr
);
919 line
= strtok_r (NULL
, "\n", &saveptr
))
921 struct mapping m
= read_mapping (line
);
923 if (gdbarch_addr_bit (gdbarch
) == 32)
925 gdb_printf ("\t%10s %10s %10s %10s %-5.*s %s\n",
926 paddress (gdbarch
, m
.addr
),
927 paddress (gdbarch
, m
.endaddr
),
928 hex_string (m
.endaddr
- m
.addr
),
929 hex_string (m
.offset
),
930 (int) m
.permissions
.size (),
931 m
.permissions
.data (),
936 gdb_printf (" %18s %18s %10s %10s %-5.*s %s\n",
937 paddress (gdbarch
, m
.addr
),
938 paddress (gdbarch
, m
.endaddr
),
939 hex_string (m
.endaddr
- m
.addr
),
940 hex_string (m
.offset
),
941 (int) m
.permissions
.size (),
942 m
.permissions
.data (),
948 warning (_("unable to open /proc file '%s'"), filename
);
952 xsnprintf (filename
, sizeof filename
, "/proc/%ld/status", pid
);
953 gdb::unique_xmalloc_ptr
<char> status
954 = target_fileio_read_stralloc (NULL
, filename
);
956 gdb_puts (status
.get ());
958 warning (_("unable to open /proc file '%s'"), filename
);
962 xsnprintf (filename
, sizeof filename
, "/proc/%ld/stat", pid
);
963 gdb::unique_xmalloc_ptr
<char> statstr
964 = target_fileio_read_stralloc (NULL
, filename
);
967 const char *p
= statstr
.get ();
969 gdb_printf (_("Process: %s\n"),
970 pulongest (strtoulst (p
, &p
, 10)));
975 /* ps command also relies on no trailing fields
977 const char *ep
= strrchr (p
, ')');
980 gdb_printf ("Exec file: %.*s\n",
981 (int) (ep
- p
- 1), p
+ 1);
988 gdb_printf (_("State: %c\n"), *p
++);
991 gdb_printf (_("Parent process: %s\n"),
992 pulongest (strtoulst (p
, &p
, 10)));
994 gdb_printf (_("Process group: %s\n"),
995 pulongest (strtoulst (p
, &p
, 10)));
997 gdb_printf (_("Session id: %s\n"),
998 pulongest (strtoulst (p
, &p
, 10)));
1000 gdb_printf (_("TTY: %s\n"),
1001 pulongest (strtoulst (p
, &p
, 10)));
1003 gdb_printf (_("TTY owner process group: %s\n"),
1004 pulongest (strtoulst (p
, &p
, 10)));
1007 gdb_printf (_("Flags: %s\n"),
1008 hex_string (strtoulst (p
, &p
, 10)));
1010 gdb_printf (_("Minor faults (no memory page): %s\n"),
1011 pulongest (strtoulst (p
, &p
, 10)));
1013 gdb_printf (_("Minor faults, children: %s\n"),
1014 pulongest (strtoulst (p
, &p
, 10)));
1016 gdb_printf (_("Major faults (memory page faults): %s\n"),
1017 pulongest (strtoulst (p
, &p
, 10)));
1019 gdb_printf (_("Major faults, children: %s\n"),
1020 pulongest (strtoulst (p
, &p
, 10)));
1022 gdb_printf (_("utime: %s\n"),
1023 pulongest (strtoulst (p
, &p
, 10)));
1025 gdb_printf (_("stime: %s\n"),
1026 pulongest (strtoulst (p
, &p
, 10)));
1028 gdb_printf (_("utime, children: %s\n"),
1029 pulongest (strtoulst (p
, &p
, 10)));
1031 gdb_printf (_("stime, children: %s\n"),
1032 pulongest (strtoulst (p
, &p
, 10)));
1034 gdb_printf (_("jiffies remaining in current "
1035 "time slice: %s\n"),
1036 pulongest (strtoulst (p
, &p
, 10)));
1038 gdb_printf (_("'nice' value: %s\n"),
1039 pulongest (strtoulst (p
, &p
, 10)));
1041 gdb_printf (_("jiffies until next timeout: %s\n"),
1042 pulongest (strtoulst (p
, &p
, 10)));
1044 gdb_printf (_("jiffies until next SIGALRM: %s\n"),
1045 pulongest (strtoulst (p
, &p
, 10)));
1047 gdb_printf (_("start time (jiffies since "
1048 "system boot): %s\n"),
1049 pulongest (strtoulst (p
, &p
, 10)));
1051 gdb_printf (_("Virtual memory size: %s\n"),
1052 pulongest (strtoulst (p
, &p
, 10)));
1054 gdb_printf (_("Resident set size: %s\n"),
1055 pulongest (strtoulst (p
, &p
, 10)));
1057 gdb_printf (_("rlim: %s\n"),
1058 pulongest (strtoulst (p
, &p
, 10)));
1060 gdb_printf (_("Start of text: %s\n"),
1061 hex_string (strtoulst (p
, &p
, 10)));
1063 gdb_printf (_("End of text: %s\n"),
1064 hex_string (strtoulst (p
, &p
, 10)));
1066 gdb_printf (_("Start of stack: %s\n"),
1067 hex_string (strtoulst (p
, &p
, 10)));
1068 #if 0 /* Don't know how architecture-dependent the rest is...
1069 Anyway the signal bitmap info is available from "status". */
1071 gdb_printf (_("Kernel stack pointer: %s\n"),
1072 hex_string (strtoulst (p
, &p
, 10)));
1074 gdb_printf (_("Kernel instr pointer: %s\n"),
1075 hex_string (strtoulst (p
, &p
, 10)));
1077 gdb_printf (_("Pending signals bitmap: %s\n"),
1078 hex_string (strtoulst (p
, &p
, 10)));
1080 gdb_printf (_("Blocked signals bitmap: %s\n"),
1081 hex_string (strtoulst (p
, &p
, 10)));
1083 gdb_printf (_("Ignored signals bitmap: %s\n"),
1084 hex_string (strtoulst (p
, &p
, 10)));
1086 gdb_printf (_("Catched signals bitmap: %s\n"),
1087 hex_string (strtoulst (p
, &p
, 10)));
1089 gdb_printf (_("wchan (system call): %s\n"),
1090 hex_string (strtoulst (p
, &p
, 10)));
1094 warning (_("unable to open /proc file '%s'"), filename
);
1098 /* Implementation of `gdbarch_read_core_file_mappings', as defined in
1101 This function reads the NT_FILE note (which BFD turns into the
1102 section ".note.linuxcore.file"). The format of this note / section
1103 is described as follows in the Linux kernel sources in
1106 long count -- how many files are mapped
1107 long page_size -- units for file_ofs
1108 array of [COUNT] elements of
1112 followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
1114 CBFD is the BFD of the core file.
1116 PRE_LOOP_CB is the callback function to invoke prior to starting
1117 the loop which processes individual entries. This callback will
1118 only be executed after the note has been examined in enough
1119 detail to verify that it's not malformed in some way.
1121 LOOP_CB is the callback function that will be executed once
1122 for each mapping. */
1125 linux_read_core_file_mappings
1126 (struct gdbarch
*gdbarch
,
1128 read_core_file_mappings_pre_loop_ftype pre_loop_cb
,
1129 read_core_file_mappings_loop_ftype loop_cb
)
1131 /* Ensure that ULONGEST is big enough for reading 64-bit core files. */
1132 static_assert (sizeof (ULONGEST
) >= 8);
1134 /* It's not required that the NT_FILE note exists, so return silently
1135 if it's not found. Beyond this point though, we'll complain
1136 if problems are found. */
1137 asection
*section
= bfd_get_section_by_name (cbfd
, ".note.linuxcore.file");
1138 if (section
== nullptr)
1141 unsigned int addr_size_bits
= gdbarch_addr_bit (gdbarch
);
1142 unsigned int addr_size
= addr_size_bits
/ 8;
1143 size_t note_size
= bfd_section_size (section
);
1145 if (note_size
< 2 * addr_size
)
1147 warning (_("malformed core note - too short for header"));
1151 gdb::byte_vector
contents (note_size
);
1152 if (!bfd_get_section_contents (current_program_space
->core_bfd (), section
,
1153 contents
.data (), 0, note_size
))
1155 warning (_("could not get core note contents"));
1159 gdb_byte
*descdata
= contents
.data ();
1160 char *descend
= (char *) descdata
+ note_size
;
1162 if (descdata
[note_size
- 1] != '\0')
1164 warning (_("malformed note - does not end with \\0"));
1168 ULONGEST count
= bfd_get (addr_size_bits
, current_program_space
->core_bfd (),
1170 descdata
+= addr_size
;
1172 ULONGEST page_size
= bfd_get (addr_size_bits
,
1173 current_program_space
->core_bfd (),
1175 descdata
+= addr_size
;
1177 if (note_size
< 2 * addr_size
+ count
* 3 * addr_size
)
1179 warning (_("malformed note - too short for supplied file count"));
1183 char *filenames
= (char *) descdata
+ count
* 3 * addr_size
;
1185 /* Make sure that the correct number of filenames exist. Complain
1186 if there aren't enough or are too many. */
1187 char *f
= filenames
;
1188 for (int i
= 0; i
< count
; i
++)
1192 warning (_("malformed note - filename area is too small"));
1195 f
+= strnlen (f
, descend
- f
) + 1;
1197 /* Complain, but don't return early if the filename area is too big. */
1199 warning (_("malformed note - filename area is too big"));
1201 const bfd_build_id
*orig_build_id
= cbfd
->build_id
;
1202 std::unordered_map
<ULONGEST
, const bfd_build_id
*> vma_map
;
1204 /* Search for solib build-ids in the core file. Each time one is found,
1205 map the start vma of the corresponding elf header to the build-id. */
1206 for (bfd_section
*sec
= cbfd
->sections
; sec
!= nullptr; sec
= sec
->next
)
1208 cbfd
->build_id
= nullptr;
1210 if (sec
->flags
& SEC_LOAD
1211 && (get_elf_backend_data (cbfd
)->elf_backend_core_find_build_id
1212 (cbfd
, (bfd_vma
) sec
->filepos
)))
1213 vma_map
[sec
->vma
] = cbfd
->build_id
;
1216 cbfd
->build_id
= orig_build_id
;
1217 pre_loop_cb (count
);
1219 for (int i
= 0; i
< count
; i
++)
1221 ULONGEST start
= bfd_get (addr_size_bits
, current_program_space
->core_bfd (), descdata
);
1222 descdata
+= addr_size
;
1223 ULONGEST end
= bfd_get (addr_size_bits
, current_program_space
->core_bfd (), descdata
);
1224 descdata
+= addr_size
;
1226 = bfd_get (addr_size_bits
, current_program_space
->core_bfd (), descdata
) * page_size
;
1227 descdata
+= addr_size
;
1228 char * filename
= filenames
;
1229 filenames
+= strlen ((char *) filenames
) + 1;
1230 const bfd_build_id
*build_id
= nullptr;
1231 auto vma_map_it
= vma_map
.find (start
);
1233 if (vma_map_it
!= vma_map
.end ())
1234 build_id
= vma_map_it
->second
;
1236 loop_cb (i
, start
, end
, file_ofs
, filename
, build_id
);
1240 /* Implement "info proc mappings" for a corefile. */
1243 linux_core_info_proc_mappings (struct gdbarch
*gdbarch
, const char *args
)
1245 linux_read_core_file_mappings (gdbarch
, current_program_space
->core_bfd (),
1246 [=] (ULONGEST count
)
1248 gdb_printf (_("Mapped address spaces:\n\n"));
1249 if (gdbarch_addr_bit (gdbarch
) == 32)
1251 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1254 " Size", " Offset", "objfile");
1258 gdb_printf (" %18s %18s %10s %10s %s\n",
1261 " Size", " Offset", "objfile");
1264 [=] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
1265 const char *filename
, const bfd_build_id
*build_id
)
1267 if (gdbarch_addr_bit (gdbarch
) == 32)
1268 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1269 paddress (gdbarch
, start
),
1270 paddress (gdbarch
, end
),
1271 hex_string (end
- start
),
1272 hex_string (file_ofs
),
1275 gdb_printf (" %18s %18s %10s %10s %s\n",
1276 paddress (gdbarch
, start
),
1277 paddress (gdbarch
, end
),
1278 hex_string (end
- start
),
1279 hex_string (file_ofs
),
1284 /* Implement "info proc" for a corefile. */
1287 linux_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
1288 enum info_proc_what what
)
1290 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
1291 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
1296 = bfd_core_file_failing_command (current_program_space
->core_bfd ());
1299 gdb_printf ("exe = '%s'\n", exe
);
1301 warning (_("unable to find command name in core file"));
1305 linux_core_info_proc_mappings (gdbarch
, args
);
1307 if (!exe_f
&& !mappings_f
)
1308 error (_("unable to handle request"));
1311 /* Read siginfo data from the core, if possible. Returns -1 on
1312 failure. Otherwise, returns the number of bytes read. READBUF,
1313 OFFSET, and LEN are all as specified by the to_xfer_partial
1317 linux_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
1318 ULONGEST offset
, ULONGEST len
)
1320 thread_section_name
section_name (".note.linuxcore.siginfo", inferior_ptid
);
1322 = bfd_get_section_by_name (current_program_space
->core_bfd (),
1323 section_name
.c_str ());
1324 if (section
== NULL
)
1327 if (!bfd_get_section_contents (current_program_space
->core_bfd (), section
,
1328 readbuf
, offset
, len
))
1334 typedef int linux_find_memory_region_ftype (ULONGEST vaddr
, ULONGEST size
,
1335 ULONGEST offset
, ULONGEST inode
,
1336 int read
, int write
,
1337 int exec
, int modified
,
1339 const char *filename
,
1342 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags
,
1343 const struct smaps_vmflags
*v
,
1344 int maybe_private_p
,
1347 const char *filename
,
1351 /* Helper function to parse the contents of /proc/<pid>/smaps into a data
1352 structure, for easy access.
1354 DATA is the contents of the smaps file. The parsed contents are stored
1355 into the SMAPS vector. */
1357 static std::vector
<struct smaps_data
>
1358 parse_smaps_data (const char *data
,
1359 const std::string maps_filename
)
1363 gdb_assert (data
!= nullptr);
1365 line
= strtok_r ((char *) data
, "\n", &t
);
1367 std::vector
<struct smaps_data
> smaps
;
1369 while (line
!= NULL
)
1371 struct smaps_vmflags v
;
1372 int read
, write
, exec
, priv
;
1373 int has_anonymous
= 0;
1377 memset (&v
, 0, sizeof (v
));
1378 struct mapping m
= read_mapping (line
);
1379 mapping_anon_p
= mapping_is_anonymous_p (m
.filename
);
1380 /* If the mapping is not anonymous, then we can consider it
1381 to be file-backed. These two states (anonymous or
1382 file-backed) seem to be exclusive, but they can actually
1383 coexist. For example, if a file-backed mapping has
1384 "Anonymous:" pages (see more below), then the Linux
1385 kernel will dump this mapping when the user specified
1386 that she only wants anonymous mappings in the corefile
1387 (*even* when she explicitly disabled the dumping of
1388 file-backed mappings). */
1389 mapping_file_p
= !mapping_anon_p
;
1391 /* Decode permissions. */
1392 auto has_perm
= [&m
] (char c
)
1393 { return m
.permissions
.find (c
) != std::string_view::npos
; };
1394 read
= has_perm ('r');
1395 write
= has_perm ('w');
1396 exec
= has_perm ('x');
1398 /* 'private' here actually means VM_MAYSHARE, and not
1399 VM_SHARED. In order to know if a mapping is really
1400 private or not, we must check the flag "sh" in the
1401 VmFlags field. This is done by decode_vmflags. However,
1402 if we are using a Linux kernel released before the commit
1403 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
1404 not have the VmFlags there. In this case, there is
1405 really no way to know if we are dealing with VM_SHARED,
1406 so we just assume that VM_MAYSHARE is enough. */
1407 priv
= has_perm ('p');
1409 /* Try to detect if region should be dumped by parsing smaps
1411 for (line
= strtok_r (NULL
, "\n", &t
);
1412 line
!= NULL
&& line
[0] >= 'A' && line
[0] <= 'Z';
1413 line
= strtok_r (NULL
, "\n", &t
))
1415 char keyword
[64 + 1];
1417 if (sscanf (line
, "%64s", keyword
) != 1)
1419 warning (_("Error parsing {s,}maps file '%s'"),
1420 maps_filename
.c_str ());
1424 if (strcmp (keyword
, "Anonymous:") == 0)
1426 /* Older Linux kernels did not support the
1427 "Anonymous:" counter. Check it here. */
1430 else if (strcmp (keyword
, "VmFlags:") == 0)
1431 decode_vmflags (line
, &v
);
1433 if (strcmp (keyword
, "AnonHugePages:") == 0
1434 || strcmp (keyword
, "Anonymous:") == 0)
1436 unsigned long number
;
1438 if (sscanf (line
, "%*s%lu", &number
) != 1)
1440 warning (_("Error parsing {s,}maps file '%s' number"),
1441 maps_filename
.c_str ());
1446 /* Even if we are dealing with a file-backed
1447 mapping, if it contains anonymous pages we
1448 consider it to be *also* an anonymous
1449 mapping, because this is what the Linux
1452 // Dump segments that have been written to.
1453 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1456 Note that if the mapping is already marked as
1457 file-backed (i.e., mapping_file_p is
1458 non-zero), then this is a special case, and
1459 this mapping will be dumped either when the
1460 user wants to dump file-backed *or* anonymous
1466 /* Save the smaps entry to the vector. */
1467 struct smaps_data map
;
1469 map
.start_address
= m
.addr
;
1470 map
.end_address
= m
.endaddr
;
1471 map
.filename
= m
.filename
;
1473 map
.read
= read
? true : false;
1474 map
.write
= write
? true : false;
1475 map
.exec
= exec
? true : false;
1476 map
.priv
= priv
? true : false;
1477 map
.has_anonymous
= has_anonymous
;
1478 map
.mapping_anon_p
= mapping_anon_p
? true : false;
1479 map
.mapping_file_p
= mapping_file_p
? true : false;
1480 map
.offset
= m
.offset
;
1481 map
.inode
= m
.inode
;
1483 smaps
.emplace_back (map
);
1489 /* Helper that checks if an address is in a memory tag page for a live
1493 linux_process_address_in_memtag_page (CORE_ADDR address
)
1495 if (current_inferior ()->fake_pid_p
)
1498 pid_t pid
= current_inferior ()->pid
;
1500 std::string smaps_file
= string_printf ("/proc/%d/smaps", pid
);
1502 gdb::unique_xmalloc_ptr
<char> data
1503 = target_fileio_read_stralloc (NULL
, smaps_file
.c_str ());
1505 if (data
== nullptr)
1508 /* Parse the contents of smaps into a vector. */
1509 std::vector
<struct smaps_data
> smaps
1510 = parse_smaps_data (data
.get (), smaps_file
);
1512 for (const smaps_data
&map
: smaps
)
1514 /* Is the address within [start_address, end_address) in a page
1515 mapped with memory tagging? */
1516 if (address
>= map
.start_address
1517 && address
< map
.end_address
1518 && map
.vmflags
.memory_tagging
)
1525 /* Helper that checks if an address is in a memory tag page for a core file
1529 linux_core_file_address_in_memtag_page (CORE_ADDR address
)
1531 if (current_program_space
->core_bfd () == nullptr)
1534 memtag_section_info info
;
1535 return get_next_core_memtag_section (current_program_space
->core_bfd (),
1536 nullptr, address
, info
);
1539 /* See linux-tdep.h. */
1542 linux_address_in_memtag_page (CORE_ADDR address
)
1544 if (!target_has_execution ())
1545 return linux_core_file_address_in_memtag_page (address
);
1547 return linux_process_address_in_memtag_page (address
);
1550 /* List memory regions in the inferior for a corefile. */
1553 linux_find_memory_regions_full (struct gdbarch
*gdbarch
,
1554 linux_dump_mapping_p_ftype
*should_dump_mapping_p
,
1555 linux_find_memory_region_ftype
*func
,
1559 /* Default dump behavior of coredump_filter (0x33), according to
1560 Documentation/filesystems/proc.txt from the Linux kernel
1562 filter_flags filterflags
= (COREFILTER_ANON_PRIVATE
1563 | COREFILTER_ANON_SHARED
1564 | COREFILTER_ELF_HEADERS
1565 | COREFILTER_HUGETLB_PRIVATE
);
1567 /* We need to know the real target PID to access /proc. */
1568 if (current_inferior ()->fake_pid_p
)
1571 pid
= current_inferior ()->pid
;
1573 if (use_coredump_filter
)
1575 std::string core_dump_filter_name
1576 = string_printf ("/proc/%d/coredump_filter", pid
);
1578 gdb::unique_xmalloc_ptr
<char> coredumpfilterdata
1579 = target_fileio_read_stralloc (NULL
, core_dump_filter_name
.c_str ());
1581 if (coredumpfilterdata
!= NULL
)
1585 sscanf (coredumpfilterdata
.get (), "%x", &flags
);
1586 filterflags
= (enum filter_flag
) flags
;
1590 std::string maps_filename
= string_printf ("/proc/%d/smaps", pid
);
1592 gdb::unique_xmalloc_ptr
<char> data
1593 = target_fileio_read_stralloc (NULL
, maps_filename
.c_str ());
1597 /* Older Linux kernels did not support /proc/PID/smaps. */
1598 maps_filename
= string_printf ("/proc/%d/maps", pid
);
1599 data
= target_fileio_read_stralloc (NULL
, maps_filename
.c_str ());
1601 if (data
== nullptr)
1605 /* Parse the contents of smaps into a vector. */
1606 std::vector
<struct smaps_data
> smaps
1607 = parse_smaps_data (data
.get (), maps_filename
.c_str ());
1609 for (const struct smaps_data
&map
: smaps
)
1611 int should_dump_p
= 0;
1613 if (map
.has_anonymous
)
1616 = should_dump_mapping_p (filterflags
, &map
.vmflags
,
1620 map
.filename
.c_str (),
1626 /* Older Linux kernels did not support the "Anonymous:" counter.
1627 If it is missing, we can't be sure - dump all the pages. */
1631 /* Invoke the callback function to create the corefile segment. */
1634 func (map
.start_address
, map
.end_address
- map
.start_address
,
1635 map
.offset
, map
.inode
, map
.read
, map
.write
, map
.exec
,
1636 1, /* MODIFIED is true because we want to dump
1638 map
.vmflags
.memory_tagging
!= 0,
1639 map
.filename
.c_str (), obfd
);
1646 /* A structure for passing information through
1647 linux_find_memory_regions_full. */
1649 struct linux_find_memory_regions_data
1651 /* The original callback. */
1653 find_memory_region_ftype func
;
1655 /* The original datum. */
1660 /* A callback for linux_find_memory_regions that converts between the
1661 "full"-style callback and find_memory_region_ftype. */
1664 linux_find_memory_regions_thunk (ULONGEST vaddr
, ULONGEST size
,
1665 ULONGEST offset
, ULONGEST inode
,
1666 int read
, int write
, int exec
, int modified
,
1668 const char *filename
, void *arg
)
1670 struct linux_find_memory_regions_data
*data
1671 = (struct linux_find_memory_regions_data
*) arg
;
1673 return data
->func (vaddr
, size
, read
, write
, exec
, modified
, memory_tagged
,
1677 /* A variant of linux_find_memory_regions_full that is suitable as the
1678 gdbarch find_memory_regions method. */
1681 linux_find_memory_regions (struct gdbarch
*gdbarch
,
1682 find_memory_region_ftype func
, void *obfd
)
1684 struct linux_find_memory_regions_data data
;
1689 return linux_find_memory_regions_full (gdbarch
,
1691 linux_find_memory_regions_thunk
,
1695 /* This is used to pass information from
1696 linux_make_mappings_corefile_notes through
1697 linux_find_memory_regions_full. */
1699 struct linux_make_mappings_data
1701 /* Number of files mapped. */
1702 ULONGEST file_count
;
1704 /* The obstack for the main part of the data. */
1705 struct obstack
*data_obstack
;
1707 /* The filename obstack. */
1708 struct obstack
*filename_obstack
;
1710 /* The architecture's "long" type. */
1711 struct type
*long_type
;
1714 static linux_find_memory_region_ftype linux_make_mappings_callback
;
1716 /* A callback for linux_find_memory_regions_full that updates the
1717 mappings data for linux_make_mappings_corefile_notes.
1719 MEMORY_TAGGED is true if the memory region contains memory tags, false
1723 linux_make_mappings_callback (ULONGEST vaddr
, ULONGEST size
,
1724 ULONGEST offset
, ULONGEST inode
,
1725 int read
, int write
, int exec
, int modified
,
1727 const char *filename
, void *data
)
1729 struct linux_make_mappings_data
*map_data
1730 = (struct linux_make_mappings_data
*) data
;
1731 gdb_byte buf
[sizeof (ULONGEST
)];
1733 if (*filename
== '\0' || inode
== 0)
1736 ++map_data
->file_count
;
1738 pack_long (buf
, map_data
->long_type
, vaddr
);
1739 obstack_grow (map_data
->data_obstack
, buf
, map_data
->long_type
->length ());
1740 pack_long (buf
, map_data
->long_type
, vaddr
+ size
);
1741 obstack_grow (map_data
->data_obstack
, buf
, map_data
->long_type
->length ());
1742 pack_long (buf
, map_data
->long_type
, offset
);
1743 obstack_grow (map_data
->data_obstack
, buf
, map_data
->long_type
->length ());
1745 obstack_grow_str0 (map_data
->filename_obstack
, filename
);
1750 /* Write the file mapping data to the core file, if possible. OBFD is
1751 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1752 is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
1755 linux_make_mappings_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
,
1756 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
1759 struct linux_make_mappings_data mapping_data
;
1760 type_allocator
alloc (gdbarch
);
1761 struct type
*long_type
1762 = init_integer_type (alloc
, gdbarch_long_bit (gdbarch
), 0, "long");
1763 gdb_byte buf
[sizeof (ULONGEST
)];
1765 auto_obstack data_obstack
, filename_obstack
;
1767 mapping_data
.file_count
= 0;
1768 mapping_data
.data_obstack
= &data_obstack
;
1769 mapping_data
.filename_obstack
= &filename_obstack
;
1770 mapping_data
.long_type
= long_type
;
1772 /* Reserve space for the count. */
1773 obstack_blank (&data_obstack
, long_type
->length ());
1774 /* We always write the page size as 1 since we have no good way to
1775 determine the correct value. */
1776 pack_long (buf
, long_type
, 1);
1777 obstack_grow (&data_obstack
, buf
, long_type
->length ());
1779 linux_find_memory_regions_full (gdbarch
,
1781 linux_make_mappings_callback
,
1784 if (mapping_data
.file_count
!= 0)
1786 /* Write the count to the obstack. */
1787 pack_long ((gdb_byte
*) obstack_base (&data_obstack
),
1788 long_type
, mapping_data
.file_count
);
1790 /* Copy the filenames to the data obstack. */
1791 int size
= obstack_object_size (&filename_obstack
);
1792 obstack_grow (&data_obstack
, obstack_base (&filename_obstack
),
1795 note_data
.reset (elfcore_write_file_note (obfd
, note_data
.release (), note_size
,
1796 obstack_base (&data_obstack
),
1797 obstack_object_size (&data_obstack
)));
1801 /* Fetch the siginfo data for the specified thread, if it exists. If
1802 there is no data, or we could not read it, return an empty
1805 static gdb::byte_vector
1806 linux_get_siginfo_data (thread_info
*thread
, struct gdbarch
*gdbarch
)
1808 struct type
*siginfo_type
;
1811 if (!gdbarch_get_siginfo_type_p (gdbarch
))
1812 return gdb::byte_vector ();
1814 scoped_restore_current_thread save_current_thread
;
1815 switch_to_thread (thread
);
1817 siginfo_type
= gdbarch_get_siginfo_type (gdbarch
);
1819 gdb::byte_vector
buf (siginfo_type
->length ());
1821 bytes_read
= target_read (current_inferior ()->top_target (),
1822 TARGET_OBJECT_SIGNAL_INFO
, NULL
,
1823 buf
.data (), 0, siginfo_type
->length ());
1824 if (bytes_read
!= siginfo_type
->length ())
1830 /* Records the thread's register state for the corefile note
1834 linux_corefile_thread (struct thread_info
*info
,
1835 struct gdbarch
*gdbarch
, bfd
*obfd
,
1836 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
1837 int *note_size
, gdb_signal stop_signal
)
1839 gcore_elf_build_thread_register_notes (gdbarch
, info
, stop_signal
, obfd
,
1840 ¬e_data
, note_size
);
1842 /* Don't return anything if we got no register information above,
1843 such a core file is useless. */
1844 if (note_data
!= nullptr)
1846 gdb::byte_vector siginfo_data
1847 = linux_get_siginfo_data (info
, gdbarch
);
1848 if (!siginfo_data
.empty ())
1849 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
1850 note_size
, "CORE", NT_SIGINFO
,
1851 siginfo_data
.data (),
1852 siginfo_data
.size ()));
1856 /* Fill the PRPSINFO structure with information about the process being
1857 debugged. Returns 1 in case of success, 0 for failures. Please note that
1858 even if the structure cannot be entirely filled (e.g., GDB was unable to
1859 gather information about the process UID/GID), this function will still
1860 return 1 since some information was already recorded. It will only return
1861 0 iff nothing can be gathered. */
1864 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo
*p
)
1866 /* The filename which we will use to obtain some info about the process.
1867 We will basically use this to store the `/proc/PID/FILENAME' file. */
1869 /* The basename of the executable. */
1870 const char *basename
;
1871 /* Temporary buffer. */
1873 /* The valid states of a process, according to the Linux kernel. */
1874 const char valid_states
[] = "RSDTZW";
1875 /* The program state. */
1876 const char *prog_state
;
1877 /* The state of the process. */
1879 /* The PID of the program which generated the corefile. */
1881 /* Process flags. */
1882 unsigned int pr_flag
;
1883 /* Process nice value. */
1885 /* The number of fields read by `sscanf'. */
1888 gdb_assert (p
!= NULL
);
1890 /* Obtaining PID and filename. */
1891 pid
= inferior_ptid
.pid ();
1892 xsnprintf (filename
, sizeof (filename
), "/proc/%d/cmdline", (int) pid
);
1893 /* The full name of the program which generated the corefile. */
1894 gdb_byte
*buf
= NULL
;
1895 size_t buf_len
= target_fileio_read_alloc (NULL
, filename
, &buf
);
1896 gdb::unique_xmalloc_ptr
<char> fname ((char *)buf
);
1898 if (buf_len
< 1 || fname
.get ()[0] == '\0')
1900 /* No program name was read, so we won't be able to retrieve more
1901 information about the process. */
1904 if (fname
.get ()[buf_len
- 1] != '\0')
1906 warning (_("target file %s "
1907 "does not contain a trailing null character"),
1912 memset (p
, 0, sizeof (*p
));
1914 /* Defining the PID. */
1917 /* Copying the program name. Only the basename matters. */
1918 basename
= lbasename (fname
.get ());
1919 strncpy (p
->pr_fname
, basename
, sizeof (p
->pr_fname
) - 1);
1920 p
->pr_fname
[sizeof (p
->pr_fname
) - 1] = '\0';
1922 const std::string
&infargs
= current_inferior ()->args ();
1924 /* The arguments of the program. */
1925 std::string psargs
= fname
.get ();
1926 if (!infargs
.empty ())
1927 psargs
+= ' ' + infargs
;
1929 strncpy (p
->pr_psargs
, psargs
.c_str (), sizeof (p
->pr_psargs
) - 1);
1930 p
->pr_psargs
[sizeof (p
->pr_psargs
) - 1] = '\0';
1932 xsnprintf (filename
, sizeof (filename
), "/proc/%d/stat", (int) pid
);
1933 /* The contents of `/proc/PID/stat'. */
1934 gdb::unique_xmalloc_ptr
<char> proc_stat_contents
1935 = target_fileio_read_stralloc (NULL
, filename
);
1936 char *proc_stat
= proc_stat_contents
.get ();
1938 if (proc_stat
== NULL
|| *proc_stat
== '\0')
1940 /* Despite being unable to read more information about the
1941 process, we return 1 here because at least we have its
1942 command line, PID and arguments. */
1946 /* Ok, we have the stats. It's time to do a little parsing of the
1947 contents of the buffer, so that we end up reading what we want.
1949 The following parsing mechanism is strongly based on the
1950 information generated by the `fs/proc/array.c' file, present in
1951 the Linux kernel tree. More details about how the information is
1952 displayed can be obtained by seeing the manpage of proc(5),
1953 specifically under the entry of `/proc/[pid]/stat'. */
1955 /* Getting rid of the PID, since we already have it. */
1956 while (isdigit (*proc_stat
))
1959 proc_stat
= skip_spaces (proc_stat
);
1961 /* ps command also relies on no trailing fields ever contain ')'. */
1962 proc_stat
= strrchr (proc_stat
, ')');
1963 if (proc_stat
== NULL
)
1967 proc_stat
= skip_spaces (proc_stat
);
1969 n_fields
= sscanf (proc_stat
,
1970 "%c" /* Process state. */
1971 "%d%d%d" /* Parent PID, group ID, session ID. */
1972 "%*d%*d" /* tty_nr, tpgid (not used). */
1974 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1975 cmajflt (not used). */
1976 "%*s%*s%*s%*s" /* utime, stime, cutime,
1977 cstime (not used). */
1978 "%*s" /* Priority (not used). */
1981 &p
->pr_ppid
, &p
->pr_pgrp
, &p
->pr_sid
,
1987 /* Again, we couldn't read the complementary information about
1988 the process state. However, we already have minimal
1989 information, so we just return 1 here. */
1993 /* Filling the structure fields. */
1994 prog_state
= strchr (valid_states
, pr_sname
);
1995 if (prog_state
!= NULL
)
1996 p
->pr_state
= prog_state
- valid_states
;
1999 /* Zero means "Running". */
2003 p
->pr_sname
= p
->pr_state
> 5 ? '.' : pr_sname
;
2004 p
->pr_zomb
= p
->pr_sname
== 'Z';
2005 p
->pr_nice
= pr_nice
;
2006 p
->pr_flag
= pr_flag
;
2008 /* Finally, obtaining the UID and GID. For that, we read and parse the
2009 contents of the `/proc/PID/status' file. */
2010 xsnprintf (filename
, sizeof (filename
), "/proc/%d/status", (int) pid
);
2011 /* The contents of `/proc/PID/status'. */
2012 gdb::unique_xmalloc_ptr
<char> proc_status_contents
2013 = target_fileio_read_stralloc (NULL
, filename
);
2014 char *proc_status
= proc_status_contents
.get ();
2016 if (proc_status
== NULL
|| *proc_status
== '\0')
2018 /* Returning 1 since we already have a bunch of information. */
2022 /* Extracting the UID. */
2023 tmpstr
= strstr (proc_status
, "Uid:");
2026 /* Advancing the pointer to the beginning of the UID. */
2027 tmpstr
+= sizeof ("Uid:");
2028 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
2031 if (isdigit (*tmpstr
))
2032 p
->pr_uid
= strtol (tmpstr
, &tmpstr
, 10);
2035 /* Extracting the GID. */
2036 tmpstr
= strstr (proc_status
, "Gid:");
2039 /* Advancing the pointer to the beginning of the GID. */
2040 tmpstr
+= sizeof ("Gid:");
2041 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
2044 if (isdigit (*tmpstr
))
2045 p
->pr_gid
= strtol (tmpstr
, &tmpstr
, 10);
2051 /* Build the note section for a corefile, and return it in a malloc
2054 static gdb::unique_xmalloc_ptr
<char>
2055 linux_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
2057 struct elf_internal_linux_prpsinfo prpsinfo
;
2058 gdb::unique_xmalloc_ptr
<char> note_data
;
2060 if (! gdbarch_iterate_over_regset_sections_p (gdbarch
))
2063 if (linux_fill_prpsinfo (&prpsinfo
))
2065 if (gdbarch_ptr_bit (gdbarch
) == 64)
2066 note_data
.reset (elfcore_write_linux_prpsinfo64 (obfd
,
2067 note_data
.release (),
2068 note_size
, &prpsinfo
));
2070 note_data
.reset (elfcore_write_linux_prpsinfo32 (obfd
,
2071 note_data
.release (),
2072 note_size
, &prpsinfo
));
2075 /* Thread register information. */
2078 update_thread_list ();
2080 catch (const gdb_exception_error
&e
)
2082 exception_print (gdb_stderr
, e
);
2085 /* Like the kernel, prefer dumping the signalled thread first.
2086 "First thread" is what tools use to infer the signalled
2088 thread_info
*signalled_thr
= gcore_find_signalled_thread ();
2089 gdb_signal stop_signal
;
2090 if (signalled_thr
!= nullptr)
2091 stop_signal
= signalled_thr
->stop_signal ();
2093 stop_signal
= GDB_SIGNAL_0
;
2095 if (signalled_thr
!= nullptr)
2097 /* On some architectures, like AArch64, each thread can have a distinct
2098 gdbarch (due to scalable extensions), and using the inferior gdbarch
2101 Fetch each thread's gdbarch and pass it down to the lower layers so
2102 we can dump the right set of registers. */
2103 linux_corefile_thread (signalled_thr
,
2104 target_thread_architecture (signalled_thr
->ptid
),
2105 obfd
, note_data
, note_size
, stop_signal
);
2107 for (thread_info
*thr
: current_inferior ()->non_exited_threads ())
2109 if (thr
== signalled_thr
)
2112 /* On some architectures, like AArch64, each thread can have a distinct
2113 gdbarch (due to scalable extensions), and using the inferior gdbarch
2116 Fetch each thread's gdbarch and pass it down to the lower layers so
2117 we can dump the right set of registers. */
2118 linux_corefile_thread (thr
, target_thread_architecture (thr
->ptid
),
2119 obfd
, note_data
, note_size
, stop_signal
);
2125 /* Auxillary vector. */
2126 std::optional
<gdb::byte_vector
> auxv
=
2127 target_read_alloc (current_inferior ()->top_target (),
2128 TARGET_OBJECT_AUXV
, NULL
);
2129 if (auxv
&& !auxv
->empty ())
2131 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
2132 note_size
, "CORE", NT_AUXV
,
2133 auxv
->data (), auxv
->size ()));
2139 /* File mappings. */
2140 linux_make_mappings_corefile_notes (gdbarch
, obfd
, note_data
, note_size
);
2142 /* Include the target description when possible. Some architectures
2143 allow for per-thread gdbarch so we should really be emitting a tdesc
2144 per-thread, however, we don't currently support reading in a
2145 per-thread tdesc, so just emit the tdesc for the signalled thread. */
2146 gdbarch
= target_thread_architecture (signalled_thr
->ptid
);
2147 gcore_elf_make_tdesc_note (gdbarch
, obfd
, ¬e_data
, note_size
);
2152 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
2153 gdbarch.h. This function is not static because it is exported to
2154 other -tdep files. */
2157 linux_gdb_signal_from_target (struct gdbarch
*gdbarch
, int signal
)
2162 return GDB_SIGNAL_0
;
2165 return GDB_SIGNAL_HUP
;
2168 return GDB_SIGNAL_INT
;
2171 return GDB_SIGNAL_QUIT
;
2174 return GDB_SIGNAL_ILL
;
2177 return GDB_SIGNAL_TRAP
;
2180 return GDB_SIGNAL_ABRT
;
2183 return GDB_SIGNAL_BUS
;
2186 return GDB_SIGNAL_FPE
;
2189 return GDB_SIGNAL_KILL
;
2192 return GDB_SIGNAL_USR1
;
2195 return GDB_SIGNAL_SEGV
;
2198 return GDB_SIGNAL_USR2
;
2201 return GDB_SIGNAL_PIPE
;
2204 return GDB_SIGNAL_ALRM
;
2207 return GDB_SIGNAL_TERM
;
2210 return GDB_SIGNAL_CHLD
;
2213 return GDB_SIGNAL_CONT
;
2216 return GDB_SIGNAL_STOP
;
2219 return GDB_SIGNAL_TSTP
;
2222 return GDB_SIGNAL_TTIN
;
2225 return GDB_SIGNAL_TTOU
;
2228 return GDB_SIGNAL_URG
;
2231 return GDB_SIGNAL_XCPU
;
2234 return GDB_SIGNAL_XFSZ
;
2236 case LINUX_SIGVTALRM
:
2237 return GDB_SIGNAL_VTALRM
;
2240 return GDB_SIGNAL_PROF
;
2242 case LINUX_SIGWINCH
:
2243 return GDB_SIGNAL_WINCH
;
2245 /* No way to differentiate between SIGIO and SIGPOLL.
2246 Therefore, we just handle the first one. */
2248 return GDB_SIGNAL_IO
;
2251 return GDB_SIGNAL_PWR
;
2254 return GDB_SIGNAL_SYS
;
2256 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
2257 therefore we have to handle them here. */
2258 case LINUX_SIGRTMIN
:
2259 return GDB_SIGNAL_REALTIME_32
;
2261 case LINUX_SIGRTMAX
:
2262 return GDB_SIGNAL_REALTIME_64
;
2265 if (signal
>= LINUX_SIGRTMIN
+ 1 && signal
<= LINUX_SIGRTMAX
- 1)
2267 int offset
= signal
- LINUX_SIGRTMIN
+ 1;
2269 return (enum gdb_signal
) ((int) GDB_SIGNAL_REALTIME_33
+ offset
);
2272 return GDB_SIGNAL_UNKNOWN
;
2275 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
2276 gdbarch.h. This function is not static because it is exported to
2277 other -tdep files. */
2280 linux_gdb_signal_to_target (struct gdbarch
*gdbarch
,
2281 enum gdb_signal signal
)
2288 case GDB_SIGNAL_HUP
:
2289 return LINUX_SIGHUP
;
2291 case GDB_SIGNAL_INT
:
2292 return LINUX_SIGINT
;
2294 case GDB_SIGNAL_QUIT
:
2295 return LINUX_SIGQUIT
;
2297 case GDB_SIGNAL_ILL
:
2298 return LINUX_SIGILL
;
2300 case GDB_SIGNAL_TRAP
:
2301 return LINUX_SIGTRAP
;
2303 case GDB_SIGNAL_ABRT
:
2304 return LINUX_SIGABRT
;
2306 case GDB_SIGNAL_FPE
:
2307 return LINUX_SIGFPE
;
2309 case GDB_SIGNAL_KILL
:
2310 return LINUX_SIGKILL
;
2312 case GDB_SIGNAL_BUS
:
2313 return LINUX_SIGBUS
;
2315 case GDB_SIGNAL_SEGV
:
2316 return LINUX_SIGSEGV
;
2318 case GDB_SIGNAL_SYS
:
2319 return LINUX_SIGSYS
;
2321 case GDB_SIGNAL_PIPE
:
2322 return LINUX_SIGPIPE
;
2324 case GDB_SIGNAL_ALRM
:
2325 return LINUX_SIGALRM
;
2327 case GDB_SIGNAL_TERM
:
2328 return LINUX_SIGTERM
;
2330 case GDB_SIGNAL_URG
:
2331 return LINUX_SIGURG
;
2333 case GDB_SIGNAL_STOP
:
2334 return LINUX_SIGSTOP
;
2336 case GDB_SIGNAL_TSTP
:
2337 return LINUX_SIGTSTP
;
2339 case GDB_SIGNAL_CONT
:
2340 return LINUX_SIGCONT
;
2342 case GDB_SIGNAL_CHLD
:
2343 return LINUX_SIGCHLD
;
2345 case GDB_SIGNAL_TTIN
:
2346 return LINUX_SIGTTIN
;
2348 case GDB_SIGNAL_TTOU
:
2349 return LINUX_SIGTTOU
;
2354 case GDB_SIGNAL_XCPU
:
2355 return LINUX_SIGXCPU
;
2357 case GDB_SIGNAL_XFSZ
:
2358 return LINUX_SIGXFSZ
;
2360 case GDB_SIGNAL_VTALRM
:
2361 return LINUX_SIGVTALRM
;
2363 case GDB_SIGNAL_PROF
:
2364 return LINUX_SIGPROF
;
2366 case GDB_SIGNAL_WINCH
:
2367 return LINUX_SIGWINCH
;
2369 case GDB_SIGNAL_USR1
:
2370 return LINUX_SIGUSR1
;
2372 case GDB_SIGNAL_USR2
:
2373 return LINUX_SIGUSR2
;
2375 case GDB_SIGNAL_PWR
:
2376 return LINUX_SIGPWR
;
2378 case GDB_SIGNAL_POLL
:
2379 return LINUX_SIGPOLL
;
2381 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
2382 therefore we have to handle it here. */
2383 case GDB_SIGNAL_REALTIME_32
:
2384 return LINUX_SIGRTMIN
;
2386 /* Same comment applies to _64. */
2387 case GDB_SIGNAL_REALTIME_64
:
2388 return LINUX_SIGRTMAX
;
2391 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
2392 if (signal
>= GDB_SIGNAL_REALTIME_33
2393 && signal
<= GDB_SIGNAL_REALTIME_63
)
2395 int offset
= signal
- GDB_SIGNAL_REALTIME_33
;
2397 return LINUX_SIGRTMIN
+ 1 + offset
;
2403 /* Helper for linux_vsyscall_range that does the real work of finding
2404 the vsyscall's address range. */
2407 linux_vsyscall_range_raw (struct gdbarch
*gdbarch
, struct mem_range
*range
)
2412 if (target_auxv_search (AT_SYSINFO_EHDR
, &range
->start
) <= 0)
2415 /* It doesn't make sense to access the host's /proc when debugging a
2416 core file. Instead, look for the PT_LOAD segment that matches
2418 if (!target_has_execution ())
2424 = bfd_get_elf_phdr_upper_bound (current_program_space
->core_bfd ());
2425 if (phdrs_size
== -1)
2428 gdb::unique_xmalloc_ptr
<Elf_Internal_Phdr
>
2429 phdrs ((Elf_Internal_Phdr
*) xmalloc (phdrs_size
));
2430 num_phdrs
= bfd_get_elf_phdrs (current_program_space
->core_bfd (),
2432 if (num_phdrs
== -1)
2435 for (i
= 0; i
< num_phdrs
; i
++)
2436 if (phdrs
.get ()[i
].p_type
== PT_LOAD
2437 && phdrs
.get ()[i
].p_vaddr
== range
->start
)
2439 range
->length
= phdrs
.get ()[i
].p_memsz
;
2446 /* We need to know the real target PID to access /proc. */
2447 if (current_inferior ()->fake_pid_p
)
2450 pid
= current_inferior ()->pid
;
2452 /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
2453 reading /proc/PID/maps (2). The later identifies thread stacks
2454 in the output, which requires scanning every thread in the thread
2455 group to check whether a VMA is actually a thread's stack. With
2456 Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
2457 a few thousand threads, (1) takes a few miliseconds, while (2)
2458 takes several seconds. Also note that "smaps", what we read for
2459 determining core dump mappings, is even slower than "maps". */
2460 xsnprintf (filename
, sizeof filename
, "/proc/%ld/task/%ld/maps", pid
, pid
);
2461 gdb::unique_xmalloc_ptr
<char> data
2462 = target_fileio_read_stralloc (NULL
, filename
);
2466 char *saveptr
= NULL
;
2468 for (line
= strtok_r (data
.get (), "\n", &saveptr
);
2470 line
= strtok_r (NULL
, "\n", &saveptr
))
2472 ULONGEST addr
, endaddr
;
2473 const char *p
= line
;
2475 addr
= strtoulst (p
, &p
, 16);
2476 if (addr
== range
->start
)
2480 endaddr
= strtoulst (p
, &p
, 16);
2481 range
->length
= endaddr
- addr
;
2487 warning (_("unable to open /proc file '%s'"), filename
);
2492 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2493 caching, and defers the real work to linux_vsyscall_range_raw. */
2496 linux_vsyscall_range (struct gdbarch
*gdbarch
, struct mem_range
*range
)
2498 struct linux_info
*info
= get_linux_inferior_data (current_inferior ());
2500 if (info
->vsyscall_range_p
== 0)
2502 if (linux_vsyscall_range_raw (gdbarch
, &info
->vsyscall_range
))
2503 info
->vsyscall_range_p
= 1;
2505 info
->vsyscall_range_p
= -1;
2508 if (info
->vsyscall_range_p
< 0)
2511 *range
= info
->vsyscall_range
;
2515 /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
2516 definitions would be dependent on compilation host. */
2517 #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
2518 #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
2520 /* See gdbarch.sh 'infcall_mmap'. */
2523 linux_infcall_mmap (CORE_ADDR size
, unsigned prot
)
2525 struct objfile
*objf
;
2526 /* Do there still exist any Linux systems without "mmap64"?
2527 "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
2528 struct value
*mmap_val
= find_function_in_inferior ("mmap64", &objf
);
2529 struct value
*addr_val
;
2530 struct gdbarch
*gdbarch
= objf
->arch ();
2534 ARG_ADDR
, ARG_LENGTH
, ARG_PROT
, ARG_FLAGS
, ARG_FD
, ARG_OFFSET
, ARG_LAST
2536 struct value
*arg
[ARG_LAST
];
2538 arg
[ARG_ADDR
] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
,
2540 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2541 arg
[ARG_LENGTH
] = value_from_ulongest
2542 (builtin_type (gdbarch
)->builtin_unsigned_long
, size
);
2543 gdb_assert ((prot
& ~(GDB_MMAP_PROT_READ
| GDB_MMAP_PROT_WRITE
2544 | GDB_MMAP_PROT_EXEC
))
2546 arg
[ARG_PROT
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, prot
);
2547 arg
[ARG_FLAGS
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2548 GDB_MMAP_MAP_PRIVATE
2549 | GDB_MMAP_MAP_ANONYMOUS
);
2550 arg
[ARG_FD
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, -1);
2551 arg
[ARG_OFFSET
] = value_from_longest (builtin_type (gdbarch
)->builtin_int64
,
2553 addr_val
= call_function_by_hand (mmap_val
, NULL
, arg
);
2554 retval
= value_as_address (addr_val
);
2555 if (retval
== (CORE_ADDR
) -1)
2556 error (_("Failed inferior mmap call for %s bytes, errno is changed."),
2561 /* See gdbarch.sh 'infcall_munmap'. */
2564 linux_infcall_munmap (CORE_ADDR addr
, CORE_ADDR size
)
2566 struct objfile
*objf
;
2567 struct value
*munmap_val
= find_function_in_inferior ("munmap", &objf
);
2568 struct value
*retval_val
;
2569 struct gdbarch
*gdbarch
= objf
->arch ();
2573 ARG_ADDR
, ARG_LENGTH
, ARG_LAST
2575 struct value
*arg
[ARG_LAST
];
2577 arg
[ARG_ADDR
] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
,
2579 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2580 arg
[ARG_LENGTH
] = value_from_ulongest
2581 (builtin_type (gdbarch
)->builtin_unsigned_long
, size
);
2582 retval_val
= call_function_by_hand (munmap_val
, NULL
, arg
);
2583 retval
= value_as_long (retval_val
);
2585 warning (_("Failed inferior munmap call at %s for %s bytes, "
2586 "errno is changed."),
2587 hex_string (addr
), pulongest (size
));
2590 /* See linux-tdep.h. */
2593 linux_displaced_step_location (struct gdbarch
*gdbarch
)
2598 /* Determine entry point from target auxiliary vector. This avoids
2599 the need for symbols. Also, when debugging a stand-alone SPU
2600 executable, entry_point_address () will point to an SPU
2601 local-store address and is thus not usable as displaced stepping
2602 location. The auxiliary vector gets us the PowerPC-side entry
2603 point address instead. */
2604 if (target_auxv_search (AT_ENTRY
, &addr
) <= 0)
2605 throw_error (NOT_SUPPORTED_ERROR
,
2606 _("Cannot find AT_ENTRY auxiliary vector entry."));
2608 /* Make certain that the address points at real code, and not a
2609 function descriptor. */
2610 addr
= gdbarch_convert_from_func_ptr_addr
2611 (gdbarch
, addr
, current_inferior ()->top_target ());
2613 /* Inferior calls also use the entry point as a breakpoint location.
2614 We don't want displaced stepping to interfere with those
2615 breakpoints, so leave space. */
2616 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &bp_len
);
2622 /* See linux-tdep.h. */
2624 displaced_step_prepare_status
2625 linux_displaced_step_prepare (gdbarch
*arch
, thread_info
*thread
,
2626 CORE_ADDR
&displaced_pc
)
2628 linux_info
*per_inferior
= get_linux_inferior_data (thread
->inf
);
2630 if (!per_inferior
->disp_step_bufs
.has_value ())
2632 /* Figure out the location of the buffers. They are contiguous, starting
2633 at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
2634 CORE_ADDR disp_step_buf_addr
2635 = linux_displaced_step_location (thread
->inf
->arch ());
2636 int buf_len
= gdbarch_displaced_step_buffer_length (arch
);
2638 linux_gdbarch_data
*gdbarch_data
= get_linux_gdbarch_data (arch
);
2639 gdb_assert (gdbarch_data
->num_disp_step_buffers
> 0);
2641 std::vector
<CORE_ADDR
> buffers
;
2642 for (int i
= 0; i
< gdbarch_data
->num_disp_step_buffers
; i
++)
2643 buffers
.push_back (disp_step_buf_addr
+ i
* buf_len
);
2645 per_inferior
->disp_step_bufs
.emplace (buffers
);
2648 return per_inferior
->disp_step_bufs
->prepare (thread
, displaced_pc
);
2651 /* See linux-tdep.h. */
2653 displaced_step_finish_status
2654 linux_displaced_step_finish (gdbarch
*arch
, thread_info
*thread
,
2655 const target_waitstatus
&status
)
2657 linux_info
*per_inferior
= get_linux_inferior_data (thread
->inf
);
2659 gdb_assert (per_inferior
->disp_step_bufs
.has_value ());
2661 return per_inferior
->disp_step_bufs
->finish (arch
, thread
, status
);
2664 /* See linux-tdep.h. */
2666 const displaced_step_copy_insn_closure
*
2667 linux_displaced_step_copy_insn_closure_by_addr (inferior
*inf
, CORE_ADDR addr
)
2669 linux_info
*per_inferior
= linux_inferior_data
.get (inf
);
2671 if (per_inferior
== nullptr
2672 || !per_inferior
->disp_step_bufs
.has_value ())
2675 return per_inferior
->disp_step_bufs
->copy_insn_closure_by_addr (addr
);
2678 /* See linux-tdep.h. */
2681 linux_displaced_step_restore_all_in_ptid (inferior
*parent_inf
, ptid_t ptid
)
2683 linux_info
*per_inferior
= linux_inferior_data
.get (parent_inf
);
2685 if (per_inferior
== nullptr
2686 || !per_inferior
->disp_step_bufs
.has_value ())
2689 per_inferior
->disp_step_bufs
->restore_in_ptid (ptid
);
2692 /* Helper for linux_get_hwcap and linux_get_hwcap2. */
2695 linux_get_hwcap_helper (const std::optional
<gdb::byte_vector
> &auxv
,
2696 target_ops
*target
, gdbarch
*gdbarch
, CORE_ADDR match
)
2699 if (!auxv
.has_value ()
2700 || target_auxv_search (*auxv
, target
, gdbarch
, match
, &field
) != 1)
2705 /* See linux-tdep.h. */
2708 linux_get_hwcap (const std::optional
<gdb::byte_vector
> &auxv
,
2709 target_ops
*target
, gdbarch
*gdbarch
)
2711 return linux_get_hwcap_helper (auxv
, target
, gdbarch
, AT_HWCAP
);
2714 /* See linux-tdep.h. */
2719 return linux_get_hwcap (target_read_auxv (),
2720 current_inferior ()->top_target (),
2721 current_inferior ()->arch ());
2724 /* See linux-tdep.h. */
2727 linux_get_hwcap2 (const std::optional
<gdb::byte_vector
> &auxv
,
2728 target_ops
*target
, gdbarch
*gdbarch
)
2730 return linux_get_hwcap_helper (auxv
, target
, gdbarch
, AT_HWCAP2
);
2733 /* See linux-tdep.h. */
2738 return linux_get_hwcap2 (target_read_auxv (),
2739 current_inferior ()->top_target (),
2740 current_inferior ()->arch ());
2743 /* Display whether the gcore command is using the
2744 /proc/PID/coredump_filter file. */
2747 show_use_coredump_filter (struct ui_file
*file
, int from_tty
,
2748 struct cmd_list_element
*c
, const char *value
)
2750 gdb_printf (file
, _("Use of /proc/PID/coredump_filter file to generate"
2751 " corefiles is %s.\n"), value
);
2754 /* Display whether the gcore command is dumping mappings marked with
2755 the VM_DONTDUMP flag. */
2758 show_dump_excluded_mappings (struct ui_file
*file
, int from_tty
,
2759 struct cmd_list_element
*c
, const char *value
)
2761 gdb_printf (file
, _("Dumping of mappings marked with the VM_DONTDUMP"
2762 " flag is %s.\n"), value
);
2765 /* To be called from the various GDB_OSABI_LINUX handlers for the
2766 various GNU/Linux architectures and machine types.
2768 NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
2769 displaced stepping is not supported. */
2772 linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
,
2773 int num_disp_step_buffers
)
2775 if (num_disp_step_buffers
> 0)
2777 linux_gdbarch_data
*gdbarch_data
= get_linux_gdbarch_data (gdbarch
);
2778 gdbarch_data
->num_disp_step_buffers
= num_disp_step_buffers
;
2780 set_gdbarch_displaced_step_prepare (gdbarch
,
2781 linux_displaced_step_prepare
);
2782 set_gdbarch_displaced_step_finish (gdbarch
, linux_displaced_step_finish
);
2783 set_gdbarch_displaced_step_copy_insn_closure_by_addr
2784 (gdbarch
, linux_displaced_step_copy_insn_closure_by_addr
);
2785 set_gdbarch_displaced_step_restore_all_in_ptid
2786 (gdbarch
, linux_displaced_step_restore_all_in_ptid
);
2789 set_gdbarch_core_pid_to_str (gdbarch
, linux_core_pid_to_str
);
2790 set_gdbarch_info_proc (gdbarch
, linux_info_proc
);
2791 set_gdbarch_core_info_proc (gdbarch
, linux_core_info_proc
);
2792 set_gdbarch_core_xfer_siginfo (gdbarch
, linux_core_xfer_siginfo
);
2793 set_gdbarch_read_core_file_mappings (gdbarch
, linux_read_core_file_mappings
);
2794 set_gdbarch_find_memory_regions (gdbarch
, linux_find_memory_regions
);
2795 set_gdbarch_make_corefile_notes (gdbarch
, linux_make_corefile_notes
);
2796 set_gdbarch_has_shared_address_space (gdbarch
,
2797 linux_has_shared_address_space
);
2798 set_gdbarch_gdb_signal_from_target (gdbarch
,
2799 linux_gdb_signal_from_target
);
2800 set_gdbarch_gdb_signal_to_target (gdbarch
,
2801 linux_gdb_signal_to_target
);
2802 set_gdbarch_vsyscall_range (gdbarch
, linux_vsyscall_range
);
2803 set_gdbarch_infcall_mmap (gdbarch
, linux_infcall_mmap
);
2804 set_gdbarch_infcall_munmap (gdbarch
, linux_infcall_munmap
);
2805 set_gdbarch_get_siginfo_type (gdbarch
, linux_get_siginfo_type
);
2808 void _initialize_linux_tdep ();
2810 _initialize_linux_tdep ()
2812 /* Observers used to invalidate the cache when needed. */
2813 gdb::observers::inferior_exit
.attach (invalidate_linux_cache_inf
,
2815 gdb::observers::inferior_appeared
.attach (invalidate_linux_cache_inf
,
2817 gdb::observers::inferior_execd
.attach (linux_inferior_execd
,
2820 add_setshow_boolean_cmd ("use-coredump-filter", class_files
,
2821 &use_coredump_filter
, _("\
2822 Set whether gcore should consider /proc/PID/coredump_filter."),
2824 Show whether gcore should consider /proc/PID/coredump_filter."),
2826 Use this command to set whether gcore should consider the contents\n\
2827 of /proc/PID/coredump_filter when generating the corefile. For more information\n\
2828 about this file, refer to the manpage of core(5)."),
2829 NULL
, show_use_coredump_filter
,
2830 &setlist
, &showlist
);
2832 add_setshow_boolean_cmd ("dump-excluded-mappings", class_files
,
2833 &dump_excluded_mappings
, _("\
2834 Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2836 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2838 Use this command to set whether gcore should dump mappings marked with the\n\
2839 VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
2840 more information about this file, refer to the manpage of proc(5) and core(5)."),
2841 NULL
, show_dump_excluded_mappings
,
2842 &setlist
, &showlist
);
2845 /* Fetch (and possibly build) an appropriate `link_map_offsets' for
2846 ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
2849 linux_ilp32_fetch_link_map_offsets ()
2851 static link_map_offsets lmo
;
2852 static link_map_offsets
*lmp
= nullptr;
2858 lmo
.r_version_offset
= 0;
2859 lmo
.r_version_size
= 4;
2860 lmo
.r_map_offset
= 4;
2861 lmo
.r_brk_offset
= 8;
2862 lmo
.r_ldsomap_offset
= -1;
2863 lmo
.r_next_offset
= 20;
2865 /* Everything we need is in the first 20 bytes. */
2866 lmo
.link_map_size
= 20;
2867 lmo
.l_addr_offset
= 0;
2868 lmo
.l_name_offset
= 4;
2869 lmo
.l_ld_offset
= 8;
2870 lmo
.l_next_offset
= 12;
2871 lmo
.l_prev_offset
= 16;
2878 linux_lp64_fetch_link_map_offsets ()
2880 static link_map_offsets lmo
;
2881 static link_map_offsets
*lmp
= nullptr;
2887 lmo
.r_version_offset
= 0;
2888 lmo
.r_version_size
= 4;
2889 lmo
.r_map_offset
= 8;
2890 lmo
.r_brk_offset
= 16;
2891 lmo
.r_ldsomap_offset
= -1;
2892 lmo
.r_next_offset
= 40;
2894 /* Everything we need is in the first 40 bytes. */
2895 lmo
.link_map_size
= 40;
2896 lmo
.l_addr_offset
= 0;
2897 lmo
.l_name_offset
= 8;
2898 lmo
.l_ld_offset
= 16;
2899 lmo
.l_next_offset
= 24;
2900 lmo
.l_prev_offset
= 32;