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