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