[sim] Run spellcheck.sh in sim (part 2)
[binutils-gdb.git] / gdb / linux-tdep.c
blobd3452059ce23ea33ef5b7832cb847621f7c168df
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 "exceptions.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 "cli/cli-cmds.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"
46 #include "cli/cli-style.h"
48 #include <ctype.h>
49 #include <unordered_map>
51 /* This enum represents the values that the user can choose when
52 informing the Linux kernel about which memory mappings will be
53 dumped in a corefile. They are described in the file
54 Documentation/filesystems/proc.txt, inside the Linux kernel
55 tree. */
57 enum filter_flag
59 COREFILTER_ANON_PRIVATE = 1 << 0,
60 COREFILTER_ANON_SHARED = 1 << 1,
61 COREFILTER_MAPPED_PRIVATE = 1 << 2,
62 COREFILTER_MAPPED_SHARED = 1 << 3,
63 COREFILTER_ELF_HEADERS = 1 << 4,
64 COREFILTER_HUGETLB_PRIVATE = 1 << 5,
65 COREFILTER_HUGETLB_SHARED = 1 << 6,
67 DEF_ENUM_FLAGS_TYPE (enum filter_flag, filter_flags);
69 /* This struct is used to map flags found in the "VmFlags:" field (in
70 the /proc/<PID>/smaps file). */
72 struct smaps_vmflags
74 /* Zero if this structure has not been initialized yet. It
75 probably means that the Linux kernel being used does not emit
76 the "VmFlags:" field on "/proc/PID/smaps". */
78 unsigned int initialized_p : 1;
80 /* Memory mapped I/O area (VM_IO, "io"). */
82 unsigned int io_page : 1;
84 /* Area uses huge TLB pages (VM_HUGETLB, "ht"). */
86 unsigned int uses_huge_tlb : 1;
88 /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd"). */
90 unsigned int exclude_coredump : 1;
92 /* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
94 unsigned int shared_mapping : 1;
96 /* Memory map has memory tagging enabled. */
98 unsigned int memory_tagging : 1;
101 /* Data structure that holds the information contained in the
102 /proc/<pid>/smaps file. */
104 struct smaps_data
106 ULONGEST start_address;
107 ULONGEST end_address;
108 std::string filename;
109 struct smaps_vmflags vmflags;
110 bool read;
111 bool write;
112 bool exec;
113 bool priv;
114 bool has_anonymous;
115 bool mapping_anon_p;
116 bool mapping_file_p;
118 ULONGEST inode;
119 ULONGEST offset;
122 /* Whether to take the /proc/PID/coredump_filter into account when
123 generating a corefile. */
125 static bool use_coredump_filter = true;
127 /* Whether the value of smaps_vmflags->exclude_coredump should be
128 ignored, including mappings marked with the VM_DONTDUMP flag in
129 the dump. */
130 static bool dump_excluded_mappings = false;
132 /* This enum represents the signals' numbers on a generic architecture
133 running the Linux kernel. The definition of "generic" comes from
134 the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
135 tree, which is the "de facto" implementation of signal numbers to
136 be used by new architecture ports.
138 For those architectures which have differences between the generic
139 standard (e.g., Alpha), we define the different signals (and *only*
140 those) in the specific target-dependent file (e.g.,
141 alpha-linux-tdep.c, for Alpha). Please refer to the architecture's
142 tdep file for more information.
144 ARM deserves a special mention here. On the file
145 <arch/arm/include/uapi/asm/signal.h>, it defines only one different
146 (and ARM-only) signal, which is SIGSWI, with the same number as
147 SIGRTMIN. This signal is used only for a very specific target,
148 called ArthurOS (from RISCOS). Therefore, we do not handle it on
149 the ARM-tdep file, and we can safely use the generic signal handler
150 here for ARM targets.
152 As stated above, this enum is derived from
153 <include/uapi/asm-generic/signal.h>, from the Linux kernel
154 tree. */
156 enum
158 LINUX_SIGHUP = 1,
159 LINUX_SIGINT = 2,
160 LINUX_SIGQUIT = 3,
161 LINUX_SIGILL = 4,
162 LINUX_SIGTRAP = 5,
163 LINUX_SIGABRT = 6,
164 LINUX_SIGIOT = 6,
165 LINUX_SIGBUS = 7,
166 LINUX_SIGFPE = 8,
167 LINUX_SIGKILL = 9,
168 LINUX_SIGUSR1 = 10,
169 LINUX_SIGSEGV = 11,
170 LINUX_SIGUSR2 = 12,
171 LINUX_SIGPIPE = 13,
172 LINUX_SIGALRM = 14,
173 LINUX_SIGTERM = 15,
174 LINUX_SIGSTKFLT = 16,
175 LINUX_SIGCHLD = 17,
176 LINUX_SIGCONT = 18,
177 LINUX_SIGSTOP = 19,
178 LINUX_SIGTSTP = 20,
179 LINUX_SIGTTIN = 21,
180 LINUX_SIGTTOU = 22,
181 LINUX_SIGURG = 23,
182 LINUX_SIGXCPU = 24,
183 LINUX_SIGXFSZ = 25,
184 LINUX_SIGVTALRM = 26,
185 LINUX_SIGPROF = 27,
186 LINUX_SIGWINCH = 28,
187 LINUX_SIGIO = 29,
188 LINUX_SIGPOLL = LINUX_SIGIO,
189 LINUX_SIGPWR = 30,
190 LINUX_SIGSYS = 31,
191 LINUX_SIGUNUSED = 31,
193 LINUX_SIGRTMIN = 32,
194 LINUX_SIGRTMAX = 64,
197 struct linux_gdbarch_data
199 struct type *siginfo_type = nullptr;
200 int num_disp_step_buffers = 0;
203 static const registry<gdbarch>::key<linux_gdbarch_data>
204 linux_gdbarch_data_handle;
206 static struct linux_gdbarch_data *
207 get_linux_gdbarch_data (struct gdbarch *gdbarch)
209 struct linux_gdbarch_data *result = linux_gdbarch_data_handle.get (gdbarch);
210 if (result == nullptr)
211 result = linux_gdbarch_data_handle.emplace (gdbarch);
212 return result;
215 /* Linux-specific cached data. This is used by GDB for caching
216 purposes for each inferior. This helps reduce the overhead of
217 transferring data from a remote target to the local host. */
218 struct linux_info
220 /* Cache of the inferior's vsyscall/vDSO mapping range. Only valid
221 if VSYSCALL_RANGE_P is positive. This is cached because getting
222 at this info requires an auxv lookup (which is itself cached),
223 and looking through the inferior's mappings (which change
224 throughout execution and therefore cannot be cached). */
225 struct mem_range vsyscall_range {};
227 /* Zero if we haven't tried looking up the vsyscall's range before
228 yet. Positive if we tried looking it up, and found it. Negative
229 if we tried looking it up but failed. */
230 int vsyscall_range_p = 0;
232 /* Inferior's displaced step buffers. */
233 std::optional<displaced_step_buffers> disp_step_bufs;
236 /* Per-inferior data key. */
237 static const registry<inferior>::key<linux_info> linux_inferior_data;
239 /* Frees whatever allocated space there is to be freed and sets INF's
240 linux cache data pointer to NULL. */
242 static void
243 invalidate_linux_cache_inf (struct inferior *inf)
245 linux_inferior_data.clear (inf);
248 /* inferior_execd observer. */
250 static void
251 linux_inferior_execd (inferior *exec_inf, inferior *follow_inf)
253 invalidate_linux_cache_inf (follow_inf);
256 /* Fetch the linux cache info for INF. This function always returns a
257 valid INFO pointer. */
259 static struct linux_info *
260 get_linux_inferior_data (inferior *inf)
262 linux_info *info = linux_inferior_data.get (inf);
264 if (info == nullptr)
265 info = linux_inferior_data.emplace (inf);
267 return info;
270 /* See linux-tdep.h. */
272 struct type *
273 linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
274 linux_siginfo_extra_fields extra_fields)
276 struct linux_gdbarch_data *linux_gdbarch_data;
277 struct type *int_type, *uint_type, *long_type, *void_ptr_type, *short_type;
278 struct type *uid_type, *pid_type;
279 struct type *sigval_type, *clock_type;
280 struct type *siginfo_type, *sifields_type;
281 struct type *type;
283 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
284 if (linux_gdbarch_data->siginfo_type != NULL)
285 return linux_gdbarch_data->siginfo_type;
287 type_allocator alloc (gdbarch);
289 int_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
290 0, "int");
291 uint_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
292 1, "unsigned int");
293 long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
294 0, "long");
295 short_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
296 0, "short");
297 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
299 /* sival_t */
300 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
301 sigval_type->set_name (xstrdup ("sigval_t"));
302 append_composite_type_field (sigval_type, "sival_int", int_type);
303 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
305 /* __pid_t */
306 pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
307 int_type->length () * TARGET_CHAR_BIT,
308 "__pid_t");
309 pid_type->set_target_type (int_type);
310 pid_type->set_target_is_stub (true);
312 /* __uid_t */
313 uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
314 uint_type->length () * TARGET_CHAR_BIT,
315 "__uid_t");
316 uid_type->set_target_type (uint_type);
317 uid_type->set_target_is_stub (true);
319 /* __clock_t */
320 clock_type = alloc.new_type (TYPE_CODE_TYPEDEF,
321 long_type->length () * TARGET_CHAR_BIT,
322 "__clock_t");
323 clock_type->set_target_type (long_type);
324 clock_type->set_target_is_stub (true);
326 /* _sifields */
327 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
330 const int si_max_size = 128;
331 int si_pad_size;
332 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
334 /* _pad */
335 if (gdbarch_ptr_bit (gdbarch) == 64)
336 si_pad_size = (si_max_size / size_of_int) - 4;
337 else
338 si_pad_size = (si_max_size / size_of_int) - 3;
339 append_composite_type_field (sifields_type, "_pad",
340 init_vector_type (int_type, si_pad_size));
343 /* _kill */
344 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
345 append_composite_type_field (type, "si_pid", pid_type);
346 append_composite_type_field (type, "si_uid", uid_type);
347 append_composite_type_field (sifields_type, "_kill", type);
349 /* _timer */
350 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
351 append_composite_type_field (type, "si_tid", int_type);
352 append_composite_type_field (type, "si_overrun", int_type);
353 append_composite_type_field (type, "si_sigval", sigval_type);
354 append_composite_type_field (sifields_type, "_timer", type);
356 /* _rt */
357 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
358 append_composite_type_field (type, "si_pid", pid_type);
359 append_composite_type_field (type, "si_uid", uid_type);
360 append_composite_type_field (type, "si_sigval", sigval_type);
361 append_composite_type_field (sifields_type, "_rt", type);
363 /* _sigchld */
364 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
365 append_composite_type_field (type, "si_pid", pid_type);
366 append_composite_type_field (type, "si_uid", uid_type);
367 append_composite_type_field (type, "si_status", int_type);
368 append_composite_type_field (type, "si_utime", clock_type);
369 append_composite_type_field (type, "si_stime", clock_type);
370 append_composite_type_field (sifields_type, "_sigchld", type);
372 /* _sigfault */
373 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
374 append_composite_type_field (type, "si_addr", void_ptr_type);
376 /* Additional bound fields for _sigfault in case they were requested. */
377 if ((extra_fields & LINUX_SIGINFO_FIELD_ADDR_BND) != 0)
379 struct type *sigfault_bnd_fields;
381 append_composite_type_field (type, "_addr_lsb", short_type);
382 sigfault_bnd_fields = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
383 append_composite_type_field (sigfault_bnd_fields, "_lower", void_ptr_type);
384 append_composite_type_field (sigfault_bnd_fields, "_upper", void_ptr_type);
385 append_composite_type_field (type, "_addr_bnd", sigfault_bnd_fields);
387 append_composite_type_field (sifields_type, "_sigfault", type);
389 /* _sigpoll */
390 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
391 append_composite_type_field (type, "si_band", long_type);
392 append_composite_type_field (type, "si_fd", int_type);
393 append_composite_type_field (sifields_type, "_sigpoll", type);
395 /* _sigsys */
396 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
397 append_composite_type_field (type, "_call_addr", void_ptr_type);
398 append_composite_type_field (type, "_syscall", int_type);
399 append_composite_type_field (type, "_arch", uint_type);
400 append_composite_type_field (sifields_type, "_sigsys", type);
402 /* struct siginfo */
403 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
404 siginfo_type->set_name (xstrdup ("siginfo"));
405 append_composite_type_field (siginfo_type, "si_signo", int_type);
406 append_composite_type_field (siginfo_type, "si_errno", int_type);
407 append_composite_type_field (siginfo_type, "si_code", int_type);
408 append_composite_type_field_aligned (siginfo_type,
409 "_sifields", sifields_type,
410 long_type->length ());
412 linux_gdbarch_data->siginfo_type = siginfo_type;
414 return siginfo_type;
417 /* This function is suitable for architectures that don't
418 extend/override the standard siginfo structure. */
420 static struct type *
421 linux_get_siginfo_type (struct gdbarch *gdbarch)
423 return linux_get_siginfo_type_with_fields (gdbarch, 0);
426 /* Return true if the target is running on uClinux instead of normal
427 Linux kernel. */
430 linux_is_uclinux (void)
432 CORE_ADDR dummy;
434 return (target_auxv_search (AT_NULL, &dummy) > 0
435 && target_auxv_search (AT_PAGESZ, &dummy) == 0);
438 static int
439 linux_has_shared_address_space (struct gdbarch *gdbarch)
441 return linux_is_uclinux ();
444 /* This is how we want PTIDs from core files to be printed. */
446 static std::string
447 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
449 if (ptid.lwp () != 0)
450 return string_printf ("LWP %ld", ptid.lwp ());
452 return normal_pid_to_str (ptid);
455 /* Data from one mapping from /proc/PID/maps. */
457 struct mapping
459 ULONGEST addr;
460 ULONGEST endaddr;
461 std::string permissions;
462 ULONGEST offset;
463 std::string_view device;
464 ULONGEST inode;
466 /* This field is guaranteed to be NULL-terminated, hence it is not a
467 std::string_view. */
468 const char *filename;
471 /* Service function for corefiles and info proc. */
473 static mapping
474 read_mapping (const char *line)
476 struct mapping mapping;
477 const char *p = line;
479 mapping.addr = strtoulst (p, &p, 16);
480 if (*p == '-')
481 p++;
482 mapping.endaddr = strtoulst (p, &p, 16);
484 p = skip_spaces (p);
485 const char *permissions_start = p;
486 while (*p && !isspace (*p))
487 p++;
488 mapping.permissions = std::string (permissions_start,
489 (size_t) (p - permissions_start));
491 mapping.offset = strtoulst (p, &p, 16);
493 p = skip_spaces (p);
494 const char *device_start = p;
495 while (*p && !isspace (*p))
496 p++;
497 mapping.device = {device_start, (size_t) (p - device_start)};
499 mapping.inode = strtoulst (p, &p, 10);
501 p = skip_spaces (p);
502 mapping.filename = p;
504 return mapping;
507 /* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
509 This function was based on the documentation found on
510 <Documentation/filesystems/proc.txt>, on the Linux kernel.
512 Linux kernels before commit
513 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
514 field on smaps. */
516 static void
517 decode_vmflags (char *p, struct smaps_vmflags *v)
519 char *saveptr = NULL;
520 const char *s;
522 v->initialized_p = 1;
523 p = skip_to_space (p);
524 p = skip_spaces (p);
526 for (s = strtok_r (p, " ", &saveptr);
527 s != NULL;
528 s = strtok_r (NULL, " ", &saveptr))
530 if (strcmp (s, "io") == 0)
531 v->io_page = 1;
532 else if (strcmp (s, "ht") == 0)
533 v->uses_huge_tlb = 1;
534 else if (strcmp (s, "dd") == 0)
535 v->exclude_coredump = 1;
536 else if (strcmp (s, "sh") == 0)
537 v->shared_mapping = 1;
538 else if (strcmp (s, "mt") == 0)
539 v->memory_tagging = 1;
543 /* Regexes used by mapping_is_anonymous_p. Put in a structure because
544 they're initialized lazily. */
546 struct mapping_regexes
548 /* Matches "/dev/zero" filenames (with or without the "(deleted)"
549 string in the end). We know for sure, based on the Linux kernel
550 code, that memory mappings whose associated filename is
551 "/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
552 compiled_regex dev_zero
553 {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB,
554 _("Could not compile regex to match /dev/zero filename")};
556 /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
557 string in the end). These filenames refer to shared memory
558 (shmem), and memory mappings associated with them are
559 MAP_ANONYMOUS as well. */
560 compiled_regex shmem_file
561 {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB,
562 _("Could not compile regex to match shmem filenames")};
564 /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
565 0' code, which is responsible to decide if it is dealing with a
566 'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
567 FILE_DELETED matches, it does not necessarily mean that we are
568 dealing with an anonymous shared mapping. However, there is no
569 easy way to detect this currently, so this is the best
570 approximation we have.
572 As a result, GDB will dump readonly pages of deleted executables
573 when using the default value of coredump_filter (0x33), while the
574 Linux kernel will not dump those pages. But we can live with
575 that. */
576 compiled_regex file_deleted
577 {" (deleted)$", REG_NOSUB,
578 _("Could not compile regex to match '<file> (deleted)'")};
581 /* Return 1 if the memory mapping is anonymous, 0 otherwise.
583 FILENAME is the name of the file present in the first line of the
584 memory mapping, in the "/proc/PID/smaps" output. For example, if
585 the first line is:
587 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
589 Then FILENAME will be "/path/to/file". */
591 static int
592 mapping_is_anonymous_p (const char *filename)
594 static std::optional<mapping_regexes> regexes;
595 static int init_regex_p = 0;
597 if (!init_regex_p)
599 /* Let's be pessimistic and assume there will be an error while
600 compiling the regex'es. */
601 init_regex_p = -1;
603 regexes.emplace ();
605 /* If we reached this point, then everything succeeded. */
606 init_regex_p = 1;
609 if (init_regex_p == -1)
611 const char deleted[] = " (deleted)";
612 size_t del_len = sizeof (deleted) - 1;
613 size_t filename_len = strlen (filename);
615 /* There was an error while compiling the regex'es above. In
616 order to try to give some reliable information to the caller,
617 we just try to find the string " (deleted)" in the filename.
618 If we managed to find it, then we assume the mapping is
619 anonymous. */
620 return (filename_len >= del_len
621 && strcmp (filename + filename_len - del_len, deleted) == 0);
624 if (*filename == '\0'
625 || regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
626 || regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
627 || regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
628 return 1;
630 return 0;
633 /* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
634 MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
635 be dumped, or greater than 0 if it should.
637 In a nutshell, this is the logic that we follow in order to decide
638 if a mapping should be dumped or not.
640 - If the mapping is associated to a file whose name ends with
641 " (deleted)", or if the file is "/dev/zero", or if it is
642 "/SYSV%08x" (shared memory), or if there is no file associated
643 with it, or if the AnonHugePages: or the Anonymous: fields in the
644 /proc/PID/smaps have contents, then GDB considers this mapping to
645 be anonymous. Otherwise, GDB considers this mapping to be a
646 file-backed mapping (because there will be a file associated with
647 it).
649 It is worth mentioning that, from all those checks described
650 above, the most fragile is the one to see if the file name ends
651 with " (deleted)". This does not necessarily mean that the
652 mapping is anonymous, because the deleted file associated with
653 the mapping may have been a hard link to another file, for
654 example. The Linux kernel checks to see if "i_nlink == 0", but
655 GDB cannot easily (and normally) do this check (iff running as
656 root, it could find the mapping in /proc/PID/map_files/ and
657 determine whether there still are other hard links to the
658 inode/file). Therefore, we made a compromise here, and we assume
659 that if the file name ends with " (deleted)", then the mapping is
660 indeed anonymous. FWIW, this is something the Linux kernel could
661 do better: expose this information in a more direct way.
663 - If we see the flag "sh" in the "VmFlags:" field (in
664 /proc/PID/smaps), then certainly the memory mapping is shared
665 (VM_SHARED). If we have access to the VmFlags, and we don't see
666 the "sh" there, then certainly the mapping is private. However,
667 Linux kernels before commit
668 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
669 "VmFlags:" field; in that case, we use another heuristic: if we
670 see 'p' in the permission flags, then we assume that the mapping
671 is private, even though the presence of the 's' flag there would
672 mean VM_MAYSHARE, which means the mapping could still be private.
673 This should work OK enough, however.
675 - Even if, at the end, we decided that we should not dump the
676 mapping, we still have to check if it is something like an ELF
677 header (of a DSO or an executable, for example). If it is, and
678 if the user is interested in dump it, then we should dump it. */
680 static int
681 dump_mapping_p (filter_flags filterflags, const struct smaps_vmflags *v,
682 int maybe_private_p, int mapping_anon_p, int mapping_file_p,
683 const char *filename, ULONGEST addr, ULONGEST offset)
685 /* Initially, we trust in what we received from our caller. This
686 value may not be very precise (i.e., it was probably gathered
687 from the permission line in the /proc/PID/smaps list, which
688 actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
689 what we have until we take a look at the "VmFlags:" field
690 (assuming that the version of the Linux kernel being used
691 supports it, of course). */
692 int private_p = maybe_private_p;
693 int dump_p;
695 /* We always dump vDSO and vsyscall mappings, because it's likely that
696 there'll be no file to read the contents from at core load time.
697 The kernel does the same. */
698 if (strcmp ("[vdso]", filename) == 0
699 || strcmp ("[vsyscall]", filename) == 0)
700 return 1;
702 if (v->initialized_p)
704 /* We never dump I/O mappings. */
705 if (v->io_page)
706 return 0;
708 /* Check if we should exclude this mapping. */
709 if (!dump_excluded_mappings && v->exclude_coredump)
710 return 0;
712 /* Update our notion of whether this mapping is shared or
713 private based on a trustworthy value. */
714 private_p = !v->shared_mapping;
716 /* HugeTLB checking. */
717 if (v->uses_huge_tlb)
719 if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
720 || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
721 return 1;
723 return 0;
727 if (private_p)
729 if (mapping_anon_p && mapping_file_p)
731 /* This is a special situation. It can happen when we see a
732 mapping that is file-backed, but that contains anonymous
733 pages. */
734 dump_p = ((filterflags & COREFILTER_ANON_PRIVATE) != 0
735 || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
737 else if (mapping_anon_p)
738 dump_p = (filterflags & COREFILTER_ANON_PRIVATE) != 0;
739 else
740 dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
742 else
744 if (mapping_anon_p && mapping_file_p)
746 /* This is a special situation. It can happen when we see a
747 mapping that is file-backed, but that contains anonymous
748 pages. */
749 dump_p = ((filterflags & COREFILTER_ANON_SHARED) != 0
750 || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
752 else if (mapping_anon_p)
753 dump_p = (filterflags & COREFILTER_ANON_SHARED) != 0;
754 else
755 dump_p = (filterflags & COREFILTER_MAPPED_SHARED) != 0;
758 /* Even if we decided that we shouldn't dump this mapping, we still
759 have to check whether (a) the user wants us to dump mappings
760 containing an ELF header, and (b) the mapping in question
761 contains an ELF header. If (a) and (b) are true, then we should
762 dump this mapping.
764 A mapping contains an ELF header if it is a private mapping, its
765 offset is zero, and its first word is ELFMAG. */
766 if (!dump_p && private_p && offset == 0
767 && (filterflags & COREFILTER_ELF_HEADERS) != 0)
769 /* Useful define specifying the size of the ELF magical
770 header. */
771 #ifndef SELFMAG
772 #define SELFMAG 4
773 #endif
775 /* Let's check if we have an ELF header. */
776 gdb_byte h[SELFMAG];
777 if (target_read_memory (addr, h, SELFMAG) == 0)
779 /* The EI_MAG* and ELFMAG* constants come from
780 <elf/common.h>. */
781 if (h[EI_MAG0] == ELFMAG0 && h[EI_MAG1] == ELFMAG1
782 && h[EI_MAG2] == ELFMAG2 && h[EI_MAG3] == ELFMAG3)
784 /* This mapping contains an ELF header, so we
785 should dump it. */
786 dump_p = 1;
791 return dump_p;
794 /* As above, but return true only when we should dump the NT_FILE
795 entry. */
797 static int
798 dump_note_entry_p (filter_flags filterflags, const struct smaps_vmflags *v,
799 int maybe_private_p, int mapping_anon_p, int mapping_file_p,
800 const char *filename, ULONGEST addr, ULONGEST offset)
802 /* vDSO and vsyscall mappings will end up in the core file. Don't
803 put them in the NT_FILE note. */
804 if (strcmp ("[vdso]", filename) == 0
805 || strcmp ("[vsyscall]", filename) == 0)
806 return 0;
808 /* Otherwise, any other file-based mapping should be placed in the
809 note. */
810 return 1;
813 /* Implement the "info proc" command. */
815 static void
816 linux_info_proc (struct gdbarch *gdbarch, const char *args,
817 enum info_proc_what what)
819 /* A long is used for pid instead of an int to avoid a loss of precision
820 compiler warning from the output of strtoul. */
821 long pid;
822 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
823 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
824 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
825 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
826 int status_f = (what == IP_STATUS || what == IP_ALL);
827 int stat_f = (what == IP_STAT || what == IP_ALL);
828 char filename[100];
829 fileio_error target_errno;
831 if (args && isdigit (args[0]))
833 char *tem;
835 pid = strtoul (args, &tem, 10);
836 args = tem;
838 else
840 if (!target_has_execution ())
841 error (_("No current process: you must name one."));
842 if (current_inferior ()->fake_pid_p)
843 error (_("Can't determine the current process's PID: you must name one."));
845 pid = current_inferior ()->pid;
848 args = skip_spaces (args);
849 if (args && args[0])
850 error (_("Too many parameters: %s"), args);
852 gdb_printf (_("process %ld\n"), pid);
853 if (cmdline_f)
855 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
856 gdb_byte *buffer;
857 ssize_t len = target_fileio_read_alloc (NULL, filename, &buffer);
859 if (len > 0)
861 gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
862 ssize_t pos;
864 for (pos = 0; pos < len - 1; pos++)
866 if (buffer[pos] == '\0')
867 buffer[pos] = ' ';
869 buffer[len - 1] = '\0';
870 gdb_printf ("cmdline = '%s'\n", buffer);
872 else
873 warning (_("unable to open /proc file '%s'"), filename);
875 if (cwd_f)
877 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
878 std::optional<std::string> contents
879 = target_fileio_readlink (NULL, filename, &target_errno);
880 if (contents.has_value ())
881 gdb_printf ("cwd = '%s'\n", contents->c_str ());
882 else
883 warning (_("unable to read link '%s'"), filename);
885 if (exe_f)
887 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
888 std::optional<std::string> contents
889 = target_fileio_readlink (NULL, filename, &target_errno);
890 if (contents.has_value ())
891 gdb_printf ("exe = '%s'\n", contents->c_str ());
892 else
893 warning (_("unable to read link '%s'"), filename);
895 if (mappings_f)
897 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
898 gdb::unique_xmalloc_ptr<char> map
899 = target_fileio_read_stralloc (NULL, filename);
900 if (map != NULL)
902 gdb_printf (_("Mapped address spaces:\n\n"));
903 ui_out_emit_table emitter (current_uiout, 6, -1, "ProcMappings");
905 int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
906 current_uiout->table_header (width, ui_left, "start", "Start Addr");
907 current_uiout->table_header (width, ui_left, "end", "End Addr");
908 current_uiout->table_header (width, ui_left, "size", "Size");
909 current_uiout->table_header (width, ui_left, "offset", "Offset");
910 current_uiout->table_header (5, ui_left, "perms", "Perms");
911 current_uiout->table_header (0, ui_left, "objfile", "File");
912 current_uiout->table_body ();
914 char *saveptr;
915 for (const char *line = strtok_r (map.get (), "\n", &saveptr);
916 line != nullptr;
917 line = strtok_r (nullptr, "\n", &saveptr))
919 struct mapping m = read_mapping (line);
921 ui_out_emit_tuple tuple_emitter (current_uiout, nullptr);
922 current_uiout->field_core_addr ("start", gdbarch, m.addr);
923 current_uiout->field_core_addr ("end", gdbarch, m.endaddr);
924 /* These next two aren't really addresses and so
925 shouldn't be styled as such. */
926 current_uiout->field_string ("size",
927 paddress (gdbarch,
928 m.endaddr - m.addr));
929 current_uiout->field_string ("offset",
930 paddress (gdbarch, m.offset));
931 current_uiout->field_string ("perms", m.permissions);
932 current_uiout->field_string ("objfile", m.filename,
933 file_name_style.style ());
934 current_uiout->text ("\n");
937 else
938 warning (_("unable to open /proc file '%s'"), filename);
940 if (status_f)
942 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
943 gdb::unique_xmalloc_ptr<char> status
944 = target_fileio_read_stralloc (NULL, filename);
945 if (status)
946 gdb_puts (status.get ());
947 else
948 warning (_("unable to open /proc file '%s'"), filename);
950 if (stat_f)
952 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
953 gdb::unique_xmalloc_ptr<char> statstr
954 = target_fileio_read_stralloc (NULL, filename);
955 if (statstr)
957 const char *p = statstr.get ();
959 gdb_printf (_("Process: %s\n"),
960 pulongest (strtoulst (p, &p, 10)));
962 p = skip_spaces (p);
963 if (*p == '(')
965 /* ps command also relies on no trailing fields
966 ever contain ')'. */
967 const char *ep = strrchr (p, ')');
968 if (ep != NULL)
970 gdb_printf ("Exec file: %.*s\n",
971 (int) (ep - p - 1), p + 1);
972 p = ep + 1;
976 p = skip_spaces (p);
977 if (*p)
978 gdb_printf (_("State: %c\n"), *p++);
980 if (*p)
981 gdb_printf (_("Parent process: %s\n"),
982 pulongest (strtoulst (p, &p, 10)));
983 if (*p)
984 gdb_printf (_("Process group: %s\n"),
985 pulongest (strtoulst (p, &p, 10)));
986 if (*p)
987 gdb_printf (_("Session id: %s\n"),
988 pulongest (strtoulst (p, &p, 10)));
989 if (*p)
990 gdb_printf (_("TTY: %s\n"),
991 pulongest (strtoulst (p, &p, 10)));
992 if (*p)
993 gdb_printf (_("TTY owner process group: %s\n"),
994 pulongest (strtoulst (p, &p, 10)));
996 if (*p)
997 gdb_printf (_("Flags: %s\n"),
998 hex_string (strtoulst (p, &p, 10)));
999 if (*p)
1000 gdb_printf (_("Minor faults (no memory page): %s\n"),
1001 pulongest (strtoulst (p, &p, 10)));
1002 if (*p)
1003 gdb_printf (_("Minor faults, children: %s\n"),
1004 pulongest (strtoulst (p, &p, 10)));
1005 if (*p)
1006 gdb_printf (_("Major faults (memory page faults): %s\n"),
1007 pulongest (strtoulst (p, &p, 10)));
1008 if (*p)
1009 gdb_printf (_("Major faults, children: %s\n"),
1010 pulongest (strtoulst (p, &p, 10)));
1011 if (*p)
1012 gdb_printf (_("utime: %s\n"),
1013 pulongest (strtoulst (p, &p, 10)));
1014 if (*p)
1015 gdb_printf (_("stime: %s\n"),
1016 pulongest (strtoulst (p, &p, 10)));
1017 if (*p)
1018 gdb_printf (_("utime, children: %s\n"),
1019 pulongest (strtoulst (p, &p, 10)));
1020 if (*p)
1021 gdb_printf (_("stime, children: %s\n"),
1022 pulongest (strtoulst (p, &p, 10)));
1023 if (*p)
1024 gdb_printf (_("jiffies remaining in current "
1025 "time slice: %s\n"),
1026 pulongest (strtoulst (p, &p, 10)));
1027 if (*p)
1028 gdb_printf (_("'nice' value: %s\n"),
1029 pulongest (strtoulst (p, &p, 10)));
1030 if (*p)
1031 gdb_printf (_("jiffies until next timeout: %s\n"),
1032 pulongest (strtoulst (p, &p, 10)));
1033 if (*p)
1034 gdb_printf (_("jiffies until next SIGALRM: %s\n"),
1035 pulongest (strtoulst (p, &p, 10)));
1036 if (*p)
1037 gdb_printf (_("start time (jiffies since "
1038 "system boot): %s\n"),
1039 pulongest (strtoulst (p, &p, 10)));
1040 if (*p)
1041 gdb_printf (_("Virtual memory size: %s\n"),
1042 pulongest (strtoulst (p, &p, 10)));
1043 if (*p)
1044 gdb_printf (_("Resident set size: %s\n"),
1045 pulongest (strtoulst (p, &p, 10)));
1046 if (*p)
1047 gdb_printf (_("rlim: %s\n"),
1048 pulongest (strtoulst (p, &p, 10)));
1049 if (*p)
1050 gdb_printf (_("Start of text: %s\n"),
1051 hex_string (strtoulst (p, &p, 10)));
1052 if (*p)
1053 gdb_printf (_("End of text: %s\n"),
1054 hex_string (strtoulst (p, &p, 10)));
1055 if (*p)
1056 gdb_printf (_("Start of stack: %s\n"),
1057 hex_string (strtoulst (p, &p, 10)));
1058 #if 0 /* Don't know how architecture-dependent the rest is...
1059 Anyway the signal bitmap info is available from "status". */
1060 if (*p)
1061 gdb_printf (_("Kernel stack pointer: %s\n"),
1062 hex_string (strtoulst (p, &p, 10)));
1063 if (*p)
1064 gdb_printf (_("Kernel instr pointer: %s\n"),
1065 hex_string (strtoulst (p, &p, 10)));
1066 if (*p)
1067 gdb_printf (_("Pending signals bitmap: %s\n"),
1068 hex_string (strtoulst (p, &p, 10)));
1069 if (*p)
1070 gdb_printf (_("Blocked signals bitmap: %s\n"),
1071 hex_string (strtoulst (p, &p, 10)));
1072 if (*p)
1073 gdb_printf (_("Ignored signals bitmap: %s\n"),
1074 hex_string (strtoulst (p, &p, 10)));
1075 if (*p)
1076 gdb_printf (_("Catched signals bitmap: %s\n"),
1077 hex_string (strtoulst (p, &p, 10)));
1078 if (*p)
1079 gdb_printf (_("wchan (system call): %s\n"),
1080 hex_string (strtoulst (p, &p, 10)));
1081 #endif
1083 else
1084 warning (_("unable to open /proc file '%s'"), filename);
1088 /* Implementation of `gdbarch_read_core_file_mappings', as defined in
1089 gdbarch.h.
1091 This function reads the NT_FILE note (which BFD turns into the
1092 section ".note.linuxcore.file"). The format of this note / section
1093 is described as follows in the Linux kernel sources in
1094 fs/binfmt_elf.c:
1096 long count -- how many files are mapped
1097 long page_size -- units for file_ofs
1098 array of [COUNT] elements of
1099 long start
1100 long end
1101 long file_ofs
1102 followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
1104 CBFD is the BFD of the core file.
1106 PRE_LOOP_CB is the callback function to invoke prior to starting
1107 the loop which processes individual entries. This callback will
1108 only be executed after the note has been examined in enough
1109 detail to verify that it's not malformed in some way.
1111 LOOP_CB is the callback function that will be executed once
1112 for each mapping. */
1114 static void
1115 linux_read_core_file_mappings
1116 (struct gdbarch *gdbarch,
1117 struct bfd *cbfd,
1118 read_core_file_mappings_pre_loop_ftype pre_loop_cb,
1119 read_core_file_mappings_loop_ftype loop_cb)
1121 /* Ensure that ULONGEST is big enough for reading 64-bit core files. */
1122 static_assert (sizeof (ULONGEST) >= 8);
1124 /* It's not required that the NT_FILE note exists, so return silently
1125 if it's not found. Beyond this point though, we'll complain
1126 if problems are found. */
1127 asection *section = bfd_get_section_by_name (cbfd, ".note.linuxcore.file");
1128 if (section == nullptr)
1129 return;
1131 unsigned int addr_size_bits = gdbarch_addr_bit (gdbarch);
1132 unsigned int addr_size = addr_size_bits / 8;
1133 size_t note_size = bfd_section_size (section);
1135 if (note_size < 2 * addr_size)
1137 warning (_("malformed core note - too short for header"));
1138 return;
1141 gdb::byte_vector contents (note_size);
1142 if (!bfd_get_section_contents (current_program_space->core_bfd (), section,
1143 contents.data (), 0, note_size))
1145 warning (_("could not get core note contents"));
1146 return;
1149 gdb_byte *descdata = contents.data ();
1150 char *descend = (char *) descdata + note_size;
1152 if (descdata[note_size - 1] != '\0')
1154 warning (_("malformed note - does not end with \\0"));
1155 return;
1158 ULONGEST count = bfd_get (addr_size_bits, current_program_space->core_bfd (),
1159 descdata);
1160 descdata += addr_size;
1162 ULONGEST page_size = bfd_get (addr_size_bits,
1163 current_program_space->core_bfd (),
1164 descdata);
1165 descdata += addr_size;
1167 if (note_size < 2 * addr_size + count * 3 * addr_size)
1169 warning (_("malformed note - too short for supplied file count"));
1170 return;
1173 char *filenames = (char *) descdata + count * 3 * addr_size;
1175 /* Make sure that the correct number of filenames exist. Complain
1176 if there aren't enough or are too many. */
1177 char *f = filenames;
1178 for (int i = 0; i < count; i++)
1180 if (f >= descend)
1182 warning (_("malformed note - filename area is too small"));
1183 return;
1185 f += strnlen (f, descend - f) + 1;
1187 /* Complain, but don't return early if the filename area is too big. */
1188 if (f != descend)
1189 warning (_("malformed note - filename area is too big"));
1191 const bfd_build_id *orig_build_id = cbfd->build_id;
1192 std::unordered_map<ULONGEST, const bfd_build_id *> vma_map;
1194 /* Search for solib build-ids in the core file. Each time one is found,
1195 map the start vma of the corresponding elf header to the build-id. */
1196 for (bfd_section *sec = cbfd->sections; sec != nullptr; sec = sec->next)
1198 cbfd->build_id = nullptr;
1200 if (sec->flags & SEC_LOAD
1201 && (get_elf_backend_data (cbfd)->elf_backend_core_find_build_id
1202 (cbfd, (bfd_vma) sec->filepos)))
1203 vma_map[sec->vma] = cbfd->build_id;
1206 cbfd->build_id = orig_build_id;
1207 pre_loop_cb (count);
1209 for (int i = 0; i < count; i++)
1211 ULONGEST start = bfd_get (addr_size_bits, current_program_space->core_bfd (), descdata);
1212 descdata += addr_size;
1213 ULONGEST end = bfd_get (addr_size_bits, current_program_space->core_bfd (), descdata);
1214 descdata += addr_size;
1215 ULONGEST file_ofs
1216 = bfd_get (addr_size_bits, current_program_space->core_bfd (), descdata) * page_size;
1217 descdata += addr_size;
1218 char * filename = filenames;
1219 filenames += strlen ((char *) filenames) + 1;
1220 const bfd_build_id *build_id = nullptr;
1221 auto vma_map_it = vma_map.find (start);
1223 if (vma_map_it != vma_map.end ())
1224 build_id = vma_map_it->second;
1226 loop_cb (i, start, end, file_ofs, filename, build_id);
1230 /* Implement "info proc mappings" for a corefile. */
1232 static void
1233 linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
1235 std::optional<ui_out_emit_table> emitter;
1237 linux_read_core_file_mappings (gdbarch, current_program_space->core_bfd (),
1238 [&] (ULONGEST count)
1240 gdb_printf (_("Mapped address spaces:\n\n"));
1241 emitter.emplace (current_uiout, 5, -1, "ProcMappings");
1242 int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
1243 current_uiout->table_header (width, ui_left, "start", "Start Addr");
1244 current_uiout->table_header (width, ui_left, "end", "End Addr");
1245 current_uiout->table_header (width, ui_left, "size", "Size");
1246 current_uiout->table_header (width, ui_left, "offset", "Offset");
1247 current_uiout->table_header (0, ui_left, "objfile", "File");
1248 current_uiout->table_body ();
1250 [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
1251 const char *filename, const bfd_build_id *build_id)
1253 ui_out_emit_tuple tuple_emitter (current_uiout, nullptr);
1254 current_uiout->field_core_addr ("start", gdbarch, start);
1255 current_uiout->field_core_addr ("end", gdbarch, end);
1256 /* These next two aren't really addresses and so shouldn't be
1257 styled as such. */
1258 current_uiout->field_string ("size", paddress (gdbarch, end - start));
1259 current_uiout->field_string ("offset", paddress (gdbarch, file_ofs));
1260 current_uiout->field_string ("objfile", filename,
1261 file_name_style.style ());
1262 current_uiout->text ("\n");
1266 /* Implement "info proc" for a corefile. */
1268 static void
1269 linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
1270 enum info_proc_what what)
1272 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
1273 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
1275 if (exe_f)
1277 const char *exe
1278 = bfd_core_file_failing_command (current_program_space->core_bfd ());
1280 if (exe != NULL)
1281 gdb_printf ("exe = '%s'\n", exe);
1282 else
1283 warning (_("unable to find command name in core file"));
1286 if (mappings_f)
1287 linux_core_info_proc_mappings (gdbarch, args);
1289 if (!exe_f && !mappings_f)
1290 error (_("unable to handle request"));
1293 /* Read siginfo data from the core, if possible. Returns -1 on
1294 failure. Otherwise, returns the number of bytes read. READBUF,
1295 OFFSET, and LEN are all as specified by the to_xfer_partial
1296 interface. */
1298 static LONGEST
1299 linux_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
1300 ULONGEST offset, ULONGEST len)
1302 thread_section_name section_name (".note.linuxcore.siginfo", inferior_ptid);
1303 asection *section
1304 = bfd_get_section_by_name (current_program_space->core_bfd (),
1305 section_name.c_str ());
1306 if (section == NULL)
1307 return -1;
1309 if (!bfd_get_section_contents (current_program_space->core_bfd (), section,
1310 readbuf, offset, len))
1311 return -1;
1313 return len;
1316 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
1317 ULONGEST offset, ULONGEST inode,
1318 int read, int write,
1319 int exec, int modified,
1320 bool memory_tagged,
1321 const char *filename,
1322 void *data);
1324 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags,
1325 const struct smaps_vmflags *v,
1326 int maybe_private_p,
1327 int mapping_anon_p,
1328 int mapping_file_p,
1329 const char *filename,
1330 ULONGEST addr,
1331 ULONGEST offset);
1333 /* Helper function to parse the contents of /proc/<pid>/smaps into a data
1334 structure, for easy access.
1336 DATA is the contents of the smaps file. The parsed contents are stored
1337 into the SMAPS vector. */
1339 static std::vector<struct smaps_data>
1340 parse_smaps_data (const char *data,
1341 const std::string maps_filename)
1343 char *line, *t;
1345 gdb_assert (data != nullptr);
1347 line = strtok_r ((char *) data, "\n", &t);
1349 std::vector<struct smaps_data> smaps;
1351 while (line != NULL)
1353 struct smaps_vmflags v;
1354 int read, write, exec, priv;
1355 int has_anonymous = 0;
1356 int mapping_anon_p;
1357 int mapping_file_p;
1359 memset (&v, 0, sizeof (v));
1360 struct mapping m = read_mapping (line);
1361 mapping_anon_p = mapping_is_anonymous_p (m.filename);
1362 /* If the mapping is not anonymous, then we can consider it
1363 to be file-backed. These two states (anonymous or
1364 file-backed) seem to be exclusive, but they can actually
1365 coexist. For example, if a file-backed mapping has
1366 "Anonymous:" pages (see more below), then the Linux
1367 kernel will dump this mapping when the user specified
1368 that she only wants anonymous mappings in the corefile
1369 (*even* when she explicitly disabled the dumping of
1370 file-backed mappings). */
1371 mapping_file_p = !mapping_anon_p;
1373 /* Decode permissions. */
1374 auto has_perm = [&m] (char c)
1375 { return m.permissions.find (c) != std::string_view::npos; };
1376 read = has_perm ('r');
1377 write = has_perm ('w');
1378 exec = has_perm ('x');
1380 /* 'private' here actually means VM_MAYSHARE, and not
1381 VM_SHARED. In order to know if a mapping is really
1382 private or not, we must check the flag "sh" in the
1383 VmFlags field. This is done by decode_vmflags. However,
1384 if we are using a Linux kernel released before the commit
1385 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
1386 not have the VmFlags there. In this case, there is
1387 really no way to know if we are dealing with VM_SHARED,
1388 so we just assume that VM_MAYSHARE is enough. */
1389 priv = has_perm ('p');
1391 /* Try to detect if region should be dumped by parsing smaps
1392 counters. */
1393 for (line = strtok_r (NULL, "\n", &t);
1394 line != NULL && line[0] >= 'A' && line[0] <= 'Z';
1395 line = strtok_r (NULL, "\n", &t))
1397 char keyword[64 + 1];
1399 if (sscanf (line, "%64s", keyword) != 1)
1401 warning (_("Error parsing {s,}maps file '%s'"),
1402 maps_filename.c_str ());
1403 break;
1406 if (strcmp (keyword, "Anonymous:") == 0)
1408 /* Older Linux kernels did not support the
1409 "Anonymous:" counter. Check it here. */
1410 has_anonymous = 1;
1412 else if (strcmp (keyword, "VmFlags:") == 0)
1413 decode_vmflags (line, &v);
1415 if (strcmp (keyword, "AnonHugePages:") == 0
1416 || strcmp (keyword, "Anonymous:") == 0)
1418 unsigned long number;
1420 if (sscanf (line, "%*s%lu", &number) != 1)
1422 warning (_("Error parsing {s,}maps file '%s' number"),
1423 maps_filename.c_str ());
1424 break;
1426 if (number > 0)
1428 /* Even if we are dealing with a file-backed
1429 mapping, if it contains anonymous pages we
1430 consider it to be *also* an anonymous
1431 mapping, because this is what the Linux
1432 kernel does:
1434 // Dump segments that have been written to.
1435 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1436 goto whole;
1438 Note that if the mapping is already marked as
1439 file-backed (i.e., mapping_file_p is
1440 non-zero), then this is a special case, and
1441 this mapping will be dumped either when the
1442 user wants to dump file-backed *or* anonymous
1443 mappings. */
1444 mapping_anon_p = 1;
1448 /* Save the smaps entry to the vector. */
1449 struct smaps_data map;
1451 map.start_address = m.addr;
1452 map.end_address = m.endaddr;
1453 map.filename = m.filename;
1454 map.vmflags = v;
1455 map.read = read? true : false;
1456 map.write = write? true : false;
1457 map.exec = exec? true : false;
1458 map.priv = priv? true : false;
1459 map.has_anonymous = has_anonymous;
1460 map.mapping_anon_p = mapping_anon_p? true : false;
1461 map.mapping_file_p = mapping_file_p? true : false;
1462 map.offset = m.offset;
1463 map.inode = m.inode;
1465 smaps.emplace_back (map);
1468 return smaps;
1471 /* Helper that checks if an address is in a memory tag page for a live
1472 process. */
1474 static bool
1475 linux_process_address_in_memtag_page (CORE_ADDR address)
1477 if (current_inferior ()->fake_pid_p)
1478 return false;
1480 pid_t pid = current_inferior ()->pid;
1482 std::string smaps_file = string_printf ("/proc/%d/smaps", pid);
1484 gdb::unique_xmalloc_ptr<char> data
1485 = target_fileio_read_stralloc (NULL, smaps_file.c_str ());
1487 if (data == nullptr)
1488 return false;
1490 /* Parse the contents of smaps into a vector. */
1491 std::vector<struct smaps_data> smaps
1492 = parse_smaps_data (data.get (), smaps_file);
1494 for (const smaps_data &map : smaps)
1496 /* Is the address within [start_address, end_address) in a page
1497 mapped with memory tagging? */
1498 if (address >= map.start_address
1499 && address < map.end_address
1500 && map.vmflags.memory_tagging)
1501 return true;
1504 return false;
1507 /* Helper that checks if an address is in a memory tag page for a core file
1508 process. */
1510 static bool
1511 linux_core_file_address_in_memtag_page (CORE_ADDR address)
1513 if (current_program_space->core_bfd () == nullptr)
1514 return false;
1516 memtag_section_info info;
1517 return get_next_core_memtag_section (current_program_space->core_bfd (),
1518 nullptr, address, info);
1521 /* See linux-tdep.h. */
1523 bool
1524 linux_address_in_memtag_page (CORE_ADDR address)
1526 if (!target_has_execution ())
1527 return linux_core_file_address_in_memtag_page (address);
1529 return linux_process_address_in_memtag_page (address);
1532 /* List memory regions in the inferior for a corefile. */
1534 static int
1535 linux_find_memory_regions_full (struct gdbarch *gdbarch,
1536 linux_dump_mapping_p_ftype *should_dump_mapping_p,
1537 linux_find_memory_region_ftype *func,
1538 void *obfd)
1540 pid_t pid;
1541 /* Default dump behavior of coredump_filter (0x33), according to
1542 Documentation/filesystems/proc.txt from the Linux kernel
1543 tree. */
1544 filter_flags filterflags = (COREFILTER_ANON_PRIVATE
1545 | COREFILTER_ANON_SHARED
1546 | COREFILTER_ELF_HEADERS
1547 | COREFILTER_HUGETLB_PRIVATE);
1549 /* We need to know the real target PID to access /proc. */
1550 if (current_inferior ()->fake_pid_p)
1551 return 1;
1553 pid = current_inferior ()->pid;
1555 if (use_coredump_filter)
1557 std::string core_dump_filter_name
1558 = string_printf ("/proc/%d/coredump_filter", pid);
1560 gdb::unique_xmalloc_ptr<char> coredumpfilterdata
1561 = target_fileio_read_stralloc (NULL, core_dump_filter_name.c_str ());
1563 if (coredumpfilterdata != NULL)
1565 unsigned int flags;
1567 sscanf (coredumpfilterdata.get (), "%x", &flags);
1568 filterflags = (enum filter_flag) flags;
1572 std::string maps_filename = string_printf ("/proc/%d/smaps", pid);
1574 gdb::unique_xmalloc_ptr<char> data
1575 = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
1577 if (data == NULL)
1579 /* Older Linux kernels did not support /proc/PID/smaps. */
1580 maps_filename = string_printf ("/proc/%d/maps", pid);
1581 data = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
1583 if (data == nullptr)
1584 return 1;
1587 /* Parse the contents of smaps into a vector. */
1588 std::vector<struct smaps_data> smaps
1589 = parse_smaps_data (data.get (), maps_filename.c_str ());
1591 for (const struct smaps_data &map : smaps)
1593 int should_dump_p = 0;
1595 if (map.has_anonymous)
1597 should_dump_p
1598 = should_dump_mapping_p (filterflags, &map.vmflags,
1599 map.priv,
1600 map.mapping_anon_p,
1601 map.mapping_file_p,
1602 map.filename.c_str (),
1603 map.start_address,
1604 map.offset);
1606 else
1608 /* Older Linux kernels did not support the "Anonymous:" counter.
1609 If it is missing, we can't be sure - dump all the pages. */
1610 should_dump_p = 1;
1613 /* Invoke the callback function to create the corefile segment. */
1614 if (should_dump_p)
1616 func (map.start_address, map.end_address - map.start_address,
1617 map.offset, map.inode, map.read, map.write, map.exec,
1618 1, /* MODIFIED is true because we want to dump
1619 the mapping. */
1620 map.vmflags.memory_tagging != 0,
1621 map.filename.c_str (), obfd);
1625 return 0;
1628 /* A structure for passing information through
1629 linux_find_memory_regions_full. */
1631 struct linux_find_memory_regions_data
1633 /* The original callback. */
1635 find_memory_region_ftype func;
1637 /* The original datum. */
1639 void *obfd;
1642 /* A callback for linux_find_memory_regions that converts between the
1643 "full"-style callback and find_memory_region_ftype. */
1645 static int
1646 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
1647 ULONGEST offset, ULONGEST inode,
1648 int read, int write, int exec, int modified,
1649 bool memory_tagged,
1650 const char *filename, void *arg)
1652 struct linux_find_memory_regions_data *data
1653 = (struct linux_find_memory_regions_data *) arg;
1655 return data->func (vaddr, size, read, write, exec, modified, memory_tagged,
1656 data->obfd);
1659 /* A variant of linux_find_memory_regions_full that is suitable as the
1660 gdbarch find_memory_regions method. */
1662 static int
1663 linux_find_memory_regions (struct gdbarch *gdbarch,
1664 find_memory_region_ftype func, void *obfd)
1666 struct linux_find_memory_regions_data data;
1668 data.func = func;
1669 data.obfd = obfd;
1671 return linux_find_memory_regions_full (gdbarch,
1672 dump_mapping_p,
1673 linux_find_memory_regions_thunk,
1674 &data);
1677 /* This is used to pass information from
1678 linux_make_mappings_corefile_notes through
1679 linux_find_memory_regions_full. */
1681 struct linux_make_mappings_data
1683 /* Number of files mapped. */
1684 ULONGEST file_count;
1686 /* The obstack for the main part of the data. */
1687 struct obstack *data_obstack;
1689 /* The filename obstack. */
1690 struct obstack *filename_obstack;
1692 /* The architecture's "long" type. */
1693 struct type *long_type;
1696 static linux_find_memory_region_ftype linux_make_mappings_callback;
1698 /* A callback for linux_find_memory_regions_full that updates the
1699 mappings data for linux_make_mappings_corefile_notes.
1701 MEMORY_TAGGED is true if the memory region contains memory tags, false
1702 otherwise. */
1704 static int
1705 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
1706 ULONGEST offset, ULONGEST inode,
1707 int read, int write, int exec, int modified,
1708 bool memory_tagged,
1709 const char *filename, void *data)
1711 struct linux_make_mappings_data *map_data
1712 = (struct linux_make_mappings_data *) data;
1713 gdb_byte buf[sizeof (ULONGEST)];
1715 if (*filename == '\0' || inode == 0)
1716 return 0;
1718 ++map_data->file_count;
1720 pack_long (buf, map_data->long_type, vaddr);
1721 obstack_grow (map_data->data_obstack, buf, map_data->long_type->length ());
1722 pack_long (buf, map_data->long_type, vaddr + size);
1723 obstack_grow (map_data->data_obstack, buf, map_data->long_type->length ());
1724 pack_long (buf, map_data->long_type, offset);
1725 obstack_grow (map_data->data_obstack, buf, map_data->long_type->length ());
1727 obstack_grow_str0 (map_data->filename_obstack, filename);
1729 return 0;
1732 /* Write the file mapping data to the core file, if possible. OBFD is
1733 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1734 is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
1736 static void
1737 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
1738 gdb::unique_xmalloc_ptr<char> &note_data,
1739 int *note_size)
1741 struct linux_make_mappings_data mapping_data;
1742 type_allocator alloc (gdbarch);
1743 struct type *long_type
1744 = init_integer_type (alloc, gdbarch_long_bit (gdbarch), 0, "long");
1745 gdb_byte buf[sizeof (ULONGEST)];
1747 auto_obstack data_obstack, filename_obstack;
1749 mapping_data.file_count = 0;
1750 mapping_data.data_obstack = &data_obstack;
1751 mapping_data.filename_obstack = &filename_obstack;
1752 mapping_data.long_type = long_type;
1754 /* Reserve space for the count. */
1755 obstack_blank (&data_obstack, long_type->length ());
1756 /* We always write the page size as 1 since we have no good way to
1757 determine the correct value. */
1758 pack_long (buf, long_type, 1);
1759 obstack_grow (&data_obstack, buf, long_type->length ());
1761 linux_find_memory_regions_full (gdbarch,
1762 dump_note_entry_p,
1763 linux_make_mappings_callback,
1764 &mapping_data);
1766 if (mapping_data.file_count != 0)
1768 /* Write the count to the obstack. */
1769 pack_long ((gdb_byte *) obstack_base (&data_obstack),
1770 long_type, mapping_data.file_count);
1772 /* Copy the filenames to the data obstack. */
1773 int size = obstack_object_size (&filename_obstack);
1774 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
1775 size);
1777 note_data.reset (elfcore_write_file_note (obfd, note_data.release (), note_size,
1778 obstack_base (&data_obstack),
1779 obstack_object_size (&data_obstack)));
1783 /* Fetch the siginfo data for the specified thread, if it exists. If
1784 there is no data, or we could not read it, return an empty
1785 buffer. */
1787 static gdb::byte_vector
1788 linux_get_siginfo_data (thread_info *thread, struct gdbarch *gdbarch)
1790 struct type *siginfo_type;
1791 LONGEST bytes_read;
1793 if (!gdbarch_get_siginfo_type_p (gdbarch))
1794 return gdb::byte_vector ();
1796 scoped_restore_current_thread save_current_thread;
1797 switch_to_thread (thread);
1799 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1801 gdb::byte_vector buf (siginfo_type->length ());
1803 bytes_read = target_read (current_inferior ()->top_target (),
1804 TARGET_OBJECT_SIGNAL_INFO, NULL,
1805 buf.data (), 0, siginfo_type->length ());
1806 if (bytes_read != siginfo_type->length ())
1807 buf.clear ();
1809 return buf;
1812 /* Records the thread's register state for the corefile note
1813 section. */
1815 static void
1816 linux_corefile_thread (struct thread_info *info,
1817 struct gdbarch *gdbarch, bfd *obfd,
1818 gdb::unique_xmalloc_ptr<char> &note_data,
1819 int *note_size, gdb_signal stop_signal)
1821 gcore_elf_build_thread_register_notes (gdbarch, info, stop_signal, obfd,
1822 &note_data, note_size);
1824 /* Don't return anything if we got no register information above,
1825 such a core file is useless. */
1826 if (note_data != nullptr)
1828 gdb::byte_vector siginfo_data
1829 = linux_get_siginfo_data (info, gdbarch);
1830 if (!siginfo_data.empty ())
1831 note_data.reset (elfcore_write_note (obfd, note_data.release (),
1832 note_size, "CORE", NT_SIGINFO,
1833 siginfo_data.data (),
1834 siginfo_data.size ()));
1838 /* Fill the PRPSINFO structure with information about the process being
1839 debugged. Returns 1 in case of success, 0 for failures. Please note that
1840 even if the structure cannot be entirely filled (e.g., GDB was unable to
1841 gather information about the process UID/GID), this function will still
1842 return 1 since some information was already recorded. It will only return
1843 0 iff nothing can be gathered. */
1845 static int
1846 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
1848 /* The filename which we will use to obtain some info about the process.
1849 We will basically use this to store the `/proc/PID/FILENAME' file. */
1850 char filename[100];
1851 /* The basename of the executable. */
1852 const char *basename;
1853 /* Temporary buffer. */
1854 char *tmpstr;
1855 /* The valid states of a process, according to the Linux kernel. */
1856 const char valid_states[] = "RSDTZW";
1857 /* The program state. */
1858 const char *prog_state;
1859 /* The state of the process. */
1860 char pr_sname;
1861 /* The PID of the program which generated the corefile. */
1862 pid_t pid;
1863 /* Process flags. */
1864 unsigned int pr_flag;
1865 /* Process nice value. */
1866 long pr_nice;
1867 /* The number of fields read by `sscanf'. */
1868 int n_fields = 0;
1870 gdb_assert (p != NULL);
1872 /* Obtaining PID and filename. */
1873 pid = inferior_ptid.pid ();
1874 xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
1875 /* The full name of the program which generated the corefile. */
1876 gdb_byte *buf = NULL;
1877 size_t buf_len = target_fileio_read_alloc (NULL, filename, &buf);
1878 gdb::unique_xmalloc_ptr<char> fname ((char *)buf);
1880 if (buf_len < 1 || fname.get ()[0] == '\0')
1882 /* No program name was read, so we won't be able to retrieve more
1883 information about the process. */
1884 return 0;
1886 if (fname.get ()[buf_len - 1] != '\0')
1888 warning (_("target file %s "
1889 "does not contain a trailing null character"),
1890 filename);
1891 return 0;
1894 memset (p, 0, sizeof (*p));
1896 /* Defining the PID. */
1897 p->pr_pid = pid;
1899 /* Copying the program name. Only the basename matters. */
1900 basename = lbasename (fname.get ());
1901 strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
1902 p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
1904 const std::string &infargs = current_inferior ()->args ();
1906 /* The arguments of the program. */
1907 std::string psargs = fname.get ();
1908 if (!infargs.empty ())
1909 psargs += ' ' + infargs;
1911 strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
1912 p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
1914 xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
1915 /* The contents of `/proc/PID/stat'. */
1916 gdb::unique_xmalloc_ptr<char> proc_stat_contents
1917 = target_fileio_read_stralloc (NULL, filename);
1918 char *proc_stat = proc_stat_contents.get ();
1920 if (proc_stat == NULL || *proc_stat == '\0')
1922 /* Despite being unable to read more information about the
1923 process, we return 1 here because at least we have its
1924 command line, PID and arguments. */
1925 return 1;
1928 /* Ok, we have the stats. It's time to do a little parsing of the
1929 contents of the buffer, so that we end up reading what we want.
1931 The following parsing mechanism is strongly based on the
1932 information generated by the `fs/proc/array.c' file, present in
1933 the Linux kernel tree. More details about how the information is
1934 displayed can be obtained by seeing the manpage of proc(5),
1935 specifically under the entry of `/proc/[pid]/stat'. */
1937 /* Getting rid of the PID, since we already have it. */
1938 while (isdigit (*proc_stat))
1939 ++proc_stat;
1941 proc_stat = skip_spaces (proc_stat);
1943 /* ps command also relies on no trailing fields ever contain ')'. */
1944 proc_stat = strrchr (proc_stat, ')');
1945 if (proc_stat == NULL)
1946 return 1;
1947 proc_stat++;
1949 proc_stat = skip_spaces (proc_stat);
1951 n_fields = sscanf (proc_stat,
1952 "%c" /* Process state. */
1953 "%d%d%d" /* Parent PID, group ID, session ID. */
1954 "%*d%*d" /* tty_nr, tpgid (not used). */
1955 "%u" /* Flags. */
1956 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1957 cmajflt (not used). */
1958 "%*s%*s%*s%*s" /* utime, stime, cutime,
1959 cstime (not used). */
1960 "%*s" /* Priority (not used). */
1961 "%ld", /* Nice. */
1962 &pr_sname,
1963 &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1964 &pr_flag,
1965 &pr_nice);
1967 if (n_fields != 6)
1969 /* Again, we couldn't read the complementary information about
1970 the process state. However, we already have minimal
1971 information, so we just return 1 here. */
1972 return 1;
1975 /* Filling the structure fields. */
1976 prog_state = strchr (valid_states, pr_sname);
1977 if (prog_state != NULL)
1978 p->pr_state = prog_state - valid_states;
1979 else
1981 /* Zero means "Running". */
1982 p->pr_state = 0;
1985 p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
1986 p->pr_zomb = p->pr_sname == 'Z';
1987 p->pr_nice = pr_nice;
1988 p->pr_flag = pr_flag;
1990 /* Finally, obtaining the UID and GID. For that, we read and parse the
1991 contents of the `/proc/PID/status' file. */
1992 xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
1993 /* The contents of `/proc/PID/status'. */
1994 gdb::unique_xmalloc_ptr<char> proc_status_contents
1995 = target_fileio_read_stralloc (NULL, filename);
1996 char *proc_status = proc_status_contents.get ();
1998 if (proc_status == NULL || *proc_status == '\0')
2000 /* Returning 1 since we already have a bunch of information. */
2001 return 1;
2004 /* Extracting the UID. */
2005 tmpstr = strstr (proc_status, "Uid:");
2006 if (tmpstr != NULL)
2008 /* Advancing the pointer to the beginning of the UID. */
2009 tmpstr += sizeof ("Uid:");
2010 while (*tmpstr != '\0' && !isdigit (*tmpstr))
2011 ++tmpstr;
2013 if (isdigit (*tmpstr))
2014 p->pr_uid = strtol (tmpstr, &tmpstr, 10);
2017 /* Extracting the GID. */
2018 tmpstr = strstr (proc_status, "Gid:");
2019 if (tmpstr != NULL)
2021 /* Advancing the pointer to the beginning of the GID. */
2022 tmpstr += sizeof ("Gid:");
2023 while (*tmpstr != '\0' && !isdigit (*tmpstr))
2024 ++tmpstr;
2026 if (isdigit (*tmpstr))
2027 p->pr_gid = strtol (tmpstr, &tmpstr, 10);
2030 return 1;
2033 /* Build the note section for a corefile, and return it in a malloc
2034 buffer. */
2036 static gdb::unique_xmalloc_ptr<char>
2037 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
2039 struct elf_internal_linux_prpsinfo prpsinfo;
2040 gdb::unique_xmalloc_ptr<char> note_data;
2042 if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
2043 return NULL;
2045 if (linux_fill_prpsinfo (&prpsinfo))
2047 if (gdbarch_ptr_bit (gdbarch) == 64)
2048 note_data.reset (elfcore_write_linux_prpsinfo64 (obfd,
2049 note_data.release (),
2050 note_size, &prpsinfo));
2051 else
2052 note_data.reset (elfcore_write_linux_prpsinfo32 (obfd,
2053 note_data.release (),
2054 note_size, &prpsinfo));
2057 /* Thread register information. */
2060 update_thread_list ();
2062 catch (const gdb_exception_error &e)
2064 exception_print (gdb_stderr, e);
2067 /* Like the kernel, prefer dumping the signalled thread first.
2068 "First thread" is what tools use to infer the signalled
2069 thread. */
2070 thread_info *signalled_thr = gcore_find_signalled_thread ();
2071 gdb_signal stop_signal;
2072 if (signalled_thr != nullptr)
2073 stop_signal = signalled_thr->stop_signal ();
2074 else
2075 stop_signal = GDB_SIGNAL_0;
2077 if (signalled_thr != nullptr)
2079 /* On some architectures, like AArch64, each thread can have a distinct
2080 gdbarch (due to scalable extensions), and using the inferior gdbarch
2081 is incorrect.
2083 Fetch each thread's gdbarch and pass it down to the lower layers so
2084 we can dump the right set of registers. */
2085 linux_corefile_thread (signalled_thr,
2086 target_thread_architecture (signalled_thr->ptid),
2087 obfd, note_data, note_size, stop_signal);
2089 for (thread_info *thr : current_inferior ()->non_exited_threads ())
2091 if (thr == signalled_thr)
2092 continue;
2094 /* On some architectures, like AArch64, each thread can have a distinct
2095 gdbarch (due to scalable extensions), and using the inferior gdbarch
2096 is incorrect.
2098 Fetch each thread's gdbarch and pass it down to the lower layers so
2099 we can dump the right set of registers. */
2100 linux_corefile_thread (thr, target_thread_architecture (thr->ptid),
2101 obfd, note_data, note_size, stop_signal);
2104 if (!note_data)
2105 return NULL;
2107 /* Auxiliary vector. */
2108 std::optional<gdb::byte_vector> auxv =
2109 target_read_alloc (current_inferior ()->top_target (),
2110 TARGET_OBJECT_AUXV, NULL);
2111 if (auxv && !auxv->empty ())
2113 note_data.reset (elfcore_write_note (obfd, note_data.release (),
2114 note_size, "CORE", NT_AUXV,
2115 auxv->data (), auxv->size ()));
2117 if (!note_data)
2118 return NULL;
2121 /* File mappings. */
2122 linux_make_mappings_corefile_notes (gdbarch, obfd, note_data, note_size);
2124 /* Include the target description when possible. Some architectures
2125 allow for per-thread gdbarch so we should really be emitting a tdesc
2126 per-thread, however, we don't currently support reading in a
2127 per-thread tdesc, so just emit the tdesc for the signalled thread. */
2128 gdbarch = target_thread_architecture (signalled_thr->ptid);
2129 gcore_elf_make_tdesc_note (gdbarch, obfd, &note_data, note_size);
2131 return note_data;
2134 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
2135 gdbarch.h. This function is not static because it is exported to
2136 other -tdep files. */
2138 enum gdb_signal
2139 linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
2141 switch (signal)
2143 case 0:
2144 return GDB_SIGNAL_0;
2146 case LINUX_SIGHUP:
2147 return GDB_SIGNAL_HUP;
2149 case LINUX_SIGINT:
2150 return GDB_SIGNAL_INT;
2152 case LINUX_SIGQUIT:
2153 return GDB_SIGNAL_QUIT;
2155 case LINUX_SIGILL:
2156 return GDB_SIGNAL_ILL;
2158 case LINUX_SIGTRAP:
2159 return GDB_SIGNAL_TRAP;
2161 case LINUX_SIGABRT:
2162 return GDB_SIGNAL_ABRT;
2164 case LINUX_SIGBUS:
2165 return GDB_SIGNAL_BUS;
2167 case LINUX_SIGFPE:
2168 return GDB_SIGNAL_FPE;
2170 case LINUX_SIGKILL:
2171 return GDB_SIGNAL_KILL;
2173 case LINUX_SIGUSR1:
2174 return GDB_SIGNAL_USR1;
2176 case LINUX_SIGSEGV:
2177 return GDB_SIGNAL_SEGV;
2179 case LINUX_SIGUSR2:
2180 return GDB_SIGNAL_USR2;
2182 case LINUX_SIGPIPE:
2183 return GDB_SIGNAL_PIPE;
2185 case LINUX_SIGALRM:
2186 return GDB_SIGNAL_ALRM;
2188 case LINUX_SIGTERM:
2189 return GDB_SIGNAL_TERM;
2191 case LINUX_SIGCHLD:
2192 return GDB_SIGNAL_CHLD;
2194 case LINUX_SIGCONT:
2195 return GDB_SIGNAL_CONT;
2197 case LINUX_SIGSTOP:
2198 return GDB_SIGNAL_STOP;
2200 case LINUX_SIGTSTP:
2201 return GDB_SIGNAL_TSTP;
2203 case LINUX_SIGTTIN:
2204 return GDB_SIGNAL_TTIN;
2206 case LINUX_SIGTTOU:
2207 return GDB_SIGNAL_TTOU;
2209 case LINUX_SIGURG:
2210 return GDB_SIGNAL_URG;
2212 case LINUX_SIGXCPU:
2213 return GDB_SIGNAL_XCPU;
2215 case LINUX_SIGXFSZ:
2216 return GDB_SIGNAL_XFSZ;
2218 case LINUX_SIGVTALRM:
2219 return GDB_SIGNAL_VTALRM;
2221 case LINUX_SIGPROF:
2222 return GDB_SIGNAL_PROF;
2224 case LINUX_SIGWINCH:
2225 return GDB_SIGNAL_WINCH;
2227 /* No way to differentiate between SIGIO and SIGPOLL.
2228 Therefore, we just handle the first one. */
2229 case LINUX_SIGIO:
2230 return GDB_SIGNAL_IO;
2232 case LINUX_SIGPWR:
2233 return GDB_SIGNAL_PWR;
2235 case LINUX_SIGSYS:
2236 return GDB_SIGNAL_SYS;
2238 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
2239 therefore we have to handle them here. */
2240 case LINUX_SIGRTMIN:
2241 return GDB_SIGNAL_REALTIME_32;
2243 case LINUX_SIGRTMAX:
2244 return GDB_SIGNAL_REALTIME_64;
2247 if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
2249 int offset = signal - LINUX_SIGRTMIN + 1;
2251 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
2254 return GDB_SIGNAL_UNKNOWN;
2257 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
2258 gdbarch.h. This function is not static because it is exported to
2259 other -tdep files. */
2262 linux_gdb_signal_to_target (struct gdbarch *gdbarch,
2263 enum gdb_signal signal)
2265 switch (signal)
2267 case GDB_SIGNAL_0:
2268 return 0;
2270 case GDB_SIGNAL_HUP:
2271 return LINUX_SIGHUP;
2273 case GDB_SIGNAL_INT:
2274 return LINUX_SIGINT;
2276 case GDB_SIGNAL_QUIT:
2277 return LINUX_SIGQUIT;
2279 case GDB_SIGNAL_ILL:
2280 return LINUX_SIGILL;
2282 case GDB_SIGNAL_TRAP:
2283 return LINUX_SIGTRAP;
2285 case GDB_SIGNAL_ABRT:
2286 return LINUX_SIGABRT;
2288 case GDB_SIGNAL_FPE:
2289 return LINUX_SIGFPE;
2291 case GDB_SIGNAL_KILL:
2292 return LINUX_SIGKILL;
2294 case GDB_SIGNAL_BUS:
2295 return LINUX_SIGBUS;
2297 case GDB_SIGNAL_SEGV:
2298 return LINUX_SIGSEGV;
2300 case GDB_SIGNAL_SYS:
2301 return LINUX_SIGSYS;
2303 case GDB_SIGNAL_PIPE:
2304 return LINUX_SIGPIPE;
2306 case GDB_SIGNAL_ALRM:
2307 return LINUX_SIGALRM;
2309 case GDB_SIGNAL_TERM:
2310 return LINUX_SIGTERM;
2312 case GDB_SIGNAL_URG:
2313 return LINUX_SIGURG;
2315 case GDB_SIGNAL_STOP:
2316 return LINUX_SIGSTOP;
2318 case GDB_SIGNAL_TSTP:
2319 return LINUX_SIGTSTP;
2321 case GDB_SIGNAL_CONT:
2322 return LINUX_SIGCONT;
2324 case GDB_SIGNAL_CHLD:
2325 return LINUX_SIGCHLD;
2327 case GDB_SIGNAL_TTIN:
2328 return LINUX_SIGTTIN;
2330 case GDB_SIGNAL_TTOU:
2331 return LINUX_SIGTTOU;
2333 case GDB_SIGNAL_IO:
2334 return LINUX_SIGIO;
2336 case GDB_SIGNAL_XCPU:
2337 return LINUX_SIGXCPU;
2339 case GDB_SIGNAL_XFSZ:
2340 return LINUX_SIGXFSZ;
2342 case GDB_SIGNAL_VTALRM:
2343 return LINUX_SIGVTALRM;
2345 case GDB_SIGNAL_PROF:
2346 return LINUX_SIGPROF;
2348 case GDB_SIGNAL_WINCH:
2349 return LINUX_SIGWINCH;
2351 case GDB_SIGNAL_USR1:
2352 return LINUX_SIGUSR1;
2354 case GDB_SIGNAL_USR2:
2355 return LINUX_SIGUSR2;
2357 case GDB_SIGNAL_PWR:
2358 return LINUX_SIGPWR;
2360 case GDB_SIGNAL_POLL:
2361 return LINUX_SIGPOLL;
2363 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
2364 therefore we have to handle it here. */
2365 case GDB_SIGNAL_REALTIME_32:
2366 return LINUX_SIGRTMIN;
2368 /* Same comment applies to _64. */
2369 case GDB_SIGNAL_REALTIME_64:
2370 return LINUX_SIGRTMAX;
2373 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
2374 if (signal >= GDB_SIGNAL_REALTIME_33
2375 && signal <= GDB_SIGNAL_REALTIME_63)
2377 int offset = signal - GDB_SIGNAL_REALTIME_33;
2379 return LINUX_SIGRTMIN + 1 + offset;
2382 return -1;
2385 /* Helper for linux_vsyscall_range that does the real work of finding
2386 the vsyscall's address range. */
2388 static int
2389 linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
2391 char filename[100];
2392 long pid;
2394 if (target_auxv_search (AT_SYSINFO_EHDR, &range->start) <= 0)
2395 return 0;
2397 /* It doesn't make sense to access the host's /proc when debugging a
2398 core file. Instead, look for the PT_LOAD segment that matches
2399 the vDSO. */
2400 if (!target_has_execution ())
2402 long phdrs_size;
2403 int num_phdrs, i;
2405 phdrs_size
2406 = bfd_get_elf_phdr_upper_bound (current_program_space->core_bfd ());
2407 if (phdrs_size == -1)
2408 return 0;
2410 gdb::unique_xmalloc_ptr<Elf_Internal_Phdr>
2411 phdrs ((Elf_Internal_Phdr *) xmalloc (phdrs_size));
2412 num_phdrs = bfd_get_elf_phdrs (current_program_space->core_bfd (),
2413 phdrs.get ());
2414 if (num_phdrs == -1)
2415 return 0;
2417 for (i = 0; i < num_phdrs; i++)
2418 if (phdrs.get ()[i].p_type == PT_LOAD
2419 && phdrs.get ()[i].p_vaddr == range->start)
2421 range->length = phdrs.get ()[i].p_memsz;
2422 return 1;
2425 return 0;
2428 /* We need to know the real target PID to access /proc. */
2429 if (current_inferior ()->fake_pid_p)
2430 return 0;
2432 pid = current_inferior ()->pid;
2434 /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
2435 reading /proc/PID/maps (2). The later identifies thread stacks
2436 in the output, which requires scanning every thread in the thread
2437 group to check whether a VMA is actually a thread's stack. With
2438 Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
2439 a few thousand threads, (1) takes a few miliseconds, while (2)
2440 takes several seconds. Also note that "smaps", what we read for
2441 determining core dump mappings, is even slower than "maps". */
2442 xsnprintf (filename, sizeof filename, "/proc/%ld/task/%ld/maps", pid, pid);
2443 gdb::unique_xmalloc_ptr<char> data
2444 = target_fileio_read_stralloc (NULL, filename);
2445 if (data != NULL)
2447 char *line;
2448 char *saveptr = NULL;
2450 for (line = strtok_r (data.get (), "\n", &saveptr);
2451 line != NULL;
2452 line = strtok_r (NULL, "\n", &saveptr))
2454 ULONGEST addr, endaddr;
2455 const char *p = line;
2457 addr = strtoulst (p, &p, 16);
2458 if (addr == range->start)
2460 if (*p == '-')
2461 p++;
2462 endaddr = strtoulst (p, &p, 16);
2463 range->length = endaddr - addr;
2464 return 1;
2468 else
2469 warning (_("unable to open /proc file '%s'"), filename);
2471 return 0;
2474 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2475 caching, and defers the real work to linux_vsyscall_range_raw. */
2477 static int
2478 linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
2480 struct linux_info *info = get_linux_inferior_data (current_inferior ());
2482 if (info->vsyscall_range_p == 0)
2484 if (linux_vsyscall_range_raw (gdbarch, &info->vsyscall_range))
2485 info->vsyscall_range_p = 1;
2486 else
2487 info->vsyscall_range_p = -1;
2490 if (info->vsyscall_range_p < 0)
2491 return 0;
2493 *range = info->vsyscall_range;
2494 return 1;
2497 /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
2498 definitions would be dependent on compilation host. */
2499 #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
2500 #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
2502 /* See gdbarch.sh 'infcall_mmap'. */
2504 static CORE_ADDR
2505 linux_infcall_mmap (CORE_ADDR size, unsigned prot)
2507 struct objfile *objf;
2508 /* Do there still exist any Linux systems without "mmap64"?
2509 "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
2510 struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
2511 struct value *addr_val;
2512 struct gdbarch *gdbarch = objf->arch ();
2513 CORE_ADDR retval;
2514 enum
2516 ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_LAST
2518 struct value *arg[ARG_LAST];
2520 arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
2522 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2523 arg[ARG_LENGTH] = value_from_ulongest
2524 (builtin_type (gdbarch)->builtin_unsigned_long, size);
2525 gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
2526 | GDB_MMAP_PROT_EXEC))
2527 == 0);
2528 arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int, prot);
2529 arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
2530 GDB_MMAP_MAP_PRIVATE
2531 | GDB_MMAP_MAP_ANONYMOUS);
2532 arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1);
2533 arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64,
2535 addr_val = call_function_by_hand (mmap_val, NULL, arg);
2536 retval = value_as_address (addr_val);
2537 if (retval == (CORE_ADDR) -1)
2538 error (_("Failed inferior mmap call for %s bytes, errno is changed."),
2539 pulongest (size));
2540 return retval;
2543 /* See gdbarch.sh 'infcall_munmap'. */
2545 static void
2546 linux_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
2548 struct objfile *objf;
2549 struct value *munmap_val = find_function_in_inferior ("munmap", &objf);
2550 struct value *retval_val;
2551 struct gdbarch *gdbarch = objf->arch ();
2552 LONGEST retval;
2553 enum
2555 ARG_ADDR, ARG_LENGTH, ARG_LAST
2557 struct value *arg[ARG_LAST];
2559 arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
2560 addr);
2561 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2562 arg[ARG_LENGTH] = value_from_ulongest
2563 (builtin_type (gdbarch)->builtin_unsigned_long, size);
2564 retval_val = call_function_by_hand (munmap_val, NULL, arg);
2565 retval = value_as_long (retval_val);
2566 if (retval != 0)
2567 warning (_("Failed inferior munmap call at %s for %s bytes, "
2568 "errno is changed."),
2569 hex_string (addr), pulongest (size));
2572 /* See linux-tdep.h. */
2574 CORE_ADDR
2575 linux_displaced_step_location (struct gdbarch *gdbarch)
2577 CORE_ADDR addr;
2578 int bp_len;
2580 /* Determine entry point from target auxiliary vector. This avoids
2581 the need for symbols. Also, when debugging a stand-alone SPU
2582 executable, entry_point_address () will point to an SPU
2583 local-store address and is thus not usable as displaced stepping
2584 location. The auxiliary vector gets us the PowerPC-side entry
2585 point address instead. */
2586 if (target_auxv_search (AT_ENTRY, &addr) <= 0)
2587 throw_error (NOT_SUPPORTED_ERROR,
2588 _("Cannot find AT_ENTRY auxiliary vector entry."));
2590 /* Make certain that the address points at real code, and not a
2591 function descriptor. */
2592 addr = gdbarch_convert_from_func_ptr_addr
2593 (gdbarch, addr, current_inferior ()->top_target ());
2595 /* Inferior calls also use the entry point as a breakpoint location.
2596 We don't want displaced stepping to interfere with those
2597 breakpoints, so leave space. */
2598 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
2599 addr += bp_len * 2;
2601 return addr;
2604 /* See linux-tdep.h. */
2606 displaced_step_prepare_status
2607 linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
2608 CORE_ADDR &displaced_pc)
2610 linux_info *per_inferior = get_linux_inferior_data (thread->inf);
2612 if (!per_inferior->disp_step_bufs.has_value ())
2614 /* Figure out the location of the buffers. They are contiguous, starting
2615 at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
2616 CORE_ADDR disp_step_buf_addr
2617 = linux_displaced_step_location (thread->inf->arch ());
2618 int buf_len = gdbarch_displaced_step_buffer_length (arch);
2620 linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (arch);
2621 gdb_assert (gdbarch_data->num_disp_step_buffers > 0);
2623 std::vector<CORE_ADDR> buffers;
2624 for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
2625 buffers.push_back (disp_step_buf_addr + i * buf_len);
2627 per_inferior->disp_step_bufs.emplace (buffers);
2630 return per_inferior->disp_step_bufs->prepare (thread, displaced_pc);
2633 /* See linux-tdep.h. */
2635 displaced_step_finish_status
2636 linux_displaced_step_finish (gdbarch *arch, thread_info *thread,
2637 const target_waitstatus &status)
2639 linux_info *per_inferior = get_linux_inferior_data (thread->inf);
2641 gdb_assert (per_inferior->disp_step_bufs.has_value ());
2643 return per_inferior->disp_step_bufs->finish (arch, thread, status);
2646 /* See linux-tdep.h. */
2648 const displaced_step_copy_insn_closure *
2649 linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
2651 linux_info *per_inferior = linux_inferior_data.get (inf);
2653 if (per_inferior == nullptr
2654 || !per_inferior->disp_step_bufs.has_value ())
2655 return nullptr;
2657 return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
2660 /* See linux-tdep.h. */
2662 void
2663 linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
2665 linux_info *per_inferior = linux_inferior_data.get (parent_inf);
2667 if (per_inferior == nullptr
2668 || !per_inferior->disp_step_bufs.has_value ())
2669 return;
2671 per_inferior->disp_step_bufs->restore_in_ptid (ptid);
2674 /* Helper for linux_get_hwcap and linux_get_hwcap2. */
2676 static CORE_ADDR
2677 linux_get_hwcap_helper (const std::optional<gdb::byte_vector> &auxv,
2678 target_ops *target, gdbarch *gdbarch, CORE_ADDR match)
2680 CORE_ADDR field;
2681 if (!auxv.has_value ()
2682 || target_auxv_search (*auxv, target, gdbarch, match, &field) != 1)
2683 return 0;
2684 return field;
2687 /* See linux-tdep.h. */
2689 CORE_ADDR
2690 linux_get_hwcap (const std::optional<gdb::byte_vector> &auxv,
2691 target_ops *target, gdbarch *gdbarch)
2693 return linux_get_hwcap_helper (auxv, target, gdbarch, AT_HWCAP);
2696 /* See linux-tdep.h. */
2698 CORE_ADDR
2699 linux_get_hwcap ()
2701 return linux_get_hwcap (target_read_auxv (),
2702 current_inferior ()->top_target (),
2703 current_inferior ()->arch ());
2706 /* See linux-tdep.h. */
2708 CORE_ADDR
2709 linux_get_hwcap2 (const std::optional<gdb::byte_vector> &auxv,
2710 target_ops *target, gdbarch *gdbarch)
2712 return linux_get_hwcap_helper (auxv, target, gdbarch, AT_HWCAP2);
2715 /* See linux-tdep.h. */
2717 CORE_ADDR
2718 linux_get_hwcap2 ()
2720 return linux_get_hwcap2 (target_read_auxv (),
2721 current_inferior ()->top_target (),
2722 current_inferior ()->arch ());
2725 /* Display whether the gcore command is using the
2726 /proc/PID/coredump_filter file. */
2728 static void
2729 show_use_coredump_filter (struct ui_file *file, int from_tty,
2730 struct cmd_list_element *c, const char *value)
2732 gdb_printf (file, _("Use of /proc/PID/coredump_filter file to generate"
2733 " corefiles is %s.\n"), value);
2736 /* Display whether the gcore command is dumping mappings marked with
2737 the VM_DONTDUMP flag. */
2739 static void
2740 show_dump_excluded_mappings (struct ui_file *file, int from_tty,
2741 struct cmd_list_element *c, const char *value)
2743 gdb_printf (file, _("Dumping of mappings marked with the VM_DONTDUMP"
2744 " flag is %s.\n"), value);
2747 /* To be called from the various GDB_OSABI_LINUX handlers for the
2748 various GNU/Linux architectures and machine types.
2750 NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
2751 displaced stepping is not supported. */
2753 void
2754 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
2755 int num_disp_step_buffers)
2757 if (num_disp_step_buffers > 0)
2759 linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (gdbarch);
2760 gdbarch_data->num_disp_step_buffers = num_disp_step_buffers;
2762 set_gdbarch_displaced_step_prepare (gdbarch,
2763 linux_displaced_step_prepare);
2764 set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
2765 set_gdbarch_displaced_step_copy_insn_closure_by_addr
2766 (gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
2767 set_gdbarch_displaced_step_restore_all_in_ptid
2768 (gdbarch, linux_displaced_step_restore_all_in_ptid);
2771 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
2772 set_gdbarch_info_proc (gdbarch, linux_info_proc);
2773 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
2774 set_gdbarch_core_xfer_siginfo (gdbarch, linux_core_xfer_siginfo);
2775 set_gdbarch_read_core_file_mappings (gdbarch, linux_read_core_file_mappings);
2776 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
2777 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes);
2778 set_gdbarch_has_shared_address_space (gdbarch,
2779 linux_has_shared_address_space);
2780 set_gdbarch_gdb_signal_from_target (gdbarch,
2781 linux_gdb_signal_from_target);
2782 set_gdbarch_gdb_signal_to_target (gdbarch,
2783 linux_gdb_signal_to_target);
2784 set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range);
2785 set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap);
2786 set_gdbarch_infcall_munmap (gdbarch, linux_infcall_munmap);
2787 set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
2790 void _initialize_linux_tdep ();
2791 void
2792 _initialize_linux_tdep ()
2794 /* Observers used to invalidate the cache when needed. */
2795 gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf,
2796 "linux-tdep");
2797 gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf,
2798 "linux-tdep");
2799 gdb::observers::inferior_execd.attach (linux_inferior_execd,
2800 "linux-tdep");
2802 add_setshow_boolean_cmd ("use-coredump-filter", class_files,
2803 &use_coredump_filter, _("\
2804 Set whether gcore should consider /proc/PID/coredump_filter."),
2805 _("\
2806 Show whether gcore should consider /proc/PID/coredump_filter."),
2807 _("\
2808 Use this command to set whether gcore should consider the contents\n\
2809 of /proc/PID/coredump_filter when generating the corefile. For more information\n\
2810 about this file, refer to the manpage of core(5)."),
2811 NULL, show_use_coredump_filter,
2812 &setlist, &showlist);
2814 add_setshow_boolean_cmd ("dump-excluded-mappings", class_files,
2815 &dump_excluded_mappings, _("\
2816 Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2817 _("\
2818 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2819 _("\
2820 Use this command to set whether gcore should dump mappings marked with the\n\
2821 VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
2822 more information about this file, refer to the manpage of proc(5) and core(5)."),
2823 NULL, show_dump_excluded_mappings,
2824 &setlist, &showlist);
2827 /* Fetch (and possibly build) an appropriate `link_map_offsets' for
2828 ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
2830 link_map_offsets *
2831 linux_ilp32_fetch_link_map_offsets ()
2833 static link_map_offsets lmo;
2834 static link_map_offsets *lmp = nullptr;
2836 if (lmp == nullptr)
2838 lmp = &lmo;
2840 lmo.r_version_offset = 0;
2841 lmo.r_version_size = 4;
2842 lmo.r_map_offset = 4;
2843 lmo.r_brk_offset = 8;
2844 lmo.r_ldsomap_offset = -1;
2845 lmo.r_next_offset = 20;
2847 /* Everything we need is in the first 20 bytes. */
2848 lmo.link_map_size = 20;
2849 lmo.l_addr_offset = 0;
2850 lmo.l_name_offset = 4;
2851 lmo.l_ld_offset = 8;
2852 lmo.l_next_offset = 12;
2853 lmo.l_prev_offset = 16;
2856 return lmp;
2859 link_map_offsets *
2860 linux_lp64_fetch_link_map_offsets ()
2862 static link_map_offsets lmo;
2863 static link_map_offsets *lmp = nullptr;
2865 if (lmp == nullptr)
2867 lmp = &lmo;
2869 lmo.r_version_offset = 0;
2870 lmo.r_version_size = 4;
2871 lmo.r_map_offset = 8;
2872 lmo.r_brk_offset = 16;
2873 lmo.r_ldsomap_offset = -1;
2874 lmo.r_next_offset = 40;
2876 /* Everything we need is in the first 40 bytes. */
2877 lmo.link_map_size = 40;
2878 lmo.l_addr_offset = 0;
2879 lmo.l_name_offset = 8;
2880 lmo.l_ld_offset = 16;
2881 lmo.l_next_offset = 24;
2882 lmo.l_prev_offset = 32;
2885 return lmp;