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