1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2023 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"
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 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 gdb::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 gdb::string_view permissions
;
462 gdb::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 gdb::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 gdb::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 gdb::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 gdb_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 (core_bfd
, section
, contents
.data (),
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
, core_bfd
, descdata
);
1169 descdata
+= addr_size
;
1171 ULONGEST page_size
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1172 descdata
+= addr_size
;
1174 if (note_size
< 2 * addr_size
+ count
* 3 * addr_size
)
1176 warning (_("malformed note - too short for supplied file count"));
1180 char *filenames
= (char *) descdata
+ count
* 3 * addr_size
;
1182 /* Make sure that the correct number of filenames exist. Complain
1183 if there aren't enough or are too many. */
1184 char *f
= filenames
;
1185 for (int i
= 0; i
< count
; i
++)
1189 warning (_("malformed note - filename area is too small"));
1192 f
+= strnlen (f
, descend
- f
) + 1;
1194 /* Complain, but don't return early if the filename area is too big. */
1196 warning (_("malformed note - filename area is too big"));
1198 const bfd_build_id
*orig_build_id
= cbfd
->build_id
;
1199 std::unordered_map
<ULONGEST
, const bfd_build_id
*> vma_map
;
1201 /* Search for solib build-ids in the core file. Each time one is found,
1202 map the start vma of the corresponding elf header to the build-id. */
1203 for (bfd_section
*sec
= cbfd
->sections
; sec
!= nullptr; sec
= sec
->next
)
1205 cbfd
->build_id
= nullptr;
1207 if (sec
->flags
& SEC_LOAD
1208 && (get_elf_backend_data (cbfd
)->elf_backend_core_find_build_id
1209 (cbfd
, (bfd_vma
) sec
->filepos
)))
1210 vma_map
[sec
->vma
] = cbfd
->build_id
;
1213 cbfd
->build_id
= orig_build_id
;
1214 pre_loop_cb (count
);
1216 for (int i
= 0; i
< count
; i
++)
1218 ULONGEST start
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1219 descdata
+= addr_size
;
1220 ULONGEST end
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
1221 descdata
+= addr_size
;
1223 = bfd_get (addr_size_bits
, core_bfd
, descdata
) * page_size
;
1224 descdata
+= addr_size
;
1225 char * filename
= filenames
;
1226 filenames
+= strlen ((char *) filenames
) + 1;
1227 const bfd_build_id
*build_id
= nullptr;
1228 auto vma_map_it
= vma_map
.find (start
);
1230 if (vma_map_it
!= vma_map
.end ())
1231 build_id
= vma_map_it
->second
;
1233 loop_cb (i
, start
, end
, file_ofs
, filename
, build_id
);
1237 /* Implement "info proc mappings" for a corefile. */
1240 linux_core_info_proc_mappings (struct gdbarch
*gdbarch
, const char *args
)
1242 linux_read_core_file_mappings (gdbarch
, core_bfd
,
1243 [=] (ULONGEST count
)
1245 gdb_printf (_("Mapped address spaces:\n\n"));
1246 if (gdbarch_addr_bit (gdbarch
) == 32)
1248 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1251 " Size", " Offset", "objfile");
1255 gdb_printf (" %18s %18s %10s %10s %s\n",
1258 " Size", " Offset", "objfile");
1261 [=] (int num
, ULONGEST start
, ULONGEST end
, ULONGEST file_ofs
,
1262 const char *filename
, const bfd_build_id
*build_id
)
1264 if (gdbarch_addr_bit (gdbarch
) == 32)
1265 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1266 paddress (gdbarch
, start
),
1267 paddress (gdbarch
, end
),
1268 hex_string (end
- start
),
1269 hex_string (file_ofs
),
1272 gdb_printf (" %18s %18s %10s %10s %s\n",
1273 paddress (gdbarch
, start
),
1274 paddress (gdbarch
, end
),
1275 hex_string (end
- start
),
1276 hex_string (file_ofs
),
1281 /* Implement "info proc" for a corefile. */
1284 linux_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
1285 enum info_proc_what what
)
1287 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
1288 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
1294 exe
= bfd_core_file_failing_command (core_bfd
);
1296 gdb_printf ("exe = '%s'\n", exe
);
1298 warning (_("unable to find command name in core file"));
1302 linux_core_info_proc_mappings (gdbarch
, args
);
1304 if (!exe_f
&& !mappings_f
)
1305 error (_("unable to handle request"));
1308 /* Read siginfo data from the core, if possible. Returns -1 on
1309 failure. Otherwise, returns the number of bytes read. READBUF,
1310 OFFSET, and LEN are all as specified by the to_xfer_partial
1314 linux_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
1315 ULONGEST offset
, ULONGEST len
)
1317 thread_section_name
section_name (".note.linuxcore.siginfo", inferior_ptid
);
1318 asection
*section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
1319 if (section
== NULL
)
1322 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
, offset
, len
))
1328 typedef int linux_find_memory_region_ftype (ULONGEST vaddr
, ULONGEST size
,
1329 ULONGEST offset
, ULONGEST inode
,
1330 int read
, int write
,
1331 int exec
, int modified
,
1333 const char *filename
,
1336 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags
,
1337 const struct smaps_vmflags
*v
,
1338 int maybe_private_p
,
1341 const char *filename
,
1345 /* Helper function to parse the contents of /proc/<pid>/smaps into a data
1346 structure, for easy access.
1348 DATA is the contents of the smaps file. The parsed contents are stored
1349 into the SMAPS vector. */
1351 static std::vector
<struct smaps_data
>
1352 parse_smaps_data (const char *data
,
1353 const std::string maps_filename
)
1357 gdb_assert (data
!= nullptr);
1359 line
= strtok_r ((char *) data
, "\n", &t
);
1361 std::vector
<struct smaps_data
> smaps
;
1363 while (line
!= NULL
)
1365 struct smaps_vmflags v
;
1366 int read
, write
, exec
, priv
;
1367 int has_anonymous
= 0;
1371 memset (&v
, 0, sizeof (v
));
1372 struct mapping m
= read_mapping (line
);
1373 mapping_anon_p
= mapping_is_anonymous_p (m
.filename
);
1374 /* If the mapping is not anonymous, then we can consider it
1375 to be file-backed. These two states (anonymous or
1376 file-backed) seem to be exclusive, but they can actually
1377 coexist. For example, if a file-backed mapping has
1378 "Anonymous:" pages (see more below), then the Linux
1379 kernel will dump this mapping when the user specified
1380 that she only wants anonymous mappings in the corefile
1381 (*even* when she explicitly disabled the dumping of
1382 file-backed mappings). */
1383 mapping_file_p
= !mapping_anon_p
;
1385 /* Decode permissions. */
1386 auto has_perm
= [&m
] (char c
)
1387 { return m
.permissions
.find (c
) != gdb::string_view::npos
; };
1388 read
= has_perm ('r');
1389 write
= has_perm ('w');
1390 exec
= has_perm ('x');
1392 /* 'private' here actually means VM_MAYSHARE, and not
1393 VM_SHARED. In order to know if a mapping is really
1394 private or not, we must check the flag "sh" in the
1395 VmFlags field. This is done by decode_vmflags. However,
1396 if we are using a Linux kernel released before the commit
1397 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
1398 not have the VmFlags there. In this case, there is
1399 really no way to know if we are dealing with VM_SHARED,
1400 so we just assume that VM_MAYSHARE is enough. */
1401 priv
= has_perm ('p');
1403 /* Try to detect if region should be dumped by parsing smaps
1405 for (line
= strtok_r (NULL
, "\n", &t
);
1406 line
!= NULL
&& line
[0] >= 'A' && line
[0] <= 'Z';
1407 line
= strtok_r (NULL
, "\n", &t
))
1409 char keyword
[64 + 1];
1411 if (sscanf (line
, "%64s", keyword
) != 1)
1413 warning (_("Error parsing {s,}maps file '%s'"),
1414 maps_filename
.c_str ());
1418 if (strcmp (keyword
, "Anonymous:") == 0)
1420 /* Older Linux kernels did not support the
1421 "Anonymous:" counter. Check it here. */
1424 else if (strcmp (keyword
, "VmFlags:") == 0)
1425 decode_vmflags (line
, &v
);
1427 if (strcmp (keyword
, "AnonHugePages:") == 0
1428 || strcmp (keyword
, "Anonymous:") == 0)
1430 unsigned long number
;
1432 if (sscanf (line
, "%*s%lu", &number
) != 1)
1434 warning (_("Error parsing {s,}maps file '%s' number"),
1435 maps_filename
.c_str ());
1440 /* Even if we are dealing with a file-backed
1441 mapping, if it contains anonymous pages we
1442 consider it to be *also* an anonymous
1443 mapping, because this is what the Linux
1446 // Dump segments that have been written to.
1447 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1450 Note that if the mapping is already marked as
1451 file-backed (i.e., mapping_file_p is
1452 non-zero), then this is a special case, and
1453 this mapping will be dumped either when the
1454 user wants to dump file-backed *or* anonymous
1460 /* Save the smaps entry to the vector. */
1461 struct smaps_data map
;
1463 map
.start_address
= m
.addr
;
1464 map
.end_address
= m
.endaddr
;
1465 map
.filename
= m
.filename
;
1467 map
.read
= read
? true : false;
1468 map
.write
= write
? true : false;
1469 map
.exec
= exec
? true : false;
1470 map
.priv
= priv
? true : false;
1471 map
.has_anonymous
= has_anonymous
;
1472 map
.mapping_anon_p
= mapping_anon_p
? true : false;
1473 map
.mapping_file_p
= mapping_file_p
? true : false;
1474 map
.offset
= m
.offset
;
1475 map
.inode
= m
.inode
;
1477 smaps
.emplace_back (map
);
1483 /* Helper that checks if an address is in a memory tag page for a live
1487 linux_process_address_in_memtag_page (CORE_ADDR address
)
1489 if (current_inferior ()->fake_pid_p
)
1492 pid_t pid
= current_inferior ()->pid
;
1494 std::string smaps_file
= string_printf ("/proc/%d/smaps", pid
);
1496 gdb::unique_xmalloc_ptr
<char> data
1497 = target_fileio_read_stralloc (NULL
, smaps_file
.c_str ());
1499 if (data
== nullptr)
1502 /* Parse the contents of smaps into a vector. */
1503 std::vector
<struct smaps_data
> smaps
1504 = parse_smaps_data (data
.get (), smaps_file
);
1506 for (const smaps_data
&map
: smaps
)
1508 /* Is the address within [start_address, end_address) in a page
1509 mapped with memory tagging? */
1510 if (address
>= map
.start_address
1511 && address
< map
.end_address
1512 && map
.vmflags
.memory_tagging
)
1519 /* Helper that checks if an address is in a memory tag page for a core file
1523 linux_core_file_address_in_memtag_page (CORE_ADDR address
)
1525 if (core_bfd
== nullptr)
1528 memtag_section_info info
;
1529 return get_next_core_memtag_section (core_bfd
, nullptr, address
, info
);
1532 /* See linux-tdep.h. */
1535 linux_address_in_memtag_page (CORE_ADDR address
)
1537 if (!target_has_execution ())
1538 return linux_core_file_address_in_memtag_page (address
);
1540 return linux_process_address_in_memtag_page (address
);
1543 /* List memory regions in the inferior for a corefile. */
1546 linux_find_memory_regions_full (struct gdbarch
*gdbarch
,
1547 linux_dump_mapping_p_ftype
*should_dump_mapping_p
,
1548 linux_find_memory_region_ftype
*func
,
1552 /* Default dump behavior of coredump_filter (0x33), according to
1553 Documentation/filesystems/proc.txt from the Linux kernel
1555 filter_flags filterflags
= (COREFILTER_ANON_PRIVATE
1556 | COREFILTER_ANON_SHARED
1557 | COREFILTER_ELF_HEADERS
1558 | COREFILTER_HUGETLB_PRIVATE
);
1560 /* We need to know the real target PID to access /proc. */
1561 if (current_inferior ()->fake_pid_p
)
1564 pid
= current_inferior ()->pid
;
1566 if (use_coredump_filter
)
1568 std::string core_dump_filter_name
1569 = string_printf ("/proc/%d/coredump_filter", pid
);
1571 gdb::unique_xmalloc_ptr
<char> coredumpfilterdata
1572 = target_fileio_read_stralloc (NULL
, core_dump_filter_name
.c_str ());
1574 if (coredumpfilterdata
!= NULL
)
1578 sscanf (coredumpfilterdata
.get (), "%x", &flags
);
1579 filterflags
= (enum filter_flag
) flags
;
1583 std::string maps_filename
= string_printf ("/proc/%d/smaps", pid
);
1585 gdb::unique_xmalloc_ptr
<char> data
1586 = target_fileio_read_stralloc (NULL
, maps_filename
.c_str ());
1590 /* Older Linux kernels did not support /proc/PID/smaps. */
1591 maps_filename
= string_printf ("/proc/%d/maps", pid
);
1592 data
= target_fileio_read_stralloc (NULL
, maps_filename
.c_str ());
1594 if (data
== nullptr)
1598 /* Parse the contents of smaps into a vector. */
1599 std::vector
<struct smaps_data
> smaps
1600 = parse_smaps_data (data
.get (), maps_filename
.c_str ());
1602 for (const struct smaps_data
&map
: smaps
)
1604 int should_dump_p
= 0;
1606 if (map
.has_anonymous
)
1609 = should_dump_mapping_p (filterflags
, &map
.vmflags
,
1613 map
.filename
.c_str (),
1619 /* Older Linux kernels did not support the "Anonymous:" counter.
1620 If it is missing, we can't be sure - dump all the pages. */
1624 /* Invoke the callback function to create the corefile segment. */
1627 func (map
.start_address
, map
.end_address
- map
.start_address
,
1628 map
.offset
, map
.inode
, map
.read
, map
.write
, map
.exec
,
1629 1, /* MODIFIED is true because we want to dump
1631 map
.vmflags
.memory_tagging
!= 0,
1632 map
.filename
.c_str (), obfd
);
1639 /* A structure for passing information through
1640 linux_find_memory_regions_full. */
1642 struct linux_find_memory_regions_data
1644 /* The original callback. */
1646 find_memory_region_ftype func
;
1648 /* The original datum. */
1653 /* A callback for linux_find_memory_regions that converts between the
1654 "full"-style callback and find_memory_region_ftype. */
1657 linux_find_memory_regions_thunk (ULONGEST vaddr
, ULONGEST size
,
1658 ULONGEST offset
, ULONGEST inode
,
1659 int read
, int write
, int exec
, int modified
,
1661 const char *filename
, void *arg
)
1663 struct linux_find_memory_regions_data
*data
1664 = (struct linux_find_memory_regions_data
*) arg
;
1666 return data
->func (vaddr
, size
, read
, write
, exec
, modified
, memory_tagged
,
1670 /* A variant of linux_find_memory_regions_full that is suitable as the
1671 gdbarch find_memory_regions method. */
1674 linux_find_memory_regions (struct gdbarch
*gdbarch
,
1675 find_memory_region_ftype func
, void *obfd
)
1677 struct linux_find_memory_regions_data data
;
1682 return linux_find_memory_regions_full (gdbarch
,
1684 linux_find_memory_regions_thunk
,
1688 /* This is used to pass information from
1689 linux_make_mappings_corefile_notes through
1690 linux_find_memory_regions_full. */
1692 struct linux_make_mappings_data
1694 /* Number of files mapped. */
1695 ULONGEST file_count
;
1697 /* The obstack for the main part of the data. */
1698 struct obstack
*data_obstack
;
1700 /* The filename obstack. */
1701 struct obstack
*filename_obstack
;
1703 /* The architecture's "long" type. */
1704 struct type
*long_type
;
1707 static linux_find_memory_region_ftype linux_make_mappings_callback
;
1709 /* A callback for linux_find_memory_regions_full that updates the
1710 mappings data for linux_make_mappings_corefile_notes.
1712 MEMORY_TAGGED is true if the memory region contains memory tags, false
1716 linux_make_mappings_callback (ULONGEST vaddr
, ULONGEST size
,
1717 ULONGEST offset
, ULONGEST inode
,
1718 int read
, int write
, int exec
, int modified
,
1720 const char *filename
, void *data
)
1722 struct linux_make_mappings_data
*map_data
1723 = (struct linux_make_mappings_data
*) data
;
1724 gdb_byte buf
[sizeof (ULONGEST
)];
1726 if (*filename
== '\0' || inode
== 0)
1729 ++map_data
->file_count
;
1731 pack_long (buf
, map_data
->long_type
, vaddr
);
1732 obstack_grow (map_data
->data_obstack
, buf
, map_data
->long_type
->length ());
1733 pack_long (buf
, map_data
->long_type
, vaddr
+ size
);
1734 obstack_grow (map_data
->data_obstack
, buf
, map_data
->long_type
->length ());
1735 pack_long (buf
, map_data
->long_type
, offset
);
1736 obstack_grow (map_data
->data_obstack
, buf
, map_data
->long_type
->length ());
1738 obstack_grow_str0 (map_data
->filename_obstack
, filename
);
1743 /* Write the file mapping data to the core file, if possible. OBFD is
1744 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1745 is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
1748 linux_make_mappings_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
,
1749 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
1752 struct linux_make_mappings_data mapping_data
;
1753 type_allocator
alloc (gdbarch
);
1754 struct type
*long_type
1755 = init_integer_type (alloc
, gdbarch_long_bit (gdbarch
), 0, "long");
1756 gdb_byte buf
[sizeof (ULONGEST
)];
1758 auto_obstack data_obstack
, filename_obstack
;
1760 mapping_data
.file_count
= 0;
1761 mapping_data
.data_obstack
= &data_obstack
;
1762 mapping_data
.filename_obstack
= &filename_obstack
;
1763 mapping_data
.long_type
= long_type
;
1765 /* Reserve space for the count. */
1766 obstack_blank (&data_obstack
, long_type
->length ());
1767 /* We always write the page size as 1 since we have no good way to
1768 determine the correct value. */
1769 pack_long (buf
, long_type
, 1);
1770 obstack_grow (&data_obstack
, buf
, long_type
->length ());
1772 linux_find_memory_regions_full (gdbarch
,
1774 linux_make_mappings_callback
,
1777 if (mapping_data
.file_count
!= 0)
1779 /* Write the count to the obstack. */
1780 pack_long ((gdb_byte
*) obstack_base (&data_obstack
),
1781 long_type
, mapping_data
.file_count
);
1783 /* Copy the filenames to the data obstack. */
1784 int size
= obstack_object_size (&filename_obstack
);
1785 obstack_grow (&data_obstack
, obstack_base (&filename_obstack
),
1788 note_data
.reset (elfcore_write_file_note (obfd
, note_data
.release (), note_size
,
1789 obstack_base (&data_obstack
),
1790 obstack_object_size (&data_obstack
)));
1794 /* Fetch the siginfo data for the specified thread, if it exists. If
1795 there is no data, or we could not read it, return an empty
1798 static gdb::byte_vector
1799 linux_get_siginfo_data (thread_info
*thread
, struct gdbarch
*gdbarch
)
1801 struct type
*siginfo_type
;
1804 if (!gdbarch_get_siginfo_type_p (gdbarch
))
1805 return gdb::byte_vector ();
1807 scoped_restore_current_thread save_current_thread
;
1808 switch_to_thread (thread
);
1810 siginfo_type
= gdbarch_get_siginfo_type (gdbarch
);
1812 gdb::byte_vector
buf (siginfo_type
->length ());
1814 bytes_read
= target_read (current_inferior ()->top_target (),
1815 TARGET_OBJECT_SIGNAL_INFO
, NULL
,
1816 buf
.data (), 0, siginfo_type
->length ());
1817 if (bytes_read
!= siginfo_type
->length ())
1823 /* Records the thread's register state for the corefile note
1827 linux_corefile_thread (struct thread_info
*info
,
1828 struct gdbarch
*gdbarch
, bfd
*obfd
,
1829 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
1830 int *note_size
, gdb_signal stop_signal
)
1832 gcore_elf_build_thread_register_notes (gdbarch
, info
, stop_signal
, obfd
,
1833 ¬e_data
, note_size
);
1835 /* Don't return anything if we got no register information above,
1836 such a core file is useless. */
1837 if (note_data
!= nullptr)
1839 gdb::byte_vector siginfo_data
1840 = linux_get_siginfo_data (info
, gdbarch
);
1841 if (!siginfo_data
.empty ())
1842 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
1843 note_size
, "CORE", NT_SIGINFO
,
1844 siginfo_data
.data (),
1845 siginfo_data
.size ()));
1849 /* Fill the PRPSINFO structure with information about the process being
1850 debugged. Returns 1 in case of success, 0 for failures. Please note that
1851 even if the structure cannot be entirely filled (e.g., GDB was unable to
1852 gather information about the process UID/GID), this function will still
1853 return 1 since some information was already recorded. It will only return
1854 0 iff nothing can be gathered. */
1857 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo
*p
)
1859 /* The filename which we will use to obtain some info about the process.
1860 We will basically use this to store the `/proc/PID/FILENAME' file. */
1862 /* The basename of the executable. */
1863 const char *basename
;
1864 /* Temporary buffer. */
1866 /* The valid states of a process, according to the Linux kernel. */
1867 const char valid_states
[] = "RSDTZW";
1868 /* The program state. */
1869 const char *prog_state
;
1870 /* The state of the process. */
1872 /* The PID of the program which generated the corefile. */
1874 /* Process flags. */
1875 unsigned int pr_flag
;
1876 /* Process nice value. */
1878 /* The number of fields read by `sscanf'. */
1881 gdb_assert (p
!= NULL
);
1883 /* Obtaining PID and filename. */
1884 pid
= inferior_ptid
.pid ();
1885 xsnprintf (filename
, sizeof (filename
), "/proc/%d/cmdline", (int) pid
);
1886 /* The full name of the program which generated the corefile. */
1887 gdb::unique_xmalloc_ptr
<char> fname
1888 = target_fileio_read_stralloc (NULL
, filename
);
1890 if (fname
== NULL
|| fname
.get ()[0] == '\0')
1892 /* No program name was read, so we won't be able to retrieve more
1893 information about the process. */
1897 memset (p
, 0, sizeof (*p
));
1899 /* Defining the PID. */
1902 /* Copying the program name. Only the basename matters. */
1903 basename
= lbasename (fname
.get ());
1904 strncpy (p
->pr_fname
, basename
, sizeof (p
->pr_fname
) - 1);
1905 p
->pr_fname
[sizeof (p
->pr_fname
) - 1] = '\0';
1907 const std::string
&infargs
= current_inferior ()->args ();
1909 /* The arguments of the program. */
1910 std::string psargs
= fname
.get ();
1911 if (!infargs
.empty ())
1912 psargs
+= ' ' + infargs
;
1914 strncpy (p
->pr_psargs
, psargs
.c_str (), sizeof (p
->pr_psargs
) - 1);
1915 p
->pr_psargs
[sizeof (p
->pr_psargs
) - 1] = '\0';
1917 xsnprintf (filename
, sizeof (filename
), "/proc/%d/stat", (int) pid
);
1918 /* The contents of `/proc/PID/stat'. */
1919 gdb::unique_xmalloc_ptr
<char> proc_stat_contents
1920 = target_fileio_read_stralloc (NULL
, filename
);
1921 char *proc_stat
= proc_stat_contents
.get ();
1923 if (proc_stat
== NULL
|| *proc_stat
== '\0')
1925 /* Despite being unable to read more information about the
1926 process, we return 1 here because at least we have its
1927 command line, PID and arguments. */
1931 /* Ok, we have the stats. It's time to do a little parsing of the
1932 contents of the buffer, so that we end up reading what we want.
1934 The following parsing mechanism is strongly based on the
1935 information generated by the `fs/proc/array.c' file, present in
1936 the Linux kernel tree. More details about how the information is
1937 displayed can be obtained by seeing the manpage of proc(5),
1938 specifically under the entry of `/proc/[pid]/stat'. */
1940 /* Getting rid of the PID, since we already have it. */
1941 while (isdigit (*proc_stat
))
1944 proc_stat
= skip_spaces (proc_stat
);
1946 /* ps command also relies on no trailing fields ever contain ')'. */
1947 proc_stat
= strrchr (proc_stat
, ')');
1948 if (proc_stat
== NULL
)
1952 proc_stat
= skip_spaces (proc_stat
);
1954 n_fields
= sscanf (proc_stat
,
1955 "%c" /* Process state. */
1956 "%d%d%d" /* Parent PID, group ID, session ID. */
1957 "%*d%*d" /* tty_nr, tpgid (not used). */
1959 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1960 cmajflt (not used). */
1961 "%*s%*s%*s%*s" /* utime, stime, cutime,
1962 cstime (not used). */
1963 "%*s" /* Priority (not used). */
1966 &p
->pr_ppid
, &p
->pr_pgrp
, &p
->pr_sid
,
1972 /* Again, we couldn't read the complementary information about
1973 the process state. However, we already have minimal
1974 information, so we just return 1 here. */
1978 /* Filling the structure fields. */
1979 prog_state
= strchr (valid_states
, pr_sname
);
1980 if (prog_state
!= NULL
)
1981 p
->pr_state
= prog_state
- valid_states
;
1984 /* Zero means "Running". */
1988 p
->pr_sname
= p
->pr_state
> 5 ? '.' : pr_sname
;
1989 p
->pr_zomb
= p
->pr_sname
== 'Z';
1990 p
->pr_nice
= pr_nice
;
1991 p
->pr_flag
= pr_flag
;
1993 /* Finally, obtaining the UID and GID. For that, we read and parse the
1994 contents of the `/proc/PID/status' file. */
1995 xsnprintf (filename
, sizeof (filename
), "/proc/%d/status", (int) pid
);
1996 /* The contents of `/proc/PID/status'. */
1997 gdb::unique_xmalloc_ptr
<char> proc_status_contents
1998 = target_fileio_read_stralloc (NULL
, filename
);
1999 char *proc_status
= proc_status_contents
.get ();
2001 if (proc_status
== NULL
|| *proc_status
== '\0')
2003 /* Returning 1 since we already have a bunch of information. */
2007 /* Extracting the UID. */
2008 tmpstr
= strstr (proc_status
, "Uid:");
2011 /* Advancing the pointer to the beginning of the UID. */
2012 tmpstr
+= sizeof ("Uid:");
2013 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
2016 if (isdigit (*tmpstr
))
2017 p
->pr_uid
= strtol (tmpstr
, &tmpstr
, 10);
2020 /* Extracting the GID. */
2021 tmpstr
= strstr (proc_status
, "Gid:");
2024 /* Advancing the pointer to the beginning of the GID. */
2025 tmpstr
+= sizeof ("Gid:");
2026 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
2029 if (isdigit (*tmpstr
))
2030 p
->pr_gid
= strtol (tmpstr
, &tmpstr
, 10);
2036 /* Build the note section for a corefile, and return it in a malloc
2039 static gdb::unique_xmalloc_ptr
<char>
2040 linux_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
2042 struct elf_internal_linux_prpsinfo prpsinfo
;
2043 gdb::unique_xmalloc_ptr
<char> note_data
;
2045 if (! gdbarch_iterate_over_regset_sections_p (gdbarch
))
2048 if (linux_fill_prpsinfo (&prpsinfo
))
2050 if (gdbarch_ptr_bit (gdbarch
) == 64)
2051 note_data
.reset (elfcore_write_linux_prpsinfo64 (obfd
,
2052 note_data
.release (),
2053 note_size
, &prpsinfo
));
2055 note_data
.reset (elfcore_write_linux_prpsinfo32 (obfd
,
2056 note_data
.release (),
2057 note_size
, &prpsinfo
));
2060 /* Thread register information. */
2063 update_thread_list ();
2065 catch (const gdb_exception_error
&e
)
2067 exception_print (gdb_stderr
, e
);
2070 /* Like the kernel, prefer dumping the signalled thread first.
2071 "First thread" is what tools use to infer the signalled
2073 thread_info
*signalled_thr
= gcore_find_signalled_thread ();
2074 gdb_signal stop_signal
;
2075 if (signalled_thr
!= nullptr)
2076 stop_signal
= signalled_thr
->stop_signal ();
2078 stop_signal
= GDB_SIGNAL_0
;
2080 if (signalled_thr
!= nullptr)
2082 /* On some architectures, like AArch64, each thread can have a distinct
2083 gdbarch (due to scalable extensions), and using the inferior gdbarch
2086 Fetch each thread's gdbarch and pass it down to the lower layers so
2087 we can dump the right set of registers. */
2088 linux_corefile_thread (signalled_thr
,
2089 target_thread_architecture (signalled_thr
->ptid
),
2090 obfd
, note_data
, note_size
, stop_signal
);
2092 for (thread_info
*thr
: current_inferior ()->non_exited_threads ())
2094 if (thr
== signalled_thr
)
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 (thr
, target_thread_architecture (thr
->ptid
),
2104 obfd
, note_data
, note_size
, stop_signal
);
2110 /* Auxillary vector. */
2111 gdb::optional
<gdb::byte_vector
> auxv
=
2112 target_read_alloc (current_inferior ()->top_target (),
2113 TARGET_OBJECT_AUXV
, NULL
);
2114 if (auxv
&& !auxv
->empty ())
2116 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
2117 note_size
, "CORE", NT_AUXV
,
2118 auxv
->data (), auxv
->size ()));
2124 /* File mappings. */
2125 linux_make_mappings_corefile_notes (gdbarch
, obfd
, note_data
, note_size
);
2127 /* Include the target description when possible. Some architectures
2128 allow for per-thread gdbarch so we should really be emitting a tdesc
2129 per-thread, however, we don't currently support reading in a
2130 per-thread tdesc, so just emit the tdesc for the signalled thread. */
2131 gdbarch
= target_thread_architecture (signalled_thr
->ptid
);
2132 gcore_elf_make_tdesc_note (gdbarch
, obfd
, ¬e_data
, note_size
);
2137 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
2138 gdbarch.h. This function is not static because it is exported to
2139 other -tdep files. */
2142 linux_gdb_signal_from_target (struct gdbarch
*gdbarch
, int signal
)
2147 return GDB_SIGNAL_0
;
2150 return GDB_SIGNAL_HUP
;
2153 return GDB_SIGNAL_INT
;
2156 return GDB_SIGNAL_QUIT
;
2159 return GDB_SIGNAL_ILL
;
2162 return GDB_SIGNAL_TRAP
;
2165 return GDB_SIGNAL_ABRT
;
2168 return GDB_SIGNAL_BUS
;
2171 return GDB_SIGNAL_FPE
;
2174 return GDB_SIGNAL_KILL
;
2177 return GDB_SIGNAL_USR1
;
2180 return GDB_SIGNAL_SEGV
;
2183 return GDB_SIGNAL_USR2
;
2186 return GDB_SIGNAL_PIPE
;
2189 return GDB_SIGNAL_ALRM
;
2192 return GDB_SIGNAL_TERM
;
2195 return GDB_SIGNAL_CHLD
;
2198 return GDB_SIGNAL_CONT
;
2201 return GDB_SIGNAL_STOP
;
2204 return GDB_SIGNAL_TSTP
;
2207 return GDB_SIGNAL_TTIN
;
2210 return GDB_SIGNAL_TTOU
;
2213 return GDB_SIGNAL_URG
;
2216 return GDB_SIGNAL_XCPU
;
2219 return GDB_SIGNAL_XFSZ
;
2221 case LINUX_SIGVTALRM
:
2222 return GDB_SIGNAL_VTALRM
;
2225 return GDB_SIGNAL_PROF
;
2227 case LINUX_SIGWINCH
:
2228 return GDB_SIGNAL_WINCH
;
2230 /* No way to differentiate between SIGIO and SIGPOLL.
2231 Therefore, we just handle the first one. */
2233 return GDB_SIGNAL_IO
;
2236 return GDB_SIGNAL_PWR
;
2239 return GDB_SIGNAL_SYS
;
2241 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
2242 therefore we have to handle them here. */
2243 case LINUX_SIGRTMIN
:
2244 return GDB_SIGNAL_REALTIME_32
;
2246 case LINUX_SIGRTMAX
:
2247 return GDB_SIGNAL_REALTIME_64
;
2250 if (signal
>= LINUX_SIGRTMIN
+ 1 && signal
<= LINUX_SIGRTMAX
- 1)
2252 int offset
= signal
- LINUX_SIGRTMIN
+ 1;
2254 return (enum gdb_signal
) ((int) GDB_SIGNAL_REALTIME_33
+ offset
);
2257 return GDB_SIGNAL_UNKNOWN
;
2260 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
2261 gdbarch.h. This function is not static because it is exported to
2262 other -tdep files. */
2265 linux_gdb_signal_to_target (struct gdbarch
*gdbarch
,
2266 enum gdb_signal signal
)
2273 case GDB_SIGNAL_HUP
:
2274 return LINUX_SIGHUP
;
2276 case GDB_SIGNAL_INT
:
2277 return LINUX_SIGINT
;
2279 case GDB_SIGNAL_QUIT
:
2280 return LINUX_SIGQUIT
;
2282 case GDB_SIGNAL_ILL
:
2283 return LINUX_SIGILL
;
2285 case GDB_SIGNAL_TRAP
:
2286 return LINUX_SIGTRAP
;
2288 case GDB_SIGNAL_ABRT
:
2289 return LINUX_SIGABRT
;
2291 case GDB_SIGNAL_FPE
:
2292 return LINUX_SIGFPE
;
2294 case GDB_SIGNAL_KILL
:
2295 return LINUX_SIGKILL
;
2297 case GDB_SIGNAL_BUS
:
2298 return LINUX_SIGBUS
;
2300 case GDB_SIGNAL_SEGV
:
2301 return LINUX_SIGSEGV
;
2303 case GDB_SIGNAL_SYS
:
2304 return LINUX_SIGSYS
;
2306 case GDB_SIGNAL_PIPE
:
2307 return LINUX_SIGPIPE
;
2309 case GDB_SIGNAL_ALRM
:
2310 return LINUX_SIGALRM
;
2312 case GDB_SIGNAL_TERM
:
2313 return LINUX_SIGTERM
;
2315 case GDB_SIGNAL_URG
:
2316 return LINUX_SIGURG
;
2318 case GDB_SIGNAL_STOP
:
2319 return LINUX_SIGSTOP
;
2321 case GDB_SIGNAL_TSTP
:
2322 return LINUX_SIGTSTP
;
2324 case GDB_SIGNAL_CONT
:
2325 return LINUX_SIGCONT
;
2327 case GDB_SIGNAL_CHLD
:
2328 return LINUX_SIGCHLD
;
2330 case GDB_SIGNAL_TTIN
:
2331 return LINUX_SIGTTIN
;
2333 case GDB_SIGNAL_TTOU
:
2334 return LINUX_SIGTTOU
;
2339 case GDB_SIGNAL_XCPU
:
2340 return LINUX_SIGXCPU
;
2342 case GDB_SIGNAL_XFSZ
:
2343 return LINUX_SIGXFSZ
;
2345 case GDB_SIGNAL_VTALRM
:
2346 return LINUX_SIGVTALRM
;
2348 case GDB_SIGNAL_PROF
:
2349 return LINUX_SIGPROF
;
2351 case GDB_SIGNAL_WINCH
:
2352 return LINUX_SIGWINCH
;
2354 case GDB_SIGNAL_USR1
:
2355 return LINUX_SIGUSR1
;
2357 case GDB_SIGNAL_USR2
:
2358 return LINUX_SIGUSR2
;
2360 case GDB_SIGNAL_PWR
:
2361 return LINUX_SIGPWR
;
2363 case GDB_SIGNAL_POLL
:
2364 return LINUX_SIGPOLL
;
2366 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
2367 therefore we have to handle it here. */
2368 case GDB_SIGNAL_REALTIME_32
:
2369 return LINUX_SIGRTMIN
;
2371 /* Same comment applies to _64. */
2372 case GDB_SIGNAL_REALTIME_64
:
2373 return LINUX_SIGRTMAX
;
2376 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
2377 if (signal
>= GDB_SIGNAL_REALTIME_33
2378 && signal
<= GDB_SIGNAL_REALTIME_63
)
2380 int offset
= signal
- GDB_SIGNAL_REALTIME_33
;
2382 return LINUX_SIGRTMIN
+ 1 + offset
;
2388 /* Helper for linux_vsyscall_range that does the real work of finding
2389 the vsyscall's address range. */
2392 linux_vsyscall_range_raw (struct gdbarch
*gdbarch
, struct mem_range
*range
)
2397 if (target_auxv_search (AT_SYSINFO_EHDR
, &range
->start
) <= 0)
2400 /* It doesn't make sense to access the host's /proc when debugging a
2401 core file. Instead, look for the PT_LOAD segment that matches
2403 if (!target_has_execution ())
2408 phdrs_size
= bfd_get_elf_phdr_upper_bound (core_bfd
);
2409 if (phdrs_size
== -1)
2412 gdb::unique_xmalloc_ptr
<Elf_Internal_Phdr
>
2413 phdrs ((Elf_Internal_Phdr
*) xmalloc (phdrs_size
));
2414 num_phdrs
= bfd_get_elf_phdrs (core_bfd
, phdrs
.get ());
2415 if (num_phdrs
== -1)
2418 for (i
= 0; i
< num_phdrs
; i
++)
2419 if (phdrs
.get ()[i
].p_type
== PT_LOAD
2420 && phdrs
.get ()[i
].p_vaddr
== range
->start
)
2422 range
->length
= phdrs
.get ()[i
].p_memsz
;
2429 /* We need to know the real target PID to access /proc. */
2430 if (current_inferior ()->fake_pid_p
)
2433 pid
= current_inferior ()->pid
;
2435 /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
2436 reading /proc/PID/maps (2). The later identifies thread stacks
2437 in the output, which requires scanning every thread in the thread
2438 group to check whether a VMA is actually a thread's stack. With
2439 Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
2440 a few thousand threads, (1) takes a few miliseconds, while (2)
2441 takes several seconds. Also note that "smaps", what we read for
2442 determining core dump mappings, is even slower than "maps". */
2443 xsnprintf (filename
, sizeof filename
, "/proc/%ld/task/%ld/maps", pid
, pid
);
2444 gdb::unique_xmalloc_ptr
<char> data
2445 = target_fileio_read_stralloc (NULL
, filename
);
2449 char *saveptr
= NULL
;
2451 for (line
= strtok_r (data
.get (), "\n", &saveptr
);
2453 line
= strtok_r (NULL
, "\n", &saveptr
))
2455 ULONGEST addr
, endaddr
;
2456 const char *p
= line
;
2458 addr
= strtoulst (p
, &p
, 16);
2459 if (addr
== range
->start
)
2463 endaddr
= strtoulst (p
, &p
, 16);
2464 range
->length
= endaddr
- addr
;
2470 warning (_("unable to open /proc file '%s'"), filename
);
2475 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2476 caching, and defers the real work to linux_vsyscall_range_raw. */
2479 linux_vsyscall_range (struct gdbarch
*gdbarch
, struct mem_range
*range
)
2481 struct linux_info
*info
= get_linux_inferior_data (current_inferior ());
2483 if (info
->vsyscall_range_p
== 0)
2485 if (linux_vsyscall_range_raw (gdbarch
, &info
->vsyscall_range
))
2486 info
->vsyscall_range_p
= 1;
2488 info
->vsyscall_range_p
= -1;
2491 if (info
->vsyscall_range_p
< 0)
2494 *range
= info
->vsyscall_range
;
2498 /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
2499 definitions would be dependent on compilation host. */
2500 #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
2501 #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
2503 /* See gdbarch.sh 'infcall_mmap'. */
2506 linux_infcall_mmap (CORE_ADDR size
, unsigned prot
)
2508 struct objfile
*objf
;
2509 /* Do there still exist any Linux systems without "mmap64"?
2510 "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
2511 struct value
*mmap_val
= find_function_in_inferior ("mmap64", &objf
);
2512 struct value
*addr_val
;
2513 struct gdbarch
*gdbarch
= objf
->arch ();
2517 ARG_ADDR
, ARG_LENGTH
, ARG_PROT
, ARG_FLAGS
, ARG_FD
, ARG_OFFSET
, ARG_LAST
2519 struct value
*arg
[ARG_LAST
];
2521 arg
[ARG_ADDR
] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
,
2523 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2524 arg
[ARG_LENGTH
] = value_from_ulongest
2525 (builtin_type (gdbarch
)->builtin_unsigned_long
, size
);
2526 gdb_assert ((prot
& ~(GDB_MMAP_PROT_READ
| GDB_MMAP_PROT_WRITE
2527 | GDB_MMAP_PROT_EXEC
))
2529 arg
[ARG_PROT
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, prot
);
2530 arg
[ARG_FLAGS
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2531 GDB_MMAP_MAP_PRIVATE
2532 | GDB_MMAP_MAP_ANONYMOUS
);
2533 arg
[ARG_FD
] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, -1);
2534 arg
[ARG_OFFSET
] = value_from_longest (builtin_type (gdbarch
)->builtin_int64
,
2536 addr_val
= call_function_by_hand (mmap_val
, NULL
, arg
);
2537 retval
= value_as_address (addr_val
);
2538 if (retval
== (CORE_ADDR
) -1)
2539 error (_("Failed inferior mmap call for %s bytes, errno is changed."),
2544 /* See gdbarch.sh 'infcall_munmap'. */
2547 linux_infcall_munmap (CORE_ADDR addr
, CORE_ADDR size
)
2549 struct objfile
*objf
;
2550 struct value
*munmap_val
= find_function_in_inferior ("munmap", &objf
);
2551 struct value
*retval_val
;
2552 struct gdbarch
*gdbarch
= objf
->arch ();
2556 ARG_ADDR
, ARG_LENGTH
, ARG_LAST
2558 struct value
*arg
[ARG_LAST
];
2560 arg
[ARG_ADDR
] = value_from_pointer (builtin_type (gdbarch
)->builtin_data_ptr
,
2562 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2563 arg
[ARG_LENGTH
] = value_from_ulongest
2564 (builtin_type (gdbarch
)->builtin_unsigned_long
, size
);
2565 retval_val
= call_function_by_hand (munmap_val
, NULL
, arg
);
2566 retval
= value_as_long (retval_val
);
2568 warning (_("Failed inferior munmap call at %s for %s bytes, "
2569 "errno is changed."),
2570 hex_string (addr
), pulongest (size
));
2573 /* See linux-tdep.h. */
2576 linux_displaced_step_location (struct gdbarch
*gdbarch
)
2581 /* Determine entry point from target auxiliary vector. This avoids
2582 the need for symbols. Also, when debugging a stand-alone SPU
2583 executable, entry_point_address () will point to an SPU
2584 local-store address and is thus not usable as displaced stepping
2585 location. The auxiliary vector gets us the PowerPC-side entry
2586 point address instead. */
2587 if (target_auxv_search (AT_ENTRY
, &addr
) <= 0)
2588 throw_error (NOT_SUPPORTED_ERROR
,
2589 _("Cannot find AT_ENTRY auxiliary vector entry."));
2591 /* Make certain that the address points at real code, and not a
2592 function descriptor. */
2593 addr
= gdbarch_convert_from_func_ptr_addr
2594 (gdbarch
, addr
, current_inferior ()->top_target ());
2596 /* Inferior calls also use the entry point as a breakpoint location.
2597 We don't want displaced stepping to interfere with those
2598 breakpoints, so leave space. */
2599 gdbarch_breakpoint_from_pc (gdbarch
, &addr
, &bp_len
);
2605 /* See linux-tdep.h. */
2607 displaced_step_prepare_status
2608 linux_displaced_step_prepare (gdbarch
*arch
, thread_info
*thread
,
2609 CORE_ADDR
&displaced_pc
)
2611 linux_info
*per_inferior
= get_linux_inferior_data (thread
->inf
);
2613 if (!per_inferior
->disp_step_bufs
.has_value ())
2615 /* Figure out the location of the buffers. They are contiguous, starting
2616 at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
2617 CORE_ADDR disp_step_buf_addr
2618 = linux_displaced_step_location (thread
->inf
->arch ());
2619 int buf_len
= gdbarch_displaced_step_buffer_length (arch
);
2621 linux_gdbarch_data
*gdbarch_data
= get_linux_gdbarch_data (arch
);
2622 gdb_assert (gdbarch_data
->num_disp_step_buffers
> 0);
2624 std::vector
<CORE_ADDR
> buffers
;
2625 for (int i
= 0; i
< gdbarch_data
->num_disp_step_buffers
; i
++)
2626 buffers
.push_back (disp_step_buf_addr
+ i
* buf_len
);
2628 per_inferior
->disp_step_bufs
.emplace (buffers
);
2631 return per_inferior
->disp_step_bufs
->prepare (thread
, displaced_pc
);
2634 /* See linux-tdep.h. */
2636 displaced_step_finish_status
2637 linux_displaced_step_finish (gdbarch
*arch
, thread_info
*thread
,
2638 const target_waitstatus
&status
)
2640 linux_info
*per_inferior
= get_linux_inferior_data (thread
->inf
);
2642 gdb_assert (per_inferior
->disp_step_bufs
.has_value ());
2644 return per_inferior
->disp_step_bufs
->finish (arch
, thread
, status
);
2647 /* See linux-tdep.h. */
2649 const displaced_step_copy_insn_closure
*
2650 linux_displaced_step_copy_insn_closure_by_addr (inferior
*inf
, CORE_ADDR addr
)
2652 linux_info
*per_inferior
= linux_inferior_data
.get (inf
);
2654 if (per_inferior
== nullptr
2655 || !per_inferior
->disp_step_bufs
.has_value ())
2658 return per_inferior
->disp_step_bufs
->copy_insn_closure_by_addr (addr
);
2661 /* See linux-tdep.h. */
2664 linux_displaced_step_restore_all_in_ptid (inferior
*parent_inf
, ptid_t ptid
)
2666 linux_info
*per_inferior
= linux_inferior_data
.get (parent_inf
);
2668 if (per_inferior
== nullptr
2669 || !per_inferior
->disp_step_bufs
.has_value ())
2672 per_inferior
->disp_step_bufs
->restore_in_ptid (ptid
);
2675 /* Helper for linux_get_hwcap and linux_get_hwcap2. */
2678 linux_get_hwcap_helper (const gdb::optional
<gdb::byte_vector
> &auxv
,
2679 target_ops
*target
, gdbarch
*gdbarch
, CORE_ADDR match
)
2682 if (!auxv
.has_value ()
2683 || target_auxv_search (*auxv
, target
, gdbarch
, match
, &field
) != 1)
2688 /* See linux-tdep.h. */
2691 linux_get_hwcap (const gdb::optional
<gdb::byte_vector
> &auxv
,
2692 target_ops
*target
, gdbarch
*gdbarch
)
2694 return linux_get_hwcap_helper (auxv
, target
, gdbarch
, AT_HWCAP
);
2697 /* See linux-tdep.h. */
2702 return linux_get_hwcap (target_read_auxv (),
2703 current_inferior ()->top_target (),
2704 current_inferior ()->arch ());
2707 /* See linux-tdep.h. */
2710 linux_get_hwcap2 (const gdb::optional
<gdb::byte_vector
> &auxv
,
2711 target_ops
*target
, gdbarch
*gdbarch
)
2713 return linux_get_hwcap_helper (auxv
, target
, gdbarch
, AT_HWCAP2
);
2716 /* See linux-tdep.h. */
2721 return linux_get_hwcap2 (target_read_auxv (),
2722 current_inferior ()->top_target (),
2723 current_inferior ()->arch ());
2726 /* Display whether the gcore command is using the
2727 /proc/PID/coredump_filter file. */
2730 show_use_coredump_filter (struct ui_file
*file
, int from_tty
,
2731 struct cmd_list_element
*c
, const char *value
)
2733 gdb_printf (file
, _("Use of /proc/PID/coredump_filter file to generate"
2734 " corefiles is %s.\n"), value
);
2737 /* Display whether the gcore command is dumping mappings marked with
2738 the VM_DONTDUMP flag. */
2741 show_dump_excluded_mappings (struct ui_file
*file
, int from_tty
,
2742 struct cmd_list_element
*c
, const char *value
)
2744 gdb_printf (file
, _("Dumping of mappings marked with the VM_DONTDUMP"
2745 " flag is %s.\n"), value
);
2748 /* To be called from the various GDB_OSABI_LINUX handlers for the
2749 various GNU/Linux architectures and machine types.
2751 NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
2752 displaced stepping is not supported. */
2755 linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
,
2756 int num_disp_step_buffers
)
2758 if (num_disp_step_buffers
> 0)
2760 linux_gdbarch_data
*gdbarch_data
= get_linux_gdbarch_data (gdbarch
);
2761 gdbarch_data
->num_disp_step_buffers
= num_disp_step_buffers
;
2763 set_gdbarch_displaced_step_prepare (gdbarch
,
2764 linux_displaced_step_prepare
);
2765 set_gdbarch_displaced_step_finish (gdbarch
, linux_displaced_step_finish
);
2766 set_gdbarch_displaced_step_copy_insn_closure_by_addr
2767 (gdbarch
, linux_displaced_step_copy_insn_closure_by_addr
);
2768 set_gdbarch_displaced_step_restore_all_in_ptid
2769 (gdbarch
, linux_displaced_step_restore_all_in_ptid
);
2772 set_gdbarch_core_pid_to_str (gdbarch
, linux_core_pid_to_str
);
2773 set_gdbarch_info_proc (gdbarch
, linux_info_proc
);
2774 set_gdbarch_core_info_proc (gdbarch
, linux_core_info_proc
);
2775 set_gdbarch_core_xfer_siginfo (gdbarch
, linux_core_xfer_siginfo
);
2776 set_gdbarch_read_core_file_mappings (gdbarch
, linux_read_core_file_mappings
);
2777 set_gdbarch_find_memory_regions (gdbarch
, linux_find_memory_regions
);
2778 set_gdbarch_make_corefile_notes (gdbarch
, linux_make_corefile_notes
);
2779 set_gdbarch_has_shared_address_space (gdbarch
,
2780 linux_has_shared_address_space
);
2781 set_gdbarch_gdb_signal_from_target (gdbarch
,
2782 linux_gdb_signal_from_target
);
2783 set_gdbarch_gdb_signal_to_target (gdbarch
,
2784 linux_gdb_signal_to_target
);
2785 set_gdbarch_vsyscall_range (gdbarch
, linux_vsyscall_range
);
2786 set_gdbarch_infcall_mmap (gdbarch
, linux_infcall_mmap
);
2787 set_gdbarch_infcall_munmap (gdbarch
, linux_infcall_munmap
);
2788 set_gdbarch_get_siginfo_type (gdbarch
, linux_get_siginfo_type
);
2791 void _initialize_linux_tdep ();
2793 _initialize_linux_tdep ()
2795 /* Observers used to invalidate the cache when needed. */
2796 gdb::observers::inferior_exit
.attach (invalidate_linux_cache_inf
,
2798 gdb::observers::inferior_appeared
.attach (invalidate_linux_cache_inf
,
2800 gdb::observers::inferior_execd
.attach (linux_inferior_execd
,
2803 add_setshow_boolean_cmd ("use-coredump-filter", class_files
,
2804 &use_coredump_filter
, _("\
2805 Set whether gcore should consider /proc/PID/coredump_filter."),
2807 Show whether gcore should consider /proc/PID/coredump_filter."),
2809 Use this command to set whether gcore should consider the contents\n\
2810 of /proc/PID/coredump_filter when generating the corefile. For more information\n\
2811 about this file, refer to the manpage of core(5)."),
2812 NULL
, show_use_coredump_filter
,
2813 &setlist
, &showlist
);
2815 add_setshow_boolean_cmd ("dump-excluded-mappings", class_files
,
2816 &dump_excluded_mappings
, _("\
2817 Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2819 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2821 Use this command to set whether gcore should dump mappings marked with the\n\
2822 VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
2823 more information about this file, refer to the manpage of proc(5) and core(5)."),
2824 NULL
, show_dump_excluded_mappings
,
2825 &setlist
, &showlist
);
2828 /* Fetch (and possibly build) an appropriate `link_map_offsets' for
2829 ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
2832 linux_ilp32_fetch_link_map_offsets ()
2834 static link_map_offsets lmo
;
2835 static link_map_offsets
*lmp
= nullptr;
2841 lmo
.r_version_offset
= 0;
2842 lmo
.r_version_size
= 4;
2843 lmo
.r_map_offset
= 4;
2844 lmo
.r_brk_offset
= 8;
2845 lmo
.r_ldsomap_offset
= -1;
2846 lmo
.r_next_offset
= 20;
2848 /* Everything we need is in the first 20 bytes. */
2849 lmo
.link_map_size
= 20;
2850 lmo
.l_addr_offset
= 0;
2851 lmo
.l_name_offset
= 4;
2852 lmo
.l_ld_offset
= 8;
2853 lmo
.l_next_offset
= 12;
2854 lmo
.l_prev_offset
= 16;
2861 linux_lp64_fetch_link_map_offsets ()
2863 static link_map_offsets lmo
;
2864 static link_map_offsets
*lmp
= nullptr;
2870 lmo
.r_version_offset
= 0;
2871 lmo
.r_version_size
= 4;
2872 lmo
.r_map_offset
= 8;
2873 lmo
.r_brk_offset
= 16;
2874 lmo
.r_ldsomap_offset
= -1;
2875 lmo
.r_next_offset
= 40;
2877 /* Everything we need is in the first 40 bytes. */
2878 lmo
.link_map_size
= 40;
2879 lmo
.l_addr_offset
= 0;
2880 lmo
.l_name_offset
= 8;
2881 lmo
.l_ld_offset
= 16;
2882 lmo
.l_next_offset
= 24;
2883 lmo
.l_prev_offset
= 32;