Commit | Line | Data |
---|---|---|
4aa995e1 PA |
1 | /* Target-dependent code for GNU/Linux, architecture independent. |
2 | ||
32d0add0 | 3 | Copyright (C) 2009-2015 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 TT |
33 | #include "arch-utils.h" |
34 | #include "gdb_obstack.h" | |
cdfa0b0a | 35 | #include "observer.h" |
3bc3cebe JK |
36 | #include "objfiles.h" |
37 | #include "infcall.h" | |
9904185c | 38 | #include "nat/linux-maps.h" |
df8411da | 39 | #include "gdbcmd.h" |
3030c96e UW |
40 | |
41 | #include <ctype.h> | |
4aa995e1 | 42 | |
df8411da SDJ |
43 | /* Whether to take the /proc/PID/coredump_filter into account when |
44 | generating a corefile. */ | |
45 | ||
46 | static int use_coredump_filter = 1; | |
47 | ||
eb14d406 SDJ |
48 | /* This enum represents the signals' numbers on a generic architecture |
49 | running the Linux kernel. The definition of "generic" comes from | |
50 | the file <include/uapi/asm-generic/signal.h>, from the Linux kernel | |
51 | tree, which is the "de facto" implementation of signal numbers to | |
52 | be used by new architecture ports. | |
53 | ||
54 | For those architectures which have differences between the generic | |
55 | standard (e.g., Alpha), we define the different signals (and *only* | |
56 | those) in the specific target-dependent file (e.g., | |
57 | alpha-linux-tdep.c, for Alpha). Please refer to the architecture's | |
58 | tdep file for more information. | |
59 | ||
60 | ARM deserves a special mention here. On the file | |
61 | <arch/arm/include/uapi/asm/signal.h>, it defines only one different | |
62 | (and ARM-only) signal, which is SIGSWI, with the same number as | |
63 | SIGRTMIN. This signal is used only for a very specific target, | |
64 | called ArthurOS (from RISCOS). Therefore, we do not handle it on | |
65 | the ARM-tdep file, and we can safely use the generic signal handler | |
66 | here for ARM targets. | |
67 | ||
68 | As stated above, this enum is derived from | |
69 | <include/uapi/asm-generic/signal.h>, from the Linux kernel | |
70 | tree. */ | |
71 | ||
72 | enum | |
73 | { | |
74 | LINUX_SIGHUP = 1, | |
75 | LINUX_SIGINT = 2, | |
76 | LINUX_SIGQUIT = 3, | |
77 | LINUX_SIGILL = 4, | |
78 | LINUX_SIGTRAP = 5, | |
79 | LINUX_SIGABRT = 6, | |
80 | LINUX_SIGIOT = 6, | |
81 | LINUX_SIGBUS = 7, | |
82 | LINUX_SIGFPE = 8, | |
83 | LINUX_SIGKILL = 9, | |
84 | LINUX_SIGUSR1 = 10, | |
85 | LINUX_SIGSEGV = 11, | |
86 | LINUX_SIGUSR2 = 12, | |
87 | LINUX_SIGPIPE = 13, | |
88 | LINUX_SIGALRM = 14, | |
89 | LINUX_SIGTERM = 15, | |
90 | LINUX_SIGSTKFLT = 16, | |
91 | LINUX_SIGCHLD = 17, | |
92 | LINUX_SIGCONT = 18, | |
93 | LINUX_SIGSTOP = 19, | |
94 | LINUX_SIGTSTP = 20, | |
95 | LINUX_SIGTTIN = 21, | |
96 | LINUX_SIGTTOU = 22, | |
97 | LINUX_SIGURG = 23, | |
98 | LINUX_SIGXCPU = 24, | |
99 | LINUX_SIGXFSZ = 25, | |
100 | LINUX_SIGVTALRM = 26, | |
101 | LINUX_SIGPROF = 27, | |
102 | LINUX_SIGWINCH = 28, | |
103 | LINUX_SIGIO = 29, | |
104 | LINUX_SIGPOLL = LINUX_SIGIO, | |
105 | LINUX_SIGPWR = 30, | |
106 | LINUX_SIGSYS = 31, | |
107 | LINUX_SIGUNUSED = 31, | |
108 | ||
109 | LINUX_SIGRTMIN = 32, | |
110 | LINUX_SIGRTMAX = 64, | |
111 | }; | |
112 | ||
06253dd3 JK |
113 | static struct gdbarch_data *linux_gdbarch_data_handle; |
114 | ||
115 | struct linux_gdbarch_data | |
116 | { | |
117 | struct type *siginfo_type; | |
118 | }; | |
119 | ||
120 | static void * | |
121 | init_linux_gdbarch_data (struct gdbarch *gdbarch) | |
122 | { | |
123 | return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data); | |
124 | } | |
125 | ||
126 | static struct linux_gdbarch_data * | |
127 | get_linux_gdbarch_data (struct gdbarch *gdbarch) | |
128 | { | |
129 | return gdbarch_data (gdbarch, linux_gdbarch_data_handle); | |
130 | } | |
131 | ||
cdfa0b0a PA |
132 | /* Per-inferior data key. */ |
133 | static const struct inferior_data *linux_inferior_data; | |
134 | ||
135 | /* Linux-specific cached data. This is used by GDB for caching | |
136 | purposes for each inferior. This helps reduce the overhead of | |
137 | transfering data from a remote target to the local host. */ | |
138 | struct linux_info | |
139 | { | |
140 | /* Cache of the inferior's vsyscall/vDSO mapping range. Only valid | |
141 | if VSYSCALL_RANGE_P is positive. This is cached because getting | |
142 | at this info requires an auxv lookup (which is itself cached), | |
143 | and looking through the inferior's mappings (which change | |
144 | throughout execution and therefore cannot be cached). */ | |
145 | struct mem_range vsyscall_range; | |
146 | ||
147 | /* Zero if we haven't tried looking up the vsyscall's range before | |
148 | yet. Positive if we tried looking it up, and found it. Negative | |
149 | if we tried looking it up but failed. */ | |
150 | int vsyscall_range_p; | |
151 | }; | |
152 | ||
153 | /* Frees whatever allocated space there is to be freed and sets INF's | |
154 | linux cache data pointer to NULL. */ | |
155 | ||
156 | static void | |
157 | invalidate_linux_cache_inf (struct inferior *inf) | |
158 | { | |
159 | struct linux_info *info; | |
160 | ||
161 | info = inferior_data (inf, linux_inferior_data); | |
162 | if (info != NULL) | |
163 | { | |
164 | xfree (info); | |
165 | set_inferior_data (inf, linux_inferior_data, NULL); | |
166 | } | |
167 | } | |
168 | ||
169 | /* Handles the cleanup of the linux cache for inferior INF. ARG is | |
170 | ignored. Callback for the inferior_appeared and inferior_exit | |
171 | events. */ | |
172 | ||
173 | static void | |
174 | linux_inferior_data_cleanup (struct inferior *inf, void *arg) | |
175 | { | |
176 | invalidate_linux_cache_inf (inf); | |
177 | } | |
178 | ||
179 | /* Fetch the linux cache info for INF. This function always returns a | |
180 | valid INFO pointer. */ | |
181 | ||
182 | static struct linux_info * | |
183 | get_linux_inferior_data (void) | |
184 | { | |
185 | struct linux_info *info; | |
186 | struct inferior *inf = current_inferior (); | |
187 | ||
188 | info = inferior_data (inf, linux_inferior_data); | |
189 | if (info == NULL) | |
190 | { | |
191 | info = XCNEW (struct linux_info); | |
192 | set_inferior_data (inf, linux_inferior_data, info); | |
193 | } | |
194 | ||
195 | return info; | |
196 | } | |
197 | ||
4aa995e1 PA |
198 | /* This function is suitable for architectures that don't |
199 | extend/override the standard siginfo structure. */ | |
200 | ||
5cd867b4 | 201 | static struct type * |
4aa995e1 PA |
202 | linux_get_siginfo_type (struct gdbarch *gdbarch) |
203 | { | |
06253dd3 | 204 | struct linux_gdbarch_data *linux_gdbarch_data; |
4aa995e1 PA |
205 | struct type *int_type, *uint_type, *long_type, *void_ptr_type; |
206 | struct type *uid_type, *pid_type; | |
207 | struct type *sigval_type, *clock_type; | |
208 | struct type *siginfo_type, *sifields_type; | |
209 | struct type *type; | |
210 | ||
06253dd3 JK |
211 | linux_gdbarch_data = get_linux_gdbarch_data (gdbarch); |
212 | if (linux_gdbarch_data->siginfo_type != NULL) | |
213 | return linux_gdbarch_data->siginfo_type; | |
214 | ||
e9bb382b UW |
215 | int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), |
216 | 0, "int"); | |
217 | uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), | |
218 | 1, "unsigned int"); | |
219 | long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), | |
220 | 0, "long"); | |
4aa995e1 PA |
221 | void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void); |
222 | ||
223 | /* sival_t */ | |
e9bb382b | 224 | sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION); |
4aa995e1 PA |
225 | TYPE_NAME (sigval_type) = xstrdup ("sigval_t"); |
226 | append_composite_type_field (sigval_type, "sival_int", int_type); | |
227 | append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type); | |
228 | ||
229 | /* __pid_t */ | |
e3aa49af MS |
230 | pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF, |
231 | TYPE_LENGTH (int_type), "__pid_t"); | |
4aa995e1 | 232 | TYPE_TARGET_TYPE (pid_type) = int_type; |
e9bb382b | 233 | TYPE_TARGET_STUB (pid_type) = 1; |
4aa995e1 PA |
234 | |
235 | /* __uid_t */ | |
e3aa49af MS |
236 | uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF, |
237 | TYPE_LENGTH (uint_type), "__uid_t"); | |
4aa995e1 | 238 | TYPE_TARGET_TYPE (uid_type) = uint_type; |
e9bb382b | 239 | TYPE_TARGET_STUB (uid_type) = 1; |
4aa995e1 PA |
240 | |
241 | /* __clock_t */ | |
e3aa49af MS |
242 | clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF, |
243 | TYPE_LENGTH (long_type), "__clock_t"); | |
4aa995e1 | 244 | TYPE_TARGET_TYPE (clock_type) = long_type; |
e9bb382b | 245 | TYPE_TARGET_STUB (clock_type) = 1; |
4aa995e1 PA |
246 | |
247 | /* _sifields */ | |
e9bb382b | 248 | sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION); |
4aa995e1 PA |
249 | |
250 | { | |
251 | const int si_max_size = 128; | |
252 | int si_pad_size; | |
253 | int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT; | |
254 | ||
255 | /* _pad */ | |
256 | if (gdbarch_ptr_bit (gdbarch) == 64) | |
257 | si_pad_size = (si_max_size / size_of_int) - 4; | |
258 | else | |
259 | si_pad_size = (si_max_size / size_of_int) - 3; | |
260 | append_composite_type_field (sifields_type, "_pad", | |
261 | init_vector_type (int_type, si_pad_size)); | |
262 | } | |
263 | ||
264 | /* _kill */ | |
e9bb382b | 265 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
266 | append_composite_type_field (type, "si_pid", pid_type); |
267 | append_composite_type_field (type, "si_uid", uid_type); | |
268 | append_composite_type_field (sifields_type, "_kill", type); | |
269 | ||
270 | /* _timer */ | |
e9bb382b | 271 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
272 | append_composite_type_field (type, "si_tid", int_type); |
273 | append_composite_type_field (type, "si_overrun", int_type); | |
274 | append_composite_type_field (type, "si_sigval", sigval_type); | |
275 | append_composite_type_field (sifields_type, "_timer", type); | |
276 | ||
277 | /* _rt */ | |
e9bb382b | 278 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
279 | append_composite_type_field (type, "si_pid", pid_type); |
280 | append_composite_type_field (type, "si_uid", uid_type); | |
281 | append_composite_type_field (type, "si_sigval", sigval_type); | |
282 | append_composite_type_field (sifields_type, "_rt", type); | |
283 | ||
284 | /* _sigchld */ | |
e9bb382b | 285 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
286 | append_composite_type_field (type, "si_pid", pid_type); |
287 | append_composite_type_field (type, "si_uid", uid_type); | |
288 | append_composite_type_field (type, "si_status", int_type); | |
289 | append_composite_type_field (type, "si_utime", clock_type); | |
290 | append_composite_type_field (type, "si_stime", clock_type); | |
291 | append_composite_type_field (sifields_type, "_sigchld", type); | |
292 | ||
293 | /* _sigfault */ | |
e9bb382b | 294 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
295 | append_composite_type_field (type, "si_addr", void_ptr_type); |
296 | append_composite_type_field (sifields_type, "_sigfault", type); | |
297 | ||
298 | /* _sigpoll */ | |
e9bb382b | 299 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
300 | append_composite_type_field (type, "si_band", long_type); |
301 | append_composite_type_field (type, "si_fd", int_type); | |
302 | append_composite_type_field (sifields_type, "_sigpoll", type); | |
303 | ||
304 | /* struct siginfo */ | |
e9bb382b | 305 | siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT); |
4aa995e1 PA |
306 | TYPE_NAME (siginfo_type) = xstrdup ("siginfo"); |
307 | append_composite_type_field (siginfo_type, "si_signo", int_type); | |
308 | append_composite_type_field (siginfo_type, "si_errno", int_type); | |
309 | append_composite_type_field (siginfo_type, "si_code", int_type); | |
310 | append_composite_type_field_aligned (siginfo_type, | |
311 | "_sifields", sifields_type, | |
312 | TYPE_LENGTH (long_type)); | |
313 | ||
06253dd3 JK |
314 | linux_gdbarch_data->siginfo_type = siginfo_type; |
315 | ||
4aa995e1 PA |
316 | return siginfo_type; |
317 | } | |
6b3ae818 | 318 | |
c01cbb3d YQ |
319 | /* Return true if the target is running on uClinux instead of normal |
320 | Linux kernel. */ | |
321 | ||
322 | int | |
323 | linux_is_uclinux (void) | |
6c95b8df | 324 | { |
6c95b8df | 325 | CORE_ADDR dummy; |
6c95b8df | 326 | |
c01cbb3d YQ |
327 | return (target_auxv_search (¤t_target, AT_NULL, &dummy) > 0 |
328 | && target_auxv_search (¤t_target, AT_PAGESZ, &dummy) == 0); | |
329 | } | |
6c95b8df | 330 | |
c01cbb3d YQ |
331 | static int |
332 | linux_has_shared_address_space (struct gdbarch *gdbarch) | |
333 | { | |
334 | return linux_is_uclinux (); | |
6c95b8df | 335 | } |
a5ee0f0c PA |
336 | |
337 | /* This is how we want PTIDs from core files to be printed. */ | |
338 | ||
339 | static char * | |
340 | linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid) | |
341 | { | |
342 | static char buf[80]; | |
343 | ||
344 | if (ptid_get_lwp (ptid) != 0) | |
345 | { | |
346 | snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid)); | |
347 | return buf; | |
348 | } | |
349 | ||
350 | return normal_pid_to_str (ptid); | |
351 | } | |
352 | ||
3030c96e UW |
353 | /* Implement the "info proc" command. */ |
354 | ||
355 | static void | |
7bc112c1 | 356 | linux_info_proc (struct gdbarch *gdbarch, const char *args, |
3030c96e UW |
357 | enum info_proc_what what) |
358 | { | |
359 | /* A long is used for pid instead of an int to avoid a loss of precision | |
360 | compiler warning from the output of strtoul. */ | |
361 | long pid; | |
362 | int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL); | |
363 | int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL); | |
364 | int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL); | |
365 | int mappings_f = (what == IP_MAPPINGS || what == IP_ALL); | |
366 | int status_f = (what == IP_STATUS || what == IP_ALL); | |
367 | int stat_f = (what == IP_STAT || what == IP_ALL); | |
368 | char filename[100]; | |
001f13d8 | 369 | char *data; |
3030c96e UW |
370 | int target_errno; |
371 | ||
372 | if (args && isdigit (args[0])) | |
7bc112c1 TT |
373 | { |
374 | char *tem; | |
375 | ||
376 | pid = strtoul (args, &tem, 10); | |
377 | args = tem; | |
378 | } | |
3030c96e UW |
379 | else |
380 | { | |
381 | if (!target_has_execution) | |
382 | error (_("No current process: you must name one.")); | |
383 | if (current_inferior ()->fake_pid_p) | |
384 | error (_("Can't determine the current process's PID: you must name one.")); | |
385 | ||
386 | pid = current_inferior ()->pid; | |
387 | } | |
388 | ||
7bc112c1 | 389 | args = skip_spaces_const (args); |
3030c96e UW |
390 | if (args && args[0]) |
391 | error (_("Too many parameters: %s"), args); | |
392 | ||
393 | printf_filtered (_("process %ld\n"), pid); | |
394 | if (cmdline_f) | |
395 | { | |
396 | xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid); | |
07c138c8 | 397 | data = target_fileio_read_stralloc (NULL, filename); |
3030c96e UW |
398 | if (data) |
399 | { | |
400 | struct cleanup *cleanup = make_cleanup (xfree, data); | |
401 | printf_filtered ("cmdline = '%s'\n", data); | |
402 | do_cleanups (cleanup); | |
403 | } | |
404 | else | |
405 | warning (_("unable to open /proc file '%s'"), filename); | |
406 | } | |
407 | if (cwd_f) | |
408 | { | |
409 | xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid); | |
07c138c8 | 410 | data = target_fileio_readlink (NULL, filename, &target_errno); |
3030c96e UW |
411 | if (data) |
412 | { | |
413 | struct cleanup *cleanup = make_cleanup (xfree, data); | |
414 | printf_filtered ("cwd = '%s'\n", data); | |
415 | do_cleanups (cleanup); | |
416 | } | |
417 | else | |
418 | warning (_("unable to read link '%s'"), filename); | |
419 | } | |
420 | if (exe_f) | |
421 | { | |
422 | xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid); | |
07c138c8 | 423 | data = target_fileio_readlink (NULL, filename, &target_errno); |
3030c96e UW |
424 | if (data) |
425 | { | |
426 | struct cleanup *cleanup = make_cleanup (xfree, data); | |
427 | printf_filtered ("exe = '%s'\n", data); | |
428 | do_cleanups (cleanup); | |
429 | } | |
430 | else | |
431 | warning (_("unable to read link '%s'"), filename); | |
432 | } | |
433 | if (mappings_f) | |
434 | { | |
435 | xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid); | |
07c138c8 | 436 | data = target_fileio_read_stralloc (NULL, filename); |
3030c96e UW |
437 | if (data) |
438 | { | |
439 | struct cleanup *cleanup = make_cleanup (xfree, data); | |
440 | char *line; | |
441 | ||
442 | printf_filtered (_("Mapped address spaces:\n\n")); | |
443 | if (gdbarch_addr_bit (gdbarch) == 32) | |
444 | { | |
445 | printf_filtered ("\t%10s %10s %10s %10s %s\n", | |
446 | "Start Addr", | |
447 | " End Addr", | |
448 | " Size", " Offset", "objfile"); | |
449 | } | |
450 | else | |
451 | { | |
452 | printf_filtered (" %18s %18s %10s %10s %s\n", | |
453 | "Start Addr", | |
454 | " End Addr", | |
455 | " Size", " Offset", "objfile"); | |
456 | } | |
457 | ||
458 | for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n")) | |
459 | { | |
460 | ULONGEST addr, endaddr, offset, inode; | |
461 | const char *permissions, *device, *filename; | |
462 | size_t permissions_len, device_len; | |
463 | ||
464 | read_mapping (line, &addr, &endaddr, | |
465 | &permissions, &permissions_len, | |
466 | &offset, &device, &device_len, | |
467 | &inode, &filename); | |
468 | ||
469 | if (gdbarch_addr_bit (gdbarch) == 32) | |
470 | { | |
471 | printf_filtered ("\t%10s %10s %10s %10s %s\n", | |
472 | paddress (gdbarch, addr), | |
473 | paddress (gdbarch, endaddr), | |
474 | hex_string (endaddr - addr), | |
475 | hex_string (offset), | |
476 | *filename? filename : ""); | |
477 | } | |
478 | else | |
479 | { | |
480 | printf_filtered (" %18s %18s %10s %10s %s\n", | |
481 | paddress (gdbarch, addr), | |
482 | paddress (gdbarch, endaddr), | |
483 | hex_string (endaddr - addr), | |
484 | hex_string (offset), | |
485 | *filename? filename : ""); | |
486 | } | |
487 | } | |
488 | ||
489 | do_cleanups (cleanup); | |
490 | } | |
491 | else | |
492 | warning (_("unable to open /proc file '%s'"), filename); | |
493 | } | |
494 | if (status_f) | |
495 | { | |
496 | xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid); | |
07c138c8 | 497 | data = target_fileio_read_stralloc (NULL, filename); |
3030c96e UW |
498 | if (data) |
499 | { | |
500 | struct cleanup *cleanup = make_cleanup (xfree, data); | |
501 | puts_filtered (data); | |
502 | do_cleanups (cleanup); | |
503 | } | |
504 | else | |
505 | warning (_("unable to open /proc file '%s'"), filename); | |
506 | } | |
507 | if (stat_f) | |
508 | { | |
509 | xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid); | |
07c138c8 | 510 | data = target_fileio_read_stralloc (NULL, filename); |
3030c96e UW |
511 | if (data) |
512 | { | |
513 | struct cleanup *cleanup = make_cleanup (xfree, data); | |
514 | const char *p = data; | |
3030c96e UW |
515 | |
516 | printf_filtered (_("Process: %s\n"), | |
517 | pulongest (strtoulst (p, &p, 10))); | |
518 | ||
529480d0 | 519 | p = skip_spaces_const (p); |
a71b5a38 | 520 | if (*p == '(') |
3030c96e | 521 | { |
184cd072 JK |
522 | /* ps command also relies on no trailing fields |
523 | ever contain ')'. */ | |
524 | const char *ep = strrchr (p, ')'); | |
a71b5a38 UW |
525 | if (ep != NULL) |
526 | { | |
527 | printf_filtered ("Exec file: %.*s\n", | |
528 | (int) (ep - p - 1), p + 1); | |
529 | p = ep + 1; | |
530 | } | |
3030c96e UW |
531 | } |
532 | ||
529480d0 | 533 | p = skip_spaces_const (p); |
3030c96e UW |
534 | if (*p) |
535 | printf_filtered (_("State: %c\n"), *p++); | |
536 | ||
537 | if (*p) | |
538 | printf_filtered (_("Parent process: %s\n"), | |
539 | pulongest (strtoulst (p, &p, 10))); | |
540 | if (*p) | |
541 | printf_filtered (_("Process group: %s\n"), | |
542 | pulongest (strtoulst (p, &p, 10))); | |
543 | if (*p) | |
544 | printf_filtered (_("Session id: %s\n"), | |
545 | pulongest (strtoulst (p, &p, 10))); | |
546 | if (*p) | |
547 | printf_filtered (_("TTY: %s\n"), | |
548 | pulongest (strtoulst (p, &p, 10))); | |
549 | if (*p) | |
550 | printf_filtered (_("TTY owner process group: %s\n"), | |
551 | pulongest (strtoulst (p, &p, 10))); | |
552 | ||
553 | if (*p) | |
554 | printf_filtered (_("Flags: %s\n"), | |
555 | hex_string (strtoulst (p, &p, 10))); | |
556 | if (*p) | |
557 | printf_filtered (_("Minor faults (no memory page): %s\n"), | |
558 | pulongest (strtoulst (p, &p, 10))); | |
559 | if (*p) | |
560 | printf_filtered (_("Minor faults, children: %s\n"), | |
561 | pulongest (strtoulst (p, &p, 10))); | |
562 | if (*p) | |
563 | printf_filtered (_("Major faults (memory page faults): %s\n"), | |
564 | pulongest (strtoulst (p, &p, 10))); | |
565 | if (*p) | |
566 | printf_filtered (_("Major faults, children: %s\n"), | |
567 | pulongest (strtoulst (p, &p, 10))); | |
568 | if (*p) | |
569 | printf_filtered (_("utime: %s\n"), | |
570 | pulongest (strtoulst (p, &p, 10))); | |
571 | if (*p) | |
572 | printf_filtered (_("stime: %s\n"), | |
573 | pulongest (strtoulst (p, &p, 10))); | |
574 | if (*p) | |
575 | printf_filtered (_("utime, children: %s\n"), | |
576 | pulongest (strtoulst (p, &p, 10))); | |
577 | if (*p) | |
578 | printf_filtered (_("stime, children: %s\n"), | |
579 | pulongest (strtoulst (p, &p, 10))); | |
580 | if (*p) | |
581 | printf_filtered (_("jiffies remaining in current " | |
582 | "time slice: %s\n"), | |
583 | pulongest (strtoulst (p, &p, 10))); | |
584 | if (*p) | |
585 | printf_filtered (_("'nice' value: %s\n"), | |
586 | pulongest (strtoulst (p, &p, 10))); | |
587 | if (*p) | |
588 | printf_filtered (_("jiffies until next timeout: %s\n"), | |
589 | pulongest (strtoulst (p, &p, 10))); | |
590 | if (*p) | |
591 | printf_filtered (_("jiffies until next SIGALRM: %s\n"), | |
592 | pulongest (strtoulst (p, &p, 10))); | |
593 | if (*p) | |
594 | printf_filtered (_("start time (jiffies since " | |
595 | "system boot): %s\n"), | |
596 | pulongest (strtoulst (p, &p, 10))); | |
597 | if (*p) | |
598 | printf_filtered (_("Virtual memory size: %s\n"), | |
599 | pulongest (strtoulst (p, &p, 10))); | |
600 | if (*p) | |
601 | printf_filtered (_("Resident set size: %s\n"), | |
602 | pulongest (strtoulst (p, &p, 10))); | |
603 | if (*p) | |
604 | printf_filtered (_("rlim: %s\n"), | |
605 | pulongest (strtoulst (p, &p, 10))); | |
606 | if (*p) | |
607 | printf_filtered (_("Start of text: %s\n"), | |
608 | hex_string (strtoulst (p, &p, 10))); | |
609 | if (*p) | |
610 | printf_filtered (_("End of text: %s\n"), | |
611 | hex_string (strtoulst (p, &p, 10))); | |
612 | if (*p) | |
613 | printf_filtered (_("Start of stack: %s\n"), | |
614 | hex_string (strtoulst (p, &p, 10))); | |
615 | #if 0 /* Don't know how architecture-dependent the rest is... | |
616 | Anyway the signal bitmap info is available from "status". */ | |
617 | if (*p) | |
618 | printf_filtered (_("Kernel stack pointer: %s\n"), | |
619 | hex_string (strtoulst (p, &p, 10))); | |
620 | if (*p) | |
621 | printf_filtered (_("Kernel instr pointer: %s\n"), | |
622 | hex_string (strtoulst (p, &p, 10))); | |
623 | if (*p) | |
624 | printf_filtered (_("Pending signals bitmap: %s\n"), | |
625 | hex_string (strtoulst (p, &p, 10))); | |
626 | if (*p) | |
627 | printf_filtered (_("Blocked signals bitmap: %s\n"), | |
628 | hex_string (strtoulst (p, &p, 10))); | |
629 | if (*p) | |
630 | printf_filtered (_("Ignored signals bitmap: %s\n"), | |
631 | hex_string (strtoulst (p, &p, 10))); | |
632 | if (*p) | |
633 | printf_filtered (_("Catched signals bitmap: %s\n"), | |
634 | hex_string (strtoulst (p, &p, 10))); | |
635 | if (*p) | |
636 | printf_filtered (_("wchan (system call): %s\n"), | |
637 | hex_string (strtoulst (p, &p, 10))); | |
638 | #endif | |
639 | do_cleanups (cleanup); | |
640 | } | |
641 | else | |
642 | warning (_("unable to open /proc file '%s'"), filename); | |
643 | } | |
644 | } | |
645 | ||
451b7c33 TT |
646 | /* Implement "info proc mappings" for a corefile. */ |
647 | ||
648 | static void | |
7bc112c1 | 649 | linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args) |
451b7c33 TT |
650 | { |
651 | asection *section; | |
652 | ULONGEST count, page_size; | |
653 | unsigned char *descdata, *filenames, *descend, *contents; | |
654 | size_t note_size; | |
655 | unsigned int addr_size_bits, addr_size; | |
656 | struct cleanup *cleanup; | |
657 | struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd); | |
658 | /* We assume this for reading 64-bit core files. */ | |
659 | gdb_static_assert (sizeof (ULONGEST) >= 8); | |
660 | ||
661 | section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file"); | |
662 | if (section == NULL) | |
663 | { | |
664 | warning (_("unable to find mappings in core file")); | |
665 | return; | |
666 | } | |
667 | ||
668 | addr_size_bits = gdbarch_addr_bit (core_gdbarch); | |
669 | addr_size = addr_size_bits / 8; | |
670 | note_size = bfd_get_section_size (section); | |
671 | ||
672 | if (note_size < 2 * addr_size) | |
673 | error (_("malformed core note - too short for header")); | |
674 | ||
675 | contents = xmalloc (note_size); | |
676 | cleanup = make_cleanup (xfree, contents); | |
677 | if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size)) | |
678 | error (_("could not get core note contents")); | |
679 | ||
680 | descdata = contents; | |
681 | descend = descdata + note_size; | |
682 | ||
683 | if (descdata[note_size - 1] != '\0') | |
684 | error (_("malformed note - does not end with \\0")); | |
685 | ||
686 | count = bfd_get (addr_size_bits, core_bfd, descdata); | |
687 | descdata += addr_size; | |
688 | ||
689 | page_size = bfd_get (addr_size_bits, core_bfd, descdata); | |
690 | descdata += addr_size; | |
691 | ||
692 | if (note_size < 2 * addr_size + count * 3 * addr_size) | |
693 | error (_("malformed note - too short for supplied file count")); | |
694 | ||
695 | printf_filtered (_("Mapped address spaces:\n\n")); | |
696 | if (gdbarch_addr_bit (gdbarch) == 32) | |
697 | { | |
698 | printf_filtered ("\t%10s %10s %10s %10s %s\n", | |
699 | "Start Addr", | |
700 | " End Addr", | |
701 | " Size", " Offset", "objfile"); | |
702 | } | |
703 | else | |
704 | { | |
705 | printf_filtered (" %18s %18s %10s %10s %s\n", | |
706 | "Start Addr", | |
707 | " End Addr", | |
708 | " Size", " Offset", "objfile"); | |
709 | } | |
710 | ||
711 | filenames = descdata + count * 3 * addr_size; | |
712 | while (--count > 0) | |
713 | { | |
714 | ULONGEST start, end, file_ofs; | |
715 | ||
716 | if (filenames == descend) | |
717 | error (_("malformed note - filenames end too early")); | |
718 | ||
719 | start = bfd_get (addr_size_bits, core_bfd, descdata); | |
720 | descdata += addr_size; | |
721 | end = bfd_get (addr_size_bits, core_bfd, descdata); | |
722 | descdata += addr_size; | |
723 | file_ofs = bfd_get (addr_size_bits, core_bfd, descdata); | |
724 | descdata += addr_size; | |
725 | ||
726 | file_ofs *= page_size; | |
727 | ||
728 | if (gdbarch_addr_bit (gdbarch) == 32) | |
729 | printf_filtered ("\t%10s %10s %10s %10s %s\n", | |
730 | paddress (gdbarch, start), | |
731 | paddress (gdbarch, end), | |
732 | hex_string (end - start), | |
733 | hex_string (file_ofs), | |
734 | filenames); | |
735 | else | |
736 | printf_filtered (" %18s %18s %10s %10s %s\n", | |
737 | paddress (gdbarch, start), | |
738 | paddress (gdbarch, end), | |
739 | hex_string (end - start), | |
740 | hex_string (file_ofs), | |
741 | filenames); | |
742 | ||
743 | filenames += 1 + strlen ((char *) filenames); | |
744 | } | |
745 | ||
746 | do_cleanups (cleanup); | |
747 | } | |
748 | ||
749 | /* Implement "info proc" for a corefile. */ | |
750 | ||
751 | static void | |
7bc112c1 | 752 | linux_core_info_proc (struct gdbarch *gdbarch, const char *args, |
451b7c33 TT |
753 | enum info_proc_what what) |
754 | { | |
755 | int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL); | |
756 | int mappings_f = (what == IP_MAPPINGS || what == IP_ALL); | |
757 | ||
758 | if (exe_f) | |
759 | { | |
760 | const char *exe; | |
761 | ||
762 | exe = bfd_core_file_failing_command (core_bfd); | |
763 | if (exe != NULL) | |
764 | printf_filtered ("exe = '%s'\n", exe); | |
765 | else | |
766 | warning (_("unable to find command name in core file")); | |
767 | } | |
768 | ||
769 | if (mappings_f) | |
770 | linux_core_info_proc_mappings (gdbarch, args); | |
771 | ||
772 | if (!exe_f && !mappings_f) | |
773 | error (_("unable to handle request")); | |
774 | } | |
775 | ||
451b7c33 TT |
776 | /* A structure for passing information through |
777 | linux_find_memory_regions_full. */ | |
778 | ||
779 | struct linux_find_memory_regions_data | |
780 | { | |
781 | /* The original callback. */ | |
782 | ||
783 | find_memory_region_ftype func; | |
784 | ||
785 | /* The original datum. */ | |
786 | ||
f7af1fcd | 787 | void *data; |
451b7c33 TT |
788 | }; |
789 | ||
790 | /* A callback for linux_find_memory_regions that converts between the | |
791 | "full"-style callback and find_memory_region_ftype. */ | |
792 | ||
793 | static int | |
794 | linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size, | |
795 | ULONGEST offset, ULONGEST inode, | |
796 | int read, int write, int exec, int modified, | |
797 | const char *filename, void *arg) | |
798 | { | |
799 | struct linux_find_memory_regions_data *data = arg; | |
800 | ||
f7af1fcd JK |
801 | return data->func (vaddr, size, read, write, exec, modified, data->data); |
802 | } | |
803 | ||
804 | /* Wrapper of linux_find_memory_regions_full handling FAKE_PID_P in GDB. */ | |
805 | ||
806 | static int | |
807 | linux_find_memory_regions_gdb (struct gdbarch *gdbarch, | |
808 | linux_find_memory_region_ftype *func, | |
809 | void *func_data) | |
810 | { | |
811 | pid_t pid; | |
812 | /* Default dump behavior of coredump_filter (0x33), according to | |
813 | Documentation/filesystems/proc.txt from the Linux kernel | |
814 | tree. */ | |
815 | enum filterflags filterflags = (COREFILTER_ANON_PRIVATE | |
816 | | COREFILTER_ANON_SHARED | |
817 | | COREFILTER_ELF_HEADERS | |
818 | | COREFILTER_HUGETLB_PRIVATE); | |
819 | ||
820 | /* We need to know the real target PID so | |
821 | linux_find_memory_regions_full can access /proc. */ | |
822 | if (current_inferior ()->fake_pid_p) | |
823 | return -1; | |
824 | ||
825 | pid = current_inferior ()->pid; | |
826 | ||
827 | if (use_coredump_filter) | |
828 | { | |
829 | char coredumpfilter_name[100], *coredumpfilterdata; | |
830 | ||
831 | xsnprintf (coredumpfilter_name, sizeof (coredumpfilter_name), | |
832 | "/proc/%d/coredump_filter", pid); | |
833 | coredumpfilterdata = target_fileio_read_stralloc (NULL, | |
834 | coredumpfilter_name); | |
835 | if (coredumpfilterdata != NULL) | |
836 | { | |
837 | sscanf (coredumpfilterdata, "%x", &filterflags); | |
838 | xfree (coredumpfilterdata); | |
839 | } | |
840 | } | |
841 | ||
842 | return linux_find_memory_regions_full (pid, filterflags, func, func_data); | |
451b7c33 TT |
843 | } |
844 | ||
845 | /* A variant of linux_find_memory_regions_full that is suitable as the | |
846 | gdbarch find_memory_regions method. */ | |
847 | ||
848 | static int | |
849 | linux_find_memory_regions (struct gdbarch *gdbarch, | |
f7af1fcd | 850 | find_memory_region_ftype func, void *func_data) |
451b7c33 TT |
851 | { |
852 | struct linux_find_memory_regions_data data; | |
853 | ||
854 | data.func = func; | |
f7af1fcd | 855 | data.data = func_data; |
451b7c33 | 856 | |
f7af1fcd JK |
857 | return linux_find_memory_regions_gdb (gdbarch, |
858 | linux_find_memory_regions_thunk, &data); | |
451b7c33 TT |
859 | } |
860 | ||
6432734d UW |
861 | /* Determine which signal stopped execution. */ |
862 | ||
863 | static int | |
864 | find_signalled_thread (struct thread_info *info, void *data) | |
865 | { | |
a493e3e2 | 866 | if (info->suspend.stop_signal != GDB_SIGNAL_0 |
6432734d UW |
867 | && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid)) |
868 | return 1; | |
869 | ||
870 | return 0; | |
871 | } | |
872 | ||
2ea28649 | 873 | static enum gdb_signal |
6432734d UW |
874 | find_stop_signal (void) |
875 | { | |
876 | struct thread_info *info = | |
877 | iterate_over_threads (find_signalled_thread, NULL); | |
878 | ||
879 | if (info) | |
880 | return info->suspend.stop_signal; | |
881 | else | |
a493e3e2 | 882 | return GDB_SIGNAL_0; |
6432734d UW |
883 | } |
884 | ||
885 | /* Generate corefile notes for SPU contexts. */ | |
886 | ||
887 | static char * | |
888 | linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size) | |
889 | { | |
890 | static const char *spu_files[] = | |
891 | { | |
892 | "object-id", | |
893 | "mem", | |
894 | "regs", | |
895 | "fpcr", | |
896 | "lslr", | |
897 | "decr", | |
898 | "decr_status", | |
899 | "signal1", | |
900 | "signal1_type", | |
901 | "signal2", | |
902 | "signal2_type", | |
903 | "event_mask", | |
904 | "event_status", | |
905 | "mbox_info", | |
906 | "ibox_info", | |
907 | "wbox_info", | |
908 | "dma_info", | |
909 | "proxydma_info", | |
910 | }; | |
911 | ||
f5656ead | 912 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
6432734d UW |
913 | gdb_byte *spu_ids; |
914 | LONGEST i, j, size; | |
915 | ||
916 | /* Determine list of SPU ids. */ | |
917 | size = target_read_alloc (¤t_target, TARGET_OBJECT_SPU, | |
918 | NULL, &spu_ids); | |
919 | ||
920 | /* Generate corefile notes for each SPU file. */ | |
921 | for (i = 0; i < size; i += 4) | |
922 | { | |
923 | int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order); | |
924 | ||
925 | for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++) | |
926 | { | |
927 | char annex[32], note_name[32]; | |
928 | gdb_byte *spu_data; | |
929 | LONGEST spu_len; | |
930 | ||
931 | xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]); | |
932 | spu_len = target_read_alloc (¤t_target, TARGET_OBJECT_SPU, | |
933 | annex, &spu_data); | |
934 | if (spu_len > 0) | |
935 | { | |
936 | xsnprintf (note_name, sizeof note_name, "SPU/%s", annex); | |
937 | note_data = elfcore_write_note (obfd, note_data, note_size, | |
938 | note_name, NT_SPU, | |
939 | spu_data, spu_len); | |
940 | xfree (spu_data); | |
941 | ||
942 | if (!note_data) | |
943 | { | |
944 | xfree (spu_ids); | |
945 | return NULL; | |
946 | } | |
947 | } | |
948 | } | |
949 | } | |
950 | ||
951 | if (size > 0) | |
952 | xfree (spu_ids); | |
953 | ||
954 | return note_data; | |
955 | } | |
956 | ||
451b7c33 TT |
957 | /* This is used to pass information from |
958 | linux_make_mappings_corefile_notes through | |
959 | linux_find_memory_regions_full. */ | |
960 | ||
961 | struct linux_make_mappings_data | |
962 | { | |
963 | /* Number of files mapped. */ | |
964 | ULONGEST file_count; | |
965 | ||
966 | /* The obstack for the main part of the data. */ | |
967 | struct obstack *data_obstack; | |
968 | ||
969 | /* The filename obstack. */ | |
970 | struct obstack *filename_obstack; | |
971 | ||
972 | /* The architecture's "long" type. */ | |
973 | struct type *long_type; | |
974 | }; | |
975 | ||
976 | static linux_find_memory_region_ftype linux_make_mappings_callback; | |
977 | ||
978 | /* A callback for linux_find_memory_regions_full that updates the | |
979 | mappings data for linux_make_mappings_corefile_notes. */ | |
980 | ||
981 | static int | |
982 | linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size, | |
983 | ULONGEST offset, ULONGEST inode, | |
984 | int read, int write, int exec, int modified, | |
985 | const char *filename, void *data) | |
986 | { | |
987 | struct linux_make_mappings_data *map_data = data; | |
988 | gdb_byte buf[sizeof (ULONGEST)]; | |
989 | ||
990 | if (*filename == '\0' || inode == 0) | |
991 | return 0; | |
992 | ||
993 | ++map_data->file_count; | |
994 | ||
995 | pack_long (buf, map_data->long_type, vaddr); | |
996 | obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type)); | |
997 | pack_long (buf, map_data->long_type, vaddr + size); | |
998 | obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type)); | |
999 | pack_long (buf, map_data->long_type, offset); | |
1000 | obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type)); | |
1001 | ||
1002 | obstack_grow_str0 (map_data->filename_obstack, filename); | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | /* Write the file mapping data to the core file, if possible. OBFD is | |
1008 | the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE | |
1009 | is a pointer to the note size. Returns the new NOTE_DATA and | |
1010 | updates NOTE_SIZE. */ | |
1011 | ||
1012 | static char * | |
1013 | linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, | |
1014 | char *note_data, int *note_size) | |
1015 | { | |
1016 | struct cleanup *cleanup; | |
1017 | struct obstack data_obstack, filename_obstack; | |
1018 | struct linux_make_mappings_data mapping_data; | |
1019 | struct type *long_type | |
1020 | = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long"); | |
1021 | gdb_byte buf[sizeof (ULONGEST)]; | |
1022 | ||
1023 | obstack_init (&data_obstack); | |
1024 | cleanup = make_cleanup_obstack_free (&data_obstack); | |
1025 | obstack_init (&filename_obstack); | |
1026 | make_cleanup_obstack_free (&filename_obstack); | |
1027 | ||
1028 | mapping_data.file_count = 0; | |
1029 | mapping_data.data_obstack = &data_obstack; | |
1030 | mapping_data.filename_obstack = &filename_obstack; | |
1031 | mapping_data.long_type = long_type; | |
1032 | ||
1033 | /* Reserve space for the count. */ | |
1034 | obstack_blank (&data_obstack, TYPE_LENGTH (long_type)); | |
1035 | /* We always write the page size as 1 since we have no good way to | |
1036 | determine the correct value. */ | |
1037 | pack_long (buf, long_type, 1); | |
1038 | obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type)); | |
1039 | ||
f7af1fcd JK |
1040 | linux_find_memory_regions_gdb (gdbarch, linux_make_mappings_callback, |
1041 | &mapping_data); | |
451b7c33 TT |
1042 | |
1043 | if (mapping_data.file_count != 0) | |
1044 | { | |
1045 | /* Write the count to the obstack. */ | |
51a5cd90 PA |
1046 | pack_long ((gdb_byte *) obstack_base (&data_obstack), |
1047 | long_type, mapping_data.file_count); | |
451b7c33 TT |
1048 | |
1049 | /* Copy the filenames to the data obstack. */ | |
1050 | obstack_grow (&data_obstack, obstack_base (&filename_obstack), | |
1051 | obstack_object_size (&filename_obstack)); | |
1052 | ||
1053 | note_data = elfcore_write_note (obfd, note_data, note_size, | |
1054 | "CORE", NT_FILE, | |
1055 | obstack_base (&data_obstack), | |
1056 | obstack_object_size (&data_obstack)); | |
1057 | } | |
1058 | ||
1059 | do_cleanups (cleanup); | |
1060 | return note_data; | |
1061 | } | |
1062 | ||
5aa82d05 AA |
1063 | /* Structure for passing information from |
1064 | linux_collect_thread_registers via an iterator to | |
1065 | linux_collect_regset_section_cb. */ | |
1066 | ||
1067 | struct linux_collect_regset_section_cb_data | |
1068 | { | |
1069 | struct gdbarch *gdbarch; | |
1070 | const struct regcache *regcache; | |
1071 | bfd *obfd; | |
1072 | char *note_data; | |
1073 | int *note_size; | |
1074 | unsigned long lwp; | |
1075 | enum gdb_signal stop_signal; | |
1076 | int abort_iteration; | |
1077 | }; | |
1078 | ||
1079 | /* Callback for iterate_over_regset_sections that records a single | |
1080 | regset in the corefile note section. */ | |
1081 | ||
1082 | static void | |
1083 | linux_collect_regset_section_cb (const char *sect_name, int size, | |
8f0435f7 | 1084 | const struct regset *regset, |
5aa82d05 AA |
1085 | const char *human_name, void *cb_data) |
1086 | { | |
5aa82d05 AA |
1087 | char *buf; |
1088 | struct linux_collect_regset_section_cb_data *data = cb_data; | |
1089 | ||
1090 | if (data->abort_iteration) | |
1091 | return; | |
1092 | ||
5aa82d05 AA |
1093 | gdb_assert (regset && regset->collect_regset); |
1094 | ||
1095 | buf = xmalloc (size); | |
1096 | regset->collect_regset (regset, data->regcache, -1, buf, size); | |
1097 | ||
1098 | /* PRSTATUS still needs to be treated specially. */ | |
1099 | if (strcmp (sect_name, ".reg") == 0) | |
1100 | data->note_data = (char *) elfcore_write_prstatus | |
1101 | (data->obfd, data->note_data, data->note_size, data->lwp, | |
1102 | gdb_signal_to_host (data->stop_signal), buf); | |
1103 | else | |
1104 | data->note_data = (char *) elfcore_write_register_note | |
1105 | (data->obfd, data->note_data, data->note_size, | |
1106 | sect_name, buf, size); | |
1107 | xfree (buf); | |
1108 | ||
1109 | if (data->note_data == NULL) | |
1110 | data->abort_iteration = 1; | |
1111 | } | |
1112 | ||
6432734d UW |
1113 | /* Records the thread's register state for the corefile note |
1114 | section. */ | |
1115 | ||
1116 | static char * | |
1117 | linux_collect_thread_registers (const struct regcache *regcache, | |
1118 | ptid_t ptid, bfd *obfd, | |
1119 | char *note_data, int *note_size, | |
2ea28649 | 1120 | enum gdb_signal stop_signal) |
6432734d UW |
1121 | { |
1122 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
5aa82d05 | 1123 | struct linux_collect_regset_section_cb_data data; |
6432734d | 1124 | |
5aa82d05 AA |
1125 | data.gdbarch = gdbarch; |
1126 | data.regcache = regcache; | |
1127 | data.obfd = obfd; | |
1128 | data.note_data = note_data; | |
1129 | data.note_size = note_size; | |
1130 | data.stop_signal = stop_signal; | |
1131 | data.abort_iteration = 0; | |
6432734d UW |
1132 | |
1133 | /* For remote targets the LWP may not be available, so use the TID. */ | |
5aa82d05 AA |
1134 | data.lwp = ptid_get_lwp (ptid); |
1135 | if (!data.lwp) | |
1136 | data.lwp = ptid_get_tid (ptid); | |
1137 | ||
1138 | gdbarch_iterate_over_regset_sections (gdbarch, | |
1139 | linux_collect_regset_section_cb, | |
1140 | &data, regcache); | |
1141 | return data.note_data; | |
6432734d UW |
1142 | } |
1143 | ||
9015683b TT |
1144 | /* Fetch the siginfo data for the current thread, if it exists. If |
1145 | there is no data, or we could not read it, return NULL. Otherwise, | |
1146 | return a newly malloc'd buffer holding the data and fill in *SIZE | |
1147 | with the size of the data. The caller is responsible for freeing | |
1148 | the data. */ | |
1149 | ||
1150 | static gdb_byte * | |
1151 | linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size) | |
1152 | { | |
1153 | struct type *siginfo_type; | |
1154 | gdb_byte *buf; | |
1155 | LONGEST bytes_read; | |
1156 | struct cleanup *cleanups; | |
1157 | ||
1158 | if (!gdbarch_get_siginfo_type_p (gdbarch)) | |
1159 | return NULL; | |
1160 | ||
1161 | siginfo_type = gdbarch_get_siginfo_type (gdbarch); | |
1162 | ||
1163 | buf = xmalloc (TYPE_LENGTH (siginfo_type)); | |
1164 | cleanups = make_cleanup (xfree, buf); | |
1165 | ||
1166 | bytes_read = target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL, | |
1167 | buf, 0, TYPE_LENGTH (siginfo_type)); | |
1168 | if (bytes_read == TYPE_LENGTH (siginfo_type)) | |
1169 | { | |
1170 | discard_cleanups (cleanups); | |
1171 | *size = bytes_read; | |
1172 | } | |
1173 | else | |
1174 | { | |
1175 | do_cleanups (cleanups); | |
1176 | buf = NULL; | |
1177 | } | |
1178 | ||
1179 | return buf; | |
1180 | } | |
1181 | ||
6432734d UW |
1182 | struct linux_corefile_thread_data |
1183 | { | |
1184 | struct gdbarch *gdbarch; | |
1185 | int pid; | |
1186 | bfd *obfd; | |
1187 | char *note_data; | |
1188 | int *note_size; | |
2ea28649 | 1189 | enum gdb_signal stop_signal; |
6432734d UW |
1190 | }; |
1191 | ||
1192 | /* Called by gdbthread.c once per thread. Records the thread's | |
1193 | register state for the corefile note section. */ | |
1194 | ||
1195 | static int | |
1196 | linux_corefile_thread_callback (struct thread_info *info, void *data) | |
1197 | { | |
1198 | struct linux_corefile_thread_data *args = data; | |
1199 | ||
22fd09ae JK |
1200 | /* It can be current thread |
1201 | which cannot be removed by update_thread_list. */ | |
1202 | if (info->state == THREAD_EXITED) | |
1203 | return 0; | |
1204 | ||
6432734d UW |
1205 | if (ptid_get_pid (info->ptid) == args->pid) |
1206 | { | |
1207 | struct cleanup *old_chain; | |
1208 | struct regcache *regcache; | |
9015683b | 1209 | gdb_byte *siginfo_data; |
e17aaa33 | 1210 | LONGEST siginfo_size = 0; |
9015683b | 1211 | |
6432734d UW |
1212 | regcache = get_thread_arch_regcache (info->ptid, args->gdbarch); |
1213 | ||
1214 | old_chain = save_inferior_ptid (); | |
1215 | inferior_ptid = info->ptid; | |
1216 | target_fetch_registers (regcache, -1); | |
9015683b | 1217 | siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size); |
6432734d UW |
1218 | do_cleanups (old_chain); |
1219 | ||
9015683b TT |
1220 | old_chain = make_cleanup (xfree, siginfo_data); |
1221 | ||
f968fe80 AA |
1222 | args->note_data = linux_collect_thread_registers |
1223 | (regcache, info->ptid, args->obfd, args->note_data, | |
1224 | args->note_size, args->stop_signal); | |
9015683b | 1225 | |
bbe769cc MR |
1226 | /* Don't return anything if we got no register information above, |
1227 | such a core file is useless. */ | |
1228 | if (args->note_data != NULL) | |
1229 | if (siginfo_data != NULL) | |
9015683b TT |
1230 | args->note_data = elfcore_write_note (args->obfd, |
1231 | args->note_data, | |
1232 | args->note_size, | |
1233 | "CORE", NT_SIGINFO, | |
1234 | siginfo_data, siginfo_size); | |
9015683b TT |
1235 | |
1236 | do_cleanups (old_chain); | |
6432734d UW |
1237 | } |
1238 | ||
1239 | return !args->note_data; | |
1240 | } | |
1241 | ||
b3ac9c77 SDJ |
1242 | /* Fill the PRPSINFO structure with information about the process being |
1243 | debugged. Returns 1 in case of success, 0 for failures. Please note that | |
1244 | even if the structure cannot be entirely filled (e.g., GDB was unable to | |
1245 | gather information about the process UID/GID), this function will still | |
1246 | return 1 since some information was already recorded. It will only return | |
1247 | 0 iff nothing can be gathered. */ | |
1248 | ||
1249 | static int | |
1250 | linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p) | |
1251 | { | |
1252 | /* The filename which we will use to obtain some info about the process. | |
1253 | We will basically use this to store the `/proc/PID/FILENAME' file. */ | |
1254 | char filename[100]; | |
1255 | /* The full name of the program which generated the corefile. */ | |
1256 | char *fname; | |
1257 | /* The basename of the executable. */ | |
1258 | const char *basename; | |
1259 | /* The arguments of the program. */ | |
1260 | char *psargs; | |
1261 | char *infargs; | |
1262 | /* The contents of `/proc/PID/stat' and `/proc/PID/status' files. */ | |
1263 | char *proc_stat, *proc_status; | |
1264 | /* Temporary buffer. */ | |
1265 | char *tmpstr; | |
1266 | /* The valid states of a process, according to the Linux kernel. */ | |
1267 | const char valid_states[] = "RSDTZW"; | |
1268 | /* The program state. */ | |
1269 | const char *prog_state; | |
1270 | /* The state of the process. */ | |
1271 | char pr_sname; | |
1272 | /* The PID of the program which generated the corefile. */ | |
1273 | pid_t pid; | |
1274 | /* Process flags. */ | |
1275 | unsigned int pr_flag; | |
1276 | /* Process nice value. */ | |
1277 | long pr_nice; | |
1278 | /* The number of fields read by `sscanf'. */ | |
1279 | int n_fields = 0; | |
1280 | /* Cleanups. */ | |
1281 | struct cleanup *c; | |
1282 | int i; | |
1283 | ||
1284 | gdb_assert (p != NULL); | |
1285 | ||
1286 | /* Obtaining PID and filename. */ | |
1287 | pid = ptid_get_pid (inferior_ptid); | |
1288 | xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid); | |
07c138c8 | 1289 | fname = target_fileio_read_stralloc (NULL, filename); |
b3ac9c77 SDJ |
1290 | |
1291 | if (fname == NULL || *fname == '\0') | |
1292 | { | |
1293 | /* No program name was read, so we won't be able to retrieve more | |
1294 | information about the process. */ | |
1295 | xfree (fname); | |
1296 | return 0; | |
1297 | } | |
1298 | ||
1299 | c = make_cleanup (xfree, fname); | |
1300 | memset (p, 0, sizeof (*p)); | |
1301 | ||
1302 | /* Defining the PID. */ | |
1303 | p->pr_pid = pid; | |
1304 | ||
1305 | /* Copying the program name. Only the basename matters. */ | |
1306 | basename = lbasename (fname); | |
1307 | strncpy (p->pr_fname, basename, sizeof (p->pr_fname)); | |
1308 | p->pr_fname[sizeof (p->pr_fname) - 1] = '\0'; | |
1309 | ||
1310 | infargs = get_inferior_args (); | |
1311 | ||
1312 | psargs = xstrdup (fname); | |
1313 | if (infargs != NULL) | |
1314 | psargs = reconcat (psargs, psargs, " ", infargs, NULL); | |
1315 | ||
1316 | make_cleanup (xfree, psargs); | |
1317 | ||
1318 | strncpy (p->pr_psargs, psargs, sizeof (p->pr_psargs)); | |
1319 | p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0'; | |
1320 | ||
1321 | xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid); | |
07c138c8 | 1322 | proc_stat = target_fileio_read_stralloc (NULL, filename); |
b3ac9c77 SDJ |
1323 | make_cleanup (xfree, proc_stat); |
1324 | ||
1325 | if (proc_stat == NULL || *proc_stat == '\0') | |
1326 | { | |
1327 | /* Despite being unable to read more information about the | |
1328 | process, we return 1 here because at least we have its | |
1329 | command line, PID and arguments. */ | |
1330 | do_cleanups (c); | |
1331 | return 1; | |
1332 | } | |
1333 | ||
1334 | /* Ok, we have the stats. It's time to do a little parsing of the | |
1335 | contents of the buffer, so that we end up reading what we want. | |
1336 | ||
1337 | The following parsing mechanism is strongly based on the | |
1338 | information generated by the `fs/proc/array.c' file, present in | |
1339 | the Linux kernel tree. More details about how the information is | |
1340 | displayed can be obtained by seeing the manpage of proc(5), | |
1341 | specifically under the entry of `/proc/[pid]/stat'. */ | |
1342 | ||
1343 | /* Getting rid of the PID, since we already have it. */ | |
1344 | while (isdigit (*proc_stat)) | |
1345 | ++proc_stat; | |
1346 | ||
1347 | proc_stat = skip_spaces (proc_stat); | |
1348 | ||
184cd072 JK |
1349 | /* ps command also relies on no trailing fields ever contain ')'. */ |
1350 | proc_stat = strrchr (proc_stat, ')'); | |
1351 | if (proc_stat == NULL) | |
1352 | { | |
1353 | do_cleanups (c); | |
1354 | return 1; | |
1355 | } | |
1356 | proc_stat++; | |
b3ac9c77 SDJ |
1357 | |
1358 | proc_stat = skip_spaces (proc_stat); | |
1359 | ||
1360 | n_fields = sscanf (proc_stat, | |
1361 | "%c" /* Process state. */ | |
1362 | "%d%d%d" /* Parent PID, group ID, session ID. */ | |
1363 | "%*d%*d" /* tty_nr, tpgid (not used). */ | |
1364 | "%u" /* Flags. */ | |
1365 | "%*s%*s%*s%*s" /* minflt, cminflt, majflt, | |
1366 | cmajflt (not used). */ | |
1367 | "%*s%*s%*s%*s" /* utime, stime, cutime, | |
1368 | cstime (not used). */ | |
1369 | "%*s" /* Priority (not used). */ | |
1370 | "%ld", /* Nice. */ | |
1371 | &pr_sname, | |
1372 | &p->pr_ppid, &p->pr_pgrp, &p->pr_sid, | |
1373 | &pr_flag, | |
1374 | &pr_nice); | |
1375 | ||
1376 | if (n_fields != 6) | |
1377 | { | |
1378 | /* Again, we couldn't read the complementary information about | |
1379 | the process state. However, we already have minimal | |
1380 | information, so we just return 1 here. */ | |
1381 | do_cleanups (c); | |
1382 | return 1; | |
1383 | } | |
1384 | ||
1385 | /* Filling the structure fields. */ | |
1386 | prog_state = strchr (valid_states, pr_sname); | |
1387 | if (prog_state != NULL) | |
1388 | p->pr_state = prog_state - valid_states; | |
1389 | else | |
1390 | { | |
1391 | /* Zero means "Running". */ | |
1392 | p->pr_state = 0; | |
1393 | } | |
1394 | ||
1395 | p->pr_sname = p->pr_state > 5 ? '.' : pr_sname; | |
1396 | p->pr_zomb = p->pr_sname == 'Z'; | |
1397 | p->pr_nice = pr_nice; | |
1398 | p->pr_flag = pr_flag; | |
1399 | ||
1400 | /* Finally, obtaining the UID and GID. For that, we read and parse the | |
1401 | contents of the `/proc/PID/status' file. */ | |
1402 | xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid); | |
07c138c8 | 1403 | proc_status = target_fileio_read_stralloc (NULL, filename); |
b3ac9c77 SDJ |
1404 | make_cleanup (xfree, proc_status); |
1405 | ||
1406 | if (proc_status == NULL || *proc_status == '\0') | |
1407 | { | |
1408 | /* Returning 1 since we already have a bunch of information. */ | |
1409 | do_cleanups (c); | |
1410 | return 1; | |
1411 | } | |
1412 | ||
1413 | /* Extracting the UID. */ | |
1414 | tmpstr = strstr (proc_status, "Uid:"); | |
1415 | if (tmpstr != NULL) | |
1416 | { | |
1417 | /* Advancing the pointer to the beginning of the UID. */ | |
1418 | tmpstr += sizeof ("Uid:"); | |
1419 | while (*tmpstr != '\0' && !isdigit (*tmpstr)) | |
1420 | ++tmpstr; | |
1421 | ||
1422 | if (isdigit (*tmpstr)) | |
1423 | p->pr_uid = strtol (tmpstr, &tmpstr, 10); | |
1424 | } | |
1425 | ||
1426 | /* Extracting the GID. */ | |
1427 | tmpstr = strstr (proc_status, "Gid:"); | |
1428 | if (tmpstr != NULL) | |
1429 | { | |
1430 | /* Advancing the pointer to the beginning of the GID. */ | |
1431 | tmpstr += sizeof ("Gid:"); | |
1432 | while (*tmpstr != '\0' && !isdigit (*tmpstr)) | |
1433 | ++tmpstr; | |
1434 | ||
1435 | if (isdigit (*tmpstr)) | |
1436 | p->pr_gid = strtol (tmpstr, &tmpstr, 10); | |
1437 | } | |
1438 | ||
1439 | do_cleanups (c); | |
1440 | ||
1441 | return 1; | |
1442 | } | |
1443 | ||
f968fe80 AA |
1444 | /* Build the note section for a corefile, and return it in a malloc |
1445 | buffer. */ | |
6432734d | 1446 | |
f968fe80 AA |
1447 | static char * |
1448 | linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size) | |
6432734d UW |
1449 | { |
1450 | struct linux_corefile_thread_data thread_args; | |
b3ac9c77 | 1451 | struct elf_internal_linux_prpsinfo prpsinfo; |
6432734d UW |
1452 | char *note_data = NULL; |
1453 | gdb_byte *auxv; | |
1454 | int auxv_len; | |
1455 | ||
f968fe80 AA |
1456 | if (! gdbarch_iterate_over_regset_sections_p (gdbarch)) |
1457 | return NULL; | |
1458 | ||
b3ac9c77 | 1459 | if (linux_fill_prpsinfo (&prpsinfo)) |
6432734d | 1460 | { |
b3ac9c77 SDJ |
1461 | if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch)) |
1462 | { | |
1463 | note_data = gdbarch_elfcore_write_linux_prpsinfo (gdbarch, obfd, | |
1464 | note_data, note_size, | |
1465 | &prpsinfo); | |
1466 | } | |
1467 | else | |
1468 | { | |
1469 | if (gdbarch_ptr_bit (gdbarch) == 64) | |
1470 | note_data = elfcore_write_linux_prpsinfo64 (obfd, | |
1471 | note_data, note_size, | |
1472 | &prpsinfo); | |
1473 | else | |
1474 | note_data = elfcore_write_linux_prpsinfo32 (obfd, | |
1475 | note_data, note_size, | |
1476 | &prpsinfo); | |
1477 | } | |
6432734d UW |
1478 | } |
1479 | ||
1480 | /* Thread register information. */ | |
492d29ea | 1481 | TRY |
22fd09ae JK |
1482 | { |
1483 | update_thread_list (); | |
1484 | } | |
492d29ea PA |
1485 | CATCH (e, RETURN_MASK_ERROR) |
1486 | { | |
1487 | exception_print (gdb_stderr, e); | |
1488 | } | |
1489 | END_CATCH | |
1490 | ||
6432734d UW |
1491 | thread_args.gdbarch = gdbarch; |
1492 | thread_args.pid = ptid_get_pid (inferior_ptid); | |
1493 | thread_args.obfd = obfd; | |
1494 | thread_args.note_data = note_data; | |
1495 | thread_args.note_size = note_size; | |
6432734d | 1496 | thread_args.stop_signal = find_stop_signal (); |
6432734d UW |
1497 | iterate_over_threads (linux_corefile_thread_callback, &thread_args); |
1498 | note_data = thread_args.note_data; | |
1499 | if (!note_data) | |
1500 | return NULL; | |
1501 | ||
1502 | /* Auxillary vector. */ | |
1503 | auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV, | |
1504 | NULL, &auxv); | |
1505 | if (auxv_len > 0) | |
1506 | { | |
1507 | note_data = elfcore_write_note (obfd, note_data, note_size, | |
1508 | "CORE", NT_AUXV, auxv, auxv_len); | |
1509 | xfree (auxv); | |
1510 | ||
1511 | if (!note_data) | |
1512 | return NULL; | |
1513 | } | |
1514 | ||
1515 | /* SPU information. */ | |
1516 | note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size); | |
1517 | if (!note_data) | |
1518 | return NULL; | |
1519 | ||
451b7c33 TT |
1520 | /* File mappings. */ |
1521 | note_data = linux_make_mappings_corefile_notes (gdbarch, obfd, | |
1522 | note_data, note_size); | |
1523 | ||
6432734d UW |
1524 | return note_data; |
1525 | } | |
1526 | ||
eb14d406 SDJ |
1527 | /* Implementation of `gdbarch_gdb_signal_from_target', as defined in |
1528 | gdbarch.h. This function is not static because it is exported to | |
1529 | other -tdep files. */ | |
1530 | ||
1531 | enum gdb_signal | |
1532 | linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal) | |
1533 | { | |
1534 | switch (signal) | |
1535 | { | |
1536 | case 0: | |
1537 | return GDB_SIGNAL_0; | |
1538 | ||
1539 | case LINUX_SIGHUP: | |
1540 | return GDB_SIGNAL_HUP; | |
1541 | ||
1542 | case LINUX_SIGINT: | |
1543 | return GDB_SIGNAL_INT; | |
1544 | ||
1545 | case LINUX_SIGQUIT: | |
1546 | return GDB_SIGNAL_QUIT; | |
1547 | ||
1548 | case LINUX_SIGILL: | |
1549 | return GDB_SIGNAL_ILL; | |
1550 | ||
1551 | case LINUX_SIGTRAP: | |
1552 | return GDB_SIGNAL_TRAP; | |
1553 | ||
1554 | case LINUX_SIGABRT: | |
1555 | return GDB_SIGNAL_ABRT; | |
1556 | ||
1557 | case LINUX_SIGBUS: | |
1558 | return GDB_SIGNAL_BUS; | |
1559 | ||
1560 | case LINUX_SIGFPE: | |
1561 | return GDB_SIGNAL_FPE; | |
1562 | ||
1563 | case LINUX_SIGKILL: | |
1564 | return GDB_SIGNAL_KILL; | |
1565 | ||
1566 | case LINUX_SIGUSR1: | |
1567 | return GDB_SIGNAL_USR1; | |
1568 | ||
1569 | case LINUX_SIGSEGV: | |
1570 | return GDB_SIGNAL_SEGV; | |
1571 | ||
1572 | case LINUX_SIGUSR2: | |
1573 | return GDB_SIGNAL_USR2; | |
1574 | ||
1575 | case LINUX_SIGPIPE: | |
1576 | return GDB_SIGNAL_PIPE; | |
1577 | ||
1578 | case LINUX_SIGALRM: | |
1579 | return GDB_SIGNAL_ALRM; | |
1580 | ||
1581 | case LINUX_SIGTERM: | |
1582 | return GDB_SIGNAL_TERM; | |
1583 | ||
1584 | case LINUX_SIGCHLD: | |
1585 | return GDB_SIGNAL_CHLD; | |
1586 | ||
1587 | case LINUX_SIGCONT: | |
1588 | return GDB_SIGNAL_CONT; | |
1589 | ||
1590 | case LINUX_SIGSTOP: | |
1591 | return GDB_SIGNAL_STOP; | |
1592 | ||
1593 | case LINUX_SIGTSTP: | |
1594 | return GDB_SIGNAL_TSTP; | |
1595 | ||
1596 | case LINUX_SIGTTIN: | |
1597 | return GDB_SIGNAL_TTIN; | |
1598 | ||
1599 | case LINUX_SIGTTOU: | |
1600 | return GDB_SIGNAL_TTOU; | |
1601 | ||
1602 | case LINUX_SIGURG: | |
1603 | return GDB_SIGNAL_URG; | |
1604 | ||
1605 | case LINUX_SIGXCPU: | |
1606 | return GDB_SIGNAL_XCPU; | |
1607 | ||
1608 | case LINUX_SIGXFSZ: | |
1609 | return GDB_SIGNAL_XFSZ; | |
1610 | ||
1611 | case LINUX_SIGVTALRM: | |
1612 | return GDB_SIGNAL_VTALRM; | |
1613 | ||
1614 | case LINUX_SIGPROF: | |
1615 | return GDB_SIGNAL_PROF; | |
1616 | ||
1617 | case LINUX_SIGWINCH: | |
1618 | return GDB_SIGNAL_WINCH; | |
1619 | ||
1620 | /* No way to differentiate between SIGIO and SIGPOLL. | |
1621 | Therefore, we just handle the first one. */ | |
1622 | case LINUX_SIGIO: | |
1623 | return GDB_SIGNAL_IO; | |
1624 | ||
1625 | case LINUX_SIGPWR: | |
1626 | return GDB_SIGNAL_PWR; | |
1627 | ||
1628 | case LINUX_SIGSYS: | |
1629 | return GDB_SIGNAL_SYS; | |
1630 | ||
1631 | /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>, | |
1632 | therefore we have to handle them here. */ | |
1633 | case LINUX_SIGRTMIN: | |
1634 | return GDB_SIGNAL_REALTIME_32; | |
1635 | ||
1636 | case LINUX_SIGRTMAX: | |
1637 | return GDB_SIGNAL_REALTIME_64; | |
1638 | } | |
1639 | ||
1640 | if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1) | |
1641 | { | |
1642 | int offset = signal - LINUX_SIGRTMIN + 1; | |
1643 | ||
1644 | return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset); | |
1645 | } | |
1646 | ||
1647 | return GDB_SIGNAL_UNKNOWN; | |
1648 | } | |
1649 | ||
1650 | /* Implementation of `gdbarch_gdb_signal_to_target', as defined in | |
1651 | gdbarch.h. This function is not static because it is exported to | |
1652 | other -tdep files. */ | |
1653 | ||
1654 | int | |
1655 | linux_gdb_signal_to_target (struct gdbarch *gdbarch, | |
1656 | enum gdb_signal signal) | |
1657 | { | |
1658 | switch (signal) | |
1659 | { | |
1660 | case GDB_SIGNAL_0: | |
1661 | return 0; | |
1662 | ||
1663 | case GDB_SIGNAL_HUP: | |
1664 | return LINUX_SIGHUP; | |
1665 | ||
1666 | case GDB_SIGNAL_INT: | |
1667 | return LINUX_SIGINT; | |
1668 | ||
1669 | case GDB_SIGNAL_QUIT: | |
1670 | return LINUX_SIGQUIT; | |
1671 | ||
1672 | case GDB_SIGNAL_ILL: | |
1673 | return LINUX_SIGILL; | |
1674 | ||
1675 | case GDB_SIGNAL_TRAP: | |
1676 | return LINUX_SIGTRAP; | |
1677 | ||
1678 | case GDB_SIGNAL_ABRT: | |
1679 | return LINUX_SIGABRT; | |
1680 | ||
1681 | case GDB_SIGNAL_FPE: | |
1682 | return LINUX_SIGFPE; | |
1683 | ||
1684 | case GDB_SIGNAL_KILL: | |
1685 | return LINUX_SIGKILL; | |
1686 | ||
1687 | case GDB_SIGNAL_BUS: | |
1688 | return LINUX_SIGBUS; | |
1689 | ||
1690 | case GDB_SIGNAL_SEGV: | |
1691 | return LINUX_SIGSEGV; | |
1692 | ||
1693 | case GDB_SIGNAL_SYS: | |
1694 | return LINUX_SIGSYS; | |
1695 | ||
1696 | case GDB_SIGNAL_PIPE: | |
1697 | return LINUX_SIGPIPE; | |
1698 | ||
1699 | case GDB_SIGNAL_ALRM: | |
1700 | return LINUX_SIGALRM; | |
1701 | ||
1702 | case GDB_SIGNAL_TERM: | |
1703 | return LINUX_SIGTERM; | |
1704 | ||
1705 | case GDB_SIGNAL_URG: | |
1706 | return LINUX_SIGURG; | |
1707 | ||
1708 | case GDB_SIGNAL_STOP: | |
1709 | return LINUX_SIGSTOP; | |
1710 | ||
1711 | case GDB_SIGNAL_TSTP: | |
1712 | return LINUX_SIGTSTP; | |
1713 | ||
1714 | case GDB_SIGNAL_CONT: | |
1715 | return LINUX_SIGCONT; | |
1716 | ||
1717 | case GDB_SIGNAL_CHLD: | |
1718 | return LINUX_SIGCHLD; | |
1719 | ||
1720 | case GDB_SIGNAL_TTIN: | |
1721 | return LINUX_SIGTTIN; | |
1722 | ||
1723 | case GDB_SIGNAL_TTOU: | |
1724 | return LINUX_SIGTTOU; | |
1725 | ||
1726 | case GDB_SIGNAL_IO: | |
1727 | return LINUX_SIGIO; | |
1728 | ||
1729 | case GDB_SIGNAL_XCPU: | |
1730 | return LINUX_SIGXCPU; | |
1731 | ||
1732 | case GDB_SIGNAL_XFSZ: | |
1733 | return LINUX_SIGXFSZ; | |
1734 | ||
1735 | case GDB_SIGNAL_VTALRM: | |
1736 | return LINUX_SIGVTALRM; | |
1737 | ||
1738 | case GDB_SIGNAL_PROF: | |
1739 | return LINUX_SIGPROF; | |
1740 | ||
1741 | case GDB_SIGNAL_WINCH: | |
1742 | return LINUX_SIGWINCH; | |
1743 | ||
1744 | case GDB_SIGNAL_USR1: | |
1745 | return LINUX_SIGUSR1; | |
1746 | ||
1747 | case GDB_SIGNAL_USR2: | |
1748 | return LINUX_SIGUSR2; | |
1749 | ||
1750 | case GDB_SIGNAL_PWR: | |
1751 | return LINUX_SIGPWR; | |
1752 | ||
1753 | case GDB_SIGNAL_POLL: | |
1754 | return LINUX_SIGPOLL; | |
1755 | ||
1756 | /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>, | |
1757 | therefore we have to handle it here. */ | |
1758 | case GDB_SIGNAL_REALTIME_32: | |
1759 | return LINUX_SIGRTMIN; | |
1760 | ||
1761 | /* Same comment applies to _64. */ | |
1762 | case GDB_SIGNAL_REALTIME_64: | |
1763 | return LINUX_SIGRTMAX; | |
1764 | } | |
1765 | ||
1766 | /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */ | |
1767 | if (signal >= GDB_SIGNAL_REALTIME_33 | |
1768 | && signal <= GDB_SIGNAL_REALTIME_63) | |
1769 | { | |
1770 | int offset = signal - GDB_SIGNAL_REALTIME_33; | |
1771 | ||
1772 | return LINUX_SIGRTMIN + 1 + offset; | |
1773 | } | |
1774 | ||
1775 | return -1; | |
1776 | } | |
1777 | ||
3437254d PA |
1778 | /* Rummage through mappings to find a mapping's size. */ |
1779 | ||
1780 | static int | |
1781 | find_mapping_size (CORE_ADDR vaddr, unsigned long size, | |
1782 | int read, int write, int exec, int modified, | |
1783 | void *data) | |
1784 | { | |
1785 | struct mem_range *range = data; | |
1786 | ||
1787 | if (vaddr == range->start) | |
1788 | { | |
1789 | range->length = size; | |
1790 | return 1; | |
1791 | } | |
1792 | return 0; | |
1793 | } | |
1794 | ||
cdfa0b0a PA |
1795 | /* Helper for linux_vsyscall_range that does the real work of finding |
1796 | the vsyscall's address range. */ | |
3437254d PA |
1797 | |
1798 | static int | |
cdfa0b0a | 1799 | linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range) |
3437254d PA |
1800 | { |
1801 | if (target_auxv_search (¤t_target, AT_SYSINFO_EHDR, &range->start) <= 0) | |
1802 | return 0; | |
1803 | ||
1804 | /* This is installed by linux_init_abi below, so should always be | |
1805 | available. */ | |
1806 | gdb_assert (gdbarch_find_memory_regions_p (target_gdbarch ())); | |
1807 | ||
1808 | range->length = 0; | |
1809 | gdbarch_find_memory_regions (gdbarch, find_mapping_size, range); | |
1810 | return 1; | |
1811 | } | |
1812 | ||
cdfa0b0a PA |
1813 | /* Implementation of the "vsyscall_range" gdbarch hook. Handles |
1814 | caching, and defers the real work to linux_vsyscall_range_raw. */ | |
1815 | ||
1816 | static int | |
1817 | linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range) | |
1818 | { | |
1819 | struct linux_info *info = get_linux_inferior_data (); | |
1820 | ||
1821 | if (info->vsyscall_range_p == 0) | |
1822 | { | |
1823 | if (linux_vsyscall_range_raw (gdbarch, &info->vsyscall_range)) | |
1824 | info->vsyscall_range_p = 1; | |
1825 | else | |
1826 | info->vsyscall_range_p = -1; | |
1827 | } | |
1828 | ||
1829 | if (info->vsyscall_range_p < 0) | |
1830 | return 0; | |
1831 | ||
1832 | *range = info->vsyscall_range; | |
1833 | return 1; | |
1834 | } | |
1835 | ||
3bc3cebe JK |
1836 | /* Symbols for linux_infcall_mmap's ARG_FLAGS; their Linux MAP_* system |
1837 | definitions would be dependent on compilation host. */ | |
1838 | #define GDB_MMAP_MAP_PRIVATE 0x02 /* Changes are private. */ | |
1839 | #define GDB_MMAP_MAP_ANONYMOUS 0x20 /* Don't use a file. */ | |
1840 | ||
1841 | /* See gdbarch.sh 'infcall_mmap'. */ | |
1842 | ||
1843 | static CORE_ADDR | |
1844 | linux_infcall_mmap (CORE_ADDR size, unsigned prot) | |
1845 | { | |
1846 | struct objfile *objf; | |
1847 | /* Do there still exist any Linux systems without "mmap64"? | |
1848 | "mmap" uses 64-bit off_t on x86_64 and 32-bit off_t on i386 and x32. */ | |
1849 | struct value *mmap_val = find_function_in_inferior ("mmap64", &objf); | |
1850 | struct value *addr_val; | |
1851 | struct gdbarch *gdbarch = get_objfile_arch (objf); | |
1852 | CORE_ADDR retval; | |
1853 | enum | |
1854 | { | |
2a546367 | 1855 | ARG_ADDR, ARG_LENGTH, ARG_PROT, ARG_FLAGS, ARG_FD, ARG_OFFSET, ARG_LAST |
3bc3cebe | 1856 | }; |
2a546367 | 1857 | struct value *arg[ARG_LAST]; |
3bc3cebe JK |
1858 | |
1859 | arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, | |
1860 | 0); | |
1861 | /* Assuming sizeof (unsigned long) == sizeof (size_t). */ | |
1862 | arg[ARG_LENGTH] = value_from_ulongest | |
1863 | (builtin_type (gdbarch)->builtin_unsigned_long, size); | |
1864 | gdb_assert ((prot & ~(GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE | |
1865 | | GDB_MMAP_PROT_EXEC)) | |
1866 | == 0); | |
1867 | arg[ARG_PROT] = value_from_longest (builtin_type (gdbarch)->builtin_int, prot); | |
1868 | arg[ARG_FLAGS] = value_from_longest (builtin_type (gdbarch)->builtin_int, | |
1869 | GDB_MMAP_MAP_PRIVATE | |
1870 | | GDB_MMAP_MAP_ANONYMOUS); | |
1871 | arg[ARG_FD] = value_from_longest (builtin_type (gdbarch)->builtin_int, -1); | |
1872 | arg[ARG_OFFSET] = value_from_longest (builtin_type (gdbarch)->builtin_int64, | |
1873 | 0); | |
2a546367 | 1874 | addr_val = call_function_by_hand (mmap_val, ARG_LAST, arg); |
3bc3cebe JK |
1875 | retval = value_as_address (addr_val); |
1876 | if (retval == (CORE_ADDR) -1) | |
1877 | error (_("Failed inferior mmap call for %s bytes, errno is changed."), | |
1878 | pulongest (size)); | |
1879 | return retval; | |
1880 | } | |
1881 | ||
7f361056 JK |
1882 | /* See gdbarch.sh 'infcall_munmap'. */ |
1883 | ||
1884 | static void | |
1885 | linux_infcall_munmap (CORE_ADDR addr, CORE_ADDR size) | |
1886 | { | |
1887 | struct objfile *objf; | |
1888 | struct value *munmap_val = find_function_in_inferior ("munmap", &objf); | |
1889 | struct value *retval_val; | |
1890 | struct gdbarch *gdbarch = get_objfile_arch (objf); | |
1891 | LONGEST retval; | |
1892 | enum | |
1893 | { | |
1894 | ARG_ADDR, ARG_LENGTH, ARG_LAST | |
1895 | }; | |
1896 | struct value *arg[ARG_LAST]; | |
1897 | ||
1898 | arg[ARG_ADDR] = value_from_pointer (builtin_type (gdbarch)->builtin_data_ptr, | |
1899 | addr); | |
1900 | /* Assuming sizeof (unsigned long) == sizeof (size_t). */ | |
1901 | arg[ARG_LENGTH] = value_from_ulongest | |
1902 | (builtin_type (gdbarch)->builtin_unsigned_long, size); | |
1903 | retval_val = call_function_by_hand (munmap_val, ARG_LAST, arg); | |
1904 | retval = value_as_long (retval_val); | |
1905 | if (retval != 0) | |
1906 | warning (_("Failed inferior munmap call at %s for %s bytes, " | |
1907 | "errno is changed."), | |
1908 | hex_string (addr), pulongest (size)); | |
1909 | } | |
1910 | ||
906d60cf PA |
1911 | /* See linux-tdep.h. */ |
1912 | ||
1913 | CORE_ADDR | |
1914 | linux_displaced_step_location (struct gdbarch *gdbarch) | |
1915 | { | |
1916 | CORE_ADDR addr; | |
1917 | int bp_len; | |
1918 | ||
1919 | /* Determine entry point from target auxiliary vector. This avoids | |
1920 | the need for symbols. Also, when debugging a stand-alone SPU | |
1921 | executable, entry_point_address () will point to an SPU | |
1922 | local-store address and is thus not usable as displaced stepping | |
1923 | location. The auxiliary vector gets us the PowerPC-side entry | |
1924 | point address instead. */ | |
1925 | if (target_auxv_search (¤t_target, AT_ENTRY, &addr) <= 0) | |
1926 | error (_("Cannot find AT_ENTRY auxiliary vector entry.")); | |
1927 | ||
1928 | /* Make certain that the address points at real code, and not a | |
1929 | function descriptor. */ | |
1930 | addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, | |
1931 | ¤t_target); | |
1932 | ||
1933 | /* Inferior calls also use the entry point as a breakpoint location. | |
1934 | We don't want displaced stepping to interfere with those | |
1935 | breakpoints, so leave space. */ | |
1936 | gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len); | |
1937 | addr += bp_len * 2; | |
1938 | ||
1939 | return addr; | |
1940 | } | |
1941 | ||
df8411da SDJ |
1942 | /* Display whether the gcore command is using the |
1943 | /proc/PID/coredump_filter file. */ | |
1944 | ||
1945 | static void | |
1946 | show_use_coredump_filter (struct ui_file *file, int from_tty, | |
1947 | struct cmd_list_element *c, const char *value) | |
1948 | { | |
1949 | fprintf_filtered (file, _("Use of /proc/PID/coredump_filter file to generate" | |
1950 | " corefiles is %s.\n"), value); | |
1951 | } | |
1952 | ||
a5ee0f0c PA |
1953 | /* To be called from the various GDB_OSABI_LINUX handlers for the |
1954 | various GNU/Linux architectures and machine types. */ | |
1955 | ||
1956 | void | |
1957 | linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
1958 | { | |
1959 | set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str); | |
3030c96e | 1960 | set_gdbarch_info_proc (gdbarch, linux_info_proc); |
451b7c33 | 1961 | set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc); |
35c2fab7 | 1962 | set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions); |
f968fe80 | 1963 | set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes); |
33fbcbee PA |
1964 | set_gdbarch_has_shared_address_space (gdbarch, |
1965 | linux_has_shared_address_space); | |
eb14d406 SDJ |
1966 | set_gdbarch_gdb_signal_from_target (gdbarch, |
1967 | linux_gdb_signal_from_target); | |
1968 | set_gdbarch_gdb_signal_to_target (gdbarch, | |
1969 | linux_gdb_signal_to_target); | |
3437254d | 1970 | set_gdbarch_vsyscall_range (gdbarch, linux_vsyscall_range); |
3bc3cebe | 1971 | set_gdbarch_infcall_mmap (gdbarch, linux_infcall_mmap); |
7f361056 | 1972 | set_gdbarch_infcall_munmap (gdbarch, linux_infcall_munmap); |
5cd867b4 | 1973 | set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type); |
a5ee0f0c | 1974 | } |
06253dd3 | 1975 | |
70221824 PA |
1976 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1977 | extern initialize_file_ftype _initialize_linux_tdep; | |
1978 | ||
06253dd3 JK |
1979 | void |
1980 | _initialize_linux_tdep (void) | |
1981 | { | |
1982 | linux_gdbarch_data_handle = | |
1983 | gdbarch_data_register_post_init (init_linux_gdbarch_data); | |
cdfa0b0a PA |
1984 | |
1985 | /* Set a cache per-inferior. */ | |
1986 | linux_inferior_data | |
1987 | = register_inferior_data_with_cleanup (NULL, linux_inferior_data_cleanup); | |
1988 | /* Observers used to invalidate the cache when needed. */ | |
1989 | observer_attach_inferior_exit (invalidate_linux_cache_inf); | |
1990 | observer_attach_inferior_appeared (invalidate_linux_cache_inf); | |
df8411da SDJ |
1991 | |
1992 | add_setshow_boolean_cmd ("use-coredump-filter", class_files, | |
1993 | &use_coredump_filter, _("\ | |
1994 | Set whether gcore should consider /proc/PID/coredump_filter."), | |
1995 | _("\ | |
1996 | Show whether gcore should consider /proc/PID/coredump_filter."), | |
1997 | _("\ | |
1998 | Use this command to set whether gcore should consider the contents\n\ | |
1999 | of /proc/PID/coredump_filter when generating the corefile. For more information\n\ | |
2000 | about this file, refer to the manpage of core(5)."), | |
2001 | NULL, show_use_coredump_filter, | |
2002 | &setlist, &showlist); | |
06253dd3 | 2003 | } |