gdb
[deliverable/binutils-gdb.git] / gdb / linux-tdep.c
CommitLineData
4aa995e1
PA
1/* Target-dependent code for GNU/Linux, architecture independent.
2
0b302171 3 Copyright (C) 2009-2012 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"
3030c96e
UW
35
36#include <ctype.h>
4aa995e1 37
06253dd3
JK
38static struct gdbarch_data *linux_gdbarch_data_handle;
39
40struct linux_gdbarch_data
41 {
42 struct type *siginfo_type;
43 };
44
45static void *
46init_linux_gdbarch_data (struct gdbarch *gdbarch)
47{
48 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
49}
50
51static struct linux_gdbarch_data *
52get_linux_gdbarch_data (struct gdbarch *gdbarch)
53{
54 return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
55}
56
4aa995e1
PA
57/* This function is suitable for architectures that don't
58 extend/override the standard siginfo structure. */
59
60struct type *
61linux_get_siginfo_type (struct gdbarch *gdbarch)
62{
06253dd3 63 struct linux_gdbarch_data *linux_gdbarch_data;
4aa995e1
PA
64 struct type *int_type, *uint_type, *long_type, *void_ptr_type;
65 struct type *uid_type, *pid_type;
66 struct type *sigval_type, *clock_type;
67 struct type *siginfo_type, *sifields_type;
68 struct type *type;
69
06253dd3
JK
70 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
71 if (linux_gdbarch_data->siginfo_type != NULL)
72 return linux_gdbarch_data->siginfo_type;
73
e9bb382b
UW
74 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
75 0, "int");
76 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
77 1, "unsigned int");
78 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
79 0, "long");
4aa995e1
PA
80 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
81
82 /* sival_t */
e9bb382b 83 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
4aa995e1
PA
84 TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
85 append_composite_type_field (sigval_type, "sival_int", int_type);
86 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
87
88 /* __pid_t */
e3aa49af
MS
89 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
90 TYPE_LENGTH (int_type), "__pid_t");
4aa995e1 91 TYPE_TARGET_TYPE (pid_type) = int_type;
e9bb382b 92 TYPE_TARGET_STUB (pid_type) = 1;
4aa995e1
PA
93
94 /* __uid_t */
e3aa49af
MS
95 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
96 TYPE_LENGTH (uint_type), "__uid_t");
4aa995e1 97 TYPE_TARGET_TYPE (uid_type) = uint_type;
e9bb382b 98 TYPE_TARGET_STUB (uid_type) = 1;
4aa995e1
PA
99
100 /* __clock_t */
e3aa49af
MS
101 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
102 TYPE_LENGTH (long_type), "__clock_t");
4aa995e1 103 TYPE_TARGET_TYPE (clock_type) = long_type;
e9bb382b 104 TYPE_TARGET_STUB (clock_type) = 1;
4aa995e1
PA
105
106 /* _sifields */
e9bb382b 107 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
4aa995e1
PA
108
109 {
110 const int si_max_size = 128;
111 int si_pad_size;
112 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
113
114 /* _pad */
115 if (gdbarch_ptr_bit (gdbarch) == 64)
116 si_pad_size = (si_max_size / size_of_int) - 4;
117 else
118 si_pad_size = (si_max_size / size_of_int) - 3;
119 append_composite_type_field (sifields_type, "_pad",
120 init_vector_type (int_type, si_pad_size));
121 }
122
123 /* _kill */
e9bb382b 124 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
125 append_composite_type_field (type, "si_pid", pid_type);
126 append_composite_type_field (type, "si_uid", uid_type);
127 append_composite_type_field (sifields_type, "_kill", type);
128
129 /* _timer */
e9bb382b 130 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
131 append_composite_type_field (type, "si_tid", int_type);
132 append_composite_type_field (type, "si_overrun", int_type);
133 append_composite_type_field (type, "si_sigval", sigval_type);
134 append_composite_type_field (sifields_type, "_timer", type);
135
136 /* _rt */
e9bb382b 137 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
138 append_composite_type_field (type, "si_pid", pid_type);
139 append_composite_type_field (type, "si_uid", uid_type);
140 append_composite_type_field (type, "si_sigval", sigval_type);
141 append_composite_type_field (sifields_type, "_rt", type);
142
143 /* _sigchld */
e9bb382b 144 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
145 append_composite_type_field (type, "si_pid", pid_type);
146 append_composite_type_field (type, "si_uid", uid_type);
147 append_composite_type_field (type, "si_status", int_type);
148 append_composite_type_field (type, "si_utime", clock_type);
149 append_composite_type_field (type, "si_stime", clock_type);
150 append_composite_type_field (sifields_type, "_sigchld", type);
151
152 /* _sigfault */
e9bb382b 153 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
154 append_composite_type_field (type, "si_addr", void_ptr_type);
155 append_composite_type_field (sifields_type, "_sigfault", type);
156
157 /* _sigpoll */
e9bb382b 158 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
159 append_composite_type_field (type, "si_band", long_type);
160 append_composite_type_field (type, "si_fd", int_type);
161 append_composite_type_field (sifields_type, "_sigpoll", type);
162
163 /* struct siginfo */
e9bb382b 164 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
165 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
166 append_composite_type_field (siginfo_type, "si_signo", int_type);
167 append_composite_type_field (siginfo_type, "si_errno", int_type);
168 append_composite_type_field (siginfo_type, "si_code", int_type);
169 append_composite_type_field_aligned (siginfo_type,
170 "_sifields", sifields_type,
171 TYPE_LENGTH (long_type));
172
06253dd3
JK
173 linux_gdbarch_data->siginfo_type = siginfo_type;
174
4aa995e1
PA
175 return siginfo_type;
176}
6b3ae818 177
33fbcbee
PA
178static int
179linux_has_shared_address_space (struct gdbarch *gdbarch)
6c95b8df
PA
180{
181 /* Determine whether we are running on uClinux or normal Linux
182 kernel. */
183 CORE_ADDR dummy;
184 int target_is_uclinux;
185
186 target_is_uclinux
187 = (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
188 && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
189
190 return target_is_uclinux;
191}
a5ee0f0c
PA
192
193/* This is how we want PTIDs from core files to be printed. */
194
195static char *
196linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
197{
198 static char buf[80];
199
200 if (ptid_get_lwp (ptid) != 0)
201 {
202 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
203 return buf;
204 }
205
206 return normal_pid_to_str (ptid);
207}
208
3030c96e
UW
209/* Service function for corefiles and info proc. */
210
211static void
212read_mapping (const char *line,
213 ULONGEST *addr, ULONGEST *endaddr,
214 const char **permissions, size_t *permissions_len,
215 ULONGEST *offset,
216 const char **device, size_t *device_len,
217 ULONGEST *inode,
218 const char **filename)
219{
220 const char *p = line;
221
222 *addr = strtoulst (p, &p, 16);
223 if (*p == '-')
224 p++;
225 *endaddr = strtoulst (p, &p, 16);
226
227 while (*p && isspace (*p))
228 p++;
229 *permissions = p;
230 while (*p && !isspace (*p))
231 p++;
232 *permissions_len = p - *permissions;
233
234 *offset = strtoulst (p, &p, 16);
235
236 while (*p && isspace (*p))
237 p++;
238 *device = p;
239 while (*p && !isspace (*p))
240 p++;
241 *device_len = p - *device;
242
243 *inode = strtoulst (p, &p, 10);
244
245 while (*p && isspace (*p))
246 p++;
247 *filename = p;
248}
249
250/* Implement the "info proc" command. */
251
252static void
253linux_info_proc (struct gdbarch *gdbarch, char *args,
254 enum info_proc_what what)
255{
256 /* A long is used for pid instead of an int to avoid a loss of precision
257 compiler warning from the output of strtoul. */
258 long pid;
259 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
260 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
261 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
262 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
263 int status_f = (what == IP_STATUS || what == IP_ALL);
264 int stat_f = (what == IP_STAT || what == IP_ALL);
265 char filename[100];
266 gdb_byte *data;
267 int target_errno;
268
269 if (args && isdigit (args[0]))
270 pid = strtoul (args, &args, 10);
271 else
272 {
273 if (!target_has_execution)
274 error (_("No current process: you must name one."));
275 if (current_inferior ()->fake_pid_p)
276 error (_("Can't determine the current process's PID: you must name one."));
277
278 pid = current_inferior ()->pid;
279 }
280
281 args = skip_spaces (args);
282 if (args && args[0])
283 error (_("Too many parameters: %s"), args);
284
285 printf_filtered (_("process %ld\n"), pid);
286 if (cmdline_f)
287 {
288 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
289 data = target_fileio_read_stralloc (filename);
290 if (data)
291 {
292 struct cleanup *cleanup = make_cleanup (xfree, data);
293 printf_filtered ("cmdline = '%s'\n", data);
294 do_cleanups (cleanup);
295 }
296 else
297 warning (_("unable to open /proc file '%s'"), filename);
298 }
299 if (cwd_f)
300 {
301 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
302 data = target_fileio_readlink (filename, &target_errno);
303 if (data)
304 {
305 struct cleanup *cleanup = make_cleanup (xfree, data);
306 printf_filtered ("cwd = '%s'\n", data);
307 do_cleanups (cleanup);
308 }
309 else
310 warning (_("unable to read link '%s'"), filename);
311 }
312 if (exe_f)
313 {
314 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
315 data = target_fileio_readlink (filename, &target_errno);
316 if (data)
317 {
318 struct cleanup *cleanup = make_cleanup (xfree, data);
319 printf_filtered ("exe = '%s'\n", data);
320 do_cleanups (cleanup);
321 }
322 else
323 warning (_("unable to read link '%s'"), filename);
324 }
325 if (mappings_f)
326 {
327 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
328 data = target_fileio_read_stralloc (filename);
329 if (data)
330 {
331 struct cleanup *cleanup = make_cleanup (xfree, data);
332 char *line;
333
334 printf_filtered (_("Mapped address spaces:\n\n"));
335 if (gdbarch_addr_bit (gdbarch) == 32)
336 {
337 printf_filtered ("\t%10s %10s %10s %10s %s\n",
338 "Start Addr",
339 " End Addr",
340 " Size", " Offset", "objfile");
341 }
342 else
343 {
344 printf_filtered (" %18s %18s %10s %10s %s\n",
345 "Start Addr",
346 " End Addr",
347 " Size", " Offset", "objfile");
348 }
349
350 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
351 {
352 ULONGEST addr, endaddr, offset, inode;
353 const char *permissions, *device, *filename;
354 size_t permissions_len, device_len;
355
356 read_mapping (line, &addr, &endaddr,
357 &permissions, &permissions_len,
358 &offset, &device, &device_len,
359 &inode, &filename);
360
361 if (gdbarch_addr_bit (gdbarch) == 32)
362 {
363 printf_filtered ("\t%10s %10s %10s %10s %s\n",
364 paddress (gdbarch, addr),
365 paddress (gdbarch, endaddr),
366 hex_string (endaddr - addr),
367 hex_string (offset),
368 *filename? filename : "");
369 }
370 else
371 {
372 printf_filtered (" %18s %18s %10s %10s %s\n",
373 paddress (gdbarch, addr),
374 paddress (gdbarch, endaddr),
375 hex_string (endaddr - addr),
376 hex_string (offset),
377 *filename? filename : "");
378 }
379 }
380
381 do_cleanups (cleanup);
382 }
383 else
384 warning (_("unable to open /proc file '%s'"), filename);
385 }
386 if (status_f)
387 {
388 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
389 data = target_fileio_read_stralloc (filename);
390 if (data)
391 {
392 struct cleanup *cleanup = make_cleanup (xfree, data);
393 puts_filtered (data);
394 do_cleanups (cleanup);
395 }
396 else
397 warning (_("unable to open /proc file '%s'"), filename);
398 }
399 if (stat_f)
400 {
401 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
402 data = target_fileio_read_stralloc (filename);
403 if (data)
404 {
405 struct cleanup *cleanup = make_cleanup (xfree, data);
406 const char *p = data;
3030c96e
UW
407
408 printf_filtered (_("Process: %s\n"),
409 pulongest (strtoulst (p, &p, 10)));
410
411 while (*p && isspace (*p))
412 p++;
a71b5a38 413 if (*p == '(')
3030c96e 414 {
a71b5a38
UW
415 const char *ep = strchr (p, ')');
416 if (ep != NULL)
417 {
418 printf_filtered ("Exec file: %.*s\n",
419 (int) (ep - p - 1), p + 1);
420 p = ep + 1;
421 }
3030c96e
UW
422 }
423
424 while (*p && isspace (*p))
425 p++;
426 if (*p)
427 printf_filtered (_("State: %c\n"), *p++);
428
429 if (*p)
430 printf_filtered (_("Parent process: %s\n"),
431 pulongest (strtoulst (p, &p, 10)));
432 if (*p)
433 printf_filtered (_("Process group: %s\n"),
434 pulongest (strtoulst (p, &p, 10)));
435 if (*p)
436 printf_filtered (_("Session id: %s\n"),
437 pulongest (strtoulst (p, &p, 10)));
438 if (*p)
439 printf_filtered (_("TTY: %s\n"),
440 pulongest (strtoulst (p, &p, 10)));
441 if (*p)
442 printf_filtered (_("TTY owner process group: %s\n"),
443 pulongest (strtoulst (p, &p, 10)));
444
445 if (*p)
446 printf_filtered (_("Flags: %s\n"),
447 hex_string (strtoulst (p, &p, 10)));
448 if (*p)
449 printf_filtered (_("Minor faults (no memory page): %s\n"),
450 pulongest (strtoulst (p, &p, 10)));
451 if (*p)
452 printf_filtered (_("Minor faults, children: %s\n"),
453 pulongest (strtoulst (p, &p, 10)));
454 if (*p)
455 printf_filtered (_("Major faults (memory page faults): %s\n"),
456 pulongest (strtoulst (p, &p, 10)));
457 if (*p)
458 printf_filtered (_("Major faults, children: %s\n"),
459 pulongest (strtoulst (p, &p, 10)));
460 if (*p)
461 printf_filtered (_("utime: %s\n"),
462 pulongest (strtoulst (p, &p, 10)));
463 if (*p)
464 printf_filtered (_("stime: %s\n"),
465 pulongest (strtoulst (p, &p, 10)));
466 if (*p)
467 printf_filtered (_("utime, children: %s\n"),
468 pulongest (strtoulst (p, &p, 10)));
469 if (*p)
470 printf_filtered (_("stime, children: %s\n"),
471 pulongest (strtoulst (p, &p, 10)));
472 if (*p)
473 printf_filtered (_("jiffies remaining in current "
474 "time slice: %s\n"),
475 pulongest (strtoulst (p, &p, 10)));
476 if (*p)
477 printf_filtered (_("'nice' value: %s\n"),
478 pulongest (strtoulst (p, &p, 10)));
479 if (*p)
480 printf_filtered (_("jiffies until next timeout: %s\n"),
481 pulongest (strtoulst (p, &p, 10)));
482 if (*p)
483 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
484 pulongest (strtoulst (p, &p, 10)));
485 if (*p)
486 printf_filtered (_("start time (jiffies since "
487 "system boot): %s\n"),
488 pulongest (strtoulst (p, &p, 10)));
489 if (*p)
490 printf_filtered (_("Virtual memory size: %s\n"),
491 pulongest (strtoulst (p, &p, 10)));
492 if (*p)
493 printf_filtered (_("Resident set size: %s\n"),
494 pulongest (strtoulst (p, &p, 10)));
495 if (*p)
496 printf_filtered (_("rlim: %s\n"),
497 pulongest (strtoulst (p, &p, 10)));
498 if (*p)
499 printf_filtered (_("Start of text: %s\n"),
500 hex_string (strtoulst (p, &p, 10)));
501 if (*p)
502 printf_filtered (_("End of text: %s\n"),
503 hex_string (strtoulst (p, &p, 10)));
504 if (*p)
505 printf_filtered (_("Start of stack: %s\n"),
506 hex_string (strtoulst (p, &p, 10)));
507#if 0 /* Don't know how architecture-dependent the rest is...
508 Anyway the signal bitmap info is available from "status". */
509 if (*p)
510 printf_filtered (_("Kernel stack pointer: %s\n"),
511 hex_string (strtoulst (p, &p, 10)));
512 if (*p)
513 printf_filtered (_("Kernel instr pointer: %s\n"),
514 hex_string (strtoulst (p, &p, 10)));
515 if (*p)
516 printf_filtered (_("Pending signals bitmap: %s\n"),
517 hex_string (strtoulst (p, &p, 10)));
518 if (*p)
519 printf_filtered (_("Blocked signals bitmap: %s\n"),
520 hex_string (strtoulst (p, &p, 10)));
521 if (*p)
522 printf_filtered (_("Ignored signals bitmap: %s\n"),
523 hex_string (strtoulst (p, &p, 10)));
524 if (*p)
525 printf_filtered (_("Catched signals bitmap: %s\n"),
526 hex_string (strtoulst (p, &p, 10)));
527 if (*p)
528 printf_filtered (_("wchan (system call): %s\n"),
529 hex_string (strtoulst (p, &p, 10)));
530#endif
531 do_cleanups (cleanup);
532 }
533 else
534 warning (_("unable to open /proc file '%s'"), filename);
535 }
536}
537
451b7c33
TT
538/* Implement "info proc mappings" for a corefile. */
539
540static void
541linux_core_info_proc_mappings (struct gdbarch *gdbarch, char *args)
542{
543 asection *section;
544 ULONGEST count, page_size;
545 unsigned char *descdata, *filenames, *descend, *contents;
546 size_t note_size;
547 unsigned int addr_size_bits, addr_size;
548 struct cleanup *cleanup;
549 struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
550 /* We assume this for reading 64-bit core files. */
551 gdb_static_assert (sizeof (ULONGEST) >= 8);
552
553 section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
554 if (section == NULL)
555 {
556 warning (_("unable to find mappings in core file"));
557 return;
558 }
559
560 addr_size_bits = gdbarch_addr_bit (core_gdbarch);
561 addr_size = addr_size_bits / 8;
562 note_size = bfd_get_section_size (section);
563
564 if (note_size < 2 * addr_size)
565 error (_("malformed core note - too short for header"));
566
567 contents = xmalloc (note_size);
568 cleanup = make_cleanup (xfree, contents);
569 if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
570 error (_("could not get core note contents"));
571
572 descdata = contents;
573 descend = descdata + note_size;
574
575 if (descdata[note_size - 1] != '\0')
576 error (_("malformed note - does not end with \\0"));
577
578 count = bfd_get (addr_size_bits, core_bfd, descdata);
579 descdata += addr_size;
580
581 page_size = bfd_get (addr_size_bits, core_bfd, descdata);
582 descdata += addr_size;
583
584 if (note_size < 2 * addr_size + count * 3 * addr_size)
585 error (_("malformed note - too short for supplied file count"));
586
587 printf_filtered (_("Mapped address spaces:\n\n"));
588 if (gdbarch_addr_bit (gdbarch) == 32)
589 {
590 printf_filtered ("\t%10s %10s %10s %10s %s\n",
591 "Start Addr",
592 " End Addr",
593 " Size", " Offset", "objfile");
594 }
595 else
596 {
597 printf_filtered (" %18s %18s %10s %10s %s\n",
598 "Start Addr",
599 " End Addr",
600 " Size", " Offset", "objfile");
601 }
602
603 filenames = descdata + count * 3 * addr_size;
604 while (--count > 0)
605 {
606 ULONGEST start, end, file_ofs;
607
608 if (filenames == descend)
609 error (_("malformed note - filenames end too early"));
610
611 start = bfd_get (addr_size_bits, core_bfd, descdata);
612 descdata += addr_size;
613 end = bfd_get (addr_size_bits, core_bfd, descdata);
614 descdata += addr_size;
615 file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
616 descdata += addr_size;
617
618 file_ofs *= page_size;
619
620 if (gdbarch_addr_bit (gdbarch) == 32)
621 printf_filtered ("\t%10s %10s %10s %10s %s\n",
622 paddress (gdbarch, start),
623 paddress (gdbarch, end),
624 hex_string (end - start),
625 hex_string (file_ofs),
626 filenames);
627 else
628 printf_filtered (" %18s %18s %10s %10s %s\n",
629 paddress (gdbarch, start),
630 paddress (gdbarch, end),
631 hex_string (end - start),
632 hex_string (file_ofs),
633 filenames);
634
635 filenames += 1 + strlen ((char *) filenames);
636 }
637
638 do_cleanups (cleanup);
639}
640
641/* Implement "info proc" for a corefile. */
642
643static void
644linux_core_info_proc (struct gdbarch *gdbarch, char *args,
645 enum info_proc_what what)
646{
647 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
648 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
649
650 if (exe_f)
651 {
652 const char *exe;
653
654 exe = bfd_core_file_failing_command (core_bfd);
655 if (exe != NULL)
656 printf_filtered ("exe = '%s'\n", exe);
657 else
658 warning (_("unable to find command name in core file"));
659 }
660
661 if (mappings_f)
662 linux_core_info_proc_mappings (gdbarch, args);
663
664 if (!exe_f && !mappings_f)
665 error (_("unable to handle request"));
666}
667
668typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
669 ULONGEST offset, ULONGEST inode,
670 int read, int write,
671 int exec, int modified,
672 const char *filename,
673 void *data);
674
35c2fab7
UW
675/* List memory regions in the inferior for a corefile. */
676
677static int
451b7c33
TT
678linux_find_memory_regions_full (struct gdbarch *gdbarch,
679 linux_find_memory_region_ftype *func,
680 void *obfd)
35c2fab7
UW
681{
682 char filename[100];
683 gdb_byte *data;
684
685 /* We need to know the real target PID to access /proc. */
686 if (current_inferior ()->fake_pid_p)
687 return 1;
688
689 xsnprintf (filename, sizeof filename,
4f69f4c2 690 "/proc/%d/smaps", current_inferior ()->pid);
35c2fab7 691 data = target_fileio_read_stralloc (filename);
4f69f4c2
JK
692 if (data == NULL)
693 {
694 /* Older Linux kernels did not support /proc/PID/smaps. */
695 xsnprintf (filename, sizeof filename,
696 "/proc/%d/maps", current_inferior ()->pid);
697 data = target_fileio_read_stralloc (filename);
698 }
35c2fab7
UW
699 if (data)
700 {
701 struct cleanup *cleanup = make_cleanup (xfree, data);
702 char *line;
703
4f69f4c2
JK
704 line = strtok (data, "\n");
705 while (line)
35c2fab7
UW
706 {
707 ULONGEST addr, endaddr, offset, inode;
708 const char *permissions, *device, *filename;
709 size_t permissions_len, device_len;
710 int read, write, exec;
4f69f4c2 711 int modified = 0, has_anonymous = 0;
35c2fab7
UW
712
713 read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
714 &offset, &device, &device_len, &inode, &filename);
715
716 /* Decode permissions. */
717 read = (memchr (permissions, 'r', permissions_len) != 0);
718 write = (memchr (permissions, 'w', permissions_len) != 0);
719 exec = (memchr (permissions, 'x', permissions_len) != 0);
720
4f69f4c2
JK
721 /* Try to detect if region was modified by parsing smaps counters. */
722 for (line = strtok (NULL, "\n");
723 line && line[0] >= 'A' && line[0] <= 'Z';
724 line = strtok (NULL, "\n"))
725 {
726 char keyword[64 + 1];
727 unsigned long number;
728
729 if (sscanf (line, "%64s%lu kB\n", keyword, &number) != 2)
730 {
731 warning (_("Error parsing {s,}maps file '%s'"), filename);
732 break;
733 }
734 if (strcmp (keyword, "Anonymous:") == 0)
735 has_anonymous = 1;
736 if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
737 || strcmp (keyword, "Private_Dirty:") == 0
738 || strcmp (keyword, "Swap:") == 0
739 || strcmp (keyword, "Anonymous:") == 0))
740 modified = 1;
741 }
742
743 /* Older Linux kernels did not support the "Anonymous:" counter.
744 If it is missing, we can't be sure - dump all the pages. */
745 if (!has_anonymous)
746 modified = 1;
747
35c2fab7 748 /* Invoke the callback function to create the corefile segment. */
451b7c33
TT
749 func (addr, endaddr - addr, offset, inode,
750 read, write, exec, modified, filename, obfd);
35c2fab7
UW
751 }
752
753 do_cleanups (cleanup);
754 return 0;
755 }
756
757 return 1;
758}
759
451b7c33
TT
760/* A structure for passing information through
761 linux_find_memory_regions_full. */
762
763struct linux_find_memory_regions_data
764{
765 /* The original callback. */
766
767 find_memory_region_ftype func;
768
769 /* The original datum. */
770
771 void *obfd;
772};
773
774/* A callback for linux_find_memory_regions that converts between the
775 "full"-style callback and find_memory_region_ftype. */
776
777static int
778linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
779 ULONGEST offset, ULONGEST inode,
780 int read, int write, int exec, int modified,
781 const char *filename, void *arg)
782{
783 struct linux_find_memory_regions_data *data = arg;
784
785 return data->func (vaddr, size, read, write, exec, modified, data->obfd);
786}
787
788/* A variant of linux_find_memory_regions_full that is suitable as the
789 gdbarch find_memory_regions method. */
790
791static int
792linux_find_memory_regions (struct gdbarch *gdbarch,
793 find_memory_region_ftype func, void *obfd)
794{
795 struct linux_find_memory_regions_data data;
796
797 data.func = func;
798 data.obfd = obfd;
799
800 return linux_find_memory_regions_full (gdbarch,
801 linux_find_memory_regions_thunk,
802 &data);
803}
804
6432734d
UW
805/* Determine which signal stopped execution. */
806
807static int
808find_signalled_thread (struct thread_info *info, void *data)
809{
a493e3e2 810 if (info->suspend.stop_signal != GDB_SIGNAL_0
6432734d
UW
811 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
812 return 1;
813
814 return 0;
815}
816
2ea28649 817static enum gdb_signal
6432734d
UW
818find_stop_signal (void)
819{
820 struct thread_info *info =
821 iterate_over_threads (find_signalled_thread, NULL);
822
823 if (info)
824 return info->suspend.stop_signal;
825 else
a493e3e2 826 return GDB_SIGNAL_0;
6432734d
UW
827}
828
829/* Generate corefile notes for SPU contexts. */
830
831static char *
832linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
833{
834 static const char *spu_files[] =
835 {
836 "object-id",
837 "mem",
838 "regs",
839 "fpcr",
840 "lslr",
841 "decr",
842 "decr_status",
843 "signal1",
844 "signal1_type",
845 "signal2",
846 "signal2_type",
847 "event_mask",
848 "event_status",
849 "mbox_info",
850 "ibox_info",
851 "wbox_info",
852 "dma_info",
853 "proxydma_info",
854 };
855
f5656ead 856 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
6432734d
UW
857 gdb_byte *spu_ids;
858 LONGEST i, j, size;
859
860 /* Determine list of SPU ids. */
861 size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
862 NULL, &spu_ids);
863
864 /* Generate corefile notes for each SPU file. */
865 for (i = 0; i < size; i += 4)
866 {
867 int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
868
869 for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
870 {
871 char annex[32], note_name[32];
872 gdb_byte *spu_data;
873 LONGEST spu_len;
874
875 xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
876 spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
877 annex, &spu_data);
878 if (spu_len > 0)
879 {
880 xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
881 note_data = elfcore_write_note (obfd, note_data, note_size,
882 note_name, NT_SPU,
883 spu_data, spu_len);
884 xfree (spu_data);
885
886 if (!note_data)
887 {
888 xfree (spu_ids);
889 return NULL;
890 }
891 }
892 }
893 }
894
895 if (size > 0)
896 xfree (spu_ids);
897
898 return note_data;
899}
900
451b7c33
TT
901/* This is used to pass information from
902 linux_make_mappings_corefile_notes through
903 linux_find_memory_regions_full. */
904
905struct linux_make_mappings_data
906{
907 /* Number of files mapped. */
908 ULONGEST file_count;
909
910 /* The obstack for the main part of the data. */
911 struct obstack *data_obstack;
912
913 /* The filename obstack. */
914 struct obstack *filename_obstack;
915
916 /* The architecture's "long" type. */
917 struct type *long_type;
918};
919
920static linux_find_memory_region_ftype linux_make_mappings_callback;
921
922/* A callback for linux_find_memory_regions_full that updates the
923 mappings data for linux_make_mappings_corefile_notes. */
924
925static int
926linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
927 ULONGEST offset, ULONGEST inode,
928 int read, int write, int exec, int modified,
929 const char *filename, void *data)
930{
931 struct linux_make_mappings_data *map_data = data;
932 gdb_byte buf[sizeof (ULONGEST)];
933
934 if (*filename == '\0' || inode == 0)
935 return 0;
936
937 ++map_data->file_count;
938
939 pack_long (buf, map_data->long_type, vaddr);
940 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
941 pack_long (buf, map_data->long_type, vaddr + size);
942 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
943 pack_long (buf, map_data->long_type, offset);
944 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
945
946 obstack_grow_str0 (map_data->filename_obstack, filename);
947
948 return 0;
949}
950
951/* Write the file mapping data to the core file, if possible. OBFD is
952 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
953 is a pointer to the note size. Returns the new NOTE_DATA and
954 updates NOTE_SIZE. */
955
956static char *
957linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
958 char *note_data, int *note_size)
959{
960 struct cleanup *cleanup;
961 struct obstack data_obstack, filename_obstack;
962 struct linux_make_mappings_data mapping_data;
963 struct type *long_type
964 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
965 gdb_byte buf[sizeof (ULONGEST)];
966
967 obstack_init (&data_obstack);
968 cleanup = make_cleanup_obstack_free (&data_obstack);
969 obstack_init (&filename_obstack);
970 make_cleanup_obstack_free (&filename_obstack);
971
972 mapping_data.file_count = 0;
973 mapping_data.data_obstack = &data_obstack;
974 mapping_data.filename_obstack = &filename_obstack;
975 mapping_data.long_type = long_type;
976
977 /* Reserve space for the count. */
978 obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
979 /* We always write the page size as 1 since we have no good way to
980 determine the correct value. */
981 pack_long (buf, long_type, 1);
982 obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
983
984 linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
985 &mapping_data);
986
987 if (mapping_data.file_count != 0)
988 {
989 /* Write the count to the obstack. */
990 pack_long (obstack_base (&data_obstack), long_type,
991 mapping_data.file_count);
992
993 /* Copy the filenames to the data obstack. */
994 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
995 obstack_object_size (&filename_obstack));
996
997 note_data = elfcore_write_note (obfd, note_data, note_size,
998 "CORE", NT_FILE,
999 obstack_base (&data_obstack),
1000 obstack_object_size (&data_obstack));
1001 }
1002
1003 do_cleanups (cleanup);
1004 return note_data;
1005}
1006
6432734d
UW
1007/* Records the thread's register state for the corefile note
1008 section. */
1009
1010static char *
1011linux_collect_thread_registers (const struct regcache *regcache,
1012 ptid_t ptid, bfd *obfd,
1013 char *note_data, int *note_size,
2ea28649 1014 enum gdb_signal stop_signal)
6432734d
UW
1015{
1016 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1017 struct core_regset_section *sect_list;
1018 unsigned long lwp;
1019
1020 sect_list = gdbarch_core_regset_sections (gdbarch);
1021 gdb_assert (sect_list);
1022
1023 /* For remote targets the LWP may not be available, so use the TID. */
1024 lwp = ptid_get_lwp (ptid);
1025 if (!lwp)
1026 lwp = ptid_get_tid (ptid);
1027
1028 while (sect_list->sect_name != NULL)
1029 {
1030 const struct regset *regset;
1031 char *buf;
1032
1033 regset = gdbarch_regset_from_core_section (gdbarch,
1034 sect_list->sect_name,
1035 sect_list->size);
1036 gdb_assert (regset && regset->collect_regset);
1037
1038 buf = xmalloc (sect_list->size);
1039 regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
1040
1041 /* PRSTATUS still needs to be treated specially. */
1042 if (strcmp (sect_list->sect_name, ".reg") == 0)
1043 note_data = (char *) elfcore_write_prstatus
1044 (obfd, note_data, note_size, lwp,
2ea28649 1045 gdb_signal_to_host (stop_signal), buf);
6432734d
UW
1046 else
1047 note_data = (char *) elfcore_write_register_note
1048 (obfd, note_data, note_size,
1049 sect_list->sect_name, buf, sect_list->size);
1050 xfree (buf);
1051 sect_list++;
1052
1053 if (!note_data)
1054 return NULL;
1055 }
1056
1057 return note_data;
1058}
1059
9015683b
TT
1060/* Fetch the siginfo data for the current thread, if it exists. If
1061 there is no data, or we could not read it, return NULL. Otherwise,
1062 return a newly malloc'd buffer holding the data and fill in *SIZE
1063 with the size of the data. The caller is responsible for freeing
1064 the data. */
1065
1066static gdb_byte *
1067linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
1068{
1069 struct type *siginfo_type;
1070 gdb_byte *buf;
1071 LONGEST bytes_read;
1072 struct cleanup *cleanups;
1073
1074 if (!gdbarch_get_siginfo_type_p (gdbarch))
1075 return NULL;
1076
1077 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1078
1079 buf = xmalloc (TYPE_LENGTH (siginfo_type));
1080 cleanups = make_cleanup (xfree, buf);
1081
1082 bytes_read = target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
1083 buf, 0, TYPE_LENGTH (siginfo_type));
1084 if (bytes_read == TYPE_LENGTH (siginfo_type))
1085 {
1086 discard_cleanups (cleanups);
1087 *size = bytes_read;
1088 }
1089 else
1090 {
1091 do_cleanups (cleanups);
1092 buf = NULL;
1093 }
1094
1095 return buf;
1096}
1097
6432734d
UW
1098struct linux_corefile_thread_data
1099{
1100 struct gdbarch *gdbarch;
1101 int pid;
1102 bfd *obfd;
1103 char *note_data;
1104 int *note_size;
1105 int num_notes;
2ea28649 1106 enum gdb_signal stop_signal;
6432734d
UW
1107 linux_collect_thread_registers_ftype collect;
1108};
1109
1110/* Called by gdbthread.c once per thread. Records the thread's
1111 register state for the corefile note section. */
1112
1113static int
1114linux_corefile_thread_callback (struct thread_info *info, void *data)
1115{
1116 struct linux_corefile_thread_data *args = data;
1117
1118 if (ptid_get_pid (info->ptid) == args->pid)
1119 {
1120 struct cleanup *old_chain;
1121 struct regcache *regcache;
9015683b
TT
1122 gdb_byte *siginfo_data;
1123 LONGEST siginfo_size;
1124
6432734d
UW
1125 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
1126
1127 old_chain = save_inferior_ptid ();
1128 inferior_ptid = info->ptid;
1129 target_fetch_registers (regcache, -1);
9015683b 1130 siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
6432734d
UW
1131 do_cleanups (old_chain);
1132
9015683b
TT
1133 old_chain = make_cleanup (xfree, siginfo_data);
1134
6432734d
UW
1135 args->note_data = args->collect (regcache, info->ptid, args->obfd,
1136 args->note_data, args->note_size,
1137 args->stop_signal);
1138 args->num_notes++;
9015683b
TT
1139
1140 if (siginfo_data != NULL)
1141 {
1142 args->note_data = elfcore_write_note (args->obfd,
1143 args->note_data,
1144 args->note_size,
1145 "CORE", NT_SIGINFO,
1146 siginfo_data, siginfo_size);
1147 args->num_notes++;
1148 }
1149
1150 do_cleanups (old_chain);
6432734d
UW
1151 }
1152
1153 return !args->note_data;
1154}
1155
1156/* Fills the "to_make_corefile_note" target vector. Builds the note
1157 section for a corefile, and returns it in a malloc buffer. */
1158
1159char *
1160linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
1161 linux_collect_thread_registers_ftype collect)
1162{
1163 struct linux_corefile_thread_data thread_args;
1164 char *note_data = NULL;
1165 gdb_byte *auxv;
1166 int auxv_len;
1167
1168 /* Process information. */
1169 if (get_exec_file (0))
1170 {
1171 const char *fname = lbasename (get_exec_file (0));
1172 char *psargs = xstrdup (fname);
1173
1174 if (get_inferior_args ())
1175 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
1176 (char *) NULL);
1177
1178 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
1179 fname, psargs);
1180 xfree (psargs);
1181
1182 if (!note_data)
1183 return NULL;
1184 }
1185
1186 /* Thread register information. */
1187 thread_args.gdbarch = gdbarch;
1188 thread_args.pid = ptid_get_pid (inferior_ptid);
1189 thread_args.obfd = obfd;
1190 thread_args.note_data = note_data;
1191 thread_args.note_size = note_size;
1192 thread_args.num_notes = 0;
1193 thread_args.stop_signal = find_stop_signal ();
1194 thread_args.collect = collect;
1195 iterate_over_threads (linux_corefile_thread_callback, &thread_args);
1196 note_data = thread_args.note_data;
1197 if (!note_data)
1198 return NULL;
1199
1200 /* Auxillary vector. */
1201 auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
1202 NULL, &auxv);
1203 if (auxv_len > 0)
1204 {
1205 note_data = elfcore_write_note (obfd, note_data, note_size,
1206 "CORE", NT_AUXV, auxv, auxv_len);
1207 xfree (auxv);
1208
1209 if (!note_data)
1210 return NULL;
1211 }
1212
1213 /* SPU information. */
1214 note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
1215 if (!note_data)
1216 return NULL;
1217
451b7c33
TT
1218 /* File mappings. */
1219 note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
1220 note_data, note_size);
1221
6432734d
UW
1222 make_cleanup (xfree, note_data);
1223 return note_data;
1224}
1225
1226static char *
1227linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1228{
1229 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
1230 converted to gdbarch_core_regset_sections, we no longer need to fall back
1231 to the target method at this point. */
1232
1233 if (!gdbarch_core_regset_sections (gdbarch))
1234 return target_make_corefile_notes (obfd, note_size);
1235 else
1236 return linux_make_corefile_notes (gdbarch, obfd, note_size,
1237 linux_collect_thread_registers);
1238}
1239
a5ee0f0c
PA
1240/* To be called from the various GDB_OSABI_LINUX handlers for the
1241 various GNU/Linux architectures and machine types. */
1242
1243void
1244linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1245{
1246 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
3030c96e 1247 set_gdbarch_info_proc (gdbarch, linux_info_proc);
451b7c33 1248 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
35c2fab7 1249 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
6432734d 1250 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
33fbcbee
PA
1251 set_gdbarch_has_shared_address_space (gdbarch,
1252 linux_has_shared_address_space);
a5ee0f0c 1253}
06253dd3 1254
70221824
PA
1255/* Provide a prototype to silence -Wmissing-prototypes. */
1256extern initialize_file_ftype _initialize_linux_tdep;
1257
06253dd3
JK
1258void
1259_initialize_linux_tdep (void)
1260{
1261 linux_gdbarch_data_handle =
1262 gdbarch_data_register_post_init (init_linux_gdbarch_data);
1263}
This page took 0.419397 seconds and 4 git commands to generate.