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