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