Tests for validate symbol file using build-id.
[gdb/archer.git] / gdb / linux-tdep.c
blob4544e5f38f8b341de32210e5f6ce0a5d1f92a3cd
1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "gdbtypes.h"
22 #include "linux-tdep.h"
23 #include "auxv.h"
24 #include "target.h"
25 #include "gdbthread.h"
26 #include "gdbcore.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h" /* for elfcore_write_* */
31 #include "inferior.h"
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdb_obstack.h"
35 #include "cli/cli-utils.h"
36 #include "linux-maps.h"
38 #include <ctype.h>
40 static struct gdbarch_data *linux_gdbarch_data_handle;
42 struct linux_gdbarch_data
44 struct type *siginfo_type;
47 static void *
48 init_linux_gdbarch_data (struct gdbarch *gdbarch)
50 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
53 static struct linux_gdbarch_data *
54 get_linux_gdbarch_data (struct gdbarch *gdbarch)
56 return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
59 /* This function is suitable for architectures that don't
60 extend/override the standard siginfo structure. */
62 struct type *
63 linux_get_siginfo_type (struct gdbarch *gdbarch)
65 struct linux_gdbarch_data *linux_gdbarch_data;
66 struct type *int_type, *uint_type, *long_type, *void_ptr_type;
67 struct type *uid_type, *pid_type;
68 struct type *sigval_type, *clock_type;
69 struct type *siginfo_type, *sifields_type;
70 struct type *type;
72 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
73 if (linux_gdbarch_data->siginfo_type != NULL)
74 return linux_gdbarch_data->siginfo_type;
76 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
77 0, "int");
78 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
79 1, "unsigned int");
80 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
81 0, "long");
82 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
84 /* sival_t */
85 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
86 TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
87 append_composite_type_field (sigval_type, "sival_int", int_type);
88 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
90 /* __pid_t */
91 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
92 TYPE_LENGTH (int_type), "__pid_t");
93 TYPE_TARGET_TYPE (pid_type) = int_type;
94 TYPE_TARGET_STUB (pid_type) = 1;
96 /* __uid_t */
97 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
98 TYPE_LENGTH (uint_type), "__uid_t");
99 TYPE_TARGET_TYPE (uid_type) = uint_type;
100 TYPE_TARGET_STUB (uid_type) = 1;
102 /* __clock_t */
103 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
104 TYPE_LENGTH (long_type), "__clock_t");
105 TYPE_TARGET_TYPE (clock_type) = long_type;
106 TYPE_TARGET_STUB (clock_type) = 1;
108 /* _sifields */
109 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
112 const int si_max_size = 128;
113 int si_pad_size;
114 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
116 /* _pad */
117 if (gdbarch_ptr_bit (gdbarch) == 64)
118 si_pad_size = (si_max_size / size_of_int) - 4;
119 else
120 si_pad_size = (si_max_size / size_of_int) - 3;
121 append_composite_type_field (sifields_type, "_pad",
122 init_vector_type (int_type, si_pad_size));
125 /* _kill */
126 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
127 append_composite_type_field (type, "si_pid", pid_type);
128 append_composite_type_field (type, "si_uid", uid_type);
129 append_composite_type_field (sifields_type, "_kill", type);
131 /* _timer */
132 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
133 append_composite_type_field (type, "si_tid", int_type);
134 append_composite_type_field (type, "si_overrun", int_type);
135 append_composite_type_field (type, "si_sigval", sigval_type);
136 append_composite_type_field (sifields_type, "_timer", type);
138 /* _rt */
139 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
140 append_composite_type_field (type, "si_pid", pid_type);
141 append_composite_type_field (type, "si_uid", uid_type);
142 append_composite_type_field (type, "si_sigval", sigval_type);
143 append_composite_type_field (sifields_type, "_rt", type);
145 /* _sigchld */
146 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
147 append_composite_type_field (type, "si_pid", pid_type);
148 append_composite_type_field (type, "si_uid", uid_type);
149 append_composite_type_field (type, "si_status", int_type);
150 append_composite_type_field (type, "si_utime", clock_type);
151 append_composite_type_field (type, "si_stime", clock_type);
152 append_composite_type_field (sifields_type, "_sigchld", type);
154 /* _sigfault */
155 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
156 append_composite_type_field (type, "si_addr", void_ptr_type);
157 append_composite_type_field (sifields_type, "_sigfault", type);
159 /* _sigpoll */
160 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
161 append_composite_type_field (type, "si_band", long_type);
162 append_composite_type_field (type, "si_fd", int_type);
163 append_composite_type_field (sifields_type, "_sigpoll", type);
165 /* struct siginfo */
166 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
167 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
168 append_composite_type_field (siginfo_type, "si_signo", int_type);
169 append_composite_type_field (siginfo_type, "si_errno", int_type);
170 append_composite_type_field (siginfo_type, "si_code", int_type);
171 append_composite_type_field_aligned (siginfo_type,
172 "_sifields", sifields_type,
173 TYPE_LENGTH (long_type));
175 linux_gdbarch_data->siginfo_type = siginfo_type;
177 return siginfo_type;
180 static int
181 linux_has_shared_address_space (struct gdbarch *gdbarch)
183 /* Determine whether we are running on uClinux or normal Linux
184 kernel. */
185 CORE_ADDR dummy;
186 int target_is_uclinux;
188 target_is_uclinux
189 = (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
190 && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
192 return target_is_uclinux;
195 /* This is how we want PTIDs from core files to be printed. */
197 static char *
198 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
200 static char buf[80];
202 if (ptid_get_lwp (ptid) != 0)
204 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
205 return buf;
208 return normal_pid_to_str (ptid);
211 /* Implement the "info proc" command. */
213 static void
214 linux_info_proc (struct gdbarch *gdbarch, char *args,
215 enum info_proc_what what)
217 /* A long is used for pid instead of an int to avoid a loss of precision
218 compiler warning from the output of strtoul. */
219 long pid;
220 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
221 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
222 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
223 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
224 int status_f = (what == IP_STATUS || what == IP_ALL);
225 int stat_f = (what == IP_STAT || what == IP_ALL);
226 char filename[100];
227 gdb_byte *data;
228 int target_errno;
230 if (args && isdigit (args[0]))
231 pid = strtoul (args, &args, 10);
232 else
234 if (!target_has_execution)
235 error (_("No current process: you must name one."));
236 if (current_inferior ()->fake_pid_p)
237 error (_("Can't determine the current process's PID: you must name one."));
239 pid = current_inferior ()->pid;
242 args = skip_spaces (args);
243 if (args && args[0])
244 error (_("Too many parameters: %s"), args);
246 printf_filtered (_("process %ld\n"), pid);
247 if (cmdline_f)
249 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
250 data = target_fileio_read_stralloc (filename);
251 if (data)
253 struct cleanup *cleanup = make_cleanup (xfree, data);
254 printf_filtered ("cmdline = '%s'\n", data);
255 do_cleanups (cleanup);
257 else
258 warning (_("unable to open /proc file '%s'"), filename);
260 if (cwd_f)
262 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
263 data = target_fileio_readlink (filename, &target_errno);
264 if (data)
266 struct cleanup *cleanup = make_cleanup (xfree, data);
267 printf_filtered ("cwd = '%s'\n", data);
268 do_cleanups (cleanup);
270 else
271 warning (_("unable to read link '%s'"), filename);
273 if (exe_f)
275 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
276 data = target_fileio_readlink (filename, &target_errno);
277 if (data)
279 struct cleanup *cleanup = make_cleanup (xfree, data);
280 printf_filtered ("exe = '%s'\n", data);
281 do_cleanups (cleanup);
283 else
284 warning (_("unable to read link '%s'"), filename);
286 if (mappings_f)
288 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
289 data = target_fileio_read_stralloc (filename);
290 if (data)
292 struct cleanup *cleanup = make_cleanup (xfree, data);
293 char *line;
295 printf_filtered (_("Mapped address spaces:\n\n"));
296 if (gdbarch_addr_bit (gdbarch) == 32)
298 printf_filtered ("\t%10s %10s %10s %10s %s\n",
299 "Start Addr",
300 " End Addr",
301 " Size", " Offset", "objfile");
303 else
305 printf_filtered (" %18s %18s %10s %10s %s\n",
306 "Start Addr",
307 " End Addr",
308 " Size", " Offset", "objfile");
311 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
313 ULONGEST addr, endaddr, offset, inode;
314 const char *permissions, *device, *filename;
315 size_t permissions_len, device_len;
317 read_mapping (line, &addr, &endaddr,
318 &permissions, &permissions_len,
319 &offset, &device, &device_len,
320 &inode, &filename);
322 if (gdbarch_addr_bit (gdbarch) == 32)
324 printf_filtered ("\t%10s %10s %10s %10s %s\n",
325 paddress (gdbarch, addr),
326 paddress (gdbarch, endaddr),
327 hex_string (endaddr - addr),
328 hex_string (offset),
329 *filename? filename : "");
331 else
333 printf_filtered (" %18s %18s %10s %10s %s\n",
334 paddress (gdbarch, addr),
335 paddress (gdbarch, endaddr),
336 hex_string (endaddr - addr),
337 hex_string (offset),
338 *filename? filename : "");
342 do_cleanups (cleanup);
344 else
345 warning (_("unable to open /proc file '%s'"), filename);
347 if (status_f)
349 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
350 data = target_fileio_read_stralloc (filename);
351 if (data)
353 struct cleanup *cleanup = make_cleanup (xfree, data);
354 puts_filtered (data);
355 do_cleanups (cleanup);
357 else
358 warning (_("unable to open /proc file '%s'"), filename);
360 if (stat_f)
362 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
363 data = target_fileio_read_stralloc (filename);
364 if (data)
366 struct cleanup *cleanup = make_cleanup (xfree, data);
367 const char *p = data;
369 printf_filtered (_("Process: %s\n"),
370 pulongest (strtoulst (p, &p, 10)));
372 p = skip_spaces_const (p);
373 if (*p == '(')
375 const char *ep = strchr (p, ')');
376 if (ep != NULL)
378 printf_filtered ("Exec file: %.*s\n",
379 (int) (ep - p - 1), p + 1);
380 p = ep + 1;
384 p = skip_spaces_const (p);
385 if (*p)
386 printf_filtered (_("State: %c\n"), *p++);
388 if (*p)
389 printf_filtered (_("Parent process: %s\n"),
390 pulongest (strtoulst (p, &p, 10)));
391 if (*p)
392 printf_filtered (_("Process group: %s\n"),
393 pulongest (strtoulst (p, &p, 10)));
394 if (*p)
395 printf_filtered (_("Session id: %s\n"),
396 pulongest (strtoulst (p, &p, 10)));
397 if (*p)
398 printf_filtered (_("TTY: %s\n"),
399 pulongest (strtoulst (p, &p, 10)));
400 if (*p)
401 printf_filtered (_("TTY owner process group: %s\n"),
402 pulongest (strtoulst (p, &p, 10)));
404 if (*p)
405 printf_filtered (_("Flags: %s\n"),
406 hex_string (strtoulst (p, &p, 10)));
407 if (*p)
408 printf_filtered (_("Minor faults (no memory page): %s\n"),
409 pulongest (strtoulst (p, &p, 10)));
410 if (*p)
411 printf_filtered (_("Minor faults, children: %s\n"),
412 pulongest (strtoulst (p, &p, 10)));
413 if (*p)
414 printf_filtered (_("Major faults (memory page faults): %s\n"),
415 pulongest (strtoulst (p, &p, 10)));
416 if (*p)
417 printf_filtered (_("Major faults, children: %s\n"),
418 pulongest (strtoulst (p, &p, 10)));
419 if (*p)
420 printf_filtered (_("utime: %s\n"),
421 pulongest (strtoulst (p, &p, 10)));
422 if (*p)
423 printf_filtered (_("stime: %s\n"),
424 pulongest (strtoulst (p, &p, 10)));
425 if (*p)
426 printf_filtered (_("utime, children: %s\n"),
427 pulongest (strtoulst (p, &p, 10)));
428 if (*p)
429 printf_filtered (_("stime, children: %s\n"),
430 pulongest (strtoulst (p, &p, 10)));
431 if (*p)
432 printf_filtered (_("jiffies remaining in current "
433 "time slice: %s\n"),
434 pulongest (strtoulst (p, &p, 10)));
435 if (*p)
436 printf_filtered (_("'nice' value: %s\n"),
437 pulongest (strtoulst (p, &p, 10)));
438 if (*p)
439 printf_filtered (_("jiffies until next timeout: %s\n"),
440 pulongest (strtoulst (p, &p, 10)));
441 if (*p)
442 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
443 pulongest (strtoulst (p, &p, 10)));
444 if (*p)
445 printf_filtered (_("start time (jiffies since "
446 "system boot): %s\n"),
447 pulongest (strtoulst (p, &p, 10)));
448 if (*p)
449 printf_filtered (_("Virtual memory size: %s\n"),
450 pulongest (strtoulst (p, &p, 10)));
451 if (*p)
452 printf_filtered (_("Resident set size: %s\n"),
453 pulongest (strtoulst (p, &p, 10)));
454 if (*p)
455 printf_filtered (_("rlim: %s\n"),
456 pulongest (strtoulst (p, &p, 10)));
457 if (*p)
458 printf_filtered (_("Start of text: %s\n"),
459 hex_string (strtoulst (p, &p, 10)));
460 if (*p)
461 printf_filtered (_("End of text: %s\n"),
462 hex_string (strtoulst (p, &p, 10)));
463 if (*p)
464 printf_filtered (_("Start of stack: %s\n"),
465 hex_string (strtoulst (p, &p, 10)));
466 #if 0 /* Don't know how architecture-dependent the rest is...
467 Anyway the signal bitmap info is available from "status". */
468 if (*p)
469 printf_filtered (_("Kernel stack pointer: %s\n"),
470 hex_string (strtoulst (p, &p, 10)));
471 if (*p)
472 printf_filtered (_("Kernel instr pointer: %s\n"),
473 hex_string (strtoulst (p, &p, 10)));
474 if (*p)
475 printf_filtered (_("Pending signals bitmap: %s\n"),
476 hex_string (strtoulst (p, &p, 10)));
477 if (*p)
478 printf_filtered (_("Blocked signals bitmap: %s\n"),
479 hex_string (strtoulst (p, &p, 10)));
480 if (*p)
481 printf_filtered (_("Ignored signals bitmap: %s\n"),
482 hex_string (strtoulst (p, &p, 10)));
483 if (*p)
484 printf_filtered (_("Catched signals bitmap: %s\n"),
485 hex_string (strtoulst (p, &p, 10)));
486 if (*p)
487 printf_filtered (_("wchan (system call): %s\n"),
488 hex_string (strtoulst (p, &p, 10)));
489 #endif
490 do_cleanups (cleanup);
492 else
493 warning (_("unable to open /proc file '%s'"), filename);
497 /* Implement "info proc mappings" for a corefile. */
499 static void
500 linux_core_info_proc_mappings (struct gdbarch *gdbarch, char *args)
502 asection *section;
503 ULONGEST count, page_size;
504 unsigned char *descdata, *filenames, *descend, *contents;
505 size_t note_size;
506 unsigned int addr_size_bits, addr_size;
507 struct cleanup *cleanup;
508 struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
509 /* We assume this for reading 64-bit core files. */
510 gdb_static_assert (sizeof (ULONGEST) >= 8);
512 section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
513 if (section == NULL)
515 warning (_("unable to find mappings in core file"));
516 return;
519 addr_size_bits = gdbarch_addr_bit (core_gdbarch);
520 addr_size = addr_size_bits / 8;
521 note_size = bfd_get_section_size (section);
523 if (note_size < 2 * addr_size)
524 error (_("malformed core note - too short for header"));
526 contents = xmalloc (note_size);
527 cleanup = make_cleanup (xfree, contents);
528 if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
529 error (_("could not get core note contents"));
531 descdata = contents;
532 descend = descdata + note_size;
534 if (descdata[note_size - 1] != '\0')
535 error (_("malformed note - does not end with \\0"));
537 count = bfd_get (addr_size_bits, core_bfd, descdata);
538 descdata += addr_size;
540 page_size = bfd_get (addr_size_bits, core_bfd, descdata);
541 descdata += addr_size;
543 if (note_size < 2 * addr_size + count * 3 * addr_size)
544 error (_("malformed note - too short for supplied file count"));
546 printf_filtered (_("Mapped address spaces:\n\n"));
547 if (gdbarch_addr_bit (gdbarch) == 32)
549 printf_filtered ("\t%10s %10s %10s %10s %s\n",
550 "Start Addr",
551 " End Addr",
552 " Size", " Offset", "objfile");
554 else
556 printf_filtered (" %18s %18s %10s %10s %s\n",
557 "Start Addr",
558 " End Addr",
559 " Size", " Offset", "objfile");
562 filenames = descdata + count * 3 * addr_size;
563 while (--count > 0)
565 ULONGEST start, end, file_ofs;
567 if (filenames == descend)
568 error (_("malformed note - filenames end too early"));
570 start = bfd_get (addr_size_bits, core_bfd, descdata);
571 descdata += addr_size;
572 end = bfd_get (addr_size_bits, core_bfd, descdata);
573 descdata += addr_size;
574 file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
575 descdata += addr_size;
577 file_ofs *= page_size;
579 if (gdbarch_addr_bit (gdbarch) == 32)
580 printf_filtered ("\t%10s %10s %10s %10s %s\n",
581 paddress (gdbarch, start),
582 paddress (gdbarch, end),
583 hex_string (end - start),
584 hex_string (file_ofs),
585 filenames);
586 else
587 printf_filtered (" %18s %18s %10s %10s %s\n",
588 paddress (gdbarch, start),
589 paddress (gdbarch, end),
590 hex_string (end - start),
591 hex_string (file_ofs),
592 filenames);
594 filenames += 1 + strlen ((char *) filenames);
597 do_cleanups (cleanup);
600 /* Implement "info proc" for a corefile. */
602 static void
603 linux_core_info_proc (struct gdbarch *gdbarch, char *args,
604 enum info_proc_what what)
606 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
607 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
609 if (exe_f)
611 const char *exe;
613 exe = bfd_core_file_failing_command (core_bfd);
614 if (exe != NULL)
615 printf_filtered ("exe = '%s'\n", exe);
616 else
617 warning (_("unable to find command name in core file"));
620 if (mappings_f)
621 linux_core_info_proc_mappings (gdbarch, args);
623 if (!exe_f && !mappings_f)
624 error (_("unable to handle request"));
627 /* A structure for passing information through
628 linux_find_memory_regions_full. */
630 struct linux_find_memory_regions_data
632 /* The original callback. */
634 find_memory_region_ftype func;
636 /* The original datum. */
638 void *data;
641 static linux_find_memory_region_ftype linux_find_memory_regions_thunk;
643 /* A callback for linux_find_memory_regions that converts between the
644 "full"-style callback and find_memory_region_ftype. */
646 static int
647 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
648 ULONGEST offset, ULONGEST inode,
649 int read, int write, int exec, int modified,
650 const char *filename, void *arg)
652 struct linux_find_memory_regions_data *data = arg;
654 return data->func (vaddr, size, read, write, exec, modified, data->data);
657 /* Wrapper of linux_find_memory_regions_full handling FAKE_PID_P in GDB. */
659 static int
660 linux_find_memory_regions_gdb (struct gdbarch *gdbarch,
661 linux_find_memory_region_ftype *func,
662 void *func_data)
664 void *memory_to_free = NULL;
665 struct cleanup *cleanup;
666 int retval;
668 /* We need to know the real target PID so
669 linux_find_memory_regions_full can access /proc. */
670 if (current_inferior ()->fake_pid_p)
671 return 1;
673 cleanup = make_cleanup (free_current_contents, &memory_to_free);
674 retval = linux_find_memory_regions_full (current_inferior ()->pid,
675 func, func_data, &memory_to_free);
676 do_cleanups (cleanup);
677 return retval;
680 /* A variant of linux_find_memory_regions_full that is suitable as the
681 gdbarch find_memory_regions method. */
683 static int
684 linux_find_memory_regions (struct gdbarch *gdbarch,
685 find_memory_region_ftype func, void *func_data)
687 struct linux_find_memory_regions_data data;
689 data.func = func;
690 data.data = func_data;
692 return linux_find_memory_regions_gdb (gdbarch,
693 linux_find_memory_regions_thunk, &data);
696 /* Determine which signal stopped execution. */
698 static int
699 find_signalled_thread (struct thread_info *info, void *data)
701 if (info->suspend.stop_signal != GDB_SIGNAL_0
702 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
703 return 1;
705 return 0;
708 static enum gdb_signal
709 find_stop_signal (void)
711 struct thread_info *info =
712 iterate_over_threads (find_signalled_thread, NULL);
714 if (info)
715 return info->suspend.stop_signal;
716 else
717 return GDB_SIGNAL_0;
720 /* Generate corefile notes for SPU contexts. */
722 static char *
723 linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
725 static const char *spu_files[] =
727 "object-id",
728 "mem",
729 "regs",
730 "fpcr",
731 "lslr",
732 "decr",
733 "decr_status",
734 "signal1",
735 "signal1_type",
736 "signal2",
737 "signal2_type",
738 "event_mask",
739 "event_status",
740 "mbox_info",
741 "ibox_info",
742 "wbox_info",
743 "dma_info",
744 "proxydma_info",
747 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
748 gdb_byte *spu_ids;
749 LONGEST i, j, size;
751 /* Determine list of SPU ids. */
752 size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
753 NULL, &spu_ids);
755 /* Generate corefile notes for each SPU file. */
756 for (i = 0; i < size; i += 4)
758 int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
760 for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
762 char annex[32], note_name[32];
763 gdb_byte *spu_data;
764 LONGEST spu_len;
766 xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
767 spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
768 annex, &spu_data);
769 if (spu_len > 0)
771 xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
772 note_data = elfcore_write_note (obfd, note_data, note_size,
773 note_name, NT_SPU,
774 spu_data, spu_len);
775 xfree (spu_data);
777 if (!note_data)
779 xfree (spu_ids);
780 return NULL;
786 if (size > 0)
787 xfree (spu_ids);
789 return note_data;
792 /* This is used to pass information from
793 linux_make_mappings_corefile_notes through
794 linux_find_memory_regions_full. */
796 struct linux_make_mappings_data
798 /* Number of files mapped. */
799 ULONGEST file_count;
801 /* The obstack for the main part of the data. */
802 struct obstack *data_obstack;
804 /* The filename obstack. */
805 struct obstack *filename_obstack;
807 /* The architecture's "long" type. */
808 struct type *long_type;
811 static linux_find_memory_region_ftype linux_make_mappings_callback;
813 /* A callback for linux_find_memory_regions_full that updates the
814 mappings data for linux_make_mappings_corefile_notes. */
816 static int
817 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
818 ULONGEST offset, ULONGEST inode,
819 int read, int write, int exec, int modified,
820 const char *filename, void *data)
822 struct linux_make_mappings_data *map_data = data;
823 gdb_byte buf[sizeof (ULONGEST)];
825 if (*filename == '\0' || inode == 0)
826 return 0;
828 ++map_data->file_count;
830 pack_long (buf, map_data->long_type, vaddr);
831 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
832 pack_long (buf, map_data->long_type, vaddr + size);
833 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
834 pack_long (buf, map_data->long_type, offset);
835 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
837 obstack_grow_str0 (map_data->filename_obstack, filename);
839 return 0;
842 /* Write the file mapping data to the core file, if possible. OBFD is
843 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
844 is a pointer to the note size. Returns the new NOTE_DATA and
845 updates NOTE_SIZE. */
847 static char *
848 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
849 char *note_data, int *note_size)
851 struct cleanup *cleanup;
852 struct obstack data_obstack, filename_obstack;
853 struct linux_make_mappings_data mapping_data;
854 struct type *long_type
855 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
856 gdb_byte buf[sizeof (ULONGEST)];
858 obstack_init (&data_obstack);
859 cleanup = make_cleanup_obstack_free (&data_obstack);
860 obstack_init (&filename_obstack);
861 make_cleanup_obstack_free (&filename_obstack);
863 mapping_data.file_count = 0;
864 mapping_data.data_obstack = &data_obstack;
865 mapping_data.filename_obstack = &filename_obstack;
866 mapping_data.long_type = long_type;
868 /* Reserve space for the count. */
869 obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
870 /* We always write the page size as 1 since we have no good way to
871 determine the correct value. */
872 pack_long (buf, long_type, 1);
873 obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
875 linux_find_memory_regions_gdb (gdbarch, linux_make_mappings_callback,
876 &mapping_data);
878 if (mapping_data.file_count != 0)
880 /* Write the count to the obstack. */
881 pack_long (obstack_base (&data_obstack), long_type,
882 mapping_data.file_count);
884 /* Copy the filenames to the data obstack. */
885 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
886 obstack_object_size (&filename_obstack));
888 note_data = elfcore_write_note (obfd, note_data, note_size,
889 "CORE", NT_FILE,
890 obstack_base (&data_obstack),
891 obstack_object_size (&data_obstack));
894 do_cleanups (cleanup);
895 return note_data;
898 /* Records the thread's register state for the corefile note
899 section. */
901 static char *
902 linux_collect_thread_registers (const struct regcache *regcache,
903 ptid_t ptid, bfd *obfd,
904 char *note_data, int *note_size,
905 enum gdb_signal stop_signal)
907 struct gdbarch *gdbarch = get_regcache_arch (regcache);
908 struct core_regset_section *sect_list;
909 unsigned long lwp;
911 sect_list = gdbarch_core_regset_sections (gdbarch);
912 gdb_assert (sect_list);
914 /* For remote targets the LWP may not be available, so use the TID. */
915 lwp = ptid_get_lwp (ptid);
916 if (!lwp)
917 lwp = ptid_get_tid (ptid);
919 while (sect_list->sect_name != NULL)
921 const struct regset *regset;
922 char *buf;
924 regset = gdbarch_regset_from_core_section (gdbarch,
925 sect_list->sect_name,
926 sect_list->size);
927 gdb_assert (regset && regset->collect_regset);
929 buf = xmalloc (sect_list->size);
930 regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
932 /* PRSTATUS still needs to be treated specially. */
933 if (strcmp (sect_list->sect_name, ".reg") == 0)
934 note_data = (char *) elfcore_write_prstatus
935 (obfd, note_data, note_size, lwp,
936 gdb_signal_to_host (stop_signal), buf);
937 else
938 note_data = (char *) elfcore_write_register_note
939 (obfd, note_data, note_size,
940 sect_list->sect_name, buf, sect_list->size);
941 xfree (buf);
942 sect_list++;
944 if (!note_data)
945 return NULL;
948 return note_data;
951 /* Fetch the siginfo data for the current thread, if it exists. If
952 there is no data, or we could not read it, return NULL. Otherwise,
953 return a newly malloc'd buffer holding the data and fill in *SIZE
954 with the size of the data. The caller is responsible for freeing
955 the data. */
957 static gdb_byte *
958 linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
960 struct type *siginfo_type;
961 gdb_byte *buf;
962 LONGEST bytes_read;
963 struct cleanup *cleanups;
965 if (!gdbarch_get_siginfo_type_p (gdbarch))
966 return NULL;
968 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
970 buf = xmalloc (TYPE_LENGTH (siginfo_type));
971 cleanups = make_cleanup (xfree, buf);
973 bytes_read = target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
974 buf, 0, TYPE_LENGTH (siginfo_type));
975 if (bytes_read == TYPE_LENGTH (siginfo_type))
977 discard_cleanups (cleanups);
978 *size = bytes_read;
980 else
982 do_cleanups (cleanups);
983 buf = NULL;
986 return buf;
989 struct linux_corefile_thread_data
991 struct gdbarch *gdbarch;
992 int pid;
993 bfd *obfd;
994 char *note_data;
995 int *note_size;
996 int num_notes;
997 enum gdb_signal stop_signal;
998 linux_collect_thread_registers_ftype collect;
1001 /* Called by gdbthread.c once per thread. Records the thread's
1002 register state for the corefile note section. */
1004 static int
1005 linux_corefile_thread_callback (struct thread_info *info, void *data)
1007 struct linux_corefile_thread_data *args = data;
1009 if (ptid_get_pid (info->ptid) == args->pid)
1011 struct cleanup *old_chain;
1012 struct regcache *regcache;
1013 gdb_byte *siginfo_data;
1014 LONGEST siginfo_size;
1016 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
1018 old_chain = save_inferior_ptid ();
1019 inferior_ptid = info->ptid;
1020 target_fetch_registers (regcache, -1);
1021 siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
1022 do_cleanups (old_chain);
1024 old_chain = make_cleanup (xfree, siginfo_data);
1026 args->note_data = args->collect (regcache, info->ptid, args->obfd,
1027 args->note_data, args->note_size,
1028 args->stop_signal);
1029 args->num_notes++;
1031 if (siginfo_data != NULL)
1033 args->note_data = elfcore_write_note (args->obfd,
1034 args->note_data,
1035 args->note_size,
1036 "CORE", NT_SIGINFO,
1037 siginfo_data, siginfo_size);
1038 args->num_notes++;
1041 do_cleanups (old_chain);
1044 return !args->note_data;
1047 /* Fill the PRPSINFO structure with information about the process being
1048 debugged. Returns 1 in case of success, 0 for failures. Please note that
1049 even if the structure cannot be entirely filled (e.g., GDB was unable to
1050 gather information about the process UID/GID), this function will still
1051 return 1 since some information was already recorded. It will only return
1052 0 iff nothing can be gathered. */
1054 static int
1055 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
1057 /* The filename which we will use to obtain some info about the process.
1058 We will basically use this to store the `/proc/PID/FILENAME' file. */
1059 char filename[100];
1060 /* The full name of the program which generated the corefile. */
1061 char *fname;
1062 /* The basename of the executable. */
1063 const char *basename;
1064 /* The arguments of the program. */
1065 char *psargs;
1066 char *infargs;
1067 /* The contents of `/proc/PID/stat' and `/proc/PID/status' files. */
1068 char *proc_stat, *proc_status;
1069 /* Temporary buffer. */
1070 char *tmpstr;
1071 /* The valid states of a process, according to the Linux kernel. */
1072 const char valid_states[] = "RSDTZW";
1073 /* The program state. */
1074 const char *prog_state;
1075 /* The state of the process. */
1076 char pr_sname;
1077 /* The PID of the program which generated the corefile. */
1078 pid_t pid;
1079 /* Process flags. */
1080 unsigned int pr_flag;
1081 /* Process nice value. */
1082 long pr_nice;
1083 /* The number of fields read by `sscanf'. */
1084 int n_fields = 0;
1085 /* Cleanups. */
1086 struct cleanup *c;
1087 int i;
1089 gdb_assert (p != NULL);
1091 /* Obtaining PID and filename. */
1092 pid = ptid_get_pid (inferior_ptid);
1093 xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
1094 fname = target_fileio_read_stralloc (filename);
1096 if (fname == NULL || *fname == '\0')
1098 /* No program name was read, so we won't be able to retrieve more
1099 information about the process. */
1100 xfree (fname);
1101 return 0;
1104 c = make_cleanup (xfree, fname);
1105 memset (p, 0, sizeof (*p));
1107 /* Defining the PID. */
1108 p->pr_pid = pid;
1110 /* Copying the program name. Only the basename matters. */
1111 basename = lbasename (fname);
1112 strncpy (p->pr_fname, basename, sizeof (p->pr_fname));
1113 p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
1115 infargs = get_inferior_args ();
1117 psargs = xstrdup (fname);
1118 if (infargs != NULL)
1119 psargs = reconcat (psargs, psargs, " ", infargs, NULL);
1121 make_cleanup (xfree, psargs);
1123 strncpy (p->pr_psargs, psargs, sizeof (p->pr_psargs));
1124 p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
1126 xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
1127 proc_stat = target_fileio_read_stralloc (filename);
1128 make_cleanup (xfree, proc_stat);
1130 if (proc_stat == NULL || *proc_stat == '\0')
1132 /* Despite being unable to read more information about the
1133 process, we return 1 here because at least we have its
1134 command line, PID and arguments. */
1135 do_cleanups (c);
1136 return 1;
1139 /* Ok, we have the stats. It's time to do a little parsing of the
1140 contents of the buffer, so that we end up reading what we want.
1142 The following parsing mechanism is strongly based on the
1143 information generated by the `fs/proc/array.c' file, present in
1144 the Linux kernel tree. More details about how the information is
1145 displayed can be obtained by seeing the manpage of proc(5),
1146 specifically under the entry of `/proc/[pid]/stat'. */
1148 /* Getting rid of the PID, since we already have it. */
1149 while (isdigit (*proc_stat))
1150 ++proc_stat;
1152 proc_stat = skip_spaces (proc_stat);
1154 /* Getting rid of the executable name, since we already have it. We
1155 know that this name will be in parentheses, so we can safely look
1156 for the close-paren. */
1157 while (*proc_stat != ')')
1158 ++proc_stat;
1159 ++proc_stat;
1161 proc_stat = skip_spaces (proc_stat);
1163 n_fields = sscanf (proc_stat,
1164 "%c" /* Process state. */
1165 "%d%d%d" /* Parent PID, group ID, session ID. */
1166 "%*d%*d" /* tty_nr, tpgid (not used). */
1167 "%u" /* Flags. */
1168 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1169 cmajflt (not used). */
1170 "%*s%*s%*s%*s" /* utime, stime, cutime,
1171 cstime (not used). */
1172 "%*s" /* Priority (not used). */
1173 "%ld", /* Nice. */
1174 &pr_sname,
1175 &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1176 &pr_flag,
1177 &pr_nice);
1179 if (n_fields != 6)
1181 /* Again, we couldn't read the complementary information about
1182 the process state. However, we already have minimal
1183 information, so we just return 1 here. */
1184 do_cleanups (c);
1185 return 1;
1188 /* Filling the structure fields. */
1189 prog_state = strchr (valid_states, pr_sname);
1190 if (prog_state != NULL)
1191 p->pr_state = prog_state - valid_states;
1192 else
1194 /* Zero means "Running". */
1195 p->pr_state = 0;
1198 p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
1199 p->pr_zomb = p->pr_sname == 'Z';
1200 p->pr_nice = pr_nice;
1201 p->pr_flag = pr_flag;
1203 /* Finally, obtaining the UID and GID. For that, we read and parse the
1204 contents of the `/proc/PID/status' file. */
1205 xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
1206 proc_status = target_fileio_read_stralloc (filename);
1207 make_cleanup (xfree, proc_status);
1209 if (proc_status == NULL || *proc_status == '\0')
1211 /* Returning 1 since we already have a bunch of information. */
1212 do_cleanups (c);
1213 return 1;
1216 /* Extracting the UID. */
1217 tmpstr = strstr (proc_status, "Uid:");
1218 if (tmpstr != NULL)
1220 /* Advancing the pointer to the beginning of the UID. */
1221 tmpstr += sizeof ("Uid:");
1222 while (*tmpstr != '\0' && !isdigit (*tmpstr))
1223 ++tmpstr;
1225 if (isdigit (*tmpstr))
1226 p->pr_uid = strtol (tmpstr, &tmpstr, 10);
1229 /* Extracting the GID. */
1230 tmpstr = strstr (proc_status, "Gid:");
1231 if (tmpstr != NULL)
1233 /* Advancing the pointer to the beginning of the GID. */
1234 tmpstr += sizeof ("Gid:");
1235 while (*tmpstr != '\0' && !isdigit (*tmpstr))
1236 ++tmpstr;
1238 if (isdigit (*tmpstr))
1239 p->pr_gid = strtol (tmpstr, &tmpstr, 10);
1242 do_cleanups (c);
1244 return 1;
1247 /* Fills the "to_make_corefile_note" target vector. Builds the note
1248 section for a corefile, and returns it in a malloc buffer. */
1250 char *
1251 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
1252 linux_collect_thread_registers_ftype collect)
1254 struct linux_corefile_thread_data thread_args;
1255 struct elf_internal_linux_prpsinfo prpsinfo;
1256 char *note_data = NULL;
1257 gdb_byte *auxv;
1258 int auxv_len;
1260 if (linux_fill_prpsinfo (&prpsinfo))
1262 if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch))
1264 note_data = gdbarch_elfcore_write_linux_prpsinfo (gdbarch, obfd,
1265 note_data, note_size,
1266 &prpsinfo);
1268 else
1270 if (gdbarch_ptr_bit (gdbarch) == 64)
1271 note_data = elfcore_write_linux_prpsinfo64 (obfd,
1272 note_data, note_size,
1273 &prpsinfo);
1274 else
1275 note_data = elfcore_write_linux_prpsinfo32 (obfd,
1276 note_data, note_size,
1277 &prpsinfo);
1281 /* Thread register information. */
1282 thread_args.gdbarch = gdbarch;
1283 thread_args.pid = ptid_get_pid (inferior_ptid);
1284 thread_args.obfd = obfd;
1285 thread_args.note_data = note_data;
1286 thread_args.note_size = note_size;
1287 thread_args.num_notes = 0;
1288 thread_args.stop_signal = find_stop_signal ();
1289 thread_args.collect = collect;
1290 iterate_over_threads (linux_corefile_thread_callback, &thread_args);
1291 note_data = thread_args.note_data;
1292 if (!note_data)
1293 return NULL;
1295 /* Auxillary vector. */
1296 auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
1297 NULL, &auxv);
1298 if (auxv_len > 0)
1300 note_data = elfcore_write_note (obfd, note_data, note_size,
1301 "CORE", NT_AUXV, auxv, auxv_len);
1302 xfree (auxv);
1304 if (!note_data)
1305 return NULL;
1308 /* SPU information. */
1309 note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
1310 if (!note_data)
1311 return NULL;
1313 /* File mappings. */
1314 note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
1315 note_data, note_size);
1317 make_cleanup (xfree, note_data);
1318 return note_data;
1321 static char *
1322 linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1324 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
1325 converted to gdbarch_core_regset_sections, we no longer need to fall back
1326 to the target method at this point. */
1328 if (!gdbarch_core_regset_sections (gdbarch))
1329 return target_make_corefile_notes (obfd, note_size);
1330 else
1331 return linux_make_corefile_notes (gdbarch, obfd, note_size,
1332 linux_collect_thread_registers);
1335 /* To be called from the various GDB_OSABI_LINUX handlers for the
1336 various GNU/Linux architectures and machine types. */
1338 void
1339 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1341 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
1342 set_gdbarch_info_proc (gdbarch, linux_info_proc);
1343 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
1344 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
1345 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
1346 set_gdbarch_has_shared_address_space (gdbarch,
1347 linux_has_shared_address_space);
1350 /* Provide a prototype to silence -Wmissing-prototypes. */
1351 extern initialize_file_ftype _initialize_linux_tdep;
1353 void
1354 _initialize_linux_tdep (void)
1356 linux_gdbarch_data_handle =
1357 gdbarch_data_register_post_init (init_linux_gdbarch_data);