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