gdb/linux-tdep: move "Perms" column right
[binutils-gdb.git] / gdb / linux-tdep.c
1 /* Target-dependent code for GNU/Linux, architecture independent.
2
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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
46 #include <ctype.h>
47
48 /* This enum represents the values that the user can choose when
49 informing the Linux kernel about which memory mappings will be
50 dumped in a corefile. They are described in the file
51 Documentation/filesystems/proc.txt, inside the Linux kernel
52 tree. */
53
54 enum filter_flag
55 {
56 COREFILTER_ANON_PRIVATE = 1 << 0,
57 COREFILTER_ANON_SHARED = 1 << 1,
58 COREFILTER_MAPPED_PRIVATE = 1 << 2,
59 COREFILTER_MAPPED_SHARED = 1 << 3,
60 COREFILTER_ELF_HEADERS = 1 << 4,
61 COREFILTER_HUGETLB_PRIVATE = 1 << 5,
62 COREFILTER_HUGETLB_SHARED = 1 << 6,
63 };
64 DEF_ENUM_FLAGS_TYPE (enum filter_flag, filter_flags);
65
66 /* This struct is used to map flags found in the "VmFlags:" field (in
67 the /proc/<PID>/smaps file). */
68
69 struct smaps_vmflags
70 {
71 /* Zero if this structure has not been initialized yet. It
72 probably means that the Linux kernel being used does not emit
73 the "VmFlags:" field on "/proc/PID/smaps". */
74
75 unsigned int initialized_p : 1;
76
77 /* Memory mapped I/O area (VM_IO, "io"). */
78
79 unsigned int io_page : 1;
80
81 /* Area uses huge TLB pages (VM_HUGETLB, "ht"). */
82
83 unsigned int uses_huge_tlb : 1;
84
85 /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd"). */
86
87 unsigned int exclude_coredump : 1;
88
89 /* Is this a MAP_SHARED mapping (VM_SHARED, "sh"). */
90
91 unsigned int shared_mapping : 1;
92
93 /* Memory map has memory tagging enabled. */
94
95 unsigned int memory_tagging : 1;
96 };
97
98 /* Data structure that holds the information contained in the
99 /proc/<pid>/smaps file. */
100
101 struct smaps_data
102 {
103 ULONGEST start_address;
104 ULONGEST end_address;
105 std::string filename;
106 struct smaps_vmflags vmflags;
107 bool read;
108 bool write;
109 bool exec;
110 bool priv;
111 bool has_anonymous;
112 bool mapping_anon_p;
113 bool mapping_file_p;
114
115 ULONGEST inode;
116 ULONGEST offset;
117 };
118
119 /* Whether to take the /proc/PID/coredump_filter into account when
120 generating a corefile. */
121
122 static bool use_coredump_filter = true;
123
124 /* Whether the value of smaps_vmflags->exclude_coredump should be
125 ignored, including mappings marked with the VM_DONTDUMP flag in
126 the dump. */
127 static bool dump_excluded_mappings = false;
128
129 /* This enum represents the signals' numbers on a generic architecture
130 running the Linux kernel. The definition of "generic" comes from
131 the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
132 tree, which is the "de facto" implementation of signal numbers to
133 be used by new architecture ports.
134
135 For those architectures which have differences between the generic
136 standard (e.g., Alpha), we define the different signals (and *only*
137 those) in the specific target-dependent file (e.g.,
138 alpha-linux-tdep.c, for Alpha). Please refer to the architecture's
139 tdep file for more information.
140
141 ARM deserves a special mention here. On the file
142 <arch/arm/include/uapi/asm/signal.h>, it defines only one different
143 (and ARM-only) signal, which is SIGSWI, with the same number as
144 SIGRTMIN. This signal is used only for a very specific target,
145 called ArthurOS (from RISCOS). Therefore, we do not handle it on
146 the ARM-tdep file, and we can safely use the generic signal handler
147 here for ARM targets.
148
149 As stated above, this enum is derived from
150 <include/uapi/asm-generic/signal.h>, from the Linux kernel
151 tree. */
152
153 enum
154 {
155 LINUX_SIGHUP = 1,
156 LINUX_SIGINT = 2,
157 LINUX_SIGQUIT = 3,
158 LINUX_SIGILL = 4,
159 LINUX_SIGTRAP = 5,
160 LINUX_SIGABRT = 6,
161 LINUX_SIGIOT = 6,
162 LINUX_SIGBUS = 7,
163 LINUX_SIGFPE = 8,
164 LINUX_SIGKILL = 9,
165 LINUX_SIGUSR1 = 10,
166 LINUX_SIGSEGV = 11,
167 LINUX_SIGUSR2 = 12,
168 LINUX_SIGPIPE = 13,
169 LINUX_SIGALRM = 14,
170 LINUX_SIGTERM = 15,
171 LINUX_SIGSTKFLT = 16,
172 LINUX_SIGCHLD = 17,
173 LINUX_SIGCONT = 18,
174 LINUX_SIGSTOP = 19,
175 LINUX_SIGTSTP = 20,
176 LINUX_SIGTTIN = 21,
177 LINUX_SIGTTOU = 22,
178 LINUX_SIGURG = 23,
179 LINUX_SIGXCPU = 24,
180 LINUX_SIGXFSZ = 25,
181 LINUX_SIGVTALRM = 26,
182 LINUX_SIGPROF = 27,
183 LINUX_SIGWINCH = 28,
184 LINUX_SIGIO = 29,
185 LINUX_SIGPOLL = LINUX_SIGIO,
186 LINUX_SIGPWR = 30,
187 LINUX_SIGSYS = 31,
188 LINUX_SIGUNUSED = 31,
189
190 LINUX_SIGRTMIN = 32,
191 LINUX_SIGRTMAX = 64,
192 };
193
194 static struct gdbarch_data *linux_gdbarch_data_handle;
195
196 struct linux_gdbarch_data
197 {
198 struct type *siginfo_type;
199 int num_disp_step_buffers;
200 };
201
202 static void *
203 init_linux_gdbarch_data (struct obstack *obstack)
204 {
205 return obstack_zalloc<linux_gdbarch_data> (obstack);
206 }
207
208 static struct linux_gdbarch_data *
209 get_linux_gdbarch_data (struct gdbarch *gdbarch)
210 {
211 return ((struct linux_gdbarch_data *)
212 gdbarch_data (gdbarch, linux_gdbarch_data_handle));
213 }
214
215 /* Linux-specific cached data. This is used by GDB for caching
216 purposes for each inferior. This helps reduce the overhead of
217 transfering data from a remote target to the local host. */
218 struct linux_info
219 {
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 {};
226
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;
231
232 /* Inferior's displaced step buffers. */
233 gdb::optional<displaced_step_buffers> disp_step_bufs;
234 };
235
236 /* Per-inferior data key. */
237 static const struct inferior_key<linux_info> linux_inferior_data;
238
239 /* Frees whatever allocated space there is to be freed and sets INF's
240 linux cache data pointer to NULL. */
241
242 static void
243 invalidate_linux_cache_inf (struct inferior *inf)
244 {
245 linux_inferior_data.clear (inf);
246 }
247
248 /* Fetch the linux cache info for INF. This function always returns a
249 valid INFO pointer. */
250
251 static struct linux_info *
252 get_linux_inferior_data (inferior *inf)
253 {
254 linux_info *info = linux_inferior_data.get (inf);
255
256 if (info == nullptr)
257 info = linux_inferior_data.emplace (inf);
258
259 return info;
260 }
261
262 /* See linux-tdep.h. */
263
264 struct type *
265 linux_get_siginfo_type_with_fields (struct gdbarch *gdbarch,
266 linux_siginfo_extra_fields extra_fields)
267 {
268 struct linux_gdbarch_data *linux_gdbarch_data;
269 struct type *int_type, *uint_type, *long_type, *void_ptr_type, *short_type;
270 struct type *uid_type, *pid_type;
271 struct type *sigval_type, *clock_type;
272 struct type *siginfo_type, *sifields_type;
273 struct type *type;
274
275 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
276 if (linux_gdbarch_data->siginfo_type != NULL)
277 return linux_gdbarch_data->siginfo_type;
278
279 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
280 0, "int");
281 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
282 1, "unsigned int");
283 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
284 0, "long");
285 short_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
286 0, "short");
287 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
288
289 /* sival_t */
290 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
291 sigval_type->set_name (xstrdup ("sigval_t"));
292 append_composite_type_field (sigval_type, "sival_int", int_type);
293 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
294
295 /* __pid_t */
296 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
297 TYPE_LENGTH (int_type) * TARGET_CHAR_BIT, "__pid_t");
298 TYPE_TARGET_TYPE (pid_type) = int_type;
299 pid_type->set_target_is_stub (true);
300
301 /* __uid_t */
302 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
303 TYPE_LENGTH (uint_type) * TARGET_CHAR_BIT, "__uid_t");
304 TYPE_TARGET_TYPE (uid_type) = uint_type;
305 uid_type->set_target_is_stub (true);
306
307 /* __clock_t */
308 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
309 TYPE_LENGTH (long_type) * TARGET_CHAR_BIT,
310 "__clock_t");
311 TYPE_TARGET_TYPE (clock_type) = long_type;
312 clock_type->set_target_is_stub (true);
313
314 /* _sifields */
315 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
316
317 {
318 const int si_max_size = 128;
319 int si_pad_size;
320 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
321
322 /* _pad */
323 if (gdbarch_ptr_bit (gdbarch) == 64)
324 si_pad_size = (si_max_size / size_of_int) - 4;
325 else
326 si_pad_size = (si_max_size / size_of_int) - 3;
327 append_composite_type_field (sifields_type, "_pad",
328 init_vector_type (int_type, si_pad_size));
329 }
330
331 /* _kill */
332 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
333 append_composite_type_field (type, "si_pid", pid_type);
334 append_composite_type_field (type, "si_uid", uid_type);
335 append_composite_type_field (sifields_type, "_kill", type);
336
337 /* _timer */
338 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
339 append_composite_type_field (type, "si_tid", int_type);
340 append_composite_type_field (type, "si_overrun", int_type);
341 append_composite_type_field (type, "si_sigval", sigval_type);
342 append_composite_type_field (sifields_type, "_timer", type);
343
344 /* _rt */
345 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
346 append_composite_type_field (type, "si_pid", pid_type);
347 append_composite_type_field (type, "si_uid", uid_type);
348 append_composite_type_field (type, "si_sigval", sigval_type);
349 append_composite_type_field (sifields_type, "_rt", type);
350
351 /* _sigchld */
352 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
353 append_composite_type_field (type, "si_pid", pid_type);
354 append_composite_type_field (type, "si_uid", uid_type);
355 append_composite_type_field (type, "si_status", int_type);
356 append_composite_type_field (type, "si_utime", clock_type);
357 append_composite_type_field (type, "si_stime", clock_type);
358 append_composite_type_field (sifields_type, "_sigchld", type);
359
360 /* _sigfault */
361 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
362 append_composite_type_field (type, "si_addr", void_ptr_type);
363
364 /* Additional bound fields for _sigfault in case they were requested. */
365 if ((extra_fields & LINUX_SIGINFO_FIELD_ADDR_BND) != 0)
366 {
367 struct type *sigfault_bnd_fields;
368
369 append_composite_type_field (type, "_addr_lsb", short_type);
370 sigfault_bnd_fields = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
371 append_composite_type_field (sigfault_bnd_fields, "_lower", void_ptr_type);
372 append_composite_type_field (sigfault_bnd_fields, "_upper", void_ptr_type);
373 append_composite_type_field (type, "_addr_bnd", sigfault_bnd_fields);
374 }
375 append_composite_type_field (sifields_type, "_sigfault", type);
376
377 /* _sigpoll */
378 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
379 append_composite_type_field (type, "si_band", long_type);
380 append_composite_type_field (type, "si_fd", int_type);
381 append_composite_type_field (sifields_type, "_sigpoll", type);
382
383 /* _sigsys */
384 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
385 append_composite_type_field (type, "_call_addr", void_ptr_type);
386 append_composite_type_field (type, "_syscall", int_type);
387 append_composite_type_field (type, "_arch", uint_type);
388 append_composite_type_field (sifields_type, "_sigsys", type);
389
390 /* struct siginfo */
391 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
392 siginfo_type->set_name (xstrdup ("siginfo"));
393 append_composite_type_field (siginfo_type, "si_signo", int_type);
394 append_composite_type_field (siginfo_type, "si_errno", int_type);
395 append_composite_type_field (siginfo_type, "si_code", int_type);
396 append_composite_type_field_aligned (siginfo_type,
397 "_sifields", sifields_type,
398 TYPE_LENGTH (long_type));
399
400 linux_gdbarch_data->siginfo_type = siginfo_type;
401
402 return siginfo_type;
403 }
404
405 /* This function is suitable for architectures that don't
406 extend/override the standard siginfo structure. */
407
408 static struct type *
409 linux_get_siginfo_type (struct gdbarch *gdbarch)
410 {
411 return linux_get_siginfo_type_with_fields (gdbarch, 0);
412 }
413
414 /* Return true if the target is running on uClinux instead of normal
415 Linux kernel. */
416
417 int
418 linux_is_uclinux (void)
419 {
420 CORE_ADDR dummy;
421 target_ops *target = current_inferior ()->top_target ();
422
423 return (target_auxv_search (target, AT_NULL, &dummy) > 0
424 && target_auxv_search (target, AT_PAGESZ, &dummy) == 0);
425 }
426
427 static int
428 linux_has_shared_address_space (struct gdbarch *gdbarch)
429 {
430 return linux_is_uclinux ();
431 }
432
433 /* This is how we want PTIDs from core files to be printed. */
434
435 static std::string
436 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
437 {
438 if (ptid.lwp () != 0)
439 return string_printf ("LWP %ld", ptid.lwp ());
440
441 return normal_pid_to_str (ptid);
442 }
443
444 /* Data from one mapping from /proc/PID/maps. */
445
446 struct mapping
447 {
448 ULONGEST addr;
449 ULONGEST endaddr;
450 gdb::string_view permissions;
451 ULONGEST offset;
452 gdb::string_view device;
453 ULONGEST inode;
454
455 /* This field is guaranteed to be NULL-terminated, hence it is not a
456 gdb::string_view. */
457 const char *filename;
458 };
459
460 /* Service function for corefiles and info proc. */
461
462 static mapping
463 read_mapping (const char *line)
464 {
465 struct mapping mapping;
466 const char *p = line;
467
468 mapping.addr = strtoulst (p, &p, 16);
469 if (*p == '-')
470 p++;
471 mapping.endaddr = strtoulst (p, &p, 16);
472
473 p = skip_spaces (p);
474 const char *permissions_start = p;
475 while (*p && !isspace (*p))
476 p++;
477 mapping.permissions = {permissions_start, (size_t) (p - permissions_start)};
478
479 mapping.offset = strtoulst (p, &p, 16);
480
481 p = skip_spaces (p);
482 const char *device_start = p;
483 while (*p && !isspace (*p))
484 p++;
485 mapping.device = {device_start, (size_t) (p - device_start)};
486
487 mapping.inode = strtoulst (p, &p, 10);
488
489 p = skip_spaces (p);
490 mapping.filename = p;
491
492 return mapping;
493 }
494
495 /* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
496
497 This function was based on the documentation found on
498 <Documentation/filesystems/proc.txt>, on the Linux kernel.
499
500 Linux kernels before commit
501 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
502 field on smaps. */
503
504 static void
505 decode_vmflags (char *p, struct smaps_vmflags *v)
506 {
507 char *saveptr = NULL;
508 const char *s;
509
510 v->initialized_p = 1;
511 p = skip_to_space (p);
512 p = skip_spaces (p);
513
514 for (s = strtok_r (p, " ", &saveptr);
515 s != NULL;
516 s = strtok_r (NULL, " ", &saveptr))
517 {
518 if (strcmp (s, "io") == 0)
519 v->io_page = 1;
520 else if (strcmp (s, "ht") == 0)
521 v->uses_huge_tlb = 1;
522 else if (strcmp (s, "dd") == 0)
523 v->exclude_coredump = 1;
524 else if (strcmp (s, "sh") == 0)
525 v->shared_mapping = 1;
526 else if (strcmp (s, "mt") == 0)
527 v->memory_tagging = 1;
528 }
529 }
530
531 /* Regexes used by mapping_is_anonymous_p. Put in a structure because
532 they're initialized lazily. */
533
534 struct mapping_regexes
535 {
536 /* Matches "/dev/zero" filenames (with or without the "(deleted)"
537 string in the end). We know for sure, based on the Linux kernel
538 code, that memory mappings whose associated filename is
539 "/dev/zero" are guaranteed to be MAP_ANONYMOUS. */
540 compiled_regex dev_zero
541 {"^/dev/zero\\( (deleted)\\)\\?$", REG_NOSUB,
542 _("Could not compile regex to match /dev/zero filename")};
543
544 /* Matches "/SYSV%08x" filenames (with or without the "(deleted)"
545 string in the end). These filenames refer to shared memory
546 (shmem), and memory mappings associated with them are
547 MAP_ANONYMOUS as well. */
548 compiled_regex shmem_file
549 {"^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$", REG_NOSUB,
550 _("Could not compile regex to match shmem filenames")};
551
552 /* A heuristic we use to try to mimic the Linux kernel's 'n_link ==
553 0' code, which is responsible to decide if it is dealing with a
554 'MAP_SHARED | MAP_ANONYMOUS' mapping. In other words, if
555 FILE_DELETED matches, it does not necessarily mean that we are
556 dealing with an anonymous shared mapping. However, there is no
557 easy way to detect this currently, so this is the best
558 approximation we have.
559
560 As a result, GDB will dump readonly pages of deleted executables
561 when using the default value of coredump_filter (0x33), while the
562 Linux kernel will not dump those pages. But we can live with
563 that. */
564 compiled_regex file_deleted
565 {" (deleted)$", REG_NOSUB,
566 _("Could not compile regex to match '<file> (deleted)'")};
567 };
568
569 /* Return 1 if the memory mapping is anonymous, 0 otherwise.
570
571 FILENAME is the name of the file present in the first line of the
572 memory mapping, in the "/proc/PID/smaps" output. For example, if
573 the first line is:
574
575 7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770 /path/to/file
576
577 Then FILENAME will be "/path/to/file". */
578
579 static int
580 mapping_is_anonymous_p (const char *filename)
581 {
582 static gdb::optional<mapping_regexes> regexes;
583 static int init_regex_p = 0;
584
585 if (!init_regex_p)
586 {
587 /* Let's be pessimistic and assume there will be an error while
588 compiling the regex'es. */
589 init_regex_p = -1;
590
591 regexes.emplace ();
592
593 /* If we reached this point, then everything succeeded. */
594 init_regex_p = 1;
595 }
596
597 if (init_regex_p == -1)
598 {
599 const char deleted[] = " (deleted)";
600 size_t del_len = sizeof (deleted) - 1;
601 size_t filename_len = strlen (filename);
602
603 /* There was an error while compiling the regex'es above. In
604 order to try to give some reliable information to the caller,
605 we just try to find the string " (deleted)" in the filename.
606 If we managed to find it, then we assume the mapping is
607 anonymous. */
608 return (filename_len >= del_len
609 && strcmp (filename + filename_len - del_len, deleted) == 0);
610 }
611
612 if (*filename == '\0'
613 || regexes->dev_zero.exec (filename, 0, NULL, 0) == 0
614 || regexes->shmem_file.exec (filename, 0, NULL, 0) == 0
615 || regexes->file_deleted.exec (filename, 0, NULL, 0) == 0)
616 return 1;
617
618 return 0;
619 }
620
621 /* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
622 MAYBE_PRIVATE_P, MAPPING_ANONYMOUS_P, ADDR and OFFSET) should not
623 be dumped, or greater than 0 if it should.
624
625 In a nutshell, this is the logic that we follow in order to decide
626 if a mapping should be dumped or not.
627
628 - If the mapping is associated to a file whose name ends with
629 " (deleted)", or if the file is "/dev/zero", or if it is
630 "/SYSV%08x" (shared memory), or if there is no file associated
631 with it, or if the AnonHugePages: or the Anonymous: fields in the
632 /proc/PID/smaps have contents, then GDB considers this mapping to
633 be anonymous. Otherwise, GDB considers this mapping to be a
634 file-backed mapping (because there will be a file associated with
635 it).
636
637 It is worth mentioning that, from all those checks described
638 above, the most fragile is the one to see if the file name ends
639 with " (deleted)". This does not necessarily mean that the
640 mapping is anonymous, because the deleted file associated with
641 the mapping may have been a hard link to another file, for
642 example. The Linux kernel checks to see if "i_nlink == 0", but
643 GDB cannot easily (and normally) do this check (iff running as
644 root, it could find the mapping in /proc/PID/map_files/ and
645 determine whether there still are other hard links to the
646 inode/file). Therefore, we made a compromise here, and we assume
647 that if the file name ends with " (deleted)", then the mapping is
648 indeed anonymous. FWIW, this is something the Linux kernel could
649 do better: expose this information in a more direct way.
650
651 - If we see the flag "sh" in the "VmFlags:" field (in
652 /proc/PID/smaps), then certainly the memory mapping is shared
653 (VM_SHARED). If we have access to the VmFlags, and we don't see
654 the "sh" there, then certainly the mapping is private. However,
655 Linux kernels before commit
656 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
657 "VmFlags:" field; in that case, we use another heuristic: if we
658 see 'p' in the permission flags, then we assume that the mapping
659 is private, even though the presence of the 's' flag there would
660 mean VM_MAYSHARE, which means the mapping could still be private.
661 This should work OK enough, however.
662
663 - Even if, at the end, we decided that we should not dump the
664 mapping, we still have to check if it is something like an ELF
665 header (of a DSO or an executable, for example). If it is, and
666 if the user is interested in dump it, then we should dump it. */
667
668 static int
669 dump_mapping_p (filter_flags filterflags, const struct smaps_vmflags *v,
670 int maybe_private_p, int mapping_anon_p, int mapping_file_p,
671 const char *filename, ULONGEST addr, ULONGEST offset)
672 {
673 /* Initially, we trust in what we received from our caller. This
674 value may not be very precise (i.e., it was probably gathered
675 from the permission line in the /proc/PID/smaps list, which
676 actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
677 what we have until we take a look at the "VmFlags:" field
678 (assuming that the version of the Linux kernel being used
679 supports it, of course). */
680 int private_p = maybe_private_p;
681 int dump_p;
682
683 /* We always dump vDSO and vsyscall mappings, because it's likely that
684 there'll be no file to read the contents from at core load time.
685 The kernel does the same. */
686 if (strcmp ("[vdso]", filename) == 0
687 || strcmp ("[vsyscall]", filename) == 0)
688 return 1;
689
690 if (v->initialized_p)
691 {
692 /* We never dump I/O mappings. */
693 if (v->io_page)
694 return 0;
695
696 /* Check if we should exclude this mapping. */
697 if (!dump_excluded_mappings && v->exclude_coredump)
698 return 0;
699
700 /* Update our notion of whether this mapping is shared or
701 private based on a trustworthy value. */
702 private_p = !v->shared_mapping;
703
704 /* HugeTLB checking. */
705 if (v->uses_huge_tlb)
706 {
707 if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
708 || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
709 return 1;
710
711 return 0;
712 }
713 }
714
715 if (private_p)
716 {
717 if (mapping_anon_p && mapping_file_p)
718 {
719 /* This is a special situation. It can happen when we see a
720 mapping that is file-backed, but that contains anonymous
721 pages. */
722 dump_p = ((filterflags & COREFILTER_ANON_PRIVATE) != 0
723 || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
724 }
725 else if (mapping_anon_p)
726 dump_p = (filterflags & COREFILTER_ANON_PRIVATE) != 0;
727 else
728 dump_p = (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
729 }
730 else
731 {
732 if (mapping_anon_p && mapping_file_p)
733 {
734 /* This is a special situation. It can happen when we see a
735 mapping that is file-backed, but that contains anonymous
736 pages. */
737 dump_p = ((filterflags & COREFILTER_ANON_SHARED) != 0
738 || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
739 }
740 else if (mapping_anon_p)
741 dump_p = (filterflags & COREFILTER_ANON_SHARED) != 0;
742 else
743 dump_p = (filterflags & COREFILTER_MAPPED_SHARED) != 0;
744 }
745
746 /* Even if we decided that we shouldn't dump this mapping, we still
747 have to check whether (a) the user wants us to dump mappings
748 containing an ELF header, and (b) the mapping in question
749 contains an ELF header. If (a) and (b) are true, then we should
750 dump this mapping.
751
752 A mapping contains an ELF header if it is a private mapping, its
753 offset is zero, and its first word is ELFMAG. */
754 if (!dump_p && private_p && offset == 0
755 && (filterflags & COREFILTER_ELF_HEADERS) != 0)
756 {
757 /* Useful define specifying the size of the ELF magical
758 header. */
759 #ifndef SELFMAG
760 #define SELFMAG 4
761 #endif
762
763 /* Let's check if we have an ELF header. */
764 gdb_byte h[SELFMAG];
765 if (target_read_memory (addr, h, SELFMAG) == 0)
766 {
767 /* The EI_MAG* and ELFMAG* constants come from
768 <elf/common.h>. */
769 if (h[EI_MAG0] == ELFMAG0 && h[EI_MAG1] == ELFMAG1
770 && h[EI_MAG2] == ELFMAG2 && h[EI_MAG3] == ELFMAG3)
771 {
772 /* This mapping contains an ELF header, so we
773 should dump it. */
774 dump_p = 1;
775 }
776 }
777 }
778
779 return dump_p;
780 }
781
782 /* As above, but return true only when we should dump the NT_FILE
783 entry. */
784
785 static int
786 dump_note_entry_p (filter_flags filterflags, const struct smaps_vmflags *v,
787 int maybe_private_p, int mapping_anon_p, int mapping_file_p,
788 const char *filename, ULONGEST addr, ULONGEST offset)
789 {
790 /* vDSO and vsyscall mappings will end up in the core file. Don't
791 put them in the NT_FILE note. */
792 if (strcmp ("[vdso]", filename) == 0
793 || strcmp ("[vsyscall]", filename) == 0)
794 return 0;
795
796 /* Otherwise, any other file-based mapping should be placed in the
797 note. */
798 return 1;
799 }
800
801 /* Implement the "info proc" command. */
802
803 static void
804 linux_info_proc (struct gdbarch *gdbarch, const char *args,
805 enum info_proc_what what)
806 {
807 /* A long is used for pid instead of an int to avoid a loss of precision
808 compiler warning from the output of strtoul. */
809 long pid;
810 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
811 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
812 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
813 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
814 int status_f = (what == IP_STATUS || what == IP_ALL);
815 int stat_f = (what == IP_STAT || what == IP_ALL);
816 char filename[100];
817 int target_errno;
818
819 if (args && isdigit (args[0]))
820 {
821 char *tem;
822
823 pid = strtoul (args, &tem, 10);
824 args = tem;
825 }
826 else
827 {
828 if (!target_has_execution ())
829 error (_("No current process: you must name one."));
830 if (current_inferior ()->fake_pid_p)
831 error (_("Can't determine the current process's PID: you must name one."));
832
833 pid = current_inferior ()->pid;
834 }
835
836 args = skip_spaces (args);
837 if (args && args[0])
838 error (_("Too many parameters: %s"), args);
839
840 printf_filtered (_("process %ld\n"), pid);
841 if (cmdline_f)
842 {
843 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
844 gdb_byte *buffer;
845 ssize_t len = target_fileio_read_alloc (NULL, filename, &buffer);
846
847 if (len > 0)
848 {
849 gdb::unique_xmalloc_ptr<char> cmdline ((char *) buffer);
850 ssize_t pos;
851
852 for (pos = 0; pos < len - 1; pos++)
853 {
854 if (buffer[pos] == '\0')
855 buffer[pos] = ' ';
856 }
857 buffer[len - 1] = '\0';
858 printf_filtered ("cmdline = '%s'\n", buffer);
859 }
860 else
861 warning (_("unable to open /proc file '%s'"), filename);
862 }
863 if (cwd_f)
864 {
865 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
866 gdb::optional<std::string> contents
867 = target_fileio_readlink (NULL, filename, &target_errno);
868 if (contents.has_value ())
869 printf_filtered ("cwd = '%s'\n", contents->c_str ());
870 else
871 warning (_("unable to read link '%s'"), filename);
872 }
873 if (exe_f)
874 {
875 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
876 gdb::optional<std::string> contents
877 = target_fileio_readlink (NULL, filename, &target_errno);
878 if (contents.has_value ())
879 printf_filtered ("exe = '%s'\n", contents->c_str ());
880 else
881 warning (_("unable to read link '%s'"), filename);
882 }
883 if (mappings_f)
884 {
885 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
886 gdb::unique_xmalloc_ptr<char> map
887 = target_fileio_read_stralloc (NULL, filename);
888 if (map != NULL)
889 {
890 char *line;
891
892 printf_filtered (_("Mapped address spaces:\n\n"));
893 if (gdbarch_addr_bit (gdbarch) == 32)
894 {
895 printf_filtered ("\t%10s %10s %10s %10s %s %s\n",
896 "Start Addr", " End Addr", " Size",
897 " Offset", "Perms ", "objfile");
898 }
899 else
900 {
901 printf_filtered (" %18s %18s %10s %10s %s %s\n",
902 "Start Addr", " End Addr", " Size",
903 " Offset", "Perms ", "objfile");
904 }
905
906 char *saveptr;
907 for (line = strtok_r (map.get (), "\n", &saveptr);
908 line;
909 line = strtok_r (NULL, "\n", &saveptr))
910 {
911 struct mapping m = read_mapping (line);
912
913 if (gdbarch_addr_bit (gdbarch) == 32)
914 {
915 printf_filtered ("\t%10s %10s %10s %10s %-5.*s %s\n",
916 paddress (gdbarch, m.addr),
917 paddress (gdbarch, m.endaddr),
918 hex_string (m.endaddr - m.addr),
919 hex_string (m.offset),
920 (int) m.permissions.size (),
921 m.permissions.data (),
922 m.filename);
923 }
924 else
925 {
926 printf_filtered (" %18s %18s %10s %10s %-5.*s %s\n",
927 paddress (gdbarch, m.addr),
928 paddress (gdbarch, m.endaddr),
929 hex_string (m.endaddr - m.addr),
930 hex_string (m.offset),
931 (int) m.permissions.size (),
932 m.permissions.data (),
933 m.filename);
934 }
935 }
936 }
937 else
938 warning (_("unable to open /proc file '%s'"), filename);
939 }
940 if (status_f)
941 {
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 puts_filtered (status.get ());
947 else
948 warning (_("unable to open /proc file '%s'"), filename);
949 }
950 if (stat_f)
951 {
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)
956 {
957 const char *p = statstr.get ();
958
959 printf_filtered (_("Process: %s\n"),
960 pulongest (strtoulst (p, &p, 10)));
961
962 p = skip_spaces (p);
963 if (*p == '(')
964 {
965 /* ps command also relies on no trailing fields
966 ever contain ')'. */
967 const char *ep = strrchr (p, ')');
968 if (ep != NULL)
969 {
970 printf_filtered ("Exec file: %.*s\n",
971 (int) (ep - p - 1), p + 1);
972 p = ep + 1;
973 }
974 }
975
976 p = skip_spaces (p);
977 if (*p)
978 printf_filtered (_("State: %c\n"), *p++);
979
980 if (*p)
981 printf_filtered (_("Parent process: %s\n"),
982 pulongest (strtoulst (p, &p, 10)));
983 if (*p)
984 printf_filtered (_("Process group: %s\n"),
985 pulongest (strtoulst (p, &p, 10)));
986 if (*p)
987 printf_filtered (_("Session id: %s\n"),
988 pulongest (strtoulst (p, &p, 10)));
989 if (*p)
990 printf_filtered (_("TTY: %s\n"),
991 pulongest (strtoulst (p, &p, 10)));
992 if (*p)
993 printf_filtered (_("TTY owner process group: %s\n"),
994 pulongest (strtoulst (p, &p, 10)));
995
996 if (*p)
997 printf_filtered (_("Flags: %s\n"),
998 hex_string (strtoulst (p, &p, 10)));
999 if (*p)
1000 printf_filtered (_("Minor faults (no memory page): %s\n"),
1001 pulongest (strtoulst (p, &p, 10)));
1002 if (*p)
1003 printf_filtered (_("Minor faults, children: %s\n"),
1004 pulongest (strtoulst (p, &p, 10)));
1005 if (*p)
1006 printf_filtered (_("Major faults (memory page faults): %s\n"),
1007 pulongest (strtoulst (p, &p, 10)));
1008 if (*p)
1009 printf_filtered (_("Major faults, children: %s\n"),
1010 pulongest (strtoulst (p, &p, 10)));
1011 if (*p)
1012 printf_filtered (_("utime: %s\n"),
1013 pulongest (strtoulst (p, &p, 10)));
1014 if (*p)
1015 printf_filtered (_("stime: %s\n"),
1016 pulongest (strtoulst (p, &p, 10)));
1017 if (*p)
1018 printf_filtered (_("utime, children: %s\n"),
1019 pulongest (strtoulst (p, &p, 10)));
1020 if (*p)
1021 printf_filtered (_("stime, children: %s\n"),
1022 pulongest (strtoulst (p, &p, 10)));
1023 if (*p)
1024 printf_filtered (_("jiffies remaining in current "
1025 "time slice: %s\n"),
1026 pulongest (strtoulst (p, &p, 10)));
1027 if (*p)
1028 printf_filtered (_("'nice' value: %s\n"),
1029 pulongest (strtoulst (p, &p, 10)));
1030 if (*p)
1031 printf_filtered (_("jiffies until next timeout: %s\n"),
1032 pulongest (strtoulst (p, &p, 10)));
1033 if (*p)
1034 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
1035 pulongest (strtoulst (p, &p, 10)));
1036 if (*p)
1037 printf_filtered (_("start time (jiffies since "
1038 "system boot): %s\n"),
1039 pulongest (strtoulst (p, &p, 10)));
1040 if (*p)
1041 printf_filtered (_("Virtual memory size: %s\n"),
1042 pulongest (strtoulst (p, &p, 10)));
1043 if (*p)
1044 printf_filtered (_("Resident set size: %s\n"),
1045 pulongest (strtoulst (p, &p, 10)));
1046 if (*p)
1047 printf_filtered (_("rlim: %s\n"),
1048 pulongest (strtoulst (p, &p, 10)));
1049 if (*p)
1050 printf_filtered (_("Start of text: %s\n"),
1051 hex_string (strtoulst (p, &p, 10)));
1052 if (*p)
1053 printf_filtered (_("End of text: %s\n"),
1054 hex_string (strtoulst (p, &p, 10)));
1055 if (*p)
1056 printf_filtered (_("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 printf_filtered (_("Kernel stack pointer: %s\n"),
1062 hex_string (strtoulst (p, &p, 10)));
1063 if (*p)
1064 printf_filtered (_("Kernel instr pointer: %s\n"),
1065 hex_string (strtoulst (p, &p, 10)));
1066 if (*p)
1067 printf_filtered (_("Pending signals bitmap: %s\n"),
1068 hex_string (strtoulst (p, &p, 10)));
1069 if (*p)
1070 printf_filtered (_("Blocked signals bitmap: %s\n"),
1071 hex_string (strtoulst (p, &p, 10)));
1072 if (*p)
1073 printf_filtered (_("Ignored signals bitmap: %s\n"),
1074 hex_string (strtoulst (p, &p, 10)));
1075 if (*p)
1076 printf_filtered (_("Catched signals bitmap: %s\n"),
1077 hex_string (strtoulst (p, &p, 10)));
1078 if (*p)
1079 printf_filtered (_("wchan (system call): %s\n"),
1080 hex_string (strtoulst (p, &p, 10)));
1081 #endif
1082 }
1083 else
1084 warning (_("unable to open /proc file '%s'"), filename);
1085 }
1086 }
1087
1088 /* Implementation of `gdbarch_read_core_file_mappings', as defined in
1089 gdbarch.h.
1090
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:
1095
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...
1103
1104 CBFD is the BFD of the core file.
1105
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.
1110
1111 LOOP_CB is the callback function that will be executed once
1112 for each mapping. */
1113
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)
1120 {
1121 /* Ensure that ULONGEST is big enough for reading 64-bit core files. */
1122 gdb_static_assert (sizeof (ULONGEST) >= 8);
1123
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;
1130
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);
1134
1135 if (note_size < 2 * addr_size)
1136 {
1137 warning (_("malformed core note - too short for header"));
1138 return;
1139 }
1140
1141 gdb::def_vector<gdb_byte> contents (note_size);
1142 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1143 0, note_size))
1144 {
1145 warning (_("could not get core note contents"));
1146 return;
1147 }
1148
1149 gdb_byte *descdata = contents.data ();
1150 char *descend = (char *) descdata + note_size;
1151
1152 if (descdata[note_size - 1] != '\0')
1153 {
1154 warning (_("malformed note - does not end with \\0"));
1155 return;
1156 }
1157
1158 ULONGEST count = bfd_get (addr_size_bits, core_bfd, descdata);
1159 descdata += addr_size;
1160
1161 ULONGEST page_size = bfd_get (addr_size_bits, core_bfd, descdata);
1162 descdata += addr_size;
1163
1164 if (note_size < 2 * addr_size + count * 3 * addr_size)
1165 {
1166 warning (_("malformed note - too short for supplied file count"));
1167 return;
1168 }
1169
1170 char *filenames = (char *) descdata + count * 3 * addr_size;
1171
1172 /* Make sure that the correct number of filenames exist. Complain
1173 if there aren't enough or are too many. */
1174 char *f = filenames;
1175 for (int i = 0; i < count; i++)
1176 {
1177 if (f >= descend)
1178 {
1179 warning (_("malformed note - filename area is too small"));
1180 return;
1181 }
1182 f += strnlen (f, descend - f) + 1;
1183 }
1184 /* Complain, but don't return early if the filename area is too big. */
1185 if (f != descend)
1186 warning (_("malformed note - filename area is too big"));
1187
1188 pre_loop_cb (count);
1189
1190 for (int i = 0; i < count; i++)
1191 {
1192 ULONGEST start = bfd_get (addr_size_bits, core_bfd, descdata);
1193 descdata += addr_size;
1194 ULONGEST end = bfd_get (addr_size_bits, core_bfd, descdata);
1195 descdata += addr_size;
1196 ULONGEST file_ofs
1197 = bfd_get (addr_size_bits, core_bfd, descdata) * page_size;
1198 descdata += addr_size;
1199 char * filename = filenames;
1200 filenames += strlen ((char *) filenames) + 1;
1201
1202 loop_cb (i, start, end, file_ofs, filename, nullptr);
1203 }
1204 }
1205
1206 /* Implement "info proc mappings" for a corefile. */
1207
1208 static void
1209 linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
1210 {
1211 linux_read_core_file_mappings (gdbarch, core_bfd,
1212 [=] (ULONGEST count)
1213 {
1214 printf_filtered (_("Mapped address spaces:\n\n"));
1215 if (gdbarch_addr_bit (gdbarch) == 32)
1216 {
1217 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1218 "Start Addr",
1219 " End Addr",
1220 " Size", " Offset", "objfile");
1221 }
1222 else
1223 {
1224 printf_filtered (" %18s %18s %10s %10s %s\n",
1225 "Start Addr",
1226 " End Addr",
1227 " Size", " Offset", "objfile");
1228 }
1229 },
1230 [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
1231 const char *filename, const bfd_build_id *build_id)
1232 {
1233 if (gdbarch_addr_bit (gdbarch) == 32)
1234 printf_filtered ("\t%10s %10s %10s %10s %s\n",
1235 paddress (gdbarch, start),
1236 paddress (gdbarch, end),
1237 hex_string (end - start),
1238 hex_string (file_ofs),
1239 filename);
1240 else
1241 printf_filtered (" %18s %18s %10s %10s %s\n",
1242 paddress (gdbarch, start),
1243 paddress (gdbarch, end),
1244 hex_string (end - start),
1245 hex_string (file_ofs),
1246 filename);
1247 });
1248 }
1249
1250 /* Implement "info proc" for a corefile. */
1251
1252 static void
1253 linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
1254 enum info_proc_what what)
1255 {
1256 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
1257 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
1258
1259 if (exe_f)
1260 {
1261 const char *exe;
1262
1263 exe = bfd_core_file_failing_command (core_bfd);
1264 if (exe != NULL)
1265 printf_filtered ("exe = '%s'\n", exe);
1266 else
1267 warning (_("unable to find command name in core file"));
1268 }
1269
1270 if (mappings_f)
1271 linux_core_info_proc_mappings (gdbarch, args);
1272
1273 if (!exe_f && !mappings_f)
1274 error (_("unable to handle request"));
1275 }
1276
1277 /* Read siginfo data from the core, if possible. Returns -1 on
1278 failure. Otherwise, returns the number of bytes read. READBUF,
1279 OFFSET, and LEN are all as specified by the to_xfer_partial
1280 interface. */
1281
1282 static LONGEST
1283 linux_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
1284 ULONGEST offset, ULONGEST len)
1285 {
1286 thread_section_name section_name (".note.linuxcore.siginfo", inferior_ptid);
1287 asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
1288 if (section == NULL)
1289 return -1;
1290
1291 if (!bfd_get_section_contents (core_bfd, section, readbuf, offset, len))
1292 return -1;
1293
1294 return len;
1295 }
1296
1297 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
1298 ULONGEST offset, ULONGEST inode,
1299 int read, int write,
1300 int exec, int modified,
1301 const char *filename,
1302 void *data);
1303
1304 typedef int linux_dump_mapping_p_ftype (filter_flags filterflags,
1305 const struct smaps_vmflags *v,
1306 int maybe_private_p,
1307 int mapping_anon_p,
1308 int mapping_file_p,
1309 const char *filename,
1310 ULONGEST addr,
1311 ULONGEST offset);
1312
1313 /* Helper function to parse the contents of /proc/<pid>/smaps into a data
1314 structure, for easy access.
1315
1316 DATA is the contents of the smaps file. The parsed contents are stored
1317 into the SMAPS vector. */
1318
1319 static std::vector<struct smaps_data>
1320 parse_smaps_data (const char *data,
1321 const std::string maps_filename)
1322 {
1323 char *line, *t;
1324
1325 gdb_assert (data != nullptr);
1326
1327 line = strtok_r ((char *) data, "\n", &t);
1328
1329 std::vector<struct smaps_data> smaps;
1330
1331 while (line != NULL)
1332 {
1333 struct smaps_vmflags v;
1334 int read, write, exec, priv;
1335 int has_anonymous = 0;
1336 int mapping_anon_p;
1337 int mapping_file_p;
1338
1339 memset (&v, 0, sizeof (v));
1340 struct mapping m = read_mapping (line);
1341 mapping_anon_p = mapping_is_anonymous_p (m.filename);
1342 /* If the mapping is not anonymous, then we can consider it
1343 to be file-backed. These two states (anonymous or
1344 file-backed) seem to be exclusive, but they can actually
1345 coexist. For example, if a file-backed mapping has
1346 "Anonymous:" pages (see more below), then the Linux
1347 kernel will dump this mapping when the user specified
1348 that she only wants anonymous mappings in the corefile
1349 (*even* when she explicitly disabled the dumping of
1350 file-backed mappings). */
1351 mapping_file_p = !mapping_anon_p;
1352
1353 /* Decode permissions. */
1354 auto has_perm = [&m] (char c)
1355 { return m.permissions.find (c) != gdb::string_view::npos; };
1356 read = has_perm ('r');
1357 write = has_perm ('w');
1358 exec = has_perm ('x');
1359
1360 /* 'private' here actually means VM_MAYSHARE, and not
1361 VM_SHARED. In order to know if a mapping is really
1362 private or not, we must check the flag "sh" in the
1363 VmFlags field. This is done by decode_vmflags. However,
1364 if we are using a Linux kernel released before the commit
1365 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
1366 not have the VmFlags there. In this case, there is
1367 really no way to know if we are dealing with VM_SHARED,
1368 so we just assume that VM_MAYSHARE is enough. */
1369 priv = has_perm ('p');
1370
1371 /* Try to detect if region should be dumped by parsing smaps
1372 counters. */
1373 for (line = strtok_r (NULL, "\n", &t);
1374 line != NULL && line[0] >= 'A' && line[0] <= 'Z';
1375 line = strtok_r (NULL, "\n", &t))
1376 {
1377 char keyword[64 + 1];
1378
1379 if (sscanf (line, "%64s", keyword) != 1)
1380 {
1381 warning (_("Error parsing {s,}maps file '%s'"),
1382 maps_filename.c_str ());
1383 break;
1384 }
1385
1386 if (strcmp (keyword, "Anonymous:") == 0)
1387 {
1388 /* Older Linux kernels did not support the
1389 "Anonymous:" counter. Check it here. */
1390 has_anonymous = 1;
1391 }
1392 else if (strcmp (keyword, "VmFlags:") == 0)
1393 decode_vmflags (line, &v);
1394
1395 if (strcmp (keyword, "AnonHugePages:") == 0
1396 || strcmp (keyword, "Anonymous:") == 0)
1397 {
1398 unsigned long number;
1399
1400 if (sscanf (line, "%*s%lu", &number) != 1)
1401 {
1402 warning (_("Error parsing {s,}maps file '%s' number"),
1403 maps_filename.c_str ());
1404 break;
1405 }
1406 if (number > 0)
1407 {
1408 /* Even if we are dealing with a file-backed
1409 mapping, if it contains anonymous pages we
1410 consider it to be *also* an anonymous
1411 mapping, because this is what the Linux
1412 kernel does:
1413
1414 // Dump segments that have been written to.
1415 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1416 goto whole;
1417
1418 Note that if the mapping is already marked as
1419 file-backed (i.e., mapping_file_p is
1420 non-zero), then this is a special case, and
1421 this mapping will be dumped either when the
1422 user wants to dump file-backed *or* anonymous
1423 mappings. */
1424 mapping_anon_p = 1;
1425 }
1426 }
1427 }
1428 /* Save the smaps entry to the vector. */
1429 struct smaps_data map;
1430
1431 map.start_address = m.addr;
1432 map.end_address = m.endaddr;
1433 map.filename = m.filename;
1434 map.vmflags = v;
1435 map.read = read? true : false;
1436 map.write = write? true : false;
1437 map.exec = exec? true : false;
1438 map.priv = priv? true : false;
1439 map.has_anonymous = has_anonymous;
1440 map.mapping_anon_p = mapping_anon_p? true : false;
1441 map.mapping_file_p = mapping_file_p? true : false;
1442 map.offset = m.offset;
1443 map.inode = m.inode;
1444
1445 smaps.emplace_back (map);
1446 }
1447
1448 return smaps;
1449 }
1450
1451 /* See linux-tdep.h. */
1452
1453 bool
1454 linux_address_in_memtag_page (CORE_ADDR address)
1455 {
1456 if (current_inferior ()->fake_pid_p)
1457 return false;
1458
1459 pid_t pid = current_inferior ()->pid;
1460
1461 std::string smaps_file = string_printf ("/proc/%d/smaps", pid);
1462
1463 gdb::unique_xmalloc_ptr<char> data
1464 = target_fileio_read_stralloc (NULL, smaps_file.c_str ());
1465
1466 if (data == nullptr)
1467 return false;
1468
1469 /* Parse the contents of smaps into a vector. */
1470 std::vector<struct smaps_data> smaps
1471 = parse_smaps_data (data.get (), smaps_file);
1472
1473 for (const smaps_data &map : smaps)
1474 {
1475 /* Is the address within [start_address, end_address) in a page
1476 mapped with memory tagging? */
1477 if (address >= map.start_address
1478 && address < map.end_address
1479 && map.vmflags.memory_tagging)
1480 return true;
1481 }
1482
1483 return false;
1484 }
1485
1486 /* List memory regions in the inferior for a corefile. */
1487
1488 static int
1489 linux_find_memory_regions_full (struct gdbarch *gdbarch,
1490 linux_dump_mapping_p_ftype *should_dump_mapping_p,
1491 linux_find_memory_region_ftype *func,
1492 void *obfd)
1493 {
1494 pid_t pid;
1495 /* Default dump behavior of coredump_filter (0x33), according to
1496 Documentation/filesystems/proc.txt from the Linux kernel
1497 tree. */
1498 filter_flags filterflags = (COREFILTER_ANON_PRIVATE
1499 | COREFILTER_ANON_SHARED
1500 | COREFILTER_ELF_HEADERS
1501 | COREFILTER_HUGETLB_PRIVATE);
1502
1503 /* We need to know the real target PID to access /proc. */
1504 if (current_inferior ()->fake_pid_p)
1505 return 1;
1506
1507 pid = current_inferior ()->pid;
1508
1509 if (use_coredump_filter)
1510 {
1511 std::string core_dump_filter_name
1512 = string_printf ("/proc/%d/coredump_filter", pid);
1513
1514 gdb::unique_xmalloc_ptr<char> coredumpfilterdata
1515 = target_fileio_read_stralloc (NULL, core_dump_filter_name.c_str ());
1516
1517 if (coredumpfilterdata != NULL)
1518 {
1519 unsigned int flags;
1520
1521 sscanf (coredumpfilterdata.get (), "%x", &flags);
1522 filterflags = (enum filter_flag) flags;
1523 }
1524 }
1525
1526 std::string maps_filename = string_printf ("/proc/%d/smaps", pid);
1527
1528 gdb::unique_xmalloc_ptr<char> data
1529 = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
1530
1531 if (data == NULL)
1532 {
1533 /* Older Linux kernels did not support /proc/PID/smaps. */
1534 maps_filename = string_printf ("/proc/%d/maps", pid);
1535 data = target_fileio_read_stralloc (NULL, maps_filename.c_str ());
1536
1537 if (data == nullptr)
1538 return 1;
1539 }
1540
1541 /* Parse the contents of smaps into a vector. */
1542 std::vector<struct smaps_data> smaps
1543 = parse_smaps_data (data.get (), maps_filename.c_str ());
1544
1545 for (const struct smaps_data &map : smaps)
1546 {
1547 int should_dump_p = 0;
1548
1549 if (map.has_anonymous)
1550 {
1551 should_dump_p
1552 = should_dump_mapping_p (filterflags, &map.vmflags,
1553 map.priv,
1554 map.mapping_anon_p,
1555 map.mapping_file_p,
1556 map.filename.c_str (),
1557 map.start_address,
1558 map.offset);
1559 }
1560 else
1561 {
1562 /* Older Linux kernels did not support the "Anonymous:" counter.
1563 If it is missing, we can't be sure - dump all the pages. */
1564 should_dump_p = 1;
1565 }
1566
1567 /* Invoke the callback function to create the corefile segment. */
1568 if (should_dump_p)
1569 {
1570 func (map.start_address, map.end_address - map.start_address,
1571 map.offset, map.inode, map.read, map.write, map.exec,
1572 1, /* MODIFIED is true because we want to dump
1573 the mapping. */
1574 map.filename.c_str (), obfd);
1575 }
1576 }
1577
1578 return 0;
1579 }
1580
1581 /* A structure for passing information through
1582 linux_find_memory_regions_full. */
1583
1584 struct linux_find_memory_regions_data
1585 {
1586 /* The original callback. */
1587
1588 find_memory_region_ftype func;
1589
1590 /* The original datum. */
1591
1592 void *obfd;
1593 };
1594
1595 /* A callback for linux_find_memory_regions that converts between the
1596 "full"-style callback and find_memory_region_ftype. */
1597
1598 static int
1599 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
1600 ULONGEST offset, ULONGEST inode,
1601 int read, int write, int exec, int modified,
1602 const char *filename, void *arg)
1603 {
1604 struct linux_find_memory_regions_data *data
1605 = (struct linux_find_memory_regions_data *) arg;
1606
1607 return data->func (vaddr, size, read, write, exec, modified, data->obfd);
1608 }
1609
1610 /* A variant of linux_find_memory_regions_full that is suitable as the
1611 gdbarch find_memory_regions method. */
1612
1613 static int
1614 linux_find_memory_regions (struct gdbarch *gdbarch,
1615 find_memory_region_ftype func, void *obfd)
1616 {
1617 struct linux_find_memory_regions_data data;
1618
1619 data.func = func;
1620 data.obfd = obfd;
1621
1622 return linux_find_memory_regions_full (gdbarch,
1623 dump_mapping_p,
1624 linux_find_memory_regions_thunk,
1625 &data);
1626 }
1627
1628 /* This is used to pass information from
1629 linux_make_mappings_corefile_notes through
1630 linux_find_memory_regions_full. */
1631
1632 struct linux_make_mappings_data
1633 {
1634 /* Number of files mapped. */
1635 ULONGEST file_count;
1636
1637 /* The obstack for the main part of the data. */
1638 struct obstack *data_obstack;
1639
1640 /* The filename obstack. */
1641 struct obstack *filename_obstack;
1642
1643 /* The architecture's "long" type. */
1644 struct type *long_type;
1645 };
1646
1647 static linux_find_memory_region_ftype linux_make_mappings_callback;
1648
1649 /* A callback for linux_find_memory_regions_full that updates the
1650 mappings data for linux_make_mappings_corefile_notes. */
1651
1652 static int
1653 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
1654 ULONGEST offset, ULONGEST inode,
1655 int read, int write, int exec, int modified,
1656 const char *filename, void *data)
1657 {
1658 struct linux_make_mappings_data *map_data
1659 = (struct linux_make_mappings_data *) data;
1660 gdb_byte buf[sizeof (ULONGEST)];
1661
1662 if (*filename == '\0' || inode == 0)
1663 return 0;
1664
1665 ++map_data->file_count;
1666
1667 pack_long (buf, map_data->long_type, vaddr);
1668 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1669 pack_long (buf, map_data->long_type, vaddr + size);
1670 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1671 pack_long (buf, map_data->long_type, offset);
1672 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1673
1674 obstack_grow_str0 (map_data->filename_obstack, filename);
1675
1676 return 0;
1677 }
1678
1679 /* Write the file mapping data to the core file, if possible. OBFD is
1680 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1681 is a pointer to the note size. Updates NOTE_DATA and NOTE_SIZE. */
1682
1683 static void
1684 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
1685 gdb::unique_xmalloc_ptr<char> &note_data,
1686 int *note_size)
1687 {
1688 struct linux_make_mappings_data mapping_data;
1689 struct type *long_type
1690 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
1691 gdb_byte buf[sizeof (ULONGEST)];
1692
1693 auto_obstack data_obstack, filename_obstack;
1694
1695 mapping_data.file_count = 0;
1696 mapping_data.data_obstack = &data_obstack;
1697 mapping_data.filename_obstack = &filename_obstack;
1698 mapping_data.long_type = long_type;
1699
1700 /* Reserve space for the count. */
1701 obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
1702 /* We always write the page size as 1 since we have no good way to
1703 determine the correct value. */
1704 pack_long (buf, long_type, 1);
1705 obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
1706
1707 linux_find_memory_regions_full (gdbarch,
1708 dump_note_entry_p,
1709 linux_make_mappings_callback,
1710 &mapping_data);
1711
1712 if (mapping_data.file_count != 0)
1713 {
1714 /* Write the count to the obstack. */
1715 pack_long ((gdb_byte *) obstack_base (&data_obstack),
1716 long_type, mapping_data.file_count);
1717
1718 /* Copy the filenames to the data obstack. */
1719 int size = obstack_object_size (&filename_obstack);
1720 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
1721 size);
1722
1723 note_data.reset (elfcore_write_file_note (obfd, note_data.release (), note_size,
1724 obstack_base (&data_obstack),
1725 obstack_object_size (&data_obstack)));
1726 }
1727 }
1728
1729 /* Fetch the siginfo data for the specified thread, if it exists. If
1730 there is no data, or we could not read it, return an empty
1731 buffer. */
1732
1733 static gdb::byte_vector
1734 linux_get_siginfo_data (thread_info *thread, struct gdbarch *gdbarch)
1735 {
1736 struct type *siginfo_type;
1737 LONGEST bytes_read;
1738
1739 if (!gdbarch_get_siginfo_type_p (gdbarch))
1740 return gdb::byte_vector ();
1741
1742 scoped_restore_current_thread save_current_thread;
1743 switch_to_thread (thread);
1744
1745 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1746
1747 gdb::byte_vector buf (TYPE_LENGTH (siginfo_type));
1748
1749 bytes_read = target_read (current_inferior ()->top_target (),
1750 TARGET_OBJECT_SIGNAL_INFO, NULL,
1751 buf.data (), 0, TYPE_LENGTH (siginfo_type));
1752 if (bytes_read != TYPE_LENGTH (siginfo_type))
1753 buf.clear ();
1754
1755 return buf;
1756 }
1757
1758 struct linux_corefile_thread_data
1759 {
1760 linux_corefile_thread_data (struct gdbarch *gdbarch, bfd *obfd,
1761 gdb::unique_xmalloc_ptr<char> &note_data,
1762 int *note_size, gdb_signal stop_signal)
1763 : gdbarch (gdbarch), obfd (obfd), note_data (note_data),
1764 note_size (note_size), stop_signal (stop_signal)
1765 {}
1766
1767 struct gdbarch *gdbarch;
1768 bfd *obfd;
1769 gdb::unique_xmalloc_ptr<char> &note_data;
1770 int *note_size;
1771 enum gdb_signal stop_signal;
1772 };
1773
1774 /* Records the thread's register state for the corefile note
1775 section. */
1776
1777 static void
1778 linux_corefile_thread (struct thread_info *info,
1779 struct linux_corefile_thread_data *args)
1780 {
1781 gcore_elf_build_thread_register_notes (args->gdbarch, info,
1782 args->stop_signal,
1783 args->obfd, &args->note_data,
1784 args->note_size);
1785
1786 /* Don't return anything if we got no register information above,
1787 such a core file is useless. */
1788 if (args->note_data != NULL)
1789 {
1790 gdb::byte_vector siginfo_data
1791 = linux_get_siginfo_data (info, args->gdbarch);
1792 if (!siginfo_data.empty ())
1793 args->note_data.reset (elfcore_write_note (args->obfd,
1794 args->note_data.release (),
1795 args->note_size,
1796 "CORE", NT_SIGINFO,
1797 siginfo_data.data (),
1798 siginfo_data.size ()));
1799 }
1800 }
1801
1802 /* Fill the PRPSINFO structure with information about the process being
1803 debugged. Returns 1 in case of success, 0 for failures. Please note that
1804 even if the structure cannot be entirely filled (e.g., GDB was unable to
1805 gather information about the process UID/GID), this function will still
1806 return 1 since some information was already recorded. It will only return
1807 0 iff nothing can be gathered. */
1808
1809 static int
1810 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
1811 {
1812 /* The filename which we will use to obtain some info about the process.
1813 We will basically use this to store the `/proc/PID/FILENAME' file. */
1814 char filename[100];
1815 /* The basename of the executable. */
1816 const char *basename;
1817 /* Temporary buffer. */
1818 char *tmpstr;
1819 /* The valid states of a process, according to the Linux kernel. */
1820 const char valid_states[] = "RSDTZW";
1821 /* The program state. */
1822 const char *prog_state;
1823 /* The state of the process. */
1824 char pr_sname;
1825 /* The PID of the program which generated the corefile. */
1826 pid_t pid;
1827 /* Process flags. */
1828 unsigned int pr_flag;
1829 /* Process nice value. */
1830 long pr_nice;
1831 /* The number of fields read by `sscanf'. */
1832 int n_fields = 0;
1833
1834 gdb_assert (p != NULL);
1835
1836 /* Obtaining PID and filename. */
1837 pid = inferior_ptid.pid ();
1838 xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
1839 /* The full name of the program which generated the corefile. */
1840 gdb::unique_xmalloc_ptr<char> fname
1841 = target_fileio_read_stralloc (NULL, filename);
1842
1843 if (fname == NULL || fname.get ()[0] == '\0')
1844 {
1845 /* No program name was read, so we won't be able to retrieve more
1846 information about the process. */
1847 return 0;
1848 }
1849
1850 memset (p, 0, sizeof (*p));
1851
1852 /* Defining the PID. */
1853 p->pr_pid = pid;
1854
1855 /* Copying the program name. Only the basename matters. */
1856 basename = lbasename (fname.get ());
1857 strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1);
1858 p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
1859
1860 const std::string &infargs = current_inferior ()->args ();
1861
1862 /* The arguments of the program. */
1863 std::string psargs = fname.get ();
1864 if (!infargs.empty ())
1865 psargs += ' ' + infargs;
1866
1867 strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1);
1868 p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
1869
1870 xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
1871 /* The contents of `/proc/PID/stat'. */
1872 gdb::unique_xmalloc_ptr<char> proc_stat_contents
1873 = target_fileio_read_stralloc (NULL, filename);
1874 char *proc_stat = proc_stat_contents.get ();
1875
1876 if (proc_stat == NULL || *proc_stat == '\0')
1877 {
1878 /* Despite being unable to read more information about the
1879 process, we return 1 here because at least we have its
1880 command line, PID and arguments. */
1881 return 1;
1882 }
1883
1884 /* Ok, we have the stats. It's time to do a little parsing of the
1885 contents of the buffer, so that we end up reading what we want.
1886
1887 The following parsing mechanism is strongly based on the
1888 information generated by the `fs/proc/array.c' file, present in
1889 the Linux kernel tree. More details about how the information is
1890 displayed can be obtained by seeing the manpage of proc(5),
1891 specifically under the entry of `/proc/[pid]/stat'. */
1892
1893 /* Getting rid of the PID, since we already have it. */
1894 while (isdigit (*proc_stat))
1895 ++proc_stat;
1896
1897 proc_stat = skip_spaces (proc_stat);
1898
1899 /* ps command also relies on no trailing fields ever contain ')'. */
1900 proc_stat = strrchr (proc_stat, ')');
1901 if (proc_stat == NULL)
1902 return 1;
1903 proc_stat++;
1904
1905 proc_stat = skip_spaces (proc_stat);
1906
1907 n_fields = sscanf (proc_stat,
1908 "%c" /* Process state. */
1909 "%d%d%d" /* Parent PID, group ID, session ID. */
1910 "%*d%*d" /* tty_nr, tpgid (not used). */
1911 "%u" /* Flags. */
1912 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1913 cmajflt (not used). */
1914 "%*s%*s%*s%*s" /* utime, stime, cutime,
1915 cstime (not used). */
1916 "%*s" /* Priority (not used). */
1917 "%ld", /* Nice. */
1918 &pr_sname,
1919 &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1920 &pr_flag,
1921 &pr_nice);
1922
1923 if (n_fields != 6)
1924 {
1925 /* Again, we couldn't read the complementary information about
1926 the process state. However, we already have minimal
1927 information, so we just return 1 here. */
1928 return 1;
1929 }
1930
1931 /* Filling the structure fields. */
1932 prog_state = strchr (valid_states, pr_sname);
1933 if (prog_state != NULL)
1934 p->pr_state = prog_state - valid_states;
1935 else
1936 {
1937 /* Zero means "Running". */
1938 p->pr_state = 0;
1939 }
1940
1941 p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
1942 p->pr_zomb = p->pr_sname == 'Z';
1943 p->pr_nice = pr_nice;
1944 p->pr_flag = pr_flag;
1945
1946 /* Finally, obtaining the UID and GID. For that, we read and parse the
1947 contents of the `/proc/PID/status' file. */
1948 xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
1949 /* The contents of `/proc/PID/status'. */
1950 gdb::unique_xmalloc_ptr<char> proc_status_contents
1951 = target_fileio_read_stralloc (NULL, filename);
1952 char *proc_status = proc_status_contents.get ();
1953
1954 if (proc_status == NULL || *proc_status == '\0')
1955 {
1956 /* Returning 1 since we already have a bunch of information. */
1957 return 1;
1958 }
1959
1960 /* Extracting the UID. */
1961 tmpstr = strstr (proc_status, "Uid:");
1962 if (tmpstr != NULL)
1963 {
1964 /* Advancing the pointer to the beginning of the UID. */
1965 tmpstr += sizeof ("Uid:");
1966 while (*tmpstr != '\0' && !isdigit (*tmpstr))
1967 ++tmpstr;
1968
1969 if (isdigit (*tmpstr))
1970 p->pr_uid = strtol (tmpstr, &tmpstr, 10);
1971 }
1972
1973 /* Extracting the GID. */
1974 tmpstr = strstr (proc_status, "Gid:");
1975 if (tmpstr != NULL)
1976 {
1977 /* Advancing the pointer to the beginning of the GID. */
1978 tmpstr += sizeof ("Gid:");
1979 while (*tmpstr != '\0' && !isdigit (*tmpstr))
1980 ++tmpstr;
1981
1982 if (isdigit (*tmpstr))
1983 p->pr_gid = strtol (tmpstr, &tmpstr, 10);
1984 }
1985
1986 return 1;
1987 }
1988
1989 /* Build the note section for a corefile, and return it in a malloc
1990 buffer. */
1991
1992 static gdb::unique_xmalloc_ptr<char>
1993 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1994 {
1995 struct elf_internal_linux_prpsinfo prpsinfo;
1996 gdb::unique_xmalloc_ptr<char> note_data;
1997
1998 if (! gdbarch_iterate_over_regset_sections_p (gdbarch))
1999 return NULL;
2000
2001 if (linux_fill_prpsinfo (&prpsinfo))
2002 {
2003 if (gdbarch_ptr_bit (gdbarch) == 64)
2004 note_data.reset (elfcore_write_linux_prpsinfo64 (obfd,
2005 note_data.release (),
2006 note_size, &prpsinfo));
2007 else
2008 note_data.reset (elfcore_write_linux_prpsinfo32 (obfd,
2009 note_data.release (),
2010 note_size, &prpsinfo));
2011 }
2012
2013 /* Thread register information. */
2014 try
2015 {
2016 update_thread_list ();
2017 }
2018 catch (const gdb_exception_error &e)
2019 {
2020 exception_print (gdb_stderr, e);
2021 }
2022
2023 /* Like the kernel, prefer dumping the signalled thread first.
2024 "First thread" is what tools use to infer the signalled
2025 thread. */
2026 thread_info *signalled_thr = gcore_find_signalled_thread ();
2027 gdb_signal stop_signal;
2028 if (signalled_thr != nullptr)
2029 stop_signal = signalled_thr->stop_signal ();
2030 else
2031 stop_signal = GDB_SIGNAL_0;
2032
2033 linux_corefile_thread_data thread_args (gdbarch, obfd, note_data, note_size,
2034 stop_signal);
2035
2036 if (signalled_thr != nullptr)
2037 linux_corefile_thread (signalled_thr, &thread_args);
2038 for (thread_info *thr : current_inferior ()->non_exited_threads ())
2039 {
2040 if (thr == signalled_thr)
2041 continue;
2042
2043 linux_corefile_thread (thr, &thread_args);
2044 }
2045
2046 if (!note_data)
2047 return NULL;
2048
2049 /* Auxillary vector. */
2050 gdb::optional<gdb::byte_vector> auxv =
2051 target_read_alloc (current_inferior ()->top_target (),
2052 TARGET_OBJECT_AUXV, NULL);
2053 if (auxv && !auxv->empty ())
2054 {
2055 note_data.reset (elfcore_write_note (obfd, note_data.release (),
2056 note_size, "CORE", NT_AUXV,
2057 auxv->data (), auxv->size ()));
2058
2059 if (!note_data)
2060 return NULL;
2061 }
2062
2063 /* File mappings. */
2064 linux_make_mappings_corefile_notes (gdbarch, obfd, note_data, note_size);
2065
2066 /* Target description. */
2067 gcore_elf_make_tdesc_note (obfd, &note_data, note_size);
2068
2069 return note_data;
2070 }
2071
2072 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
2073 gdbarch.h. This function is not static because it is exported to
2074 other -tdep files. */
2075
2076 enum gdb_signal
2077 linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
2078 {
2079 switch (signal)
2080 {
2081 case 0:
2082 return GDB_SIGNAL_0;
2083
2084 case LINUX_SIGHUP:
2085 return GDB_SIGNAL_HUP;
2086
2087 case LINUX_SIGINT:
2088 return GDB_SIGNAL_INT;
2089
2090 case LINUX_SIGQUIT:
2091 return GDB_SIGNAL_QUIT;
2092
2093 case LINUX_SIGILL:
2094 return GDB_SIGNAL_ILL;
2095
2096 case LINUX_SIGTRAP:
2097 return GDB_SIGNAL_TRAP;
2098
2099 case LINUX_SIGABRT:
2100 return GDB_SIGNAL_ABRT;
2101
2102 case LINUX_SIGBUS:
2103 return GDB_SIGNAL_BUS;
2104
2105 case LINUX_SIGFPE:
2106 return GDB_SIGNAL_FPE;
2107
2108 case LINUX_SIGKILL:
2109 return GDB_SIGNAL_KILL;
2110
2111 case LINUX_SIGUSR1:
2112 return GDB_SIGNAL_USR1;
2113
2114 case LINUX_SIGSEGV:
2115 return GDB_SIGNAL_SEGV;
2116
2117 case LINUX_SIGUSR2:
2118 return GDB_SIGNAL_USR2;
2119
2120 case LINUX_SIGPIPE:
2121 return GDB_SIGNAL_PIPE;
2122
2123 case LINUX_SIGALRM:
2124 return GDB_SIGNAL_ALRM;
2125
2126 case LINUX_SIGTERM:
2127 return GDB_SIGNAL_TERM;
2128
2129 case LINUX_SIGCHLD:
2130 return GDB_SIGNAL_CHLD;
2131
2132 case LINUX_SIGCONT:
2133 return GDB_SIGNAL_CONT;
2134
2135 case LINUX_SIGSTOP:
2136 return GDB_SIGNAL_STOP;
2137
2138 case LINUX_SIGTSTP:
2139 return GDB_SIGNAL_TSTP;
2140
2141 case LINUX_SIGTTIN:
2142 return GDB_SIGNAL_TTIN;
2143
2144 case LINUX_SIGTTOU:
2145 return GDB_SIGNAL_TTOU;
2146
2147 case LINUX_SIGURG:
2148 return GDB_SIGNAL_URG;
2149
2150 case LINUX_SIGXCPU:
2151 return GDB_SIGNAL_XCPU;
2152
2153 case LINUX_SIGXFSZ:
2154 return GDB_SIGNAL_XFSZ;
2155
2156 case LINUX_SIGVTALRM:
2157 return GDB_SIGNAL_VTALRM;
2158
2159 case LINUX_SIGPROF:
2160 return GDB_SIGNAL_PROF;
2161
2162 case LINUX_SIGWINCH:
2163 return GDB_SIGNAL_WINCH;
2164
2165 /* No way to differentiate between SIGIO and SIGPOLL.
2166 Therefore, we just handle the first one. */
2167 case LINUX_SIGIO:
2168 return GDB_SIGNAL_IO;
2169
2170 case LINUX_SIGPWR:
2171 return GDB_SIGNAL_PWR;
2172
2173 case LINUX_SIGSYS:
2174 return GDB_SIGNAL_SYS;
2175
2176 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
2177 therefore we have to handle them here. */
2178 case LINUX_SIGRTMIN:
2179 return GDB_SIGNAL_REALTIME_32;
2180
2181 case LINUX_SIGRTMAX:
2182 return GDB_SIGNAL_REALTIME_64;
2183 }
2184
2185 if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
2186 {
2187 int offset = signal - LINUX_SIGRTMIN + 1;
2188
2189 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
2190 }
2191
2192 return GDB_SIGNAL_UNKNOWN;
2193 }
2194
2195 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
2196 gdbarch.h. This function is not static because it is exported to
2197 other -tdep files. */
2198
2199 int
2200 linux_gdb_signal_to_target (struct gdbarch *gdbarch,
2201 enum gdb_signal signal)
2202 {
2203 switch (signal)
2204 {
2205 case GDB_SIGNAL_0:
2206 return 0;
2207
2208 case GDB_SIGNAL_HUP:
2209 return LINUX_SIGHUP;
2210
2211 case GDB_SIGNAL_INT:
2212 return LINUX_SIGINT;
2213
2214 case GDB_SIGNAL_QUIT:
2215 return LINUX_SIGQUIT;
2216
2217 case GDB_SIGNAL_ILL:
2218 return LINUX_SIGILL;
2219
2220 case GDB_SIGNAL_TRAP:
2221 return LINUX_SIGTRAP;
2222
2223 case GDB_SIGNAL_ABRT:
2224 return LINUX_SIGABRT;
2225
2226 case GDB_SIGNAL_FPE:
2227 return LINUX_SIGFPE;
2228
2229 case GDB_SIGNAL_KILL:
2230 return LINUX_SIGKILL;
2231
2232 case GDB_SIGNAL_BUS:
2233 return LINUX_SIGBUS;
2234
2235 case GDB_SIGNAL_SEGV:
2236 return LINUX_SIGSEGV;
2237
2238 case GDB_SIGNAL_SYS:
2239 return LINUX_SIGSYS;
2240
2241 case GDB_SIGNAL_PIPE:
2242 return LINUX_SIGPIPE;
2243
2244 case GDB_SIGNAL_ALRM:
2245 return LINUX_SIGALRM;
2246
2247 case GDB_SIGNAL_TERM:
2248 return LINUX_SIGTERM;
2249
2250 case GDB_SIGNAL_URG:
2251 return LINUX_SIGURG;
2252
2253 case GDB_SIGNAL_STOP:
2254 return LINUX_SIGSTOP;
2255
2256 case GDB_SIGNAL_TSTP:
2257 return LINUX_SIGTSTP;
2258
2259 case GDB_SIGNAL_CONT:
2260 return LINUX_SIGCONT;
2261
2262 case GDB_SIGNAL_CHLD:
2263 return LINUX_SIGCHLD;
2264
2265 case GDB_SIGNAL_TTIN:
2266 return LINUX_SIGTTIN;
2267
2268 case GDB_SIGNAL_TTOU:
2269 return LINUX_SIGTTOU;
2270
2271 case GDB_SIGNAL_IO:
2272 return LINUX_SIGIO;
2273
2274 case GDB_SIGNAL_XCPU:
2275 return LINUX_SIGXCPU;
2276
2277 case GDB_SIGNAL_XFSZ:
2278 return LINUX_SIGXFSZ;
2279
2280 case GDB_SIGNAL_VTALRM:
2281 return LINUX_SIGVTALRM;
2282
2283 case GDB_SIGNAL_PROF:
2284 return LINUX_SIGPROF;
2285
2286 case GDB_SIGNAL_WINCH:
2287 return LINUX_SIGWINCH;
2288
2289 case GDB_SIGNAL_USR1:
2290 return LINUX_SIGUSR1;
2291
2292 case GDB_SIGNAL_USR2:
2293 return LINUX_SIGUSR2;
2294
2295 case GDB_SIGNAL_PWR:
2296 return LINUX_SIGPWR;
2297
2298 case GDB_SIGNAL_POLL:
2299 return LINUX_SIGPOLL;
2300
2301 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
2302 therefore we have to handle it here. */
2303 case GDB_SIGNAL_REALTIME_32:
2304 return LINUX_SIGRTMIN;
2305
2306 /* Same comment applies to _64. */
2307 case GDB_SIGNAL_REALTIME_64:
2308 return LINUX_SIGRTMAX;
2309 }
2310
2311 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
2312 if (signal >= GDB_SIGNAL_REALTIME_33
2313 && signal <= GDB_SIGNAL_REALTIME_63)
2314 {
2315 int offset = signal - GDB_SIGNAL_REALTIME_33;
2316
2317 return LINUX_SIGRTMIN + 1 + offset;
2318 }
2319
2320 return -1;
2321 }
2322
2323 /* Helper for linux_vsyscall_range that does the real work of finding
2324 the vsyscall's address range. */
2325
2326 static int
2327 linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
2328 {
2329 char filename[100];
2330 long pid;
2331
2332 if (target_auxv_search (current_inferior ()->top_target (),
2333 AT_SYSINFO_EHDR, &range->start) <= 0)
2334 return 0;
2335
2336 /* It doesn't make sense to access the host's /proc when debugging a
2337 core file. Instead, look for the PT_LOAD segment that matches
2338 the vDSO. */
2339 if (!target_has_execution ())
2340 {
2341 long phdrs_size;
2342 int num_phdrs, i;
2343
2344 phdrs_size = bfd_get_elf_phdr_upper_bound (core_bfd);
2345 if (phdrs_size == -1)
2346 return 0;
2347
2348 gdb::unique_xmalloc_ptr<Elf_Internal_Phdr>
2349 phdrs ((Elf_Internal_Phdr *) xmalloc (phdrs_size));
2350 num_phdrs = bfd_get_elf_phdrs (core_bfd, phdrs.get ());
2351 if (num_phdrs == -1)
2352 return 0;
2353
2354 for (i = 0; i < num_phdrs; i++)
2355 if (phdrs.get ()[i].p_type == PT_LOAD
2356 && phdrs.get ()[i].p_vaddr == range->start)
2357 {
2358 range->length = phdrs.get ()[i].p_memsz;
2359 return 1;
2360 }
2361
2362 return 0;
2363 }
2364
2365 /* We need to know the real target PID to access /proc. */
2366 if (current_inferior ()->fake_pid_p)
2367 return 0;
2368
2369 pid = current_inferior ()->pid;
2370
2371 /* Note that reading /proc/PID/task/PID/maps (1) is much faster than
2372 reading /proc/PID/maps (2). The later identifies thread stacks
2373 in the output, which requires scanning every thread in the thread
2374 group to check whether a VMA is actually a thread's stack. With
2375 Linux 4.4 on an Intel i7-4810MQ @ 2.80GHz, with an inferior with
2376 a few thousand threads, (1) takes a few miliseconds, while (2)
2377 takes several seconds. Also note that "smaps", what we read for
2378 determining core dump mappings, is even slower than "maps". */
2379 xsnprintf (filename, sizeof filename, "/proc/%ld/task/%ld/maps", pid, pid);
2380 gdb::unique_xmalloc_ptr<char> data
2381 = target_fileio_read_stralloc (NULL, filename);
2382 if (data != NULL)
2383 {
2384 char *line;
2385 char *saveptr = NULL;
2386
2387 for (line = strtok_r (data.get (), "\n", &saveptr);
2388 line != NULL;
2389 line = strtok_r (NULL, "\n", &saveptr))
2390 {
2391 ULONGEST addr, endaddr;
2392 const char *p = line;
2393
2394 addr = strtoulst (p, &p, 16);
2395 if (addr == range->start)
2396 {
2397 if (*p == '-')
2398 p++;
2399 endaddr = strtoulst (p, &p, 16);
2400 range->length = endaddr - addr;
2401 return 1;
2402 }
2403 }
2404 }
2405 else
2406 warning (_("unable to open /proc file '%s'"), filename);
2407
2408 return 0;
2409 }
2410
2411 /* Implementation of the "vsyscall_range" gdbarch hook. Handles
2412 caching, and defers the real work to linux_vsyscall_range_raw. */
2413
2414 static int
2415 linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
2416 {
2417 struct linux_info *info = get_linux_inferior_data (current_inferior ());
2418
2419 if (info->vsyscall_range_p == 0)
2420 {
2421 if (linux_vsyscall_range_raw (gdbarch, &info->vsyscall_range))
2422 info->vsyscall_range_p = 1;
2423 else
2424 info->vsyscall_range_p = -1;
2425 }
2426
2427 if (info->vsyscall_range_p < 0)
2428 return 0;
2429
2430 *range = info->vsyscall_range;
2431 return 1;
2432 }
2433
2434 /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system
2435 definitions would be dependent on compilation host. */
2436 #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */
2437 #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */
2438
2439 /* See gdbarch.sh 'infcall_mmap'. */
2440
2441 static CORE_ADDR
2442 linux_infcall_mmap (CORE_ADDR size, unsigned prot)
2443 {
2444 struct objfile *objf;
2445 /* Do there still exist any Linux systems without "mmap64"?
2446 "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */
2447 struct value *mmap_val = find_function_in_inferior ("mmap64", &objf);
2448 struct value *addr_val;
2449 struct gdbarch *gdbarch = objf->arch ();
2450 CORE_ADDR retval;
2451 enum
2452 {
2453 ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_LAST
2454 };
2455 struct value *arg[ARG_LAST];
2456
2457 arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
2458 0);
2459 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2460 arg[ARG_LENGTH] = value_from_ulongest
2461 (builtin_type (gdbarch)->builtin_unsigned_long, size);
2462 gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE
2463 | GDB_MMAP_PROT_EXEC))
2464 == 0);
2465 arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int, prot);
2466 arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int,
2467 GDB_MMAP_MAP_PRIVATE
2468 | GDB_MMAP_MAP_ANONYMOUS);
2469 arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1);
2470 arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64,
2471 0);
2472 addr_val = call_function_by_hand (mmap_val, NULL, arg);
2473 retval = value_as_address (addr_val);
2474 if (retval == (CORE_ADDR) -1)
2475 error (_("Failed inferior mmap call for %s bytes, errno is changed."),
2476 pulongest (size));
2477 return retval;
2478 }
2479
2480 /* See gdbarch.sh 'infcall_munmap'. */
2481
2482 static void
2483 linux_infcall_munmap (CORE_ADDR addr, CORE_ADDR size)
2484 {
2485 struct objfile *objf;
2486 struct value *munmap_val = find_function_in_inferior ("munmap", &objf);
2487 struct value *retval_val;
2488 struct gdbarch *gdbarch = objf->arch ();
2489 LONGEST retval;
2490 enum
2491 {
2492 ARG_ADDR, ARG_LENGTH, ARG_LAST
2493 };
2494 struct value *arg[ARG_LAST];
2495
2496 arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr,
2497 addr);
2498 /* Assuming sizeof (unsigned long) == sizeof (size_t). */
2499 arg[ARG_LENGTH] = value_from_ulongest
2500 (builtin_type (gdbarch)->builtin_unsigned_long, size);
2501 retval_val = call_function_by_hand (munmap_val, NULL, arg);
2502 retval = value_as_long (retval_val);
2503 if (retval != 0)
2504 warning (_("Failed inferior munmap call at %s for %s bytes, "
2505 "errno is changed."),
2506 hex_string (addr), pulongest (size));
2507 }
2508
2509 /* See linux-tdep.h. */
2510
2511 CORE_ADDR
2512 linux_displaced_step_location (struct gdbarch *gdbarch)
2513 {
2514 CORE_ADDR addr;
2515 int bp_len;
2516
2517 /* Determine entry point from target auxiliary vector. This avoids
2518 the need for symbols. Also, when debugging a stand-alone SPU
2519 executable, entry_point_address () will point to an SPU
2520 local-store address and is thus not usable as displaced stepping
2521 location. The auxiliary vector gets us the PowerPC-side entry
2522 point address instead. */
2523 if (target_auxv_search (current_inferior ()->top_target (),
2524 AT_ENTRY, &addr) <= 0)
2525 throw_error (NOT_SUPPORTED_ERROR,
2526 _("Cannot find AT_ENTRY auxiliary vector entry."));
2527
2528 /* Make certain that the address points at real code, and not a
2529 function descriptor. */
2530 addr = gdbarch_convert_from_func_ptr_addr
2531 (gdbarch, addr, current_inferior ()->top_target ());
2532
2533 /* Inferior calls also use the entry point as a breakpoint location.
2534 We don't want displaced stepping to interfere with those
2535 breakpoints, so leave space. */
2536 gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
2537 addr += bp_len * 2;
2538
2539 return addr;
2540 }
2541
2542 /* See linux-tdep.h. */
2543
2544 displaced_step_prepare_status
2545 linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
2546 CORE_ADDR &displaced_pc)
2547 {
2548 linux_info *per_inferior = get_linux_inferior_data (thread->inf);
2549
2550 if (!per_inferior->disp_step_bufs.has_value ())
2551 {
2552 /* Figure out the location of the buffers. They are contiguous, starting
2553 at DISP_STEP_BUF_ADDR. They are all of size BUF_LEN. */
2554 CORE_ADDR disp_step_buf_addr
2555 = linux_displaced_step_location (thread->inf->gdbarch);
2556 int buf_len = gdbarch_max_insn_length (arch);
2557
2558 linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (arch);
2559 gdb_assert (gdbarch_data->num_disp_step_buffers > 0);
2560
2561 std::vector<CORE_ADDR> buffers;
2562 for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
2563 buffers.push_back (disp_step_buf_addr + i * buf_len);
2564
2565 per_inferior->disp_step_bufs.emplace (buffers);
2566 }
2567
2568 return per_inferior->disp_step_bufs->prepare (thread, displaced_pc);
2569 }
2570
2571 /* See linux-tdep.h. */
2572
2573 displaced_step_finish_status
2574 linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
2575 {
2576 linux_info *per_inferior = get_linux_inferior_data (thread->inf);
2577
2578 gdb_assert (per_inferior->disp_step_bufs.has_value ());
2579
2580 return per_inferior->disp_step_bufs->finish (arch, thread, sig);
2581 }
2582
2583 /* See linux-tdep.h. */
2584
2585 const displaced_step_copy_insn_closure *
2586 linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
2587 {
2588 linux_info *per_inferior = linux_inferior_data.get (inf);
2589
2590 if (per_inferior == nullptr
2591 || !per_inferior->disp_step_bufs.has_value ())
2592 return nullptr;
2593
2594 return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
2595 }
2596
2597 /* See linux-tdep.h. */
2598
2599 void
2600 linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
2601 {
2602 linux_info *per_inferior = linux_inferior_data.get (parent_inf);
2603
2604 if (per_inferior == nullptr
2605 || !per_inferior->disp_step_bufs.has_value ())
2606 return;
2607
2608 per_inferior->disp_step_bufs->restore_in_ptid (ptid);
2609 }
2610
2611 /* See linux-tdep.h. */
2612
2613 CORE_ADDR
2614 linux_get_hwcap (struct target_ops *target)
2615 {
2616 CORE_ADDR field;
2617 if (target_auxv_search (target, AT_HWCAP, &field) != 1)
2618 return 0;
2619 return field;
2620 }
2621
2622 /* See linux-tdep.h. */
2623
2624 CORE_ADDR
2625 linux_get_hwcap2 (struct target_ops *target)
2626 {
2627 CORE_ADDR field;
2628 if (target_auxv_search (target, AT_HWCAP2, &field) != 1)
2629 return 0;
2630 return field;
2631 }
2632
2633 /* Display whether the gcore command is using the
2634 /proc/PID/coredump_filter file. */
2635
2636 static void
2637 show_use_coredump_filter (struct ui_file *file, int from_tty,
2638 struct cmd_list_element *c, const char *value)
2639 {
2640 fprintf_filtered (file, _("Use of /proc/PID/coredump_filter file to generate"
2641 " corefiles is %s.\n"), value);
2642 }
2643
2644 /* Display whether the gcore command is dumping mappings marked with
2645 the VM_DONTDUMP flag. */
2646
2647 static void
2648 show_dump_excluded_mappings (struct ui_file *file, int from_tty,
2649 struct cmd_list_element *c, const char *value)
2650 {
2651 fprintf_filtered (file, _("Dumping of mappings marked with the VM_DONTDUMP"
2652 " flag is %s.\n"), value);
2653 }
2654
2655 /* To be called from the various GDB_OSABI_LINUX handlers for the
2656 various GNU/Linux architectures and machine types.
2657
2658 NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use. If 0,
2659 displaced stepping is not supported. */
2660
2661 void
2662 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
2663 int num_disp_step_buffers)
2664 {
2665 if (num_disp_step_buffers > 0)
2666 {
2667 linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (gdbarch);
2668 gdbarch_data->num_disp_step_buffers = num_disp_step_buffers;
2669
2670 set_gdbarch_displaced_step_prepare (gdbarch,
2671 linux_displaced_step_prepare);
2672 set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
2673 set_gdbarch_displaced_step_copy_insn_closure_by_addr
2674 (gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
2675 set_gdbarch_displaced_step_restore_all_in_ptid
2676 (gdbarch, linux_displaced_step_restore_all_in_ptid);
2677 }
2678
2679 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
2680 set_gdbarch_info_proc (gdbarch, linux_info_proc);
2681 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
2682 set_gdbarch_core_xfer_siginfo (gdbarch, linux_core_xfer_siginfo);
2683 set_gdbarch_read_core_file_mappings (gdbarch, linux_read_core_file_mappings);
2684 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
2685 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes);
2686 set_gdbarch_has_shared_address_space (gdbarch,
2687 linux_has_shared_address_space);
2688 set_gdbarch_gdb_signal_from_target (gdbarch,
2689 linux_gdb_signal_from_target);
2690 set_gdbarch_gdb_signal_to_target (gdbarch,
2691 linux_gdb_signal_to_target);
2692 set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range);
2693 set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap);
2694 set_gdbarch_infcall_munmap (gdbarch, linux_infcall_munmap);
2695 set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
2696 }
2697
2698 void _initialize_linux_tdep ();
2699 void
2700 _initialize_linux_tdep ()
2701 {
2702 linux_gdbarch_data_handle =
2703 gdbarch_data_register_pre_init (init_linux_gdbarch_data);
2704
2705 /* Observers used to invalidate the cache when needed. */
2706 gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf,
2707 "linux-tdep");
2708 gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf,
2709 "linux-tdep");
2710 gdb::observers::inferior_execd.attach (invalidate_linux_cache_inf,
2711 "linux-tdep");
2712
2713 add_setshow_boolean_cmd ("use-coredump-filter", class_files,
2714 &use_coredump_filter, _("\
2715 Set whether gcore should consider /proc/PID/coredump_filter."),
2716 _("\
2717 Show whether gcore should consider /proc/PID/coredump_filter."),
2718 _("\
2719 Use this command to set whether gcore should consider the contents\n\
2720 of /proc/PID/coredump_filter when generating the corefile. For more information\n\
2721 about this file, refer to the manpage of core(5)."),
2722 NULL, show_use_coredump_filter,
2723 &setlist, &showlist);
2724
2725 add_setshow_boolean_cmd ("dump-excluded-mappings", class_files,
2726 &dump_excluded_mappings, _("\
2727 Set whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2728 _("\
2729 Show whether gcore should dump mappings marked with the VM_DONTDUMP flag."),
2730 _("\
2731 Use this command to set whether gcore should dump mappings marked with the\n\
2732 VM_DONTDUMP flag (\"dd\" in /proc/PID/smaps) when generating the corefile. For\n\
2733 more information about this file, refer to the manpage of proc(5) and core(5)."),
2734 NULL, show_dump_excluded_mappings,
2735 &setlist, &showlist);
2736 }
2737
2738 /* Fetch (and possibly build) an appropriate `link_map_offsets' for
2739 ILP32/LP64 Linux systems which don't have the r_ldsomap field. */
2740
2741 link_map_offsets *
2742 linux_ilp32_fetch_link_map_offsets ()
2743 {
2744 static link_map_offsets lmo;
2745 static link_map_offsets *lmp = nullptr;
2746
2747 if (lmp == nullptr)
2748 {
2749 lmp = &lmo;
2750
2751 lmo.r_version_offset = 0;
2752 lmo.r_version_size = 4;
2753 lmo.r_map_offset = 4;
2754 lmo.r_brk_offset = 8;
2755 lmo.r_ldsomap_offset = -1;
2756
2757 /* Everything we need is in the first 20 bytes. */
2758 lmo.link_map_size = 20;
2759 lmo.l_addr_offset = 0;
2760 lmo.l_name_offset = 4;
2761 lmo.l_ld_offset = 8;
2762 lmo.l_next_offset = 12;
2763 lmo.l_prev_offset = 16;
2764 }
2765
2766 return lmp;
2767 }
2768
2769 link_map_offsets *
2770 linux_lp64_fetch_link_map_offsets ()
2771 {
2772 static link_map_offsets lmo;
2773 static link_map_offsets *lmp = nullptr;
2774
2775 if (lmp == nullptr)
2776 {
2777 lmp = &lmo;
2778
2779 lmo.r_version_offset = 0;
2780 lmo.r_version_size = 4;
2781 lmo.r_map_offset = 8;
2782 lmo.r_brk_offset = 16;
2783 lmo.r_ldsomap_offset = -1;
2784
2785 /* Everything we need is in the first 40 bytes. */
2786 lmo.link_map_size = 40;
2787 lmo.l_addr_offset = 0;
2788 lmo.l_name_offset = 8;
2789 lmo.l_ld_offset = 16;
2790 lmo.l_next_offset = 24;
2791 lmo.l_prev_offset = 32;
2792 }
2793
2794 return lmp;
2795 }