Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / linux-tdep.c
blob701e3df7a279f84d27767ec4f324c4ec02cbfae5
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/>. */
20 #include "defs.h"
21 #include "gdbtypes.h"
22 #include "linux-tdep.h"
23 #include "auxv.h"
24 #include "target.h"
25 #include "gdbthread.h"
26 #include "gdbcore.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h"
31 #include "inferior.h"
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdbsupport/gdb_obstack.h"
35 #include "observable.h"
36 #include "objfiles.h"
37 #include "infcall.h"
38 #include "gdbcmd.h"
39 #include "gdbsupport/gdb_regex.h"
40 #include "gdbsupport/enum-flags.h"
41 #include <optional>
42 #include "gcore.h"
43 #include "gcore-elf.h"
44 #include "solib-svr4.h"
45 #include "memtag.h"
47 #include <ctype.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
54 tree. */
56 enum filter_flag
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). */
71 struct smaps_vmflags
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. */
103 struct smaps_data
105 ULONGEST start_address;
106 ULONGEST end_address;
107 std::string filename;
108 struct smaps_vmflags vmflags;
109 bool read;
110 bool write;
111 bool exec;
112 bool priv;
113 bool has_anonymous;
114 bool mapping_anon_p;
115 bool mapping_file_p;
117 ULONGEST inode;
118 ULONGEST offset;
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
128 the dump. */
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
153 tree. */
155 enum
157 LINUX_SIGHUP = 1,
158 LINUX_SIGINT = 2,
159 LINUX_SIGQUIT = 3,
160 LINUX_SIGILL = 4,
161 LINUX_SIGTRAP = 5,
162 LINUX_SIGABRT = 6,
163 LINUX_SIGIOT = 6,
164 LINUX_SIGBUS = 7,
165 LINUX_SIGFPE = 8,
166 LINUX_SIGKILL = 9,
167 LINUX_SIGUSR1 = 10,
168 LINUX_SIGSEGV = 11,
169 LINUX_SIGUSR2 = 12,
170 LINUX_SIGPIPE = 13,
171 LINUX_SIGALRM = 14,
172 LINUX_SIGTERM = 15,
173 LINUX_SIGSTKFLT = 16,
174 LINUX_SIGCHLD = 17,
175 LINUX_SIGCONT = 18,
176 LINUX_SIGSTOP = 19,
177 LINUX_SIGTSTP = 20,
178 LINUX_SIGTTIN = 21,
179 LINUX_SIGTTOU = 22,
180 LINUX_SIGURG = 23,
181 LINUX_SIGXCPU = 24,
182 LINUX_SIGXFSZ = 25,
183 LINUX_SIGVTALRM = 26,
184 LINUX_SIGPROF = 27,
185 LINUX_SIGWINCH = 28,
186 LINUX_SIGIO = 29,
187 LINUX_SIGPOLL = LINUX_SIGIO,
188 LINUX_SIGPWR = 30,
189 LINUX_SIGSYS = 31,
190 LINUX_SIGUNUSED = 31,
192 LINUX_SIGRTMIN = 32,
193 LINUX_SIGRTMAX = 64,
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);
211 return result;
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. */
217 struct linux_info
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. */
241 static void
242 invalidate_linux_cache_inf (struct inferior *inf)
244 linux_inferior_data.clear (inf);
247 /* inferior_execd observer. */
249 static void
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);
263 if (info == nullptr)
264 info = linux_inferior_data.emplace (inf);
266 return info;
269 /* See linux-tdep.h. */
271 struct type *
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;
280 struct type *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),
289 0, "int");
290 uint_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
291 1, "unsigned int");
292 long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
293 0, "long");
294 short_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
295 0, "short");
296 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
298 /* sival_t */
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);
304 /* __pid_t */
305 pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
306 int_type->length () * TARGET_CHAR_BIT,
307 "__pid_t");
308 pid_type->set_target_type (int_type);
309 pid_type->set_target_is_stub (true);
311 /* __uid_t */
312 uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
313 uint_type->length () * TARGET_CHAR_BIT,
314 "__uid_t");
315 uid_type->set_target_type (uint_type);
316 uid_type->set_target_is_stub (true);
318 /* __clock_t */
319 clock_type = alloc.new_type (TYPE_CODE_TYPEDEF,
320 long_type->length () * TARGET_CHAR_BIT,
321 "__clock_t");
322 clock_type->set_target_type (long_type);
323 clock_type->set_target_is_stub (true);
325 /* _sifields */
326 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
329 const int si_max_size = 128;
330 int si_pad_size;
331 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
333 /* _pad */
334 if (gdbarch_ptr_bit (gdbarch) == 64)
335 si_pad_size = (si_max_size / size_of_int) - 4;
336 else
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));
342 /* _kill */
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);
348 /* _timer */
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);
355 /* _rt */
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);
362 /* _sigchld */
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);
371 /* _sigfault */
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);
388 /* _sigpoll */
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);
394 /* _sigsys */
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);
401 /* struct siginfo */
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;
413 return siginfo_type;
416 /* This function is suitable for architectures that don't
417 extend/override the standard siginfo structure. */
419 static struct type *
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
426 Linux kernel. */
429 linux_is_uclinux (void)
431 CORE_ADDR dummy;
433 return (target_auxv_search (AT_NULL, &dummy) > 0
434 && target_auxv_search (AT_PAGESZ, &dummy) == 0);
437 static int
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. */
445 static std::string
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. */
456 struct mapping
458 ULONGEST addr;
459 ULONGEST endaddr;
460 std::string_view permissions;
461 ULONGEST offset;
462 std::string_view device;
463 ULONGEST inode;
465 /* This field is guaranteed to be NULL-terminated, hence it is not a
466 std::string_view. */
467 const char *filename;
470 /* Service function for corefiles and info proc. */
472 static mapping
473 read_mapping (const char *line)
475 struct mapping mapping;
476 const char *p = line;
478 mapping.addr = strtoulst (p, &p, 16);
479 if (*p == '-')
480 p++;
481 mapping.endaddr = strtoulst (p, &p, 16);
483 p = skip_spaces (p);
484 const char *permissions_start = p;
485 while (*p && !isspace (*p))
486 p++;
487 mapping.permissions = {permissions_start, (size_t) (p - permissions_start)};
489 mapping.offset = strtoulst (p, &p, 16);
491 p = skip_spaces (p);
492 const char *device_start = p;
493 while (*p && !isspace (*p))
494 p++;
495 mapping.device = {device_start, (size_t) (p - device_start)};
497 mapping.inode = strtoulst (p, &p, 10);
499 p = skip_spaces (p);
500 mapping.filename = p;
502 return mapping;
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
512 field on smaps. */
514 static void
515 decode_vmflags (char *p, struct smaps_vmflags *v)
517 char *saveptr = NULL;
518 const char *s;
520 v->initialized_p = 1;
521 p = skip_to_space (p);
522 p = skip_spaces (p);
524 for (s = strtok_r (p, " ", &saveptr);
525 s != NULL;
526 s = strtok_r (NULL, " ", &saveptr))
528 if (strcmp (s, "io") == 0)
529 v->io_page = 1;
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
573 that. */
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
583 the first line is:
585 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
587 Then FILENAME will be "/path/to/file". */
589 static int
590 mapping_is_anonymous_p (const char *filename)
592 static std::optional<mapping_regexes> regexes;
593 static int init_regex_p = 0;
595 if (!init_regex_p)
597 /* Let's be pessimistic and assume there will be an error while
598 compiling the regex'es. */
599 init_regex_p = -1;
601 regexes.emplace ();
603 /* If we reached this point, then everything succeeded. */
604 init_regex_p = 1;
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
617 anonymous. */
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)
626 return 1;
628 return 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
645 it).
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. */
678 static int
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;
691 int dump_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)
698 return 1;
700 if (v->initialized_p)
702 /* We never dump I/O mappings. */
703 if (v->io_page)
704 return 0;
706 /* Check if we should exclude this mapping. */
707 if (!dump_excluded_mappings && v->exclude_coredump)
708 return 0;
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)))
719 return 1;
721 return 0;
725 if (private_p)
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
731 pages. */
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;
737 else
738 dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
740 else
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
746 pages. */
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;
752 else
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
760 dump this mapping.
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
768 header. */
769 #ifndef SELFMAG
770 #define SELFMAG 4
771 #endif
773 /* Let's check if we have an ELF header. */
774 gdb_byte h[SELFMAG];
775 if (target_read_memory (addr, h, SELFMAG) == 0)
777 /* The EI_MAG* and ELFMAG* constants come from
778 <elf/common.h>. */
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
783 should dump it. */
784 dump_p = 1;
789 return dump_p;
792 /* As above, but return true only when we should dump the NT_FILE
793 entry. */
795 static int
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)
804 return 0;
806 /* Otherwise, any other file-based mapping should be placed in the
807 note. */
808 return 1;
811 /* Implement the "info proc" command. */
813 static void
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. */
819 long pid;
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);
826 char filename[100];
827 fileio_error target_errno;
829 if (args && isdigit (args[0]))
831 char *tem;
833 pid = strtoul (args, &tem, 10);
834 args = tem;
836 else
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);
847 if (args && args[0])
848 error (_("Too many parameters: %s"), args);
850 gdb_printf (_("process %ld\n"), pid);
851 if (cmdline_f)
853 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
854 gdb_byte *buffer;
855 ssize_t len = target_fileio_read_alloc (NULL, filename, &buffer);
857 if (len > 0)
859 gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
860 ssize_t pos;
862 for (pos = 0; pos < len - 1; pos++)
864 if (buffer[pos] == '\0')
865 buffer[pos] = ' ';
867 buffer[len - 1] = '\0';
868 gdb_printf ("cmdline = '%s'\n", buffer);
870 else
871 warning (_("unable to open /proc file '%s'"), filename);
873 if (cwd_f)
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 ());
880 else
881 warning (_("unable to read link '%s'"), filename);
883 if (exe_f)
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 ());
890 else
891 warning (_("unable to read link '%s'"), filename);
893 if (mappings_f)
895 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
896 gdb::unique_xmalloc_ptr<char> map
897 = target_fileio_read_stralloc (NULL, filename);
898 if (map != NULL)
900 char *line;
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");
909 else
911 gdb_printf (" %18s %18s %10s %10s %s %s\n",
912 "Start Addr", " End Addr", " Size",
913 " Offset", "Perms ", "objfile");
916 char *saveptr;
917 for (line = strtok_r (map.get (), "\n", &saveptr);
918 line;
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 (),
932 m.filename);
934 else
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 (),
943 m.filename);
947 else
948 warning (_("unable to open /proc file '%s'"), filename);
950 if (status_f)
952 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
953 gdb::unique_xmalloc_ptr<char> status
954 = target_fileio_read_stralloc (NULL, filename);
955 if (status)
956 gdb_puts (status.get ());
957 else
958 warning (_("unable to open /proc file '%s'"), filename);
960 if (stat_f)
962 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
963 gdb::unique_xmalloc_ptr<char> statstr
964 = target_fileio_read_stralloc (NULL, filename);
965 if (statstr)
967 const char *p = statstr.get ();
969 gdb_printf (_("Process: %s\n"),
970 pulongest (strtoulst (p, &p, 10)));
972 p = skip_spaces (p);
973 if (*p == '(')
975 /* ps command also relies on no trailing fields
976 ever contain ')'. */
977 const char *ep = strrchr (p, ')');
978 if (ep != NULL)
980 gdb_printf ("Exec file: %.*s\n",
981 (int) (ep - p - 1), p + 1);
982 p = ep + 1;
986 p = skip_spaces (p);
987 if (*p)
988 gdb_printf (_("State: %c\n"), *p++);
990 if (*p)
991 gdb_printf (_("Parent process: %s\n"),
992 pulongest (strtoulst (p, &p, 10)));
993 if (*p)
994 gdb_printf (_("Process group: %s\n"),
995 pulongest (strtoulst (p, &p, 10)));
996 if (*p)
997 gdb_printf (_("Session id: %s\n"),
998 pulongest (strtoulst (p, &p, 10)));
999 if (*p)
1000 gdb_printf (_("TTY: %s\n"),
1001 pulongest (strtoulst (p, &p, 10)));
1002 if (*p)
1003 gdb_printf (_("TTY owner process group: %s\n"),
1004 pulongest (strtoulst (p, &p, 10)));
1006 if (*p)
1007 gdb_printf (_("Flags: %s\n"),
1008 hex_string (strtoulst (p, &p, 10)));
1009 if (*p)
1010 gdb_printf (_("Minor faults (no memory page): %s\n"),
1011 pulongest (strtoulst (p, &p, 10)));
1012 if (*p)
1013 gdb_printf (_("Minor faults, children: %s\n"),
1014 pulongest (strtoulst (p, &p, 10)));
1015 if (*p)
1016 gdb_printf (_("Major faults (memory page faults): %s\n"),
1017 pulongest (strtoulst (p, &p, 10)));
1018 if (*p)
1019 gdb_printf (_("Major faults, children: %s\n"),
1020 pulongest (strtoulst (p, &p, 10)));
1021 if (*p)
1022 gdb_printf (_("utime: %s\n"),
1023 pulongest (strtoulst (p, &p, 10)));
1024 if (*p)
1025 gdb_printf (_("stime: %s\n"),
1026 pulongest (strtoulst (p, &p, 10)));
1027 if (*p)
1028 gdb_printf (_("utime, children: %s\n"),
1029 pulongest (strtoulst (p, &p, 10)));
1030 if (*p)
1031 gdb_printf (_("stime, children: %s\n"),
1032 pulongest (strtoulst (p, &p, 10)));
1033 if (*p)
1034 gdb_printf (_("jiffies remaining in current "
1035 "time slice: %s\n"),
1036 pulongest (strtoulst (p, &p, 10)));
1037 if (*p)
1038 gdb_printf (_("'nice' value: %s\n"),
1039 pulongest (strtoulst (p, &p, 10)));
1040 if (*p)
1041 gdb_printf (_("jiffies until next timeout: %s\n"),
1042 pulongest (strtoulst (p, &p, 10)));
1043 if (*p)
1044 gdb_printf (_("jiffies until next SIGALRM: %s\n"),
1045 pulongest (strtoulst (p, &p, 10)));
1046 if (*p)
1047 gdb_printf (_("start time (jiffies since "
1048 "system boot): %s\n"),
1049 pulongest (strtoulst (p, &p, 10)));
1050 if (*p)
1051 gdb_printf (_("Virtual memory size: %s\n"),
1052 pulongest (strtoulst (p, &p, 10)));
1053 if (*p)
1054 gdb_printf (_("Resident set size: %s\n"),
1055 pulongest (strtoulst (p, &p, 10)));
1056 if (*p)
1057 gdb_printf (_("rlim: %s\n"),
1058 pulongest (strtoulst (p, &p, 10)));
1059 if (*p)
1060 gdb_printf (_("Start of text: %s\n"),
1061 hex_string (strtoulst (p, &p, 10)));
1062 if (*p)
1063 gdb_printf (_("End of text: %s\n"),
1064 hex_string (strtoulst (p, &p, 10)));
1065 if (*p)
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". */
1070 if (*p)
1071 gdb_printf (_("Kernel stack pointer: %s\n"),
1072 hex_string (strtoulst (p, &p, 10)));
1073 if (*p)
1074 gdb_printf (_("Kernel instr pointer: %s\n"),
1075 hex_string (strtoulst (p, &p, 10)));
1076 if (*p)
1077 gdb_printf (_("Pending signals bitmap: %s\n"),
1078 hex_string (strtoulst (p, &p, 10)));
1079 if (*p)
1080 gdb_printf (_("Blocked signals bitmap: %s\n"),
1081 hex_string (strtoulst (p, &p, 10)));
1082 if (*p)
1083 gdb_printf (_("Ignored signals bitmap: %s\n"),
1084 hex_string (strtoulst (p, &p, 10)));
1085 if (*p)
1086 gdb_printf (_("Catched signals bitmap: %s\n"),
1087 hex_string (strtoulst (p, &p, 10)));
1088 if (*p)
1089 gdb_printf (_("wchan (system call): %s\n"),
1090 hex_string (strtoulst (p, &p, 10)));
1091 #endif
1093 else
1094 warning (_("unable to open /proc file '%s'"), filename);
1098 /* Implementation of `gdbarch_read_core_file_mappings', as defined in
1099 gdbarch.h.
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
1104 fs/binfmt_elf.c:
1106 long count -- how many files are mapped
1107 long page_size -- units for file_ofs
1108 array of [COUNT] elements of
1109 long start
1110 long end
1111 long file_ofs
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. */
1124 static void
1125 linux_read_core_file_mappings
1126 (struct gdbarch *gdbarch,
1127 struct bfd *cbfd,
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)
1139 return;
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"));
1148 return;
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"));
1156 return;
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"));
1165 return;
1168 ULONGEST count = bfd_get (addr_size_bits, current_program_space->core_bfd (),
1169 descdata);
1170 descdata += addr_size;
1172 ULONGEST page_size = bfd_get (addr_size_bits,
1173 current_program_space->core_bfd (),
1174 descdata);
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"));
1180 return;
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++)
1190 if (f >= descend)
1192 warning (_("malformed note - filename area is too small"));
1193 return;
1195 f += strnlen (f, descend - f) + 1;
1197 /* Complain, but don't return early if the filename area is too big. */
1198 if (f != descend)
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;
1225 ULONGEST file_ofs
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. */
1242 static void
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",
1252 "Start Addr",
1253 " End Addr",
1254 " Size", " Offset", "objfile");
1256 else
1258 gdb_printf (" %18s %18s %10s %10s %s\n",
1259 "Start Addr",
1260 " End Addr",
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),
1273 filename);
1274 else
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),
1280 filename);
1284 /* Implement "info proc" for a corefile. */
1286 static void
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);
1293 if (exe_f)
1295 const char *exe
1296 = bfd_core_file_failing_command (current_program_space->core_bfd ());
1298 if (exe != NULL)
1299 gdb_printf ("exe = '%s'\n", exe);
1300 else
1301 warning (_("unable to find command name in core file"));
1304 if (mappings_f)
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
1314 interface. */
1316 static LONGEST
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);
1321 asection *section
1322 = bfd_get_section_by_name (current_program_space->core_bfd (),
1323 section_name.c_str ());
1324 if (section == NULL)
1325 return -1;
1327 if (!bfd_get_section_contents (current_program_space->core_bfd (), section,
1328 readbuf, offset, len))
1329 return -1;
1331 return 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,
1338 bool memory_tagged,
1339 const char *filename,
1340 void *data);
1342 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags,
1343 const struct smaps_vmflags *v,
1344 int maybe_private_p,
1345 int mapping_anon_p,
1346 int mapping_file_p,
1347 const char *filename,
1348 ULONGEST addr,
1349 ULONGEST offset);
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)
1361 char *line, *t;
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;
1374 int mapping_anon_p;
1375 int mapping_file_p;
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
1410 counters. */
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 ());
1421 break;
1424 if (strcmp (keyword, "Anonymous:") == 0)
1426 /* Older Linux kernels did not support the
1427 "Anonymous:" counter. Check it here. */
1428 has_anonymous = 1;
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 ());
1442 break;
1444 if (number > 0)
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
1450 kernel does:
1452 // Dump segments that have been written to.
1453 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1454 goto whole;
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
1461 mappings. */
1462 mapping_anon_p = 1;
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;
1472 map.vmflags = v;
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);
1486 return smaps;
1489 /* Helper that checks if an address is in a memory tag page for a live
1490 process. */
1492 static bool
1493 linux_process_address_in_memtag_page (CORE_ADDR address)
1495 if (current_inferior ()->fake_pid_p)
1496 return false;
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)
1506 return false;
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)
1519 return true;
1522 return false;
1525 /* Helper that checks if an address is in a memory tag page for a core file
1526 process. */
1528 static bool
1529 linux_core_file_address_in_memtag_page (CORE_ADDR address)
1531 if (current_program_space->core_bfd () == nullptr)
1532 return false;
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. */
1541 bool
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. */
1552 static int
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,
1556 void *obfd)
1558 pid_t pid;
1559 /* Default dump behavior of coredump_filter (0x33), according to
1560 Documentation/filesystems/proc.txt from the Linux kernel
1561 tree. */
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)
1569 return 1;
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)
1583 unsigned int flags;
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 ());
1595 if (data == NULL)
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)
1602 return 1;
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)
1615 should_dump_p
1616 = should_dump_mapping_p (filterflags, &map.vmflags,
1617 map.priv,
1618 map.mapping_anon_p,
1619 map.mapping_file_p,
1620 map.filename.c_str (),
1621 map.start_address,
1622 map.offset);
1624 else
1626 /* Older Linux kernels did not support the "Anonymous:" counter.
1627 If it is missing, we can't be sure - dump all the pages. */
1628 should_dump_p = 1;
1631 /* Invoke the callback function to create the corefile segment. */
1632 if (should_dump_p)
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
1637 the mapping. */
1638 map.vmflags.memory_tagging != 0,
1639 map.filename.c_str (), obfd);
1643 return 0;
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. */
1657 void *obfd;
1660 /* A callback for linux_find_memory_regions that converts between the
1661 "full"-style callback and find_memory_region_ftype. */
1663 static int
1664 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
1665 ULONGEST offset, ULONGEST inode,
1666 int read, int write, int exec, int modified,
1667 bool memory_tagged,
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,
1674 data->obfd);
1677 /* A variant of linux_find_memory_regions_full that is suitable as the
1678 gdbarch find_memory_regions method. */
1680 static int
1681 linux_find_memory_regions (struct gdbarch *gdbarch,
1682 find_memory_region_ftype func, void *obfd)
1684 struct linux_find_memory_regions_data data;
1686 data.func = func;
1687 data.obfd = obfd;
1689 return linux_find_memory_regions_full (gdbarch,
1690 dump_mapping_p,
1691 linux_find_memory_regions_thunk,
1692 &data);
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
1720 otherwise. */
1722 static int
1723 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
1724 ULONGEST offset, ULONGEST inode,
1725 int read, int write, int exec, int modified,
1726 bool memory_tagged,
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)
1734 return 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);
1747 return 0;
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. */
1754 static void
1755 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
1756 gdb::unique_xmalloc_ptr<char> &note_data,
1757 int *note_size)
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,
1780 dump_note_entry_p,
1781 linux_make_mappings_callback,
1782 &mapping_data);
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),
1793 size);
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
1803 buffer. */
1805 static gdb::byte_vector
1806 linux_get_siginfo_data (thread_info *thread, struct gdbarch *gdbarch)
1808 struct type *siginfo_type;
1809 LONGEST bytes_read;
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 ())
1825 buf.clear ();
1827 return buf;
1830 /* Records the thread's register state for the corefile note
1831 section. */
1833 static void
1834 linux_corefile_thread (struct thread_info *info,
1835 struct gdbarch *gdbarch, bfd *obfd,
1836 gdb::unique_xmalloc_ptr<char> &note_data,
1837 int *note_size, gdb_signal stop_signal)
1839 gcore_elf_build_thread_register_notes (gdbarch, info, stop_signal, obfd,
1840 &note_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. */
1863 static int
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. */
1868 char filename[100];
1869 /* The basename of the executable. */
1870 const char *basename;
1871 /* Temporary buffer. */
1872 char *tmpstr;
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. */
1878 char pr_sname;
1879 /* The PID of the program which generated the corefile. */
1880 pid_t pid;
1881 /* Process flags. */
1882 unsigned int pr_flag;
1883 /* Process nice value. */
1884 long pr_nice;
1885 /* The number of fields read by `sscanf'. */
1886 int n_fields = 0;
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. */
1902 return 0;
1904 if (fname.get ()[buf_len - 1] != '\0')
1906 warning (_("target file %s "
1907 "does not contain a trailing null character"),
1908 filename);
1909 return 0;
1912 memset (p, 0, sizeof (*p));
1914 /* Defining the PID. */
1915 p->pr_pid = 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. */
1943 return 1;
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))
1957 ++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)
1964 return 1;
1965 proc_stat++;
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). */
1973 "%u" /* Flags. */
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). */
1979 "%ld", /* Nice. */
1980 &pr_sname,
1981 &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1982 &pr_flag,
1983 &pr_nice);
1985 if (n_fields != 6)
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. */
1990 return 1;
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;
1997 else
1999 /* Zero means "Running". */
2000 p->pr_state = 0;
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. */
2019 return 1;
2022 /* Extracting the UID. */
2023 tmpstr = strstr (proc_status, "Uid:");
2024 if (tmpstr != NULL)
2026 /* Advancing the pointer to the beginning of the UID. */
2027 tmpstr += sizeof ("Uid:");
2028 while (*tmpstr != '\0' && !isdigit (*tmpstr))
2029 ++tmpstr;
2031 if (isdigit (*tmpstr))
2032 p->pr_uid = strtol (tmpstr, &tmpstr, 10);
2035 /* Extracting the GID. */
2036 tmpstr = strstr (proc_status, "Gid:");
2037 if (tmpstr != NULL)
2039 /* Advancing the pointer to the beginning of the GID. */
2040 tmpstr += sizeof ("Gid:");
2041 while (*tmpstr != '\0' && !isdigit (*tmpstr))
2042 ++tmpstr;
2044 if (isdigit (*tmpstr))
2045 p->pr_gid = strtol (tmpstr, &tmpstr, 10);
2048 return 1;
2051 /* Build the note section for a corefile, and return it in a malloc
2052 buffer. */
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))
2061 return NULL;
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));
2069 else
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
2087 thread. */
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 ();
2092 else
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
2099 is incorrect.
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)
2110 continue;
2112 /* On some architectures, like AArch64, each thread can have a distinct
2113 gdbarch (due to scalable extensions), and using the inferior gdbarch
2114 is incorrect.
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);
2122 if (!note_data)
2123 return NULL;
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 ()));
2135 if (!note_data)
2136 return NULL;
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, &note_data, note_size);
2149 return note_data;
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. */
2156 enum gdb_signal
2157 linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
2159 switch (signal)
2161 case 0:
2162 return GDB_SIGNAL_0;
2164 case LINUX_SIGHUP:
2165 return GDB_SIGNAL_HUP;
2167 case LINUX_SIGINT:
2168 return GDB_SIGNAL_INT;
2170 case LINUX_SIGQUIT:
2171 return GDB_SIGNAL_QUIT;
2173 case LINUX_SIGILL:
2174 return GDB_SIGNAL_ILL;
2176 case LINUX_SIGTRAP:
2177 return GDB_SIGNAL_TRAP;
2179 case LINUX_SIGABRT:
2180 return GDB_SIGNAL_ABRT;
2182 case LINUX_SIGBUS:
2183 return GDB_SIGNAL_BUS;
2185 case LINUX_SIGFPE:
2186 return GDB_SIGNAL_FPE;
2188 case LINUX_SIGKILL:
2189 return GDB_SIGNAL_KILL;
2191 case LINUX_SIGUSR1:
2192 return GDB_SIGNAL_USR1;
2194 case LINUX_SIGSEGV:
2195 return GDB_SIGNAL_SEGV;
2197 case LINUX_SIGUSR2:
2198 return GDB_SIGNAL_USR2;
2200 case LINUX_SIGPIPE:
2201 return GDB_SIGNAL_PIPE;
2203 case LINUX_SIGALRM:
2204 return GDB_SIGNAL_ALRM;
2206 case LINUX_SIGTERM:
2207 return GDB_SIGNAL_TERM;
2209 case LINUX_SIGCHLD:
2210 return GDB_SIGNAL_CHLD;
2212 case LINUX_SIGCONT:
2213 return GDB_SIGNAL_CONT;
2215 case LINUX_SIGSTOP:
2216 return GDB_SIGNAL_STOP;
2218 case LINUX_SIGTSTP:
2219 return GDB_SIGNAL_TSTP;
2221 case LINUX_SIGTTIN:
2222 return GDB_SIGNAL_TTIN;
2224 case LINUX_SIGTTOU:
2225 return GDB_SIGNAL_TTOU;
2227 case LINUX_SIGURG:
2228 return GDB_SIGNAL_URG;
2230 case LINUX_SIGXCPU:
2231 return GDB_SIGNAL_XCPU;
2233 case LINUX_SIGXFSZ:
2234 return GDB_SIGNAL_XFSZ;
2236 case LINUX_SIGVTALRM:
2237 return GDB_SIGNAL_VTALRM;
2239 case LINUX_SIGPROF:
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. */
2247 case LINUX_SIGIO:
2248 return GDB_SIGNAL_IO;
2250 case LINUX_SIGPWR:
2251 return GDB_SIGNAL_PWR;
2253 case LINUX_SIGSYS:
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)
2283 switch (signal)
2285 case GDB_SIGNAL_0:
2286 return 0;
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;
2351 case GDB_SIGNAL_IO:
2352 return LINUX_SIGIO;
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;
2400 return -1;
2403 /* Helper for linux_vsyscall_range that does the real work of finding
2404 the vsyscall's address range. */
2406 static int
2407 linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
2409 char filename[100];
2410 long pid;
2412 if (target_auxv_search (AT_SYSINFO_EHDR, &range->start) <= 0)
2413 return 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
2417 the vDSO. */
2418 if (!target_has_execution ())
2420 long phdrs_size;
2421 int num_phdrs, i;
2423 phdrs_size
2424 = bfd_get_elf_phdr_upper_bound (current_program_space->core_bfd ());
2425 if (phdrs_size == -1)
2426 return 0;
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 (),
2431 phdrs.get ());
2432 if (num_phdrs == -1)
2433 return 0;
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;
2440 return 1;
2443 return 0;
2446 /* We need to know the real target PID to access /proc. */
2447 if (current_inferior ()->fake_pid_p)
2448 return 0;
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);
2463 if (data != NULL)
2465 char *line;
2466 char *saveptr = NULL;
2468 for (line = strtok_r (data.get (), "\n", &saveptr);
2469 line != NULL;
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)
2478 if (*p == '-')
2479 p++;
2480 endaddr = strtoulst (p, &p, 16);
2481 range->length = endaddr - addr;
2482 return 1;
2486 else
2487 warning (_("unable to open /proc file '%s'"), filename);
2489 return 0;
2492 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2493 caching, and defers the real work to linux_vsyscall_range_raw. */
2495 static int
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;
2504 else
2505 info->vsyscall_range_p = -1;
2508 if (info->vsyscall_range_p < 0)
2509 return 0;
2511 *range = info->vsyscall_range;
2512 return 1;
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'. */
2522 static CORE_ADDR
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 ();
2531 CORE_ADDR retval;
2532 enum
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))
2545 == 0);
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."),
2557 pulongest (size));
2558 return retval;
2561 /* See gdbarch.sh 'infcall_munmap'. */
2563 static void
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 ();
2570 LONGEST retval;
2571 enum
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,
2578 addr);
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);
2584 if (retval != 0)
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. */
2592 CORE_ADDR
2593 linux_displaced_step_location (struct gdbarch *gdbarch)
2595 CORE_ADDR addr;
2596 int bp_len;
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);
2617 addr += bp_len * 2;
2619 return addr;
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 ())
2673 return nullptr;
2675 return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
2678 /* See linux-tdep.h. */
2680 void
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 ())
2687 return;
2689 per_inferior->disp_step_bufs->restore_in_ptid (ptid);
2692 /* Helper for linux_get_hwcap and linux_get_hwcap2. */
2694 static CORE_ADDR
2695 linux_get_hwcap_helper (const std::optional<gdb::byte_vector> &auxv,
2696 target_ops *target, gdbarch *gdbarch, CORE_ADDR match)
2698 CORE_ADDR field;
2699 if (!auxv.has_value ()
2700 || target_auxv_search (*auxv, target, gdbarch, match, &field) != 1)
2701 return 0;
2702 return field;
2705 /* See linux-tdep.h. */
2707 CORE_ADDR
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. */
2716 CORE_ADDR
2717 linux_get_hwcap ()
2719 return linux_get_hwcap (target_read_auxv (),
2720 current_inferior ()->top_target (),
2721 current_inferior ()->arch ());
2724 /* See linux-tdep.h. */
2726 CORE_ADDR
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. */
2735 CORE_ADDR
2736 linux_get_hwcap2 ()
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. */
2746 static void
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. */
2757 static void
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. */
2771 void
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 ();
2809 void
2810 _initialize_linux_tdep ()
2812 /* Observers used to invalidate the cache when needed. */
2813 gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf,
2814 "linux-tdep");
2815 gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf,
2816 "linux-tdep");
2817 gdb::observers::inferior_execd.attach (linux_inferior_execd,
2818 "linux-tdep");
2820 add_setshow_boolean_cmd ("use-coredump-filter", class_files,
2821 &use_coredump_filter, _("\
2822 Set whether gcore should consider /proc/PID/coredump_filter."),
2823 _("\
2824 Show whether gcore should consider /proc/PID/coredump_filter."),
2825 _("\
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."),
2835 _("\
2836 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2837 _("\
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. */
2848 link_map_offsets *
2849 linux_ilp32_fetch_link_map_offsets ()
2851 static link_map_offsets lmo;
2852 static link_map_offsets *lmp = nullptr;
2854 if (lmp == nullptr)
2856 lmp = &lmo;
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;
2874 return lmp;
2877 link_map_offsets *
2878 linux_lp64_fetch_link_map_offsets ()
2880 static link_map_offsets lmo;
2881 static link_map_offsets *lmp = nullptr;
2883 if (lmp == nullptr)
2885 lmp = &lmo;
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;
2903 return lmp;