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