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