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