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/>. */
22 #include "linux-tdep.h"
25 #include "gdbthread.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h" /* for elfcore_write_* */
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdb_obstack.h"
35 #include "cli/cli-utils.h"
39 static struct gdbarch_data
*linux_gdbarch_data_handle
;
41 struct linux_gdbarch_data
43 struct type
*siginfo_type
;
47 init_linux_gdbarch_data (struct gdbarch
*gdbarch
)
49 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct linux_gdbarch_data
);
52 static struct linux_gdbarch_data
*
53 get_linux_gdbarch_data (struct gdbarch
*gdbarch
)
55 return gdbarch_data (gdbarch
, linux_gdbarch_data_handle
);
58 /* This function is suitable for architectures that don't
59 extend/override the standard siginfo structure. */
62 linux_get_siginfo_type (struct gdbarch
*gdbarch
)
64 struct linux_gdbarch_data
*linux_gdbarch_data
;
65 struct type
*int_type
, *uint_type
, *long_type
, *void_ptr_type
;
66 struct type
*uid_type
, *pid_type
;
67 struct type
*sigval_type
, *clock_type
;
68 struct type
*siginfo_type
, *sifields_type
;
71 linux_gdbarch_data
= get_linux_gdbarch_data (gdbarch
);
72 if (linux_gdbarch_data
->siginfo_type
!= NULL
)
73 return linux_gdbarch_data
->siginfo_type
;
75 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
77 uint_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
79 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
81 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
84 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
85 TYPE_NAME (sigval_type
) = xstrdup ("sigval_t");
86 append_composite_type_field (sigval_type
, "sival_int", int_type
);
87 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
90 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
91 TYPE_LENGTH (int_type
), "__pid_t");
92 TYPE_TARGET_TYPE (pid_type
) = int_type
;
93 TYPE_TARGET_STUB (pid_type
) = 1;
96 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
97 TYPE_LENGTH (uint_type
), "__uid_t");
98 TYPE_TARGET_TYPE (uid_type
) = uint_type
;
99 TYPE_TARGET_STUB (uid_type
) = 1;
102 clock_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
103 TYPE_LENGTH (long_type
), "__clock_t");
104 TYPE_TARGET_TYPE (clock_type
) = long_type
;
105 TYPE_TARGET_STUB (clock_type
) = 1;
108 sifields_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
111 const int si_max_size
= 128;
113 int size_of_int
= gdbarch_int_bit (gdbarch
) / HOST_CHAR_BIT
;
116 if (gdbarch_ptr_bit (gdbarch
) == 64)
117 si_pad_size
= (si_max_size
/ size_of_int
) - 4;
119 si_pad_size
= (si_max_size
/ size_of_int
) - 3;
120 append_composite_type_field (sifields_type
, "_pad",
121 init_vector_type (int_type
, si_pad_size
));
125 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
126 append_composite_type_field (type
, "si_pid", pid_type
);
127 append_composite_type_field (type
, "si_uid", uid_type
);
128 append_composite_type_field (sifields_type
, "_kill", type
);
131 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
132 append_composite_type_field (type
, "si_tid", int_type
);
133 append_composite_type_field (type
, "si_overrun", int_type
);
134 append_composite_type_field (type
, "si_sigval", sigval_type
);
135 append_composite_type_field (sifields_type
, "_timer", type
);
138 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
139 append_composite_type_field (type
, "si_pid", pid_type
);
140 append_composite_type_field (type
, "si_uid", uid_type
);
141 append_composite_type_field (type
, "si_sigval", sigval_type
);
142 append_composite_type_field (sifields_type
, "_rt", type
);
145 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
146 append_composite_type_field (type
, "si_pid", pid_type
);
147 append_composite_type_field (type
, "si_uid", uid_type
);
148 append_composite_type_field (type
, "si_status", int_type
);
149 append_composite_type_field (type
, "si_utime", clock_type
);
150 append_composite_type_field (type
, "si_stime", clock_type
);
151 append_composite_type_field (sifields_type
, "_sigchld", type
);
154 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
155 append_composite_type_field (type
, "si_addr", void_ptr_type
);
156 append_composite_type_field (sifields_type
, "_sigfault", type
);
159 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
160 append_composite_type_field (type
, "si_band", long_type
);
161 append_composite_type_field (type
, "si_fd", int_type
);
162 append_composite_type_field (sifields_type
, "_sigpoll", type
);
165 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
166 TYPE_NAME (siginfo_type
) = xstrdup ("siginfo");
167 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
168 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
169 append_composite_type_field (siginfo_type
, "si_code", int_type
);
170 append_composite_type_field_aligned (siginfo_type
,
171 "_sifields", sifields_type
,
172 TYPE_LENGTH (long_type
));
174 linux_gdbarch_data
->siginfo_type
= siginfo_type
;
180 linux_has_shared_address_space (struct gdbarch
*gdbarch
)
182 /* Determine whether we are running on uClinux or normal Linux
185 int target_is_uclinux
;
188 = (target_auxv_search (¤t_target
, AT_NULL
, &dummy
) > 0
189 && target_auxv_search (¤t_target
, AT_PAGESZ
, &dummy
) == 0);
191 return target_is_uclinux
;
194 /* This is how we want PTIDs from core files to be printed. */
197 linux_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
201 if (ptid_get_lwp (ptid
) != 0)
203 snprintf (buf
, sizeof (buf
), "LWP %ld", ptid_get_lwp (ptid
));
207 return normal_pid_to_str (ptid
);
210 /* Service function for corefiles and info proc. */
213 read_mapping (const char *line
,
214 ULONGEST
*addr
, ULONGEST
*endaddr
,
215 const char **permissions
, size_t *permissions_len
,
217 const char **device
, size_t *device_len
,
219 const char **filename
)
221 const char *p
= line
;
223 *addr
= strtoulst (p
, &p
, 16);
226 *endaddr
= strtoulst (p
, &p
, 16);
228 p
= skip_spaces_const (p
);
230 while (*p
&& !isspace (*p
))
232 *permissions_len
= p
- *permissions
;
234 *offset
= strtoulst (p
, &p
, 16);
236 p
= skip_spaces_const (p
);
238 while (*p
&& !isspace (*p
))
240 *device_len
= p
- *device
;
242 *inode
= strtoulst (p
, &p
, 10);
244 p
= skip_spaces_const (p
);
248 /* Implement the "info proc" command. */
251 linux_info_proc (struct gdbarch
*gdbarch
, char *args
,
252 enum info_proc_what what
)
254 /* A long is used for pid instead of an int to avoid a loss of precision
255 compiler warning from the output of strtoul. */
257 int cmdline_f
= (what
== IP_MINIMAL
|| what
== IP_CMDLINE
|| what
== IP_ALL
);
258 int cwd_f
= (what
== IP_MINIMAL
|| what
== IP_CWD
|| what
== IP_ALL
);
259 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
260 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
261 int status_f
= (what
== IP_STATUS
|| what
== IP_ALL
);
262 int stat_f
= (what
== IP_STAT
|| what
== IP_ALL
);
267 if (args
&& isdigit (args
[0]))
268 pid
= strtoul (args
, &args
, 10);
271 if (!target_has_execution
)
272 error (_("No current process: you must name one."));
273 if (current_inferior ()->fake_pid_p
)
274 error (_("Can't determine the current process's PID: you must name one."));
276 pid
= current_inferior ()->pid
;
279 args
= skip_spaces (args
);
281 error (_("Too many parameters: %s"), args
);
283 printf_filtered (_("process %ld\n"), pid
);
286 xsnprintf (filename
, sizeof filename
, "/proc/%ld/cmdline", pid
);
287 data
= target_fileio_read_stralloc (filename
);
290 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
291 printf_filtered ("cmdline = '%s'\n", data
);
292 do_cleanups (cleanup
);
295 warning (_("unable to open /proc file '%s'"), filename
);
299 xsnprintf (filename
, sizeof filename
, "/proc/%ld/cwd", pid
);
300 data
= target_fileio_readlink (filename
, &target_errno
);
303 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
304 printf_filtered ("cwd = '%s'\n", data
);
305 do_cleanups (cleanup
);
308 warning (_("unable to read link '%s'"), filename
);
312 xsnprintf (filename
, sizeof filename
, "/proc/%ld/exe", pid
);
313 data
= target_fileio_readlink (filename
, &target_errno
);
316 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
317 printf_filtered ("exe = '%s'\n", data
);
318 do_cleanups (cleanup
);
321 warning (_("unable to read link '%s'"), filename
);
325 xsnprintf (filename
, sizeof filename
, "/proc/%ld/maps", pid
);
326 data
= target_fileio_read_stralloc (filename
);
329 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
332 printf_filtered (_("Mapped address spaces:\n\n"));
333 if (gdbarch_addr_bit (gdbarch
) == 32)
335 printf_filtered ("\t%10s %10s %10s %10s %s\n",
338 " Size", " Offset", "objfile");
342 printf_filtered (" %18s %18s %10s %10s %s\n",
345 " Size", " Offset", "objfile");
348 for (line
= strtok (data
, "\n"); line
; line
= strtok (NULL
, "\n"))
350 ULONGEST addr
, endaddr
, offset
, inode
;
351 const char *permissions
, *device
, *filename
;
352 size_t permissions_len
, device_len
;
354 read_mapping (line
, &addr
, &endaddr
,
355 &permissions
, &permissions_len
,
356 &offset
, &device
, &device_len
,
359 if (gdbarch_addr_bit (gdbarch
) == 32)
361 printf_filtered ("\t%10s %10s %10s %10s %s\n",
362 paddress (gdbarch
, addr
),
363 paddress (gdbarch
, endaddr
),
364 hex_string (endaddr
- addr
),
366 *filename
? filename
: "");
370 printf_filtered (" %18s %18s %10s %10s %s\n",
371 paddress (gdbarch
, addr
),
372 paddress (gdbarch
, endaddr
),
373 hex_string (endaddr
- addr
),
375 *filename
? filename
: "");
379 do_cleanups (cleanup
);
382 warning (_("unable to open /proc file '%s'"), filename
);
386 xsnprintf (filename
, sizeof filename
, "/proc/%ld/status", pid
);
387 data
= target_fileio_read_stralloc (filename
);
390 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
391 puts_filtered (data
);
392 do_cleanups (cleanup
);
395 warning (_("unable to open /proc file '%s'"), filename
);
399 xsnprintf (filename
, sizeof filename
, "/proc/%ld/stat", pid
);
400 data
= target_fileio_read_stralloc (filename
);
403 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
404 const char *p
= data
;
406 printf_filtered (_("Process: %s\n"),
407 pulongest (strtoulst (p
, &p
, 10)));
409 p
= skip_spaces_const (p
);
412 const char *ep
= strchr (p
, ')');
415 printf_filtered ("Exec file: %.*s\n",
416 (int) (ep
- p
- 1), p
+ 1);
421 p
= skip_spaces_const (p
);
423 printf_filtered (_("State: %c\n"), *p
++);
426 printf_filtered (_("Parent process: %s\n"),
427 pulongest (strtoulst (p
, &p
, 10)));
429 printf_filtered (_("Process group: %s\n"),
430 pulongest (strtoulst (p
, &p
, 10)));
432 printf_filtered (_("Session id: %s\n"),
433 pulongest (strtoulst (p
, &p
, 10)));
435 printf_filtered (_("TTY: %s\n"),
436 pulongest (strtoulst (p
, &p
, 10)));
438 printf_filtered (_("TTY owner process group: %s\n"),
439 pulongest (strtoulst (p
, &p
, 10)));
442 printf_filtered (_("Flags: %s\n"),
443 hex_string (strtoulst (p
, &p
, 10)));
445 printf_filtered (_("Minor faults (no memory page): %s\n"),
446 pulongest (strtoulst (p
, &p
, 10)));
448 printf_filtered (_("Minor faults, children: %s\n"),
449 pulongest (strtoulst (p
, &p
, 10)));
451 printf_filtered (_("Major faults (memory page faults): %s\n"),
452 pulongest (strtoulst (p
, &p
, 10)));
454 printf_filtered (_("Major faults, children: %s\n"),
455 pulongest (strtoulst (p
, &p
, 10)));
457 printf_filtered (_("utime: %s\n"),
458 pulongest (strtoulst (p
, &p
, 10)));
460 printf_filtered (_("stime: %s\n"),
461 pulongest (strtoulst (p
, &p
, 10)));
463 printf_filtered (_("utime, children: %s\n"),
464 pulongest (strtoulst (p
, &p
, 10)));
466 printf_filtered (_("stime, children: %s\n"),
467 pulongest (strtoulst (p
, &p
, 10)));
469 printf_filtered (_("jiffies remaining in current "
471 pulongest (strtoulst (p
, &p
, 10)));
473 printf_filtered (_("'nice' value: %s\n"),
474 pulongest (strtoulst (p
, &p
, 10)));
476 printf_filtered (_("jiffies until next timeout: %s\n"),
477 pulongest (strtoulst (p
, &p
, 10)));
479 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
480 pulongest (strtoulst (p
, &p
, 10)));
482 printf_filtered (_("start time (jiffies since "
483 "system boot): %s\n"),
484 pulongest (strtoulst (p
, &p
, 10)));
486 printf_filtered (_("Virtual memory size: %s\n"),
487 pulongest (strtoulst (p
, &p
, 10)));
489 printf_filtered (_("Resident set size: %s\n"),
490 pulongest (strtoulst (p
, &p
, 10)));
492 printf_filtered (_("rlim: %s\n"),
493 pulongest (strtoulst (p
, &p
, 10)));
495 printf_filtered (_("Start of text: %s\n"),
496 hex_string (strtoulst (p
, &p
, 10)));
498 printf_filtered (_("End of text: %s\n"),
499 hex_string (strtoulst (p
, &p
, 10)));
501 printf_filtered (_("Start of stack: %s\n"),
502 hex_string (strtoulst (p
, &p
, 10)));
503 #if 0 /* Don't know how architecture-dependent the rest is...
504 Anyway the signal bitmap info is available from "status". */
506 printf_filtered (_("Kernel stack pointer: %s\n"),
507 hex_string (strtoulst (p
, &p
, 10)));
509 printf_filtered (_("Kernel instr pointer: %s\n"),
510 hex_string (strtoulst (p
, &p
, 10)));
512 printf_filtered (_("Pending signals bitmap: %s\n"),
513 hex_string (strtoulst (p
, &p
, 10)));
515 printf_filtered (_("Blocked signals bitmap: %s\n"),
516 hex_string (strtoulst (p
, &p
, 10)));
518 printf_filtered (_("Ignored signals bitmap: %s\n"),
519 hex_string (strtoulst (p
, &p
, 10)));
521 printf_filtered (_("Catched signals bitmap: %s\n"),
522 hex_string (strtoulst (p
, &p
, 10)));
524 printf_filtered (_("wchan (system call): %s\n"),
525 hex_string (strtoulst (p
, &p
, 10)));
527 do_cleanups (cleanup
);
530 warning (_("unable to open /proc file '%s'"), filename
);
534 /* Implement "info proc mappings" for a corefile. */
537 linux_core_info_proc_mappings (struct gdbarch
*gdbarch
, char *args
)
540 ULONGEST count
, page_size
;
541 unsigned char *descdata
, *filenames
, *descend
, *contents
;
543 unsigned int addr_size_bits
, addr_size
;
544 struct cleanup
*cleanup
;
545 struct gdbarch
*core_gdbarch
= gdbarch_from_bfd (core_bfd
);
546 /* We assume this for reading 64-bit core files. */
547 gdb_static_assert (sizeof (ULONGEST
) >= 8);
549 section
= bfd_get_section_by_name (core_bfd
, ".note.linuxcore.file");
552 warning (_("unable to find mappings in core file"));
556 addr_size_bits
= gdbarch_addr_bit (core_gdbarch
);
557 addr_size
= addr_size_bits
/ 8;
558 note_size
= bfd_get_section_size (section
);
560 if (note_size
< 2 * addr_size
)
561 error (_("malformed core note - too short for header"));
563 contents
= xmalloc (note_size
);
564 cleanup
= make_cleanup (xfree
, contents
);
565 if (!bfd_get_section_contents (core_bfd
, section
, contents
, 0, note_size
))
566 error (_("could not get core note contents"));
569 descend
= descdata
+ note_size
;
571 if (descdata
[note_size
- 1] != '\0')
572 error (_("malformed note - does not end with \\0"));
574 count
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
575 descdata
+= addr_size
;
577 page_size
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
578 descdata
+= addr_size
;
580 if (note_size
< 2 * addr_size
+ count
* 3 * addr_size
)
581 error (_("malformed note - too short for supplied file count"));
583 printf_filtered (_("Mapped address spaces:\n\n"));
584 if (gdbarch_addr_bit (gdbarch
) == 32)
586 printf_filtered ("\t%10s %10s %10s %10s %s\n",
589 " Size", " Offset", "objfile");
593 printf_filtered (" %18s %18s %10s %10s %s\n",
596 " Size", " Offset", "objfile");
599 filenames
= descdata
+ count
* 3 * addr_size
;
602 ULONGEST start
, end
, file_ofs
;
604 if (filenames
== descend
)
605 error (_("malformed note - filenames end too early"));
607 start
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
608 descdata
+= addr_size
;
609 end
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
610 descdata
+= addr_size
;
611 file_ofs
= bfd_get (addr_size_bits
, core_bfd
, descdata
);
612 descdata
+= addr_size
;
614 file_ofs
*= page_size
;
616 if (gdbarch_addr_bit (gdbarch
) == 32)
617 printf_filtered ("\t%10s %10s %10s %10s %s\n",
618 paddress (gdbarch
, start
),
619 paddress (gdbarch
, end
),
620 hex_string (end
- start
),
621 hex_string (file_ofs
),
624 printf_filtered (" %18s %18s %10s %10s %s\n",
625 paddress (gdbarch
, start
),
626 paddress (gdbarch
, end
),
627 hex_string (end
- start
),
628 hex_string (file_ofs
),
631 filenames
+= 1 + strlen ((char *) filenames
);
634 do_cleanups (cleanup
);
637 /* Implement "info proc" for a corefile. */
640 linux_core_info_proc (struct gdbarch
*gdbarch
, char *args
,
641 enum info_proc_what what
)
643 int exe_f
= (what
== IP_MINIMAL
|| what
== IP_EXE
|| what
== IP_ALL
);
644 int mappings_f
= (what
== IP_MAPPINGS
|| what
== IP_ALL
);
650 exe
= bfd_core_file_failing_command (core_bfd
);
652 printf_filtered ("exe = '%s'\n", exe
);
654 warning (_("unable to find command name in core file"));
658 linux_core_info_proc_mappings (gdbarch
, args
);
660 if (!exe_f
&& !mappings_f
)
661 error (_("unable to handle request"));
664 typedef int linux_find_memory_region_ftype (ULONGEST vaddr
, ULONGEST size
,
665 ULONGEST offset
, ULONGEST inode
,
667 int exec
, int modified
,
668 const char *filename
,
671 /* List memory regions in the inferior for a corefile. */
674 linux_find_memory_regions_full (struct gdbarch
*gdbarch
,
675 linux_find_memory_region_ftype
*func
,
681 /* We need to know the real target PID to access /proc. */
682 if (current_inferior ()->fake_pid_p
)
685 xsnprintf (filename
, sizeof filename
,
686 "/proc/%d/smaps", current_inferior ()->pid
);
687 data
= target_fileio_read_stralloc (filename
);
690 /* Older Linux kernels did not support /proc/PID/smaps. */
691 xsnprintf (filename
, sizeof filename
,
692 "/proc/%d/maps", current_inferior ()->pid
);
693 data
= target_fileio_read_stralloc (filename
);
697 struct cleanup
*cleanup
= make_cleanup (xfree
, data
);
700 line
= strtok (data
, "\n");
703 ULONGEST addr
, endaddr
, offset
, inode
;
704 const char *permissions
, *device
, *filename
;
705 size_t permissions_len
, device_len
;
706 int read
, write
, exec
;
707 int modified
= 0, has_anonymous
= 0;
709 read_mapping (line
, &addr
, &endaddr
, &permissions
, &permissions_len
,
710 &offset
, &device
, &device_len
, &inode
, &filename
);
712 /* Decode permissions. */
713 read
= (memchr (permissions
, 'r', permissions_len
) != 0);
714 write
= (memchr (permissions
, 'w', permissions_len
) != 0);
715 exec
= (memchr (permissions
, 'x', permissions_len
) != 0);
717 /* Try to detect if region was modified by parsing smaps counters. */
718 for (line
= strtok (NULL
, "\n");
719 line
&& line
[0] >= 'A' && line
[0] <= 'Z';
720 line
= strtok (NULL
, "\n"))
722 char keyword
[64 + 1];
723 unsigned long number
;
725 if (sscanf (line
, "%64s%lu kB\n", keyword
, &number
) != 2)
727 warning (_("Error parsing {s,}maps file '%s'"), filename
);
730 if (strcmp (keyword
, "Anonymous:") == 0)
732 if (number
!= 0 && (strcmp (keyword
, "Shared_Dirty:") == 0
733 || strcmp (keyword
, "Private_Dirty:") == 0
734 || strcmp (keyword
, "Swap:") == 0
735 || strcmp (keyword
, "Anonymous:") == 0))
739 /* Older Linux kernels did not support the "Anonymous:" counter.
740 If it is missing, we can't be sure - dump all the pages. */
744 /* Invoke the callback function to create the corefile segment. */
745 func (addr
, endaddr
- addr
, offset
, inode
,
746 read
, write
, exec
, modified
, filename
, obfd
);
749 do_cleanups (cleanup
);
756 /* A structure for passing information through
757 linux_find_memory_regions_full. */
759 struct linux_find_memory_regions_data
761 /* The original callback. */
763 find_memory_region_ftype func
;
765 /* The original datum. */
770 /* A callback for linux_find_memory_regions that converts between the
771 "full"-style callback and find_memory_region_ftype. */
774 linux_find_memory_regions_thunk (ULONGEST vaddr
, ULONGEST size
,
775 ULONGEST offset
, ULONGEST inode
,
776 int read
, int write
, int exec
, int modified
,
777 const char *filename
, void *arg
)
779 struct linux_find_memory_regions_data
*data
= arg
;
781 return data
->func (vaddr
, size
, read
, write
, exec
, modified
, data
->obfd
);
784 /* A variant of linux_find_memory_regions_full that is suitable as the
785 gdbarch find_memory_regions method. */
788 linux_find_memory_regions (struct gdbarch
*gdbarch
,
789 find_memory_region_ftype func
, void *obfd
)
791 struct linux_find_memory_regions_data data
;
796 return linux_find_memory_regions_full (gdbarch
,
797 linux_find_memory_regions_thunk
,
801 /* Determine which signal stopped execution. */
804 find_signalled_thread (struct thread_info
*info
, void *data
)
806 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
807 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
813 static enum gdb_signal
814 find_stop_signal (void)
816 struct thread_info
*info
=
817 iterate_over_threads (find_signalled_thread
, NULL
);
820 return info
->suspend
.stop_signal
;
825 /* Generate corefile notes for SPU contexts. */
828 linux_spu_make_corefile_notes (bfd
*obfd
, char *note_data
, int *note_size
)
830 static const char *spu_files
[] =
852 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
856 /* Determine list of SPU ids. */
857 size
= target_read_alloc (¤t_target
, TARGET_OBJECT_SPU
,
860 /* Generate corefile notes for each SPU file. */
861 for (i
= 0; i
< size
; i
+= 4)
863 int fd
= extract_unsigned_integer (spu_ids
+ i
, 4, byte_order
);
865 for (j
= 0; j
< sizeof (spu_files
) / sizeof (spu_files
[0]); j
++)
867 char annex
[32], note_name
[32];
871 xsnprintf (annex
, sizeof annex
, "%d/%s", fd
, spu_files
[j
]);
872 spu_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_SPU
,
876 xsnprintf (note_name
, sizeof note_name
, "SPU/%s", annex
);
877 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
897 /* This is used to pass information from
898 linux_make_mappings_corefile_notes through
899 linux_find_memory_regions_full. */
901 struct linux_make_mappings_data
903 /* Number of files mapped. */
906 /* The obstack for the main part of the data. */
907 struct obstack
*data_obstack
;
909 /* The filename obstack. */
910 struct obstack
*filename_obstack
;
912 /* The architecture's "long" type. */
913 struct type
*long_type
;
916 static linux_find_memory_region_ftype linux_make_mappings_callback
;
918 /* A callback for linux_find_memory_regions_full that updates the
919 mappings data for linux_make_mappings_corefile_notes. */
922 linux_make_mappings_callback (ULONGEST vaddr
, ULONGEST size
,
923 ULONGEST offset
, ULONGEST inode
,
924 int read
, int write
, int exec
, int modified
,
925 const char *filename
, void *data
)
927 struct linux_make_mappings_data
*map_data
= data
;
928 gdb_byte buf
[sizeof (ULONGEST
)];
930 if (*filename
== '\0' || inode
== 0)
933 ++map_data
->file_count
;
935 pack_long (buf
, map_data
->long_type
, vaddr
);
936 obstack_grow (map_data
->data_obstack
, buf
, TYPE_LENGTH (map_data
->long_type
));
937 pack_long (buf
, map_data
->long_type
, vaddr
+ size
);
938 obstack_grow (map_data
->data_obstack
, buf
, TYPE_LENGTH (map_data
->long_type
));
939 pack_long (buf
, map_data
->long_type
, offset
);
940 obstack_grow (map_data
->data_obstack
, buf
, TYPE_LENGTH (map_data
->long_type
));
942 obstack_grow_str0 (map_data
->filename_obstack
, filename
);
947 /* Write the file mapping data to the core file, if possible. OBFD is
948 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
949 is a pointer to the note size. Returns the new NOTE_DATA and
950 updates NOTE_SIZE. */
953 linux_make_mappings_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
,
954 char *note_data
, int *note_size
)
956 struct cleanup
*cleanup
;
957 struct obstack data_obstack
, filename_obstack
;
958 struct linux_make_mappings_data mapping_data
;
959 struct type
*long_type
960 = arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
), 0, "long");
961 gdb_byte buf
[sizeof (ULONGEST
)];
963 obstack_init (&data_obstack
);
964 cleanup
= make_cleanup_obstack_free (&data_obstack
);
965 obstack_init (&filename_obstack
);
966 make_cleanup_obstack_free (&filename_obstack
);
968 mapping_data
.file_count
= 0;
969 mapping_data
.data_obstack
= &data_obstack
;
970 mapping_data
.filename_obstack
= &filename_obstack
;
971 mapping_data
.long_type
= long_type
;
973 /* Reserve space for the count. */
974 obstack_blank (&data_obstack
, TYPE_LENGTH (long_type
));
975 /* We always write the page size as 1 since we have no good way to
976 determine the correct value. */
977 pack_long (buf
, long_type
, 1);
978 obstack_grow (&data_obstack
, buf
, TYPE_LENGTH (long_type
));
980 linux_find_memory_regions_full (gdbarch
, linux_make_mappings_callback
,
983 if (mapping_data
.file_count
!= 0)
985 /* Write the count to the obstack. */
986 pack_long (obstack_base (&data_obstack
), long_type
,
987 mapping_data
.file_count
);
989 /* Copy the filenames to the data obstack. */
990 obstack_grow (&data_obstack
, obstack_base (&filename_obstack
),
991 obstack_object_size (&filename_obstack
));
993 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
995 obstack_base (&data_obstack
),
996 obstack_object_size (&data_obstack
));
999 do_cleanups (cleanup
);
1003 /* Records the thread's register state for the corefile note
1007 linux_collect_thread_registers (const struct regcache
*regcache
,
1008 ptid_t ptid
, bfd
*obfd
,
1009 char *note_data
, int *note_size
,
1010 enum gdb_signal stop_signal
)
1012 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1013 struct core_regset_section
*sect_list
;
1016 sect_list
= gdbarch_core_regset_sections (gdbarch
);
1017 gdb_assert (sect_list
);
1019 /* For remote targets the LWP may not be available, so use the TID. */
1020 lwp
= ptid_get_lwp (ptid
);
1022 lwp
= ptid_get_tid (ptid
);
1024 while (sect_list
->sect_name
!= NULL
)
1026 const struct regset
*regset
;
1029 regset
= gdbarch_regset_from_core_section (gdbarch
,
1030 sect_list
->sect_name
,
1032 gdb_assert (regset
&& regset
->collect_regset
);
1034 buf
= xmalloc (sect_list
->size
);
1035 regset
->collect_regset (regset
, regcache
, -1, buf
, sect_list
->size
);
1037 /* PRSTATUS still needs to be treated specially. */
1038 if (strcmp (sect_list
->sect_name
, ".reg") == 0)
1039 note_data
= (char *) elfcore_write_prstatus
1040 (obfd
, note_data
, note_size
, lwp
,
1041 gdb_signal_to_host (stop_signal
), buf
);
1043 note_data
= (char *) elfcore_write_register_note
1044 (obfd
, note_data
, note_size
,
1045 sect_list
->sect_name
, buf
, sect_list
->size
);
1056 /* Fetch the siginfo data for the current thread, if it exists. If
1057 there is no data, or we could not read it, return NULL. Otherwise,
1058 return a newly malloc'd buffer holding the data and fill in *SIZE
1059 with the size of the data. The caller is responsible for freeing
1063 linux_get_siginfo_data (struct gdbarch
*gdbarch
, LONGEST
*size
)
1065 struct type
*siginfo_type
;
1068 struct cleanup
*cleanups
;
1070 if (!gdbarch_get_siginfo_type_p (gdbarch
))
1073 siginfo_type
= gdbarch_get_siginfo_type (gdbarch
);
1075 buf
= xmalloc (TYPE_LENGTH (siginfo_type
));
1076 cleanups
= make_cleanup (xfree
, buf
);
1078 bytes_read
= target_read (¤t_target
, TARGET_OBJECT_SIGNAL_INFO
, NULL
,
1079 buf
, 0, TYPE_LENGTH (siginfo_type
));
1080 if (bytes_read
== TYPE_LENGTH (siginfo_type
))
1082 discard_cleanups (cleanups
);
1087 do_cleanups (cleanups
);
1094 struct linux_corefile_thread_data
1096 struct gdbarch
*gdbarch
;
1102 enum gdb_signal stop_signal
;
1103 linux_collect_thread_registers_ftype collect
;
1106 /* Called by gdbthread.c once per thread. Records the thread's
1107 register state for the corefile note section. */
1110 linux_corefile_thread_callback (struct thread_info
*info
, void *data
)
1112 struct linux_corefile_thread_data
*args
= data
;
1114 if (ptid_get_pid (info
->ptid
) == args
->pid
)
1116 struct cleanup
*old_chain
;
1117 struct regcache
*regcache
;
1118 gdb_byte
*siginfo_data
;
1119 LONGEST siginfo_size
;
1121 regcache
= get_thread_arch_regcache (info
->ptid
, args
->gdbarch
);
1123 old_chain
= save_inferior_ptid ();
1124 inferior_ptid
= info
->ptid
;
1125 target_fetch_registers (regcache
, -1);
1126 siginfo_data
= linux_get_siginfo_data (args
->gdbarch
, &siginfo_size
);
1127 do_cleanups (old_chain
);
1129 old_chain
= make_cleanup (xfree
, siginfo_data
);
1131 args
->note_data
= args
->collect (regcache
, info
->ptid
, args
->obfd
,
1132 args
->note_data
, args
->note_size
,
1136 if (siginfo_data
!= NULL
)
1138 args
->note_data
= elfcore_write_note (args
->obfd
,
1142 siginfo_data
, siginfo_size
);
1146 do_cleanups (old_chain
);
1149 return !args
->note_data
;
1152 /* Fill the PRPSINFO structure with information about the process being
1153 debugged. Returns 1 in case of success, 0 for failures. Please note that
1154 even if the structure cannot be entirely filled (e.g., GDB was unable to
1155 gather information about the process UID/GID), this function will still
1156 return 1 since some information was already recorded. It will only return
1157 0 iff nothing can be gathered. */
1160 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo
*p
)
1162 /* The filename which we will use to obtain some info about the process.
1163 We will basically use this to store the `/proc/PID/FILENAME' file. */
1165 /* The full name of the program which generated the corefile. */
1167 /* The basename of the executable. */
1168 const char *basename
;
1169 /* The arguments of the program. */
1172 /* The contents of `/proc/PID/stat' and `/proc/PID/status' files. */
1173 char *proc_stat
, *proc_status
;
1174 /* Temporary buffer. */
1176 /* The valid states of a process, according to the Linux kernel. */
1177 const char valid_states
[] = "RSDTZW";
1178 /* The program state. */
1179 const char *prog_state
;
1180 /* The state of the process. */
1182 /* The PID of the program which generated the corefile. */
1184 /* Process flags. */
1185 unsigned int pr_flag
;
1186 /* Process nice value. */
1188 /* The number of fields read by `sscanf'. */
1194 gdb_assert (p
!= NULL
);
1196 /* Obtaining PID and filename. */
1197 pid
= ptid_get_pid (inferior_ptid
);
1198 xsnprintf (filename
, sizeof (filename
), "/proc/%d/cmdline", (int) pid
);
1199 fname
= target_fileio_read_stralloc (filename
);
1201 if (fname
== NULL
|| *fname
== '\0')
1203 /* No program name was read, so we won't be able to retrieve more
1204 information about the process. */
1209 c
= make_cleanup (xfree
, fname
);
1210 memset (p
, 0, sizeof (*p
));
1212 /* Defining the PID. */
1215 /* Copying the program name. Only the basename matters. */
1216 basename
= lbasename (fname
);
1217 strncpy (p
->pr_fname
, basename
, sizeof (p
->pr_fname
));
1218 p
->pr_fname
[sizeof (p
->pr_fname
) - 1] = '\0';
1220 infargs
= get_inferior_args ();
1222 psargs
= xstrdup (fname
);
1223 if (infargs
!= NULL
)
1224 psargs
= reconcat (psargs
, psargs
, " ", infargs
, NULL
);
1226 make_cleanup (xfree
, psargs
);
1228 strncpy (p
->pr_psargs
, psargs
, sizeof (p
->pr_psargs
));
1229 p
->pr_psargs
[sizeof (p
->pr_psargs
) - 1] = '\0';
1231 xsnprintf (filename
, sizeof (filename
), "/proc/%d/stat", (int) pid
);
1232 proc_stat
= target_fileio_read_stralloc (filename
);
1233 make_cleanup (xfree
, proc_stat
);
1235 if (proc_stat
== NULL
|| *proc_stat
== '\0')
1237 /* Despite being unable to read more information about the
1238 process, we return 1 here because at least we have its
1239 command line, PID and arguments. */
1244 /* Ok, we have the stats. It's time to do a little parsing of the
1245 contents of the buffer, so that we end up reading what we want.
1247 The following parsing mechanism is strongly based on the
1248 information generated by the `fs/proc/array.c' file, present in
1249 the Linux kernel tree. More details about how the information is
1250 displayed can be obtained by seeing the manpage of proc(5),
1251 specifically under the entry of `/proc/[pid]/stat'. */
1253 /* Getting rid of the PID, since we already have it. */
1254 while (isdigit (*proc_stat
))
1257 proc_stat
= skip_spaces (proc_stat
);
1259 /* Getting rid of the executable name, since we already have it. We
1260 know that this name will be in parentheses, so we can safely look
1261 for the close-paren. */
1262 while (*proc_stat
!= ')')
1266 proc_stat
= skip_spaces (proc_stat
);
1268 n_fields
= sscanf (proc_stat
,
1269 "%c" /* Process state. */
1270 "%d%d%d" /* Parent PID, group ID, session ID. */
1271 "%*d%*d" /* tty_nr, tpgid (not used). */
1273 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1274 cmajflt (not used). */
1275 "%*s%*s%*s%*s" /* utime, stime, cutime,
1276 cstime (not used). */
1277 "%*s" /* Priority (not used). */
1280 &p
->pr_ppid
, &p
->pr_pgrp
, &p
->pr_sid
,
1286 /* Again, we couldn't read the complementary information about
1287 the process state. However, we already have minimal
1288 information, so we just return 1 here. */
1293 /* Filling the structure fields. */
1294 prog_state
= strchr (valid_states
, pr_sname
);
1295 if (prog_state
!= NULL
)
1296 p
->pr_state
= prog_state
- valid_states
;
1299 /* Zero means "Running". */
1303 p
->pr_sname
= p
->pr_state
> 5 ? '.' : pr_sname
;
1304 p
->pr_zomb
= p
->pr_sname
== 'Z';
1305 p
->pr_nice
= pr_nice
;
1306 p
->pr_flag
= pr_flag
;
1308 /* Finally, obtaining the UID and GID. For that, we read and parse the
1309 contents of the `/proc/PID/status' file. */
1310 xsnprintf (filename
, sizeof (filename
), "/proc/%d/status", (int) pid
);
1311 proc_status
= target_fileio_read_stralloc (filename
);
1312 make_cleanup (xfree
, proc_status
);
1314 if (proc_status
== NULL
|| *proc_status
== '\0')
1316 /* Returning 1 since we already have a bunch of information. */
1321 /* Extracting the UID. */
1322 tmpstr
= strstr (proc_status
, "Uid:");
1325 /* Advancing the pointer to the beginning of the UID. */
1326 tmpstr
+= sizeof ("Uid:");
1327 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
1330 if (isdigit (*tmpstr
))
1331 p
->pr_uid
= strtol (tmpstr
, &tmpstr
, 10);
1334 /* Extracting the GID. */
1335 tmpstr
= strstr (proc_status
, "Gid:");
1338 /* Advancing the pointer to the beginning of the GID. */
1339 tmpstr
+= sizeof ("Gid:");
1340 while (*tmpstr
!= '\0' && !isdigit (*tmpstr
))
1343 if (isdigit (*tmpstr
))
1344 p
->pr_gid
= strtol (tmpstr
, &tmpstr
, 10);
1352 /* Fills the "to_make_corefile_note" target vector. Builds the note
1353 section for a corefile, and returns it in a malloc buffer. */
1356 linux_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
,
1357 linux_collect_thread_registers_ftype collect
)
1359 struct linux_corefile_thread_data thread_args
;
1360 struct elf_internal_linux_prpsinfo prpsinfo
;
1361 char *note_data
= NULL
;
1365 if (linux_fill_prpsinfo (&prpsinfo
))
1367 if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch
))
1369 note_data
= gdbarch_elfcore_write_linux_prpsinfo (gdbarch
, obfd
,
1370 note_data
, note_size
,
1375 if (gdbarch_ptr_bit (gdbarch
) == 64)
1376 note_data
= elfcore_write_linux_prpsinfo64 (obfd
,
1377 note_data
, note_size
,
1380 note_data
= elfcore_write_linux_prpsinfo32 (obfd
,
1381 note_data
, note_size
,
1386 /* Thread register information. */
1387 thread_args
.gdbarch
= gdbarch
;
1388 thread_args
.pid
= ptid_get_pid (inferior_ptid
);
1389 thread_args
.obfd
= obfd
;
1390 thread_args
.note_data
= note_data
;
1391 thread_args
.note_size
= note_size
;
1392 thread_args
.num_notes
= 0;
1393 thread_args
.stop_signal
= find_stop_signal ();
1394 thread_args
.collect
= collect
;
1395 iterate_over_threads (linux_corefile_thread_callback
, &thread_args
);
1396 note_data
= thread_args
.note_data
;
1400 /* Auxillary vector. */
1401 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
1405 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
1406 "CORE", NT_AUXV
, auxv
, auxv_len
);
1413 /* SPU information. */
1414 note_data
= linux_spu_make_corefile_notes (obfd
, note_data
, note_size
);
1418 /* File mappings. */
1419 note_data
= linux_make_mappings_corefile_notes (gdbarch
, obfd
,
1420 note_data
, note_size
);
1422 make_cleanup (xfree
, note_data
);
1427 linux_make_corefile_notes_1 (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
1429 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
1430 converted to gdbarch_core_regset_sections, we no longer need to fall back
1431 to the target method at this point. */
1433 if (!gdbarch_core_regset_sections (gdbarch
))
1434 return target_make_corefile_notes (obfd
, note_size
);
1436 return linux_make_corefile_notes (gdbarch
, obfd
, note_size
,
1437 linux_collect_thread_registers
);
1440 /* To be called from the various GDB_OSABI_LINUX handlers for the
1441 various GNU/Linux architectures and machine types. */
1444 linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1446 set_gdbarch_core_pid_to_str (gdbarch
, linux_core_pid_to_str
);
1447 set_gdbarch_info_proc (gdbarch
, linux_info_proc
);
1448 set_gdbarch_core_info_proc (gdbarch
, linux_core_info_proc
);
1449 set_gdbarch_find_memory_regions (gdbarch
, linux_find_memory_regions
);
1450 set_gdbarch_make_corefile_notes (gdbarch
, linux_make_corefile_notes_1
);
1451 set_gdbarch_has_shared_address_space (gdbarch
,
1452 linux_has_shared_address_space
);
1455 /* Provide a prototype to silence -Wmissing-prototypes. */
1456 extern initialize_file_ftype _initialize_linux_tdep
;
1459 _initialize_linux_tdep (void)
1461 linux_gdbarch_data_handle
=
1462 gdbarch_data_register_post_init (init_linux_gdbarch_data
);