2012-03-01 Pedro Alves <palves@redhat.com>
[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
UW
32#include "cli/cli-utils.h"
33
34#include <ctype.h>
4aa995e1 35
06253dd3
JK
36static struct gdbarch_data *linux_gdbarch_data_handle;
37
38struct linux_gdbarch_data
39 {
40 struct type *siginfo_type;
41 };
42
43static void *
44init_linux_gdbarch_data (struct gdbarch *gdbarch)
45{
46 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
47}
48
49static struct linux_gdbarch_data *
50get_linux_gdbarch_data (struct gdbarch *gdbarch)
51{
52 return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
53}
54
4aa995e1
PA
55/* This function is suitable for architectures that don't
56 extend/override the standard siginfo structure. */
57
58struct type *
59linux_get_siginfo_type (struct gdbarch *gdbarch)
60{
06253dd3 61 struct linux_gdbarch_data *linux_gdbarch_data;
4aa995e1
PA
62 struct type *int_type, *uint_type, *long_type, *void_ptr_type;
63 struct type *uid_type, *pid_type;
64 struct type *sigval_type, *clock_type;
65 struct type *siginfo_type, *sifields_type;
66 struct type *type;
67
06253dd3
JK
68 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
69 if (linux_gdbarch_data->siginfo_type != NULL)
70 return linux_gdbarch_data->siginfo_type;
71
e9bb382b
UW
72 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
73 0, "int");
74 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
75 1, "unsigned int");
76 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
77 0, "long");
4aa995e1
PA
78 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
79
80 /* sival_t */
e9bb382b 81 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
4aa995e1
PA
82 TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
83 append_composite_type_field (sigval_type, "sival_int", int_type);
84 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
85
86 /* __pid_t */
e3aa49af
MS
87 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
88 TYPE_LENGTH (int_type), "__pid_t");
4aa995e1 89 TYPE_TARGET_TYPE (pid_type) = int_type;
e9bb382b 90 TYPE_TARGET_STUB (pid_type) = 1;
4aa995e1
PA
91
92 /* __uid_t */
e3aa49af
MS
93 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
94 TYPE_LENGTH (uint_type), "__uid_t");
4aa995e1 95 TYPE_TARGET_TYPE (uid_type) = uint_type;
e9bb382b 96 TYPE_TARGET_STUB (uid_type) = 1;
4aa995e1
PA
97
98 /* __clock_t */
e3aa49af
MS
99 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
100 TYPE_LENGTH (long_type), "__clock_t");
4aa995e1 101 TYPE_TARGET_TYPE (clock_type) = long_type;
e9bb382b 102 TYPE_TARGET_STUB (clock_type) = 1;
4aa995e1
PA
103
104 /* _sifields */
e9bb382b 105 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
4aa995e1
PA
106
107 {
108 const int si_max_size = 128;
109 int si_pad_size;
110 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
111
112 /* _pad */
113 if (gdbarch_ptr_bit (gdbarch) == 64)
114 si_pad_size = (si_max_size / size_of_int) - 4;
115 else
116 si_pad_size = (si_max_size / size_of_int) - 3;
117 append_composite_type_field (sifields_type, "_pad",
118 init_vector_type (int_type, si_pad_size));
119 }
120
121 /* _kill */
e9bb382b 122 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
123 append_composite_type_field (type, "si_pid", pid_type);
124 append_composite_type_field (type, "si_uid", uid_type);
125 append_composite_type_field (sifields_type, "_kill", type);
126
127 /* _timer */
e9bb382b 128 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
129 append_composite_type_field (type, "si_tid", int_type);
130 append_composite_type_field (type, "si_overrun", int_type);
131 append_composite_type_field (type, "si_sigval", sigval_type);
132 append_composite_type_field (sifields_type, "_timer", type);
133
134 /* _rt */
e9bb382b 135 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
136 append_composite_type_field (type, "si_pid", pid_type);
137 append_composite_type_field (type, "si_uid", uid_type);
138 append_composite_type_field (type, "si_sigval", sigval_type);
139 append_composite_type_field (sifields_type, "_rt", type);
140
141 /* _sigchld */
e9bb382b 142 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
143 append_composite_type_field (type, "si_pid", pid_type);
144 append_composite_type_field (type, "si_uid", uid_type);
145 append_composite_type_field (type, "si_status", int_type);
146 append_composite_type_field (type, "si_utime", clock_type);
147 append_composite_type_field (type, "si_stime", clock_type);
148 append_composite_type_field (sifields_type, "_sigchld", type);
149
150 /* _sigfault */
e9bb382b 151 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
152 append_composite_type_field (type, "si_addr", void_ptr_type);
153 append_composite_type_field (sifields_type, "_sigfault", type);
154
155 /* _sigpoll */
e9bb382b 156 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
157 append_composite_type_field (type, "si_band", long_type);
158 append_composite_type_field (type, "si_fd", int_type);
159 append_composite_type_field (sifields_type, "_sigpoll", type);
160
161 /* struct siginfo */
e9bb382b 162 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
4aa995e1
PA
163 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
164 append_composite_type_field (siginfo_type, "si_signo", int_type);
165 append_composite_type_field (siginfo_type, "si_errno", int_type);
166 append_composite_type_field (siginfo_type, "si_code", int_type);
167 append_composite_type_field_aligned (siginfo_type,
168 "_sifields", sifields_type,
169 TYPE_LENGTH (long_type));
170
06253dd3
JK
171 linux_gdbarch_data->siginfo_type = siginfo_type;
172
4aa995e1
PA
173 return siginfo_type;
174}
6b3ae818 175
33fbcbee
PA
176static int
177linux_has_shared_address_space (struct gdbarch *gdbarch)
6c95b8df
PA
178{
179 /* Determine whether we are running on uClinux or normal Linux
180 kernel. */
181 CORE_ADDR dummy;
182 int target_is_uclinux;
183
184 target_is_uclinux
185 = (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
186 && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
187
188 return target_is_uclinux;
189}
a5ee0f0c
PA
190
191/* This is how we want PTIDs from core files to be printed. */
192
193static char *
194linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
195{
196 static char buf[80];
197
198 if (ptid_get_lwp (ptid) != 0)
199 {
200 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
201 return buf;
202 }
203
204 return normal_pid_to_str (ptid);
205}
206
3030c96e
UW
207/* Service function for corefiles and info proc. */
208
209static void
210read_mapping (const char *line,
211 ULONGEST *addr, ULONGEST *endaddr,
212 const char **permissions, size_t *permissions_len,
213 ULONGEST *offset,
214 const char **device, size_t *device_len,
215 ULONGEST *inode,
216 const char **filename)
217{
218 const char *p = line;
219
220 *addr = strtoulst (p, &p, 16);
221 if (*p == '-')
222 p++;
223 *endaddr = strtoulst (p, &p, 16);
224
225 while (*p && isspace (*p))
226 p++;
227 *permissions = p;
228 while (*p && !isspace (*p))
229 p++;
230 *permissions_len = p - *permissions;
231
232 *offset = strtoulst (p, &p, 16);
233
234 while (*p && isspace (*p))
235 p++;
236 *device = p;
237 while (*p && !isspace (*p))
238 p++;
239 *device_len = p - *device;
240
241 *inode = strtoulst (p, &p, 10);
242
243 while (*p && isspace (*p))
244 p++;
245 *filename = p;
246}
247
248/* Implement the "info proc" command. */
249
250static void
251linux_info_proc (struct gdbarch *gdbarch, char *args,
252 enum info_proc_what what)
253{
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. */
256 long pid;
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);
263 char filename[100];
264 gdb_byte *data;
265 int target_errno;
266
267 if (args && isdigit (args[0]))
268 pid = strtoul (args, &args, 10);
269 else
270 {
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."));
275
276 pid = current_inferior ()->pid;
277 }
278
279 args = skip_spaces (args);
280 if (args && args[0])
281 error (_("Too many parameters: %s"), args);
282
283 printf_filtered (_("process %ld\n"), pid);
284 if (cmdline_f)
285 {
286 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
287 data = target_fileio_read_stralloc (filename);
288 if (data)
289 {
290 struct cleanup *cleanup = make_cleanup (xfree, data);
291 printf_filtered ("cmdline = '%s'\n", data);
292 do_cleanups (cleanup);
293 }
294 else
295 warning (_("unable to open /proc file '%s'"), filename);
296 }
297 if (cwd_f)
298 {
299 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
300 data = target_fileio_readlink (filename, &target_errno);
301 if (data)
302 {
303 struct cleanup *cleanup = make_cleanup (xfree, data);
304 printf_filtered ("cwd = '%s'\n", data);
305 do_cleanups (cleanup);
306 }
307 else
308 warning (_("unable to read link '%s'"), filename);
309 }
310 if (exe_f)
311 {
312 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
313 data = target_fileio_readlink (filename, &target_errno);
314 if (data)
315 {
316 struct cleanup *cleanup = make_cleanup (xfree, data);
317 printf_filtered ("exe = '%s'\n", data);
318 do_cleanups (cleanup);
319 }
320 else
321 warning (_("unable to read link '%s'"), filename);
322 }
323 if (mappings_f)
324 {
325 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
326 data = target_fileio_read_stralloc (filename);
327 if (data)
328 {
329 struct cleanup *cleanup = make_cleanup (xfree, data);
330 char *line;
331
332 printf_filtered (_("Mapped address spaces:\n\n"));
333 if (gdbarch_addr_bit (gdbarch) == 32)
334 {
335 printf_filtered ("\t%10s %10s %10s %10s %s\n",
336 "Start Addr",
337 " End Addr",
338 " Size", " Offset", "objfile");
339 }
340 else
341 {
342 printf_filtered (" %18s %18s %10s %10s %s\n",
343 "Start Addr",
344 " End Addr",
345 " Size", " Offset", "objfile");
346 }
347
348 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
349 {
350 ULONGEST addr, endaddr, offset, inode;
351 const char *permissions, *device, *filename;
352 size_t permissions_len, device_len;
353
354 read_mapping (line, &addr, &endaddr,
355 &permissions, &permissions_len,
356 &offset, &device, &device_len,
357 &inode, &filename);
358
359 if (gdbarch_addr_bit (gdbarch) == 32)
360 {
361 printf_filtered ("\t%10s %10s %10s %10s %s\n",
362 paddress (gdbarch, addr),
363 paddress (gdbarch, endaddr),
364 hex_string (endaddr - addr),
365 hex_string (offset),
366 *filename? filename : "");
367 }
368 else
369 {
370 printf_filtered (" %18s %18s %10s %10s %s\n",
371 paddress (gdbarch, addr),
372 paddress (gdbarch, endaddr),
373 hex_string (endaddr - addr),
374 hex_string (offset),
375 *filename? filename : "");
376 }
377 }
378
379 do_cleanups (cleanup);
380 }
381 else
382 warning (_("unable to open /proc file '%s'"), filename);
383 }
384 if (status_f)
385 {
386 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
387 data = target_fileio_read_stralloc (filename);
388 if (data)
389 {
390 struct cleanup *cleanup = make_cleanup (xfree, data);
391 puts_filtered (data);
392 do_cleanups (cleanup);
393 }
394 else
395 warning (_("unable to open /proc file '%s'"), filename);
396 }
397 if (stat_f)
398 {
399 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
400 data = target_fileio_read_stralloc (filename);
401 if (data)
402 {
403 struct cleanup *cleanup = make_cleanup (xfree, data);
404 const char *p = data;
3030c96e
UW
405 ULONGEST val;
406
407 printf_filtered (_("Process: %s\n"),
408 pulongest (strtoulst (p, &p, 10)));
409
410 while (*p && isspace (*p))
411 p++;
a71b5a38 412 if (*p == '(')
3030c96e 413 {
a71b5a38
UW
414 const char *ep = strchr (p, ')');
415 if (ep != NULL)
416 {
417 printf_filtered ("Exec file: %.*s\n",
418 (int) (ep - p - 1), p + 1);
419 p = ep + 1;
420 }
3030c96e
UW
421 }
422
423 while (*p && isspace (*p))
424 p++;
425 if (*p)
426 printf_filtered (_("State: %c\n"), *p++);
427
428 if (*p)
429 printf_filtered (_("Parent process: %s\n"),
430 pulongest (strtoulst (p, &p, 10)));
431 if (*p)
432 printf_filtered (_("Process group: %s\n"),
433 pulongest (strtoulst (p, &p, 10)));
434 if (*p)
435 printf_filtered (_("Session id: %s\n"),
436 pulongest (strtoulst (p, &p, 10)));
437 if (*p)
438 printf_filtered (_("TTY: %s\n"),
439 pulongest (strtoulst (p, &p, 10)));
440 if (*p)
441 printf_filtered (_("TTY owner process group: %s\n"),
442 pulongest (strtoulst (p, &p, 10)));
443
444 if (*p)
445 printf_filtered (_("Flags: %s\n"),
446 hex_string (strtoulst (p, &p, 10)));
447 if (*p)
448 printf_filtered (_("Minor faults (no memory page): %s\n"),
449 pulongest (strtoulst (p, &p, 10)));
450 if (*p)
451 printf_filtered (_("Minor faults, children: %s\n"),
452 pulongest (strtoulst (p, &p, 10)));
453 if (*p)
454 printf_filtered (_("Major faults (memory page faults): %s\n"),
455 pulongest (strtoulst (p, &p, 10)));
456 if (*p)
457 printf_filtered (_("Major faults, children: %s\n"),
458 pulongest (strtoulst (p, &p, 10)));
459 if (*p)
460 printf_filtered (_("utime: %s\n"),
461 pulongest (strtoulst (p, &p, 10)));
462 if (*p)
463 printf_filtered (_("stime: %s\n"),
464 pulongest (strtoulst (p, &p, 10)));
465 if (*p)
466 printf_filtered (_("utime, children: %s\n"),
467 pulongest (strtoulst (p, &p, 10)));
468 if (*p)
469 printf_filtered (_("stime, children: %s\n"),
470 pulongest (strtoulst (p, &p, 10)));
471 if (*p)
472 printf_filtered (_("jiffies remaining in current "
473 "time slice: %s\n"),
474 pulongest (strtoulst (p, &p, 10)));
475 if (*p)
476 printf_filtered (_("'nice' value: %s\n"),
477 pulongest (strtoulst (p, &p, 10)));
478 if (*p)
479 printf_filtered (_("jiffies until next timeout: %s\n"),
480 pulongest (strtoulst (p, &p, 10)));
481 if (*p)
482 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
483 pulongest (strtoulst (p, &p, 10)));
484 if (*p)
485 printf_filtered (_("start time (jiffies since "
486 "system boot): %s\n"),
487 pulongest (strtoulst (p, &p, 10)));
488 if (*p)
489 printf_filtered (_("Virtual memory size: %s\n"),
490 pulongest (strtoulst (p, &p, 10)));
491 if (*p)
492 printf_filtered (_("Resident set size: %s\n"),
493 pulongest (strtoulst (p, &p, 10)));
494 if (*p)
495 printf_filtered (_("rlim: %s\n"),
496 pulongest (strtoulst (p, &p, 10)));
497 if (*p)
498 printf_filtered (_("Start of text: %s\n"),
499 hex_string (strtoulst (p, &p, 10)));
500 if (*p)
501 printf_filtered (_("End of text: %s\n"),
502 hex_string (strtoulst (p, &p, 10)));
503 if (*p)
504 printf_filtered (_("Start of stack: %s\n"),
505 hex_string (strtoulst (p, &p, 10)));
506#if 0 /* Don't know how architecture-dependent the rest is...
507 Anyway the signal bitmap info is available from "status". */
508 if (*p)
509 printf_filtered (_("Kernel stack pointer: %s\n"),
510 hex_string (strtoulst (p, &p, 10)));
511 if (*p)
512 printf_filtered (_("Kernel instr pointer: %s\n"),
513 hex_string (strtoulst (p, &p, 10)));
514 if (*p)
515 printf_filtered (_("Pending signals bitmap: %s\n"),
516 hex_string (strtoulst (p, &p, 10)));
517 if (*p)
518 printf_filtered (_("Blocked signals bitmap: %s\n"),
519 hex_string (strtoulst (p, &p, 10)));
520 if (*p)
521 printf_filtered (_("Ignored signals bitmap: %s\n"),
522 hex_string (strtoulst (p, &p, 10)));
523 if (*p)
524 printf_filtered (_("Catched signals bitmap: %s\n"),
525 hex_string (strtoulst (p, &p, 10)));
526 if (*p)
527 printf_filtered (_("wchan (system call): %s\n"),
528 hex_string (strtoulst (p, &p, 10)));
529#endif
530 do_cleanups (cleanup);
531 }
532 else
533 warning (_("unable to open /proc file '%s'"), filename);
534 }
535}
536
35c2fab7
UW
537/* List memory regions in the inferior for a corefile. */
538
539static int
540linux_find_memory_regions (struct gdbarch *gdbarch,
541 find_memory_region_ftype func, void *obfd)
542{
543 char filename[100];
544 gdb_byte *data;
545
546 /* We need to know the real target PID to access /proc. */
547 if (current_inferior ()->fake_pid_p)
548 return 1;
549
550 xsnprintf (filename, sizeof filename,
551 "/proc/%d/maps", current_inferior ()->pid);
552 data = target_fileio_read_stralloc (filename);
553 if (data)
554 {
555 struct cleanup *cleanup = make_cleanup (xfree, data);
556 char *line;
557
558 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
559 {
560 ULONGEST addr, endaddr, offset, inode;
561 const char *permissions, *device, *filename;
562 size_t permissions_len, device_len;
563 int read, write, exec;
564
565 read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
566 &offset, &device, &device_len, &inode, &filename);
567
568 /* Decode permissions. */
569 read = (memchr (permissions, 'r', permissions_len) != 0);
570 write = (memchr (permissions, 'w', permissions_len) != 0);
571 exec = (memchr (permissions, 'x', permissions_len) != 0);
572
573 /* Invoke the callback function to create the corefile segment. */
574 func (addr, endaddr - addr, read, write, exec, obfd);
575 }
576
577 do_cleanups (cleanup);
578 return 0;
579 }
580
581 return 1;
582}
583
6432734d
UW
584/* Determine which signal stopped execution. */
585
586static int
587find_signalled_thread (struct thread_info *info, void *data)
588{
589 if (info->suspend.stop_signal != TARGET_SIGNAL_0
590 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
591 return 1;
592
593 return 0;
594}
595
596static enum target_signal
597find_stop_signal (void)
598{
599 struct thread_info *info =
600 iterate_over_threads (find_signalled_thread, NULL);
601
602 if (info)
603 return info->suspend.stop_signal;
604 else
605 return TARGET_SIGNAL_0;
606}
607
608/* Generate corefile notes for SPU contexts. */
609
610static char *
611linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
612{
613 static const char *spu_files[] =
614 {
615 "object-id",
616 "mem",
617 "regs",
618 "fpcr",
619 "lslr",
620 "decr",
621 "decr_status",
622 "signal1",
623 "signal1_type",
624 "signal2",
625 "signal2_type",
626 "event_mask",
627 "event_status",
628 "mbox_info",
629 "ibox_info",
630 "wbox_info",
631 "dma_info",
632 "proxydma_info",
633 };
634
635 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
636 gdb_byte *spu_ids;
637 LONGEST i, j, size;
638
639 /* Determine list of SPU ids. */
640 size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
641 NULL, &spu_ids);
642
643 /* Generate corefile notes for each SPU file. */
644 for (i = 0; i < size; i += 4)
645 {
646 int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
647
648 for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
649 {
650 char annex[32], note_name[32];
651 gdb_byte *spu_data;
652 LONGEST spu_len;
653
654 xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
655 spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
656 annex, &spu_data);
657 if (spu_len > 0)
658 {
659 xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
660 note_data = elfcore_write_note (obfd, note_data, note_size,
661 note_name, NT_SPU,
662 spu_data, spu_len);
663 xfree (spu_data);
664
665 if (!note_data)
666 {
667 xfree (spu_ids);
668 return NULL;
669 }
670 }
671 }
672 }
673
674 if (size > 0)
675 xfree (spu_ids);
676
677 return note_data;
678}
679
680/* Records the thread's register state for the corefile note
681 section. */
682
683static char *
684linux_collect_thread_registers (const struct regcache *regcache,
685 ptid_t ptid, bfd *obfd,
686 char *note_data, int *note_size,
687 enum target_signal stop_signal)
688{
689 struct gdbarch *gdbarch = get_regcache_arch (regcache);
690 struct core_regset_section *sect_list;
691 unsigned long lwp;
692
693 sect_list = gdbarch_core_regset_sections (gdbarch);
694 gdb_assert (sect_list);
695
696 /* For remote targets the LWP may not be available, so use the TID. */
697 lwp = ptid_get_lwp (ptid);
698 if (!lwp)
699 lwp = ptid_get_tid (ptid);
700
701 while (sect_list->sect_name != NULL)
702 {
703 const struct regset *regset;
704 char *buf;
705
706 regset = gdbarch_regset_from_core_section (gdbarch,
707 sect_list->sect_name,
708 sect_list->size);
709 gdb_assert (regset && regset->collect_regset);
710
711 buf = xmalloc (sect_list->size);
712 regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
713
714 /* PRSTATUS still needs to be treated specially. */
715 if (strcmp (sect_list->sect_name, ".reg") == 0)
716 note_data = (char *) elfcore_write_prstatus
717 (obfd, note_data, note_size, lwp,
718 target_signal_to_host (stop_signal), buf);
719 else
720 note_data = (char *) elfcore_write_register_note
721 (obfd, note_data, note_size,
722 sect_list->sect_name, buf, sect_list->size);
723 xfree (buf);
724 sect_list++;
725
726 if (!note_data)
727 return NULL;
728 }
729
730 return note_data;
731}
732
733struct linux_corefile_thread_data
734{
735 struct gdbarch *gdbarch;
736 int pid;
737 bfd *obfd;
738 char *note_data;
739 int *note_size;
740 int num_notes;
741 enum target_signal stop_signal;
742 linux_collect_thread_registers_ftype collect;
743};
744
745/* Called by gdbthread.c once per thread. Records the thread's
746 register state for the corefile note section. */
747
748static int
749linux_corefile_thread_callback (struct thread_info *info, void *data)
750{
751 struct linux_corefile_thread_data *args = data;
752
753 if (ptid_get_pid (info->ptid) == args->pid)
754 {
755 struct cleanup *old_chain;
756 struct regcache *regcache;
757 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
758
759 old_chain = save_inferior_ptid ();
760 inferior_ptid = info->ptid;
761 target_fetch_registers (regcache, -1);
762 do_cleanups (old_chain);
763
764 args->note_data = args->collect (regcache, info->ptid, args->obfd,
765 args->note_data, args->note_size,
766 args->stop_signal);
767 args->num_notes++;
768 }
769
770 return !args->note_data;
771}
772
773/* Fills the "to_make_corefile_note" target vector. Builds the note
774 section for a corefile, and returns it in a malloc buffer. */
775
776char *
777linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
778 linux_collect_thread_registers_ftype collect)
779{
780 struct linux_corefile_thread_data thread_args;
781 char *note_data = NULL;
782 gdb_byte *auxv;
783 int auxv_len;
784
785 /* Process information. */
786 if (get_exec_file (0))
787 {
788 const char *fname = lbasename (get_exec_file (0));
789 char *psargs = xstrdup (fname);
790
791 if (get_inferior_args ())
792 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
793 (char *) NULL);
794
795 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
796 fname, psargs);
797 xfree (psargs);
798
799 if (!note_data)
800 return NULL;
801 }
802
803 /* Thread register information. */
804 thread_args.gdbarch = gdbarch;
805 thread_args.pid = ptid_get_pid (inferior_ptid);
806 thread_args.obfd = obfd;
807 thread_args.note_data = note_data;
808 thread_args.note_size = note_size;
809 thread_args.num_notes = 0;
810 thread_args.stop_signal = find_stop_signal ();
811 thread_args.collect = collect;
812 iterate_over_threads (linux_corefile_thread_callback, &thread_args);
813 note_data = thread_args.note_data;
814 if (!note_data)
815 return NULL;
816
817 /* Auxillary vector. */
818 auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
819 NULL, &auxv);
820 if (auxv_len > 0)
821 {
822 note_data = elfcore_write_note (obfd, note_data, note_size,
823 "CORE", NT_AUXV, auxv, auxv_len);
824 xfree (auxv);
825
826 if (!note_data)
827 return NULL;
828 }
829
830 /* SPU information. */
831 note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
832 if (!note_data)
833 return NULL;
834
835 make_cleanup (xfree, note_data);
836 return note_data;
837}
838
839static char *
840linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
841{
842 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
843 converted to gdbarch_core_regset_sections, we no longer need to fall back
844 to the target method at this point. */
845
846 if (!gdbarch_core_regset_sections (gdbarch))
847 return target_make_corefile_notes (obfd, note_size);
848 else
849 return linux_make_corefile_notes (gdbarch, obfd, note_size,
850 linux_collect_thread_registers);
851}
852
a5ee0f0c
PA
853/* To be called from the various GDB_OSABI_LINUX handlers for the
854 various GNU/Linux architectures and machine types. */
855
856void
857linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
858{
859 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
3030c96e 860 set_gdbarch_info_proc (gdbarch, linux_info_proc);
35c2fab7 861 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
6432734d 862 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
33fbcbee
PA
863 set_gdbarch_has_shared_address_space (gdbarch,
864 linux_has_shared_address_space);
a5ee0f0c 865}
06253dd3
JK
866
867void
868_initialize_linux_tdep (void)
869{
870 linux_gdbarch_data_handle =
871 gdbarch_data_register_post_init (init_linux_gdbarch_data);
872}
This page took 0.344672 seconds and 4 git commands to generate.