Automatic date update in version.in
[binutils-gdb.git] / gdb / linux-tdep.c
blob5fe6be16c3116203278ae72891c663557a066c41
1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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" /* for elfcore_write_* */
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 "gdbsupport/gdb_optional.h"
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 gdb::optional<displaced_step_buffers> disp_step_bufs;
235 /* Per-inferior data key. */
236 static const registry<inferior>::key<linux_info> linux_inferior_data;
238 /* Frees whatever allocated space there is to be freed and sets INF's
239 linux cache data pointer to NULL. */
241 static void
242 invalidate_linux_cache_inf (struct inferior *inf)
244 linux_inferior_data.clear (inf);
247 /* Fetch the linux cache info for INF. This function always returns a
248 valid INFO pointer. */
250 static struct linux_info *
251 get_linux_inferior_data (inferior *inf)
253 linux_info *info = linux_inferior_data.get (inf);
255 if (info == nullptr)
256 info = linux_inferior_data.emplace (inf);
258 return info;
261 /* See linux-tdep.h. */
263 struct type *
264 linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
265 linux_siginfo_extra_fields extra_fields)
267 struct linux_gdbarch_data *linux_gdbarch_data;
268 struct type *int_type, *uint_type, *long_type, *void_ptr_type, *short_type;
269 struct type *uid_type, *pid_type;
270 struct type *sigval_type, *clock_type;
271 struct type *siginfo_type, *sifields_type;
272 struct type *type;
274 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
275 if (linux_gdbarch_data->siginfo_type != NULL)
276 return linux_gdbarch_data->siginfo_type;
278 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
279 0, "int");
280 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
281 1, "unsigned int");
282 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
283 0, "long");
284 short_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
285 0, "short");
286 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
288 /* sival_t */
289 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
290 sigval_type->set_name (xstrdup ("sigval_t"));
291 append_composite_type_field (sigval_type, "sival_int", int_type);
292 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
294 /* __pid_t */
295 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
296 TYPE_LENGTH (int_type) * TARGET_CHAR_BIT, "__pid_t");
297 TYPE_TARGET_TYPE (pid_type) = int_type;
298 pid_type->set_target_is_stub (true);
300 /* __uid_t */
301 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
302 TYPE_LENGTH (uint_type) * TARGET_CHAR_BIT, "__uid_t");
303 TYPE_TARGET_TYPE (uid_type) = uint_type;
304 uid_type->set_target_is_stub (true);
306 /* __clock_t */
307 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
308 TYPE_LENGTH (long_type) * TARGET_CHAR_BIT,
309 "__clock_t");
310 TYPE_TARGET_TYPE (clock_type) = long_type;
311 clock_type->set_target_is_stub (true);
313 /* _sifields */
314 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
317 const int si_max_size = 128;
318 int si_pad_size;
319 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
321 /* _pad */
322 if (gdbarch_ptr_bit (gdbarch) == 64)
323 si_pad_size = (si_max_size / size_of_int) - 4;
324 else
325 si_pad_size = (si_max_size / size_of_int) - 3;
326 append_composite_type_field (sifields_type, "_pad",
327 init_vector_type (int_type, si_pad_size));
330 /* _kill */
331 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
332 append_composite_type_field (type, "si_pid", pid_type);
333 append_composite_type_field (type, "si_uid", uid_type);
334 append_composite_type_field (sifields_type, "_kill", type);
336 /* _timer */
337 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
338 append_composite_type_field (type, "si_tid", int_type);
339 append_composite_type_field (type, "si_overrun", int_type);
340 append_composite_type_field (type, "si_sigval", sigval_type);
341 append_composite_type_field (sifields_type, "_timer", type);
343 /* _rt */
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 (type, "si_sigval", sigval_type);
348 append_composite_type_field (sifields_type, "_rt", type);
350 /* _sigchld */
351 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
352 append_composite_type_field (type, "si_pid", pid_type);
353 append_composite_type_field (type, "si_uid", uid_type);
354 append_composite_type_field (type, "si_status", int_type);
355 append_composite_type_field (type, "si_utime", clock_type);
356 append_composite_type_field (type, "si_stime", clock_type);
357 append_composite_type_field (sifields_type, "_sigchld", type);
359 /* _sigfault */
360 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
361 append_composite_type_field (type, "si_addr", void_ptr_type);
363 /* Additional bound fields for _sigfault in case they were requested. */
364 if ((extra_fields & LINUX_SIGINFO_FIELD_ADDR_BND) != 0)
366 struct type *sigfault_bnd_fields;
368 append_composite_type_field (type, "_addr_lsb", short_type);
369 sigfault_bnd_fields = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
370 append_composite_type_field (sigfault_bnd_fields, "_lower", void_ptr_type);
371 append_composite_type_field (sigfault_bnd_fields, "_upper", void_ptr_type);
372 append_composite_type_field (type, "_addr_bnd", sigfault_bnd_fields);
374 append_composite_type_field (sifields_type, "_sigfault", type);
376 /* _sigpoll */
377 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
378 append_composite_type_field (type, "si_band", long_type);
379 append_composite_type_field (type, "si_fd", int_type);
380 append_composite_type_field (sifields_type, "_sigpoll", type);
382 /* _sigsys */
383 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
384 append_composite_type_field (type, "_call_addr", void_ptr_type);
385 append_composite_type_field (type, "_syscall", int_type);
386 append_composite_type_field (type, "_arch", uint_type);
387 append_composite_type_field (sifields_type, "_sigsys", type);
389 /* struct siginfo */
390 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
391 siginfo_type->set_name (xstrdup ("siginfo"));
392 append_composite_type_field (siginfo_type, "si_signo", int_type);
393 append_composite_type_field (siginfo_type, "si_errno", int_type);
394 append_composite_type_field (siginfo_type, "si_code", int_type);
395 append_composite_type_field_aligned (siginfo_type,
396 "_sifields", sifields_type,
397 TYPE_LENGTH (long_type));
399 linux_gdbarch_data->siginfo_type = siginfo_type;
401 return siginfo_type;
404 /* This function is suitable for architectures that don't
405 extend/override the standard siginfo structure. */
407 static struct type *
408 linux_get_siginfo_type (struct gdbarch *gdbarch)
410 return linux_get_siginfo_type_with_fields (gdbarch, 0);
413 /* Return true if the target is running on uClinux instead of normal
414 Linux kernel. */
417 linux_is_uclinux (void)
419 CORE_ADDR dummy;
420 target_ops *target = current_inferior ()->top_target ();
422 return (target_auxv_search (target, AT_NULL, &dummy) > 0
423 && target_auxv_search (target, AT_PAGESZ, &dummy) == 0);
426 static int
427 linux_has_shared_address_space (struct gdbarch *gdbarch)
429 return linux_is_uclinux ();
432 /* This is how we want PTIDs from core files to be printed. */
434 static std::string
435 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
437 if (ptid.lwp () != 0)
438 return string_printf ("LWP %ld", ptid.lwp ());
440 return normal_pid_to_str (ptid);
443 /* Data from one mapping from /proc/PID/maps. */
445 struct mapping
447 ULONGEST addr;
448 ULONGEST endaddr;
449 gdb::string_view permissions;
450 ULONGEST offset;
451 gdb::string_view device;
452 ULONGEST inode;
454 /* This field is guaranteed to be NULL-terminated, hence it is not a
455 gdb::string_view. */
456 const char *filename;
459 /* Service function for corefiles and info proc. */
461 static mapping
462 read_mapping (const char *line)
464 struct mapping mapping;
465 const char *p = line;
467 mapping.addr = strtoulst (p, &p, 16);
468 if (*p == '-')
469 p++;
470 mapping.endaddr = strtoulst (p, &p, 16);
472 p = skip_spaces (p);
473 const char *permissions_start = p;
474 while (*p && !isspace (*p))
475 p++;
476 mapping.permissions = {permissions_start, (size_t) (p - permissions_start)};
478 mapping.offset = strtoulst (p, &p, 16);
480 p = skip_spaces (p);
481 const char *device_start = p;
482 while (*p && !isspace (*p))
483 p++;
484 mapping.device = {device_start, (size_t) (p - device_start)};
486 mapping.inode = strtoulst (p, &p, 10);
488 p = skip_spaces (p);
489 mapping.filename = p;
491 return mapping;
494 /* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
496 This function was based on the documentation found on
497 <Documentation/filesystems/proc.txt>, on the Linux kernel.
499 Linux kernels before commit
500 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
501 field on smaps. */
503 static void
504 decode_vmflags (char *p, struct smaps_vmflags *v)
506 char *saveptr = NULL;
507 const char *s;
509 v->initialized_p = 1;
510 p = skip_to_space (p);
511 p = skip_spaces (p);
513 for (s = strtok_r (p, " ", &saveptr);
514 s != NULL;
515 s = strtok_r (NULL, " ", &saveptr))
517 if (strcmp (s, "io") == 0)
518 v->io_page = 1;
519 else if (strcmp (s, "ht") == 0)
520 v->uses_huge_tlb = 1;
521 else if (strcmp (s, "dd") == 0)
522 v->exclude_coredump = 1;
523 else if (strcmp (s, "sh") == 0)
524 v->shared_mapping = 1;
525 else if (strcmp (s, "mt") == 0)
526 v->memory_tagging = 1;
530 /* Regexes used by mapping_is_anonymous_p. Put in a structure because
531 they're initialized lazily. */
533 struct mapping_regexes
535 /* Matches "/dev/zero" filenames (with or without the "(deleted)"
536 string in the end). We know for sure, based on the Linux kernel
537 code, that memory mappings whose associated filename is
538 "/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
539 compiled_regex dev_zero
540 {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB,
541 _("Could not compile regex to match /dev/zero filename")};
543 /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
544 string in the end). These filenames refer to shared memory
545 (shmem), and memory mappings associated with them are
546 MAP_ANONYMOUS as well. */
547 compiled_regex shmem_file
548 {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB,
549 _("Could not compile regex to match shmem filenames")};
551 /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
552 0' code, which is responsible to decide if it is dealing with a
553 'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
554 FILE_DELETED matches, it does not necessarily mean that we are
555 dealing with an anonymous shared mapping. However, there is no
556 easy way to detect this currently, so this is the best
557 approximation we have.
559 As a result, GDB will dump readonly pages of deleted executables
560 when using the default value of coredump_filter (0x33), while the
561 Linux kernel will not dump those pages. But we can live with
562 that. */
563 compiled_regex file_deleted
564 {" (deleted)$", REG_NOSUB,
565 _("Could not compile regex to match '<file> (deleted)'")};
568 /* Return 1 if the memory mapping is anonymous, 0 otherwise.
570 FILENAME is the name of the file present in the first line of the
571 memory mapping, in the "/proc/PID/smaps" output. For example, if
572 the first line is:
574 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
576 Then FILENAME will be "/path/to/file". */
578 static int
579 mapping_is_anonymous_p (const char *filename)
581 static gdb::optional<mapping_regexes> regexes;
582 static int init_regex_p = 0;
584 if (!init_regex_p)
586 /* Let's be pessimistic and assume there will be an error while
587 compiling the regex'es. */
588 init_regex_p = -1;
590 regexes.emplace ();
592 /* If we reached this point, then everything succeeded. */
593 init_regex_p = 1;
596 if (init_regex_p == -1)
598 const char deleted[] = " (deleted)";
599 size_t del_len = sizeof (deleted) - 1;
600 size_t filename_len = strlen (filename);
602 /* There was an error while compiling the regex'es above. In
603 order to try to give some reliable information to the caller,
604 we just try to find the string " (deleted)" in the filename.
605 If we managed to find it, then we assume the mapping is
606 anonymous. */
607 return (filename_len >= del_len
608 && strcmp (filename + filename_len - del_len, deleted) == 0);
611 if (*filename == '\0'
612 || regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
613 || regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
614 || regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
615 return 1;
617 return 0;
620 /* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
621 MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
622 be dumped, or greater than 0 if it should.
624 In a nutshell, this is the logic that we follow in order to decide
625 if a mapping should be dumped or not.
627 - If the mapping is associated to a file whose name ends with
628 " (deleted)", or if the file is "/dev/zero", or if it is
629 "/SYSV%08x" (shared memory), or if there is no file associated
630 with it, or if the AnonHugePages: or the Anonymous: fields in the
631 /proc/PID/smaps have contents, then GDB considers this mapping to
632 be anonymous. Otherwise, GDB considers this mapping to be a
633 file-backed mapping (because there will be a file associated with
634 it).
636 It is worth mentioning that, from all those checks described
637 above, the most fragile is the one to see if the file name ends
638 with " (deleted)". This does not necessarily mean that the
639 mapping is anonymous, because the deleted file associated with
640 the mapping may have been a hard link to another file, for
641 example. The Linux kernel checks to see if "i_nlink == 0", but
642 GDB cannot easily (and normally) do this check (iff running as
643 root, it could find the mapping in /proc/PID/map_files/ and
644 determine whether there still are other hard links to the
645 inode/file). Therefore, we made a compromise here, and we assume
646 that if the file name ends with " (deleted)", then the mapping is
647 indeed anonymous. FWIW, this is something the Linux kernel could
648 do better: expose this information in a more direct way.
650 - If we see the flag "sh" in the "VmFlags:" field (in
651 /proc/PID/smaps), then certainly the memory mapping is shared
652 (VM_SHARED). If we have access to the VmFlags, and we don't see
653 the "sh" there, then certainly the mapping is private. However,
654 Linux kernels before commit
655 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
656 "VmFlags:" field; in that case, we use another heuristic: if we
657 see 'p' in the permission flags, then we assume that the mapping
658 is private, even though the presence of the 's' flag there would
659 mean VM_MAYSHARE, which means the mapping could still be private.
660 This should work OK enough, however.
662 - Even if, at the end, we decided that we should not dump the
663 mapping, we still have to check if it is something like an ELF
664 header (of a DSO or an executable, for example). If it is, and
665 if the user is interested in dump it, then we should dump it. */
667 static int
668 dump_mapping_p (filter_flags filterflags, const struct smaps_vmflags *v,
669 int maybe_private_p, int mapping_anon_p, int mapping_file_p,
670 const char *filename, ULONGEST addr, ULONGEST offset)
672 /* Initially, we trust in what we received from our caller. This
673 value may not be very precise (i.e., it was probably gathered
674 from the permission line in the /proc/PID/smaps list, which
675 actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
676 what we have until we take a look at the "VmFlags:" field
677 (assuming that the version of the Linux kernel being used
678 supports it, of course). */
679 int private_p = maybe_private_p;
680 int dump_p;
682 /* We always dump vDSO and vsyscall mappings, because it's likely that
683 there'll be no file to read the contents from at core load time.
684 The kernel does the same. */
685 if (strcmp ("[vdso]", filename) == 0
686 || strcmp ("[vsyscall]", filename) == 0)
687 return 1;
689 if (v->initialized_p)
691 /* We never dump I/O mappings. */
692 if (v->io_page)
693 return 0;
695 /* Check if we should exclude this mapping. */
696 if (!dump_excluded_mappings && v->exclude_coredump)
697 return 0;
699 /* Update our notion of whether this mapping is shared or
700 private based on a trustworthy value. */
701 private_p = !v->shared_mapping;
703 /* HugeTLB checking. */
704 if (v->uses_huge_tlb)
706 if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
707 || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
708 return 1;
710 return 0;
714 if (private_p)
716 if (mapping_anon_p && mapping_file_p)
718 /* This is a special situation. It can happen when we see a
719 mapping that is file-backed, but that contains anonymous
720 pages. */
721 dump_p = ((filterflags & COREFILTER_ANON_PRIVATE) != 0
722 || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
724 else if (mapping_anon_p)
725 dump_p = (filterflags & COREFILTER_ANON_PRIVATE) != 0;
726 else
727 dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
729 else
731 if (mapping_anon_p && mapping_file_p)
733 /* This is a special situation. It can happen when we see a
734 mapping that is file-backed, but that contains anonymous
735 pages. */
736 dump_p = ((filterflags & COREFILTER_ANON_SHARED) != 0
737 || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
739 else if (mapping_anon_p)
740 dump_p = (filterflags & COREFILTER_ANON_SHARED) != 0;
741 else
742 dump_p = (filterflags & COREFILTER_MAPPED_SHARED) != 0;
745 /* Even if we decided that we shouldn't dump this mapping, we still
746 have to check whether (a) the user wants us to dump mappings
747 containing an ELF header, and (b) the mapping in question
748 contains an ELF header. If (a) and (b) are true, then we should
749 dump this mapping.
751 A mapping contains an ELF header if it is a private mapping, its
752 offset is zero, and its first word is ELFMAG. */
753 if (!dump_p && private_p && offset == 0
754 && (filterflags & COREFILTER_ELF_HEADERS) != 0)
756 /* Useful define specifying the size of the ELF magical
757 header. */
758 #ifndef SELFMAG
759 #define SELFMAG 4
760 #endif
762 /* Let's check if we have an ELF header. */
763 gdb_byte h[SELFMAG];
764 if (target_read_memory (addr, h, SELFMAG) == 0)
766 /* The EI_MAG* and ELFMAG* constants come from
767 <elf/common.h>. */
768 if (h[EI_MAG0] == ELFMAG0 && h[EI_MAG1] == ELFMAG1
769 && h[EI_MAG2] == ELFMAG2 && h[EI_MAG3] == ELFMAG3)
771 /* This mapping contains an ELF header, so we
772 should dump it. */
773 dump_p = 1;
778 return dump_p;
781 /* As above, but return true only when we should dump the NT_FILE
782 entry. */
784 static int
785 dump_note_entry_p (filter_flags filterflags, const struct smaps_vmflags *v,
786 int maybe_private_p, int mapping_anon_p, int mapping_file_p,
787 const char *filename, ULONGEST addr, ULONGEST offset)
789 /* vDSO and vsyscall mappings will end up in the core file. Don't
790 put them in the NT_FILE note. */
791 if (strcmp ("[vdso]", filename) == 0
792 || strcmp ("[vsyscall]", filename) == 0)
793 return 0;
795 /* Otherwise, any other file-based mapping should be placed in the
796 note. */
797 return 1;
800 /* Implement the "info proc" command. */
802 static void
803 linux_info_proc (struct gdbarch *gdbarch, const char *args,
804 enum info_proc_what what)
806 /* A long is used for pid instead of an int to avoid a loss of precision
807 compiler warning from the output of strtoul. */
808 long pid;
809 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
810 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
811 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
812 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
813 int status_f = (what == IP_STATUS || what == IP_ALL);
814 int stat_f = (what == IP_STAT || what == IP_ALL);
815 char filename[100];
816 int target_errno;
818 if (args && isdigit (args[0]))
820 char *tem;
822 pid = strtoul (args, &tem, 10);
823 args = tem;
825 else
827 if (!target_has_execution ())
828 error (_("No current process: you must name one."));
829 if (current_inferior ()->fake_pid_p)
830 error (_("Can't determine the current process's PID: you must name one."));
832 pid = current_inferior ()->pid;
835 args = skip_spaces (args);
836 if (args && args[0])
837 error (_("Too many parameters: %s"), args);
839 gdb_printf (_("process %ld\n"), pid);
840 if (cmdline_f)
842 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
843 gdb_byte *buffer;
844 ssize_t len = target_fileio_read_alloc (NULL, filename, &buffer);
846 if (len > 0)
848 gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
849 ssize_t pos;
851 for (pos = 0; pos < len - 1; pos++)
853 if (buffer[pos] == '\0')
854 buffer[pos] = ' ';
856 buffer[len - 1] = '\0';
857 gdb_printf ("cmdline = '%s'\n", buffer);
859 else
860 warning (_("unable to open /proc file '%s'"), filename);
862 if (cwd_f)
864 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
865 gdb::optional<std::string> contents
866 = target_fileio_readlink (NULL, filename, &target_errno);
867 if (contents.has_value ())
868 gdb_printf ("cwd = '%s'\n", contents->c_str ());
869 else
870 warning (_("unable to read link '%s'"), filename);
872 if (exe_f)
874 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
875 gdb::optional<std::string> contents
876 = target_fileio_readlink (NULL, filename, &target_errno);
877 if (contents.has_value ())
878 gdb_printf ("exe = '%s'\n", contents->c_str ());
879 else
880 warning (_("unable to read link '%s'"), filename);
882 if (mappings_f)
884 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
885 gdb::unique_xmalloc_ptr<char> map
886 = target_fileio_read_stralloc (NULL, filename);
887 if (map != NULL)
889 char *line;
891 gdb_printf (_("Mapped address spaces:\n\n"));
892 if (gdbarch_addr_bit (gdbarch) == 32)
894 gdb_printf ("\t%10s %10s %10s %10s %s %s\n",
895 "Start Addr", " End Addr", " Size",
896 " Offset", "Perms ", "objfile");
898 else
900 gdb_printf (" %18s %18s %10s %10s %s %s\n",
901 "Start Addr", " End Addr", " Size",
902 " Offset", "Perms ", "objfile");
905 char *saveptr;
906 for (line = strtok_r (map.get (), "\n", &saveptr);
907 line;
908 line = strtok_r (NULL, "\n", &saveptr))
910 struct mapping m = read_mapping (line);
912 if (gdbarch_addr_bit (gdbarch) == 32)
914 gdb_printf ("\t%10s %10s %10s %10s %-5.*s %s\n",
915 paddress (gdbarch, m.addr),
916 paddress (gdbarch, m.endaddr),
917 hex_string (m.endaddr - m.addr),
918 hex_string (m.offset),
919 (int) m.permissions.size (),
920 m.permissions.data (),
921 m.filename);
923 else
925 gdb_printf (" %18s %18s %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);
936 else
937 warning (_("unable to open /proc file '%s'"), filename);
939 if (status_f)
941 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
942 gdb::unique_xmalloc_ptr<char> status
943 = target_fileio_read_stralloc (NULL, filename);
944 if (status)
945 gdb_puts (status.get ());
946 else
947 warning (_("unable to open /proc file '%s'"), filename);
949 if (stat_f)
951 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
952 gdb::unique_xmalloc_ptr<char> statstr
953 = target_fileio_read_stralloc (NULL, filename);
954 if (statstr)
956 const char *p = statstr.get ();
958 gdb_printf (_("Process: %s\n"),
959 pulongest (strtoulst (p, &p, 10)));
961 p = skip_spaces (p);
962 if (*p == '(')
964 /* ps command also relies on no trailing fields
965 ever contain ')'. */
966 const char *ep = strrchr (p, ')');
967 if (ep != NULL)
969 gdb_printf ("Exec file: %.*s\n",
970 (int) (ep - p - 1), p + 1);
971 p = ep + 1;
975 p = skip_spaces (p);
976 if (*p)
977 gdb_printf (_("State: %c\n"), *p++);
979 if (*p)
980 gdb_printf (_("Parent process: %s\n"),
981 pulongest (strtoulst (p, &p, 10)));
982 if (*p)
983 gdb_printf (_("Process group: %s\n"),
984 pulongest (strtoulst (p, &p, 10)));
985 if (*p)
986 gdb_printf (_("Session id: %s\n"),
987 pulongest (strtoulst (p, &p, 10)));
988 if (*p)
989 gdb_printf (_("TTY: %s\n"),
990 pulongest (strtoulst (p, &p, 10)));
991 if (*p)
992 gdb_printf (_("TTY owner process group: %s\n"),
993 pulongest (strtoulst (p, &p, 10)));
995 if (*p)
996 gdb_printf (_("Flags: %s\n"),
997 hex_string (strtoulst (p, &p, 10)));
998 if (*p)
999 gdb_printf (_("Minor faults (no memory page): %s\n"),
1000 pulongest (strtoulst (p, &p, 10)));
1001 if (*p)
1002 gdb_printf (_("Minor faults, children: %s\n"),
1003 pulongest (strtoulst (p, &p, 10)));
1004 if (*p)
1005 gdb_printf (_("Major faults (memory page faults): %s\n"),
1006 pulongest (strtoulst (p, &p, 10)));
1007 if (*p)
1008 gdb_printf (_("Major faults, children: %s\n"),
1009 pulongest (strtoulst (p, &p, 10)));
1010 if (*p)
1011 gdb_printf (_("utime: %s\n"),
1012 pulongest (strtoulst (p, &p, 10)));
1013 if (*p)
1014 gdb_printf (_("stime: %s\n"),
1015 pulongest (strtoulst (p, &p, 10)));
1016 if (*p)
1017 gdb_printf (_("utime, children: %s\n"),
1018 pulongest (strtoulst (p, &p, 10)));
1019 if (*p)
1020 gdb_printf (_("stime, children: %s\n"),
1021 pulongest (strtoulst (p, &p, 10)));
1022 if (*p)
1023 gdb_printf (_("jiffies remaining in current "
1024 "time slice: %s\n"),
1025 pulongest (strtoulst (p, &p, 10)));
1026 if (*p)
1027 gdb_printf (_("'nice' value: %s\n"),
1028 pulongest (strtoulst (p, &p, 10)));
1029 if (*p)
1030 gdb_printf (_("jiffies until next timeout: %s\n"),
1031 pulongest (strtoulst (p, &p, 10)));
1032 if (*p)
1033 gdb_printf (_("jiffies until next SIGALRM: %s\n"),
1034 pulongest (strtoulst (p, &p, 10)));
1035 if (*p)
1036 gdb_printf (_("start time (jiffies since "
1037 "system boot): %s\n"),
1038 pulongest (strtoulst (p, &p, 10)));
1039 if (*p)
1040 gdb_printf (_("Virtual memory size: %s\n"),
1041 pulongest (strtoulst (p, &p, 10)));
1042 if (*p)
1043 gdb_printf (_("Resident set size: %s\n"),
1044 pulongest (strtoulst (p, &p, 10)));
1045 if (*p)
1046 gdb_printf (_("rlim: %s\n"),
1047 pulongest (strtoulst (p, &p, 10)));
1048 if (*p)
1049 gdb_printf (_("Start of text: %s\n"),
1050 hex_string (strtoulst (p, &p, 10)));
1051 if (*p)
1052 gdb_printf (_("End of text: %s\n"),
1053 hex_string (strtoulst (p, &p, 10)));
1054 if (*p)
1055 gdb_printf (_("Start of stack: %s\n"),
1056 hex_string (strtoulst (p, &p, 10)));
1057 #if 0 /* Don't know how architecture-dependent the rest is...
1058 Anyway the signal bitmap info is available from "status". */
1059 if (*p)
1060 gdb_printf (_("Kernel stack pointer: %s\n"),
1061 hex_string (strtoulst (p, &p, 10)));
1062 if (*p)
1063 gdb_printf (_("Kernel instr pointer: %s\n"),
1064 hex_string (strtoulst (p, &p, 10)));
1065 if (*p)
1066 gdb_printf (_("Pending signals bitmap: %s\n"),
1067 hex_string (strtoulst (p, &p, 10)));
1068 if (*p)
1069 gdb_printf (_("Blocked signals bitmap: %s\n"),
1070 hex_string (strtoulst (p, &p, 10)));
1071 if (*p)
1072 gdb_printf (_("Ignored signals bitmap: %s\n"),
1073 hex_string (strtoulst (p, &p, 10)));
1074 if (*p)
1075 gdb_printf (_("Catched signals bitmap: %s\n"),
1076 hex_string (strtoulst (p, &p, 10)));
1077 if (*p)
1078 gdb_printf (_("wchan (system call): %s\n"),
1079 hex_string (strtoulst (p, &p, 10)));
1080 #endif
1082 else
1083 warning (_("unable to open /proc file '%s'"), filename);
1087 /* Implementation of `gdbarch_read_core_file_mappings', as defined in
1088 gdbarch.h.
1090 This function reads the NT_FILE note (which BFD turns into the
1091 section ".note.linuxcore.file"). The format of this note / section
1092 is described as follows in the Linux kernel sources in
1093 fs/binfmt_elf.c:
1095 long count -- how many files are mapped
1096 long page_size -- units for file_ofs
1097 array of [COUNT] elements of
1098 long start
1099 long end
1100 long file_ofs
1101 followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
1103 CBFD is the BFD of the core file.
1105 PRE_LOOP_CB is the callback function to invoke prior to starting
1106 the loop which processes individual entries. This callback will
1107 only be executed after the note has been examined in enough
1108 detail to verify that it's not malformed in some way.
1110 LOOP_CB is the callback function that will be executed once
1111 for each mapping. */
1113 static void
1114 linux_read_core_file_mappings
1115 (struct gdbarch *gdbarch,
1116 struct bfd *cbfd,
1117 read_core_file_mappings_pre_loop_ftype pre_loop_cb,
1118 read_core_file_mappings_loop_ftype loop_cb)
1120 /* Ensure that ULONGEST is big enough for reading 64-bit core files. */
1121 gdb_static_assert (sizeof (ULONGEST) >= 8);
1123 /* It's not required that the NT_FILE note exists, so return silently
1124 if it's not found. Beyond this point though, we'll complain
1125 if problems are found. */
1126 asection *section = bfd_get_section_by_name (cbfd, ".note.linuxcore.file");
1127 if (section == nullptr)
1128 return;
1130 unsigned int addr_size_bits = gdbarch_addr_bit (gdbarch);
1131 unsigned int addr_size = addr_size_bits / 8;
1132 size_t note_size = bfd_section_size (section);
1134 if (note_size < 2 * addr_size)
1136 warning (_("malformed core note - too short for header"));
1137 return;
1140 gdb::def_vector<gdb_byte> contents (note_size);
1141 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1142 0, note_size))
1144 warning (_("could not get core note contents"));
1145 return;
1148 gdb_byte *descdata = contents.data ();
1149 char *descend = (char *) descdata + note_size;
1151 if (descdata[note_size - 1] != '\0')
1153 warning (_("malformed note - does not end with \\0"));
1154 return;
1157 ULONGEST count = bfd_get (addr_size_bits, core_bfd, descdata);
1158 descdata += addr_size;
1160 ULONGEST page_size = bfd_get (addr_size_bits, core_bfd, descdata);
1161 descdata += addr_size;
1163 if (note_size < 2 * addr_size + count * 3 * addr_size)
1165 warning (_("malformed note - too short for supplied file count"));
1166 return;
1169 char *filenames = (char *) descdata + count * 3 * addr_size;
1171 /* Make sure that the correct number of filenames exist. Complain
1172 if there aren't enough or are too many. */
1173 char *f = filenames;
1174 for (int i = 0; i < count; i++)
1176 if (f >= descend)
1178 warning (_("malformed note - filename area is too small"));
1179 return;
1181 f += strnlen (f, descend - f) + 1;
1183 /* Complain, but don't return early if the filename area is too big. */
1184 if (f != descend)
1185 warning (_("malformed note - filename area is too big"));
1187 const bfd_build_id *orig_build_id = cbfd->build_id;
1188 std::unordered_map<ULONGEST, const bfd_build_id *> vma_map;
1190 /* Search for solib build-ids in the core file. Each time one is found,
1191 map the start vma of the corresponding elf header to the build-id. */
1192 for (bfd_section *sec = cbfd->sections; sec != nullptr; sec = sec->next)
1194 cbfd->build_id = nullptr;
1196 if (sec->flags & SEC_LOAD
1197 && (get_elf_backend_data (cbfd)->elf_backend_core_find_build_id
1198 (cbfd, (bfd_vma) sec->filepos)))
1199 vma_map[sec->vma] = cbfd->build_id;
1202 cbfd->build_id = orig_build_id;
1203 pre_loop_cb (count);
1205 for (int i = 0; i < count; i++)
1207 ULONGEST start = bfd_get (addr_size_bits, core_bfd, descdata);
1208 descdata += addr_size;
1209 ULONGEST end = bfd_get (addr_size_bits, core_bfd, descdata);
1210 descdata += addr_size;
1211 ULONGEST file_ofs
1212 = bfd_get (addr_size_bits, core_bfd, descdata) * page_size;
1213 descdata += addr_size;
1214 char * filename = filenames;
1215 filenames += strlen ((char *) filenames) + 1;
1216 const bfd_build_id *build_id = nullptr;
1217 auto vma_map_it = vma_map.find (start);
1219 if (vma_map_it != vma_map.end ())
1220 build_id = vma_map_it->second;
1222 loop_cb (i, start, end, file_ofs, filename, build_id);
1226 /* Implement "info proc mappings" for a corefile. */
1228 static void
1229 linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
1231 linux_read_core_file_mappings (gdbarch, core_bfd,
1232 [=] (ULONGEST count)
1234 gdb_printf (_("Mapped address spaces:\n\n"));
1235 if (gdbarch_addr_bit (gdbarch) == 32)
1237 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1238 "Start Addr",
1239 " End Addr",
1240 " Size", " Offset", "objfile");
1242 else
1244 gdb_printf (" %18s %18s %10s %10s %s\n",
1245 "Start Addr",
1246 " End Addr",
1247 " Size", " Offset", "objfile");
1250 [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
1251 const char *filename, const bfd_build_id *build_id)
1253 if (gdbarch_addr_bit (gdbarch) == 32)
1254 gdb_printf ("\t%10s %10s %10s %10s %s\n",
1255 paddress (gdbarch, start),
1256 paddress (gdbarch, end),
1257 hex_string (end - start),
1258 hex_string (file_ofs),
1259 filename);
1260 else
1261 gdb_printf (" %18s %18s %10s %10s %s\n",
1262 paddress (gdbarch, start),
1263 paddress (gdbarch, end),
1264 hex_string (end - start),
1265 hex_string (file_ofs),
1266 filename);
1270 /* Implement "info proc" for a corefile. */
1272 static void
1273 linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
1274 enum info_proc_what what)
1276 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
1277 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
1279 if (exe_f)
1281 const char *exe;
1283 exe = bfd_core_file_failing_command (core_bfd);
1284 if (exe != NULL)
1285 gdb_printf ("exe = '%s'\n", exe);
1286 else
1287 warning (_("unable to find command name in core file"));
1290 if (mappings_f)
1291 linux_core_info_proc_mappings (gdbarch, args);
1293 if (!exe_f && !mappings_f)
1294 error (_("unable to handle request"));
1297 /* Read siginfo data from the core, if possible. Returns -1 on
1298 failure. Otherwise, returns the number of bytes read. READBUF,
1299 OFFSET, and LEN are all as specified by the to_xfer_partial
1300 interface. */
1302 static LONGEST
1303 linux_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
1304 ULONGEST offset, ULONGEST len)
1306 thread_section_name section_name (".note.linuxcore.siginfo", inferior_ptid);
1307 asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
1308 if (section == NULL)
1309 return -1;
1311 if (!bfd_get_section_contents (core_bfd, section, readbuf, offset, len))
1312 return -1;
1314 return len;
1317 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
1318 ULONGEST offset, ULONGEST inode,
1319 int read, int write,
1320 int exec, int modified,
1321 bool memory_tagged,
1322 const char *filename,
1323 void *data);
1325 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags,
1326 const struct smaps_vmflags *v,
1327 int maybe_private_p,
1328 int mapping_anon_p,
1329 int mapping_file_p,
1330 const char *filename,
1331 ULONGEST addr,
1332 ULONGEST offset);
1334 /* Helper function to parse the contents of /proc/<pid>/smaps into a data
1335 structure, for easy access.
1337 DATA is the contents of the smaps file. The parsed contents are stored
1338 into the SMAPS vector. */
1340 static std::vector<struct smaps_data>
1341 parse_smaps_data (const char *data,
1342 const std::string maps_filename)
1344 char *line, *t;
1346 gdb_assert (data != nullptr);
1348 line = strtok_r ((char *) data, "\n", &t);
1350 std::vector<struct smaps_data> smaps;
1352 while (line != NULL)
1354 struct smaps_vmflags v;
1355 int read, write, exec, priv;
1356 int has_anonymous = 0;
1357 int mapping_anon_p;
1358 int mapping_file_p;
1360 memset (&v, 0, sizeof (v));
1361 struct mapping m = read_mapping (line);
1362 mapping_anon_p = mapping_is_anonymous_p (m.filename);
1363 /* If the mapping is not anonymous, then we can consider it
1364 to be file-backed. These two states (anonymous or
1365 file-backed) seem to be exclusive, but they can actually
1366 coexist. For example, if a file-backed mapping has
1367 "Anonymous:" pages (see more below), then the Linux
1368 kernel will dump this mapping when the user specified
1369 that she only wants anonymous mappings in the corefile
1370 (*even* when she explicitly disabled the dumping of
1371 file-backed mappings). */
1372 mapping_file_p = !mapping_anon_p;
1374 /* Decode permissions. */
1375 auto has_perm = [&m] (char c)
1376 { return m.permissions.find (c) != gdb::string_view::npos; };
1377 read = has_perm ('r');
1378 write = has_perm ('w');
1379 exec = has_perm ('x');
1381 /* 'private' here actually means VM_MAYSHARE, and not
1382 VM_SHARED. In order to know if a mapping is really
1383 private or not, we must check the flag "sh" in the
1384 VmFlags field. This is done by decode_vmflags. However,
1385 if we are using a Linux kernel released before the commit
1386 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
1387 not have the VmFlags there. In this case, there is
1388 really no way to know if we are dealing with VM_SHARED,
1389 so we just assume that VM_MAYSHARE is enough. */
1390 priv = has_perm ('p');
1392 /* Try to detect if region should be dumped by parsing smaps
1393 counters. */
1394 for (line = strtok_r (NULL, "\n", &t);
1395 line != NULL && line[0] >= 'A' && line[0] <= 'Z';
1396 line = strtok_r (NULL, "\n", &t))
1398 char keyword[64 + 1];
1400 if (sscanf (line, "%64s", keyword) != 1)
1402 warning (_("Error parsing {s,}maps file '%s'"),
1403 maps_filename.c_str ());
1404 break;
1407 if (strcmp (keyword, "Anonymous:") == 0)
1409 /* Older Linux kernels did not support the
1410 "Anonymous:" counter. Check it here. */
1411 has_anonymous = 1;
1413 else if (strcmp (keyword, "VmFlags:") == 0)
1414 decode_vmflags (line, &v);
1416 if (strcmp (keyword, "AnonHugePages:") == 0
1417 || strcmp (keyword, "Anonymous:") == 0)
1419 unsigned long number;
1421 if (sscanf (line, "%*s%lu", &number) != 1)
1423 warning (_("Error parsing {s,}maps file '%s' number"),
1424 maps_filename.c_str ());
1425 break;
1427 if (number > 0)
1429 /* Even if we are dealing with a file-backed
1430 mapping, if it contains anonymous pages we
1431 consider it to be *also* an anonymous
1432 mapping, because this is what the Linux
1433 kernel does:
1435 // Dump segments that have been written to.
1436 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1437 goto whole;
1439 Note that if the mapping is already marked as
1440 file-backed (i.e., mapping_file_p is
1441 non-zero), then this is a special case, and
1442 this mapping will be dumped either when the
1443 user wants to dump file-backed *or* anonymous
1444 mappings. */
1445 mapping_anon_p = 1;
1449 /* Save the smaps entry to the vector. */
1450 struct smaps_data map;
1452 map.start_address = m.addr;
1453 map.end_address = m.endaddr;
1454 map.filename = m.filename;
1455 map.vmflags = v;
1456 map.read = read? true : false;
1457 map.write = write? true : false;
1458 map.exec = exec? true : false;
1459 map.priv = priv? true : false;
1460 map.has_anonymous = has_anonymous;
1461 map.mapping_anon_p = mapping_anon_p? true : false;
1462 map.mapping_file_p = mapping_file_p? true : false;
1463 map.offset = m.offset;
1464 map.inode = m.inode;
1466 smaps.emplace_back (map);
1469 return smaps;
1472 /* Helper that checks if an address is in a memory tag page for a live
1473 process. */
1475 static bool
1476 linux_process_address_in_memtag_page (CORE_ADDR address)
1478 if (current_inferior ()->fake_pid_p)
1479 return false;
1481 pid_t pid = current_inferior ()->pid;
1483 std::string smaps_file = string_printf ("/proc/%d/smaps", pid);
1485 gdb::unique_xmalloc_ptr<char> data
1486 = target_fileio_read_stralloc (NULL, smaps_file.c_str ());
1488 if (data == nullptr)
1489 return false;
1491 /* Parse the contents of smaps into a vector. */
1492 std::vector<struct smaps_data> smaps
1493 = parse_smaps_data (data.get (), smaps_file);
1495 for (const smaps_data &map : smaps)
1497 /* Is the address within [start_address, end_address) in a page
1498 mapped with memory tagging? */
1499 if (address >= map.start_address
1500 && address < map.end_address
1501 && map.vmflags.memory_tagging)
1502 return true;
1505 return false;
1508 /* Helper that checks if an address is in a memory tag page for a core file
1509 process. */
1511 static bool
1512 linux_core_file_address_in_memtag_page (CORE_ADDR address)
1514 if (core_bfd == nullptr)
1515 return false;
1517 memtag_section_info info;
1518 return get_next_core_memtag_section (core_bfd, 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 static int
1702 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
1703 ULONGEST offset, ULONGEST inode,
1704 int read, int write, int exec, int modified,
1705 bool memory_tagged,
1706 const char *filename, void *data)
1708 struct linux_make_mappings_data *map_data
1709 = (struct linux_make_mappings_data *) data;
1710 gdb_byte buf[sizeof (ULONGEST)];
1712 if (*filename == '\0' || inode == 0)
1713 return 0;
1715 ++map_data->file_count;
1717 pack_long (buf, map_data->long_type, vaddr);
1718 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1719 pack_long (buf, map_data->long_type, vaddr + size);
1720 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1721 pack_long (buf, map_data->long_type, offset);
1722 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1724 obstack_grow_str0 (map_data->filename_obstack, filename);
1726 return 0;
1729 /* Write the file mapping data to the core file, if possible. OBFD is
1730 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1731 is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
1733 static void
1734 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
1735 gdb::unique_xmalloc_ptr<char> &note_data,
1736 int *note_size)
1738 struct linux_make_mappings_data mapping_data;
1739 struct type *long_type
1740 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
1741 gdb_byte buf[sizeof (ULONGEST)];
1743 auto_obstack data_obstack, filename_obstack;
1745 mapping_data.file_count = 0;
1746 mapping_data.data_obstack = &data_obstack;
1747 mapping_data.filename_obstack = &filename_obstack;
1748 mapping_data.long_type = long_type;
1750 /* Reserve space for the count. */
1751 obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
1752 /* We always write the page size as 1 since we have no good way to
1753 determine the correct value. */
1754 pack_long (buf, long_type, 1);
1755 obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
1757 linux_find_memory_regions_full (gdbarch,
1758 dump_note_entry_p,
1759 linux_make_mappings_callback,
1760 &mapping_data);
1762 if (mapping_data.file_count != 0)
1764 /* Write the count to the obstack. */
1765 pack_long ((gdb_byte *) obstack_base (&data_obstack),
1766 long_type, mapping_data.file_count);
1768 /* Copy the filenames to the data obstack. */
1769 int size = obstack_object_size (&filename_obstack);
1770 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
1771 size);
1773 note_data.reset (elfcore_write_file_note (obfd, note_data.release (), note_size,
1774 obstack_base (&data_obstack),
1775 obstack_object_size (&data_obstack)));
1779 /* Fetch the siginfo data for the specified thread, if it exists. If
1780 there is no data, or we could not read it, return an empty
1781 buffer. */
1783 static gdb::byte_vector
1784 linux_get_siginfo_data (thread_info *thread, struct gdbarch *gdbarch)
1786 struct type *siginfo_type;
1787 LONGEST bytes_read;
1789 if (!gdbarch_get_siginfo_type_p (gdbarch))
1790 return gdb::byte_vector ();
1792 scoped_restore_current_thread save_current_thread;
1793 switch_to_thread (thread);
1795 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1797 gdb::byte_vector buf (TYPE_LENGTH (siginfo_type));
1799 bytes_read = target_read (current_inferior ()->top_target (),
1800 TARGET_OBJECT_SIGNAL_INFO, NULL,
1801 buf.data (), 0, TYPE_LENGTH (siginfo_type));
1802 if (bytes_read != TYPE_LENGTH (siginfo_type))
1803 buf.clear ();
1805 return buf;
1808 struct linux_corefile_thread_data
1810 linux_corefile_thread_data (struct gdbarch *gdbarch, bfd *obfd,
1811 gdb::unique_xmalloc_ptr<char> &note_data,
1812 int *note_size, gdb_signal stop_signal)
1813 : gdbarch (gdbarch), obfd (obfd), note_data (note_data),
1814 note_size (note_size), stop_signal (stop_signal)
1817 struct gdbarch *gdbarch;
1818 bfd *obfd;
1819 gdb::unique_xmalloc_ptr<char> &note_data;
1820 int *note_size;
1821 enum gdb_signal stop_signal;
1824 /* Records the thread's register state for the corefile note
1825 section. */
1827 static void
1828 linux_corefile_thread (struct thread_info *info,
1829 struct linux_corefile_thread_data *args)
1831 gcore_elf_build_thread_register_notes (args->gdbarch, info,
1832 args->stop_signal,
1833 args->obfd, &args->note_data,
1834 args->note_size);
1836 /* Don't return anything if we got no register information above,
1837 such a core file is useless. */
1838 if (args->note_data != NULL)
1840 gdb::byte_vector siginfo_data
1841 = linux_get_siginfo_data (info, args->gdbarch);
1842 if (!siginfo_data.empty ())
1843 args->note_data.reset (elfcore_write_note (args->obfd,
1844 args->note_data.release (),
1845 args->note_size,
1846 "CORE", NT_SIGINFO,
1847 siginfo_data.data (),
1848 siginfo_data.size ()));
1852 /* Fill the PRPSINFO structure with information about the process being
1853 debugged. Returns 1 in case of success, 0 for failures. Please note that
1854 even if the structure cannot be entirely filled (e.g., GDB was unable to
1855 gather information about the process UID/GID), this function will still
1856 return 1 since some information was already recorded. It will only return
1857 0 iff nothing can be gathered. */
1859 static int
1860 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
1862 /* The filename which we will use to obtain some info about the process.
1863 We will basically use this to store the `/proc/PID/FILENAME' file. */
1864 char filename[100];
1865 /* The basename of the executable. */
1866 const char *basename;
1867 /* Temporary buffer. */
1868 char *tmpstr;
1869 /* The valid states of a process, according to the Linux kernel. */
1870 const char valid_states[] = "RSDTZW";
1871 /* The program state. */
1872 const char *prog_state;
1873 /* The state of the process. */
1874 char pr_sname;
1875 /* The PID of the program which generated the corefile. */
1876 pid_t pid;
1877 /* Process flags. */
1878 unsigned int pr_flag;
1879 /* Process nice value. */
1880 long pr_nice;
1881 /* The number of fields read by `sscanf'. */
1882 int n_fields = 0;
1884 gdb_assert (p != NULL);
1886 /* Obtaining PID and filename. */
1887 pid = inferior_ptid.pid ();
1888 xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
1889 /* The full name of the program which generated the corefile. */
1890 gdb::unique_xmalloc_ptr<char> fname
1891 = target_fileio_read_stralloc (NULL, filename);
1893 if (fname == NULL || fname.get ()[0] == '\0')
1895 /* No program name was read, so we won't be able to retrieve more
1896 information about the process. */
1897 return 0;
1900 memset (p, 0, sizeof (*p));
1902 /* Defining the PID. */
1903 p->pr_pid = pid;
1905 /* Copying the program name. Only the basename matters. */
1906 basename = lbasename (fname.get ());
1907 strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
1908 p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
1910 const std::string &infargs = current_inferior ()->args ();
1912 /* The arguments of the program. */
1913 std::string psargs = fname.get ();
1914 if (!infargs.empty ())
1915 psargs += ' ' + infargs;
1917 strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
1918 p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
1920 xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
1921 /* The contents of `/proc/PID/stat'. */
1922 gdb::unique_xmalloc_ptr<char> proc_stat_contents
1923 = target_fileio_read_stralloc (NULL, filename);
1924 char *proc_stat = proc_stat_contents.get ();
1926 if (proc_stat == NULL || *proc_stat == '\0')
1928 /* Despite being unable to read more information about the
1929 process, we return 1 here because at least we have its
1930 command line, PID and arguments. */
1931 return 1;
1934 /* Ok, we have the stats. It's time to do a little parsing of the
1935 contents of the buffer, so that we end up reading what we want.
1937 The following parsing mechanism is strongly based on the
1938 information generated by the `fs/proc/array.c' file, present in
1939 the Linux kernel tree. More details about how the information is
1940 displayed can be obtained by seeing the manpage of proc(5),
1941 specifically under the entry of `/proc/[pid]/stat'. */
1943 /* Getting rid of the PID, since we already have it. */
1944 while (isdigit (*proc_stat))
1945 ++proc_stat;
1947 proc_stat = skip_spaces (proc_stat);
1949 /* ps command also relies on no trailing fields ever contain ')'. */
1950 proc_stat = strrchr (proc_stat, ')');
1951 if (proc_stat == NULL)
1952 return 1;
1953 proc_stat++;
1955 proc_stat = skip_spaces (proc_stat);
1957 n_fields = sscanf (proc_stat,
1958 "%c" /* Process state. */
1959 "%d%d%d" /* Parent PID, group ID, session ID. */
1960 "%*d%*d" /* tty_nr, tpgid (not used). */
1961 "%u" /* Flags. */
1962 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1963 cmajflt (not used). */
1964 "%*s%*s%*s%*s" /* utime, stime, cutime,
1965 cstime (not used). */
1966 "%*s" /* Priority (not used). */
1967 "%ld", /* Nice. */
1968 &pr_sname,
1969 &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1970 &pr_flag,
1971 &pr_nice);
1973 if (n_fields != 6)
1975 /* Again, we couldn't read the complementary information about
1976 the process state. However, we already have minimal
1977 information, so we just return 1 here. */
1978 return 1;
1981 /* Filling the structure fields. */
1982 prog_state = strchr (valid_states, pr_sname);
1983 if (prog_state != NULL)
1984 p->pr_state = prog_state - valid_states;
1985 else
1987 /* Zero means "Running". */
1988 p->pr_state = 0;
1991 p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
1992 p->pr_zomb = p->pr_sname == 'Z';
1993 p->pr_nice = pr_nice;
1994 p->pr_flag = pr_flag;
1996 /* Finally, obtaining the UID and GID. For that, we read and parse the
1997 contents of the `/proc/PID/status' file. */
1998 xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
1999 /* The contents of `/proc/PID/status'. */
2000 gdb::unique_xmalloc_ptr<char> proc_status_contents
2001 = target_fileio_read_stralloc (NULL, filename);
2002 char *proc_status = proc_status_contents.get ();
2004 if (proc_status == NULL || *proc_status == '\0')
2006 /* Returning 1 since we already have a bunch of information. */
2007 return 1;
2010 /* Extracting the UID. */
2011 tmpstr = strstr (proc_status, "Uid:");
2012 if (tmpstr != NULL)
2014 /* Advancing the pointer to the beginning of the UID. */
2015 tmpstr += sizeof ("Uid:");
2016 while (*tmpstr != '\0' && !isdigit (*tmpstr))
2017 ++tmpstr;
2019 if (isdigit (*tmpstr))
2020 p->pr_uid = strtol (tmpstr, &tmpstr, 10);
2023 /* Extracting the GID. */
2024 tmpstr = strstr (proc_status, "Gid:");
2025 if (tmpstr != NULL)
2027 /* Advancing the pointer to the beginning of the GID. */
2028 tmpstr += sizeof ("Gid:");
2029 while (*tmpstr != '\0' && !isdigit (*tmpstr))
2030 ++tmpstr;
2032 if (isdigit (*tmpstr))
2033 p->pr_gid = strtol (tmpstr, &tmpstr, 10);
2036 return 1;
2039 /* Build the note section for a corefile, and return it in a malloc
2040 buffer. */
2042 static gdb::unique_xmalloc_ptr<char>
2043 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
2045 struct elf_internal_linux_prpsinfo prpsinfo;
2046 gdb::unique_xmalloc_ptr<char> note_data;
2048 if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
2049 return NULL;
2051 if (linux_fill_prpsinfo (&prpsinfo))
2053 if (gdbarch_ptr_bit (gdbarch) == 64)
2054 note_data.reset (elfcore_write_linux_prpsinfo64 (obfd,
2055 note_data.release (),
2056 note_size, &prpsinfo));
2057 else
2058 note_data.reset (elfcore_write_linux_prpsinfo32 (obfd,
2059 note_data.release (),
2060 note_size, &prpsinfo));
2063 /* Thread register information. */
2066 update_thread_list ();
2068 catch (const gdb_exception_error &e)
2070 exception_print (gdb_stderr, e);
2073 /* Like the kernel, prefer dumping the signalled thread first.
2074 "First thread" is what tools use to infer the signalled
2075 thread. */
2076 thread_info *signalled_thr = gcore_find_signalled_thread ();
2077 gdb_signal stop_signal;
2078 if (signalled_thr != nullptr)
2079 stop_signal = signalled_thr->stop_signal ();
2080 else
2081 stop_signal = GDB_SIGNAL_0;
2083 linux_corefile_thread_data thread_args (gdbarch, obfd, note_data, note_size,
2084 stop_signal);
2086 if (signalled_thr != nullptr)
2087 linux_corefile_thread (signalled_thr, &thread_args);
2088 for (thread_info *thr : current_inferior ()->non_exited_threads ())
2090 if (thr == signalled_thr)
2091 continue;
2093 linux_corefile_thread (thr, &thread_args);
2096 if (!note_data)
2097 return NULL;
2099 /* Auxillary vector. */
2100 gdb::optional<gdb::byte_vector> auxv =
2101 target_read_alloc (current_inferior ()->top_target (),
2102 TARGET_OBJECT_AUXV, NULL);
2103 if (auxv && !auxv->empty ())
2105 note_data.reset (elfcore_write_note (obfd, note_data.release (),
2106 note_size, "CORE", NT_AUXV,
2107 auxv->data (), auxv->size ()));
2109 if (!note_data)
2110 return NULL;
2113 /* File mappings. */
2114 linux_make_mappings_corefile_notes (gdbarch, obfd, note_data, note_size);
2116 /* Target description. */
2117 gcore_elf_make_tdesc_note (obfd, &note_data, note_size);
2119 return note_data;
2122 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
2123 gdbarch.h. This function is not static because it is exported to
2124 other -tdep files. */
2126 enum gdb_signal
2127 linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
2129 switch (signal)
2131 case 0:
2132 return GDB_SIGNAL_0;
2134 case LINUX_SIGHUP:
2135 return GDB_SIGNAL_HUP;
2137 case LINUX_SIGINT:
2138 return GDB_SIGNAL_INT;
2140 case LINUX_SIGQUIT:
2141 return GDB_SIGNAL_QUIT;
2143 case LINUX_SIGILL:
2144 return GDB_SIGNAL_ILL;
2146 case LINUX_SIGTRAP:
2147 return GDB_SIGNAL_TRAP;
2149 case LINUX_SIGABRT:
2150 return GDB_SIGNAL_ABRT;
2152 case LINUX_SIGBUS:
2153 return GDB_SIGNAL_BUS;
2155 case LINUX_SIGFPE:
2156 return GDB_SIGNAL_FPE;
2158 case LINUX_SIGKILL:
2159 return GDB_SIGNAL_KILL;
2161 case LINUX_SIGUSR1:
2162 return GDB_SIGNAL_USR1;
2164 case LINUX_SIGSEGV:
2165 return GDB_SIGNAL_SEGV;
2167 case LINUX_SIGUSR2:
2168 return GDB_SIGNAL_USR2;
2170 case LINUX_SIGPIPE:
2171 return GDB_SIGNAL_PIPE;
2173 case LINUX_SIGALRM:
2174 return GDB_SIGNAL_ALRM;
2176 case LINUX_SIGTERM:
2177 return GDB_SIGNAL_TERM;
2179 case LINUX_SIGCHLD:
2180 return GDB_SIGNAL_CHLD;
2182 case LINUX_SIGCONT:
2183 return GDB_SIGNAL_CONT;
2185 case LINUX_SIGSTOP:
2186 return GDB_SIGNAL_STOP;
2188 case LINUX_SIGTSTP:
2189 return GDB_SIGNAL_TSTP;
2191 case LINUX_SIGTTIN:
2192 return GDB_SIGNAL_TTIN;
2194 case LINUX_SIGTTOU:
2195 return GDB_SIGNAL_TTOU;
2197 case LINUX_SIGURG:
2198 return GDB_SIGNAL_URG;
2200 case LINUX_SIGXCPU:
2201 return GDB_SIGNAL_XCPU;
2203 case LINUX_SIGXFSZ:
2204 return GDB_SIGNAL_XFSZ;
2206 case LINUX_SIGVTALRM:
2207 return GDB_SIGNAL_VTALRM;
2209 case LINUX_SIGPROF:
2210 return GDB_SIGNAL_PROF;
2212 case LINUX_SIGWINCH:
2213 return GDB_SIGNAL_WINCH;
2215 /* No way to differentiate between SIGIO and SIGPOLL.
2216 Therefore, we just handle the first one. */
2217 case LINUX_SIGIO:
2218 return GDB_SIGNAL_IO;
2220 case LINUX_SIGPWR:
2221 return GDB_SIGNAL_PWR;
2223 case LINUX_SIGSYS:
2224 return GDB_SIGNAL_SYS;
2226 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
2227 therefore we have to handle them here. */
2228 case LINUX_SIGRTMIN:
2229 return GDB_SIGNAL_REALTIME_32;
2231 case LINUX_SIGRTMAX:
2232 return GDB_SIGNAL_REALTIME_64;
2235 if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
2237 int offset = signal - LINUX_SIGRTMIN + 1;
2239 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
2242 return GDB_SIGNAL_UNKNOWN;
2245 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
2246 gdbarch.h. This function is not static because it is exported to
2247 other -tdep files. */
2250 linux_gdb_signal_to_target (struct gdbarch *gdbarch,
2251 enum gdb_signal signal)
2253 switch (signal)
2255 case GDB_SIGNAL_0:
2256 return 0;
2258 case GDB_SIGNAL_HUP:
2259 return LINUX_SIGHUP;
2261 case GDB_SIGNAL_INT:
2262 return LINUX_SIGINT;
2264 case GDB_SIGNAL_QUIT:
2265 return LINUX_SIGQUIT;
2267 case GDB_SIGNAL_ILL:
2268 return LINUX_SIGILL;
2270 case GDB_SIGNAL_TRAP:
2271 return LINUX_SIGTRAP;
2273 case GDB_SIGNAL_ABRT:
2274 return LINUX_SIGABRT;
2276 case GDB_SIGNAL_FPE:
2277 return LINUX_SIGFPE;
2279 case GDB_SIGNAL_KILL:
2280 return LINUX_SIGKILL;
2282 case GDB_SIGNAL_BUS:
2283 return LINUX_SIGBUS;
2285 case GDB_SIGNAL_SEGV:
2286 return LINUX_SIGSEGV;
2288 case GDB_SIGNAL_SYS:
2289 return LINUX_SIGSYS;
2291 case GDB_SIGNAL_PIPE:
2292 return LINUX_SIGPIPE;
2294 case GDB_SIGNAL_ALRM:
2295 return LINUX_SIGALRM;
2297 case GDB_SIGNAL_TERM:
2298 return LINUX_SIGTERM;
2300 case GDB_SIGNAL_URG:
2301 return LINUX_SIGURG;
2303 case GDB_SIGNAL_STOP:
2304 return LINUX_SIGSTOP;
2306 case GDB_SIGNAL_TSTP:
2307 return LINUX_SIGTSTP;
2309 case GDB_SIGNAL_CONT:
2310 return LINUX_SIGCONT;
2312 case GDB_SIGNAL_CHLD:
2313 return LINUX_SIGCHLD;
2315 case GDB_SIGNAL_TTIN:
2316 return LINUX_SIGTTIN;
2318 case GDB_SIGNAL_TTOU:
2319 return LINUX_SIGTTOU;
2321 case GDB_SIGNAL_IO:
2322 return LINUX_SIGIO;
2324 case GDB_SIGNAL_XCPU:
2325 return LINUX_SIGXCPU;
2327 case GDB_SIGNAL_XFSZ:
2328 return LINUX_SIGXFSZ;
2330 case GDB_SIGNAL_VTALRM:
2331 return LINUX_SIGVTALRM;
2333 case GDB_SIGNAL_PROF:
2334 return LINUX_SIGPROF;
2336 case GDB_SIGNAL_WINCH:
2337 return LINUX_SIGWINCH;
2339 case GDB_SIGNAL_USR1:
2340 return LINUX_SIGUSR1;
2342 case GDB_SIGNAL_USR2:
2343 return LINUX_SIGUSR2;
2345 case GDB_SIGNAL_PWR:
2346 return LINUX_SIGPWR;
2348 case GDB_SIGNAL_POLL:
2349 return LINUX_SIGPOLL;
2351 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
2352 therefore we have to handle it here. */
2353 case GDB_SIGNAL_REALTIME_32:
2354 return LINUX_SIGRTMIN;
2356 /* Same comment applies to _64. */
2357 case GDB_SIGNAL_REALTIME_64:
2358 return LINUX_SIGRTMAX;
2361 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
2362 if (signal >= GDB_SIGNAL_REALTIME_33
2363 && signal <= GDB_SIGNAL_REALTIME_63)
2365 int offset = signal - GDB_SIGNAL_REALTIME_33;
2367 return LINUX_SIGRTMIN + 1 + offset;
2370 return -1;
2373 /* Helper for linux_vsyscall_range that does the real work of finding
2374 the vsyscall's address range. */
2376 static int
2377 linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
2379 char filename[100];
2380 long pid;
2382 if (target_auxv_search (current_inferior ()->top_target (),
2383 AT_SYSINFO_EHDR, &range->start) <= 0)
2384 return 0;
2386 /* It doesn't make sense to access the host's /proc when debugging a
2387 core file. Instead, look for the PT_LOAD segment that matches
2388 the vDSO. */
2389 if (!target_has_execution ())
2391 long phdrs_size;
2392 int num_phdrs, i;
2394 phdrs_size = bfd_get_elf_phdr_upper_bound (core_bfd);
2395 if (phdrs_size == -1)
2396 return 0;
2398 gdb::unique_xmalloc_ptr<Elf_Internal_Phdr>
2399 phdrs ((Elf_Internal_Phdr *) xmalloc (phdrs_size));
2400 num_phdrs = bfd_get_elf_phdrs (core_bfd, phdrs.get ());
2401 if (num_phdrs == -1)
2402 return 0;
2404 for (i = 0; i < num_phdrs; i++)
2405 if (phdrs.get ()[i].p_type == PT_LOAD
2406 && phdrs.get ()[i].p_vaddr == range->start)
2408 range->length = phdrs.get ()[i].p_memsz;
2409 return 1;
2412 return 0;
2415 /* We need to know the real target PID to access /proc. */
2416 if (current_inferior ()->fake_pid_p)
2417 return 0;
2419 pid = current_inferior ()->pid;
2421 /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
2422 reading /proc/PID/maps (2). The later identifies thread stacks
2423 in the output, which requires scanning every thread in the thread
2424 group to check whether a VMA is actually a thread's stack. With
2425 Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
2426 a few thousand threads, (1) takes a few miliseconds, while (2)
2427 takes several seconds. Also note that "smaps", what we read for
2428 determining core dump mappings, is even slower than "maps". */
2429 xsnprintf (filename, sizeof filename, "/proc/%ld/task/%ld/maps", pid, pid);
2430 gdb::unique_xmalloc_ptr<char> data
2431 = target_fileio_read_stralloc (NULL, filename);
2432 if (data != NULL)
2434 char *line;
2435 char *saveptr = NULL;
2437 for (line = strtok_r (data.get (), "\n", &saveptr);
2438 line != NULL;
2439 line = strtok_r (NULL, "\n", &saveptr))
2441 ULONGEST addr, endaddr;
2442 const char *p = line;
2444 addr = strtoulst (p, &p, 16);
2445 if (addr == range->start)
2447 if (*p == '-')
2448 p++;
2449 endaddr = strtoulst (p, &p, 16);
2450 range->length = endaddr - addr;
2451 return 1;
2455 else
2456 warning (_("unable to open /proc file '%s'"), filename);
2458 return 0;
2461 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2462 caching, and defers the real work to linux_vsyscall_range_raw. */
2464 static int
2465 linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
2467 struct linux_info *info = get_linux_inferior_data (current_inferior ());
2469 if (info->vsyscall_range_p == 0)
2471 if (linux_vsyscall_range_raw (gdbarch, &info->vsyscall_range))
2472 info->vsyscall_range_p = 1;
2473 else
2474 info->vsyscall_range_p = -1;
2477 if (info->vsyscall_range_p < 0)
2478 return 0;
2480 *range = info->vsyscall_range;
2481 return 1;
2484 /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
2485 definitions would be dependent on compilation host. */
2486 #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
2487 #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
2489 /* See gdbarch.sh 'infcall_mmap'. */
2491 static CORE_ADDR
2492 linux_infcall_mmap (CORE_ADDR size, unsigned prot)
2494 struct objfile *objf;
2495 /* Do there still exist any Linux systems without "mmap64"?
2496 "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
2497 struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
2498 struct value *addr_val;
2499 struct gdbarch *gdbarch = objf->arch ();
2500 CORE_ADDR retval;
2501 enum
2503 ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_LAST
2505 struct value *arg[ARG_LAST];
2507 arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
2509 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2510 arg[ARG_LENGTH] = value_from_ulongest
2511 (builtin_type (gdbarch)->builtin_unsigned_long, size);
2512 gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
2513 | GDB_MMAP_PROT_EXEC))
2514 == 0);
2515 arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int, prot);
2516 arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
2517 GDB_MMAP_MAP_PRIVATE
2518 | GDB_MMAP_MAP_ANONYMOUS);
2519 arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1);
2520 arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64,
2522 addr_val = call_function_by_hand (mmap_val, NULL, arg);
2523 retval = value_as_address (addr_val);
2524 if (retval == (CORE_ADDR) -1)
2525 error (_("Failed inferior mmap call for %s bytes, errno is changed."),
2526 pulongest (size));
2527 return retval;
2530 /* See gdbarch.sh 'infcall_munmap'. */
2532 static void
2533 linux_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
2535 struct objfile *objf;
2536 struct value *munmap_val = find_function_in_inferior ("munmap", &objf);
2537 struct value *retval_val;
2538 struct gdbarch *gdbarch = objf->arch ();
2539 LONGEST retval;
2540 enum
2542 ARG_ADDR, ARG_LENGTH, ARG_LAST
2544 struct value *arg[ARG_LAST];
2546 arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
2547 addr);
2548 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2549 arg[ARG_LENGTH] = value_from_ulongest
2550 (builtin_type (gdbarch)->builtin_unsigned_long, size);
2551 retval_val = call_function_by_hand (munmap_val, NULL, arg);
2552 retval = value_as_long (retval_val);
2553 if (retval != 0)
2554 warning (_("Failed inferior munmap call at %s for %s bytes, "
2555 "errno is changed."),
2556 hex_string (addr), pulongest (size));
2559 /* See linux-tdep.h. */
2561 CORE_ADDR
2562 linux_displaced_step_location (struct gdbarch *gdbarch)
2564 CORE_ADDR addr;
2565 int bp_len;
2567 /* Determine entry point from target auxiliary vector. This avoids
2568 the need for symbols. Also, when debugging a stand-alone SPU
2569 executable, entry_point_address () will point to an SPU
2570 local-store address and is thus not usable as displaced stepping
2571 location. The auxiliary vector gets us the PowerPC-side entry
2572 point address instead. */
2573 if (target_auxv_search (current_inferior ()->top_target (),
2574 AT_ENTRY, &addr) <= 0)
2575 throw_error (NOT_SUPPORTED_ERROR,
2576 _("Cannot find AT_ENTRY auxiliary vector entry."));
2578 /* Make certain that the address points at real code, and not a
2579 function descriptor. */
2580 addr = gdbarch_convert_from_func_ptr_addr
2581 (gdbarch, addr, current_inferior ()->top_target ());
2583 /* Inferior calls also use the entry point as a breakpoint location.
2584 We don't want displaced stepping to interfere with those
2585 breakpoints, so leave space. */
2586 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
2587 addr += bp_len * 2;
2589 return addr;
2592 /* See linux-tdep.h. */
2594 displaced_step_prepare_status
2595 linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
2596 CORE_ADDR &displaced_pc)
2598 linux_info *per_inferior = get_linux_inferior_data (thread->inf);
2600 if (!per_inferior->disp_step_bufs.has_value ())
2602 /* Figure out the location of the buffers. They are contiguous, starting
2603 at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
2604 CORE_ADDR disp_step_buf_addr
2605 = linux_displaced_step_location (thread->inf->gdbarch);
2606 int buf_len = gdbarch_max_insn_length (arch);
2608 linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (arch);
2609 gdb_assert (gdbarch_data->num_disp_step_buffers > 0);
2611 std::vector<CORE_ADDR> buffers;
2612 for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
2613 buffers.push_back (disp_step_buf_addr + i * buf_len);
2615 per_inferior->disp_step_bufs.emplace (buffers);
2618 return per_inferior->disp_step_bufs->prepare (thread, displaced_pc);
2621 /* See linux-tdep.h. */
2623 displaced_step_finish_status
2624 linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
2626 linux_info *per_inferior = get_linux_inferior_data (thread->inf);
2628 gdb_assert (per_inferior->disp_step_bufs.has_value ());
2630 return per_inferior->disp_step_bufs->finish (arch, thread, sig);
2633 /* See linux-tdep.h. */
2635 const displaced_step_copy_insn_closure *
2636 linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
2638 linux_info *per_inferior = linux_inferior_data.get (inf);
2640 if (per_inferior == nullptr
2641 || !per_inferior->disp_step_bufs.has_value ())
2642 return nullptr;
2644 return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
2647 /* See linux-tdep.h. */
2649 void
2650 linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
2652 linux_info *per_inferior = linux_inferior_data.get (parent_inf);
2654 if (per_inferior == nullptr
2655 || !per_inferior->disp_step_bufs.has_value ())
2656 return;
2658 per_inferior->disp_step_bufs->restore_in_ptid (ptid);
2661 /* See linux-tdep.h. */
2663 CORE_ADDR
2664 linux_get_hwcap (struct target_ops *target)
2666 CORE_ADDR field;
2667 if (target_auxv_search (target, AT_HWCAP, &field) != 1)
2668 return 0;
2669 return field;
2672 /* See linux-tdep.h. */
2674 CORE_ADDR
2675 linux_get_hwcap2 (struct target_ops *target)
2677 CORE_ADDR field;
2678 if (target_auxv_search (target, AT_HWCAP2, &field) != 1)
2679 return 0;
2680 return field;
2683 /* Display whether the gcore command is using the
2684 /proc/PID/coredump_filter file. */
2686 static void
2687 show_use_coredump_filter (struct ui_file *file, int from_tty,
2688 struct cmd_list_element *c, const char *value)
2690 gdb_printf (file, _("Use of /proc/PID/coredump_filter file to generate"
2691 " corefiles is %s.\n"), value);
2694 /* Display whether the gcore command is dumping mappings marked with
2695 the VM_DONTDUMP flag. */
2697 static void
2698 show_dump_excluded_mappings (struct ui_file *file, int from_tty,
2699 struct cmd_list_element *c, const char *value)
2701 gdb_printf (file, _("Dumping of mappings marked with the VM_DONTDUMP"
2702 " flag is %s.\n"), value);
2705 /* To be called from the various GDB_OSABI_LINUX handlers for the
2706 various GNU/Linux architectures and machine types.
2708 NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
2709 displaced stepping is not supported. */
2711 void
2712 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
2713 int num_disp_step_buffers)
2715 if (num_disp_step_buffers > 0)
2717 linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (gdbarch);
2718 gdbarch_data->num_disp_step_buffers = num_disp_step_buffers;
2720 set_gdbarch_displaced_step_prepare (gdbarch,
2721 linux_displaced_step_prepare);
2722 set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
2723 set_gdbarch_displaced_step_copy_insn_closure_by_addr
2724 (gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
2725 set_gdbarch_displaced_step_restore_all_in_ptid
2726 (gdbarch, linux_displaced_step_restore_all_in_ptid);
2729 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
2730 set_gdbarch_info_proc (gdbarch, linux_info_proc);
2731 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
2732 set_gdbarch_core_xfer_siginfo (gdbarch, linux_core_xfer_siginfo);
2733 set_gdbarch_read_core_file_mappings (gdbarch, linux_read_core_file_mappings);
2734 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
2735 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes);
2736 set_gdbarch_has_shared_address_space (gdbarch,
2737 linux_has_shared_address_space);
2738 set_gdbarch_gdb_signal_from_target (gdbarch,
2739 linux_gdb_signal_from_target);
2740 set_gdbarch_gdb_signal_to_target (gdbarch,
2741 linux_gdb_signal_to_target);
2742 set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range);
2743 set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap);
2744 set_gdbarch_infcall_munmap (gdbarch, linux_infcall_munmap);
2745 set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
2748 void _initialize_linux_tdep ();
2749 void
2750 _initialize_linux_tdep ()
2752 /* Observers used to invalidate the cache when needed. */
2753 gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf,
2754 "linux-tdep");
2755 gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf,
2756 "linux-tdep");
2757 gdb::observers::inferior_execd.attach (invalidate_linux_cache_inf,
2758 "linux-tdep");
2760 add_setshow_boolean_cmd ("use-coredump-filter", class_files,
2761 &use_coredump_filter, _("\
2762 Set whether gcore should consider /proc/PID/coredump_filter."),
2763 _("\
2764 Show whether gcore should consider /proc/PID/coredump_filter."),
2765 _("\
2766 Use this command to set whether gcore should consider the contents\n\
2767 of /proc/PID/coredump_filter when generating the corefile. For more information\n\
2768 about this file, refer to the manpage of core(5)."),
2769 NULL, show_use_coredump_filter,
2770 &setlist, &showlist);
2772 add_setshow_boolean_cmd ("dump-excluded-mappings", class_files,
2773 &dump_excluded_mappings, _("\
2774 Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2775 _("\
2776 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2777 _("\
2778 Use this command to set whether gcore should dump mappings marked with the\n\
2779 VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
2780 more information about this file, refer to the manpage of proc(5) and core(5)."),
2781 NULL, show_dump_excluded_mappings,
2782 &setlist, &showlist);
2785 /* Fetch (and possibly build) an appropriate `link_map_offsets' for
2786 ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
2788 link_map_offsets *
2789 linux_ilp32_fetch_link_map_offsets ()
2791 static link_map_offsets lmo;
2792 static link_map_offsets *lmp = nullptr;
2794 if (lmp == nullptr)
2796 lmp = &lmo;
2798 lmo.r_version_offset = 0;
2799 lmo.r_version_size = 4;
2800 lmo.r_map_offset = 4;
2801 lmo.r_brk_offset = 8;
2802 lmo.r_ldsomap_offset = -1;
2804 /* Everything we need is in the first 20 bytes. */
2805 lmo.link_map_size = 20;
2806 lmo.l_addr_offset = 0;
2807 lmo.l_name_offset = 4;
2808 lmo.l_ld_offset = 8;
2809 lmo.l_next_offset = 12;
2810 lmo.l_prev_offset = 16;
2813 return lmp;
2816 link_map_offsets *
2817 linux_lp64_fetch_link_map_offsets ()
2819 static link_map_offsets lmo;
2820 static link_map_offsets *lmp = nullptr;
2822 if (lmp == nullptr)
2824 lmp = &lmo;
2826 lmo.r_version_offset = 0;
2827 lmo.r_version_size = 4;
2828 lmo.r_map_offset = 8;
2829 lmo.r_brk_offset = 16;
2830 lmo.r_ldsomap_offset = -1;
2832 /* Everything we need is in the first 40 bytes. */
2833 lmo.link_map_size = 40;
2834 lmo.l_addr_offset = 0;
2835 lmo.l_name_offset = 8;
2836 lmo.l_ld_offset = 16;
2837 lmo.l_next_offset = 24;
2838 lmo.l_prev_offset = 32;
2841 return lmp;